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 24 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 34 of file VtkCustomInteractorStyle.cpp.

35{
36 _selectedMapper = vtkDataSetMapper::New();
37 _selectedActor = vtkActor::New();
39 _selectedActor->GetProperty()->EdgeVisibilityOn();
40 _selectedActor->GetProperty()->SetEdgeColor(1, 0, 0);
41 _selectedActor->GetProperty()->SetLineWidth(3);
42}
vtkDataSetMapper * _selectedMapper
The mapper for highlighting the selected cell.
vtkActor * _selectedActor
The actor for highlighting the selected cell.

References _selectedActor, and _selectedMapper.

Referenced by New(), and vtkTypeMacro().

◆ ~VtkCustomInteractorStyle()

VtkCustomInteractorStyle::~VtkCustomInteractorStyle ( )
overrideprotected

Definition at line 44 of file VtkCustomInteractorStyle.cpp.

45{
46 _selectedActor->Delete();
47 _selectedMapper->Delete();
48}

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 90 of file VtkCustomInteractorStyle.cpp.

91{
93 {
94 HighlightProp((vtkProp*)actor);
95 }
96}

References _highlightActor.

◆ New()

VtkCustomInteractorStyle * VtkCustomInteractorStyle::New ( )
static

◆ OnChar()

void VtkCustomInteractorStyle::OnChar ( )
override

Handles key press events.

Definition at line 50 of file VtkCustomInteractorStyle.cpp.

51{
52 switch (Interactor->GetKeyCode())
53 {
54 case '3':
55 INFO("The 3 key was pressed.");
56 break;
57 case 'a':
58 break;
59 default:
60 vtkInteractorStyleTrackballCamera::OnChar();
61 }
62}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28

References INFO().

◆ OnKeyDown()

void VtkCustomInteractorStyle::OnKeyDown ( )
override

Handles key down events.

Definition at line 64 of file VtkCustomInteractorStyle.cpp.

65{
66 switch (Interactor->GetKeyCode())
67 {
68 case 32: // Space
70 emit cursorChanged(Qt::CrossCursor);
71 break;
72 default:
73 vtkInteractorStyleTrackballCamera::OnKeyDown();
74 }
75}
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 77 of file VtkCustomInteractorStyle.cpp.

78{
79 switch (Interactor->GetKeyCode())
80 {
81 case 32: // Space
83 emit cursorChanged(Qt::ArrowCursor);
84 break;
85 default:
86 vtkInteractorStyleTrackballCamera::OnKeyUp();
87 }
88}

References _alternateMouseActions, and cursorChanged().

◆ OnLeftButtonDown()

void VtkCustomInteractorStyle::OnLeftButtonDown ( )
override

Handles left mouse button events (picking).

Definition at line 129 of file VtkCustomInteractorStyle.cpp.

130{
131 if (!_data)
132 {
133 return vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
134 }
135
137 {
138 // Get the location of the click (in window coordinates)
139 int* pos = this->GetInteractor()->GetEventPosition();
140
141 vtkSmartPointer<vtkCellPicker> picker =
142 vtkSmartPointer<vtkCellPicker>::New();
143 picker->SetTolerance(0.0005);
144
145 // Pick from this location.
146 picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
147
148 double* worldPosition = picker->GetPickPosition();
149 INFO("Cell id is: {:d}", picker->GetCellId());
150
151 if (picker->GetCellId() != -1)
152 {
153 INFO("Pick position is: {:f} {:f} {:f}", worldPosition[0],
154 worldPosition[1], worldPosition[2]);
155
156 vtkSmartPointer<vtkIdTypeArray> ids =
157 vtkSmartPointer<vtkIdTypeArray>::New();
158 ids->SetNumberOfValues(1);
159 ids->SetValue(0, picker->GetCellId());
160
161 vtkSmartPointer<vtkSelectionNode> selectionNode =
162 vtkSmartPointer<vtkSelectionNode>::New();
163 selectionNode->SetFieldType(vtkSelectionNode::CELL);
164 selectionNode->SetContentType(vtkSelectionNode::INDICES);
165 selectionNode->SetSelectionList(ids);
166
167 vtkSmartPointer<vtkSelection> selection =
168 vtkSmartPointer<vtkSelection>::New();
169 selection->AddNode(selectionNode);
170
171 vtkSmartPointer<vtkExtractSelection> extractSelection =
172 vtkSmartPointer<vtkExtractSelection>::New();
173 extractSelection->SetInputData(0, _data);
174 extractSelection->SetInputData(1, selection);
175 extractSelection->Update();
176
177 // In selection
178 vtkSmartPointer<vtkUnstructuredGrid> selected =
179 vtkSmartPointer<vtkUnstructuredGrid>::New();
180 selected->ShallowCopy(extractSelection->GetOutput());
181
182 INFO("There are {:d} points in the selection.",
183 selected->GetNumberOfPoints());
184 INFO("There are {:d} cells in the selection.",
185 selected->GetNumberOfCells());
186
187 // check if the underlying object is a mesh and if so, send a signal
188 // to the element model for display of information about the picked
189 // element.
190 vtkAlgorithm* data_set = picker->GetActor()
191 ->GetMapper()
192 ->GetInputConnection(0, 0)
193 ->GetProducer()
194 ->GetInputConnection(0, 0)
195 ->GetProducer();
196 auto* source =
197 dynamic_cast<vtkUnstructuredGridAlgorithm*>(data_set);
198 if (source)
199 {
200 emit elementPicked(source,
201 static_cast<unsigned>(picker->GetCellId()));
202 }
203 else
204 {
205 emit clearElementView();
206 }
207 _selectedMapper->SetInputData(selected);
208
209 this->Interactor->GetRenderWindow()
210 ->GetRenderers()
211 ->GetFirstRenderer()
212 ->AddActor(_selectedActor);
213 //_highlightActor = true;
214 }
215 else
216 {
217 this->Interactor->GetRenderWindow()
218 ->GetRenderers()
219 ->GetFirstRenderer()
220 ->RemoveActor(_selectedActor);
221 }
222 emit requestViewUpdate();
223 }
224 else
225 {
226 // Forward events
227 vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
228 }
229}
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:216

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 231 of file VtkCustomInteractorStyle.cpp.

