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