OGS
VtkCustomInteractorStyle.cpp
Go to the documentation of this file.
1
15// ** INCLUDES **
17
18#include <vtkActor.h>
19#include <vtkAlgorithmOutput.h>
20#include <vtkCamera.h>
21#include <vtkCellData.h>
22#include <vtkCellPicker.h>
23#include <vtkDataSetMapper.h>
24#include <vtkExtractSelection.h>
25#include <vtkIdTypeArray.h>
26#include <vtkObjectFactory.h>
27#include <vtkProp.h>
28#include <vtkProperty.h>
29#include <vtkRenderWindow.h>
30#include <vtkRenderWindowInteractor.h>
31#include <vtkRendererCollection.h>
32#include <vtkSelection.h>
33#include <vtkSelectionNode.h>
34#include <vtkSmartPointer.h>
35#include <vtkUnstructuredGrid.h>
36#include <vtkUnstructuredGridAlgorithm.h>
37
38#include <string>
39
40#include "BaseLib/Logging.h"
42
44
46{
47 _selectedMapper = vtkDataSetMapper::New();
48 _selectedActor = vtkActor::New();
50 _selectedActor->GetProperty()->EdgeVisibilityOn();
51 _selectedActor->GetProperty()->SetEdgeColor(1, 0, 0);
52 _selectedActor->GetProperty()->SetLineWidth(3);
53}
54
60
62{
63 switch (Interactor->GetKeyCode())
64 {
65 case '3':
66 INFO("The 3 key was pressed.");
67 break;
68 case 'a':
69 break;
70 default:
71 vtkInteractorStyleTrackballCamera::OnChar();
72 }
73}
74
76{
77 switch (Interactor->GetKeyCode())
78 {
79 case 32: // Space
81 emit cursorChanged(Qt::CrossCursor);
82 break;
83 default:
84 vtkInteractorStyleTrackballCamera::OnKeyDown();
85 }
86}
87
89{
90 switch (Interactor->GetKeyCode())
91 {
92 case 32: // Space
94 emit cursorChanged(Qt::ArrowCursor);
95 break;
96 default:
97 vtkInteractorStyleTrackballCamera::OnKeyUp();
98 }
99}
100
102{
103 if (_highlightActor)
104 {
105 HighlightProp((vtkProp*)actor);
106 }
107}
108
110{
111 this->Interactor->GetRenderWindow()
112 ->GetRenderers()
113 ->GetFirstRenderer()
114 ->RemoveActor(_selectedActor);
115}
116
118{
119 _highlightActor = on;
120 if (!on)
121 {
122 HighlightProp((vtkProp*)nullptr);
123 }
124}
125
127{
128 _data = object;
129 if (!object)
130 {
131 this->Interactor->GetRenderWindow()
132 ->GetRenderers()
133 ->GetFirstRenderer()
134 ->RemoveActor(_selectedActor);
135 _selectedMapper->SetInputConnection(nullptr);
136 }
137}
138
139// From https://kitware.github.io/vtk-examples/site/Cxx/Picking/CellPicking/
141{
142 if (!_data)
143 {
144 return vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
145 }
146
148 {
149 // Get the location of the click (in window coordinates)
150 int* pos = this->GetInteractor()->GetEventPosition();
151
152 vtkSmartPointer<vtkCellPicker> picker =
153 vtkSmartPointer<vtkCellPicker>::New();
154 picker->SetTolerance(0.0005);
155
156 // Pick from this location.
157 picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
158
159 double* worldPosition = picker->GetPickPosition();
160 INFO("Cell id is: {:d}", picker->GetCellId());
161
162 if (picker->GetCellId() != -1)
163 {
164 INFO("Pick position is: {:f} {:f} {:f}", worldPosition[0],
165 worldPosition[1], worldPosition[2]);
166
167 vtkSmartPointer<vtkIdTypeArray> ids =
168 vtkSmartPointer<vtkIdTypeArray>::New();
169 ids->SetNumberOfValues(1);
170 ids->SetValue(0, picker->GetCellId());
171
172 vtkSmartPointer<vtkSelectionNode> selectionNode =
173 vtkSmartPointer<vtkSelectionNode>::New();
174 selectionNode->SetFieldType(vtkSelectionNode::CELL);
175 selectionNode->SetContentType(vtkSelectionNode::INDICES);
176 selectionNode->SetSelectionList(ids);
177
178 vtkSmartPointer<vtkSelection> selection =
179 vtkSmartPointer<vtkSelection>::New();
180 selection->AddNode(selectionNode);
181
182 vtkSmartPointer<vtkExtractSelection> extractSelection =
183 vtkSmartPointer<vtkExtractSelection>::New();
184 extractSelection->SetInputData(0, _data);
185 extractSelection->SetInputData(1, selection);
186 extractSelection->Update();
187
188 // In selection
189 vtkSmartPointer<vtkUnstructuredGrid> selected =
190 vtkSmartPointer<vtkUnstructuredGrid>::New();
191 selected->ShallowCopy(extractSelection->GetOutput());
192
193 INFO("There are {:d} points in the selection.",
194 selected->GetNumberOfPoints());
195 INFO("There are {:d} cells in the selection.",
196 selected->GetNumberOfCells());
197
198 // check if the underlying object is a mesh and if so, send a signal
199 // to the element model for display of information about the picked
200 // element.
201 vtkAlgorithm* data_set = picker->GetActor()
202 ->GetMapper()
203 ->GetInputConnection(0, 0)
204 ->GetProducer()
205 ->GetInputConnection(0, 0)
206 ->GetProducer();
207 auto* source =
208 dynamic_cast<vtkUnstructuredGridAlgorithm*>(data_set);
209 if (source)
210 {
211 emit elementPicked(source,
212 static_cast<unsigned>(picker->GetCellId()));
213 }
214 else
215 {
216 emit clearElementView();
217 }
218 _selectedMapper->SetInputData(selected);
219
220 this->Interactor->GetRenderWindow()
221 ->GetRenderers()
222 ->GetFirstRenderer()
223 ->AddActor(_selectedActor);
224 //_highlightActor = true;
225 }
226 else
227 {
228 this->Interactor->GetRenderWindow()
229 ->GetRenderers()
230 ->GetFirstRenderer()
231 ->RemoveActor(_selectedActor);
232 }
233 emit requestViewUpdate();
234 }
235 else
236 {
237 // Forward events
238 vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
239 }
240}
241
243{
244 if (!_data)
245 {
246 return vtkInteractorStyleTrackballCamera::OnRightButtonDown();
247 }
248
250 {
251 // Get the location of the click (in window coordinates)
252 int* pos = this->GetInteractor()->GetEventPosition();
253
254 vtkSmartPointer<vtkCellPicker> picker =
255 vtkSmartPointer<vtkCellPicker>::New();
256 picker->SetTolerance(0.0005);
257
258 // Pick from this location.
259 picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
260
261 double* worldPosition = picker->GetPickPosition();
262 INFO("Cell id is: {:d}", picker->GetCellId());
263
264 if (picker->GetCellId() != -1)
265 {
266 vtkRenderer* renderer = this->Interactor->GetRenderWindow()
267 ->GetRenderers()
268 ->GetFirstRenderer();
269 vtkCamera* cam = renderer->GetActiveCamera();
270 cam->SetFocalPoint(worldPosition);
271 emit requestViewUpdate();
272 }
273 }
274 else
275 {
276 // Forward events
277 vtkInteractorStyleTrackballCamera::OnRightButtonDown();
278 }
279}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
Definition of the VtkCompositeSelectionFilter class.
vtkStandardNewMacro(VtkCustomInteractorStyle)
Definition of the VtkCustomInteractorStyle class.
vtkDataSetMapper * _selectedMapper
The mapper for highlighting the selected cell.
void OnChar() override
Handles key press events.
void OnKeyDown() override
Handles key down events.
void elementPicked(vtkUnstructuredGridAlgorithm const *const, unsigned)
Emitted when a mesh element has been picked.
void cursorChanged(Qt::CursorShape)
Emitted when the cursor shape was changed due to alternate mouse action mode.
void clearElementView()
Emitted when the current object type cannot be handled by the element model.
void pickableDataObject(vtkDataObject *object)
Sets the highlightable vtk object.
vtkDataObject * _data
The vtk object to pick.
void removeHighlightActor()
Removes the highlight actor from the visible scene.
vtkActor * _selectedActor
The actor for highlighting the selected cell.
void highlightActor(vtkProp3D *actor)
void requestViewUpdate()
Emitted when something was picked.
void OnLeftButtonDown() override
Handles left mouse button events (picking).
void OnKeyUp() override
Handles key up events.
void OnRightButtonDown() override
Handles middle mouse button events (rotation point picking).