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