232{
233 if (!_data)
234 {
235 return vtkInteractorStyleTrackballCamera::OnRightButtonDown();
236 }
237
239 {
240 // Get the location of the click (in window coordinates)
241 int* pos = this->GetInteractor()->GetEventPosition();
242
243 vtkSmartPointer<vtkCellPicker> picker =
244 vtkSmartPointer<vtkCellPicker>::New();
245 picker->SetTolerance(0.0005);
246
247 // Pick from this location.
248 picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
249
250 double* worldPosition = picker->GetPickPosition();
251 INFO("Cell id is: {:d}", picker->GetCellId());
252
253 if (picker->GetCellId() != -1)
254 {
255 vtkRenderer* renderer = this->Interactor->GetRenderWindow()
256 ->GetRenderers()
257 ->GetFirstRenderer();
258 vtkCamera* cam = renderer->GetActiveCamera();
259 cam->SetFocalPoint(worldPosition);
260 emit requestViewUpdate();
261 }
262 }
263 else
264 {
265 // Forward events
266 vtkInteractorStyleTrackballCamera::OnRightButtonDown();
267 }
268}

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

◆ pickableDataObject

void VtkCustomInteractorStyle::pickableDataObject ( vtkDataObject * object)
slot

Sets the highlightable vtk object.

Definition at line 115 of file VtkCustomInteractorStyle.cpp.

116{
117 _data = object;
118 if (!object)
119 {
120 this->Interactor->GetRenderWindow()
121 ->GetRenderers()
122 ->GetFirstRenderer()
123 ->RemoveActor(_selectedActor);
124 _selectedMapper->SetInputConnection(nullptr);
125 }
126}

References _data, _selectedActor, and _selectedMapper.

◆ removeHighlightActor

void VtkCustomInteractorStyle::removeHighlightActor ( )
slot

Removes the highlight actor from the visible scene.

Definition at line 98 of file VtkCustomInteractorStyle.cpp.

99{
100 this->Interactor->GetRenderWindow()
101 ->GetRenderers()
102 ->GetFirstRenderer()
103 ->RemoveActor(_selectedActor);
104}

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 106 of file VtkCustomInteractorStyle.cpp.

107{
108 _highlightActor = on;
109 if (!on)
110 {
111 HighlightProp((vtkProp*)nullptr);
112 }
113}

References _highlightActor.

◆ vtkTypeMacro()

VtkCustomInteractorStyle::vtkTypeMacro ( VtkCustomInteractorStyle ,
vtkInteractorStyleTrackballCamera  )

Member Data Documentation

◆ _alternateMouseActions

bool VtkCustomInteractorStyle::_alternateMouseActions {false}
private

Definition at line 73 of file VtkCustomInteractorStyle.h.

73{false};

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

◆ _data

vtkDataObject* VtkCustomInteractorStyle::_data {nullptr}
protected

The vtk object to pick.

Definition at line 63 of file VtkCustomInteractorStyle.h.

63{nullptr};

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

◆ _highlightActor

bool VtkCustomInteractorStyle::_highlightActor {false}
private

Definition at line 72 of file VtkCustomInteractorStyle.h.

72{false};

Referenced by highlightActor(), and setHighlightActor().

◆ _selectedActor

vtkActor* VtkCustomInteractorStyle::_selectedActor
protected

The actor for highlighting the selected cell.

Definition at line 69 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 66 of file VtkCustomInteractorStyle.h.

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


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