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