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 // Update window level values
138 if( new_map != NULL )
141 dynamic_cast< vtkImageData* >( new_map->GetInput( ) )->
142 GetScalarRange( range );
143 this->m_MinWindow = double( 0 );
144 this->m_MaxWindow = range[ 1 ] - range[ 0 ];
145 this->m_MinLevel = range[ 0 ];
146 this->m_MaxLevel = range[ 1 ];
150 // Create mapper and actors
151 vtkSmartPointer< vtkImageSliceMapper > mapper =
152 vtkSmartPointer< vtkImageSliceMapper >::New( );
153 if( new_map != NULL )
154 mapper->SetInputConnection( new_map->GetOutputPort( ) );
156 mapper->SetInputConnection( aout );
157 this->m_SliceMappers.push_back( mapper );
158 this->_ConfigureNewInput( axis );
161 // -------------------------------------------------------------------------
162 void cpExtensions::Visualization::ImageSliceActors::
163 AddInputData( vtkImageData* data, int axis )
165 // Update window level values
166 if( this->m_ImageMaps.size( ) == 0 )
169 data->GetScalarRange( range );
170 this->m_MinWindow = double( 0 );
171 this->m_MaxWindow = range[ 1 ] - range[ 0 ];
172 this->m_MinLevel = range[ 0 ];
173 this->m_MaxLevel = range[ 1 ];
177 // Configure LUT, if possible (NULL is returned if not)
178 this->_ConfigureNewLUT( data );
179 vtkImageMapToColors* new_map = *( this->m_ImageMaps.rbegin( ) );
180 if( new_map != NULL )
182 new_map->SetInputData( data );
187 // Create mapper and actors
188 vtkSmartPointer< vtkImageSliceMapper > mapper =
189 vtkSmartPointer< vtkImageSliceMapper >::New( );
190 if( new_map != NULL )
191 mapper->SetInputConnection( new_map->GetOutputPort( ) );
193 mapper->SetInputData( data );
194 this->m_SliceMappers.push_back( mapper );
195 this->_ConfigureNewInput( axis );
198 // -------------------------------------------------------------------------
199 void cpExtensions::Visualization::ImageSliceActors::
203 this->m_MinWindow = double( 0 );
204 this->m_MaxWindow = double( 0 );
205 this->m_MinLevel = double( 0 );
206 this->m_MaxLevel = double( 0 );
208 // Unbind from container
209 this->RemoveAllItems( );
212 this->m_ImageMaps.clear( );
213 this->m_SliceMappers.clear( );
214 this->m_ImageActors.clear( );
216 // Reconfigure unique objects
217 this->m_Cursor = vtkSmartPointer< vtkPolyData >::New( );
218 this->m_CursorMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
219 this->m_CursorActor = vtkSmartPointer< vtkActor >::New( );
220 this->m_Plane = vtkSmartPointer< vtkPolyData >::New( );
221 this->m_PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
222 this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
223 this->m_PlaneActor = vtkSmartPointer< vtkActor >::New( );
224 this->m_TextBuffer[ 0 ] = '\0';
226 // Unique objects configuration
227 vtkSmartPointer< vtkPoints > cursor_points =
228 vtkSmartPointer< vtkPoints >::New( );
229 vtkSmartPointer< vtkCellArray > cursor_lines =
230 vtkSmartPointer< vtkCellArray >::New( );
231 cursor_points->InsertNextPoint( 0, 0, 0 );
232 cursor_points->InsertNextPoint( 0, 0, 0 );
233 cursor_points->InsertNextPoint( 0, 0, 0 );
234 cursor_points->InsertNextPoint( 0, 0, 0 );
235 cursor_points->InsertNextPoint( 0, 0, 0 );
236 cursor_points->InsertNextPoint( 0, 0, 0 );
237 cursor_points->InsertNextPoint( 0, 0, 0 );
238 cursor_points->InsertNextPoint( 0, 0, 0 );
239 cursor_lines->InsertNextCell( 2 );
240 cursor_lines->InsertCellPoint( 0 );
241 cursor_lines->InsertCellPoint( 1 );
242 cursor_lines->InsertNextCell( 2 );
243 cursor_lines->InsertCellPoint( 2 );
244 cursor_lines->InsertCellPoint( 3 );
245 cursor_lines->InsertNextCell( 2 );
246 cursor_lines->InsertCellPoint( 4 );
247 cursor_lines->InsertCellPoint( 5 );
248 cursor_lines->InsertNextCell( 2 );
249 cursor_lines->InsertCellPoint( 6 );
250 cursor_lines->InsertCellPoint( 7 );
251 this->m_Cursor->SetPoints( cursor_points );
252 this->m_Cursor->SetLines( cursor_lines );
253 this->m_CursorMapper->SetInputData( this->m_Cursor );
254 this->m_CursorActor->SetMapper( this->m_CursorMapper );
256 vtkSmartPointer< vtkPoints > plane_points =
257 vtkSmartPointer< vtkPoints >::New( );
258 vtkSmartPointer< vtkCellArray > plane_lines =
259 vtkSmartPointer< vtkCellArray >::New( );
261 plane_points->InsertNextPoint( 0, 0, 0 );
262 plane_points->InsertNextPoint( 0, 1, 0 );
263 plane_points->InsertNextPoint( 1, 1, 0 );
264 plane_points->InsertNextPoint( 1, 0, 0 );
265 plane_lines->InsertNextCell( 5 );
266 plane_lines->InsertCellPoint( 0 );
267 plane_lines->InsertCellPoint( 1 );
268 plane_lines->InsertCellPoint( 2 );
269 plane_lines->InsertCellPoint( 3 );
270 plane_lines->InsertCellPoint( 0 );
271 this->m_Plane->SetPoints( plane_points );
272 this->m_Plane->SetLines( plane_lines );
274 this->m_PlaneMapper->SetInputData( this->m_Plane );
275 this->m_PlaneActor->SetMapper( this->m_PlaneMapper );
277 this->m_TextActor->SetTextScaleModeToNone( );
278 vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
279 textprop->SetColor( 1, 1, 1 );
280 textprop->SetFontFamilyToCourier( );
281 textprop->SetFontSize( 18 );
282 textprop->BoldOff( );
283 textprop->ItalicOff( );
284 textprop->ShadowOff( );
285 textprop->SetJustificationToLeft( );
286 textprop->SetVerticalJustificationToBottom( );
287 vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
288 coord->SetCoordinateSystemToNormalizedViewport( );
289 coord->SetValue( 0.01, 0.01 );
291 // Update actor collection
292 this->AddItem( this->m_CursorActor );
293 this->AddItem( this->m_TextActor );
294 this->AddItem( this->m_PlaneActor );
297 // -------------------------------------------------------------------------
298 void cpExtensions::Visualization::ImageSliceActors::
299 AssociateSlice( Self* slice )
301 this->m_AssociatedSlices.push_back( slice );
305 // -------------------------------------------------------------------------
306 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
309 return( this->m_Style.GetPointer( ) );
312 // -------------------------------------------------------------------------
313 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
316 return( this->m_Style.GetPointer( ) );
319 // -------------------------------------------------------------------------
320 vtkImageData* cpExtensions::Visualization::ImageSliceActors::
321 GetInputImage( unsigned int id )
323 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
324 vtkImageData* image = dynamic_cast< vtkImageData* >(
325 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
330 // -------------------------------------------------------------------------
331 const vtkImageData* cpExtensions::Visualization::ImageSliceActors::
332 GetInputImage( unsigned int id ) const
334 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
335 const vtkImageData* image = dynamic_cast< const vtkImageData* >(
336 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
341 // -------------------------------------------------------------------------
342 void cpExtensions::Visualization::ImageSliceActors::
343 PushActorsInto( vtkRenderWindow* window, bool force_style )
345 this->m_Window = window;
348 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
349 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
350 if( rwi == NULL || renderer == NULL )
354 if( this->m_Style.GetPointer( ) != NULL && force_style )
355 rwi->SetInteractorStyle( this->m_Style );
359 this->InitTraversal( );
360 while( prop = this->GetNextProp( ) )
361 renderer->AddViewProp( prop );
362 renderer->Modified( );
365 renderer->RemoveViewProp( this->m_CursorActor );
366 renderer->RemoveViewProp( this->m_TextActor );
371 vtkCamera* camera = renderer->GetActiveCamera( );
372 if( camera != NULL && force_style )
374 // Parallel projections are better when displaying 2D images
375 int axis = this->GetAxis( );
376 camera->ParallelProjectionOn( );
377 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
380 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
381 camera->SetViewUp ( double( 0 ), double( 0 ), double( 1 ) );
385 camera->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
386 camera->SetViewUp ( double( 0 ), double( 0 ), double( 1 ) );
388 else // if( axis == 2 )
390 camera->SetPosition( double( 0 ), double( 0 ), double( -1 ) );
391 camera->SetViewUp ( double( 0 ), double( -1 ), double( 0 ) );
396 renderer->ResetCamera( );
400 // -------------------------------------------------------------------------
401 void cpExtensions::Visualization::ImageSliceActors::
402 PopActorsFrom( vtkRenderWindow* window )
404 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
405 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
407 if( renderer != NULL )
411 this->InitTraversal( );
412 while( prop = this->GetNextProp( ) )
413 renderer->RemoveViewProp( prop );
414 renderer->Modified( );
421 // -------------------------------------------------------------------------
422 unsigned int cpExtensions::Visualization::ImageSliceActors::
423 GetNumberOfImageActors( ) const
425 return( this->m_ImageActors.size( ) );
428 // -------------------------------------------------------------------------
429 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
430 GetImageActor( unsigned int id )
432 if( id < this->m_ImageActors.size( ) )
433 return( this->m_ImageActors[ id ] );
438 // -------------------------------------------------------------------------
439 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
440 GetImageActor( unsigned int id ) const
442 if( id < this->m_ImageActors.size( ) )
443 return( this->m_ImageActors[ id ] );
448 // -------------------------------------------------------------------------
449 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
452 return( this->m_TextActor );
455 // -------------------------------------------------------------------------
456 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
457 GetTextActor( ) const
459 return( this->m_TextActor );
462 // -------------------------------------------------------------------------
463 vtkActor* cpExtensions::Visualization::ImageSliceActors::
466 return( this->m_PlaneActor );
469 // -------------------------------------------------------------------------
470 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
471 GetPlaneActor( ) const
473 return( this->m_PlaneActor );
476 // -------------------------------------------------------------------------
477 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
480 return( this->m_SliceMappers[ 0 ]->GetSlicePlane( ) );
483 // -------------------------------------------------------------------------
484 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
485 GetPlaneFunction( ) const
487 return( this->m_SliceMappers[ 0 ]->GetSlicePlane( ) );
490 // -------------------------------------------------------------------------
491 void cpExtensions::Visualization::ImageSliceActors::
492 SetInterpolate( bool v )
494 if( this->m_Interpolate != v )
496 for( unsigned int i = 0; i < this->m_ImageActors.size( ); ++i )
497 this->m_ImageActors[ i ]->SetInterpolate( v );
498 this->m_Interpolate = v;
504 // -------------------------------------------------------------------------
505 void cpExtensions::Visualization::ImageSliceActors::
508 this->SetInterpolate( true );
511 // -------------------------------------------------------------------------
512 void cpExtensions::Visualization::ImageSliceActors::
515 this->SetInterpolate( false );
518 // -------------------------------------------------------------------------
519 double* cpExtensions::Visualization::ImageSliceActors::
520 GetDisplayBounds( ) const
522 if( this->m_ImageActors.size( ) > 0 )
523 return( this->m_ImageActors[ 0 ]->GetDisplayBounds( ) );
528 // -------------------------------------------------------------------------
529 void cpExtensions::Visualization::ImageSliceActors::
530 GetDisplayBounds( double bounds[ 6 ] ) const
532 if( this->m_ImageActors.size( ) == 0 )
534 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
535 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
538 this->m_ImageActors[ 0 ]->GetDisplayBounds( bounds );
541 // -------------------------------------------------------------------------
542 void cpExtensions::Visualization::ImageSliceActors::
545 if( this->m_SliceMappers.size( ) > 0 )
548 this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
549 double pos[] = { bounds[ 0 ], bounds[ 2 ], bounds[ 4 ] };
550 this->SetCursor( pos );
554 vtkPoints* points = this->m_Cursor->GetPoints( );
555 points->SetPoint( 0, 0, 0, 0 );
556 points->SetPoint( 1, 0, 0, 0 );
557 points->SetPoint( 2, 0, 0, 0 );
558 points->SetPoint( 3, 0, 0, 0 );
559 points->SetPoint( 4, 0, 0, 0 );
560 points->SetPoint( 5, 0, 0, 0 );
561 points->SetPoint( 6, 0, 0, 0 );
562 points->SetPoint( 7, 0, 0, 0 );
563 this->m_Cursor->Modified( );
564 this->m_CursorMapper->Modified( );
565 this->m_CursorActor->Modified( );
570 // -------------------------------------------------------------------------
571 void cpExtensions::Visualization::ImageSliceActors::
572 SetCursor( double pos[ 3 ] )
574 if( this->m_SliceMappers.size( ) == 0 )
578 int a0 = this->GetAxis( );
579 int a1 = ( a0 + 1 ) % 3;
580 int a2 = ( a0 + 2 ) % 3;
586 this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
589 p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
590 p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
592 p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
593 p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
594 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
595 p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
596 p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
597 p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
598 p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
599 p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
601 vtkPoints* points = this->m_Cursor->GetPoints( );
602 points->SetPoint( 0, p0 );
603 points->SetPoint( 1, p1 );
604 points->SetPoint( 2, p2 );
605 points->SetPoint( 3, p3 );
606 points->SetPoint( 4, p4 );
607 points->SetPoint( 5, p5 );
608 points->SetPoint( 6, p6 );
609 points->SetPoint( 7, p7 );
610 this->m_Cursor->Modified( );
611 this->m_CursorMapper->Modified( );
612 this->m_CursorActor->Modified( );
615 // -------------------------------------------------------------------------
616 vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
617 GetImageMap( unsigned int id )
619 if( id < this->m_ImageMaps.size( ) )
620 return( this->m_ImageMaps[ id ].GetPointer( ) );
625 // -------------------------------------------------------------------------
626 const vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
627 GetImageMap( unsigned int id ) const
629 if( id < this->m_ImageMaps.size( ) )
630 return( this->m_ImageMaps[ id ].GetPointer( ) );
635 // -------------------------------------------------------------------------
636 double cpExtensions::Visualization::ImageSliceActors::
637 GetMinWindow( ) const
639 return( this->m_MinWindow );
642 // -------------------------------------------------------------------------
643 double cpExtensions::Visualization::ImageSliceActors::
644 GetMaxWindow( ) const
646 return( this->m_MaxWindow );
649 // -------------------------------------------------------------------------
650 double cpExtensions::Visualization::ImageSliceActors::
653 return( this->m_MinLevel );
656 // -------------------------------------------------------------------------
657 double cpExtensions::Visualization::ImageSliceActors::
660 return( this->m_MaxLevel );
663 // -------------------------------------------------------------------------
664 double cpExtensions::Visualization::ImageSliceActors::
667 if( this->m_ImageMaps.size( ) == 0 )
668 return( double( 0 ) );
669 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
670 return( double( 0 ) );
672 vtkWindowLevelLookupTable* lut =
673 dynamic_cast< vtkWindowLevelLookupTable* >(
674 this->m_ImageMaps[ 0 ]->GetLookupTable( )
677 return( lut->GetWindow( ) );
679 return( double( 0 ) );
682 // -------------------------------------------------------------------------
683 double cpExtensions::Visualization::ImageSliceActors::
686 if( this->m_ImageMaps.size( ) == 0 )
687 return( double( 0 ) );
688 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
689 return( double( 0 ) );
691 vtkWindowLevelLookupTable* lut =
692 dynamic_cast< vtkWindowLevelLookupTable* >(
693 this->m_ImageMaps[ 0 ]->GetLookupTable( )
696 return( lut->GetLevel( ) );
698 return( double( 0 ) );
701 // -------------------------------------------------------------------------
702 void cpExtensions::Visualization::ImageSliceActors::
703 SetWindow( double w )
705 if( this->m_ImageMaps.size( ) == 0 )
707 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
709 vtkWindowLevelLookupTable* lut =
710 dynamic_cast< vtkWindowLevelLookupTable* >(
711 this->m_ImageMaps[ 0 ]->GetLookupTable( )
716 if( w < this->m_MinWindow )
717 w = this->m_MinWindow;
718 if( w > this->m_MaxWindow )
719 w = this->m_MaxWindow;
723 this->m_ImageMaps[ 0 ]->Modified( );
727 // -------------------------------------------------------------------------
728 void cpExtensions::Visualization::ImageSliceActors::
731 if( this->m_ImageMaps.size( ) == 0 )
733 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
735 vtkWindowLevelLookupTable* lut =
736 dynamic_cast< vtkWindowLevelLookupTable* >(
737 this->m_ImageMaps[ 0 ]->GetLookupTable( )
742 if( l < this->m_MinLevel )
743 l = this->m_MinLevel;
744 if( l > this->m_MaxLevel )
745 l = this->m_MaxLevel;
749 this->m_ImageMaps[ 0 ]->Modified( );
753 // -------------------------------------------------------------------------
754 void cpExtensions::Visualization::ImageSliceActors::
755 SetWindowLevel( double w, double l )
757 if( this->m_ImageMaps.size( ) == 0 )
759 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
761 vtkWindowLevelLookupTable* lut =
762 dynamic_cast< vtkWindowLevelLookupTable* >(
763 this->m_ImageMaps[ 0 ]->GetLookupTable( )
768 if( w < this->m_MinWindow )
769 w = this->m_MinWindow;
770 if( w > this->m_MaxWindow )
771 w = this->m_MaxWindow;
772 if( l < this->m_MinLevel )
773 l = this->m_MinLevel;
774 if( l > this->m_MaxLevel )
775 l = this->m_MaxLevel;
780 this->m_ImageMaps[ 0 ]->Modified( );
781 this->UpdateText( w, l );
785 // -------------------------------------------------------------------------
786 void cpExtensions::Visualization::ImageSliceActors::
789 static const double _2 = double( 2 );
790 this->SetWindowLevel(
791 this->m_MaxWindow, ( this->m_MaxLevel + this->m_MinLevel ) / _2
795 // -------------------------------------------------------------------------
796 void cpExtensions::Visualization::ImageSliceActors::
797 SetLookupTable( unsigned int id, vtkLookupTable* lut )
799 if( id < this->m_ImageMaps.size( ) && id > 0 )
801 if( this->m_ImageMaps[ id ].GetPointer( ) != NULL )
803 this->m_ImageMaps[ id ]->SetLookupTable( lut );
804 this->m_ImageMaps[ id ]->Modified( );
812 // -------------------------------------------------------------------------
813 void cpExtensions::Visualization::ImageSliceActors::
814 SetLookupTableAsColor( unsigned int id, double r, double g, double b )
816 static const double _0 = double( 0 );
817 static const double _2 = double( 2 );
818 static const double _4 = double( 4 );
819 static const double _6 = double( 6 );
820 static const double _OPACITY = double( 0.6 );
822 // Check ID consistency
823 if( id == 0 || id >= this->m_ImageMaps.size( ) )
826 // Get image scalar range
827 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
828 vtkImageData* image = dynamic_cast< vtkImageData* >(
829 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
832 image->GetScalarRange( range );
834 // Get HSV from display color
835 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
836 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
837 double d = cmax - cmin;
839 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
845 hue = std::fmod( ( g - b ) / d, _6 );
847 hue = ( ( b - r ) / d ) + _2;
849 hue = ( ( r - g ) / d ) + _4;
854 // Define new lookup table
855 vtkSmartPointer< vtkLookupTable > lut =
856 vtkSmartPointer< vtkLookupTable >::New( );
857 lut->SetScaleToLinear( );
858 lut->SetNanColor( _0, _0, _0, _0 );
859 lut->SetTableRange( range[ 0 ], range[ 1 ] );
860 lut->SetAlphaRange( _0, _OPACITY );
861 lut->SetHueRange( _0, hue );
862 lut->SetSaturationRange( _0, saturation );
863 lut->SetValueRange( _0, value );
865 this->SetLookupTable( id, lut );
868 // -------------------------------------------------------------------------
869 int cpExtensions::Visualization::ImageSliceActors::
872 if( this->m_SliceMappers.size( ) > 0 )
873 return( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
878 // -------------------------------------------------------------------------
879 int cpExtensions::Visualization::ImageSliceActors::
880 GetSliceNumber( ) const
882 if( this->m_SliceMappers.size( ) > 0 )
883 return( this->m_SliceMappers[ 0 ]->GetSliceNumber( ) );
888 // -------------------------------------------------------------------------
889 int cpExtensions::Visualization::ImageSliceActors::
890 GetSliceNumberMinValue( ) const
892 if( this->m_SliceMappers.size( ) > 0 )
893 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
898 // -------------------------------------------------------------------------
899 int cpExtensions::Visualization::ImageSliceActors::
900 GetSliceNumberMaxValue( ) const
902 if( this->m_SliceMappers.size( ) > 0 )
903 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
908 // -------------------------------------------------------------------------
909 void cpExtensions::Visualization::ImageSliceActors::
910 SetSliceNumber( const int& slice )
912 unsigned int nImages = this->m_SliceMappers.size( );
916 // Change visualization extent
917 for( unsigned int i = 0; i < nImages; ++i )
919 this->m_SliceMappers[ i ]->SetSliceNumber( slice );
920 this->m_SliceMappers[ i ]->Modified( );
921 this->m_ImageActors[ i ]->Modified( );
922 this->m_SliceMappers[ i ]->Update( );
927 this->m_SliceMappers[ 0 ]->GetBounds( bnds );
930 { bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] },
931 { bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] }
935 vtkPlane* plane = this->m_SliceMappers[ 0 ]->GetSlicePlane( );
936 plane->GeneralizedProjectPoint( x0[ 0 ], p0[ 0 ] );
937 plane->GeneralizedProjectPoint( x0[ 1 ], p0[ 1 ] );
939 int axis = this->GetAxis( );
940 bnds[ 0 ] = p0[ 0 ][ 0 ];
941 bnds[ 1 ] = p0[ 1 ][ 0 ];
942 bnds[ 2 ] = p0[ 0 ][ 1 ];
943 bnds[ 3 ] = p0[ 1 ][ 1 ];
944 bnds[ 4 ] = p0[ 0 ][ 2 ];
945 bnds[ 5 ] = p0[ 1 ][ 2 ];
947 // Configure visualization and implicit plane orientation
948 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
949 this->m_PlaneActor->GetProperty( )->SetLineWidth( 3 );
950 vtkPoints* plane_points = this->m_Plane->GetPoints( );
951 plane_points->SetPoint( 0, bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] );
952 plane_points->SetPoint( 2, bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] );
953 if( axis == 0 || axis == 2 ) // YZ, x-normal
955 plane_points->SetPoint( 1, bnds[ 1 ], bnds[ 2 ], bnds[ 5 ] );
956 plane_points->SetPoint( 3, bnds[ 0 ], bnds[ 3 ], bnds[ 4 ] );
958 this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
960 this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
962 else if( axis == 1 ) // ZX, y-normal
964 plane_points->SetPoint( 1, bnds[ 0 ], bnds[ 2 ], bnds[ 5 ] );
965 plane_points->SetPoint( 3, bnds[ 1 ], bnds[ 3 ], bnds[ 4 ] );
966 this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
969 this->m_Plane->Modified( );
970 this->m_PlaneMapper->Modified( );
971 this->m_PlaneActor->Modified( );
977 // -------------------------------------------------------------------------
978 void cpExtensions::Visualization::ImageSliceActors::
979 SetSlice( double* pos )
982 if( this->m_ImageMaps[ 0 ] != NULL )
984 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
986 image = this->m_SliceMappers[ 0 ]->GetInput( );
990 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
991 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
994 // -------------------------------------------------------------------------
995 void cpExtensions::Visualization::ImageSliceActors::
998 if( this->m_SliceMappers.size( ) > 0 )
1001 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1002 if ( axId == 0 ) axis = 'X';
1003 else if( axId == 1 ) axis = 'Y';
1004 else if( axId == 2 ) axis = 'Z';
1007 this->m_TextBuffer, "Axis: %c (%d)",
1008 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( )
1012 this->m_TextBuffer[ 0 ] = '\0';
1013 this->m_TextActor->SetInput( this->m_TextBuffer );
1014 this->m_TextActor->Modified( );
1018 // -------------------------------------------------------------------------
1019 void cpExtensions::Visualization::ImageSliceActors::
1020 UpdateText( double pos[ 3 ] )
1022 if( this->m_SliceMappers.size( ) > 0 )
1025 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1026 if ( axId == 0 ) axis = 'X';
1027 else if( axId == 1 ) axis = 'Y';
1028 else if( axId == 2 ) axis = 'Z';
1029 int slice = this->GetSliceNumber( );
1031 vtkImageData* image;
1032 if( this->m_ImageMaps[ 0 ] != NULL )
1034 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
1036 image = this->m_SliceMappers[ 0 ]->GetInput( );
1039 double pcoords[ 3 ];
1040 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1041 ijk[ axId ] = slice;
1044 image->GetExtent( ext );
1046 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
1047 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
1048 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
1051 int nScl = image->GetNumberOfScalarComponents( );
1052 std::stringstream str;
1056 << "," << ijk[ 2 ] << "]=(";
1058 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
1059 for( int n = 1; n < nScl; ++n )
1062 << image->GetScalarComponentAsFloat(
1063 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
1067 this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
1068 axis, slice, str.str( ).c_str( )
1074 this->m_TextBuffer[ 0 ] = '\0';
1075 this->m_TextActor->SetInput( this->m_TextBuffer );
1076 this->m_TextActor->Modified( );
1080 // -------------------------------------------------------------------------
1081 void cpExtensions::Visualization::ImageSliceActors::
1082 UpdateText( const double& w, const double& l )
1084 if( this->m_SliceMappers.size( ) > 0 )
1087 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1088 if ( axId == 0 ) axis = 'X';
1089 else if( axId == 1 ) axis = 'Y';
1090 else if( axId == 2 ) axis = 'Z';
1093 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1094 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1098 this->m_TextBuffer[ 0 ] = '\0';
1099 this->m_TextActor->SetInput( this->m_TextBuffer );
1100 this->m_TextActor->Modified( );
1104 // -------------------------------------------------------------------------
1105 void cpExtensions::Visualization::ImageSliceActors::
1108 if( this->m_Window != NULL )
1109 this->m_Window->Render( );
1112 // -------------------------------------------------------------------------
1113 void cpExtensions::Visualization::ImageSliceActors::
1116 if( this->m_Window == NULL )
1118 vtkRenderer* renderer =
1119 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1120 if( renderer != NULL )
1121 renderer->ResetCamera( );
1124 // -------------------------------------------------------------------------
1125 cpExtensions::Visualization::ImageSliceActors::
1129 m_Interpolate( false )
1132 this->_ConfigureStyle( );
1135 // -------------------------------------------------------------------------
1136 cpExtensions::Visualization::ImageSliceActors::
1137 ~ImageSliceActors( )
1141 // -------------------------------------------------------------------------
1142 void cpExtensions::Visualization::ImageSliceActors::
1145 // Connect this view with a controller
1146 this->m_Style = vtkSmartPointer< TStyle >::New( );
1147 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1148 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1149 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1150 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1151 this->m_Style->AddEnterCommand( Self::_EnterCommand, this );
1152 this->m_Style->AddLeaveCommand( Self::_LeaveCommand, this );
1155 // -------------------------------------------------------------------------
1156 void cpExtensions::Visualization::ImageSliceActors::
1157 _ConfigureNewLUT( vtkImageData* data )
1159 // Does the new LUT is a window-level one?
1160 unsigned int nImgs = this->m_ImageMaps.size( );
1161 unsigned int nCmps = data->GetNumberOfScalarComponents( );
1165 this->m_ImageMaps.push_back( NULL );
1170 // Create LUT filter
1171 this->m_ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1175 data->GetScalarRange( range );
1176 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1177 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1178 lut->SetScaleToLinear( );
1179 lut->SetTableRange( range );
1180 lut->SetWindow( range[ 1 ] - range[ 0 ] );
1181 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1183 this->m_ImageMaps[ 0 ]->SetLookupTable( lut );
1186 this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1189 // -------------------------------------------------------------------------
1190 void cpExtensions::Visualization::ImageSliceActors::
1191 _ConfigureNewInput( int axis )
1193 unsigned int nImages = this->m_ImageActors.size( );
1196 vtkImageSliceMapper* mapper = this->m_SliceMappers[ nImages ];
1198 mapper->SetOrientation( axis );
1200 mapper->SetOrientation( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
1204 vtkSmartPointer< vtkImageActor > actor =
1205 vtkSmartPointer< vtkImageActor >::New( );
1206 this->m_ImageActors.push_back( actor );
1207 actor->SetMapper( mapper );
1208 actor->SetInterpolate( this->m_Interpolate );
1212 this->m_Style->AssociateImageActor( actor );
1213 this->AddItem( actor );
1216 this->SetSliceNumber( this->GetSliceNumber( ) );
1218 this->SetSliceNumber( this->GetSliceNumberMaxValue( ) );
1219 this->ResetCursor( );
1223 // -------------------------------------------------------------------------
1224 void cpExtensions::Visualization::ImageSliceActors::
1226 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1227 bool alt, bool ctr, bool sft
1230 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1231 if( actors == NULL )
1234 if( btn == TStyle::ButtonID_None )
1236 // Just show the pixel information
1237 actors->SetCursor( pos );
1238 actors->UpdateText( pos );
1241 else if( btn == TStyle::ButtonID_Left )
1243 if( !alt && ctr && !sft )
1245 // Interactively move slices
1246 auto i = actors->m_SlicesCommands.begin( );
1247 for( ; i != actors->m_SlicesCommands.end( ); ++i )
1248 i->first( pos, actors->GetAxis( ), i->second );
1253 else if( btn == TStyle::ButtonID_Right )
1255 if( !alt && !ctr && sft )
1257 // Change image window level
1259 actors->m_SliceMappers[ 0 ]->GetBounds( bounds );
1261 int a0 = actors->GetAxis( );
1262 int a1 = ( a0 + 1 ) % 3;
1263 int a2 = ( a0 + 2 ) % 3;
1264 double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1265 double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1266 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1267 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1269 dx *= actors->m_StartWindowLevel[ 0 ];
1270 dy *= actors->m_StartWindowLevel[ 1 ];
1271 dx += actors->m_StartWindowLevel[ 0 ];
1272 dy += actors->m_StartWindowLevel[ 1 ];
1273 actors->SetWindowLevel( dx, dy );
1276 // Associate objects
1277 auto i = actors->m_WindowLevelCommands.begin( );
1278 for( ; i != actors->m_WindowLevelCommands.end( ); ++i )
1279 i->first( dx, dy, i->second );
1286 // -------------------------------------------------------------------------
1287 void cpExtensions::Visualization::ImageSliceActors::
1289 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1290 bool alt, bool ctr, bool sft
1293 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1294 if( actors == NULL )
1297 actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1298 actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1299 actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1300 actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1301 actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1304 // -------------------------------------------------------------------------
1305 void cpExtensions::Visualization::ImageSliceActors::
1307 void* data, const int& dir,
1308 bool alt, bool ctr, bool sft
1311 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1312 if( actors == NULL )
1317 int slice = actors->GetSliceNumber( ) + ( dir * ( ( sft )? 10: 1 ) );
1318 if( slice < actors->GetSliceNumberMinValue( ) )
1319 slice = actors->GetSliceNumberMinValue( );
1320 if( slice > actors->GetSliceNumberMaxValue( ) )
1321 slice = actors->GetSliceNumberMaxValue( );
1322 actors->SetSliceNumber( slice );
1324 auto a = actors->m_AssociatedSlices.begin( );
1325 for( ; a != actors->m_AssociatedSlices.end( ); ++a )
1327 ( *a )->SetSliceNumber( slice );
1333 // Associate objects
1334 auto i = actors->m_RenderCommands.begin( );
1335 for( ; i != actors->m_RenderCommands.end( ); ++i )
1336 i->first( i->second );
1341 // -------------------------------------------------------------------------
1342 void cpExtensions::Visualization::ImageSliceActors::
1343 _KeyCommand( void* data, const char& key )
1345 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1346 if( actors == NULL )
1353 actors->ResetCamera( );
1356 // Associate objects
1357 auto i = actors->m_RenderCommands.begin( );
1358 for( ; i != actors->m_RenderCommands.end( ); ++i )
1359 i->first( i->second );
1364 actors->ResetWindowLevel( );
1367 // Associate objects
1368 auto i = actors->m_RenderCommands.begin( );
1369 for( ; i != actors->m_RenderCommands.end( ); ++i )
1370 i->first( i->second );
1378 // -------------------------------------------------------------------------
1379 void cpExtensions::Visualization::ImageSliceActors::
1380 _EnterCommand( void* data )
1382 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1383 if( actors == NULL )
1386 actors->ResetCursor( );
1387 actors->m_CursorActor->VisibilityOn( );
1391 // -------------------------------------------------------------------------
1392 void cpExtensions::Visualization::ImageSliceActors::
1393 _LeaveCommand( void* data )
1395 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1396 if( actors == NULL )
1399 actors->ResetCursor( );
1400 actors->m_CursorActor->VisibilityOff( );