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_PlaneFunction = vtkSmartPointer< vtkPlane >::New( );
190 this->m_Plane = vtkSmartPointer< vtkPolyData >::New( );
191 this->m_PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
192 this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
193 this->m_PlaneActor = vtkSmartPointer< vtkActor >::New( );
194 this->m_TextBuffer[ 0 ] = '\0';
196 // Unique objects configuration
197 vtkSmartPointer< vtkPoints > cursor_points =
198 vtkSmartPointer< vtkPoints >::New( );
199 vtkSmartPointer< vtkCellArray > cursor_lines =
200 vtkSmartPointer< vtkCellArray >::New( );
201 cursor_points->InsertNextPoint( 0, 0, 0 );
202 cursor_points->InsertNextPoint( 0, 0, 0 );
203 cursor_points->InsertNextPoint( 0, 0, 0 );
204 cursor_points->InsertNextPoint( 0, 0, 0 );
205 cursor_points->InsertNextPoint( 0, 0, 0 );
206 cursor_points->InsertNextPoint( 0, 0, 0 );
207 cursor_points->InsertNextPoint( 0, 0, 0 );
208 cursor_points->InsertNextPoint( 0, 0, 0 );
209 cursor_lines->InsertNextCell( 2 );
210 cursor_lines->InsertCellPoint( 0 );
211 cursor_lines->InsertCellPoint( 1 );
212 cursor_lines->InsertNextCell( 2 );
213 cursor_lines->InsertCellPoint( 2 );
214 cursor_lines->InsertCellPoint( 3 );
215 cursor_lines->InsertNextCell( 2 );
216 cursor_lines->InsertCellPoint( 4 );
217 cursor_lines->InsertCellPoint( 5 );
218 cursor_lines->InsertNextCell( 2 );
219 cursor_lines->InsertCellPoint( 6 );
220 cursor_lines->InsertCellPoint( 7 );
221 this->m_Cursor->SetPoints( cursor_points );
222 this->m_Cursor->SetLines( cursor_lines );
223 this->m_CursorMapper->SetInputData( this->m_Cursor );
224 this->m_CursorActor->SetMapper( this->m_CursorMapper );
226 vtkSmartPointer< vtkPoints > plane_points =
227 vtkSmartPointer< vtkPoints >::New( );
228 vtkSmartPointer< vtkCellArray > plane_lines =
229 vtkSmartPointer< vtkCellArray >::New( );
231 plane_points->InsertNextPoint( 0, 0, 0 );
232 plane_points->InsertNextPoint( 0, 1, 0 );
233 plane_points->InsertNextPoint( 1, 1, 0 );
234 plane_points->InsertNextPoint( 1, 0, 0 );
235 plane_lines->InsertNextCell( 5 );
236 plane_lines->InsertCellPoint( 0 );
237 plane_lines->InsertCellPoint( 1 );
238 plane_lines->InsertCellPoint( 2 );
239 plane_lines->InsertCellPoint( 3 );
240 plane_lines->InsertCellPoint( 0 );
241 this->m_Plane->SetPoints( plane_points );
242 this->m_Plane->SetLines( plane_lines );
244 this->m_PlaneMapper->SetInputData( this->m_Plane );
245 this->m_PlaneActor->SetMapper( this->m_PlaneMapper );
247 this->m_TextActor->SetTextScaleModeToNone( );
248 vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
249 textprop->SetColor( 1, 1, 1 );
250 textprop->SetFontFamilyToCourier( );
251 textprop->SetFontSize( 18 );
252 textprop->BoldOff( );
253 textprop->ItalicOff( );
254 textprop->ShadowOff( );
255 textprop->SetJustificationToLeft( );
256 textprop->SetVerticalJustificationToBottom( );
257 vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
258 coord->SetCoordinateSystemToNormalizedViewport( );
259 coord->SetValue( 0.01, 0.01 );
261 // Update actor collection
262 this->AddItem( this->m_CursorActor );
263 this->AddItem( this->m_TextActor );
264 this->AddItem( this->m_PlaneActor );
267 // -------------------------------------------------------------------------
268 void cpExtensions::Visualization::ImageSliceActors::
269 AssociateSlice( Self* slice )
271 this->m_AssociatedSlices.push_back( slice );
275 // -------------------------------------------------------------------------
276 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
279 return( this->m_Style.GetPointer( ) );
282 // -------------------------------------------------------------------------
283 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
286 return( this->m_Style.GetPointer( ) );
289 // -------------------------------------------------------------------------
290 vtkImageData* cpExtensions::Visualization::ImageSliceActors::
291 GetInputImage( unsigned int id )
293 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
294 vtkImageData* image = dynamic_cast< vtkImageData* >(
295 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
300 // -------------------------------------------------------------------------
301 const vtkImageData* cpExtensions::Visualization::ImageSliceActors::
302 GetInputImage( unsigned int id ) const
304 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
305 const vtkImageData* image = dynamic_cast< const vtkImageData* >(
306 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
311 // -------------------------------------------------------------------------
312 void cpExtensions::Visualization::ImageSliceActors::
313 PushActorsInto( vtkRenderWindow* window, bool force_style )
315 this->m_Window = window;
318 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
319 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
320 if( rwi == NULL || renderer == NULL )
324 if( this->m_Style.GetPointer( ) != NULL && force_style )
325 rwi->SetInteractorStyle( this->m_Style );
329 this->InitTraversal( );
330 while( prop = this->GetNextProp( ) )
331 renderer->AddViewProp( prop );
332 renderer->Modified( );
335 renderer->RemoveViewProp( this->m_CursorActor );
336 renderer->RemoveViewProp( this->m_TextActor );
341 vtkCamera* camera = renderer->GetActiveCamera( );
342 if( camera != NULL && force_style )
344 // Parallel projections are better when displaying 2D images
345 int axis = this->GetAxis( );
346 camera->ParallelProjectionOn( );
347 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
350 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
351 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
355 camera->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
356 camera->SetViewUp ( double( 0 ), double( 0 ), double( -1 ) );
358 else // if( axis == 2 )
360 camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
361 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
366 renderer->ResetCamera( );
370 // -------------------------------------------------------------------------
371 void cpExtensions::Visualization::ImageSliceActors::
372 PopActorsFrom( vtkRenderWindow* window )
374 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
375 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
377 if( renderer != NULL )
381 this->InitTraversal( );
382 while( prop = this->GetNextProp( ) )
383 renderer->RemoveViewProp( prop );
384 renderer->Modified( );
391 // -------------------------------------------------------------------------
392 unsigned int cpExtensions::Visualization::ImageSliceActors::
393 GetNumberOfImageActors( ) const
395 return( this->m_ImageActors.size( ) );
398 // -------------------------------------------------------------------------
399 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
400 GetImageActor( unsigned int id )
402 if( id < this->m_ImageActors.size( ) )
403 return( this->m_ImageActors[ id ] );
408 // -------------------------------------------------------------------------
409 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
410 GetImageActor( unsigned int id ) const
412 if( id < this->m_ImageActors.size( ) )
413 return( this->m_ImageActors[ id ] );
418 // -------------------------------------------------------------------------
419 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
422 return( this->m_TextActor );
425 // -------------------------------------------------------------------------
426 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
427 GetTextActor( ) const
429 return( this->m_TextActor );
432 // -------------------------------------------------------------------------
433 vtkActor* cpExtensions::Visualization::ImageSliceActors::
436 return( this->m_PlaneActor );
439 // -------------------------------------------------------------------------
440 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
441 GetPlaneActor( ) const
443 return( this->m_PlaneActor );
446 // -------------------------------------------------------------------------
447 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
450 return( this->m_PlaneFunction );
453 // -------------------------------------------------------------------------
454 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
455 GetPlaneFunction( ) const
457 return( this->m_PlaneFunction );
460 // -------------------------------------------------------------------------
461 void cpExtensions::Visualization::ImageSliceActors::
462 SetInterpolate( bool v )
464 if( this->m_Interpolate != v )
466 for( unsigned int i = 0; i < this->m_ImageActors.size( ); ++i )
467 this->m_ImageActors[ i ]->SetInterpolate( v );
468 this->m_Interpolate = v;
474 // -------------------------------------------------------------------------
475 void cpExtensions::Visualization::ImageSliceActors::
478 this->SetInterpolate( true );
481 // -------------------------------------------------------------------------
482 void cpExtensions::Visualization::ImageSliceActors::
485 this->SetInterpolate( false );
488 // -------------------------------------------------------------------------
489 double* cpExtensions::Visualization::ImageSliceActors::
490 GetDisplayBounds( ) const
492 if( this->m_ImageActors.size( ) > 0 )
493 return( this->m_ImageActors[ 0 ]->GetDisplayBounds( ) );
498 // -------------------------------------------------------------------------
499 void cpExtensions::Visualization::ImageSliceActors::
500 GetDisplayBounds( double bounds[ 6 ] ) const
502 if( this->m_ImageActors.size( ) == 0 )
504 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
505 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
508 this->m_ImageActors[ 0 ]->GetDisplayBounds( bounds );
511 // -------------------------------------------------------------------------
512 void cpExtensions::Visualization::ImageSliceActors::
515 vtkPoints* points = this->m_Cursor->GetPoints( );
516 points->SetPoint( 0, 0, 0, 0 );
517 points->SetPoint( 1, 0, 0, 0 );
518 points->SetPoint( 2, 0, 0, 0 );
519 points->SetPoint( 3, 0, 0, 0 );
520 points->SetPoint( 4, 0, 0, 0 );
521 points->SetPoint( 5, 0, 0, 0 );
522 points->SetPoint( 6, 0, 0, 0 );
523 points->SetPoint( 7, 0, 0, 0 );
524 this->m_Cursor->Modified( );
525 this->m_CursorMapper->Modified( );
526 this->m_CursorActor->Modified( );
529 // -------------------------------------------------------------------------
530 void cpExtensions::Visualization::ImageSliceActors::
531 SetCursor( double pos[ 3 ] )
533 if( this->m_SliceMappers.size( ) == 0 )
537 int a0 = this->GetAxis( );
538 int a1 = ( a0 + 1 ) % 3;
539 int a2 = ( a0 + 2 ) % 3;
545 this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
548 p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
549 p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
551 p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
552 p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
553 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
554 p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
555 p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
556 p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
557 p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
558 p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
560 vtkPoints* points = this->m_Cursor->GetPoints( );
561 points->SetPoint( 0, p0 );
562 points->SetPoint( 1, p1 );
563 points->SetPoint( 2, p2 );
564 points->SetPoint( 3, p3 );
565 points->SetPoint( 4, p4 );
566 points->SetPoint( 5, p5 );
567 points->SetPoint( 6, p6 );
568 points->SetPoint( 7, p7 );
569 this->m_Cursor->Modified( );
570 this->m_CursorMapper->Modified( );
571 this->m_CursorActor->Modified( );
574 // -------------------------------------------------------------------------
575 vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
576 GetImageMap( unsigned int id )
578 if( id < this->m_ImageMaps.size( ) )
579 return( this->m_ImageMaps[ id ].GetPointer( ) );
584 // -------------------------------------------------------------------------
585 const vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
586 GetImageMap( unsigned int id ) const
588 if( id < this->m_ImageMaps.size( ) )
589 return( this->m_ImageMaps[ id ].GetPointer( ) );
594 // -------------------------------------------------------------------------
595 double cpExtensions::Visualization::ImageSliceActors::
596 GetMinWindow( ) const
598 return( this->m_MinWindow );
601 // -------------------------------------------------------------------------
602 double cpExtensions::Visualization::ImageSliceActors::
603 GetMaxWindow( ) const
605 return( this->m_MaxWindow );
608 // -------------------------------------------------------------------------
609 double cpExtensions::Visualization::ImageSliceActors::
612 return( this->m_MinLevel );
615 // -------------------------------------------------------------------------
616 double cpExtensions::Visualization::ImageSliceActors::
619 return( this->m_MaxLevel );
622 // -------------------------------------------------------------------------
623 double cpExtensions::Visualization::ImageSliceActors::
626 if( this->m_ImageMaps.size( ) == 0 )
627 return( double( 0 ) );
628 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
629 return( double( 0 ) );
631 vtkWindowLevelLookupTable* lut =
632 dynamic_cast< vtkWindowLevelLookupTable* >(
633 this->m_ImageMaps[ 0 ]->GetLookupTable( )
636 return( lut->GetWindow( ) );
638 return( double( 0 ) );
641 // -------------------------------------------------------------------------
642 double cpExtensions::Visualization::ImageSliceActors::
645 if( this->m_ImageMaps.size( ) == 0 )
646 return( double( 0 ) );
647 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
648 return( double( 0 ) );
650 vtkWindowLevelLookupTable* lut =
651 dynamic_cast< vtkWindowLevelLookupTable* >(
652 this->m_ImageMaps[ 0 ]->GetLookupTable( )
655 return( lut->GetLevel( ) );
657 return( double( 0 ) );
660 // -------------------------------------------------------------------------
661 void cpExtensions::Visualization::ImageSliceActors::
662 SetWindow( double w )
664 if( this->m_ImageMaps.size( ) == 0 )
666 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
668 vtkWindowLevelLookupTable* lut =
669 dynamic_cast< vtkWindowLevelLookupTable* >(
670 this->m_ImageMaps[ 0 ]->GetLookupTable( )
675 /* TODO: Min and Max values are assigned 0!!!
676 if( w < this->MinWindow )
678 if( w > this->MaxWindow )
683 this->m_ImageMaps[ 0 ]->Modified( );
687 // -------------------------------------------------------------------------
688 void cpExtensions::Visualization::ImageSliceActors::
691 if( this->m_ImageMaps.size( ) == 0 )
693 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
695 vtkWindowLevelLookupTable* lut =
696 dynamic_cast< vtkWindowLevelLookupTable* >(
697 this->m_ImageMaps[ 0 ]->GetLookupTable( )
702 /* TODO: Min and Max values are assigned 0!!!
703 if( l < this->MinLevel )
705 if( l > this->MaxLevel )
710 this->m_ImageMaps[ 0 ]->Modified( );
714 // -------------------------------------------------------------------------
715 void cpExtensions::Visualization::ImageSliceActors::
716 SetWindowLevel( double w, double l )
718 if( this->m_ImageMaps.size( ) == 0 )
720 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
722 vtkWindowLevelLookupTable* lut =
723 dynamic_cast< vtkWindowLevelLookupTable* >(
724 this->m_ImageMaps[ 0 ]->GetLookupTable( )
729 /* TODO: Min and Max values are assigned 0!!!
730 if( w < this->MinWindow )
732 if( w > this->MaxWindow )
734 if( l < this->MinLevel )
736 if( l > this->MaxLevel )
742 this->m_ImageMaps[ 0 ]->Modified( );
743 this->UpdateText( w, l );
747 // -------------------------------------------------------------------------
748 void cpExtensions::Visualization::ImageSliceActors::
751 static const double _2 = double( 2 );
752 this->SetWindowLevel(
753 this->m_MaxWindow, ( this->m_MaxLevel + this->m_MinLevel ) / _2
757 // -------------------------------------------------------------------------
758 void cpExtensions::Visualization::ImageSliceActors::
759 SetLookupTable( unsigned int id, vtkLookupTable* lut )
761 if( id < this->m_ImageMaps.size( ) && id > 0 )
763 if( this->m_ImageMaps[ id ].GetPointer( ) != NULL )
765 this->m_ImageMaps[ id ]->SetLookupTable( lut );
766 this->m_ImageMaps[ id ]->Modified( );
774 // -------------------------------------------------------------------------
775 void cpExtensions::Visualization::ImageSliceActors::
776 SetLookupTableAsColor( unsigned int id, double r, double g, double b )
778 static const double _0 = double( 0 );
779 static const double _2 = double( 2 );
780 static const double _4 = double( 4 );
781 static const double _6 = double( 6 );
782 static const double _OPACITY = double( 0.6 );
784 // Check ID consistency
785 if( id == 0 || id >= this->m_ImageMaps.size( ) )
788 // Get image scalar range
789 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
790 vtkImageData* image = dynamic_cast< vtkImageData* >(
791 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
794 image->GetScalarRange( range );
796 // Get HSV from display color
797 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
798 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
799 double d = cmax - cmin;
801 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
807 hue = std::fmod( ( g - b ) / d, _6 );
809 hue = ( ( b - r ) / d ) + _2;
811 hue = ( ( r - g ) / d ) + _4;
816 // Define new lookup table
817 vtkSmartPointer< vtkLookupTable > lut =
818 vtkSmartPointer< vtkLookupTable >::New( );
819 lut->SetScaleToLinear( );
820 lut->SetNanColor( _0, _0, _0, _0 );
821 lut->SetTableRange( range[ 0 ], range[ 1 ] );
822 lut->SetAlphaRange( _0, _OPACITY );
823 lut->SetHueRange( _0, hue );
824 lut->SetSaturationRange( _0, saturation );
825 lut->SetValueRange( _0, value );
827 this->SetLookupTable( id, lut );
830 // -------------------------------------------------------------------------
831 int cpExtensions::Visualization::ImageSliceActors::
834 if( this->m_SliceMappers.size( ) > 0 )
835 return( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
840 // -------------------------------------------------------------------------
841 int cpExtensions::Visualization::ImageSliceActors::
842 GetSliceNumber( ) const
844 if( this->m_SliceMappers.size( ) > 0 )
845 return( this->m_SliceMappers[ 0 ]->GetSliceNumber( ) );
850 // -------------------------------------------------------------------------
851 int cpExtensions::Visualization::ImageSliceActors::
852 GetSliceNumberMinValue( ) const
854 if( this->m_SliceMappers.size( ) > 0 )
855 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
860 // -------------------------------------------------------------------------
861 int cpExtensions::Visualization::ImageSliceActors::
862 GetSliceNumberMaxValue( ) const
864 if( this->m_SliceMappers.size( ) > 0 )
865 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
870 // -------------------------------------------------------------------------
871 void cpExtensions::Visualization::ImageSliceActors::
872 SetSliceNumber( const int& slice )
874 unsigned int nImages = this->m_SliceMappers.size( );
878 // Compute plane properties
879 int axis = this->m_SliceMappers[ 0 ]->GetOrientation( );
880 vtkAlgorithm* algo = this->m_SliceMappers[ 0 ]->GetInputAlgorithm( );
881 vtkInformation* info = algo->GetOutputInformation( 0 );
883 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
884 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
885 info->Get( vtkDataObject::ORIGIN( ), ori );
886 info->Get( vtkDataObject::SPACING( ), spac );
887 this->m_SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
889 // Update display extent
896 d_ext[ axis << 1 ] = slice;
897 d_ext[ ( axis << 1 ) + 1 ] = slice;
905 << d_ext[ 5 ] << " : "
906 << axis << std::endl;
908 // Change visualization extent
909 for( unsigned int i = 0; i < nImages; ++i )
911 this->m_SliceMappers[ i ]->SetSliceNumber( slice );
912 this->m_ImageActors[ i ]->SetDisplayExtent( d_ext );
913 this->m_SliceMappers[ i ]->Modified( );
914 this->m_ImageActors[ i ]->Modified( );
915 this->m_SliceMappers[ i ]->Update( );
919 // Prevent obscuring voxels by offsetting the plane geometry
922 ori[ 0 ] + ( spac[ 0 ] * double( d_ext[ 0 ] ) ),
923 ori[ 0 ] + ( spac[ 0 ] * double( d_ext[ 1 ] ) )
927 ori[ 1 ] + ( spac[ 1 ] * double( d_ext[ 2 ] ) ),
928 ori[ 1 ] + ( spac[ 1 ] * double( d_ext[ 3 ] ) )
932 ori[ 2 ] + ( spac[ 2 ] * double( d_ext[ 4 ] ) ),
933 ori[ 2 ] + ( spac[ 2 ] * double( d_ext[ 5 ] ) )
936 if( spac[ 0 ] < double( 0 ) )
938 double t = xbnds[ 0 ];
939 xbnds[ 0 ] = xbnds[ 1 ];
943 if( spac[ 1 ] < double( 0 ) )
945 double t = ybnds[ 0 ];
946 ybnds[ 0 ] = ybnds[ 1 ];
950 if( spac[ 2 ] < double( 0 ) )
952 double t = zbnds[ 0 ];
953 zbnds[ 0 ] = zbnds[ 1 ];
958 // Plane function origin
959 this->m_PlaneFunction->SetOrigin( pos );
961 // Configure visualization and implicit plane orientation
962 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
963 this->m_PlaneActor->GetProperty( )->SetLineWidth( 2 );
964 vtkPoints* plane_points = this->m_Plane->GetPoints( );
965 if( axis == 0 ) // YZ, x-normal
967 this->m_PlaneFunction->SetNormal( 1, 0, 0 );
968 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
969 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
970 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
971 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
972 this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
974 else if( axis == 1 ) // ZX, y-normal
976 this->m_PlaneFunction->SetNormal( 0, 1, 0 );
977 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
978 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
979 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
980 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
981 this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
985 this->m_PlaneFunction->SetNormal( 0, 0, 1 );
986 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
987 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
988 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
989 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
990 this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
993 this->m_PlaneFunction->Modified( );
994 this->m_Plane->Modified( );
995 this->m_PlaneMapper->Modified( );
996 this->m_PlaneActor->Modified( );
1002 // -------------------------------------------------------------------------
1003 void cpExtensions::Visualization::ImageSliceActors::
1004 SetSlice( double* pos )
1006 vtkImageData* image;
1007 if( this->m_ImageMaps[ 0 ] != NULL )
1009 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
1011 image = this->m_SliceMappers[ 0 ]->GetInput( );
1014 double pcoords[ 3 ];
1015 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1016 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
1019 // -------------------------------------------------------------------------
1020 void cpExtensions::Visualization::ImageSliceActors::
1023 if( this->m_SliceMappers.size( ) > 0 )
1026 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1027 if ( axId == 0 ) axis = 'X';
1028 else if( axId == 1 ) axis = 'Y';
1029 else if( axId == 2 ) axis = 'Z';
1032 this->m_TextBuffer, "Axis: %c (%d)",
1033 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( )
1037 this->m_TextBuffer[ 0 ] = '\0';
1038 this->m_TextActor->SetInput( this->m_TextBuffer );
1039 this->m_TextActor->Modified( );
1043 // -------------------------------------------------------------------------
1044 void cpExtensions::Visualization::ImageSliceActors::
1045 UpdateText( double pos[ 3 ] )
1047 if( this->m_SliceMappers.size( ) > 0 )
1050 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1051 if ( axId == 0 ) axis = 'X';
1052 else if( axId == 1 ) axis = 'Y';
1053 else if( axId == 2 ) axis = 'Z';
1054 int slice = this->GetSliceNumber( );
1056 vtkImageData* image;
1057 if( this->m_ImageMaps[ 0 ] != NULL )
1059 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
1061 image = this->m_SliceMappers[ 0 ]->GetInput( );
1064 double pcoords[ 3 ];
1065 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1066 ijk[ axId ] = slice;
1069 image->GetExtent( ext );
1071 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
1072 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
1073 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
1076 int nScl = image->GetNumberOfScalarComponents( );
1077 std::stringstream str;
1081 << "," << ijk[ 2 ] << "]=(";
1083 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
1084 for( int n = 1; n < nScl; ++n )
1087 << image->GetScalarComponentAsFloat(
1088 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
1092 this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
1093 axis, slice, str.str( ).c_str( )
1099 this->m_TextBuffer[ 0 ] = '\0';
1100 this->m_TextActor->SetInput( this->m_TextBuffer );
1101 this->m_TextActor->Modified( );
1105 // -------------------------------------------------------------------------
1106 void cpExtensions::Visualization::ImageSliceActors::
1107 UpdateText( const double& w, const double& l )
1109 if( this->m_SliceMappers.size( ) > 0 )
1112 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1113 if ( axId == 0 ) axis = 'X';
1114 else if( axId == 1 ) axis = 'Y';
1115 else if( axId == 2 ) axis = 'Z';
1118 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1119 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1123 this->m_TextBuffer[ 0 ] = '\0';
1124 this->m_TextActor->SetInput( this->m_TextBuffer );
1125 this->m_TextActor->Modified( );
1129 // -------------------------------------------------------------------------
1130 void cpExtensions::Visualization::ImageSliceActors::
1133 if( this->m_Window != NULL )
1134 this->m_Window->Render( );
1137 // -------------------------------------------------------------------------
1138 void cpExtensions::Visualization::ImageSliceActors::
1141 if( this->m_Window == NULL )
1143 vtkRenderer* renderer =
1144 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1145 if( renderer != NULL )
1146 renderer->ResetCamera( );
1149 // -------------------------------------------------------------------------
1150 cpExtensions::Visualization::ImageSliceActors::
1154 m_Interpolate( false )
1157 this->_ConfigureStyle( );
1160 // -------------------------------------------------------------------------
1161 cpExtensions::Visualization::ImageSliceActors::
1162 ~ImageSliceActors( )
1166 // -------------------------------------------------------------------------
1167 void cpExtensions::Visualization::ImageSliceActors::
1170 // Connect this view with a controller
1171 this->m_Style = vtkSmartPointer< TStyle >::New( );
1172 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1173 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1174 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1175 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1178 // -------------------------------------------------------------------------
1179 void cpExtensions::Visualization::ImageSliceActors::
1180 _ConfigureNewLUT( vtkImageData* data )
1182 // Does the new LUT is a window-level one?
1183 unsigned int nImgs = this->m_ImageMaps.size( );
1184 unsigned int nCmps = data->GetNumberOfScalarComponents( );
1188 this->m_ImageMaps.push_back( NULL );
1193 // Create LUT filter
1194 this->m_ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1198 data->GetScalarRange( range );
1199 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1200 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1201 lut->SetScaleToLinear( );
1202 lut->SetTableRange( range );
1203 lut->SetWindow( range[ 1 ] - range[ 0 ] );
1204 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1206 this->m_ImageMaps[ 0 ]->SetLookupTable( lut );
1208 this->m_MinWindow = double( 0 );
1209 this->m_MaxWindow = range[ 1 ] - range[ 0 ];
1210 this->m_MinLevel = range[ 0 ];
1211 this->m_MaxLevel = range[ 1 ];
1214 this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1217 // -------------------------------------------------------------------------
1218 void cpExtensions::Visualization::ImageSliceActors::
1219 _ConfigureNewInput( int axis )
1221 unsigned int nImages = this->m_ImageActors.size( );
1224 vtkImageSliceMapper* mapper = this->m_SliceMappers[ nImages ];
1226 mapper->SetOrientation( axis );
1228 mapper->SetOrientation( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
1232 vtkSmartPointer< vtkImageActor > actor =
1233 vtkSmartPointer< vtkImageActor >::New( );
1234 this->m_ImageActors.push_back( actor );
1235 actor->SetMapper( mapper );
1236 actor->SetInterpolate( this->m_Interpolate );
1240 this->m_Style->AssociateImageActor( actor );
1241 this->AddItem( actor );
1244 this->SetSliceNumber( this->GetSliceNumber( ) );
1246 this->SetSliceNumber( this->GetSliceNumberMaxValue( ) );
1250 // -------------------------------------------------------------------------
1251 void cpExtensions::Visualization::ImageSliceActors::
1253 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1254 bool alt, bool ctr, bool sft
1257 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1258 if( actors == NULL )
1261 if( btn == TStyle::ButtonID_None )
1263 // Just show the pixel information
1264 actors->SetCursor( pos );
1265 actors->UpdateText( pos );
1268 else if( btn == TStyle::ButtonID_Left )
1271 unsigned int nC = actors->m_SlicesCommands.size( );
1272 if( !alt && ctr && !sft && nC > 0 )
1274 for( unsigned int i = 0; i < nC; ++i )
1275 actors->m_SlicesCommands[ i ].first(
1276 pos, actors->GetAxis( ), actors->m_SlicesCommands[ i ].second
1283 else if( btn == TStyle::ButtonID_Right )
1285 if( !alt && !ctr && sft )
1287 // Change image window level
1289 actors->m_SliceMappers[ 0 ]->GetBounds( bounds );
1291 int a0 = actors->GetAxis( );
1292 int a1 = ( a0 + 1 ) % 3;
1293 int a2 = ( a0 + 2 ) % 3;
1294 double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1295 double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1296 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1297 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1299 dx *= actors->m_StartWindowLevel[ 0 ];
1300 dy *= actors->m_StartWindowLevel[ 1 ];
1301 dx += actors->m_StartWindowLevel[ 0 ];
1302 dy += actors->m_StartWindowLevel[ 1 ];
1303 actors->SetWindowLevel( dx, dy );
1306 // Associate objects
1307 auto i = actors->m_WindowLevelCommands.begin( );
1308 for( ; i != actors->m_WindowLevelCommands.end( ); ++i )
1309 i->first( dx, dy, i->second );
1316 // -------------------------------------------------------------------------
1317 void cpExtensions::Visualization::ImageSliceActors::
1319 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1320 bool alt, bool ctr, bool sft
1323 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1324 if( actors == NULL )
1327 actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1328 actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1329 actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1330 actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1331 actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1334 // -------------------------------------------------------------------------
1335 void cpExtensions::Visualization::ImageSliceActors::
1337 void* data, const int& dir,
1338 bool alt, bool ctr, bool sft
1341 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1342 if( actors == NULL )
1347 int slice = actors->GetSliceNumber( ) + ( dir * ( ( sft )? 10: 1 ) );
1348 if( slice < actors->GetSliceNumberMinValue( ) )
1349 slice = actors->GetSliceNumberMinValue( );
1350 if( slice > actors->GetSliceNumberMaxValue( ) )
1351 slice = actors->GetSliceNumberMaxValue( );
1352 actors->SetSliceNumber( slice );
1354 auto a = actors->m_AssociatedSlices.begin( );
1355 for( ; a != actors->m_AssociatedSlices.end( ); ++a )
1357 ( *a )->SetSliceNumber( slice );
1363 // Associate objects
1364 auto i = actors->m_RenderCommands.begin( );
1365 for( ; i != actors->m_RenderCommands.end( ); ++i )
1366 i->first( i->second );
1371 // -------------------------------------------------------------------------
1372 void cpExtensions::Visualization::ImageSliceActors::
1373 _KeyCommand( void* data, const char& key )
1375 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1376 if( actors == NULL )
1383 actors->ResetCamera( );
1386 // Associate objects
1387 auto i = actors->m_RenderCommands.begin( );
1388 for( ; i != actors->m_RenderCommands.end( ); ++i )
1389 i->first( i->second );
1394 actors->ResetWindowLevel( );
1397 // Associate objects
1398 auto i = actors->m_RenderCommands.begin( );
1399 for( ; i != actors->m_RenderCommands.end( ); ++i )
1400 i->first( i->second );