]> Creatis software - clitk.git/blob - vv/vvInteractorStyleNavigator.cxx
removed headers
[clitk.git] / vv / vvInteractorStyleNavigator.cxx
1 #include "vvInteractorStyleNavigator.h"
2
3 #include "vtkAbstractPropPicker.h"
4 #include "vtkAssemblyPath.h"
5 #include "vtkCallbackCommand.h"
6 #include "vtkMath.h"
7 #include "vtkObjectFactory.h"
8 #include "vtkCamera.h"
9 #include "vtkRenderWindow.h"
10 #include "vtkRenderWindowInteractor.h"
11 #include "vtkRenderer.h"
12 #include <vtkRendererCollection.h>
13 #include "clitkCommon.h"
14
15
16 vtkCxxRevisionMacro(vvInteractorStyleNavigator, "DummyRevision");
17 vtkStandardNewMacro(vvInteractorStyleNavigator);
18
19 //----------------------------------------------------------------------------
20 vvInteractorStyleNavigator::vvInteractorStyleNavigator()
21 {
22     this->WindowLevelStartPosition[0]   = 0;
23     this->WindowLevelStartPosition[1]   = 0;
24
25     this->WindowLevelCurrentPosition[0] = 0;
26     this->WindowLevelCurrentPosition[1] = 0;
27
28     this->MotionFactor   = 10.0;
29 }
30
31 //----------------------------------------------------------------------------
32 vvInteractorStyleNavigator::~vvInteractorStyleNavigator()
33 {
34     CurrentRenderer=NULL;
35 }
36
37 void vvInteractorStyleNavigator::FindPokedRenderer(int dummy1,int dummy2)
38 {
39     vtkRenderWindow * renwin=this->GetInteractor()->GetRenderWindow();
40     renwin->GetRenderers()->InitTraversal();
41     while (true)
42     {
43         vtkRenderer* current = renwin->GetRenderers()->GetNextItem();
44         if (current==NULL || current->GetDraw())
45         {
46             CurrentRenderer=current;
47             return;
48         }
49     }
50 }
51
52 //----------------------------------------------------------------------------
53 void vvInteractorStyleNavigator::StartWindowLevel()
54 {
55     if (this->State != VTKIS_NONE)
56     {
57         return;
58     }
59     this->StartState(VTKIS_WINDOW_LEVEL);
60     this->InvokeEvent(vtkCommand::StartWindowLevelEvent,this);
61 }
62
63 //----------------------------------------------------------------------------
64 void vvInteractorStyleNavigator::EndWindowLevel()
65 {
66     if (this->State != VTKIS_WINDOW_LEVEL)
67     {
68         return;
69     }
70     this->InvokeEvent(vtkCommand::EndWindowLevelEvent, this);
71     this->StopState();
72 }
73
74 //----------------------------------------------------------------------------
75 void vvInteractorStyleNavigator::StartPick()
76 {
77     if (this->State != VTKIS_NONE)
78     {
79         return;
80     }
81     this->StartState(VTKIS_PICK);
82     this->InvokeEvent(vtkCommand::StartPickEvent, this);
83 }
84
85 //----------------------------------------------------------------------------
86 void vvInteractorStyleNavigator::EndPick()
87 {
88     if (this->State != VTKIS_PICK)
89     {
90         return;
91     }
92     this->InvokeEvent(vtkCommand::EndPickEvent, this);
93     this->StopState();
94 }
95
96 //----------------------------------------------------------------------------
97 void vvInteractorStyleNavigator::OnMouseMove()
98 {
99     int x = this->Interactor->GetEventPosition()[0];
100     int y = this->Interactor->GetEventPosition()[1];
101
102     switch (this->State)
103     {
104     case VTKIS_WINDOW_LEVEL:
105         this->FindPokedRenderer(x, y);
106         this->WindowLevel();
107         this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
108         break;
109
110     case VTKIS_PICK:
111         this->FindPokedRenderer(x, y);
112         this->Pick();
113         this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
114         break;
115
116     case VTKIS_PAN:
117         this->FindPokedRenderer(x, y);
118         this->Pan();
119         this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
120         break;
121
122     case VTKIS_DOLLY:
123         this->FindPokedRenderer(x, y);
124         this->Dolly();
125         this->InvokeEvent(vtkCommand::InteractionEvent, NULL);
126         break;
127
128     default:
129         this->InvokeEvent(vtkCommand::UserEvent, NULL);
130         break;
131     }
132
133     // Call parent to handle all other states and perform additional work
134
135 }
136
137 void vvInteractorStyleNavigator::OnEnter()
138 {
139     //  int x = this->Interactor->GetEventPosition()[0];
140     //int y = this->Interactor->GetEventPosition()[1];
141
142     switch (this->State)
143     {
144     case VTKIS_WINDOW_LEVEL:
145         break;
146
147     case VTKIS_PICK:
148         break;
149
150     case VTKIS_PAN:
151         break;
152
153     default:
154         this->InvokeEvent(vtkCommand::EnterEvent, NULL);
155         break;
156     }
157
158     // Call parent to handle all other states and perform additional work
159
160 }
161
162 //----------------------------------------------------------------------------
163 void vvInteractorStyleNavigator::OnLeave()
164 {
165     //  int x = this->Interactor->GetEventPosition()[0];
166     //int y = this->Interactor->GetEventPosition()[1];
167
168     switch (this->State)
169     {
170     case VTKIS_WINDOW_LEVEL:
171         break;
172
173     case VTKIS_PICK:
174         break;
175
176     case VTKIS_PAN:
177         break;
178
179     default:
180         this->InvokeEvent(vtkCommand::LeaveEvent, NULL);
181         break;
182     }
183
184     // Call parent to handle all other states and perform additional work
185
186 }
187
188 //----------------------------------------------------------------------------
189 void vvInteractorStyleNavigator::OnRightButtonDown()
190 {
191     int x = this->Interactor->GetEventPosition()[0];
192     int y = this->Interactor->GetEventPosition()[1];
193
194     this->FindPokedRenderer(x, y);
195     if (this->CurrentRenderer == NULL)
196     {
197         return;
198     }
199
200     // Redefine this button to handle window/level
201     this->GrabFocus(this->EventCallbackCommand);
202     if (!this->Interactor->GetShiftKey() && !this->Interactor->GetControlKey())
203     {
204         this->WindowLevelStartPosition[0] = x;
205         this->WindowLevelStartPosition[1] = y;
206         this->StartWindowLevel();
207     }
208
209     // The rest of the button + key combinations remain the same
210
211     else
212     {
213         this->Superclass::OnRightButtonDown();
214     }
215 }
216
217 //----------------------------------------------------------------------------
218 void vvInteractorStyleNavigator::OnRightButtonUp()
219 {
220     switch (this->State)
221     {
222     case VTKIS_WINDOW_LEVEL:
223         this->EndWindowLevel();
224         if ( this->Interactor )
225         {
226             this->ReleaseFocus();
227         }
228         break;
229     }
230
231     // Call parent to handle all other states and perform additional work
232
233     this->Superclass::OnRightButtonUp();
234 }
235
236 //----------------------------------------------------------------------------
237 void vvInteractorStyleNavigator::OnLeftButtonDown()
238 {
239     int x = this->Interactor->GetEventPosition()[0];
240     int y = this->Interactor->GetEventPosition()[1];
241
242     this->FindPokedRenderer(x, y);
243     if (this->CurrentRenderer == NULL)
244     {
245         return;
246     }
247
248     // Redefine this button to handle pick
249     this->GrabFocus(this->EventCallbackCommand);
250     if (!this->Interactor->GetShiftKey() && !this->Interactor->GetControlKey())
251     {
252         this->StartPick();
253     }
254
255     // The rest of the button + key combinations remain the same
256
257     else
258     {
259         this->Superclass::OnLeftButtonDown();
260     }
261 }
262
263 //----------------------------------------------------------------------------
264 void vvInteractorStyleNavigator::OnLeftButtonUp()
265 {
266     switch (this->State)
267     {
268     case VTKIS_PICK:
269         this->EndPick();
270         if ( this->Interactor )
271         {
272             this->ReleaseFocus();
273         }
274         break;
275     }
276
277     // Call parent to handle all other states and perform additional work
278
279     this->Superclass::OnLeftButtonUp();
280 }
281
282 //----------------------------------------------------------------------------
283 void vvInteractorStyleNavigator::OnMiddleButtonDown()
284 {
285     this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
286                             this->Interactor->GetEventPosition()[1]);
287     if (this->CurrentRenderer == NULL)
288     {
289         return;
290     }
291     this->CurrentRenderer->GetRenderWindow()->SetCurrentCursor(8);
292     this->GrabFocus(this->EventCallbackCommand);
293     this->StartPan();
294 }
295
296 //----------------------------------------------------------------------------
297 void vvInteractorStyleNavigator::OnMiddleButtonUp()
298 {
299     switch (this->State)
300     {
301     case VTKIS_PAN:
302         this->EndPan();
303         if ( this->Interactor )
304         {
305             this->Interactor->GetRenderWindow()->SetCurrentCursor(0);
306             this->ReleaseFocus();
307         }
308         break;
309     }
310 }
311
312 //----------------------------------------------------------------------------
313 void vvInteractorStyleNavigator::OnChar()
314 {
315     vtkRenderWindowInteractor *rwi = this->Interactor;
316
317     switch (rwi->GetKeyCode())
318     {
319     case 'f' :
320     case 'F' :
321     {
322         this->AnimState = VTKIS_ANIM_ON;
323         this->AnimState = VTKIS_ANIM_OFF;
324         break;
325     }
326
327     case 'w'  :
328     case 'W'  :
329     {
330         this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
331                                 this->Interactor->GetEventPosition()[1]);
332         if (this->CurrentRenderer == NULL)
333         {
334             return;
335         }
336         this->GrabFocus(this->EventCallbackCommand);
337         this->StartDolly();
338         double factor = -2;
339         this->Dolly(pow((double)1.1, factor));
340         this->EndDolly();
341         this->ReleaseFocus();
342         break;
343     }
344     case 'x'  :
345     case 'X'  :
346     {
347         this->FindPokedRenderer(rwi->GetEventPosition()[0],
348                                 rwi->GetEventPosition()[1]);
349         if (this->CurrentRenderer == NULL)
350         {
351             return;
352         }
353         this->GrabFocus(this->EventCallbackCommand);
354         this->StartDolly();
355         double factor = 2;
356         this->Dolly(pow((double)1.1, factor));
357         this->EndDolly();
358         this->ReleaseFocus();
359         break;
360     }
361     case '3' :
362         // Disable StereoVision
363         break;
364     case 'r' :
365     case 'R' :
366         //Do nothing, this is handled in vvSlicerManagerCommand
367         break;
368     default:
369         this->Superclass::OnChar();
370         break;
371     }
372 }
373
374 //----------------------------------------------------------------------------
375 void vvInteractorStyleNavigator::OnMouseWheelForward()
376 {
377     this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
378                             this->Interactor->GetEventPosition()[1]);
379     if (this->CurrentRenderer == NULL)
380     {
381         return;
382     }
383     this->GrabFocus(this->EventCallbackCommand);
384     if (this->Interactor->GetControlKey())
385     {
386         this->StartDolly();
387         double factor = this->MotionFactor * 0.2 * this->MouseWheelMotionFactor;
388         this->Dolly(pow((double)1.1, factor));
389         this->EndDolly();
390     }
391     this->ReleaseFocus();
392     this->InvokeEvent(vtkCommand::MouseWheelForwardEvent, this);
393 }
394
395 //----------------------------------------------------------------------------
396 void vvInteractorStyleNavigator::OnMouseWheelBackward()
397 {
398     this->FindPokedRenderer(this->Interactor->GetEventPosition()[0],
399                             this->Interactor->GetEventPosition()[1]);
400     if (this->CurrentRenderer == NULL)
401     {
402         return;
403     }
404
405     this->GrabFocus(this->EventCallbackCommand);
406     if (this->Interactor->GetControlKey())
407     {
408         this->StartDolly();
409         double factor = this->MotionFactor * -0.2 * this->MouseWheelMotionFactor;
410         this->Dolly(pow((double)1.1, factor));
411         this->EndDolly();
412     }
413     this->ReleaseFocus();
414     this->InvokeEvent(vtkCommand::MouseWheelBackwardEvent, this);
415 }
416
417
418 //----------------------------------------------------------------------------
419 void vvInteractorStyleNavigator::WindowLevel()
420 {
421     vtkRenderWindowInteractor *rwi = this->Interactor;
422
423     this->WindowLevelCurrentPosition[0] = rwi->GetEventPosition()[0];
424     this->WindowLevelCurrentPosition[1] = rwi->GetEventPosition()[1];
425
426     this->InvokeEvent(vtkCommand::WindowLevelEvent, this);
427 }
428
429 //----------------------------------------------------------------------------
430 void vvInteractorStyleNavigator::Pick()
431 {
432     this->InvokeEvent(vtkCommand::PickEvent, this);
433 }
434
435 //----------------------------------------------------------------------------
436 void vvInteractorStyleNavigator::Pan()
437 {
438     if (this->CurrentRenderer == NULL)
439     {
440         return;
441     }
442
443     vtkRenderWindowInteractor *rwi = this->Interactor;
444
445     double viewFocus[4], focalDepth, viewPoint[3];
446     double newPickPoint[4], oldPickPoint[4], motionVector[3];
447
448     // Calculate the focal depth since we'll be using it a lot
449
450     vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
451     camera->GetFocalPoint(viewFocus);
452     this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2],
453                                 viewFocus);
454     focalDepth = viewFocus[2];
455
456     this->ComputeDisplayToWorld((double)rwi->GetEventPosition()[0],
457                                 (double)rwi->GetEventPosition()[1],
458                                 focalDepth,
459                                 newPickPoint);
460
461     // Has to recalc old mouse point since the viewport has moved,
462     // so can't move it outside the loop
463
464     this->ComputeDisplayToWorld((double)rwi->GetLastEventPosition()[0],
465                                 (double)rwi->GetLastEventPosition()[1],
466                                 focalDepth,
467                                 oldPickPoint);
468
469     // Camera motion is reversed
470
471     motionVector[0] = oldPickPoint[0] - newPickPoint[0];
472     motionVector[1] = oldPickPoint[1] - newPickPoint[1];
473     motionVector[2] = oldPickPoint[2] - newPickPoint[2];
474
475     camera->GetFocalPoint(viewFocus);
476     camera->GetPosition(viewPoint);
477     camera->SetFocalPoint(motionVector[0] + viewFocus[0],
478                           motionVector[1] + viewFocus[1],
479                           motionVector[2] + viewFocus[2]);
480
481     camera->SetPosition(motionVector[0] + viewPoint[0],
482                         motionVector[1] + viewPoint[1],
483                         motionVector[2] + viewPoint[2]);
484
485     if (rwi->GetLightFollowCamera())
486     {
487         this->CurrentRenderer->UpdateLightsGeometryToFollowCamera();
488     }
489
490     rwi->Render();
491 }
492
493 //----------------------------------------------------------------------------
494 void vvInteractorStyleNavigator::Dolly()
495 {
496     if (this->CurrentRenderer == NULL)
497     {
498         return;
499     }
500
501     vtkRenderWindowInteractor *rwi = this->Interactor;
502     double *center = this->CurrentRenderer->GetCenter();
503     int dy = rwi->GetEventPosition()[1] - rwi->GetLastEventPosition()[1];
504     double dyf = this->MotionFactor * (double)(dy) / (double)(center[1]);
505     this->Dolly(pow((double)1.1, dyf));
506 }
507
508 //----------------------------------------------------------------------------
509 void vvInteractorStyleNavigator::Dolly(double factor)
510 {
511     if (this->CurrentRenderer == NULL)
512     {
513         return;
514     }
515
516     double viewFocus[4],viewPoint[4],motionVector[3], focalDepth;
517     double oldPos[3], newPos[3], distance[2];
518     vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
519     camera->GetFocalPoint(viewFocus);
520     this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2],
521                                 viewFocus);
522     focalDepth = viewFocus[2];
523
524     oldPos[0] = this->CurrentRenderer->GetCenter()[0];
525     oldPos[1] = this->CurrentRenderer->GetCenter()[1];
526     oldPos[2] = focalDepth;
527
528     distance[0] = 1/factor*
529                   (this->Interactor->GetEventPosition()[0]-this->CurrentRenderer->GetCenter()[0]);
530     distance[1] = 1/factor*
531                   (this->Interactor->GetEventPosition()[1]-this->CurrentRenderer->GetCenter()[1]);
532
533     newPos[0] = this->Interactor->GetEventPosition()[0] - distance[0];
534     newPos[1] = this->Interactor->GetEventPosition()[1] - distance[1];
535     newPos[2] = focalDepth;
536
537     this->CurrentRenderer->DisplayToNormalizedDisplay(oldPos[0],oldPos[1]);
538     this->CurrentRenderer->NormalizedDisplayToViewport(oldPos[0],oldPos[1]);
539     this->CurrentRenderer->ViewportToNormalizedViewport(oldPos[0],oldPos[1]);
540     this->CurrentRenderer->NormalizedViewportToView(oldPos[0],oldPos[1],oldPos[2]);
541     this->CurrentRenderer->ViewToWorld(oldPos[0],oldPos[1],oldPos[2]);
542
543     this->CurrentRenderer->DisplayToNormalizedDisplay(newPos[0],newPos[1]);
544     this->CurrentRenderer->NormalizedDisplayToViewport(newPos[0],newPos[1]);
545     this->CurrentRenderer->ViewportToNormalizedViewport(newPos[0],newPos[1]);
546     this->CurrentRenderer->NormalizedViewportToView(newPos[0],newPos[1],newPos[2]);
547     this->CurrentRenderer->ViewToWorld(newPos[0],newPos[1],newPos[2]);
548
549     motionVector[0] = newPos[0] - oldPos[0];
550     motionVector[1] = newPos[1] - oldPos[1];
551     motionVector[2] = newPos[2] - oldPos[2];
552
553     camera->GetFocalPoint(viewFocus);
554     camera->GetPosition(viewPoint);
555     camera->SetFocalPoint(motionVector[0] + viewFocus[0],
556                           motionVector[1] + viewFocus[1],
557                           motionVector[2] + viewFocus[2]);
558
559     camera->SetPosition(motionVector[0] + viewPoint[0],
560                         motionVector[1] + viewPoint[1],
561                         motionVector[2] + viewPoint[2]);
562
563     if (camera->GetParallelProjection())
564     {
565         camera->SetParallelScale(camera->GetParallelScale() / factor);
566     }
567     else
568     {
569         camera->Dolly(factor);
570         if (this->AutoAdjustCameraClippingRange)
571         {
572             this->CurrentRenderer->ResetCameraClippingRange();
573         }
574     }
575
576     if (this->Interactor->GetLightFollowCamera())
577     {
578         this->CurrentRenderer->UpdateLightsGeometryToFollowCamera();
579     }
580     this->CurrentRenderer->ResetCameraClippingRange();
581     //this->Interactor->Render();
582 }
583
584
585 //----------------------------------------------------------------------------
586 void vvInteractorStyleNavigator::PrintSelf(ostream& os, vtkIndent indent)
587 {
588     this->Superclass::PrintSelf(os, indent);
589
590     os << indent << "Window Level Current Position: ("
591     << this->WindowLevelCurrentPosition[0] << ", "
592     << this->WindowLevelCurrentPosition[1] << ")" << endl;
593
594     os << indent << "Window Level Start Position: ("
595     << this->WindowLevelStartPosition[0] << ", "
596     << this->WindowLevelStartPosition[1] << ")" << endl;
597 }