VTK  9.3.20240328
TestMotionFXCFGReaderCommon.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 #ifndef TestMotionFXCFGReaderCommon_h
4 #define TestMotionFXCFGReaderCommon_h
5 
6 #include <vtkActor.h>
7 #include <vtkCallbackCommand.h>
9 #include <vtkInformation.h>
10 #include <vtkMotionFXCFGReader.h>
11 #include <vtkNew.h>
12 #include <vtkRenderWindow.h>
14 #include <vtkRenderer.h>
15 #include <vtkSmartPointer.h>
17 #include <vtkTestUtilities.h>
18 #include <vtkTesting.h>
19 
20 #include <algorithm>
21 #include <cassert>
22 #include <vector>
23 
24 namespace impl
25 {
26 
27 struct ClientData
28 {
32  std::vector<double> TimeSteps;
34 
35  void GoToNext()
36  {
37  cout << "Go to next" << endl;
38  this->CurrentIndex =
39  std::min(static_cast<int>(this->TimeSteps.size()) - 1, this->CurrentIndex + 1);
40  this->Render();
41  }
42 
43  void GoToPrev()
44  {
45  cout << "Go to prev" << endl;
46  this->CurrentIndex = std::max(0, this->CurrentIndex - 1);
47  this->Render();
48  }
49 
50  void Play()
51  {
52  cout << "Playing";
53  for (size_t cc = 0; cc < this->TimeSteps.size(); ++cc)
54  {
55  cout << ".";
56  cout.flush();
57  this->CurrentIndex = static_cast<int>(cc);
58  this->Render();
59  }
60  cout << endl;
61  }
62 
63  void Render()
64  {
65  assert(
66  this->CurrentIndex >= 0 && this->CurrentIndex < static_cast<int>(this->TimeSteps.size()));
67  this->Reader->UpdateTimeStep(this->TimeSteps[this->CurrentIndex]);
68  this->Mapper->SetInputDataObject(this->Reader->GetOutputDataObject(0));
69  this->Window->Render();
70  }
71 };
72 
73 static void CharEventCallback(vtkObject* caller, unsigned long, void* clientdata, void*)
74 {
75  ClientData& data = *reinterpret_cast<ClientData*>(clientdata);
76  auto iren = vtkRenderWindowInteractor::SafeDownCast(caller);
77  switch (iren->GetKeyCode())
78  {
79  case 'x':
80  case 'X':
81  data.GoToNext();
82  break;
83 
84  case 'z':
85  case 'Z':
86  data.GoToPrev();
87  break;
88 
89  case 'c':
90  case 'C':
91  data.Play();
92  break;
93  }
94 }
95 
96 template <typename InitializationCallback>
97 int Test(int argc, char* argv[], const char* dfile, const InitializationCallback& initCallback)
98 {
100  char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, dfile);
101  reader->SetFileName(fname);
102  delete[] fname;
103 
104  reader->SetTimeResolution(100);
105  reader->UpdateInformation();
106 
108  vtkInformation* outInfo = reader->GetOutputInformation(0);
109  const int numTimeSteps = outInfo->Length(SDDP::TIME_STEPS());
110 
111  if (numTimeSteps != 100)
112  {
113  cerr << "ERROR: missing timesteps. Potential issue reading the CFG file." << endl;
114  return EXIT_FAILURE;
115  }
116 
118 
119  vtkNew<vtkRenderer> renderer;
120  renWin->AddRenderer(renderer);
121 
123  iren->SetRenderWindow(renWin);
124 
126  mapper->SetInputConnection(reader->GetOutputPort());
127 
128  vtkNew<vtkActor> actor;
129  actor->SetMapper(mapper);
130  renderer->AddActor(actor);
131 
132  initCallback(renWin, renderer, reader);
133 
134  std::vector<double> ts(numTimeSteps);
135  outInfo->Get(SDDP::TIME_STEPS(), ts.data());
136 
137  // for baseline comparison, we'll jump to the middle of the
138  // time sequence and do a capture.
139  reader->UpdateTimeStep(ts[numTimeSteps / 2]);
140  mapper->SetInputDataObject(reader->GetOutputDataObject(0));
141  renWin->Render();
142 
143  const int retVal = vtkTesting::Test(argc, argv, renWin, 10);
144  if (retVal == vtkTesting::DO_INTERACTOR)
145  {
147  data.Window = renWin;
148  data.Reader = reader;
149  data.Mapper = mapper;
150  data.TimeSteps = ts;
151  data.CurrentIndex = numTimeSteps / 2;
152 
154  observer->SetClientData(&data);
155  observer->SetCallback(&CharEventCallback);
156  iren->AddObserver(vtkCommand::CharEvent, observer);
157 
158  cout << "Entering interactive mode......" << endl
159  << "Supported operations:" << endl
160  << " 'z' or 'Z' : go to next time step" << endl
161  << " 'x' or 'X' : go to previous time step" << endl
162  << " 'c' or 'C' : play animation from start to end" << endl
163  << " 'q' or 'Q' : quit" << endl;
164  iren->Start();
165  return EXIT_SUCCESS;
166  }
167  else if (retVal == vtkTesting::NOT_RUN)
168  {
169  return VTK_SKIP_RETURN_CODE;
170  }
171  else if (retVal == vtkTesting::PASSED)
172  {
173  return EXIT_SUCCESS;
174  }
175 
176  return EXIT_FAILURE;
177 }
178 }
179 
180 #endif
virtual void SetMapper(vtkMapper *)
This is the method that is used to connect an actor to the end of a visualization pipeline,...
vtkDataObject * GetOutputDataObject(int port)
Get the data object that will contain the algorithm output for the given port.
virtual void SetInputDataObject(int port, vtkDataObject *data)
Sets the data-object as an input on the given 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.
virtual void SetCallback(void(*f)(vtkObject *caller, unsigned long eid, void *clientdata, void *calldata))
virtual void SetClientData(void *cd)
Methods to set and get client and callback information, and the callback function.
Store vtkAlgorithm input/output information.
int Get(vtkInformationIntegerKey *key)
Get/Set an integer-valued entry.
int Length(vtkInformationIntegerVectorKey *key)
Get/Set an integer-vector-valued entry.
Allocate and hold a VTK object.
Definition: vtkNew.h:160
abstract base class for most VTK objects
Definition: vtkObject.h:161
static vtkRenderWindowInteractor * SafeDownCast(vtkObjectBase *o)
void Render() override
Ask each renderer owned by this RenderWindow to render its image and synchronize this process.
void AddActor(vtkProp *p)
Add/Remove different types of props to the renderer.
Executive supporting partial updates.
static void CharEventCallback(vtkObject *caller, unsigned long, void *clientdata, void *)
int Test(int argc, char *argv[], const char *dfile, const InitializationCallback &initCallback)
@ data
Definition: vtkX3D.h:315
std::vector< double > TimeSteps
vtkSmartPointer< vtkMotionFXCFGReader > Reader
vtkSmartPointer< vtkRenderWindow > Window
vtkSmartPointer< vtkCompositePolyDataMapper > Mapper
#define max(a, b)