VTK  9.2.6
TestMotionFXCFGReaderCommon.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: TestMotionFXCFGReaderCommon.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #ifndef TestMotionFXCFGReaderCommon_h
16 #define TestMotionFXCFGReaderCommon_h
17 
18 #include <vtkActor.h>
19 #include <vtkCallbackCommand.h>
21 #include <vtkInformation.h>
22 #include <vtkMotionFXCFGReader.h>
23 #include <vtkNew.h>
24 #include <vtkRenderWindow.h>
26 #include <vtkRenderer.h>
27 #include <vtkSmartPointer.h>
29 #include <vtkTestUtilities.h>
30 #include <vtkTesting.h>
31 
32 #include <algorithm>
33 #include <cassert>
34 #include <vector>
35 
36 namespace impl
37 {
38 
39 struct ClientData
40 {
44  std::vector<double> TimeSteps;
46 
47  void GoToNext()
48  {
49  cout << "Go to next" << endl;
50  this->CurrentIndex =
51  std::min(static_cast<int>(this->TimeSteps.size()) - 1, this->CurrentIndex + 1);
52  this->Render();
53  }
54 
55  void GoToPrev()
56  {
57  cout << "Go to prev" << endl;
58  this->CurrentIndex = std::max(0, this->CurrentIndex - 1);
59  this->Render();
60  }
61 
62  void Play()
63  {
64  cout << "Playing";
65  for (size_t cc = 0; cc < this->TimeSteps.size(); ++cc)
66  {
67  cout << ".";
68  cout.flush();
69  this->CurrentIndex = static_cast<int>(cc);
70  this->Render();
71  }
72  cout << endl;
73  }
74 
75  void Render()
76  {
77  assert(
78  this->CurrentIndex >= 0 && this->CurrentIndex < static_cast<int>(this->TimeSteps.size()));
79  this->Reader->UpdateTimeStep(this->TimeSteps[this->CurrentIndex]);
80  this->Mapper->SetInputDataObject(this->Reader->GetOutputDataObject(0));
81  this->Window->Render();
82  }
83 };
84 
85 static void CharEventCallback(vtkObject* caller, unsigned long, void* clientdata, void*)
86 {
87  ClientData& data = *reinterpret_cast<ClientData*>(clientdata);
88  auto iren = vtkRenderWindowInteractor::SafeDownCast(caller);
89  switch (iren->GetKeyCode())
90  {
91  case 'x':
92  case 'X':
93  data.GoToNext();
94  break;
95 
96  case 'z':
97  case 'Z':
98  data.GoToPrev();
99  break;
100 
101  case 'c':
102  case 'C':
103  data.Play();
104  break;
105  }
106 }
107 
108 template <typename InitializationCallback>
109 int Test(int argc, char* argv[], const char* dfile, const InitializationCallback& initCallback)
110 {
112  char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, dfile);
113  reader->SetFileName(fname);
114  delete[] fname;
115 
116  reader->SetTimeResolution(100);
117  reader->UpdateInformation();
118 
120  vtkInformation* outInfo = reader->GetOutputInformation(0);
121  const int numTimeSteps = outInfo->Length(SDDP::TIME_STEPS());
122 
123  if (numTimeSteps != 100)
124  {
125  cerr << "ERROR: missing timesteps. Potential issue reading the CFG file." << endl;
126  return EXIT_FAILURE;
127  }
128 
130 
131  vtkNew<vtkRenderer> renderer;
132  renWin->AddRenderer(renderer);
133 
135  iren->SetRenderWindow(renWin);
136 
138  mapper->SetInputConnection(reader->GetOutputPort());
139 
140  vtkNew<vtkActor> actor;
141  actor->SetMapper(mapper);
142  renderer->AddActor(actor);
143 
144  initCallback(renWin, renderer, reader);
145 
146  std::vector<double> ts(numTimeSteps);
147  outInfo->Get(SDDP::TIME_STEPS(), &ts[0]);
148 
149  // for baseline comparison, we'll jump to the middle of the
150  // time sequence and do a capture.
151  reader->UpdateTimeStep(ts[numTimeSteps / 2]);
152  mapper->SetInputDataObject(reader->GetOutputDataObject(0));
153  renWin->Render();
154 
155  const int retVal = vtkTesting::Test(argc, argv, renWin, 10);
156  if (retVal == vtkTesting::DO_INTERACTOR)
157  {
159  data.Window = renWin;
160  data.Reader = reader;
161  data.Mapper = mapper;
162  data.TimeSteps = ts;
163  data.CurrentIndex = numTimeSteps / 2;
164 
166  observer->SetClientData(&data);
167  observer->SetCallback(&CharEventCallback);
168  iren->AddObserver(vtkCommand::CharEvent, observer);
169 
170  cout << "Entering interactive mode......" << endl
171  << "Supported operations:" << endl
172  << " 'z' or 'Z' : go to next time step" << endl
173  << " 'x' or 'X' : go to previous time step" << endl
174  << " 'c' or 'C' : play animation from start to end" << endl
175  << " 'q' or 'Q' : quit" << endl;
176  iren->Start();
177  return EXIT_SUCCESS;
178  }
179  else if (retVal == vtkTesting::NOT_RUN)
180  {
181  return VTK_SKIP_RETURN_CODE;
182  }
183  else if (retVal == vtkTesting::PASSED)
184  {
185  return EXIT_SUCCESS;
186  }
187 
188  return EXIT_FAILURE;
189 }
190 }
191 
192 #endif
void AddActor(vtkProp *p)
Add/Remove different types of props to the renderer.
vtkSmartPointer< vtkCompositePolyDataMapper2 > Mapper
std::vector< double > TimeSteps
virtual void SetClientData(void *cd)
Methods to set and get client and callback information, and the callback function.
virtual void SetTimeResolution(int)
Get/Set the time resolution for timesteps produced by the reader.
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkAlgorithmOutput * GetOutputPort(int index)
Get a proxy object corresponding to the given output port of this algorithm.
static vtkRenderWindowInteractor * SafeDownCast(vtkObjectBase *o)
Store vtkAlgorithm input/output information.
static void CharEventCallback(vtkObject *caller, unsigned long, void *clientdata, void *)
virtual void UpdateInformation()
Bring the algorithm's information up-to-date.
vtkSmartPointer< vtkMotionFXCFGReader > Reader
void SetRenderWindow(vtkRenderWindow *aren)
Set/Get the rendering window being controlled by this object.
vtkDataObject * GetOutputDataObject(int port)
Get the data object that will contain the algorithm output for the given port.
virtual void AddRenderer(vtkRenderer *)
Add a renderer to the list of renderers.
unsigned long AddObserver(unsigned long event, vtkCommand *, float priority=0.0f)
Allow people to add/remove/invoke observers (callbacks) to any VTK object.
void Render() override
Ask each renderer owned by this RenderWindow to render its image and synchronize this process...
virtual void SetInputDataObject(int port, vtkDataObject *data)
Sets the data-object as an input on the given port index.
virtual void Start()
Start the event loop.
vtkSmartPointer< vtkRenderWindow > Window
int Get(vtkInformationIntegerKey *key)
Get/Set an integer-valued entry.
int Test(int argc, char *argv[], const char *dfile, const InitializationCallback &initCallback)
virtual void SetInputConnection(int port, vtkAlgorithmOutput *input)
Set the connection for the given input port index.
virtual int UpdateTimeStep(double time, int piece=-1, int numPieces=1, int ghostLevels=0, const int extents[6]=nullptr)
Convenience method to update an algorithm after passing requests to its first output port...
void SetFileName(VTK_FILEPATH const char *fname)
Get/Set the filename.
int Length(vtkInformationIntegerVectorKey *key)
Get/Set an integer-vector-valued entry.
Allocate and hold a VTK object.
Definition: vtkMeta.h:30
virtual void SetMapper(vtkMapper *)
This is the method that is used to connect an actor to the end of a visualization pipeline...
Executive supporting partial updates.
vtkInformation * GetOutputInformation(int port)
Return the information object that is associated with a particular output port.
#define max(a, b)
virtual void SetCallback(void(*f)(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata))