1 #include <cpExtensions/Visualization/ImageSliceActors.h>
6 #include <vtkAlgorithmOutput.h>
8 #include <vtkCellArray.h>
9 #include <vtkImageData.h>
10 #include <vtkInformation.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>
22 // -------------------------------------------------------------------------
23 cpExtensions::Visualization::ImageSliceActors*
24 cpExtensions::Visualization::ImageSliceActors::
27 return( new Self( ) );
30 // -------------------------------------------------------------------------
31 void cpExtensions::Visualization::ImageSliceActors::
32 AddInputConnection( vtkAlgorithmOutput* aout, int axis )
34 // Get input vtkImageData
37 vtkAlgorithm* producer = aout->GetProducer( );
38 vtkImageData* data = dynamic_cast< vtkImageData* >(
39 producer->GetOutputDataObject( aout->GetIndex( ) )
44 // Try to infere if input comes from a color mapping filter
45 vtkImageMapToColors* new_map =
46 dynamic_cast< vtkImageMapToColors* >( producer );
49 // Configure LUT, if possible (NULL is returned if not)
50 this->_ConfigureNewLUT( data );
51 new_map = *( this->m_ImageMaps.rbegin( ) );
54 new_map->SetInputConnection( aout );
60 this->m_ImageMaps.push_back( new_map );
62 // Update window level values
66 dynamic_cast< vtkImageData* >( new_map->GetInput( ) )->
67 GetScalarRange( range );
68 this->m_MinWindow = double( 0 );
69 this->m_MaxWindow = range[ 1 ] - range[ 0 ];
70 this->m_MinLevel = range[ 0 ];
71 this->m_MaxLevel = range[ 1 ];
75 // Create mapper and actors
76 vtkSmartPointer< vtkImageSliceMapper > mapper =
77 vtkSmartPointer< vtkImageSliceMapper >::New( );
79 mapper->SetInputConnection( new_map->GetOutputPort( ) );
81 mapper->SetInputConnection( aout );
82 this->m_SliceMappers.push_back( mapper );
83 this->_ConfigureNewInput( axis );
86 // -------------------------------------------------------------------------
87 void cpExtensions::Visualization::ImageSliceActors::
88 AddInputData( vtkImageData* data, int axis )
90 // Update window level values
91 if( this->m_ImageMaps.size( ) == 0 )
94 data->GetScalarRange( range );
95 this->m_MinWindow = double( 0 );
96 this->m_MaxWindow = range[ 1 ] - range[ 0 ];
97 this->m_MinLevel = range[ 0 ];
98 this->m_MaxLevel = range[ 1 ];
102 // Configure LUT, if possible (NULL is returned if not)
103 this->_ConfigureNewLUT( data );
104 vtkImageMapToColors* new_map = *( this->m_ImageMaps.rbegin( ) );
105 if( new_map != NULL )
107 new_map->SetInputData( data );
112 // Create mapper and actors
113 vtkSmartPointer< vtkImageSliceMapper > mapper =
114 vtkSmartPointer< vtkImageSliceMapper >::New( );
115 if( new_map != NULL )
116 mapper->SetInputConnection( new_map->GetOutputPort( ) );
118 mapper->SetInputData( data );
119 this->m_SliceMappers.push_back( mapper );
120 this->_ConfigureNewInput( axis );
123 // -------------------------------------------------------------------------
124 void cpExtensions::Visualization::ImageSliceActors::
128 this->m_MinWindow = double( 0 );
129 this->m_MaxWindow = double( 0 );
130 this->m_MinLevel = double( 0 );
131 this->m_MaxLevel = double( 0 );
132 this->m_VisibleExtent[ 0 ] =
133 this->m_VisibleExtent[ 2 ] =
134 this->m_VisibleExtent[ 4 ] = -1;
135 this->m_VisibleExtent[ 1 ] =
136 this->m_VisibleExtent[ 3 ] =
137 this->m_VisibleExtent[ 5 ] = 0;
138 this->m_VisibleBounds[ 0 ] =
139 this->m_VisibleBounds[ 2 ] =
140 this->m_VisibleBounds[ 4 ] = double( 0 );
141 this->m_VisibleBounds[ 1 ] =
142 this->m_VisibleBounds[ 3 ] =
143 this->m_VisibleBounds[ 5 ] = double( 0 );
145 // Unbind from container
146 this->RemoveAllItems( );
149 this->m_ImageMaps.clear( );
150 this->m_SliceMappers.clear( );
151 this->m_ImageActors.clear( );
153 // Reconfigure unique objects
154 this->m_Cursor = vtkSmartPointer< vtkPolyData >::New( );
155 this->m_CursorMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
156 this->m_CursorActor = vtkSmartPointer< vtkActor >::New( );
157 this->m_HorizontalLine = vtkSmartPointer< vtkPolyData >::New( );
158 this->m_HorizontalLineMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
159 this->m_HorizontalLineActor = vtkSmartPointer< vtkActor >::New( );
160 this->m_VerticalLine = vtkSmartPointer< vtkPolyData >::New( );
161 this->m_VerticalLineMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
162 this->m_VerticalLineActor = vtkSmartPointer< vtkActor >::New( );
163 this->m_Plane = vtkSmartPointer< vtkPolyData >::New( );
164 this->m_PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
165 this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
166 this->m_PlaneActor = vtkSmartPointer< vtkActor >::New( );
167 this->m_TextBuffer[ 0 ] = '\0';
169 // Unique objects configuration
170 vtkSmartPointer< vtkPoints > cursor_points =
171 vtkSmartPointer< vtkPoints >::New( );
172 vtkSmartPointer< vtkCellArray > cursor_lines =
173 vtkSmartPointer< vtkCellArray >::New( );
174 cursor_points->InsertNextPoint( 0, 0, 0 );
175 cursor_points->InsertNextPoint( 0, 0, 0 );
176 cursor_points->InsertNextPoint( 0, 0, 0 );
177 cursor_points->InsertNextPoint( 0, 0, 0 );
178 cursor_points->InsertNextPoint( 0, 0, 0 );
179 cursor_points->InsertNextPoint( 0, 0, 0 );
180 cursor_points->InsertNextPoint( 0, 0, 0 );
181 cursor_points->InsertNextPoint( 0, 0, 0 );
182 cursor_lines->InsertNextCell( 2 );
183 cursor_lines->InsertCellPoint( 0 );
184 cursor_lines->InsertCellPoint( 1 );
185 cursor_lines->InsertNextCell( 2 );
186 cursor_lines->InsertCellPoint( 2 );
187 cursor_lines->InsertCellPoint( 3 );
188 cursor_lines->InsertNextCell( 2 );
189 cursor_lines->InsertCellPoint( 4 );
190 cursor_lines->InsertCellPoint( 5 );
191 cursor_lines->InsertNextCell( 2 );
192 cursor_lines->InsertCellPoint( 6 );
193 cursor_lines->InsertCellPoint( 7 );
194 this->m_Cursor->SetPoints( cursor_points );
195 this->m_Cursor->SetLines( cursor_lines );
196 this->m_CursorMapper->SetInputData( this->m_Cursor );
197 this->m_CursorActor->SetMapper( this->m_CursorMapper );
199 vtkSmartPointer< vtkPoints > h_points =
200 vtkSmartPointer< vtkPoints >::New( );
201 vtkSmartPointer< vtkCellArray > h_lines =
202 vtkSmartPointer< vtkCellArray >::New( );
203 h_points->InsertNextPoint( 0, 0, 0 );
204 h_points->InsertNextPoint( 0, 0, 0 );
205 h_lines->InsertNextCell( 2 );
206 h_lines->InsertCellPoint( 0 );
207 h_lines->InsertCellPoint( 1 );
208 this->m_HorizontalLine->SetPoints( h_points );
209 this->m_HorizontalLine->SetLines( h_lines );
210 this->m_HorizontalLineMapper->SetInputData( this->m_HorizontalLine );
211 this->m_HorizontalLineActor->SetMapper( this->m_HorizontalLineMapper );
213 vtkSmartPointer< vtkPoints > v_points =
214 vtkSmartPointer< vtkPoints >::New( );
215 vtkSmartPointer< vtkCellArray > v_lines =
216 vtkSmartPointer< vtkCellArray >::New( );
217 v_points->InsertNextPoint( 0, 0, 0 );
218 v_points->InsertNextPoint( 0, 0, 0 );
219 v_lines->InsertNextCell( 2 );
220 v_lines->InsertCellPoint( 0 );
221 v_lines->InsertCellPoint( 1 );
222 this->m_VerticalLine->SetPoints( v_points );
223 this->m_VerticalLine->SetLines( v_lines );
224 this->m_VerticalLineMapper->SetInputData( this->m_VerticalLine );
225 this->m_VerticalLineActor->SetMapper( this->m_VerticalLineMapper );
227 vtkSmartPointer< vtkPoints > plane_points =
228 vtkSmartPointer< vtkPoints >::New( );
229 vtkSmartPointer< vtkCellArray > plane_lines =
230 vtkSmartPointer< vtkCellArray >::New( );
232 plane_points->InsertNextPoint( 0, 0, 0 );
233 plane_points->InsertNextPoint( 0, 1, 0 );
234 plane_points->InsertNextPoint( 1, 1, 0 );
235 plane_points->InsertNextPoint( 1, 0, 0 );
236 plane_lines->InsertNextCell( 5 );
237 plane_lines->InsertCellPoint( 0 );
238 plane_lines->InsertCellPoint( 1 );
239 plane_lines->InsertCellPoint( 2 );
240 plane_lines->InsertCellPoint( 3 );
241 plane_lines->InsertCellPoint( 0 );
242 this->m_Plane->SetPoints( plane_points );
243 this->m_Plane->SetLines( plane_lines );
245 this->m_PlaneMapper->SetInputData( this->m_Plane );
246 this->m_PlaneActor->SetMapper( this->m_PlaneMapper );
248 this->m_TextActor->SetTextScaleModeToNone( );
249 vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
250 textprop->SetColor( 1, 1, 1 );
251 textprop->SetFontFamilyToCourier( );
252 textprop->SetFontSize( 18 );
253 textprop->BoldOff( );
254 textprop->ItalicOff( );
255 textprop->ShadowOff( );
256 textprop->SetJustificationToLeft( );
257 textprop->SetVerticalJustificationToBottom( );
258 vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
259 coord->SetCoordinateSystemToNormalizedViewport( );
260 coord->SetValue( 0.01, 0.01 );
262 // Update actor collection
263 this->AddItem( this->m_CursorActor );
264 this->AddItem( this->m_HorizontalLineActor );
265 this->AddItem( this->m_VerticalLineActor );
266 this->AddItem( this->m_TextActor );
267 this->AddItem( this->m_PlaneActor );
270 // -------------------------------------------------------------------------
271 void cpExtensions::Visualization::ImageSliceActors::
272 AssociateSlice( Self* slice )
274 this->m_AssociatedSlices.push_back( slice );
278 // -------------------------------------------------------------------------
279 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
282 return( this->m_Style.GetPointer( ) );
285 // -------------------------------------------------------------------------
286 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
289 return( this->m_Style.GetPointer( ) );
292 // -------------------------------------------------------------------------
293 vtkImageData* cpExtensions::Visualization::ImageSliceActors::
294 GetInputImage( unsigned int id )
296 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
297 vtkImageData* image = dynamic_cast< vtkImageData* >(
298 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
303 // -------------------------------------------------------------------------
304 const vtkImageData* cpExtensions::Visualization::ImageSliceActors::
305 GetInputImage( unsigned int id ) const
307 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
308 const vtkImageData* image = dynamic_cast< const vtkImageData* >(
309 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
314 // -------------------------------------------------------------------------
315 void cpExtensions::Visualization::ImageSliceActors::
316 PushActorsInto( vtkRenderWindow* window, bool force_style )
318 this->m_Window = window;
321 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
322 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
323 if( rwi == NULL || renderer == NULL )
327 if( this->m_Style.GetPointer( ) != NULL && force_style )
328 rwi->SetInteractorStyle( this->m_Style );
332 this->InitTraversal( );
333 while( prop = this->GetNextProp( ) )
334 renderer->AddViewProp( prop );
335 renderer->Modified( );
338 renderer->RemoveViewProp( this->m_CursorActor );
339 renderer->RemoveViewProp( this->m_TextActor );
344 vtkCamera* camera = renderer->GetActiveCamera( );
345 if( camera != NULL && force_style )
347 // Parallel projections are better when displaying 2D images
348 int axis = this->GetAxis( );
349 camera->ParallelProjectionOn( );
350 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
353 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
354 camera->SetViewUp ( double( 0 ), double( 0 ), double( 1 ) );
358 camera->SetPosition( double( 0 ), double( -1 ), double( 0 ) );
359 camera->SetViewUp ( double( 0 ), double( 0 ), double( 1 ) );
361 else // if( axis == 2 )
363 camera->SetPosition( double( 0 ), double( 0 ), double( -1 ) );
364 camera->SetViewUp ( double( 0 ), double( -1 ), double( 0 ) );
369 this->ResetCamera( );
373 // -------------------------------------------------------------------------
374 void cpExtensions::Visualization::ImageSliceActors::
375 PopActorsFrom( vtkRenderWindow* window )
377 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
378 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
380 if( renderer != NULL )
384 this->InitTraversal( );
385 while( prop = this->GetNextProp( ) )
386 renderer->RemoveViewProp( prop );
387 renderer->Modified( );
394 // -------------------------------------------------------------------------
395 unsigned int cpExtensions::Visualization::ImageSliceActors::
396 GetNumberOfImageActors( ) const
398 return( this->m_ImageActors.size( ) );
401 // -------------------------------------------------------------------------
402 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
403 GetImageActor( unsigned int id )
405 if( id < this->m_ImageActors.size( ) )
406 return( this->m_ImageActors[ id ] );
411 // -------------------------------------------------------------------------
412 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
413 GetImageActor( unsigned int id ) const
415 if( id < this->m_ImageActors.size( ) )
416 return( this->m_ImageActors[ id ] );
421 // -------------------------------------------------------------------------
422 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
425 return( this->m_TextActor );
428 // -------------------------------------------------------------------------
429 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
430 GetTextActor( ) const
432 return( this->m_TextActor );
435 // -------------------------------------------------------------------------
436 vtkActor* cpExtensions::Visualization::ImageSliceActors::
439 return( this->m_PlaneActor );
442 // -------------------------------------------------------------------------
443 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
444 GetPlaneActor( ) const
446 return( this->m_PlaneActor );
449 // -------------------------------------------------------------------------
450 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
453 return( this->m_SliceMappers[ 0 ]->GetSlicePlane( ) );
456 // -------------------------------------------------------------------------
457 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
458 GetPlaneFunction( ) const
460 return( this->m_SliceMappers[ 0 ]->GetSlicePlane( ) );
463 // -------------------------------------------------------------------------
464 void cpExtensions::Visualization::ImageSliceActors::
465 SetInterpolate( bool v )
467 if( this->m_Interpolate != v )
469 for( unsigned int i = 0; i < this->m_ImageActors.size( ); ++i )
470 this->m_ImageActors[ i ]->SetInterpolate( v );
471 this->m_Interpolate = v;
477 // -------------------------------------------------------------------------
478 void cpExtensions::Visualization::ImageSliceActors::
481 this->SetInterpolate( true );
484 // -------------------------------------------------------------------------
485 void cpExtensions::Visualization::ImageSliceActors::
488 this->SetInterpolate( false );
491 // -------------------------------------------------------------------------
492 double* cpExtensions::Visualization::ImageSliceActors::
493 GetDisplayBounds( ) const
495 if( this->m_ImageActors.size( ) > 0 )
496 return( this->m_ImageActors[ 0 ]->GetDisplayBounds( ) );
501 // -------------------------------------------------------------------------
502 void cpExtensions::Visualization::ImageSliceActors::
503 GetDisplayBounds( double bounds[ 6 ] ) const
505 if( this->m_ImageActors.size( ) == 0 )
507 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
508 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
511 this->m_ImageActors[ 0 ]->GetDisplayBounds( bounds );
514 // -------------------------------------------------------------------------
515 void cpExtensions::Visualization::ImageSliceActors::
518 if( this->m_SliceMappers.size( ) > 0 )
521 this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
524 this->m_VisibleBounds[ 0 ],
525 this->m_VisibleBounds[ 2 ],
526 this->m_VisibleBounds[ 4 ]
528 this->SetCursor( pos );
532 vtkPoints* points = this->m_Cursor->GetPoints( );
533 points->SetPoint( 0, 0, 0, 0 );
534 points->SetPoint( 1, 0, 0, 0 );
535 points->SetPoint( 2, 0, 0, 0 );
536 points->SetPoint( 3, 0, 0, 0 );
537 points->SetPoint( 4, 0, 0, 0 );
538 points->SetPoint( 5, 0, 0, 0 );
539 points->SetPoint( 6, 0, 0, 0 );
540 points->SetPoint( 7, 0, 0, 0 );
541 this->m_Cursor->Modified( );
542 this->m_CursorMapper->Modified( );
543 this->m_CursorActor->Modified( );
548 // -------------------------------------------------------------------------
549 void cpExtensions::Visualization::ImageSliceActors::
550 SetCursor( double pos[ 3 ] )
552 if( this->m_SliceMappers.size( ) == 0 )
556 int a0 = this->GetAxis( );
557 int a1 = ( a0 + 1 ) % 3;
558 int a2 = ( a0 + 2 ) % 3;
564 double* bounds = this->m_VisibleBounds;
566 p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
567 p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
569 p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
570 p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
571 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
572 p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
573 p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
574 p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
575 p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
576 p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
578 vtkPoints* points = this->m_Cursor->GetPoints( );
579 points->SetPoint( 0, p0 );
580 points->SetPoint( 1, p1 );
581 points->SetPoint( 2, p2 );
582 points->SetPoint( 3, p3 );
583 points->SetPoint( 4, p4 );
584 points->SetPoint( 5, p5 );
585 points->SetPoint( 6, p6 );
586 points->SetPoint( 7, p7 );
587 this->m_Cursor->Modified( );
588 this->m_CursorMapper->Modified( );
589 this->m_CursorActor->Modified( );
592 // -------------------------------------------------------------------------
593 vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
594 GetImageMap( unsigned int id )
596 if( id < this->m_ImageMaps.size( ) )
597 return( this->m_ImageMaps[ id ].GetPointer( ) );
602 // -------------------------------------------------------------------------
603 const vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
604 GetImageMap( unsigned int id ) const
606 if( id < this->m_ImageMaps.size( ) )
607 return( this->m_ImageMaps[ id ].GetPointer( ) );
612 // -------------------------------------------------------------------------
613 double cpExtensions::Visualization::ImageSliceActors::
614 GetMinWindow( ) const
616 return( this->m_MinWindow );
619 // -------------------------------------------------------------------------
620 double cpExtensions::Visualization::ImageSliceActors::
621 GetMaxWindow( ) const
623 return( this->m_MaxWindow );
626 // -------------------------------------------------------------------------
627 double cpExtensions::Visualization::ImageSliceActors::
630 return( this->m_MinLevel );
633 // -------------------------------------------------------------------------
634 double cpExtensions::Visualization::ImageSliceActors::
637 return( this->m_MaxLevel );
640 // -------------------------------------------------------------------------
641 double cpExtensions::Visualization::ImageSliceActors::
644 if( this->m_ImageMaps.size( ) == 0 )
645 return( double( 0 ) );
646 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
647 return( double( 0 ) );
649 vtkWindowLevelLookupTable* lut =
650 dynamic_cast< vtkWindowLevelLookupTable* >(
651 this->m_ImageMaps[ 0 ]->GetLookupTable( )
654 return( lut->GetWindow( ) );
656 return( double( 0 ) );
659 // -------------------------------------------------------------------------
660 double cpExtensions::Visualization::ImageSliceActors::
663 if( this->m_ImageMaps.size( ) == 0 )
664 return( double( 0 ) );
665 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
666 return( double( 0 ) );
668 vtkWindowLevelLookupTable* lut =
669 dynamic_cast< vtkWindowLevelLookupTable* >(
670 this->m_ImageMaps[ 0 ]->GetLookupTable( )
673 return( lut->GetLevel( ) );
675 return( double( 0 ) );
678 // -------------------------------------------------------------------------
679 void cpExtensions::Visualization::ImageSliceActors::
680 SetWindow( double w )
682 if( this->m_ImageMaps.size( ) == 0 )
684 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
686 vtkWindowLevelLookupTable* lut =
687 dynamic_cast< vtkWindowLevelLookupTable* >(
688 this->m_ImageMaps[ 0 ]->GetLookupTable( )
693 if( w < this->m_MinWindow )
694 w = this->m_MinWindow;
695 if( w > this->m_MaxWindow )
696 w = this->m_MaxWindow;
700 this->m_ImageMaps[ 0 ]->Modified( );
704 // -------------------------------------------------------------------------
705 void cpExtensions::Visualization::ImageSliceActors::
708 if( this->m_ImageMaps.size( ) == 0 )
710 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
712 vtkWindowLevelLookupTable* lut =
713 dynamic_cast< vtkWindowLevelLookupTable* >(
714 this->m_ImageMaps[ 0 ]->GetLookupTable( )
719 if( l < this->m_MinLevel )
720 l = this->m_MinLevel;
721 if( l > this->m_MaxLevel )
722 l = this->m_MaxLevel;
726 this->m_ImageMaps[ 0 ]->Modified( );
730 // -------------------------------------------------------------------------
731 void cpExtensions::Visualization::ImageSliceActors::
732 SetWindowLevel( double w, double l )
734 if( this->m_ImageMaps.size( ) == 0 )
736 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
738 vtkWindowLevelLookupTable* lut =
739 dynamic_cast< vtkWindowLevelLookupTable* >(
740 this->m_ImageMaps[ 0 ]->GetLookupTable( )
745 if( w < this->m_MinWindow )
746 w = this->m_MinWindow;
747 if( w > this->m_MaxWindow )
748 w = this->m_MaxWindow;
749 if( l < this->m_MinLevel )
750 l = this->m_MinLevel;
751 if( l > this->m_MaxLevel )
752 l = this->m_MaxLevel;
757 this->m_ImageMaps[ 0 ]->Modified( );
758 this->UpdateText( w, l );
762 // -------------------------------------------------------------------------
763 void cpExtensions::Visualization::ImageSliceActors::
766 static const double _2 = double( 2 );
767 this->SetWindowLevel(
768 this->m_MaxWindow, ( this->m_MaxLevel + this->m_MinLevel ) / _2
772 // -------------------------------------------------------------------------
773 void cpExtensions::Visualization::ImageSliceActors::
774 SetLookupTable( unsigned int id, vtkLookupTable* lut )
776 if( id < this->m_ImageMaps.size( ) && id > 0 )
778 if( this->m_ImageMaps[ id ].GetPointer( ) != NULL )
780 this->m_ImageMaps[ id ]->SetLookupTable( lut );
781 this->m_ImageMaps[ id ]->Modified( );
789 // -------------------------------------------------------------------------
790 void cpExtensions::Visualization::ImageSliceActors::
791 SetLookupTableAsColor( unsigned int id, double r, double g, double b )
793 static const double _0 = double( 0 );
794 static const double _2 = double( 2 );
795 static const double _4 = double( 4 );
796 static const double _6 = double( 6 );
797 static const double _OPACITY = double( 0.6 );
799 // Check ID consistency
800 if( id == 0 || id >= this->m_ImageMaps.size( ) )
803 // Get image scalar range
804 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
805 vtkImageData* image = dynamic_cast< vtkImageData* >(
806 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
809 image->GetScalarRange( range );
811 // Get HSV from display color
812 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
813 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
814 double d = cmax - cmin;
816 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
822 hue = std::fmod( ( g - b ) / d, _6 );
824 hue = ( ( b - r ) / d ) + _2;
826 hue = ( ( r - g ) / d ) + _4;
831 // Define new lookup table
832 vtkSmartPointer< vtkLookupTable > lut =
833 vtkSmartPointer< vtkLookupTable >::New( );
834 lut->SetScaleToLinear( );
835 lut->SetNanColor( _0, _0, _0, _0 );
836 lut->SetTableRange( range[ 0 ], range[ 1 ] );
837 lut->SetAlphaRange( _0, _OPACITY );
838 lut->SetHueRange( _0, hue );
839 lut->SetSaturationRange( _0, saturation );
840 lut->SetValueRange( _0, value );
842 this->SetLookupTable( id, lut );
845 // -------------------------------------------------------------------------
846 int cpExtensions::Visualization::ImageSliceActors::
849 if( this->m_SliceMappers.size( ) > 0 )
850 return( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
855 // -------------------------------------------------------------------------
856 int cpExtensions::Visualization::ImageSliceActors::
857 GetSliceNumber( ) const
859 if( this->m_SliceMappers.size( ) > 0 )
860 return( this->m_SliceMappers[ 0 ]->GetSliceNumber( ) );
865 // -------------------------------------------------------------------------
866 int cpExtensions::Visualization::ImageSliceActors::
867 GetSliceNumberMinValue( ) const
869 if( this->m_SliceMappers.size( ) > 0 )
870 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
875 // -------------------------------------------------------------------------
876 int cpExtensions::Visualization::ImageSliceActors::
877 GetSliceNumberMaxValue( ) const
879 if( this->m_SliceMappers.size( ) > 0 )
880 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
885 // -------------------------------------------------------------------------
886 void cpExtensions::Visualization::ImageSliceActors::
887 SetSliceNumber( const int& slice )
889 unsigned int nImages = this->m_SliceMappers.size( );
893 int axis = this->GetAxis( );
894 double prev_pos = this->m_VisibleBounds[ axis << 1 ];
896 // Change visualization extent
897 for( unsigned int i = 0; i < nImages; ++i )
899 // Update mappers and display bounds
900 this->m_SliceMappers[ i ]->SetSliceNumber( slice );
901 this->m_SliceMappers[ i ]->Modified( );
902 this->m_ImageActors[ i ]->Modified( );
903 this->m_SliceMappers[ i ]->Update( );
905 // Update display extent (this isn't done automatically)
908 this->m_SliceMappers[ i ]->GetInput( )->
909 GetExtent( this->m_VisibleExtent );
910 this->m_VisibleExtent[ axis << 1 ] = slice;
911 this->m_VisibleExtent[ ( axis << 1 ) + 1 ] = slice;
914 this->m_ImageActors[ i ]->SetDisplayExtent( this->m_VisibleExtent );
918 this->m_SliceMappers[ 0 ]->GetBounds( this->m_VisibleBounds );
922 this->m_VisibleBounds[ 0 ],
923 this->m_VisibleBounds[ 2 ],
924 this->m_VisibleBounds[ 4 ]
927 this->m_VisibleBounds[ 1 ],
928 this->m_VisibleBounds[ 3 ],
929 this->m_VisibleBounds[ 5 ]
934 vtkPlane* plane = this->m_SliceMappers[ 0 ]->GetSlicePlane( );
935 plane->GeneralizedProjectPoint( x0[ 0 ], p0[ 0 ] );
936 plane->GeneralizedProjectPoint( x0[ 1 ], p0[ 1 ] );
938 this->m_VisibleBounds[ 0 ] = p0[ 0 ][ 0 ];
939 this->m_VisibleBounds[ 1 ] = p0[ 1 ][ 0 ];
940 this->m_VisibleBounds[ 2 ] = p0[ 0 ][ 1 ];
941 this->m_VisibleBounds[ 3 ] = p0[ 1 ][ 1 ];
942 this->m_VisibleBounds[ 4 ] = p0[ 0 ][ 2 ];
943 this->m_VisibleBounds[ 5 ] = p0[ 1 ][ 2 ];
944 double* bnds = this->m_VisibleBounds;
946 // Configure visualization and implicit plane orientation
947 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
948 this->m_PlaneActor->GetProperty( )->SetLineWidth( 3 );
949 vtkPoints* plane_points = this->m_Plane->GetPoints( );
950 plane_points->SetPoint( 0, bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] );
951 plane_points->SetPoint( 2, bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] );
952 if( axis == 0 || axis == 2 ) // YZ, x-normal
954 plane_points->SetPoint( 1, bnds[ 1 ], bnds[ 2 ], bnds[ 5 ] );
955 plane_points->SetPoint( 3, bnds[ 0 ], bnds[ 3 ], bnds[ 4 ] );
957 this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
959 this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
961 else if( axis == 1 ) // ZX, y-normal
963 plane_points->SetPoint( 1, bnds[ 0 ], bnds[ 2 ], bnds[ 5 ] );
964 plane_points->SetPoint( 3, bnds[ 1 ], bnds[ 3 ], bnds[ 4 ] );
965 this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
968 this->m_Plane->Modified( );
969 this->m_PlaneMapper->Modified( );
970 this->m_PlaneActor->Modified( );
975 // Update lines from associated slices
977 auto sIt = this->m_AssociatedSlices.begin( );
978 for( ; sIt != this->m_AssociatedSlices.end( ); ++sIt )
981 for( unsigned int id = 0; id < slice->m_AssociatedSlices.size( ); ++id )
983 std::cout << id << std::endl;
984 if( slice->m_AssociatedSlices[ id ] != this )
987 std::cout << "id : " << id << std::endl;
994 // Update camera position
995 if( this->m_Window == NULL )
997 vtkRenderer* renderer =
998 this->m_Window->GetRenderers( )->GetFirstRenderer( );
999 if( renderer == NULL )
1001 vtkCamera* camera = renderer->GetActiveCamera( );
1002 if( camera == NULL )
1004 double cam_pos[ 3 ];
1005 camera->GetPosition( cam_pos );
1006 cam_pos[ axis ] += this->m_VisibleBounds[ axis << 1 ] - prev_pos;
1007 camera->SetPosition( cam_pos );
1010 // -------------------------------------------------------------------------
1011 void cpExtensions::Visualization::ImageSliceActors::
1012 SetSlice( double* pos )
1014 vtkImageData* image;
1015 if( this->m_ImageMaps[ 0 ] != NULL )
1017 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
1019 image = this->m_SliceMappers[ 0 ]->GetInput( );
1022 double pcoords[ 3 ];
1023 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1024 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
1027 // -------------------------------------------------------------------------
1028 void cpExtensions::Visualization::ImageSliceActors::
1031 if( this->m_SliceMappers.size( ) > 0 )
1034 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1035 if ( axId == 0 ) axis = 'X';
1036 else if( axId == 1 ) axis = 'Y';
1037 else if( axId == 2 ) axis = 'Z';
1040 this->m_TextBuffer, "Axis: %c (%d)",
1041 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( )
1045 this->m_TextBuffer[ 0 ] = '\0';
1046 this->m_TextActor->SetInput( this->m_TextBuffer );
1047 this->m_TextActor->Modified( );
1051 // -------------------------------------------------------------------------
1052 void cpExtensions::Visualization::ImageSliceActors::
1053 UpdateText( double pos[ 3 ] )
1055 if( this->m_SliceMappers.size( ) > 0 )
1058 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1059 if ( axId == 0 ) axis = 'X';
1060 else if( axId == 1 ) axis = 'Y';
1061 else if( axId == 2 ) axis = 'Z';
1062 int slice = this->GetSliceNumber( );
1064 vtkImageData* image;
1065 if( this->m_ImageMaps[ 0 ] != NULL )
1067 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
1069 image = this->m_SliceMappers[ 0 ]->GetInput( );
1072 double pcoords[ 3 ];
1073 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1074 ijk[ axId ] = slice;
1077 image->GetExtent( ext );
1079 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
1080 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
1081 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
1084 int nScl = image->GetNumberOfScalarComponents( );
1085 std::stringstream str;
1089 << "," << ijk[ 2 ] << "]=(";
1091 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
1092 for( int n = 1; n < nScl; ++n )
1095 << image->GetScalarComponentAsFloat(
1096 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
1100 this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
1101 axis, slice, str.str( ).c_str( )
1107 this->m_TextBuffer[ 0 ] = '\0';
1108 this->m_TextActor->SetInput( this->m_TextBuffer );
1109 this->m_TextActor->Modified( );
1113 // -------------------------------------------------------------------------
1114 void cpExtensions::Visualization::ImageSliceActors::
1115 UpdateText( const double& w, const double& l )
1117 if( this->m_SliceMappers.size( ) > 0 )
1120 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1121 if ( axId == 0 ) axis = 'X';
1122 else if( axId == 1 ) axis = 'Y';
1123 else if( axId == 2 ) axis = 'Z';
1126 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1127 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1131 this->m_TextBuffer[ 0 ] = '\0';
1132 this->m_TextActor->SetInput( this->m_TextBuffer );
1133 this->m_TextActor->Modified( );
1137 // -------------------------------------------------------------------------
1138 void cpExtensions::Visualization::ImageSliceActors::
1139 Render( const double& t )
1141 if( this->m_Window != NULL )
1143 vtkRenderer* renderer =
1144 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1145 if( renderer != NULL )
1146 renderer->SetAllocatedRenderTime( t );
1147 this->m_Window->Render( );
1152 // -------------------------------------------------------------------------
1153 void cpExtensions::Visualization::ImageSliceActors::
1156 if( this->m_Window == NULL )
1158 vtkRenderer* renderer =
1159 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1160 if( renderer != NULL )
1161 renderer->ResetCamera( this->m_VisibleBounds );
1164 // -------------------------------------------------------------------------
1165 cpExtensions::Visualization::ImageSliceActors::
1169 m_Interpolate( false )
1172 this->_ConfigureStyle( );
1175 // -------------------------------------------------------------------------
1176 cpExtensions::Visualization::ImageSliceActors::
1177 ~ImageSliceActors( )
1181 // -------------------------------------------------------------------------
1182 void cpExtensions::Visualization::ImageSliceActors::
1185 // Connect this view with a controller
1186 this->m_Style = vtkSmartPointer< TStyle >::New( );
1187 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1188 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1189 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1190 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1191 this->m_Style->AddEnterCommand( Self::_EnterCommand, this );
1192 this->m_Style->AddLeaveCommand( Self::_LeaveCommand, this );
1195 // -------------------------------------------------------------------------
1196 void cpExtensions::Visualization::ImageSliceActors::
1197 _ConfigureNewLUT( vtkImageData* data )
1199 // Does the new LUT is a window-level one?
1200 unsigned int nImgs = this->m_ImageMaps.size( );
1201 unsigned int nCmps = data->GetNumberOfScalarComponents( );
1205 this->m_ImageMaps.push_back( NULL );
1210 // Create LUT filter
1211 this->m_ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1215 data->GetScalarRange( range );
1216 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1217 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1218 lut->SetScaleToLinear( );
1219 lut->SetTableRange( range );
1220 lut->SetWindow( range[ 1 ] - range[ 0 ] );
1221 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1223 this->m_ImageMaps[ 0 ]->SetLookupTable( lut );
1226 this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1229 // -------------------------------------------------------------------------
1230 void cpExtensions::Visualization::ImageSliceActors::
1231 _ConfigureNewInput( int axis )
1233 unsigned int nImages = this->m_ImageActors.size( );
1236 vtkImageSliceMapper* mapper = this->m_SliceMappers[ nImages ];
1238 mapper->SetOrientation( axis );
1240 mapper->SetOrientation( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
1244 vtkSmartPointer< vtkImageActor > actor =
1245 vtkSmartPointer< vtkImageActor >::New( );
1246 this->m_ImageActors.push_back( actor );
1247 actor->SetMapper( mapper );
1248 actor->SetInterpolate( this->m_Interpolate );
1252 this->m_Style->AssociateImageActor( actor );
1253 this->AddItem( actor );
1256 this->SetSliceNumber( this->GetSliceNumber( ) );
1258 this->SetSliceNumber( this->GetSliceNumberMaxValue( ) );
1259 this->ResetCursor( );
1263 // -------------------------------------------------------------------------
1264 void cpExtensions::Visualization::ImageSliceActors::
1266 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1267 bool alt, bool ctr, bool sft
1270 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1271 if( actors == NULL )
1274 if( btn == TStyle::ButtonID_None )
1276 // Just show the pixel information
1277 actors->SetCursor( pos );
1278 actors->UpdateText( pos );
1279 actors->Render( 1e-3 );
1281 else if( btn == TStyle::ButtonID_Left )
1283 if( !alt && ctr && !sft )
1285 // Interactively move slices
1286 auto i = actors->m_SlicesCommands.begin( );
1287 for( ; i != actors->m_SlicesCommands.end( ); ++i )
1288 i->first( pos, actors->GetAxis( ), i->second );
1289 actors->Render( 1e-3 );
1293 else if( btn == TStyle::ButtonID_Right )
1295 if( !alt && !ctr && sft )
1297 // Change image window level
1299 actors->m_SliceMappers[ 0 ]->GetBounds( bounds );
1301 int a0 = actors->GetAxis( );
1302 int a1 = ( a0 + 1 ) % 3;
1303 int a2 = ( a0 + 2 ) % 3;
1304 double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1305 double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1306 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1307 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1309 dx *= actors->m_StartWindowLevel[ 0 ];
1310 dy *= actors->m_StartWindowLevel[ 1 ];
1311 dx += actors->m_StartWindowLevel[ 0 ];
1312 dy += actors->m_StartWindowLevel[ 1 ];
1313 actors->SetWindowLevel( dx, dy );
1314 actors->Render( 1e-3 );
1316 // Associate objects
1317 auto i = actors->m_WindowLevelCommands.begin( );
1318 for( ; i != actors->m_WindowLevelCommands.end( ); ++i )
1319 i->first( dx, dy, i->second );
1326 // -------------------------------------------------------------------------
1327 void cpExtensions::Visualization::ImageSliceActors::
1329 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1330 bool alt, bool ctr, bool sft
1333 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1334 if( actors == NULL )
1337 actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1338 actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1339 actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1340 actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1341 actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1344 // -------------------------------------------------------------------------
1345 void cpExtensions::Visualization::ImageSliceActors::
1347 void* data, const int& dir,
1348 bool alt, bool ctr, bool sft
1351 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1352 if( actors == NULL )
1357 int slice = actors->GetSliceNumber( ) + ( dir * ( ( sft )? 10: 1 ) );
1358 if( slice < actors->GetSliceNumberMinValue( ) )
1359 slice = actors->GetSliceNumberMinValue( );
1360 if( slice > actors->GetSliceNumberMaxValue( ) )
1361 slice = actors->GetSliceNumberMaxValue( );
1362 actors->SetSliceNumber( slice );
1364 auto a = actors->m_AssociatedSlices.begin( );
1365 for( ; a != actors->m_AssociatedSlices.end( ); ++a )
1367 ( *a )->SetSliceNumber( slice );
1368 ( *a )->Render( 1e-3 );
1371 actors->Render( 1e-3 );
1373 // Associate objects
1374 auto i = actors->m_RenderCommands.begin( );
1375 for( ; i != actors->m_RenderCommands.end( ); ++i )
1376 i->first( i->second );
1381 // -------------------------------------------------------------------------
1382 void cpExtensions::Visualization::ImageSliceActors::
1383 _KeyCommand( void* data, const char& key )
1385 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1386 if( actors == NULL )
1393 actors->ResetCamera( );
1394 actors->Render( 1e-3 );
1396 // Associate objects
1397 auto i = actors->m_RenderCommands.begin( );
1398 for( ; i != actors->m_RenderCommands.end( ); ++i )
1399 i->first( i->second );
1404 actors->ResetWindowLevel( );
1405 actors->Render( 1e-3 );
1407 // Associate objects
1408 auto i = actors->m_RenderCommands.begin( );
1409 for( ; i != actors->m_RenderCommands.end( ); ++i )
1410 i->first( i->second );
1418 // -------------------------------------------------------------------------
1419 void cpExtensions::Visualization::ImageSliceActors::
1420 _EnterCommand( void* data )
1422 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1423 if( actors == NULL )
1426 actors->ResetCursor( );
1427 actors->m_CursorActor->VisibilityOn( );
1428 actors->Render( 1e-3 );
1431 // -------------------------------------------------------------------------
1432 void cpExtensions::Visualization::ImageSliceActors::
1433 _LeaveCommand( void* data )
1435 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1436 if( actors == NULL )
1439 actors->ResetCursor( );
1440 actors->m_CursorActor->VisibilityOff( );
1441 actors->Render( 1e-3 );