forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkImplicitCylinderWidget.cxx
380 lines (328 loc) · 15.8 KB
/
vtkImplicitCylinderWidget.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImplicitCylinderWidget.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkImplicitCylinderWidget.h"
#include "vtkCallbackCommand.h"
#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkEvent.h"
#include "vtkImplicitCylinderRepresentation.h"
#include "vtkObjectFactory.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkStdString.h"
#include "vtkWidgetCallbackMapper.h"
#include "vtkWidgetEvent.h"
#include "vtkWidgetEventTranslator.h"
vtkStandardNewMacro(vtkImplicitCylinderWidget);
//------------------------------------------------------------------------------
vtkImplicitCylinderWidget::vtkImplicitCylinderWidget()
{
this->WidgetState = vtkImplicitCylinderWidget::Start;
// Define widget events
this->CallbackMapper->SetCallbackMethod(vtkCommand::LeftButtonPressEvent, vtkWidgetEvent::Select,
this, vtkImplicitCylinderWidget::SelectAction);
this->CallbackMapper->SetCallbackMethod(vtkCommand::LeftButtonReleaseEvent,
vtkWidgetEvent::EndSelect, this, vtkImplicitCylinderWidget::EndSelectAction);
this->CallbackMapper->SetCallbackMethod(vtkCommand::MiddleButtonPressEvent,
vtkWidgetEvent::Translate, this, vtkImplicitCylinderWidget::TranslateAction);
this->CallbackMapper->SetCallbackMethod(vtkCommand::MiddleButtonReleaseEvent,
vtkWidgetEvent::EndTranslate, this, vtkImplicitCylinderWidget::EndSelectAction);
this->CallbackMapper->SetCallbackMethod(vtkCommand::RightButtonPressEvent, vtkWidgetEvent::Scale,
this, vtkImplicitCylinderWidget::ScaleAction);
this->CallbackMapper->SetCallbackMethod(vtkCommand::RightButtonReleaseEvent,
vtkWidgetEvent::EndScale, this, vtkImplicitCylinderWidget::EndSelectAction);
this->CallbackMapper->SetCallbackMethod(
vtkCommand::MouseMoveEvent, vtkWidgetEvent::Move, this, vtkImplicitCylinderWidget::MoveAction);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyPressEvent, vtkEvent::AnyModifier, 30, 1,
"Up", vtkWidgetEvent::Up, this, vtkImplicitCylinderWidget::MoveCylinderAction);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyPressEvent, vtkEvent::AnyModifier, 28, 1,
"Right", vtkWidgetEvent::Up, this, vtkImplicitCylinderWidget::MoveCylinderAction);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyPressEvent, vtkEvent::AnyModifier, 31, 1,
"Down", vtkWidgetEvent::Down, this, vtkImplicitCylinderWidget::MoveCylinderAction);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyPressEvent, vtkEvent::AnyModifier, 29, 1,
"Left", vtkWidgetEvent::Down, this, vtkImplicitCylinderWidget::MoveCylinderAction);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyPressEvent, vtkEvent::AnyModifier, 'x', 1,
"x", vtkWidgetEvent::ModifyEvent, this, vtkImplicitCylinderWidget::TranslationAxisLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyPressEvent, vtkEvent::AnyModifier, 'X', 1,
"X", vtkWidgetEvent::ModifyEvent, this, vtkImplicitCylinderWidget::TranslationAxisLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyPressEvent, vtkEvent::AnyModifier, 'y', 1,
"y", vtkWidgetEvent::ModifyEvent, this, vtkImplicitCylinderWidget::TranslationAxisLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyPressEvent, vtkEvent::AnyModifier, 'Y', 1,
"Y", vtkWidgetEvent::ModifyEvent, this, vtkImplicitCylinderWidget::TranslationAxisLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyPressEvent, vtkEvent::AnyModifier, 'z', 1,
"z", vtkWidgetEvent::ModifyEvent, this, vtkImplicitCylinderWidget::TranslationAxisLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyPressEvent, vtkEvent::AnyModifier, 'Z', 1,
"Z", vtkWidgetEvent::ModifyEvent, this, vtkImplicitCylinderWidget::TranslationAxisLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyReleaseEvent, vtkEvent::AnyModifier, 'x',
1, "x", vtkWidgetEvent::Reset, this, vtkImplicitCylinderWidget::TranslationAxisUnLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyReleaseEvent, vtkEvent::AnyModifier, 'X',
1, "X", vtkWidgetEvent::Reset, this, vtkImplicitCylinderWidget::TranslationAxisUnLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyReleaseEvent, vtkEvent::AnyModifier, 'y',
1, "y", vtkWidgetEvent::Reset, this, vtkImplicitCylinderWidget::TranslationAxisUnLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyReleaseEvent, vtkEvent::AnyModifier, 'Y',
1, "Y", vtkWidgetEvent::Reset, this, vtkImplicitCylinderWidget::TranslationAxisUnLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyReleaseEvent, vtkEvent::AnyModifier, 'z',
1, "z", vtkWidgetEvent::Reset, this, vtkImplicitCylinderWidget::TranslationAxisUnLock);
this->CallbackMapper->SetCallbackMethod(vtkCommand::KeyReleaseEvent, vtkEvent::AnyModifier, 'Z',
1, "Z", vtkWidgetEvent::Reset, this, vtkImplicitCylinderWidget::TranslationAxisUnLock);
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::SelectAction(vtkAbstractWidget* w)
{
vtkImplicitCylinderWidget* self = reinterpret_cast<vtkImplicitCylinderWidget*>(w);
// Get the event position
int X = self->Interactor->GetEventPosition()[0];
int Y = self->Interactor->GetEventPosition()[1];
// We want to update the radius, axis and center as appropriate
reinterpret_cast<vtkImplicitCylinderRepresentation*>(self->WidgetRep)
->SetInteractionState(vtkImplicitCylinderRepresentation::Moving);
int interactionState = self->WidgetRep->ComputeInteractionState(X, Y);
self->UpdateCursorShape(interactionState);
if (self->WidgetRep->GetInteractionState() == vtkImplicitCylinderRepresentation::Outside)
{
return;
}
if (self->Interactor->GetControlKey() &&
interactionState == vtkImplicitCylinderRepresentation::MovingCenter)
{
reinterpret_cast<vtkImplicitCylinderRepresentation*>(self->WidgetRep)
->SetInteractionState(vtkImplicitCylinderRepresentation::TranslatingCenter);
}
// We are definitely selected
self->GrabFocus(self->EventCallbackCommand);
double eventPos[2];
eventPos[0] = static_cast<double>(X);
eventPos[1] = static_cast<double>(Y);
self->WidgetState = vtkImplicitCylinderWidget::Active;
self->WidgetRep->StartWidgetInteraction(eventPos);
self->EventCallbackCommand->SetAbortFlag(1);
self->StartInteraction();
self->InvokeEvent(vtkCommand::StartInteractionEvent, nullptr);
self->Render();
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::TranslateAction(vtkAbstractWidget* w)
{
vtkImplicitCylinderWidget* self = reinterpret_cast<vtkImplicitCylinderWidget*>(w);
// Get the event position
int X = self->Interactor->GetEventPosition()[0];
int Y = self->Interactor->GetEventPosition()[1];
// We want to compute an orthogonal vector to the pane that has been selected
reinterpret_cast<vtkImplicitCylinderRepresentation*>(self->WidgetRep)
->SetInteractionState(vtkImplicitCylinderRepresentation::Moving);
int interactionState = self->WidgetRep->ComputeInteractionState(X, Y);
self->UpdateCursorShape(interactionState);
if (self->WidgetRep->GetInteractionState() == vtkImplicitCylinderRepresentation::Outside)
{
return;
}
// We are definitely selected
self->GrabFocus(self->EventCallbackCommand);
double eventPos[2];
eventPos[0] = static_cast<double>(X);
eventPos[1] = static_cast<double>(Y);
self->WidgetState = vtkImplicitCylinderWidget::Active;
self->WidgetRep->StartWidgetInteraction(eventPos);
self->EventCallbackCommand->SetAbortFlag(1);
self->StartInteraction();
self->InvokeEvent(vtkCommand::StartInteractionEvent, nullptr);
self->Render();
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::ScaleAction(vtkAbstractWidget* w)
{
vtkImplicitCylinderWidget* self = reinterpret_cast<vtkImplicitCylinderWidget*>(w);
// Get the event position
int X = self->Interactor->GetEventPosition()[0];
int Y = self->Interactor->GetEventPosition()[1];
// We want to compute an orthogonal vector to the pane that has been selected
reinterpret_cast<vtkImplicitCylinderRepresentation*>(self->WidgetRep)
->SetInteractionState(vtkImplicitCylinderRepresentation::Scaling);
int interactionState = self->WidgetRep->ComputeInteractionState(X, Y);
self->UpdateCursorShape(interactionState);
if (self->WidgetRep->GetInteractionState() == vtkImplicitCylinderRepresentation::Outside)
{
return;
}
// We are definitely selected
self->GrabFocus(self->EventCallbackCommand);
double eventPos[2];
eventPos[0] = static_cast<double>(X);
eventPos[1] = static_cast<double>(Y);
self->WidgetState = vtkImplicitCylinderWidget::Active;
self->WidgetRep->StartWidgetInteraction(eventPos);
self->EventCallbackCommand->SetAbortFlag(1);
self->StartInteraction();
self->InvokeEvent(vtkCommand::StartInteractionEvent, nullptr);
self->Render();
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::MoveAction(vtkAbstractWidget* w)
{
vtkImplicitCylinderWidget* self = reinterpret_cast<vtkImplicitCylinderWidget*>(w);
// So as to change the cursor shape when the mouse is poised over
// the widget. Unfortunately, this results in a few extra picks
// due to the cell picker. However given that its picking simple geometry
// like the handles/arrows, this should be very quick
int X = self->Interactor->GetEventPosition()[0];
int Y = self->Interactor->GetEventPosition()[1];
int changed = 0;
if (self->ManagesCursor && self->WidgetState != vtkImplicitCylinderWidget::Active)
{
int oldInteractionState =
reinterpret_cast<vtkImplicitCylinderRepresentation*>(self->WidgetRep)->GetInteractionState();
reinterpret_cast<vtkImplicitCylinderRepresentation*>(self->WidgetRep)
->SetInteractionState(vtkImplicitCylinderRepresentation::Moving);
int state = self->WidgetRep->ComputeInteractionState(X, Y);
changed = self->UpdateCursorShape(state);
reinterpret_cast<vtkImplicitCylinderRepresentation*>(self->WidgetRep)
->SetInteractionState(oldInteractionState);
changed = (changed || state != oldInteractionState) ? 1 : 0;
}
// See whether we're active
if (self->WidgetState == vtkImplicitCylinderWidget::Start)
{
if (changed && self->ManagesCursor)
{
self->Render();
}
return;
}
// Okay, adjust the representation
double e[2];
e[0] = static_cast<double>(X);
e[1] = static_cast<double>(Y);
self->WidgetRep->WidgetInteraction(e);
// moving something
self->EventCallbackCommand->SetAbortFlag(1);
self->InvokeEvent(vtkCommand::InteractionEvent, nullptr);
self->Render();
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::EndSelectAction(vtkAbstractWidget* w)
{
vtkImplicitCylinderWidget* self = reinterpret_cast<vtkImplicitCylinderWidget*>(w);
if (self->WidgetState != vtkImplicitCylinderWidget::Active ||
self->WidgetRep->GetInteractionState() == vtkImplicitCylinderRepresentation::Outside)
{
return;
}
// Return state to not selected
double e[2];
self->WidgetRep->EndWidgetInteraction(e);
self->WidgetState = vtkImplicitCylinderWidget::Start;
self->ReleaseFocus();
// Update cursor if managed
self->UpdateCursorShape(reinterpret_cast<vtkImplicitCylinderRepresentation*>(self->WidgetRep)
->GetRepresentationState());
self->EventCallbackCommand->SetAbortFlag(1);
self->EndInteraction();
self->InvokeEvent(vtkCommand::EndInteractionEvent, nullptr);
self->Render();
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::MoveCylinderAction(vtkAbstractWidget* w)
{
vtkImplicitCylinderWidget* self = reinterpret_cast<vtkImplicitCylinderWidget*>(w);
reinterpret_cast<vtkImplicitCylinderRepresentation*>(self->WidgetRep)
->SetInteractionState(vtkImplicitCylinderRepresentation::Moving);
int X = self->Interactor->GetEventPosition()[0];
int Y = self->Interactor->GetEventPosition()[1];
self->WidgetRep->ComputeInteractionState(X, Y);
// The cursor must be over part of the widget for these key presses to work
if (self->WidgetRep->GetInteractionState() == vtkImplicitCylinderRepresentation::Outside)
{
return;
}
// Invoke all of the events associated with moving the cylinder
self->InvokeEvent(vtkCommand::StartInteractionEvent, nullptr);
// Move the cylinder
double factor = (self->Interactor->GetControlKey() ? 0.5 : 1.0);
if (vtkStdString(self->Interactor->GetKeySym()) == vtkStdString("Down") ||
vtkStdString(self->Interactor->GetKeySym()) == vtkStdString("Left"))
{
self->GetCylinderRepresentation()->BumpCylinder(-1, factor);
}
else
{
self->GetCylinderRepresentation()->BumpCylinder(1, factor);
}
self->InvokeEvent(vtkCommand::InteractionEvent, nullptr);
self->EventCallbackCommand->SetAbortFlag(1);
self->InvokeEvent(vtkCommand::EndInteractionEvent, nullptr);
self->Render();
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::CreateDefaultRepresentation()
{
if (!this->WidgetRep)
{
this->WidgetRep = vtkImplicitCylinderRepresentation::New();
}
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::SetRepresentation(vtkImplicitCylinderRepresentation* rep)
{
this->Superclass::SetWidgetRepresentation(reinterpret_cast<vtkWidgetRepresentation*>(rep));
}
//------------------------------------------------------------------------------
int vtkImplicitCylinderWidget::UpdateCursorShape(int state)
{
// So as to change the cursor shape when the mouse is poised over
// the widget.
if (this->ManagesCursor)
{
if (state == vtkImplicitCylinderRepresentation::Outside)
{
return this->RequestCursorShape(VTK_CURSOR_DEFAULT);
}
else if (state == vtkImplicitCylinderRepresentation::MovingOutline)
{
return this->RequestCursorShape(VTK_CURSOR_SIZEALL);
}
else
{
return this->RequestCursorShape(VTK_CURSOR_HAND);
}
}
return 0;
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::TranslationAxisLock(vtkAbstractWidget* widget)
{
vtkImplicitCylinderWidget* self = reinterpret_cast<vtkImplicitCylinderWidget*>(widget);
vtkImplicitCylinderRepresentation* rep =
vtkImplicitCylinderRepresentation::SafeDownCast(self->WidgetRep);
if (self->Interactor->GetKeyCode() == 'x' || self->Interactor->GetKeyCode() == 'X')
{
rep->SetXTranslationAxisOn();
}
if (self->Interactor->GetKeyCode() == 'y' || self->Interactor->GetKeyCode() == 'Y')
{
rep->SetYTranslationAxisOn();
}
if (self->Interactor->GetKeyCode() == 'z' || self->Interactor->GetKeyCode() == 'Z')
{
rep->SetZTranslationAxisOn();
}
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::TranslationAxisUnLock(vtkAbstractWidget* widget)
{
vtkImplicitCylinderWidget* self = reinterpret_cast<vtkImplicitCylinderWidget*>(widget);
vtkImplicitCylinderRepresentation::SafeDownCast(self->WidgetRep)->SetTranslationAxisOff();
}
//------------------------------------------------------------------------------
void vtkImplicitCylinderWidget::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}