VTK  9.4.20241108
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>
9#include <vtkInformation.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
24namespace impl
25{
26
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
73static void CharEventCallback(vtkObject* caller, unsigned long, void* clientdata, void*)
74{
75 ClientData& data = *reinterpret_cast<ClientData*>(clientdata);
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
96template <typename InitializationCallback>
97int 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 {
146 ClientData data;
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
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:167
abstract base class for most VTK objects
Definition vtkObject.h:162
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< vtkCompositePolyDataMapper > Mapper