VTK  9.1.0
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>
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
36namespace impl
37{
38
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
85static void CharEventCallback(vtkObject* caller, unsigned long, void* clientdata, void*)
86{
87 ClientData& data = *reinterpret_cast<ClientData*>(clientdata);
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
108template <typename InitializationCallback>
109int 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 {
158 ClientData data;
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
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:165
abstract base class for most VTK objects
Definition: vtkObject.h:82
static vtkRenderWindowInteractor * SafeDownCast(vtkObjectBase *o)
Hold a reference to a vtkObjectBase instance.
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)
std::vector< double > TimeSteps
vtkSmartPointer< vtkMotionFXCFGReader > Reader
vtkSmartPointer< vtkRenderWindow > Window
vtkSmartPointer< vtkCompositePolyDataMapper2 > Mapper