]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Visualization/ImageSliceActors.cxx
Widgets updated
[cpPlugins.git] / lib / cpExtensions / Visualization / ImageSliceActors.cxx
1 #include <cpExtensions/Visualization/ImageSliceActors.h>
2
3 #include <cmath>
4 #include <sstream>
5
6 #include <vtkAlgorithmOutput.h>
7 #include <vtkCamera.h>
8 #include <vtkCellArray.h>
9 #include <vtkImageData.h>
10 #include <vtkInformation.h>
11 #include <vtkPlane.h>
12 #include <vtkPoints.h>
13 #include <vtkProperty.h>
14 #include <vtkRenderer.h>
15 #include <vtkRendererCollection.h>
16 #include <vtkRenderWindow.h>
17 #include <vtkRenderWindowInteractor.h>
18 #include <vtkStreamingDemandDrivenPipeline.h>
19 #include <vtkTextProperty.h>
20 #include <vtkWindowLevelLookupTable.h>
21
22 // -------------------------------------------------------------------------
23 cpExtensions::Visualization::ImageSliceActors*
24 cpExtensions::Visualization::ImageSliceActors::
25 New( )
26 {
27   return( new Self( ) );
28 }
29
30 // -------------------------------------------------------------------------
31 void cpExtensions::Visualization::ImageSliceActors::
32 AddSlicesCommand( TSlicesCommand command, void* data )
33 {
34   if( command != NULL )
35   {
36     this->m_SlicesCommands[ command ] = data;
37     this->Modified( );
38
39   } // fi
40 }
41
42 // -------------------------------------------------------------------------
43 void cpExtensions::Visualization::ImageSliceActors::
44 AddWindowLevelCommand( TWindowLevelCommand command, void* data )
45 {
46   if( command != NULL )
47   {
48     this->m_WindowLevelCommands[ command ] = data;
49     this->Modified( );
50
51   } // fi
52 }
53
54 // -------------------------------------------------------------------------
55 void cpExtensions::Visualization::ImageSliceActors::
56 AddRenderCommand( TVoidCommand command, void* data )
57 {
58   if( command != NULL )
59   {
60     this->m_RenderCommands[ command ] = data;
61     this->Modified( );
62
63   } // fi
64 }
65
66 // -------------------------------------------------------------------------
67 void cpExtensions::Visualization::ImageSliceActors::
68 RemoveSlicesCommand( TSlicesCommand command )
69 {
70   auto i = this->m_SlicesCommands.find( command );
71   if( i != this->m_SlicesCommands.end( ) )
72   {
73     this->m_SlicesCommands.erase( i );
74     this->Modified( );
75
76   } // fi
77 }
78
79 // -------------------------------------------------------------------------
80 void cpExtensions::Visualization::ImageSliceActors::
81 RemoveWindowLevelCommand( TWindowLevelCommand command )
82 {
83   auto i = this->m_WindowLevelCommands.find( command );
84   if( i != this->m_WindowLevelCommands.end( ) )
85   {
86     this->m_WindowLevelCommands.erase( i );
87     this->Modified( );
88
89   } // fi
90 }
91
92 // -------------------------------------------------------------------------
93 void cpExtensions::Visualization::ImageSliceActors::
94 RemoveRenderCommand( TVoidCommand command )
95 {
96   auto i = this->m_RenderCommands.find( command );
97   if( i != this->m_RenderCommands.end( ) )
98   {
99     this->m_RenderCommands.erase( i );
100     this->Modified( );
101
102   } // fi
103 }
104
105 // -------------------------------------------------------------------------
106 void cpExtensions::Visualization::ImageSliceActors::
107 AddInputConnection( vtkAlgorithmOutput* aout, int axis )
108 {
109   // Get input vtkImageData
110   if( aout == NULL )
111     return;
112   vtkAlgorithm* producer = aout->GetProducer( );
113   vtkImageData* data = dynamic_cast< vtkImageData* >(
114     producer->GetOutputDataObject( aout->GetIndex( ) )
115     );
116   if( data == NULL )
117     return;
118
119   // Try to infere if input comes from a color mapping filter
120   vtkImageMapToColors* new_map =
121     dynamic_cast< vtkImageMapToColors* >( producer );
122   if( new_map == NULL )
123   {
124     // Configure LUT, if possible (NULL is returned if not)
125     this->_ConfigureNewLUT( data );
126     new_map = *( this->m_ImageMaps.rbegin( ) );
127     if( new_map != NULL )
128     {
129       new_map->SetInputConnection( aout );
130       new_map->Update( );
131
132     } // fi
133   }
134   else
135     this->m_ImageMaps.push_back( new_map );
136   
137   // Create mapper and actors
138   vtkSmartPointer< vtkImageSliceMapper > mapper =
139     vtkSmartPointer< vtkImageSliceMapper >::New( );
140   if( new_map != NULL )
141     mapper->SetInputConnection( new_map->GetOutputPort( ) );
142   else
143     mapper->SetInputConnection( aout );
144   this->m_SliceMappers.push_back( mapper );
145   this->_ConfigureNewInput( axis );
146 }
147
148 // -------------------------------------------------------------------------
149 void cpExtensions::Visualization::ImageSliceActors::
150 AddInputData( vtkImageData* data, int axis )
151 {
152   // Configure LUT, if possible (NULL is returned if not)
153   this->_ConfigureNewLUT( data );
154   vtkImageMapToColors* new_map = *( this->m_ImageMaps.rbegin( ) );
155   if( new_map != NULL )
156   {
157     new_map->SetInputData( data );
158     new_map->Update( );
159
160   } // fi
161
162   // Create mapper and actors
163   vtkSmartPointer< vtkImageSliceMapper > mapper =
164     vtkSmartPointer< vtkImageSliceMapper >::New( );
165   if( new_map != NULL )
166     mapper->SetInputConnection( new_map->GetOutputPort( ) );
167   else
168     mapper->SetInputData( data );
169   this->m_SliceMappers.push_back( mapper );
170   this->_ConfigureNewInput( axis );
171 }
172
173 // -------------------------------------------------------------------------
174 void cpExtensions::Visualization::ImageSliceActors::
175 Clear( )
176 {
177   // Unbind from container
178   this->RemoveAllItems( );
179
180   // Delete all images
181   this->m_ImageMaps.clear( );
182   this->m_SliceMappers.clear( );
183   this->m_ImageActors.clear( );
184
185   // Reconfigure unique objects
186   this->m_Cursor        = vtkSmartPointer< vtkPolyData >::New( );
187   this->m_CursorMapper  = vtkSmartPointer< vtkPolyDataMapper >::New( );
188   this->m_CursorActor   = vtkSmartPointer< vtkActor >::New( );
189   this->m_PlaneFunction = vtkSmartPointer< vtkPlane >::New( );
190   this->m_Plane         = vtkSmartPointer< vtkPolyData >::New( );
191   this->m_PlaneMapper   = vtkSmartPointer< vtkPolyDataMapper >::New( );
192   this->m_TextActor     = vtkSmartPointer< vtkTextActor >::New( );
193   this->m_PlaneActor    = vtkSmartPointer< vtkActor >::New( );
194   this->m_TextBuffer[ 0 ] = '\0';
195
196   // Unique objects configuration
197   vtkSmartPointer< vtkPoints > cursor_points =
198     vtkSmartPointer< vtkPoints >::New( );
199   vtkSmartPointer< vtkCellArray > cursor_lines =
200     vtkSmartPointer< vtkCellArray >::New( );
201   cursor_points->InsertNextPoint( 0, 0, 0 );
202   cursor_points->InsertNextPoint( 0, 0, 0 );
203   cursor_points->InsertNextPoint( 0, 0, 0 );
204   cursor_points->InsertNextPoint( 0, 0, 0 );
205   cursor_points->InsertNextPoint( 0, 0, 0 );
206   cursor_points->InsertNextPoint( 0, 0, 0 );
207   cursor_points->InsertNextPoint( 0, 0, 0 );
208   cursor_points->InsertNextPoint( 0, 0, 0 );
209   cursor_lines->InsertNextCell( 2 );
210   cursor_lines->InsertCellPoint( 0 );
211   cursor_lines->InsertCellPoint( 1 );
212   cursor_lines->InsertNextCell( 2 );
213   cursor_lines->InsertCellPoint( 2 );
214   cursor_lines->InsertCellPoint( 3 );
215   cursor_lines->InsertNextCell( 2 );
216   cursor_lines->InsertCellPoint( 4 );
217   cursor_lines->InsertCellPoint( 5 );
218   cursor_lines->InsertNextCell( 2 );
219   cursor_lines->InsertCellPoint( 6 );
220   cursor_lines->InsertCellPoint( 7 );
221   this->m_Cursor->SetPoints( cursor_points );
222   this->m_Cursor->SetLines( cursor_lines );
223   this->m_CursorMapper->SetInputData( this->m_Cursor );
224   this->m_CursorActor->SetMapper( this->m_CursorMapper );
225
226   vtkSmartPointer< vtkPoints > plane_points =
227     vtkSmartPointer< vtkPoints >::New( );
228   vtkSmartPointer< vtkCellArray > plane_lines =
229     vtkSmartPointer< vtkCellArray >::New( );
230
231   plane_points->InsertNextPoint( 0, 0, 0 );
232   plane_points->InsertNextPoint( 0, 1, 0 );
233   plane_points->InsertNextPoint( 1, 1, 0 );
234   plane_points->InsertNextPoint( 1, 0, 0 );
235   plane_lines->InsertNextCell( 5 );
236   plane_lines->InsertCellPoint( 0 );
237   plane_lines->InsertCellPoint( 1 );
238   plane_lines->InsertCellPoint( 2 );
239   plane_lines->InsertCellPoint( 3 );
240   plane_lines->InsertCellPoint( 0 );
241   this->m_Plane->SetPoints( plane_points );
242   this->m_Plane->SetLines( plane_lines );
243
244   this->m_PlaneMapper->SetInputData( this->m_Plane );
245   this->m_PlaneActor->SetMapper( this->m_PlaneMapper );
246
247   this->m_TextActor->SetTextScaleModeToNone( );
248   vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
249   textprop->SetColor( 1, 1, 1 );
250   textprop->SetFontFamilyToCourier( );
251   textprop->SetFontSize( 18 );
252   textprop->BoldOff( );
253   textprop->ItalicOff( );
254   textprop->ShadowOff( );
255   textprop->SetJustificationToLeft( );
256   textprop->SetVerticalJustificationToBottom( );
257   vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
258   coord->SetCoordinateSystemToNormalizedViewport( );
259   coord->SetValue( 0.01, 0.01 );
260
261   // Update actor collection
262   this->AddItem( this->m_CursorActor );
263   this->AddItem( this->m_TextActor );
264   this->AddItem( this->m_PlaneActor );
265 }
266
267 // -------------------------------------------------------------------------
268 void cpExtensions::Visualization::ImageSliceActors::
269 AssociateSlice( Self* slice )
270 {
271   this->m_AssociatedSlices.push_back( slice );
272   this->Modified( );
273 }
274
275 // -------------------------------------------------------------------------
276 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
277 GetStyle( )
278 {
279   return( this->m_Style.GetPointer( ) );
280 }
281
282 // -------------------------------------------------------------------------
283 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
284 GetStyle( ) const
285 {
286   return( this->m_Style.GetPointer( ) );
287 }
288
289 // -------------------------------------------------------------------------
290 vtkImageData* cpExtensions::Visualization::ImageSliceActors::
291 GetInputImage( unsigned int id )
292 {
293   vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
294   vtkImageData* image = dynamic_cast< vtkImageData* >(
295     aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
296     );
297   return( image );
298 }
299
300 // -------------------------------------------------------------------------
301 const vtkImageData* cpExtensions::Visualization::ImageSliceActors::
302 GetInputImage( unsigned int id ) const
303 {
304   vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
305   const vtkImageData* image = dynamic_cast< const vtkImageData* >(
306     aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
307     );
308   return( image );
309 }
310
311 // -------------------------------------------------------------------------
312 void cpExtensions::Visualization::ImageSliceActors::
313 PushActorsInto( vtkRenderWindow* window, bool force_style )
314 {
315   this->m_Window = window;
316   if( window == NULL )
317     return;
318   vtkRenderWindowInteractor* rwi = window->GetInteractor( );
319   vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
320   if( rwi == NULL || renderer == NULL )
321     return;
322
323   // Update style
324   if( this->m_Style.GetPointer( ) != NULL && force_style )
325     rwi->SetInteractorStyle( this->m_Style );
326
327   // Update actors
328   vtkProp* prop;
329   this->InitTraversal( );
330   while( prop = this->GetNextProp( ) )
331     renderer->AddViewProp( prop );
332   renderer->Modified( );
333   if( !force_style )
334   {
335     renderer->RemoveViewProp( this->m_CursorActor );
336     renderer->RemoveViewProp( this->m_TextActor );
337
338   } // fi
339
340   // Configure camera
341   vtkCamera* camera = renderer->GetActiveCamera( );
342   if( camera != NULL && force_style )
343   {
344     // Parallel projections are better when displaying 2D images
345     int axis = this->GetAxis( );
346     camera->ParallelProjectionOn( );
347     camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
348     if( axis == 0 )
349     {
350       camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
351       camera->SetViewUp  ( double( 0 ), double( 1 ), double( 0 ) );
352     }
353     else if( axis == 1 )
354     {
355       camera->SetPosition( double( 0 ), double( 1 ), double(  0 ) );
356       camera->SetViewUp  ( double( 0 ), double( 0 ), double( -1 ) );
357     }
358     else // if( axis == 2 )
359     {
360       camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
361       camera->SetViewUp  ( double( 0 ), double( 1 ), double( 0 ) );
362
363     } // fi
364
365   } // fi
366   renderer->ResetCamera( );
367   rwi->Render( );
368 }
369
370 // -------------------------------------------------------------------------
371 void cpExtensions::Visualization::ImageSliceActors::
372 PopActorsFrom( vtkRenderWindow* window )
373 {
374   vtkRenderWindowInteractor* rwi = window->GetInteractor( );
375   vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
376
377   if( renderer != NULL )
378   {
379     // Update actors
380     vtkProp* prop;
381     this->InitTraversal( );
382     while( prop = this->GetNextProp( ) )
383       renderer->RemoveViewProp( prop );
384     renderer->Modified( );
385
386   } // fi
387   if( rwi != NULL )
388     rwi->Render( );
389 }
390
391 // -------------------------------------------------------------------------
392 unsigned int cpExtensions::Visualization::ImageSliceActors::
393 GetNumberOfImageActors( ) const
394 {
395   return( this->m_ImageActors.size( ) );
396 }
397
398 // -------------------------------------------------------------------------
399 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
400 GetImageActor( unsigned int id )
401 {
402   if( id < this->m_ImageActors.size( ) )
403     return( this->m_ImageActors[ id ] );
404   else
405     return( NULL );
406 }
407
408 // -------------------------------------------------------------------------
409 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
410 GetImageActor( unsigned int id ) const
411 {
412   if( id < this->m_ImageActors.size( ) )
413     return( this->m_ImageActors[ id ] );
414   else
415     return( NULL );
416 }
417
418 // -------------------------------------------------------------------------
419 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
420 GetTextActor( )
421 {
422   return( this->m_TextActor );
423 }
424
425 // -------------------------------------------------------------------------
426 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
427 GetTextActor( ) const
428 {
429   return( this->m_TextActor );
430 }
431
432 // -------------------------------------------------------------------------
433 vtkActor* cpExtensions::Visualization::ImageSliceActors::
434 GetPlaneActor( )
435 {
436   return( this->m_PlaneActor );
437 }
438
439 // -------------------------------------------------------------------------
440 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
441 GetPlaneActor( ) const
442 {
443   return( this->m_PlaneActor );
444 }
445
446 // -------------------------------------------------------------------------
447 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
448 GetPlaneFunction( )
449 {
450   return( this->m_PlaneFunction );
451 }
452
453 // -------------------------------------------------------------------------
454 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
455 GetPlaneFunction( ) const
456 {
457   return( this->m_PlaneFunction );
458 }
459
460 // -------------------------------------------------------------------------
461 void cpExtensions::Visualization::ImageSliceActors::
462 SetInterpolate( bool v )
463 {
464   if( this->m_Interpolate != v )
465   {
466     for( unsigned int i = 0; i < this->m_ImageActors.size( ); ++i )
467       this->m_ImageActors[ i ]->SetInterpolate( v );
468     this->m_Interpolate = v;
469     this->Modified( );
470
471   } // fi
472 }
473
474 // -------------------------------------------------------------------------
475 void cpExtensions::Visualization::ImageSliceActors::
476 InterpolateOn( )
477 {
478   this->SetInterpolate( true );
479 }
480
481 // -------------------------------------------------------------------------
482 void cpExtensions::Visualization::ImageSliceActors::
483 InterpolateOff( )
484 {
485   this->SetInterpolate( false );
486 }
487
488 // -------------------------------------------------------------------------
489 double* cpExtensions::Visualization::ImageSliceActors::
490 GetDisplayBounds( ) const
491 {
492   if( this->m_ImageActors.size( ) > 0 )
493     return( this->m_ImageActors[ 0 ]->GetDisplayBounds( ) );
494   else
495     return( NULL );
496 }
497
498 // -------------------------------------------------------------------------
499 void cpExtensions::Visualization::ImageSliceActors::
500 GetDisplayBounds( double bounds[ 6 ] ) const
501 {
502   if( this->m_ImageActors.size( ) == 0 )
503   {
504     bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
505     bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
506   }
507   else
508     this->m_ImageActors[ 0 ]->GetDisplayBounds( bounds );
509 }
510
511 // -------------------------------------------------------------------------
512 void cpExtensions::Visualization::ImageSliceActors::
513 ResetCursor( )
514 {
515   vtkPoints* points = this->m_Cursor->GetPoints( );
516   points->SetPoint( 0, 0, 0, 0 );
517   points->SetPoint( 1, 0, 0, 0 );
518   points->SetPoint( 2, 0, 0, 0 );
519   points->SetPoint( 3, 0, 0, 0 );
520   points->SetPoint( 4, 0, 0, 0 );
521   points->SetPoint( 5, 0, 0, 0 );
522   points->SetPoint( 6, 0, 0, 0 );
523   points->SetPoint( 7, 0, 0, 0 );
524   this->m_Cursor->Modified( );
525   this->m_CursorMapper->Modified( );
526   this->m_CursorActor->Modified( );
527 }
528
529 // -------------------------------------------------------------------------
530 void cpExtensions::Visualization::ImageSliceActors::
531 SetCursor( double pos[ 3 ] )
532 {
533   if( this->m_SliceMappers.size( ) == 0 )
534     return;
535
536   // Get ordered axes
537   int a0 = this->GetAxis( );
538   int a1 = ( a0 + 1 ) % 3;
539   int a2 = ( a0 + 2 ) % 3;
540   int ma0 = a0 << 1;
541   int ma1 = a1 << 1;
542   int ma2 = a2 << 1;
543
544   double bounds[ 6 ];
545   this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
546
547   double
548     p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
549     p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
550
551   p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
552   p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
553   p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
554   p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
555   p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
556   p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
557   p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
558   p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
559
560   vtkPoints* points = this->m_Cursor->GetPoints( );
561   points->SetPoint( 0, p0 );
562   points->SetPoint( 1, p1 );
563   points->SetPoint( 2, p2 );
564   points->SetPoint( 3, p3 );
565   points->SetPoint( 4, p4 );
566   points->SetPoint( 5, p5 );
567   points->SetPoint( 6, p6 );
568   points->SetPoint( 7, p7 );
569   this->m_Cursor->Modified( );
570   this->m_CursorMapper->Modified( );
571   this->m_CursorActor->Modified( );
572 }
573
574 // -------------------------------------------------------------------------
575 vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
576 GetImageMap( unsigned int id )
577 {
578   if( id < this->m_ImageMaps.size( ) )
579     return( this->m_ImageMaps[ id ].GetPointer( ) );
580   else
581     return( NULL );
582 }
583
584 // -------------------------------------------------------------------------
585 const vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
586 GetImageMap( unsigned int id ) const
587 {
588   if( id < this->m_ImageMaps.size( ) )
589     return( this->m_ImageMaps[ id ].GetPointer( ) );
590   else
591     return( NULL );
592 }
593
594 // -------------------------------------------------------------------------
595 double cpExtensions::Visualization::ImageSliceActors::
596 GetMinWindow( ) const
597 {
598   return( this->m_MinWindow );
599 }
600
601 // -------------------------------------------------------------------------
602 double cpExtensions::Visualization::ImageSliceActors::
603 GetMaxWindow( ) const
604 {
605   return( this->m_MaxWindow );
606 }
607
608 // -------------------------------------------------------------------------
609 double cpExtensions::Visualization::ImageSliceActors::
610 GetMinLevel( ) const
611 {
612   return( this->m_MinLevel );
613 }
614
615 // -------------------------------------------------------------------------
616 double cpExtensions::Visualization::ImageSliceActors::
617 GetMaxLevel( ) const
618 {
619   return( this->m_MaxLevel );
620 }
621
622 // -------------------------------------------------------------------------
623 double cpExtensions::Visualization::ImageSliceActors::
624 GetWindow( ) const
625 {
626   if( this->m_ImageMaps.size( ) == 0 )
627     return( double( 0 ) );
628   if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
629     return( double( 0 ) );
630
631   vtkWindowLevelLookupTable* lut =
632     dynamic_cast< vtkWindowLevelLookupTable* >(
633       this->m_ImageMaps[ 0 ]->GetLookupTable( )
634       );
635   if( lut != NULL )
636     return( lut->GetWindow( ) );
637   else
638     return( double( 0 ) );
639 }
640
641 // -------------------------------------------------------------------------
642 double cpExtensions::Visualization::ImageSliceActors::
643 GetLevel( ) const
644 {
645   if( this->m_ImageMaps.size( ) == 0 )
646     return( double( 0 ) );
647   if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
648     return( double( 0 ) );
649
650   vtkWindowLevelLookupTable* lut =
651     dynamic_cast< vtkWindowLevelLookupTable* >(
652       this->m_ImageMaps[ 0 ]->GetLookupTable( )
653       );
654   if( lut != NULL )
655     return( lut->GetLevel( ) );
656   else
657     return( double( 0 ) );
658 }
659
660 // -------------------------------------------------------------------------
661 void cpExtensions::Visualization::ImageSliceActors::
662 SetWindow( double w )
663 {
664   if( this->m_ImageMaps.size( ) == 0 )
665     return;
666   if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
667     return;
668   vtkWindowLevelLookupTable* lut =
669     dynamic_cast< vtkWindowLevelLookupTable* >(
670       this->m_ImageMaps[ 0 ]->GetLookupTable( )
671       );
672   if( lut == NULL )
673     return;
674
675   /* TODO: Min and Max values are assigned 0!!!
676      if( w < this->MinWindow )
677      w = this->MinWindow;
678      if( w > this->MaxWindow )
679      w = this->MaxWindow;
680   */
681   lut->SetWindow( w );
682   lut->Build( );
683   this->m_ImageMaps[ 0 ]->Modified( );
684   this->Modified( );
685 }
686
687 // -------------------------------------------------------------------------
688 void cpExtensions::Visualization::ImageSliceActors::
689 SetLevel( double l )
690 {
691   if( this->m_ImageMaps.size( ) == 0 )
692     return;
693   if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
694     return;
695   vtkWindowLevelLookupTable* lut =
696     dynamic_cast< vtkWindowLevelLookupTable* >(
697       this->m_ImageMaps[ 0 ]->GetLookupTable( )
698       );
699   if( lut == NULL )
700     return;
701
702   /* TODO: Min and Max values are assigned 0!!!
703      if( l < this->MinLevel )
704      l = this->MinLevel;
705      if( l > this->MaxLevel )
706      l = this->MaxLevel;
707   */
708   lut->SetLevel( l );
709   lut->Build( );
710   this->m_ImageMaps[ 0 ]->Modified( );
711   this->Modified( );
712 }
713
714 // -------------------------------------------------------------------------
715 void cpExtensions::Visualization::ImageSliceActors::
716 SetWindowLevel( double w, double l )
717 {
718   if( this->m_ImageMaps.size( ) == 0 )
719     return;
720   if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
721     return;
722   vtkWindowLevelLookupTable* lut =
723     dynamic_cast< vtkWindowLevelLookupTable* >(
724       this->m_ImageMaps[ 0 ]->GetLookupTable( )
725       );
726   if( lut == NULL )
727     return;
728
729   /* TODO: Min and Max values are assigned 0!!!
730      if( w < this->MinWindow )
731      w = this->MinWindow;
732      if( w > this->MaxWindow )
733      w = this->MaxWindow;
734      if( l < this->MinLevel )
735      l = this->MinLevel;
736      if( l > this->MaxLevel )
737      l = this->MaxLevel;
738   */
739   lut->SetWindow( w );
740   lut->SetLevel( l );
741   lut->Build( );
742   this->m_ImageMaps[ 0 ]->Modified( );
743   this->UpdateText( w, l );
744   this->Modified( );
745 }
746
747 // -------------------------------------------------------------------------
748 void cpExtensions::Visualization::ImageSliceActors::
749 ResetWindowLevel( )
750 {
751   static const double _2 = double( 2 );
752   this->SetWindowLevel(
753     this->m_MaxWindow, ( this->m_MaxLevel + this->m_MinLevel ) / _2
754     );
755 }
756
757 // -------------------------------------------------------------------------
758 void cpExtensions::Visualization::ImageSliceActors::
759 SetLookupTable( unsigned int id, vtkLookupTable* lut )
760 {
761   if( id < this->m_ImageMaps.size( ) && id > 0 )
762   {
763     if( this->m_ImageMaps[ id ].GetPointer( ) != NULL )
764     {
765       this->m_ImageMaps[ id ]->SetLookupTable( lut );
766       this->m_ImageMaps[ id ]->Modified( );
767       this->Modified( );
768
769     } // fi
770
771   } // fi
772 }
773
774 // -------------------------------------------------------------------------
775 void cpExtensions::Visualization::ImageSliceActors::
776 SetLookupTableAsColor( unsigned int id, double r, double g, double b )
777 {
778   static const double _0 = double( 0 );
779   static const double _2 = double( 2 );
780   static const double _4 = double( 4 );
781   static const double _6 = double( 6 );
782   static const double _OPACITY = double( 0.6 );
783
784   // Check ID consistency
785   if( id == 0 || id >= this->m_ImageMaps.size( ) )
786     return;
787
788   // Get image scalar range
789   vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
790   vtkImageData* image = dynamic_cast< vtkImageData* >(
791     aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
792     );
793   double range[ 2 ];
794   image->GetScalarRange( range );
795
796   // Get HSV from display color
797   double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
798   double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
799   double d = cmax - cmin;
800
801   double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
802   double value = cmax;
803   double hue = _0;
804   if( d > _0 )
805   {
806     if( r == cmax )
807       hue = std::fmod( ( g - b ) / d, _6 );
808     else if( g == cmax )
809       hue = ( ( b - r ) / d ) + _2;
810     else if( b == cmax )
811       hue = ( ( r - g ) / d ) + _4;
812     hue /= _6;
813
814   } // fi
815
816   // Define new lookup table
817   vtkSmartPointer< vtkLookupTable > lut =
818     vtkSmartPointer< vtkLookupTable >::New( );
819   lut->SetScaleToLinear( );
820   lut->SetNanColor( _0, _0, _0, _0 );
821   lut->SetTableRange( range[ 0 ], range[ 1 ] );
822   lut->SetAlphaRange( _0, _OPACITY );
823   lut->SetHueRange( _0, hue );
824   lut->SetSaturationRange( _0, saturation );
825   lut->SetValueRange( _0, value );
826   lut->Build( );
827   this->SetLookupTable( id, lut );
828 }
829
830 // -------------------------------------------------------------------------
831 int cpExtensions::Visualization::ImageSliceActors::
832 GetAxis( ) const
833 {
834   if( this->m_SliceMappers.size( ) > 0 )
835     return( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
836   else
837     return( -1 );
838 }
839
840 // -------------------------------------------------------------------------
841 int cpExtensions::Visualization::ImageSliceActors::
842 GetSliceNumber( ) const
843 {
844   if( this->m_SliceMappers.size( ) > 0 )
845     return( this->m_SliceMappers[ 0 ]->GetSliceNumber( ) );
846   else
847     return( -1 );
848 }
849
850 // -------------------------------------------------------------------------
851 int cpExtensions::Visualization::ImageSliceActors::
852 GetSliceNumberMinValue( ) const
853 {
854   if( this->m_SliceMappers.size( ) > 0 )
855     return( this->m_SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
856   else
857     return( -1 );
858 }
859
860 // -------------------------------------------------------------------------
861 int cpExtensions::Visualization::ImageSliceActors::
862 GetSliceNumberMaxValue( ) const
863 {
864   if( this->m_SliceMappers.size( ) > 0 )
865     return( this->m_SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
866   else
867     return( -1 );
868 }
869
870 // -------------------------------------------------------------------------
871 void cpExtensions::Visualization::ImageSliceActors::
872 SetSliceNumber( const int& slice )
873 {
874   unsigned int nImages = this->m_SliceMappers.size( );
875   if( nImages == 0 )
876     return;
877
878   // Compute plane properties
879   int axis = this->m_SliceMappers[ 0 ]->GetOrientation( );
880   vtkAlgorithm* algo = this->m_SliceMappers[ 0 ]->GetInputAlgorithm( );
881   vtkInformation* info = algo->GetOutputInformation( 0 );
882   int ext[ 6 ];
883   double ori[ 3 ], spac[ 3 ], pos[ 3 ];
884   info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
885   info->Get( vtkDataObject::ORIGIN( ), ori );
886   info->Get( vtkDataObject::SPACING( ), spac );
887   this->m_SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
888
889   // Update display extent
890   int d_ext[ 6 ] =
891     {
892       ext[ 0 ], ext[ 1 ],
893       ext[ 2 ], ext[ 3 ],
894       ext[ 4 ], ext[ 5 ]
895     };
896   d_ext[ axis << 1 ] = slice;
897   d_ext[ ( axis << 1 ) + 1 ] = slice;
898
899   std::cout
900     << d_ext[ 0 ] << " "
901     << d_ext[ 1 ] << " "
902     << d_ext[ 2 ] << " "
903     << d_ext[ 3 ] << " "
904     << d_ext[ 4 ] << " "
905     << d_ext[ 5 ] << " : "
906     << axis << std::endl;
907
908   // Change visualization extent
909   for( unsigned int i = 0; i < nImages; ++i )
910   {
911     this->m_SliceMappers[ i ]->SetSliceNumber( slice );
912     this->m_ImageActors[ i ]->SetDisplayExtent( d_ext );
913     this->m_SliceMappers[ i ]->Modified( );
914     this->m_ImageActors[ i ]->Modified( );
915     this->m_SliceMappers[ i ]->Update( );
916
917   } // rof
918
919   // Prevent obscuring voxels by offsetting the plane geometry
920   double xbnds[ ] =
921     {
922       ori[ 0 ] + ( spac[ 0 ] * double( d_ext[ 0 ] ) ),
923       ori[ 0 ] + ( spac[ 0 ] * double( d_ext[ 1 ] ) )
924     };
925   double ybnds[ ] =
926     {
927       ori[ 1 ] + ( spac[ 1 ] * double( d_ext[ 2 ] ) ),
928       ori[ 1 ] + ( spac[ 1 ] * double( d_ext[ 3 ] ) )
929     };
930   double zbnds[ ] =
931     {
932       ori[ 2 ] + ( spac[ 2 ] * double( d_ext[ 4 ] ) ),
933       ori[ 2 ] + ( spac[ 2 ] * double( d_ext[ 5 ] ) )
934     };
935
936   if( spac[ 0 ] < double( 0 ) )
937   {
938     double t = xbnds[ 0 ];
939     xbnds[ 0 ] = xbnds[ 1 ];
940     xbnds[ 1 ] = t;
941
942   } // fi
943   if( spac[ 1 ] < double( 0 ) )
944   {
945     double t = ybnds[ 0 ];
946     ybnds[ 0 ] = ybnds[ 1 ];
947     ybnds[ 1 ] = t;
948
949   } // fi
950   if( spac[ 2 ] < double( 0 ) )
951   {
952     double t = zbnds[ 0 ];
953     zbnds[ 0 ] = zbnds[ 1 ];
954     zbnds[ 1 ] = t;
955
956   } // fi
957
958   // Plane function origin
959   this->m_PlaneFunction->SetOrigin( pos );
960
961   // Configure visualization and implicit plane orientation
962   this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
963   this->m_PlaneActor->GetProperty( )->SetLineWidth( 2 );
964   vtkPoints* plane_points = this->m_Plane->GetPoints( );
965   if( axis == 0 ) // YZ, x-normal
966   {
967     this->m_PlaneFunction->SetNormal( 1, 0, 0 );
968     plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
969     plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
970     plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
971     plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
972     this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
973   }
974   else if( axis == 1 ) // ZX, y-normal
975   {
976     this->m_PlaneFunction->SetNormal( 0, 1, 0 );
977     plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
978     plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
979     plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
980     plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
981     this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
982   }
983   else // XY, z-normal
984   {
985     this->m_PlaneFunction->SetNormal( 0, 0, 1 );
986     plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
987     plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
988     plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
989     plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
990     this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
991
992   } // fi
993   this->m_PlaneFunction->Modified( );
994   this->m_Plane->Modified( );
995   this->m_PlaneMapper->Modified( );
996   this->m_PlaneActor->Modified( );
997
998   // Update text
999   this->UpdateText( );
1000 }
1001
1002 // -------------------------------------------------------------------------
1003 void cpExtensions::Visualization::ImageSliceActors::
1004 SetSlice( double* pos )
1005 {
1006   vtkImageData* image;
1007   if( this->m_ImageMaps[ 0 ] != NULL )
1008     image =
1009       dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
1010   else
1011     image = this->m_SliceMappers[ 0 ]->GetInput( );
1012
1013   int ijk[ 3 ];
1014   double pcoords[ 3 ];
1015   image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1016   this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
1017 }
1018
1019 // -------------------------------------------------------------------------
1020 void cpExtensions::Visualization::ImageSliceActors::
1021 UpdateText( )
1022 {
1023   if( this->m_SliceMappers.size( ) > 0 )
1024   {
1025     char axis;
1026     int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1027     if     ( axId == 0 ) axis = 'X';
1028     else if( axId == 1 ) axis = 'Y';
1029     else if( axId == 2 ) axis = 'Z';
1030
1031     std::sprintf(
1032       this->m_TextBuffer, "Axis: %c (%d)",
1033       axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( )
1034       );
1035   }
1036   else
1037     this->m_TextBuffer[ 0 ] = '\0';
1038   this->m_TextActor->SetInput( this->m_TextBuffer );
1039   this->m_TextActor->Modified( );
1040   this->Modified( );
1041 }
1042
1043 // -------------------------------------------------------------------------
1044 void cpExtensions::Visualization::ImageSliceActors::
1045 UpdateText( double pos[ 3 ] )
1046 {
1047   if( this->m_SliceMappers.size( ) > 0 )
1048   {
1049     char axis;
1050     int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1051     if     ( axId == 0 ) axis = 'X';
1052     else if( axId == 1 ) axis = 'Y';
1053     else if( axId == 2 ) axis = 'Z';
1054     int slice = this->GetSliceNumber( );
1055
1056     vtkImageData* image;
1057     if( this->m_ImageMaps[ 0 ] != NULL )
1058       image =
1059         dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
1060     else
1061       image = this->m_SliceMappers[ 0 ]->GetInput( );
1062
1063     int ijk[ 3 ];
1064     double pcoords[ 3 ];
1065     image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1066     ijk[ axId ] = slice;
1067
1068     int ext[ 6 ];
1069     image->GetExtent( ext );
1070     if(
1071       ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
1072       ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
1073       ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
1074       )
1075     {
1076       int nScl = image->GetNumberOfScalarComponents( );
1077       std::stringstream str;
1078       str
1079         << "[" << ijk[ 0 ]
1080         << "," << ijk[ 1 ]
1081         << "," << ijk[ 2 ] << "]=(";
1082       str <<
1083         image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
1084       for( int n = 1; n < nScl; ++n )
1085         str
1086           << " "
1087           << image->GetScalarComponentAsFloat(
1088             ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
1089             );
1090       str << ")";
1091       std::sprintf(
1092         this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
1093         axis, slice, str.str( ).c_str( )
1094         );
1095
1096     } // fi
1097   }
1098   else
1099     this->m_TextBuffer[ 0 ] = '\0';
1100   this->m_TextActor->SetInput( this->m_TextBuffer );
1101   this->m_TextActor->Modified( );
1102   this->Modified( );
1103 }
1104
1105 // -------------------------------------------------------------------------
1106 void cpExtensions::Visualization::ImageSliceActors::
1107 UpdateText( const double& w, const double& l )
1108 {
1109   if( this->m_SliceMappers.size( ) > 0 )
1110   {
1111     char axis;
1112     int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1113     if     ( axId == 0 ) axis = 'X';
1114     else if( axId == 1 ) axis = 'Y';
1115     else if( axId == 2 ) axis = 'Z';
1116
1117     std::sprintf(
1118       this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1119       axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1120       );
1121   }
1122   else
1123     this->m_TextBuffer[ 0 ] = '\0';
1124   this->m_TextActor->SetInput( this->m_TextBuffer );
1125   this->m_TextActor->Modified( );
1126   this->Modified( );
1127 }
1128
1129 // -------------------------------------------------------------------------
1130 void cpExtensions::Visualization::ImageSliceActors::
1131 Render( )
1132 {
1133   if( this->m_Window != NULL )
1134     this->m_Window->Render( );
1135 }
1136
1137 // -------------------------------------------------------------------------
1138 void cpExtensions::Visualization::ImageSliceActors::
1139 ResetCamera( )
1140 {
1141   if( this->m_Window == NULL )
1142     return;
1143   vtkRenderer* renderer =
1144     this->m_Window->GetRenderers( )->GetFirstRenderer( );
1145   if( renderer != NULL )
1146     renderer->ResetCamera( );
1147 }
1148
1149 // -------------------------------------------------------------------------
1150 cpExtensions::Visualization::ImageSliceActors::
1151 ImageSliceActors( )
1152   : Superclass( ),
1153     m_Window( NULL ),
1154     m_Interpolate( false )
1155 {
1156   this->Clear( );
1157   this->_ConfigureStyle( );
1158 }
1159
1160 // -------------------------------------------------------------------------
1161 cpExtensions::Visualization::ImageSliceActors::
1162 ~ImageSliceActors( )
1163 {
1164 }
1165
1166 // -------------------------------------------------------------------------
1167 void cpExtensions::Visualization::ImageSliceActors::
1168 _ConfigureStyle( )
1169 {
1170   // Connect this view with a controller
1171   this->m_Style = vtkSmartPointer< TStyle >::New( );
1172   this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1173   this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1174   this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1175   this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1176 }
1177
1178 // -------------------------------------------------------------------------
1179 void cpExtensions::Visualization::ImageSliceActors::
1180 _ConfigureNewLUT( vtkImageData* data )
1181 {
1182   // Does the new LUT is a window-level one?
1183   unsigned int nImgs = this->m_ImageMaps.size( );
1184   unsigned int nCmps = data->GetNumberOfScalarComponents( );
1185
1186   if( nCmps > 1 )
1187   {
1188     this->m_ImageMaps.push_back( NULL );
1189     return;
1190
1191   } // fi
1192
1193   // Create LUT filter
1194   this->m_ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1195   if( nImgs == 0 )
1196   {
1197     double range[ 2 ];
1198     data->GetScalarRange( range );
1199     vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1200       vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1201     lut->SetScaleToLinear( );
1202     lut->SetTableRange( range );
1203     lut->SetWindow( range[ 1 ] - range[ 0 ] );
1204     lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1205     lut->Build( );
1206     this->m_ImageMaps[ 0 ]->SetLookupTable( lut );
1207
1208     this->m_MinWindow = double( 0 );
1209     this->m_MaxWindow = range[ 1 ] - range[ 0 ];
1210     this->m_MinLevel = range[ 0 ];
1211     this->m_MaxLevel = range[ 1 ];
1212   }
1213   else
1214     this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1215 }
1216
1217 // -------------------------------------------------------------------------
1218 void cpExtensions::Visualization::ImageSliceActors::
1219 _ConfigureNewInput( int axis )
1220 {
1221   unsigned int nImages = this->m_ImageActors.size( );
1222
1223   // Configure mapper
1224   vtkImageSliceMapper* mapper = this->m_SliceMappers[ nImages ];
1225   if( nImages == 0 )
1226     mapper->SetOrientation( axis );
1227   else
1228     mapper->SetOrientation( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
1229   mapper->Update( );
1230
1231   // Create actor
1232   vtkSmartPointer< vtkImageActor > actor =
1233     vtkSmartPointer< vtkImageActor >::New( );
1234   this->m_ImageActors.push_back( actor );
1235   actor->SetMapper( mapper );
1236   actor->SetInterpolate( this->m_Interpolate );
1237   actor->Modified( );
1238
1239   if( nImages == 0 )
1240     this->m_Style->AssociateImageActor( actor );
1241   this->AddItem( actor );
1242
1243   if( nImages > 1 )
1244     this->SetSliceNumber( this->GetSliceNumber( ) );
1245   else
1246     this->SetSliceNumber( this->GetSliceNumberMaxValue( ) );
1247   this->Modified( );
1248 }
1249
1250 // -------------------------------------------------------------------------
1251 void cpExtensions::Visualization::ImageSliceActors::
1252 _MouseMoveCommand(
1253   void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1254   bool alt, bool ctr, bool sft
1255   )
1256 {
1257   ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1258   if( actors == NULL )
1259     return;
1260
1261   if( btn == TStyle::ButtonID_None )
1262   {
1263     // Just show the pixel information
1264     actors->SetCursor( pos );
1265     actors->UpdateText( pos );
1266     actors->Render( );
1267   }
1268   else if( btn == TStyle::ButtonID_Left )
1269   {
1270     /* TODO
1271        unsigned int nC = actors->m_SlicesCommands.size( );
1272        if( !alt && ctr && !sft && nC > 0 )
1273        {
1274        for( unsigned int i = 0; i < nC; ++i )
1275        actors->m_SlicesCommands[ i ].first(
1276        pos, actors->GetAxis( ), actors->m_SlicesCommands[ i ].second
1277        );
1278        actors->Render( );
1279
1280        } // fi
1281     */
1282   }
1283   else if( btn == TStyle::ButtonID_Right )
1284   {
1285     if( !alt && !ctr && sft )
1286     {
1287       // Change image window level
1288       double bounds[ 6 ];
1289       actors->m_SliceMappers[ 0 ]->GetBounds( bounds );
1290
1291       int a0 = actors->GetAxis( );
1292       int a1 = ( a0 + 1 ) % 3;
1293       int a2 = ( a0 + 2 ) % 3;
1294       double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1295       double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1296       dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1297       dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1298
1299       dx *= actors->m_StartWindowLevel[ 0 ];
1300       dy *= actors->m_StartWindowLevel[ 1 ];
1301       dx += actors->m_StartWindowLevel[ 0 ];
1302       dy += actors->m_StartWindowLevel[ 1 ];
1303       actors->SetWindowLevel( dx, dy );
1304       actors->Render( );
1305
1306       // Associate objects
1307       auto i = actors->m_WindowLevelCommands.begin( );
1308       for( ; i != actors->m_WindowLevelCommands.end( ); ++i )
1309         i->first( dx, dy, i->second );
1310       
1311     } // fi
1312
1313   } // fi
1314 }
1315
1316 // -------------------------------------------------------------------------
1317 void cpExtensions::Visualization::ImageSliceActors::
1318 _MouseClickCommand(
1319   void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1320   bool alt, bool ctr, bool sft
1321   )
1322 {
1323   ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1324   if( actors == NULL )
1325     return;
1326
1327   actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1328   actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1329   actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1330   actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1331   actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1332 }
1333
1334 // -------------------------------------------------------------------------
1335 void cpExtensions::Visualization::ImageSliceActors::
1336 _MouseWheelCommand(
1337   void* data, const int& dir,
1338   bool alt, bool ctr, bool sft
1339   )
1340 {
1341   ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1342   if( actors == NULL )
1343     return;
1344
1345   if( !alt && !ctr )
1346   {
1347     int slice = actors->GetSliceNumber( ) + ( dir * ( ( sft )? 10: 1 ) );
1348     if( slice < actors->GetSliceNumberMinValue( ) )
1349       slice = actors->GetSliceNumberMinValue( );
1350     if( slice > actors->GetSliceNumberMaxValue( ) )
1351       slice = actors->GetSliceNumberMaxValue( );
1352     actors->SetSliceNumber( slice );
1353
1354     auto a = actors->m_AssociatedSlices.begin( );
1355     for( ; a != actors->m_AssociatedSlices.end( ); ++a )
1356     {
1357       ( *a )->SetSliceNumber( slice );
1358       ( *a )->Render( );
1359
1360     } // rof
1361     actors->Render( );
1362     
1363     // Associate objects
1364     auto i = actors->m_RenderCommands.begin( );
1365     for( ; i != actors->m_RenderCommands.end( ); ++i )
1366       i->first( i->second );
1367
1368   } // fi
1369 }
1370
1371 // -------------------------------------------------------------------------
1372 void cpExtensions::Visualization::ImageSliceActors::
1373 _KeyCommand( void* data, const char& key )
1374 {
1375   ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1376   if( actors == NULL )
1377     return;
1378
1379   switch( key )
1380   {
1381   case 'r': case 'R':
1382   {
1383     actors->ResetCamera( );
1384     actors->Render( );
1385
1386     // Associate objects
1387     auto i = actors->m_RenderCommands.begin( );
1388     for( ; i != actors->m_RenderCommands.end( ); ++i )
1389       i->first( i->second );
1390   }
1391   break;
1392   case 'w': case 'W':
1393   {
1394     actors->ResetWindowLevel( );
1395     actors->Render( );
1396
1397     // Associate objects
1398     auto i = actors->m_RenderCommands.begin( );
1399     for( ; i != actors->m_RenderCommands.end( ); ++i )
1400       i->first( i->second );
1401   }
1402   break;
1403   default:
1404     break;
1405   } // hctiws
1406 }
1407
1408 // eof - $RCSfile$