VTK
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(this->CurrentIndex >= 0 && this->CurrentIndex < static_cast<int>(this->TimeSteps.size()));
78  this->Reader->UpdateTimeStep(this->TimeSteps[this->CurrentIndex]);
79  this->Mapper->SetInputDataObject(this->Reader->GetOutputDataObject(0));
80  this->Window->Render();
81  }
82 };
83 
84 static void CharEventCallback(vtkObject* caller, unsigned long, void* clientdata, void*)
85 {
86  ClientData& data = *reinterpret_cast<ClientData*>(clientdata);
87  auto iren = vtkRenderWindowInteractor::SafeDownCast(caller);
88  switch (iren->GetKeyCode())
89  {
90  case 'x':
91  case 'X':
92  data.GoToNext();
93  break;
94 
95  case 'z':
96  case 'Z':
97  data.GoToPrev();
98  break;
99 
100  case 'c':
101  case 'C':
102  data.Play();
103  break;
104  }
105 }
106 
107 template <typename InitializationCallback>
108 int Test(int argc, char* argv[], const char* dfile, const InitializationCallback& initCallback)
109 {
111  char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, dfile);
112  reader->SetFileName(fname);
113  delete[] fname;
114 
115  reader->SetTimeResolution(100);
116  reader->UpdateInformation();
117 
119  vtkInformation* outInfo = reader->GetOutputInformation(0);
120  const int numTimeSteps = outInfo->Length(SDDP::TIME_STEPS());
121 
122  if (numTimeSteps != 100)
123  {
124  cerr << "ERROR: missing timesteps. Potential issue reading the CFG file." << endl;
125  return EXIT_FAILURE;
126  }
127 
129 
130  vtkNew<vtkRenderer> renderer;
131  renWin->AddRenderer(renderer);
132 
134  iren->SetRenderWindow(renWin);
135 
137  mapper->SetInputConnection(reader->GetOutputPort());
138 
139  vtkNew<vtkActor> actor;
140  actor->SetMapper(mapper);
141  renderer->AddActor(actor);
142 
143  initCallback(renWin, renderer, reader);
144 
145  std::vector<double> ts(numTimeSteps);
146  outInfo->Get(SDDP::TIME_STEPS(), &ts[0]);
147 
148  // for baseline comparison, we'll jump to the middle of the
149  // time sequence and do a capture.
150  reader->UpdateTimeStep(ts[numTimeSteps / 2]);
151  mapper->SetInputDataObject(reader->GetOutputDataObject(0));
152  renWin->Render();
153 
154  const int retVal = vtkTesting::Test(argc, argv, renWin, 10);
155  if (retVal == vtkTesting::DO_INTERACTOR)
156  {
158  data.Window = renWin;
159  data.Reader = reader;
160  data.Mapper = mapper;
161  data.TimeSteps = ts;
162  data.CurrentIndex = numTimeSteps / 2;
163 
165  observer->SetClientData(&data);
166  observer->SetCallback(&CharEventCallback);
167  iren->AddObserver(vtkCommand::CharEvent, observer);
168 
169  cout << "Entering interactive mode......" << endl
170  << "Supported operations:" << endl
171  << " 'z' or 'Z' : go to next time step" << endl
172  << " 'x' or 'X' : go to previous time step" << endl
173  << " 'c' or 'C' : play animation from start to end" << endl
174  << " 'q' or 'Q' : quit" << endl;
175  iren->Start();
176  return EXIT_SUCCESS;
177  }
178  else if (retVal == vtkTesting::NOT_RUN)
179  {
180  return VTK_SKIP_RETURN_CODE;
181  }
182  else if (retVal == vtkTesting::PASSED)
183  {
184  return EXIT_SUCCESS;
185  }
186 
187  return EXIT_FAILURE;
188 }
189 }
190 
191 #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:59
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&#39;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.
void SetFileName(const char *fname)
Get/Set the filename.
virtual void Start()
Start the event loop.
vtkSmartPointer< vtkRenderWindow > Window
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...
VTKCOMMONCORE_EXPORT int Length(vtkInformationIntegerVectorKey *key)
Get/Set an integer-vector-valued entry.
Allocate and hold a VTK object.
Definition: vtkNew.h:58
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.
VTKCOMMONCORE_EXPORT int Get(vtkInformationIntegerKey *key)
Get/Set an integer-valued entry.
#define max(a, b)
virtual void SetCallback(void(*f)(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata))