]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/ImageInteractorStyle.cxx
Widget integration (step 5/6): Just one step leftgit shortlog !
[cpPlugins.git] / lib / cpExtensions / Visualization / ImageInteractorStyle.cxx
1 #include <cpExtensions/Visualization/ImageInteractorStyle.h>
2
3 #include <vtkImageActor.h>
4 #include <vtkRenderWindowInteractor.h>
5
6 // -------------------------------------------------------------------------
7 cpExtensions::Visualization::ImageInteractorStyle::
8 Self* cpExtensions::Visualization::ImageInteractorStyle::
9 New( )
10 {
11   return( new Self );
12 }
13
14 // -------------------------------------------------------------------------
15 void cpExtensions::Visualization::ImageInteractorStyle::
16 AssociateView( void* data )
17 {
18   this->Data = data;
19 }
20
21 // -------------------------------------------------------------------------
22 void cpExtensions::Visualization::ImageInteractorStyle::
23 AssociateImageActor( vtkImageActor* actor )
24 {
25   this->PropPicker->AddPickList( actor );
26   this->Modified( );
27 }
28
29 // -------------------------------------------------------------------------
30 void cpExtensions::Visualization::ImageInteractorStyle::
31 AssociateInteractor( vtkRenderWindowInteractor* rwi )
32 {
33   if( rwi != NULL )
34   {
35     this->AssociatedInteractors.push_back( rwi );
36     this->Modified( );
37
38   } // fi
39 }
40
41 // -------------------------------------------------------------------------
42 void cpExtensions::Visualization::ImageInteractorStyle::
43 OnMouseMove( )
44 {
45   this->Superclass::OnMouseMove( );
46
47   // Get current position on the associated actors
48   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
49   if( rwi == NULL || this->MouseMoveCommand == NULL )
50     return;
51
52   // Get modifiers
53   bool alt = ( rwi->GetAltKey( ) == 1 );
54   bool ctr = ( rwi->GetControlKey( ) == 1 );
55   bool sft = ( rwi->GetShiftKey( ) == 1 );
56   ButtonID button = this->GetButtonID( );
57
58   double pos[ 3 ];
59   if( !( this->_PickPosition( pos ) ) )
60     return;
61
62   // Invoke possible events
63   this->MouseMoveCommand( this->Data, button, pos, alt, ctr, sft );
64   rwi->Render( );
65   this->_RenderAssociatedInteractors( );
66 }
67
68 // -------------------------------------------------------------------------
69 void cpExtensions::Visualization::ImageInteractorStyle::
70 OnMouseWheelForward( )
71 {
72   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
73   if( rwi == NULL || this->MouseWheelCommand == NULL )
74     return;
75
76   // Get modifiers
77   bool alt = ( rwi->GetAltKey( ) == 1 );
78   bool ctr = ( rwi->GetControlKey( ) == 1 );
79   bool sft = ( rwi->GetShiftKey( ) == 1 );
80
81   // Invoke possible events
82   this->MouseWheelCommand( this->Data, 1, alt, ctr, sft );
83   rwi->Render( );
84   this->_RenderAssociatedInteractors( );
85 }
86
87 // -------------------------------------------------------------------------
88 void cpExtensions::Visualization::ImageInteractorStyle::
89 OnMouseWheelBackward( )
90 {
91   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
92   if( rwi == NULL || this->MouseWheelCommand == NULL )
93     return;
94
95   // Get modifiers
96   bool alt = ( rwi->GetAltKey( ) == 1 );
97   bool ctr = ( rwi->GetControlKey( ) == 1 );
98   bool sft = ( rwi->GetShiftKey( ) == 1 );
99
100   // Invoke possible events
101   this->MouseWheelCommand( this->Data, -1, alt, ctr, sft );
102   rwi->Render( );
103   this->_RenderAssociatedInteractors( );
104 }
105
106 // -------------------------------------------------------------------------
107 void cpExtensions::Visualization::ImageInteractorStyle::
108 OnLeftClick( )
109 {
110   // Get current position on the associated actors
111   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
112   if( rwi == NULL || this->MouseClickCommand == NULL )
113     return;
114   double pos[ 3 ];
115   if( !( this->_PickPosition( pos ) ) )
116     return;
117
118   // Get modifiers
119   bool alt = ( rwi->GetAltKey( ) == 1 );
120   bool ctr = ( rwi->GetControlKey( ) == 1 );
121   bool sft = ( rwi->GetShiftKey( ) == 1 );
122
123   // Invoke possible events
124   this->MouseClickCommand(
125     this->Data, Self::ButtonID_Left, pos, alt, ctr, sft
126     );
127   rwi->Render( );
128   this->_RenderAssociatedInteractors( );
129 }
130
131 // -------------------------------------------------------------------------
132 void cpExtensions::Visualization::ImageInteractorStyle::
133 OnLeftDoubleClick( )
134 {
135   // Get current position on the associated actors
136   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
137   if( rwi == NULL || this->MouseDoubleClickCommand == NULL )
138     return;
139   double pos[ 3 ];
140   if( !( this->_PickPosition( pos ) ) )
141     return;
142
143   // Get modifiers
144   bool alt = ( rwi->GetAltKey( ) == 1 );
145   bool ctr = ( rwi->GetControlKey( ) == 1 );
146   bool sft = ( rwi->GetShiftKey( ) == 1 );
147
148   // Invoke possible events
149   this->MouseDoubleClickCommand(
150     this->Data, Self::ButtonID_Left, pos, alt, ctr, sft
151     );
152   rwi->Render( );
153   this->_RenderAssociatedInteractors( );
154 }
155
156 // -------------------------------------------------------------------------
157 void cpExtensions::Visualization::ImageInteractorStyle::
158 OnMiddleClick( )
159 {
160   // Get current position on the associated actors
161   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
162   if( rwi == NULL || this->MouseClickCommand == NULL )
163     return;
164   double pos[ 3 ];
165   if( !( this->_PickPosition( pos ) ) )
166     return;
167
168   // Get modifiers
169   bool alt = ( rwi->GetAltKey( ) == 1 );
170   bool ctr = ( rwi->GetControlKey( ) == 1 );
171   bool sft = ( rwi->GetShiftKey( ) == 1 );
172
173   // Invoke possible events
174   this->MouseClickCommand(
175     this->Data, Self::ButtonID_Middle, pos, alt, ctr, sft
176     );
177   rwi->Render( );
178   this->_RenderAssociatedInteractors( );
179 }
180
181 // -------------------------------------------------------------------------
182 void cpExtensions::Visualization::ImageInteractorStyle::
183 OnMiddleDoubleClick( )
184 {
185   // Get current position on the associated actors
186   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
187   if( rwi == NULL || this->MouseDoubleClickCommand == NULL )
188     return;
189   double pos[ 3 ];
190   if( !( this->_PickPosition( pos ) ) )
191     return;
192
193   // Get modifiers
194   bool alt = ( rwi->GetAltKey( ) == 1 );
195   bool ctr = ( rwi->GetControlKey( ) == 1 );
196   bool sft = ( rwi->GetShiftKey( ) == 1 );
197
198   // Invoke possible events
199   this->MouseDoubleClickCommand(
200     this->Data, Self::ButtonID_Middle, pos, alt, ctr, sft
201     );
202   rwi->Render( );
203   this->_RenderAssociatedInteractors( );
204 }
205
206 // -------------------------------------------------------------------------
207 void cpExtensions::Visualization::ImageInteractorStyle::
208 OnRightClick( )
209 {
210   // Get current position on the associated actors
211   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
212   if( rwi == NULL || this->MouseClickCommand == NULL )
213     return;
214   double pos[ 3 ];
215   if( !( this->_PickPosition( pos ) ) )
216     return;
217
218   // Get modifiers
219   bool alt = ( rwi->GetAltKey( ) == 1 );
220   bool ctr = ( rwi->GetControlKey( ) == 1 );
221   bool sft = ( rwi->GetShiftKey( ) == 1 );
222
223   // Invoke possible events
224   this->MouseClickCommand(
225     this->Data, Self::ButtonID_Right, pos, alt, ctr, sft
226     );
227   rwi->Render( );
228   this->_RenderAssociatedInteractors( );
229 }
230
231 // -------------------------------------------------------------------------
232 void cpExtensions::Visualization::ImageInteractorStyle::
233 OnRightDoubleClick( )
234 {
235   // Get current position on the associated actors
236   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
237   if( rwi == NULL || this->MouseDoubleClickCommand == NULL )
238     return;
239   double pos[ 3 ];
240   if( !( this->_PickPosition( pos ) ) )
241     return;
242
243   // Get modifiers
244   bool alt = ( rwi->GetAltKey( ) == 1 );
245   bool ctr = ( rwi->GetControlKey( ) == 1 );
246   bool sft = ( rwi->GetShiftKey( ) == 1 );
247
248   // Invoke possible events
249   this->MouseDoubleClickCommand(
250     this->Data, Self::ButtonID_Right, pos, alt, ctr, sft
251     );
252   rwi->Render( );
253   this->_RenderAssociatedInteractors( );
254 }
255
256 // -------------------------------------------------------------------------
257 void cpExtensions::Visualization::ImageInteractorStyle::
258 OnChar( )
259 {
260   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
261   if( rwi == NULL || this->KeyCommand == NULL )
262     return;
263   this->KeyCommand( this->Data, rwi->GetKeyCode( ) );
264   rwi->Render( );
265   this->_RenderAssociatedInteractors( );
266 }
267
268 // -------------------------------------------------------------------------
269 void cpExtensions::Visualization::ImageInteractorStyle::
270 OnExpose( )
271 {
272   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
273   if( rwi == NULL )
274     return;
275   rwi->Render( );
276   this->_RenderAssociatedInteractors( );
277 }
278
279 // -------------------------------------------------------------------------
280 void cpExtensions::Visualization::ImageInteractorStyle::
281 OnConfigure( )
282 {
283   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
284   if( rwi == NULL )
285     return;
286   rwi->Render( );
287   this->_RenderAssociatedInteractors( );
288 }
289
290 // -------------------------------------------------------------------------
291 void cpExtensions::Visualization::ImageInteractorStyle::
292 OnEnter( )
293 {
294   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
295   if( rwi == NULL )
296     return;
297   rwi->Render( );
298   this->_RenderAssociatedInteractors( );
299 }
300
301 // -------------------------------------------------------------------------
302 void cpExtensions::Visualization::ImageInteractorStyle::
303 OnLeave( )
304 {
305   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
306   if( rwi == NULL )
307     return;
308   rwi->Render( );
309   this->_RenderAssociatedInteractors( );
310 }
311
312 // -------------------------------------------------------------------------
313 cpExtensions::Visualization::ImageInteractorStyle::
314 ImageInteractorStyle( )
315   : Superclass( ),
316     Data( NULL ),
317     MouseMoveCommand( NULL ),
318     MouseClickCommand( NULL ),
319     MouseDoubleClickCommand( NULL ),
320     MouseWheelCommand( NULL ),
321     KeyCommand( NULL )
322 {
323   this->PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
324   this->PropPicker->PickFromListOn( );
325 }
326
327 // -------------------------------------------------------------------------
328 cpExtensions::Visualization::ImageInteractorStyle::
329 ~ImageInteractorStyle( )
330 {
331 }
332
333 // -------------------------------------------------------------------------
334 bool cpExtensions::Visualization::ImageInteractorStyle::
335 _PickPosition( double pos[ 3 ] )
336 {
337   vtkRenderWindowInteractor* rwi = this->GetInteractor( );
338   if( rwi == NULL )
339     return( false );
340
341   // Find the renderer where the event has been raised
342   double x = double( rwi->GetEventPosition( )[ 0 ] );
343   double y = double( rwi->GetEventPosition( )[ 1 ] );
344   this->FindPokedRenderer( x, y );
345
346   // Pick a 3D position
347   int r = this->PropPicker->Pick( x, y, double( 0 ), this->CurrentRenderer );
348   if( r == 0 )
349     return( false );
350   this->PropPicker->GetPickPosition( pos );
351
352   return( true );
353 }
354
355 // -------------------------------------------------------------------------
356 void cpExtensions::Visualization::ImageInteractorStyle::
357 _RenderAssociatedInteractors( )
358 {
359   for( unsigned int i = 0; i < this->AssociatedInteractors.size( ); ++i )
360     this->AssociatedInteractors[ i ]->Render( );
361 }
362
363 /*
364 #include <cmath>
365 #include <ctime>
366
367 #include <vtkAnnotatedCubeActor.h>
368 #include <vtkAxesActor.h>
369 #include <vtkCallbackCommand.h>
370 #include <vtkCamera.h>
371 #include <vtkCellArray.h>
372 #include <vtkCommand.h>
373 #include <vtkMatrix4x4.h>
374 #include <vtkPropAssembly.h>
375 #include <vtkProperty.h>
376 #include <vtkRendererCollection.h>
377 #include <vtkRenderWindow.h>
378 #include <vtkRenderWindowInteractor.h>
379
380 #include <cpExtensions/Visualization/ImageSliceActors.h>
381 #include <cpExtensions/Visualization/MPRActors.h>
382
383 // -------------------------------------------------------------------------
384 const int cpExtensions::Visualization::
385 ImageInteractorStyle::CursorEvent = vtkCommand::UserEvent + 1;
386 const int cpExtensions::Visualization::
387 ImageInteractorStyle::RadiusEvent = vtkCommand::UserEvent + 2;
388 const int cpExtensions::Visualization::
389 ImageInteractorStyle::DoubleClickEvent = vtkCommand::UserEvent + 3;
390
391 // -------------------------------------------------------------------------
392 cpExtensions::Visualization::ImageInteractorStyle::
393 Self* cpExtensions::Visualization::ImageInteractorStyle::
394 New( )
395 {
396   return( new Self( ) );
397 }
398
399 // -------------------------------------------------------------------------
400 void cpExtensions::Visualization::ImageInteractorStyle::
401 Configure( ImageSliceActors* slice_actors, MPRActors* mpr_actors )
402 {
403   this->m_SliceActors = slice_actors;
404   this->m_MPRActors = mpr_actors;
405   this->SetModeToNavigation( );
406   this->PropPicker->AddPickList( slice_actors->GetImageActor( 0 ) );
407   this->Modified( );
408 }
409
410 // -------------------------------------------------------------------------
411 void cpExtensions::Visualization::ImageInteractorStyle::
412 AssociateInteractor( vtkRenderWindowInteractor* interactor )
413 {
414   if( interactor != NULL )
415   {
416     this->AssociatedInteractors.push_back( interactor );
417     this->Modified( );
418
419   } // fi
420 }
421
422 // -------------------------------------------------------------------------
423 void cpExtensions::Visualization::ImageInteractorStyle::
424 SetModeToNavigation( )
425 {
426   this->Mode = Self::NavigationMode;
427 }
428
429 // -------------------------------------------------------------------------
430 void cpExtensions::Visualization::ImageInteractorStyle::
431 SetModeToDeformation( )
432 {
433   this->Mode = Self::DeformationMode;
434 }
435
436 // -------------------------------------------------------------------------
437 void cpExtensions::Visualization::ImageInteractorStyle::
438 SetInteractor( vtkRenderWindowInteractor* interactor, const int& axis )
439 {
440   this->Superclass::SetInteractor( interactor );
441   this->OrientationWidget->SetInteractor( interactor );
442   interactor->SetInteractorStyle( this );
443   if( interactor == NULL )
444     return;
445
446   // Get camera, avoiding segfaults
447   vtkRenderer* ren =
448     interactor->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
449   if( ren == NULL )
450     return;
451   vtkCamera* cam = ren->GetActiveCamera( );
452   if( cam == NULL )
453     return;
454
455   // Parallel projections are better when displaying 2D images
456   cam->ParallelProjectionOn( );
457   cam->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
458   if( axis == 0 )
459   {
460     cam->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
461     cam->SetViewUp  ( double( 0 ), double( 1 ), double( 0 ) );
462   }
463   else if( axis == 1 )
464   {
465     cam->SetPosition( double( 0 ), double( 1 ), double(  0 ) );
466     cam->SetViewUp  ( double( 0 ), double( 0 ), double( -1 ) );
467   }
468   else // if( axis == 2 )
469   {
470     cam->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
471     cam->SetViewUp  ( double( 0 ), double( 1 ), double( 0 ) );
472
473   } // fi
474   ren->ResetCamera( );
475
476   // Enable 2D orientation widget
477   this->OrientationWidget->SetEnabled( 1 );
478   this->OrientationWidget->InteractiveOff( );
479 }
480
481 // -------------------------------------------------------------------------
482 void cpExtensions::Visualization::ImageInteractorStyle::
483 OnMouseMove( )
484 {
485   if( this->m_MPRActors == NULL )
486     return;
487
488   if( this->CursorMoving )
489   {
490     bool picked = this->_PickPosition( this->Cursor );
491     if( picked )
492     {
493       for( int i = 0; i < 3; ++i )
494         if( this->m_SliceActors->GetAxis( ) != i )
495           this->m_MPRActors->SetSlice( i, this->Cursor[ i ] );
496       this->InvokeEvent( Self::CursorEvent, this->Cursor );
497       this->Interactor->Render( );
498       this->_RenderAssociateInteractors( );
499
500     } // fi
501   }
502   else if( this->RadiusMoving )
503   {
504     bool picked = this->_PickPosition( this->Radius );
505     if( picked )
506     {
507       this->InvokeEvent( Self::RadiusEvent, this->Radius );
508       this->_UpdateRadius( );
509
510     } // fi
511   }
512   else
513   {
514     switch( this->State )
515     {
516     case VTKIS_WINDOW_LEVEL:
517       this->WindowLevel( );
518       break;
519     case VTKIS_DOLLY:
520       this->Dolly( );
521       break;
522     case VTKIS_PAN:
523       this->Pan( );
524       break;
525     } // hctiws
526
527   } // fi
528 }
529
530 // -------------------------------------------------------------------------
531 void cpExtensions::Visualization::ImageInteractorStyle::
532 OnLeftButtonDown( )
533 {
534   static double pnt[ 3 ];
535   static int pos[ 2 ];
536   this->Interactor->GetEventPosition( pos );
537   this->FindPokedRenderer( pos[ 0 ], pos[ 1 ] );
538   if( this->CurrentRenderer == NULL )
539     return;
540   this->GrabFocus( this->EventCallbackCommand );
541
542   // TODO: check this code
543   // Manage double-click
544   static const long epsilon_time = 800;
545   static long last_click_time = -( epsilon_time << 1 );
546   long click_time = static_cast< long >( std::clock( ) );
547   if( ( click_time - last_click_time ) < epsilon_time )
548   {
549     last_click_time = -( epsilon_time << 1 );
550     if( this->_PickPosition( pnt ) )
551       this->InvokeEvent( Self::DoubleClickEvent, pnt );
552   }
553   else
554   {
555     last_click_time = click_time;
556     if( this->Interactor->GetControlKey( ) )
557       this->StartCursorMoving( );
558
559   } // fi
560 }
561
562 // -------------------------------------------------------------------------
563 void cpExtensions::Visualization::ImageInteractorStyle::
564 OnLeftButtonUp( )
565 {
566   if( this->CursorMoving )
567   {
568     this->EndCursorMoving( );
569     if( this->Interactor )
570       this->ReleaseFocus( );
571
572   } // fi
573 }
574
575 // -------------------------------------------------------------------------
576 void cpExtensions::Visualization::ImageInteractorStyle::
577 OnMiddleButtonDown( )
578 {
579   int x = this->Interactor->GetEventPosition( )[ 0 ];
580   int y = this->Interactor->GetEventPosition( )[ 1 ];
581
582   this->FindPokedRenderer( x, y );
583   if( this->CurrentRenderer == NULL )
584     return;
585   this->GrabFocus( this->EventCallbackCommand );
586
587   if( this->Interactor->GetAltKey( ) )
588   {
589   }
590   else if( this->Interactor->GetControlKey( ) )
591   {
592     this->StartRadiusMoving( );
593   }
594   else if( this->Interactor->GetShiftKey( ) )
595   {
596   }
597   else
598     this->StartPan( );
599 }
600
601 // -------------------------------------------------------------------------
602 void cpExtensions::Visualization::ImageInteractorStyle::
603 OnMiddleButtonUp( )
604 {
605   if( this->RadiusMoving )
606   {
607     this->EndRadiusMoving( );
608     if( this->Interactor )
609       this->ReleaseFocus( );
610   }
611   else
612   {
613     switch( this->State )
614     {
615     case VTKIS_PAN:
616       this->EndPan( );
617       break;
618     } // hctiws
619
620   } // fi
621 }
622
623 // -------------------------------------------------------------------------
624 void cpExtensions::Visualization::ImageInteractorStyle::
625 OnRightButtonDown( )
626 {
627   int x = this->Interactor->GetEventPosition( )[ 0 ];
628   int y = this->Interactor->GetEventPosition( )[ 1 ];
629
630   this->FindPokedRenderer( x, y );
631   if( this->CurrentRenderer == NULL )
632     return;
633   this->GrabFocus( this->EventCallbackCommand );
634
635   if( this->Interactor->GetControlKey( ) )
636   {
637     this->WindowLevelStartPosition[ 0 ] = x;
638     this->WindowLevelStartPosition[ 1 ] = y;
639     this->StartWindowLevel( );
640   }
641   else
642   {
643     this->StartDolly( );
644
645   } // fi
646 }
647
648 // -------------------------------------------------------------------------
649 void cpExtensions::Visualization::ImageInteractorStyle::
650 OnRightButtonUp( )
651 {
652   switch( this->State )
653   {
654   case VTKIS_WINDOW_LEVEL:
655   {
656     this->EndWindowLevel( );
657     if( this->Interactor )
658       this->ReleaseFocus( );
659   }
660   break;
661   case VTKIS_DOLLY:
662     this->EndDolly( );
663     break;
664   } // hctiws
665 }
666
667 // -------------------------------------------------------------------------
668 void cpExtensions::Visualization::ImageInteractorStyle::
669 OnMouseWheelForward( )
670 {
671   if( this->m_SliceActors == NULL || this->Interactor == NULL )
672     return;
673   int off = 1;
674   if( this->Interactor->GetShiftKey( ) == 1 )
675     off *= 10;
676   int s = this->m_SliceActors->GetSliceNumber( ) + off;
677   int maxs = this->m_SliceActors->GetSliceNumberMaxValue( );
678   this->m_SliceActors->SetSliceNumber( ( s < maxs )? s: maxs );
679   this->m_MPRActors->SetSlice(
680     this->m_SliceActors->GetAxis( ),
681     this->m_SliceActors->GetSliceNumber( )
682     );
683   this->Interactor->Render( );
684   this->_RenderAssociateInteractors( );
685 }
686
687 // -------------------------------------------------------------------------
688 void cpExtensions::Visualization::ImageInteractorStyle::
689 OnMouseWheelBackward( )
690 {
691   if( this->m_SliceActors == NULL || this->Interactor == NULL )
692     return;
693   int off = 1;
694   if( this->Interactor->GetShiftKey( ) == 1 )
695     off *= 10;
696   int s = this->m_SliceActors->GetSliceNumber( ) - off;
697   int mins = this->m_SliceActors->GetSliceNumberMinValue( );
698   this->m_SliceActors->SetSliceNumber( ( mins < s )? s: mins );
699   this->m_MPRActors->SetSlice(
700     this->m_SliceActors->GetAxis( ),
701     this->m_SliceActors->GetSliceNumber( )
702     );
703   this->Interactor->Render( );
704   this->_RenderAssociateInteractors( );
705 }
706
707 // -------------------------------------------------------------------------
708 void cpExtensions::Visualization::ImageInteractorStyle::
709 OnChar( )
710 {
711   switch( this->Interactor->GetKeyCode( ) )
712   {
713   case 'r': case 'R':
714   {
715     vtkRenderer* ren =
716       this->Interactor->GetRenderWindow( )->
717       GetRenderers( )->GetFirstRenderer( );
718     if( ren != NULL )
719       ren->ResetCamera( );
720     this->Interactor->Render( );
721   }
722   break;
723   case 'w': case 'W': case 'l': case 'L':
724   {
725     if( this->m_MPRActors != NULL )
726     {
727       this->m_MPRActors->ResetWindowLevel( 0 );
728       this->Interactor->Render( );
729       this->_RenderAssociateInteractors( );
730
731     } // fi
732   }
733   break;
734   } // hctiws
735 }
736
737 // -------------------------------------------------------------------------
738 void cpExtensions::Visualization::ImageInteractorStyle::
739 WindowLevel( )
740 {
741   if( this->Mode == Self::NavigationMode )
742   {
743     if( this->Interactor == NULL )
744       return;
745     vtkRenderer* ren =
746       this->Interactor->GetRenderWindow( )->
747       GetRenderers( )->GetFirstRenderer( );
748     if( ren == NULL )
749       return;
750
751     // Compute scales
752     this->WindowLevelCurrentPosition[ 0 ] =
753       this->Interactor->GetEventPosition( )[ 0 ];
754     this->WindowLevelCurrentPosition[ 1 ] =
755       this->Interactor->GetEventPosition( )[ 1 ];
756     int* size = ren->GetSize( );
757     double sw = double(
758       this->WindowLevelCurrentPosition[ 0 ] -
759       this->WindowLevelStartPosition[ 0 ]
760       ) / double( size[ 0 ] );
761     double sl = (
762       this->WindowLevelStartPosition[ 1 ] -
763       this->WindowLevelCurrentPosition[ 1 ]
764       ) / double( size[ 1 ] );
765
766     double w = this->WindowLevelInitial[ 0 ] + ( sw * 1000.0 );
767     double l = this->WindowLevelInitial[ 1 ] + ( sl * 1000.0 );
768     double minw = this->m_MPRActors->GetMinWindow( 0 );
769     double maxw = this->m_MPRActors->GetMaxWindow( 0 );
770     double minl = this->m_MPRActors->GetMinLevel( 0 );
771     double maxl = this->m_MPRActors->GetMaxLevel( 0 );
772
773     if( w < minw ) w = minw;
774     if( maxw < w ) w = maxw;
775     if( l < minl ) l = minl;
776     if( maxl < l ) l = maxl;
777
778     this->m_MPRActors->SetWindowLevel( 0, w, l );
779     this->Interactor->Render( );
780     this->_RenderAssociateInteractors( );
781   }
782   else if( this->Mode == Self::DeformationMode )
783   {
784     // TODO
785
786   } // fi
787 }
788
789 // -------------------------------------------------------------------------
790 void cpExtensions::Visualization::ImageInteractorStyle::
791 StartWindowLevel( )
792 {
793   if( this->State != VTKIS_NONE )
794     return;
795   if( this->Mode == Self::NavigationMode )
796   {
797     this->StartState( VTKIS_WINDOW_LEVEL );
798
799     this->WindowLevelInitial[ 0 ] = this->m_MPRActors->GetWindow( 0 );
800     this->WindowLevelInitial[ 1 ] = this->m_MPRActors->GetLevel( 0 );
801   }
802   else if( this->Mode == Self::DeformationMode )
803   {
804     // TODO
805
806   } // fi
807 }
808
809 // -------------------------------------------------------------------------
810 void cpExtensions::Visualization::ImageInteractorStyle::
811 EndWindowLevel( )
812 {
813   if( this->Mode == Self::NavigationMode )
814   {
815     if( this->State != VTKIS_WINDOW_LEVEL )
816       return;
817     this->StopState( );
818   }
819   else
820   {
821     // TODO
822
823   } // fi
824 }
825
826 // -------------------------------------------------------------------------
827 void cpExtensions::Visualization::ImageInteractorStyle::
828 StartCursorMoving( )
829 {
830   if( this->CursorMoving )
831     return;
832   this->_PickPosition( this->Cursor );
833   this->CursorMoving = true;
834 }
835
836 // -------------------------------------------------------------------------
837 void cpExtensions::Visualization::ImageInteractorStyle::
838 EndCursorMoving( )
839 {
840   if( !( this->CursorMoving ) )
841     return;
842   this->CursorMoving = false;
843 }
844
845 // -------------------------------------------------------------------------
846 void cpExtensions::Visualization::ImageInteractorStyle::
847 StartRadiusMoving( )
848 {
849   if( this->RadiusMoving )
850     return;
851   this->_PickPosition( this->Radius );
852   this->RadiusMoving = true;
853   this->_UpdateRadius( );
854 }
855
856 // -------------------------------------------------------------------------
857 void cpExtensions::Visualization::ImageInteractorStyle::
858 EndRadiusMoving( )
859 {
860   if( !( this->RadiusMoving ) )
861     return;
862   this->RadiusMoving = false;
863   this->_UpdateRadius( );
864   this->InvokeEvent( Self::RadiusEvent, NULL );
865 }
866
867 // -------------------------------------------------------------------------
868 cpExtensions::Visualization::ImageInteractorStyle::
869 ImageInteractorStyle( )
870   : Superclass( ),
871     Mode( Self::NavigationMode ),
872     m_SliceActors( NULL ),
873     m_MPRActors( NULL ),
874     CursorMoving( false ),
875     RadiusMoving( false )
876 {
877   // Orientation marks
878   vtkSmartPointer< vtkAnnotatedCubeActor > cube =
879     vtkSmartPointer< vtkAnnotatedCubeActor >::New( );
880   cube->GetCubeProperty( )->SetColor( 0.9, 0.7, 0.2 );
881   cube->GetTextEdgesProperty( )->SetLineWidth( 1 );
882   cube->GetTextEdgesProperty( )->SetDiffuse( 0 );
883   cube->GetTextEdgesProperty( )->SetAmbient( 1 );
884   cube->GetTextEdgesProperty( )->SetColor( 0.18, 0.28, 0.23 );
885   cube->GetXPlusFaceProperty( )->SetColor( 1, 0, 0 );
886   cube->GetXPlusFaceProperty( )->SetInterpolationToFlat( );
887   cube->GetXMinusFaceProperty( )->SetColor( 1, 0, 0 );
888   cube->GetXMinusFaceProperty( )->SetInterpolationToFlat( );
889   cube->GetYPlusFaceProperty( )->SetColor( 0, 1, 0 );
890   cube->GetYPlusFaceProperty( )->SetInterpolationToFlat( );
891   cube->GetYMinusFaceProperty( )->SetColor( 0, 1, 0 );
892   cube->GetYMinusFaceProperty( )->SetInterpolationToFlat( );
893   cube->GetZPlusFaceProperty( )->SetColor( 0, 0, 1 );
894   cube->GetZPlusFaceProperty( )->SetInterpolationToFlat( );
895   cube->GetZMinusFaceProperty( )->SetColor( 0, 0, 1 );
896   cube->GetZMinusFaceProperty( )->SetInterpolationToFlat( );
897
898   vtkSmartPointer< vtkAxesActor > axes =
899     vtkSmartPointer< vtkAxesActor >::New( );
900   axes->AxisLabelsOff( );
901   axes->SetShaftTypeToCylinder( );
902   axes->SetTotalLength( 2, 2, 2 );
903
904   vtkSmartPointer< vtkPropAssembly > actors =
905     vtkSmartPointer< vtkPropAssembly >::New( );
906   actors->AddPart( cube );
907   actors->AddPart( axes );
908
909   this->OrientationWidget =
910     vtkSmartPointer< vtkOrientationMarkerWidget >::New( );
911   this->OrientationWidget->SetOutlineColor( 0.93, 0.57, 0.13 );
912   this->OrientationWidget->SetOrientationMarker( actors );
913   this->OrientationWidget->SetViewport( 0.0, 0.0, 0.2, 0.2 );
914
915   // Circle
916   unsigned long circle_samples = 1000;
917   this->Circle = vtkSmartPointer< vtkPolyData >::New( );
918
919   vtkSmartPointer< vtkPoints > circle_points =
920     vtkSmartPointer< vtkPoints >::New( );
921   vtkSmartPointer< vtkCellArray > circle_lines =
922     vtkSmartPointer< vtkCellArray >::New( );
923   for( unsigned long s = 0; s < circle_samples; ++s )
924   {
925     double t = double( 6.2832 ) * double( s ) / double( circle_samples );
926     circle_points->InsertNextPoint(
927       std::cos( t ), std::sin( t ), double( 0 )
928       );
929
930     circle_lines->InsertNextCell( 2 );
931     circle_lines->InsertCellPoint( s );
932     circle_lines->InsertCellPoint( ( s + 1 ) % circle_samples );
933
934   } // rof
935   this->Circle->SetPoints( circle_points );
936   this->Circle->SetLines( circle_lines );
937
938   this->CircleMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
939   this->CircleMapper->SetInputData( this->Circle );
940   this->CircleActor = vtkSmartPointer< vtkActor >::New( );
941   this->CircleActor->SetMapper( this->CircleMapper );
942   this->CircleActor->GetProperty( )->SetColor( 1, 0, 1 );
943   this->CircleActor->GetProperty( )->SetLineWidth( 2 );
944
945   this->PropPicker = vtkSmartPointer< vtkPropPicker >::New( );
946   this->PropPicker->PickFromListOn( );
947 }
948
949 // -------------------------------------------------------------------------
950 cpExtensions::Visualization::ImageInteractorStyle::
951 ~ImageInteractorStyle( )
952 {
953 }
954
955 // -------------------------------------------------------------------------
956 void cpExtensions::Visualization::ImageInteractorStyle::
957 _RenderAssociateInteractors( )
958 {
959   std::vector< vtkRenderWindowInteractor* >::iterator rIt =
960     this->AssociatedInteractors.begin( );
961   for( ; rIt != this->AssociatedInteractors.end( ); ++rIt )
962     ( *rIt )->Render( );
963 }
964
965 // -------------------------------------------------------------------------
966 bool cpExtensions::Visualization::ImageInteractorStyle::
967 _PickPosition( double pos[ 3 ] )
968 {
969   if( this->m_SliceActors == NULL )
970     return( false );
971
972   double x = double( this->Interactor->GetEventPosition( )[ 0 ] );
973   double y = double( this->Interactor->GetEventPosition( )[ 1 ] );
974   this->FindPokedRenderer( x, y );
975   int success =
976     this->PropPicker->Pick( x, y, double( 0 ), this->CurrentRenderer );
977   if( success == 0 )
978     return( false );
979   this->PropPicker->GetPickPosition( pos );
980
981   int axis = this->m_SliceActors->GetAxis( );
982   double* bounds = this->m_SliceActors->GetDisplayBounds( );
983   pos[ axis ] = bounds[ axis << 1 ];
984
985   return( true );
986 }
987
988 // -------------------------------------------------------------------------
989 void cpExtensions::Visualization::ImageInteractorStyle::
990 _UpdateCursor( )
991 {
992   std::cout << "upcur" << std::endl;
993 }
994
995 // -------------------------------------------------------------------------
996 void cpExtensions::Visualization::ImageInteractorStyle::
997 _UpdateRadius( )
998 {
999   vtkRenderer* ren =
1000     this->Interactor->GetRenderWindow( )->
1001     GetRenderers( )->GetFirstRenderer( );
1002   if( ren == NULL )
1003     return;
1004   vtkCamera* cam = ren->GetActiveCamera( );
1005   if( cam == NULL )
1006     return;
1007
1008   if( this->RadiusMoving )
1009   {
1010     double x = this->Cursor[ 0 ] - this->Radius[ 0 ];
1011     double y = this->Cursor[ 1 ] - this->Radius[ 1 ];
1012     double z = this->Cursor[ 2 ] - this->Radius[ 2 ];
1013     double r = std::sqrt( ( x * x ) + ( y * y ) + ( z * z ) );
1014
1015     vtkMatrix4x4* cam_matrix = cam->GetModelViewTransformMatrix( );
1016     vtkSmartPointer< vtkMatrix4x4 > circle_matrix =
1017       this->CircleActor->GetUserMatrix( );
1018     if( circle_matrix.GetPointer( ) == NULL )
1019     {
1020       circle_matrix = vtkSmartPointer< vtkMatrix4x4 >::New( );
1021       this->CircleActor->SetUserMatrix( circle_matrix );
1022
1023     } // fi
1024     for( int i = 0; i < 4; ++i )
1025     {
1026       for( int j = 0; j < 4; ++j )
1027       {
1028         double v = cam_matrix->GetElement( i, j );
1029         if( i < 3 && j == 3 )
1030           v = this->Cursor[ i ];
1031         if( i < 3 && j < 3 )
1032           v *= r;
1033         circle_matrix->SetElement( i, j, v );
1034
1035       } // rof
1036
1037     } // rof
1038     this->CircleActor->Modified( );
1039     ren->AddActor( this->CircleActor );
1040   }
1041   else
1042     ren->RemoveActor( this->CircleActor );
1043
1044   this->Interactor->Render( );
1045 }
1046 */
1047
1048 // eof - $RCSfile$