TTK
Loading...
Searching...
No Matches
ttkUserInterfaceBase.cpp
Go to the documentation of this file.
1// local includes
3
4#include <vtkPointData.h>
5#include <vtkTexture.h>
6
7#ifndef TTK_INSTALL_ASSETS_DIR
8#define TTK_INSTALL_ASSETS_DIR "."
9#endif // TTK_INSTALL_ASSETS_DIR
10
11using namespace std;
12using namespace ttk;
13
15
17
18 vtkRenderWindowInteractor *interactor = this->Interactor;
19 string key = interactor->GetKeySym();
20
21 if(interactor->GetControlKey()) {
22 // control key pressed
23 if(key == "e") {
24 // export
25 userInterface_->exportScene("output.wrl");
26 }
27 if(key == "s") {
28 // save in vtk file format
30 }
31 } else {
32
33 if(key == "Return") {
34 // do something if the user hits Enter
36 } else if(key == "Escape") {
37 exit(0);
38 }
39
40 if((key == "0") || (key == "1") || (key == "2") || (key == "3")
41 || (key == "4") || (key == "5") || (key == "6") || (key == "7")
42 || (key == "8") || (key == "9")) {
43
44 int outputId = -1;
45 stringstream stream;
46
47 stream << key;
48 stream >> outputId;
49
51 }
52
53 if(key == "t") {
55 }
56 }
57
59 userInterface_->getKeyHandler()->OnKeyPress(interactor, key);
60 }
61
63}
64
66
67 keyHandler_ = nullptr;
68 vtkWrapper_ = nullptr;
69 isUp_ = false;
70 repeat_ = false;
71 transparency_ = false;
72 fullscreen_ = false;
73
80
81 pngReader_->SetFileName(
83 "/textures/png/scalarFieldTexturePaleInterleavedRules.png");
84 pngReader_->Update();
85 hasTexture_ = !((pngReader_->GetOutput()->GetNumberOfPoints() == 1)
86 && (pngReader_->GetOutput()->GetNumberOfCells() == 1));
87}
88
90
91int ttkUserInterfaceBase::exportScene(const string &fileName) const {
92
93 vtkVRMLExporter *exporter = vtkVRMLExporter::New();
94
95 exporter->SetInput(renderWindow_);
96 exporter->SetFileName(fileName.data());
97 exporter->Write();
98
99 exporter->Delete();
100
101 return 0;
102}
103
104int ttkUserInterfaceBase::init(int &argc, char **argv) {
105
106 parser_.setOption("R", &repeat_, "Repeat the program when hitting `Return'");
108 "fullscreen", &fullscreen_, "Maximize the window at launch");
109
110 return ProgramBase::init(argc, argv);
111}
112
114
115 // collect the output and update the rendering
116 int const outputPortNumber = vtkWrapper_->GetNumberOfOutputPorts();
117
118 if((int)visibleOutputs_.size() != outputPortNumber) {
119 visibleOutputs_.resize(outputPortNumber, true);
120
121 for(int i = 0; i < (int)hiddenOutputs_.size(); i++) {
122 if((hiddenOutputs_[i] >= 0)
123 && (hiddenOutputs_[i] < (int)visibleOutputs_.size())) {
125 }
126 }
127 }
128
129 if((int)surfaces_.size() != outputPortNumber) {
130 surfaces_.resize(outputPortNumber, nullptr);
131 mainActors_.resize(outputPortNumber);
132 boundaryFilters_.resize(outputPortNumber);
133 boundaryMappers_.resize(outputPortNumber);
134 textureMapFromFields_.resize(outputPortNumber);
135
136 for(int i = 0; i < outputPortNumber; i++) {
141 }
142 }
143
144 for(int i = 0; i < outputPortNumber; i++) {
145
146 if(visibleOutputs_[i]) {
147
148 if(hasTexture_) {
149 vtkWrapper_->GetOutput(i)->GetPointData()->SetActiveScalars(nullptr);
150 }
151
152 if((repeat_) && (i < vtkWrapper_->GetNumberOfInputPorts())) {
153 inputs_[i]->DeepCopy(vtkWrapper_->GetOutput(i));
154 vtkWrapper_->SetInputData(i, inputs_[i]);
155 }
156
157 // extract the boundary surface of the tet-mesh
158 boundaryFilters_[i]->SetInputData(vtkWrapper_->GetOutput(i));
159 boundaryFilters_[i]->Update();
160 }
161 }
162
163 // update the scalar field texture
165
166 for(int i = 0; i < (int)mainActors_.size(); i++) {
167 if(visibleOutputs_[i]) {
168 mainActors_[i]->SetMapper(boundaryMappers_[i]);
169 } else {
170 mainActors_[i]->SetMapper(nullptr);
171 }
172 if(transparency_) {
173 mainActors_[i]->GetProperty()->SetOpacity(0.3);
174 } else {
175 mainActors_[i]->GetProperty()->SetOpacity(1);
176 }
177 renderer_->AddActor(mainActors_[i]);
178 }
179
180 if(isUp_)
181 renderWindow_->Render();
182
183 return 0;
184}
185
187
188 execute();
189
190 {
191 stringstream msg;
192 msg << "[UserInterface] Initializing user interface..." << endl;
193 printMsg(msg.str());
194 }
195
196 renderWindow_->AddRenderer(renderer_);
197 if(fullscreen_) {
198 renderWindow_->SetFullScreen(fullscreen_);
199 } else {
200 renderWindow_->SetSize(1920, 1080);
201 }
202 renderWindow_->SetWindowName("TTK - The Topology ToolKit");
203
204 interactor_->SetRenderWindow(renderWindow_);
205 interactor_->SetInteractorStyle(customInteractor_);
206 customInteractor_->SetCurrentRenderer(renderer_);
207 customInteractor_->setUserInterface(this);
208
209 refresh();
210
211 renderer_->SetBackground(44 / 255.0, 44 / 255.0, 44 / 255.0);
212
213 interactor_->Initialize();
214
215 {
216 stringstream msg;
217 msg << "[ttkUserInterfaceBase] Running user interface!" << endl;
218 printMsg(msg.str());
219 }
220
221 isUp_ = true;
222
223 interactor_->Start();
224
225 return 0;
226}
227
228int ttkUserInterfaceBase::switchOutput(const int &outputId) {
229
230 if(!vtkWrapper_)
231 return -1;
232
233 if((outputId < 0) || (outputId >= vtkWrapper_->GetNumberOfOutputPorts()))
234 return -2;
235
236 stringstream msg;
237 msg << "[ttkUserInterfaceBase] Turning output #" << outputId << " ";
238
239 if(visibleOutputs_[outputId]) {
240 msg << "off";
241 } else {
242 msg << "on";
243 }
244 msg << endl;
245 printMsg(msg.str());
246
247 visibleOutputs_[outputId] = !visibleOutputs_[outputId];
248
249 return 0;
250}
251
253
254 stringstream msg;
255 msg << "[ttkUserInterfaceBase] Switching transparency ";
256
257 if(transparency_) {
258 msg << "off";
259 } else {
260 msg << "on";
261 }
262 msg << endl;
263 printMsg(msg.str());
264
266
267 return 0;
268}
269
271
272 for(int i = 0; i < (int)boundaryFilters_.size(); i++) {
273
274 // use a texture for the color-value visualization
275 if((boundaryFilters_[i]->GetOutput()->GetPointData())
276 && (boundaryFilters_[i]->GetOutput()->GetPointData()->GetArray(0))) {
277
278 textureMapFromFields_[i]->SetInputDataObject(
279 0, boundaryFilters_[i]->GetOutput());
280 textureMapFromFields_[i]->Update();
281 surfaces_[i] = vtkPolyData::SafeDownCast(
282 textureMapFromFields_[i]->GetOutputDataObject(0));
283
284 texture_->SetInputConnection(pngReader_->GetOutputPort());
285
286 if(hasTexture_) {
287 mainActors_[i]->SetTexture(texture_);
288 }
289 } else {
290 surfaces_[i] = boundaryFilters_[i]->GetOutput();
291 }
292
293 boundaryMappers_[i]->SetInputData(surfaces_[i]);
294 }
295
296 return 0;
297}
ttkUserInterfaceBase * userInterface_
virtual int OnKeyPress(vtkRenderWindowInteractor *interactor, std::string &key)=0
int execute() override
Set the arguments of your ttk module and execute it here.
vtkDataSetAlgorithm * vtkWrapper_
int save() const override
Save the output(s) of the TTK module.
std::vector< vtkDataSet * > inputs_
std::vector< vtkPolyData * > surfaces_
vtkSmartPointer< vtkRenderWindowInteractor > interactor_
vtkSmartPointer< ttkCustomInteractor > customInteractor_
int switchOutput(const int &outputId)
vtkSmartPointer< vtkRenderer > renderer_
vtkSmartPointer< vtkPNGReader > pngReader_
std::vector< vtkSmartPointer< vtkActor > > mainActors_
vtkSmartPointer< vtkRenderWindow > renderWindow_
std::vector< vtkSmartPointer< vtkDataSetSurfaceFilter > > boundaryFilters_
int init(int &argc, char **argv) override
~ttkUserInterfaceBase() override
std::vector< int > hiddenOutputs_
std::vector< bool > visibleOutputs_
std::vector< vtkSmartPointer< ttkTextureMapFromField > > textureMapFromFields_
int exportScene(const std::string &fileName="output.wrl") const
ttkKeyHandler * getKeyHandler()
std::vector< vtkSmartPointer< vtkPolyDataMapper > > boundaryMappers_
vtkSmartPointer< vtkTexture > texture_
int setOption(const std::string &key, bool *value, const std::string &description="")
CommandLineParser parser_
Definition ProgramBase.h:69
virtual int init(int &argc, char **argv)
Definition ProgramBase.h:30
The Topology ToolKit.
vtkStandardNewMacro(ttkCustomInteractor)
#define TTK_INSTALL_ASSETS_DIR
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)