OGS
VtkCustomInteractorStyle Class Reference

Detailed Description

VtkCustomInteractorStyle implements highlighting of an active actor and highlighting of picked cells inside a vtk object. Picking occurs when a vtk object was selected, the alternate mouse mode is active (hold spacebar) and the user left clicks. On right click the cameras focal point (center of rotation) is set to the picking position.

Definition at line 35 of file VtkCustomInteractorStyle.h.

#include <VtkCustomInteractorStyle.h>

Inheritance diagram for VtkCustomInteractorStyle:
[legend]
Collaboration diagram for VtkCustomInteractorStyle:
[legend]

Public Slots

void highlightActor (vtkProp3D *actor)
 
void removeHighlightActor ()
 Removes the highlight actor from the visible scene.
 
void setHighlightActor (bool on)
 
void pickableDataObject (vtkDataObject *object)
 Sets the highlightable vtk object.
 

Signals

void requestViewUpdate ()
 Emitted when something was picked.
 
void cursorChanged (Qt::CursorShape)
 Emitted when the cursor shape was changed due to alternate mouse action mode.
 
void elementPicked (vtkUnstructuredGridAlgorithm const *const, unsigned)
 Emitted when a mesh element has been picked.
 
void clearElementView ()
 Emitted when the current object type cannot be handled by the element model.
 

Public Member Functions

 vtkTypeMacro (VtkCustomInteractorStyle, vtkInteractorStyleTrackballCamera)
 
void OnChar () override
 Handles key press events.
 
void OnKeyDown () override
 Handles key down events.
 
void OnKeyUp () override
 Handles key up events.
 
void OnLeftButtonDown () override
 Handles left mouse button events (picking).
 
void OnRightButtonDown () override
 Handles middle mouse button events (rotation point picking).
 

Static Public Member Functions

static VtkCustomInteractorStyleNew ()
 

Protected Member Functions

 VtkCustomInteractorStyle ()
 
 ~VtkCustomInteractorStyle () override
 

Protected Attributes

vtkDataObject * _data {nullptr}
 The vtk object to pick.
 
vtkDataSetMapper * _selectedMapper
 The mapper for highlighting the selected cell.
 
vtkActor * _selectedActor
 The actor for highlighting the selected cell.
 

Private Attributes

bool _highlightActor {false}
 
bool _alternateMouseActions {false}
 

Constructor & Destructor Documentation

◆ VtkCustomInteractorStyle()

VtkCustomInteractorStyle::VtkCustomInteractorStyle ( )
protected

Definition at line 45 of file VtkCustomInteractorStyle.cpp.

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}
vtkDataSetMapper * _selectedMapper
The mapper for highlighting the selected cell.
vtkActor * _selectedActor
The actor for highlighting the selected cell.

References _selectedActor, and _selectedMapper.

◆ ~VtkCustomInteractorStyle()

VtkCustomInteractorStyle::~VtkCustomInteractorStyle ( )
overrideprotected

Definition at line 55 of file VtkCustomInteractorStyle.cpp.

56{
57 _selectedActor->Delete();
58 _selectedMapper->Delete();
59}

References _selectedActor, and _selectedMapper.

Member Function Documentation

◆ clearElementView

void VtkCustomInteractorStyle::clearElementView ( )
signal

Emitted when the current object type cannot be handled by the element model.

Referenced by OnLeftButtonDown().

◆ cursorChanged

void VtkCustomInteractorStyle::cursorChanged ( Qt::CursorShape )
signal

Emitted when the cursor shape was changed due to alternate mouse action mode.

Referenced by OnKeyDown(), and OnKeyUp().

◆ elementPicked

void VtkCustomInteractorStyle::elementPicked ( vtkUnstructuredGridAlgorithm const * const ,
unsigned  )
signal

Emitted when a mesh element has been picked.

Referenced by OnLeftButtonDown().

◆ highlightActor

void VtkCustomInteractorStyle::highlightActor ( vtkProp3D * actor)
slot

Definition at line 101 of file VtkCustomInteractorStyle.cpp.

102{
103 if (_highlightActor)
104 {
105 HighlightProp((vtkProp*)actor);
106 }
107}

References _highlightActor.

◆ New()

static VtkCustomInteractorStyle * VtkCustomInteractorStyle::New ( )
static

◆ OnChar()

void VtkCustomInteractorStyle::OnChar ( )
override

Handles key press events.

Definition at line 61 of file VtkCustomInteractorStyle.cpp.

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}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35

References INFO().

◆ OnKeyDown()

void VtkCustomInteractorStyle::OnKeyDown ( )
override

Handles key down events.

Definition at line 75 of file VtkCustomInteractorStyle.cpp.

76{
77 switch (Interactor->GetKeyCode())
78 {
79 case 32: // Space
81 emit cursorChanged(Qt::CrossCursor);
82 break;
83 default:
84 vtkInteractorStyleTrackballCamera::OnKeyDown();
85 }
86}
void cursorChanged(Qt::CursorShape)
Emitted when the cursor shape was changed due to alternate mouse action mode.

