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