18 #include <vtkCallbackCommand.h>
19 #include <vtkCommand.h>
20 #include <vtkHandleRepresentation.h>
21 #include <vtkLineRepresentation.h>
23 #include <vtkObjectFactory.h>
24 #include <vtkPointHandleRepresentation3D.h>
25 #include <vtkRenderWindowInteractor.h>
26 #include <vtkVersion.h>
38 : KeyPressObserverId(0),
39 KeyReleaseObserverId(0),
40 KeyboardCallbackCommand(nullptr) {}
44 if (this->Interactor) {
45 if (this->KeyPressObserverId) {
46 this->Interactor->RemoveObserver(this->KeyPressObserverId);
48 if (this->KeyReleaseObserverId) {
49 this->Interactor->RemoveObserver(this->KeyReleaseObserverId);
52 if (this->KeyboardCallbackCommand) {
53 this->KeyboardCallbackCommand->Delete();
54 this->KeyboardCallbackCommand =
nullptr;
61 vtkRenderWindowInteractor* iren) {
63 this->Superclass::SetInteractor(iren);
72 if (enabling && this->Interactor && this->KeyPressObserverId == 0) {
75 if (!this->KeyboardCallbackCommand) {
76 this->KeyboardCallbackCommand = vtkCallbackCommand::New();
77 this->KeyboardCallbackCommand->SetCallback(
79 this->KeyboardCallbackCommand->SetClientData(
this);
83 this->KeyPressObserverId = this->Interactor->AddObserver(
84 vtkCommand::KeyPressEvent, this->KeyboardCallbackCommand, 10.0);
85 this->KeyReleaseObserverId = this->Interactor->AddObserver(
86 vtkCommand::KeyReleaseEvent, this->KeyboardCallbackCommand,
92 this->Superclass::SetEnabled(enabling);
94 if (enabling && this->Interactor) {
98 unsigned long savedKeyPress = this->KeyPressObserverId;
99 unsigned long savedKeyRelease = this->KeyReleaseObserverId;
100 vtkCallbackCommand* savedCallback = this->KeyboardCallbackCommand;
103 this->Interactor->RemoveObservers(vtkCommand::KeyPressEvent);
104 this->Interactor->RemoveObservers(vtkCommand::KeyReleaseEvent);
108 this->KeyPressObserverId = this->Interactor->AddObserver(
109 vtkCommand::KeyPressEvent, savedCallback, 10.0);
110 this->KeyReleaseObserverId = this->Interactor->AddObserver(
111 vtkCommand::KeyReleaseEvent, savedCallback, 10.0);
115 if (!enabling && this->Interactor) {
117 if (this->KeyPressObserverId) {
118 this->Interactor->RemoveObserver(this->KeyPressObserverId);
119 this->KeyPressObserverId = 0;
121 if (this->KeyReleaseObserverId) {
122 this->Interactor->RemoveObserver(this->KeyReleaseObserverId);
123 this->KeyReleaseObserverId = 0;
150 if (!self->GetEnabled()) {
157 if (!self->Interactor || !self->WidgetRep) {
161 if (!self->GetProcessEvents()) {
165 vtkLineRepresentation* rep =
166 vtkLineRepresentation::SafeDownCast(self->WidgetRep);
172 char* cKeySym =
nullptr;
174 if (self->Interactor) {
175 cKeySym =
self->Interactor->GetKeySym();
185 std::string keySym = cKeySym;
186 std::transform(keySym.begin(), keySym.end(), keySym.begin(), ::toupper);
190 bool isConstraintKey =
191 (keySym ==
"X" || keySym ==
"Y" || keySym ==
"Z" || keySym ==
"L");
192 if (!isConstraintKey) {
197 if (
event == vtkCommand::KeyPressEvent) {
199 rep->GetPoint1Representation()->SetXTranslationAxisOn();
200 rep->GetPoint2Representation()->SetXTranslationAxisOn();
201 rep->GetLineHandleRepresentation()->SetXTranslationAxisOn();
202 rep->GetPoint1Representation()->SetConstrained(
true);
203 rep->GetPoint2Representation()->SetConstrained(
true);
204 rep->GetLineHandleRepresentation()->SetConstrained(
true);
205 }
else if (keySym ==
"Y") {
206 rep->GetPoint1Representation()->SetYTranslationAxisOn();
207 rep->GetPoint2Representation()->SetYTranslationAxisOn();
208 rep->GetLineHandleRepresentation()->SetYTranslationAxisOn();
209 rep->GetPoint1Representation()->SetConstrained(
true);
210 rep->GetPoint2Representation()->SetConstrained(
true);
211 rep->GetLineHandleRepresentation()->SetConstrained(
true);
212 }
else if (keySym ==
"Z") {
213 rep->GetPoint1Representation()->SetZTranslationAxisOn();
214 rep->GetPoint2Representation()->SetZTranslationAxisOn();
215 rep->GetLineHandleRepresentation()->SetZTranslationAxisOn();
216 rep->GetPoint1Representation()->SetConstrained(
true);
217 rep->GetPoint2Representation()->SetConstrained(
true);
218 rep->GetLineHandleRepresentation()->SetConstrained(
true);
219 }
else if (keySym ==
"L") {
223 double p1[3], p2[3], v[3];
224 rep->GetPoint1WorldPosition(p1);
225 rep->GetPoint2WorldPosition(p2);
226 vtkMath::Subtract(p2, p1, v);
227 vtkMath::Normalize(v);
231 rep->GetPoint1Representation());
233 rep->GetPoint2Representation());
235 rep->GetLineHandleRepresentation());
237 if (h1 && h2 && hLine) {
241 h1->SetCustomTranslationAxis(v);
242 h2->SetCustomTranslationAxisOn();
243 h2->SetCustomTranslationAxis(v);
244 hLine->SetCustomTranslationAxisOn();
245 hLine->SetCustomTranslationAxis(v);
246 h1->SetConstrained(
true);
247 h2->SetConstrained(
true);
248 hLine->SetConstrained(
true);
251 #if (VTK_MAJOR_VERSION > 9) || \
252 (VTK_MAJOR_VERSION == 9 && VTK_MINOR_VERSION >= 3)
254 rep->GetPoint1Representation()->SetCustomTranslationAxisOn();
255 rep->GetPoint1Representation()->SetCustomTranslationAxis(v);
256 rep->GetPoint2Representation()->SetCustomTranslationAxisOn();
257 rep->GetPoint2Representation()->SetCustomTranslationAxis(v);
258 rep->GetLineHandleRepresentation()
259 ->SetCustomTranslationAxisOn();
260 rep->GetLineHandleRepresentation()->SetCustomTranslationAxis(v);
261 rep->GetPoint1Representation()->SetConstrained(
true);
262 rep->GetPoint2Representation()->SetConstrained(
true);
263 rep->GetLineHandleRepresentation()->SetConstrained(
true);
267 "L key: Custom axis handle representation not in use. "
268 "Please use cvCustomAxisHandleRepresentation for full "
273 }
else if (
event == vtkCommand::KeyReleaseEvent) {
274 if (keySym ==
"L" || keySym ==
"X" || keySym ==
"Y" || keySym ==
"Z") {
277 rep->GetPoint1Representation());
279 rep->GetPoint2Representation());
281 rep->GetLineHandleRepresentation());
283 if (h1 && h2 && hLine) {
287 h2->SetCustomTranslationAxisOff();
288 hLine->SetCustomTranslationAxisOff();
291 h1->SetTranslationAxis(-1);
292 h2->SetTranslationAxis(-1);
293 hLine->SetTranslationAxis(-1);
296 #if (VTK_MAJOR_VERSION > 9) || \
297 (VTK_MAJOR_VERSION == 9 && VTK_MINOR_VERSION >= 3)
299 rep->GetPoint1Representation()->SetTranslationAxisOff();
300 rep->GetPoint2Representation()->SetTranslationAxisOff();
301 rep->GetLineHandleRepresentation()->SetTranslationAxisOff();
304 rep->GetPoint1Representation()->SetTranslationAxis(-1);
305 rep->GetPoint2Representation()->SetTranslationAxis(-1);
306 rep->GetLineHandleRepresentation()->SetTranslationAxis(-1);
310 rep->GetPoint1Representation()->SetConstrained(
false);
311 rep->GetPoint2Representation()->SetConstrained(
false);
312 rep->GetLineHandleRepresentation()->SetConstrained(
false);
319 this->Superclass::PrintSelf(os, indent);
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Handle representation with custom translation axis support.
void SetCustomTranslationAxisOff()
Disable custom translation axis mode.
void SetCustomTranslationAxisOn()
Enable custom translation axis mode.