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