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 AddSlicesCommand( TSlicesCommand command, void* data )
36 this->m_SlicesCommands[ command ] = data;
42 // -------------------------------------------------------------------------
43 void cpExtensions::Visualization::ImageSliceActors::
44 AddWindowLevelCommand( TWindowLevelCommand command, void* data )
48 this->m_WindowLevelCommands[ command ] = data;
54 // -------------------------------------------------------------------------
55 void cpExtensions::Visualization::ImageSliceActors::
56 AddRenderCommand( TVoidCommand command, void* data )
60 this->m_RenderCommands[ command ] = data;
66 // -------------------------------------------------------------------------
67 void cpExtensions::Visualization::ImageSliceActors::
68 RemoveSlicesCommand( TSlicesCommand command )
70 auto i = this->m_SlicesCommands.find( command );
71 if( i != this->m_SlicesCommands.end( ) )
73 this->m_SlicesCommands.erase( i );
79 // -------------------------------------------------------------------------
80 void cpExtensions::Visualization::ImageSliceActors::
81 RemoveWindowLevelCommand( TWindowLevelCommand command )
83 auto i = this->m_WindowLevelCommands.find( command );
84 if( i != this->m_WindowLevelCommands.end( ) )
86 this->m_WindowLevelCommands.erase( i );
92 // -------------------------------------------------------------------------
93 void cpExtensions::Visualization::ImageSliceActors::
94 RemoveRenderCommand( TVoidCommand command )
96 auto i = this->m_RenderCommands.find( command );
97 if( i != this->m_RenderCommands.end( ) )
99 this->m_RenderCommands.erase( i );
105 // -------------------------------------------------------------------------
106 void cpExtensions::Visualization::ImageSliceActors::
107 AddInputConnection( vtkAlgorithmOutput* aout, int axis )
109 // Get input vtkImageData
112 vtkAlgorithm* producer = aout->GetProducer( );
113 vtkImageData* data = dynamic_cast< vtkImageData* >(
114 producer->GetOutputDataObject( aout->GetIndex( ) )
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 )
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 )
129 new_map->SetInputConnection( aout );
135 this->m_ImageMaps.push_back( new_map );
137 // Create mapper and actors
138 vtkSmartPointer< vtkImageSliceMapper > mapper =
139 vtkSmartPointer< vtkImageSliceMapper >::New( );
140 if( new_map != NULL )
141 mapper->SetInputConnection( new_map->GetOutputPort( ) );
143 mapper->SetInputConnection( aout );
144 this->m_SliceMappers.push_back( mapper );
145 this->_ConfigureNewInput( axis );
148 // -------------------------------------------------------------------------
149 void cpExtensions::Visualization::ImageSliceActors::
150 AddInputData( vtkImageData* data, int axis )
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 )
157 new_map->SetInputData( data );
162 // Create mapper and actors
163 vtkSmartPointer< vtkImageSliceMapper > mapper =
164 vtkSmartPointer< vtkImageSliceMapper >::New( );
165 if( new_map != NULL )
166 mapper->SetInputConnection( new_map->GetOutputPort( ) );
168 mapper->SetInputData( data );
169 this->m_SliceMappers.push_back( mapper );
170 this->_ConfigureNewInput( axis );
173 // -------------------------------------------------------------------------
174 void cpExtensions::Visualization::ImageSliceActors::
177 // Unbind from container
178 this->RemoveAllItems( );
181 this->m_ImageMaps.clear( );
182 this->m_SliceMappers.clear( );
183 this->m_ImageActors.clear( );
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_Plane = vtkSmartPointer< vtkPolyData >::New( );
190 this->m_PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
191 this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
192 this->m_PlaneActor = vtkSmartPointer< vtkActor >::New( );
193 this->m_TextBuffer[ 0 ] = '\0';
195 // Unique objects configuration
196 vtkSmartPointer< vtkPoints > cursor_points =
197 vtkSmartPointer< vtkPoints >::New( );
198 vtkSmartPointer< vtkCellArray > cursor_lines =
199 vtkSmartPointer< vtkCellArray >::New( );
200 cursor_points->InsertNextPoint( 0, 0, 0 );
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_lines->InsertNextCell( 2 );
209 cursor_lines->InsertCellPoint( 0 );
210 cursor_lines->InsertCellPoint( 1 );
211 cursor_lines->InsertNextCell( 2 );
212 cursor_lines->InsertCellPoint( 2 );
213 cursor_lines->InsertCellPoint( 3 );
214 cursor_lines->InsertNextCell( 2 );
215 cursor_lines->InsertCellPoint( 4 );
216 cursor_lines->InsertCellPoint( 5 );
217 cursor_lines->InsertNextCell( 2 );
218 cursor_lines->InsertCellPoint( 6 );
219 cursor_lines->InsertCellPoint( 7 );
220 this->m_Cursor->SetPoints( cursor_points );
221 this->m_Cursor->SetLines( cursor_lines );
222 this->m_CursorMapper->SetInputData( this->m_Cursor );
223 this->m_CursorActor->SetMapper( this->m_CursorMapper );
225 vtkSmartPointer< vtkPoints > plane_points =
226 vtkSmartPointer< vtkPoints >::New( );
227 vtkSmartPointer< vtkCellArray > plane_lines =
228 vtkSmartPointer< vtkCellArray >::New( );
230 plane_points->InsertNextPoint( 0, 0, 0 );
231 plane_points->InsertNextPoint( 0, 1, 0 );
232 plane_points->InsertNextPoint( 1, 1, 0 );
233 plane_points->InsertNextPoint( 1, 0, 0 );
234 plane_lines->InsertNextCell( 5 );
235 plane_lines->InsertCellPoint( 0 );
236 plane_lines->InsertCellPoint( 1 );
237 plane_lines->InsertCellPoint( 2 );
238 plane_lines->InsertCellPoint( 3 );
239 plane_lines->InsertCellPoint( 0 );
240 this->m_Plane->SetPoints( plane_points );
241 this->m_Plane->SetLines( plane_lines );
243 this->m_PlaneMapper->SetInputData( this->m_Plane );
244 this->m_PlaneActor->SetMapper( this->m_PlaneMapper );
246 this->m_TextActor->SetTextScaleModeToNone( );
247 vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
248 textprop->SetColor( 1, 1, 1 );
249 textprop->SetFontFamilyToCourier( );
250 textprop->SetFontSize( 18 );
251 textprop->BoldOff( );
252 textprop->ItalicOff( );
253 textprop->ShadowOff( );
254 textprop->SetJustificationToLeft( );
255 textprop->SetVerticalJustificationToBottom( );
256 vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
257 coord->SetCoordinateSystemToNormalizedViewport( );
258 coord->SetValue( 0.01, 0.01 );
260 // Update actor collection
261 this->AddItem( this->m_CursorActor );
262 this->AddItem( this->m_TextActor );
263 this->AddItem( this->m_PlaneActor );
266 // -------------------------------------------------------------------------
267 void cpExtensions::Visualization::ImageSliceActors::
268 AssociateSlice( Self* slice )
270 this->m_AssociatedSlices.push_back( slice );
274 // -------------------------------------------------------------------------
275 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
278 return( this->m_Style.GetPointer( ) );
281 // -------------------------------------------------------------------------
282 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
285 return( this->m_Style.GetPointer( ) );
288 // -------------------------------------------------------------------------
289 vtkImageData* cpExtensions::Visualization::ImageSliceActors::
290 GetInputImage( unsigned int id )
292 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
293 vtkImageData* image = dynamic_cast< vtkImageData* >(
294 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
299 // -------------------------------------------------------------------------
300 const vtkImageData* cpExtensions::Visualization::ImageSliceActors::
301 GetInputImage( unsigned int id ) const
303 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
304 const vtkImageData* image = dynamic_cast< const vtkImageData* >(
305 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
310 // -------------------------------------------------------------------------
311 void cpExtensions::Visualization::ImageSliceActors::
312 PushActorsInto( vtkRenderWindow* window, bool force_style )
314 this->m_Window = window;
317 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
318 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
319 if( rwi == NULL || renderer == NULL )
323 if( this->m_Style.GetPointer( ) != NULL && force_style )
324 rwi->SetInteractorStyle( this->m_Style );
328 this->InitTraversal( );
329 while( prop = this->GetNextProp( ) )
330 renderer->AddViewProp( prop );
331 renderer->Modified( );
334 renderer->RemoveViewProp( this->m_CursorActor );
335 renderer->RemoveViewProp( this->m_TextActor );
340 vtkCamera* camera = renderer->GetActiveCamera( );
341 if( camera != NULL && force_style )
343 // Parallel projections are better when displaying 2D images
344 int axis = this->GetAxis( );
345 camera->ParallelProjectionOn( );
346 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
349 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
350 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
354 camera->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
355 camera->SetViewUp ( double( 0 ), double( 0 ), double( -1 ) );
357 else // if( axis == 2 )
359 camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
360 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
365 renderer->ResetCamera( );
369 // -------------------------------------------------------------------------
370 void cpExtensions::Visualization::ImageSliceActors::
371 PopActorsFrom( vtkRenderWindow* window )
373 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
374 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
376 if( renderer != NULL )
380 this->InitTraversal( );
381 while( prop = this->GetNextProp( ) )
382 renderer->RemoveViewProp( prop );
383 renderer->Modified( );
390 // -------------------------------------------------------------------------
391 unsigned int cpExtensions::Visualization::ImageSliceActors::
392 GetNumberOfImageActors( ) const
394 return( this->m_ImageActors.size( ) );
397 // -------------------------------------------------------------------------
398 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
399 GetImageActor( unsigned int id )
401 if( id < this->m_ImageActors.size( ) )
402 return( this->m_ImageActors[ id ] );
407 // -------------------------------------------------------------------------
408 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
409 GetImageActor( unsigned int id ) const
411 if( id < this->m_ImageActors.size( ) )
412 return( this->m_ImageActors[ id ] );
417 // -------------------------------------------------------------------------
418 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
421 return( this->m_TextActor );
424 // -------------------------------------------------------------------------
425 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
426 GetTextActor( ) const
428 return( this->m_TextActor );
431 // -------------------------------------------------------------------------
432 vtkActor* cpExtensions::Visualization::ImageSliceActors::
435 return( this->m_PlaneActor );
438 // -------------------------------------------------------------------------
439 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
440 GetPlaneActor( ) const
442 return( this->m_PlaneActor );
445 // -------------------------------------------------------------------------
446 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
449 return( this->m_SliceMappers[ 0 ]->GetSlicePlane( ) );
452 // -------------------------------------------------------------------------
453 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
454 GetPlaneFunction( ) const
456 return( this->m_SliceMappers[ 0 ]->GetSlicePlane( ) );
459 // -------------------------------------------------------------------------
460 void cpExtensions::Visualization::ImageSliceActors::
461 SetInterpolate( bool v )
463 if( this->m_Interpolate != v )
465 for( unsigned int i = 0; i < this->m_ImageActors.size( ); ++i )
466 this->m_ImageActors[ i ]->SetInterpolate( v );
467 this->m_Interpolate = v;
473 // -------------------------------------------------------------------------
474 void cpExtensions::Visualization::ImageSliceActors::
477 this->SetInterpolate( true );
480 // -------------------------------------------------------------------------
481 void cpExtensions::Visualization::ImageSliceActors::
484 this->SetInterpolate( false );
487 // -------------------------------------------------------------------------
488 double* cpExtensions::Visualization::ImageSliceActors::
489 GetDisplayBounds( ) const
491 if( this->m_ImageActors.size( ) > 0 )
492 return( this->m_ImageActors[ 0 ]->GetDisplayBounds( ) );
497 // -------------------------------------------------------------------------
498 void cpExtensions::Visualization::ImageSliceActors::
499 GetDisplayBounds( double bounds[ 6 ] ) const
501 if( this->m_ImageActors.size( ) == 0 )
503 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
504 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
507 this->m_ImageActors[ 0 ]->GetDisplayBounds( bounds );
510 // -------------------------------------------------------------------------
511 void cpExtensions::Visualization::ImageSliceActors::
514 vtkPoints* points = this->m_Cursor->GetPoints( );
515 points->SetPoint( 0, 0, 0, 0 );
516 points->SetPoint( 1, 0, 0, 0 );
517 points->SetPoint( 2, 0, 0, 0 );
518 points->SetPoint( 3, 0, 0, 0 );
519 points->SetPoint( 4, 0, 0, 0 );
520 points->SetPoint( 5, 0, 0, 0 );
521 points->SetPoint( 6, 0, 0, 0 );
522 points->SetPoint( 7, 0, 0, 0 );
523 this->m_Cursor->Modified( );
524 this->m_CursorMapper->Modified( );
525 this->m_CursorActor->Modified( );
528 // -------------------------------------------------------------------------
529 void cpExtensions::Visualization::ImageSliceActors::
530 SetCursor( double pos[ 3 ] )
532 if( this->m_SliceMappers.size( ) == 0 )
536 int a0 = this->GetAxis( );
537 int a1 = ( a0 + 1 ) % 3;
538 int a2 = ( a0 + 2 ) % 3;
544 this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
547 p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
548 p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
550 p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
551 p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
552 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
553 p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
554 p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
555 p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
556 p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
557 p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
559 vtkPoints* points = this->m_Cursor->GetPoints( );
560 points->SetPoint( 0, p0 );
561 points->SetPoint( 1, p1 );
562 points->SetPoint( 2, p2 );
563 points->SetPoint( 3, p3 );
564 points->SetPoint( 4, p4 );
565 points->SetPoint( 5, p5 );
566 points->SetPoint( 6, p6 );
567 points->SetPoint( 7, p7 );
568 this->m_Cursor->Modified( );
569 this->m_CursorMapper->Modified( );
570 this->m_CursorActor->Modified( );
573 // -------------------------------------------------------------------------
574 vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
575 GetImageMap( unsigned int id )
577 if( id < this->m_ImageMaps.size( ) )
578 return( this->m_ImageMaps[ id ].GetPointer( ) );
583 // -------------------------------------------------------------------------
584 const vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
585 GetImageMap( unsigned int id ) const
587 if( id < this->m_ImageMaps.size( ) )
588 return( this->m_ImageMaps[ id ].GetPointer( ) );
593 // -------------------------------------------------------------------------
594 double cpExtensions::Visualization::ImageSliceActors::
595 GetMinWindow( ) const
597 return( this->m_MinWindow );
600 // -------------------------------------------------------------------------
601 double cpExtensions::Visualization::ImageSliceActors::
602 GetMaxWindow( ) const
604 return( this->m_MaxWindow );
607 // -------------------------------------------------------------------------
608 double cpExtensions::Visualization::ImageSliceActors::
611 return( this->m_MinLevel );
614 // -------------------------------------------------------------------------
615 double cpExtensions::Visualization::ImageSliceActors::
618 return( this->m_MaxLevel );
621 // -------------------------------------------------------------------------
622 double cpExtensions::Visualization::ImageSliceActors::
625 if( this->m_ImageMaps.size( ) == 0 )
626 return( double( 0 ) );
627 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
628 return( double( 0 ) );
630 vtkWindowLevelLookupTable* lut =
631 dynamic_cast< vtkWindowLevelLookupTable* >(
632 this->m_ImageMaps[ 0 ]->GetLookupTable( )
635 return( lut->GetWindow( ) );
637 return( double( 0 ) );
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->GetLevel( ) );
656 return( double( 0 ) );
659 // -------------------------------------------------------------------------
660 void cpExtensions::Visualization::ImageSliceActors::
661 SetWindow( double w )
663 if( this->m_ImageMaps.size( ) == 0 )
665 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
667 vtkWindowLevelLookupTable* lut =
668 dynamic_cast< vtkWindowLevelLookupTable* >(
669 this->m_ImageMaps[ 0 ]->GetLookupTable( )
674 /* TODO: Min and Max values are assigned 0!!!
675 if( w < this->MinWindow )
677 if( w > this->MaxWindow )
682 this->m_ImageMaps[ 0 ]->Modified( );
686 // -------------------------------------------------------------------------
687 void cpExtensions::Visualization::ImageSliceActors::
690 if( this->m_ImageMaps.size( ) == 0 )
692 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
694 vtkWindowLevelLookupTable* lut =
695 dynamic_cast< vtkWindowLevelLookupTable* >(
696 this->m_ImageMaps[ 0 ]->GetLookupTable( )
701 /* TODO: Min and Max values are assigned 0!!!
702 if( l < this->MinLevel )
704 if( l > this->MaxLevel )
709 this->m_ImageMaps[ 0 ]->Modified( );
713 // -------------------------------------------------------------------------
714 void cpExtensions::Visualization::ImageSliceActors::
715 SetWindowLevel( double w, double l )
717 if( this->m_ImageMaps.size( ) == 0 )
719 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
721 vtkWindowLevelLookupTable* lut =
722 dynamic_cast< vtkWindowLevelLookupTable* >(
723 this->m_ImageMaps[ 0 ]->GetLookupTable( )
728 /* TODO: Min and Max values are assigned 0!!!
729 if( w < this->MinWindow )
731 if( w > this->MaxWindow )
733 if( l < this->MinLevel )
735 if( l > this->MaxLevel )
741 this->m_ImageMaps[ 0 ]->Modified( );
742 this->UpdateText( w, l );
746 // -------------------------------------------------------------------------
747 void cpExtensions::Visualization::ImageSliceActors::
750 static const double _2 = double( 2 );
751 this->SetWindowLevel(
752 this->m_MaxWindow, ( this->m_MaxLevel + this->m_MinLevel ) / _2
756 // -------------------------------------------------------------------------
757 void cpExtensions::Visualization::ImageSliceActors::
758 SetLookupTable( unsigned int id, vtkLookupTable* lut )
760 if( id < this->m_ImageMaps.size( ) && id > 0 )
762 if( this->m_ImageMaps[ id ].GetPointer( ) != NULL )
764 this->m_ImageMaps[ id ]->SetLookupTable( lut );
765 this->m_ImageMaps[ id ]->Modified( );
773 // -------------------------------------------------------------------------
774 void cpExtensions::Visualization::ImageSliceActors::
775 SetLookupTableAsColor( unsigned int id, double r, double g, double b )
777 static const double _0 = double( 0 );
778 static const double _2 = double( 2 );
779 static const double _4 = double( 4 );
780 static const double _6 = double( 6 );
781 static const double _OPACITY = double( 0.6 );
783 // Check ID consistency
784 if( id == 0 || id >= this->m_ImageMaps.size( ) )
787 // Get image scalar range
788 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
789 vtkImageData* image = dynamic_cast< vtkImageData* >(
790 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
793 image->GetScalarRange( range );
795 // Get HSV from display color
796 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
797 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
798 double d = cmax - cmin;
800 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
806 hue = std::fmod( ( g - b ) / d, _6 );
808 hue = ( ( b - r ) / d ) + _2;
810 hue = ( ( r - g ) / d ) + _4;
815 // Define new lookup table
816 vtkSmartPointer< vtkLookupTable > lut =
817 vtkSmartPointer< vtkLookupTable >::New( );
818 lut->SetScaleToLinear( );
819 lut->SetNanColor( _0, _0, _0, _0 );
820 lut->SetTableRange( range[ 0 ], range[ 1 ] );
821 lut->SetAlphaRange( _0, _OPACITY );
822 lut->SetHueRange( _0, hue );
823 lut->SetSaturationRange( _0, saturation );
824 lut->SetValueRange( _0, value );
826 this->SetLookupTable( id, lut );
829 // -------------------------------------------------------------------------
830 int cpExtensions::Visualization::ImageSliceActors::
833 if( this->m_SliceMappers.size( ) > 0 )
834 return( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
839 // -------------------------------------------------------------------------
840 int cpExtensions::Visualization::ImageSliceActors::
841 GetSliceNumber( ) const
843 if( this->m_SliceMappers.size( ) > 0 )
844 return( this->m_SliceMappers[ 0 ]->GetSliceNumber( ) );
849 // -------------------------------------------------------------------------
850 int cpExtensions::Visualization::ImageSliceActors::
851 GetSliceNumberMinValue( ) const
853 if( this->m_SliceMappers.size( ) > 0 )
854 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
859 // -------------------------------------------------------------------------
860 int cpExtensions::Visualization::ImageSliceActors::
861 GetSliceNumberMaxValue( ) const
863 if( this->m_SliceMappers.size( ) > 0 )
864 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
869 // -------------------------------------------------------------------------
870 void cpExtensions::Visualization::ImageSliceActors::
871 SetSliceNumber( const int& slice )
873 unsigned int nImages = this->m_SliceMappers.size( );
877 // Change visualization extent
878 for( unsigned int i = 0; i < nImages; ++i )
880 this->m_SliceMappers[ i ]->SetSliceNumber( slice );
881 this->m_SliceMappers[ i ]->Modified( );
882 this->m_ImageActors[ i ]->Modified( );
883 this->m_SliceMappers[ i ]->Update( );
888 this->m_SliceMappers[ 0 ]->GetBounds( bnds );
891 { bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] },
892 { bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] }
896 vtkPlane* plane = this->m_SliceMappers[ 0 ]->GetSlicePlane( );
897 plane->GeneralizedProjectPoint( x0[ 0 ], p0[ 0 ] );
898 plane->GeneralizedProjectPoint( x0[ 1 ], p0[ 1 ] );
900 int axis = this->GetAxis( );
901 bnds[ 0 ] = p0[ 0 ][ 0 ];
902 bnds[ 1 ] = p0[ 1 ][ 0 ];
903 bnds[ 2 ] = p0[ 0 ][ 1 ];
904 bnds[ 3 ] = p0[ 1 ][ 1 ];
905 bnds[ 4 ] = p0[ 0 ][ 2 ];
906 bnds[ 5 ] = p0[ 1 ][ 2 ];
908 // Configure visualization and implicit plane orientation
909 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
910 this->m_PlaneActor->GetProperty( )->SetLineWidth( 3 );
911 vtkPoints* plane_points = this->m_Plane->GetPoints( );
912 plane_points->SetPoint( 0, bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] );
913 plane_points->SetPoint( 2, bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] );
914 if( axis == 0 || axis == 2 ) // YZ, x-normal
916 plane_points->SetPoint( 1, bnds[ 1 ], bnds[ 2 ], bnds[ 5 ] );
917 plane_points->SetPoint( 3, bnds[ 0 ], bnds[ 3 ], bnds[ 4 ] );
919 this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
921 this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
923 else if( axis == 1 ) // ZX, y-normal
925 plane_points->SetPoint( 1, bnds[ 0 ], bnds[ 2 ], bnds[ 5 ] );
926 plane_points->SetPoint( 3, bnds[ 1 ], bnds[ 3 ], bnds[ 4 ] );
927 this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
930 this->m_Plane->Modified( );
931 this->m_PlaneMapper->Modified( );
932 this->m_PlaneActor->Modified( );
938 // -------------------------------------------------------------------------
939 void cpExtensions::Visualization::ImageSliceActors::
940 SetSlice( double* pos )
943 if( this->m_ImageMaps[ 0 ] != NULL )
945 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
947 image = this->m_SliceMappers[ 0 ]->GetInput( );
951 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
952 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
955 // -------------------------------------------------------------------------
956 void cpExtensions::Visualization::ImageSliceActors::
959 if( this->m_SliceMappers.size( ) > 0 )
962 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
963 if ( axId == 0 ) axis = 'X';
964 else if( axId == 1 ) axis = 'Y';
965 else if( axId == 2 ) axis = 'Z';
968 this->m_TextBuffer, "Axis: %c (%d)",
969 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( )
973 this->m_TextBuffer[ 0 ] = '\0';
974 this->m_TextActor->SetInput( this->m_TextBuffer );
975 this->m_TextActor->Modified( );
979 // -------------------------------------------------------------------------
980 void cpExtensions::Visualization::ImageSliceActors::
981 UpdateText( double pos[ 3 ] )
983 if( this->m_SliceMappers.size( ) > 0 )
986 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
987 if ( axId == 0 ) axis = 'X';
988 else if( axId == 1 ) axis = 'Y';
989 else if( axId == 2 ) axis = 'Z';
990 int slice = this->GetSliceNumber( );
993 if( this->m_ImageMaps[ 0 ] != NULL )
995 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
997 image = this->m_SliceMappers[ 0 ]->GetInput( );
1000 double pcoords[ 3 ];
1001 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1002 ijk[ axId ] = slice;
1005 image->GetExtent( ext );
1007 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
1008 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
1009 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
1012 int nScl = image->GetNumberOfScalarComponents( );
1013 std::stringstream str;
1017 << "," << ijk[ 2 ] << "]=(";
1019 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
1020 for( int n = 1; n < nScl; ++n )
1023 << image->GetScalarComponentAsFloat(
1024 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
1028 this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
1029 axis, slice, str.str( ).c_str( )
1035 this->m_TextBuffer[ 0 ] = '\0';
1036 this->m_TextActor->SetInput( this->m_TextBuffer );
1037 this->m_TextActor->Modified( );
1041 // -------------------------------------------------------------------------
1042 void cpExtensions::Visualization::ImageSliceActors::
1043 UpdateText( const double& w, const double& l )
1045 if( this->m_SliceMappers.size( ) > 0 )
1048 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1049 if ( axId == 0 ) axis = 'X';
1050 else if( axId == 1 ) axis = 'Y';
1051 else if( axId == 2 ) axis = 'Z';
1054 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1055 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1059 this->m_TextBuffer[ 0 ] = '\0';
1060 this->m_TextActor->SetInput( this->m_TextBuffer );
1061 this->m_TextActor->Modified( );
1065 // -------------------------------------------------------------------------
1066 void cpExtensions::Visualization::ImageSliceActors::
1069 if( this->m_Window != NULL )
1070 this->m_Window->Render( );
1073 // -------------------------------------------------------------------------
1074 void cpExtensions::Visualization::ImageSliceActors::
1077 if( this->m_Window == NULL )
1079 vtkRenderer* renderer =
1080 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1081 if( renderer != NULL )
1082 renderer->ResetCamera( );
1085 // -------------------------------------------------------------------------
1086 cpExtensions::Visualization::ImageSliceActors::
1090 m_Interpolate( false )
1093 this->_ConfigureStyle( );
1096 // -------------------------------------------------------------------------
1097 cpExtensions::Visualization::ImageSliceActors::
1098 ~ImageSliceActors( )
1102 // -------------------------------------------------------------------------
1103 void cpExtensions::Visualization::ImageSliceActors::
1106 // Connect this view with a controller
1107 this->m_Style = vtkSmartPointer< TStyle >::New( );
1108 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1109 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1110 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1111 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1114 // -------------------------------------------------------------------------
1115 void cpExtensions::Visualization::ImageSliceActors::
1116 _ConfigureNewLUT( vtkImageData* data )
1118 // Does the new LUT is a window-level one?
1119 unsigned int nImgs = this->m_ImageMaps.size( );
1120 unsigned int nCmps = data->GetNumberOfScalarComponents( );
1124 this->m_ImageMaps.push_back( NULL );
1129 // Create LUT filter
1130 this->m_ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1134 data->GetScalarRange( range );
1135 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1136 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1137 lut->SetScaleToLinear( );
1138 lut->SetTableRange( range );
1139 lut->SetWindow( range[ 1 ] - range[ 0 ] );
1140 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1142 this->m_ImageMaps[ 0 ]->SetLookupTable( lut );
1144 this->m_MinWindow = double( 0 );
1145 this->m_MaxWindow = range[ 1 ] - range[ 0 ];
1146 this->m_MinLevel = range[ 0 ];
1147 this->m_MaxLevel = range[ 1 ];
1150 this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1153 // -------------------------------------------------------------------------
1154 void cpExtensions::Visualization::ImageSliceActors::
1155 _ConfigureNewInput( int axis )
1157 unsigned int nImages = this->m_ImageActors.size( );
1160 vtkImageSliceMapper* mapper = this->m_SliceMappers[ nImages ];
1162 mapper->SetOrientation( axis );
1164 mapper->SetOrientation( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
1168 vtkSmartPointer< vtkImageActor > actor =
1169 vtkSmartPointer< vtkImageActor >::New( );
1170 this->m_ImageActors.push_back( actor );
1171 actor->SetMapper( mapper );
1172 actor->SetInterpolate( this->m_Interpolate );
1176 this->m_Style->AssociateImageActor( actor );
1177 this->AddItem( actor );
1180 this->SetSliceNumber( this->GetSliceNumber( ) );
1182 this->SetSliceNumber( this->GetSliceNumberMaxValue( ) );
1186 // -------------------------------------------------------------------------
1187 void cpExtensions::Visualization::ImageSliceActors::
1189 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1190 bool alt, bool ctr, bool sft
1193 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1194 if( actors == NULL )
1197 if( btn == TStyle::ButtonID_None )
1199 // Just show the pixel information
1200 actors->SetCursor( pos );
1201 actors->UpdateText( pos );
1204 else if( btn == TStyle::ButtonID_Left )
1207 unsigned int nC = actors->m_SlicesCommands.size( );
1208 if( !alt && ctr && !sft && nC > 0 )
1210 for( unsigned int i = 0; i < nC; ++i )
1211 actors->m_SlicesCommands[ i ].first(
1212 pos, actors->GetAxis( ), actors->m_SlicesCommands[ i ].second
1219 else if( btn == TStyle::ButtonID_Right )
1221 if( !alt && !ctr && sft )
1223 // Change image window level
1225 actors->m_SliceMappers[ 0 ]->GetBounds( bounds );
1227 int a0 = actors->GetAxis( );
1228 int a1 = ( a0 + 1 ) % 3;
1229 int a2 = ( a0 + 2 ) % 3;
1230 double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1231 double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1232 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1233 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1235 dx *= actors->m_StartWindowLevel[ 0 ];
1236 dy *= actors->m_StartWindowLevel[ 1 ];
1237 dx += actors->m_StartWindowLevel[ 0 ];
1238 dy += actors->m_StartWindowLevel[ 1 ];
1239 actors->SetWindowLevel( dx, dy );
1242 // Associate objects
1243 auto i = actors->m_WindowLevelCommands.begin( );
1244 for( ; i != actors->m_WindowLevelCommands.end( ); ++i )
1245 i->first( dx, dy, i->second );
1252 // -------------------------------------------------------------------------
1253 void cpExtensions::Visualization::ImageSliceActors::
1255 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1256 bool alt, bool ctr, bool sft
1259 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1260 if( actors == NULL )
1263 actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1264 actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1265 actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1266 actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1267 actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1270 // -------------------------------------------------------------------------
1271 void cpExtensions::Visualization::ImageSliceActors::
1273 void* data, const int& dir,
1274 bool alt, bool ctr, bool sft
1277 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1278 if( actors == NULL )
1283 int slice = actors->GetSliceNumber( ) + ( dir * ( ( sft )? 10: 1 ) );
1284 if( slice < actors->GetSliceNumberMinValue( ) )
1285 slice = actors->GetSliceNumberMinValue( );
1286 if( slice > actors->GetSliceNumberMaxValue( ) )
1287 slice = actors->GetSliceNumberMaxValue( );
1288 actors->SetSliceNumber( slice );
1290 auto a = actors->m_AssociatedSlices.begin( );
1291 for( ; a != actors->m_AssociatedSlices.end( ); ++a )
1293 ( *a )->SetSliceNumber( slice );
1299 // Associate objects
1300 auto i = actors->m_RenderCommands.begin( );
1301 for( ; i != actors->m_RenderCommands.end( ); ++i )
1302 i->first( i->second );
1307 // -------------------------------------------------------------------------
1308 void cpExtensions::Visualization::ImageSliceActors::
1309 _KeyCommand( void* data, const char& key )
1311 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1312 if( actors == NULL )
1319 actors->ResetCamera( );
1322 // Associate objects
1323 auto i = actors->m_RenderCommands.begin( );
1324 for( ; i != actors->m_RenderCommands.end( ); ++i )
1325 i->first( i->second );
1330 actors->ResetWindowLevel( );
1333 // Associate objects
1334 auto i = actors->m_RenderCommands.begin( );
1335 for( ; i != actors->m_RenderCommands.end( ); ++i )
1336 i->first( i->second );