References _alternateMouseActions, and cursorChanged().

◆ OnKeyUp()

void VtkCustomInteractorStyle::OnKeyUp ( )
override

Handles key up events.

Definition at line 88 of file VtkCustomInteractorStyle.cpp.

89{
90 switch (Interactor->GetKeyCode())
91 {
92 case 32: // Space
94 emit cursorChanged(Qt::ArrowCursor);
95 break;
96 default:
97 vtkInteractorStyleTrackballCamera::OnKeyUp();
98 }
99}

References _alternateMouseActions, and cursorChanged().

◆ OnLeftButtonDown()

void VtkCustomInteractorStyle::OnLeftButtonDown ( )
override

Handles left mouse button events (picking).

Definition at line 140 of file VtkCustomInteractorStyle.cpp.

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}
void elementPicked(vtkUnstructuredGridAlgorithm const *const, unsigned)
Emitted when a mesh element has been picked.
void clearElementView()
Emitted when the current object type cannot be handled by the element model.
vtkDataObject * _data
The vtk object to pick.
void requestViewUpdate()
Emitted when something was picked.
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:225

References _alternateMouseActions, _data, _selectedActor, _selectedMapper, clearElementView(), elementPicked(), INFO(), and requestViewUpdate().

◆ OnRightButtonDown()

void VtkCustomInteractorStyle::OnRightButtonDown ( )
override

Handles middle mouse button events (rotation point picking).

Definition at line 242 of file VtkCustomInteractorStyle.cpp.

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}

References _alternateMouseActions, _data, INFO(), and requestViewUpdate().

◆ pickableDataObject

void VtkCustomInteractorStyle::pickableDataObject ( vtkDataObject * object)
slot

Sets the highlightable vtk object.

Definition at line 126 of file VtkCustomInteractorStyle.cpp.

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}

References _data, _selectedActor, and _selectedMapper.

◆ removeHighlightActor

void VtkCustomInteractorStyle::removeHighlightActor ( )
slot

Removes the highlight actor from the visible scene.

Definition at line 109 of file VtkCustomInteractorStyle.cpp.

110{
111 this->Interactor->GetRenderWindow()
112 ->GetRenderers()
113 ->GetFirstRenderer()
114 ->RemoveActor(_selectedActor);
115}

References _selectedActor.

◆ requestViewUpdate

void VtkCustomInteractorStyle::requestViewUpdate ( )
signal

Emitted when something was picked.

Referenced by OnLeftButtonDown(), and OnRightButtonDown().

◆ setHighlightActor

void VtkCustomInteractorStyle::setHighlightActor ( bool on)
slot

Definition at line 117 of file VtkCustomInteractorStyle.cpp.

118{
119 _highlightActor = on;
120 if (!on)
121 {
122 HighlightProp((vtkProp*)nullptr);
123 }
124}

References _highlightActor.

Referenced by VisualizationWidget::on_highlightToolButton_toggled().

◆ vtkTypeMacro()

VtkCustomInteractorStyle::vtkTypeMacro ( VtkCustomInteractorStyle ,
vtkInteractorStyleTrackballCamera  )

Member Data Documentation

◆ _alternateMouseActions

bool VtkCustomInteractorStyle::_alternateMouseActions {false}
private

Definition at line 84 of file VtkCustomInteractorStyle.h.

84{false};

Referenced by OnKeyDown(), OnKeyUp(), OnLeftButtonDown(), and OnRightButtonDown().

◆ _data

vtkDataObject* VtkCustomInteractorStyle::_data {nullptr}
protected

The vtk object to pick.

Definition at line 74 of file VtkCustomInteractorStyle.h.

74{nullptr};

Referenced by OnLeftButtonDown(), OnRightButtonDown(), and pickableDataObject().

◆ _highlightActor

bool VtkCustomInteractorStyle::_highlightActor {false}
private

Definition at line 83 of file VtkCustomInteractorStyle.h.

83{false};

Referenced by highlightActor(), and setHighlightActor().

◆ _selectedActor

vtkActor* VtkCustomInteractorStyle::_selectedActor
protected

The actor for highlighting the selected cell.

Definition at line 80 of file VtkCustomInteractorStyle.h.

Referenced by VtkCustomInteractorStyle(), ~VtkCustomInteractorStyle(), OnLeftButtonDown(), pickableDataObject(), and removeHighlightActor().

◆ _selectedMapper

vtkDataSetMapper* VtkCustomInteractorStyle::_selectedMapper
protected

The mapper for highlighting the selected cell.

Definition at line 77 of file VtkCustomInteractorStyle.h.

Referenced by VtkCustomInteractorStyle(), ~VtkCustomInteractorStyle(), OnLeftButtonDown(), and pickableDataObject().


The documentation for this class was generated from the following files: