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 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
271 return( this->m_Style.GetPointer( ) );
274 // -------------------------------------------------------------------------
275 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
278 return( this->m_Style.GetPointer( ) );
281 // -------------------------------------------------------------------------
282 void cpExtensions::Visualization::ImageSliceActors::
283 PushActorsInto( vtkRenderWindow* window, bool force_style )
287 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
288 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
289 if( rwi == NULL || renderer == NULL )
293 if( this->m_Style.GetPointer( ) != NULL && force_style )
294 rwi->SetInteractorStyle( this->m_Style );
298 this->InitTraversal( );
299 while( prop = this->GetNextProp( ) )
300 renderer->AddViewProp( prop );
301 renderer->Modified( );
304 vtkCamera* camera = renderer->GetActiveCamera( );
305 if( camera != NULL && force_style )
307 // Parallel projections are better when displaying 2D images
308 int axis = this->GetAxis( );
309 camera->ParallelProjectionOn( );
310 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
313 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
314 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
318 camera->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
319 camera->SetViewUp ( double( 0 ), double( 0 ), double( -1 ) );
321 else // if( axis == 2 )
323 camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
324 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
329 renderer->ResetCamera( );
333 // -------------------------------------------------------------------------
334 void cpExtensions::Visualization::ImageSliceActors::
335 PopActorsFrom( vtkRenderWindow* window )
337 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
338 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
340 if( renderer != NULL )
344 this->InitTraversal( );
345 while( prop = this->GetNextProp( ) )
346 renderer->RemoveViewProp( prop );
347 renderer->Modified( );
354 // -------------------------------------------------------------------------
355 unsigned int cpExtensions::Visualization::ImageSliceActors::
356 GetNumberOfImageActors( ) const
358 return( this->m_ImageActors.size( ) );
361 // -------------------------------------------------------------------------
362 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
363 GetImageActor( unsigned int id )
365 if( id < this->m_ImageActors.size( ) )
366 return( this->m_ImageActors[ id ] );
371 // -------------------------------------------------------------------------
372 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
373 GetImageActor( unsigned int id ) const
375 if( id < this->m_ImageActors.size( ) )
376 return( this->m_ImageActors[ id ] );
381 // -------------------------------------------------------------------------
382 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
385 return( this->m_TextActor );
388 // -------------------------------------------------------------------------
389 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
390 GetTextActor( ) const
392 return( this->m_TextActor );
395 // -------------------------------------------------------------------------
396 vtkActor* cpExtensions::Visualization::ImageSliceActors::
399 return( this->m_PlaneActor );
402 // -------------------------------------------------------------------------
403 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
404 GetPlaneActor( ) const
406 return( this->m_PlaneActor );
409 // -------------------------------------------------------------------------
410 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
413 return( this->m_PlaneFunction );
416 // -------------------------------------------------------------------------
417 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
418 GetPlaneFunction( ) const
420 return( this->m_PlaneFunction );
423 // -------------------------------------------------------------------------
424 void cpExtensions::Visualization::ImageSliceActors::
425 SetInterpolate( bool v )
427 if( this->m_Interpolate != v )
429 for( unsigned int i = 0; i < this->m_ImageActors.size( ); ++i )
430 this->m_ImageActors[ i ]->SetInterpolate( v );
431 this->m_Interpolate = v;
437 // -------------------------------------------------------------------------
438 void cpExtensions::Visualization::ImageSliceActors::
441 this->SetInterpolate( true );
444 // -------------------------------------------------------------------------
445 void cpExtensions::Visualization::ImageSliceActors::
448 this->SetInterpolate( false );
451 // -------------------------------------------------------------------------
452 double* cpExtensions::Visualization::ImageSliceActors::
453 GetDisplayBounds( ) const
455 if( this->m_ImageActors.size( ) > 0 )
456 return( this->m_ImageActors[ 0 ]->GetDisplayBounds( ) );
461 // -------------------------------------------------------------------------
462 void cpExtensions::Visualization::ImageSliceActors::
463 GetDisplayBounds( double bounds[ 6 ] ) const
465 if( this->m_ImageActors.size( ) == 0 )
467 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
468 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
471 this->m_ImageActors[ 0 ]->GetDisplayBounds( bounds );
474 // -------------------------------------------------------------------------
475 void cpExtensions::Visualization::ImageSliceActors::
478 vtkPoints* points = this->m_Cursor->GetPoints( );
479 points->SetPoint( 0, 0, 0, 0 );
480 points->SetPoint( 1, 0, 0, 0 );
481 points->SetPoint( 2, 0, 0, 0 );
482 points->SetPoint( 3, 0, 0, 0 );
483 points->SetPoint( 4, 0, 0, 0 );
484 points->SetPoint( 5, 0, 0, 0 );
485 points->SetPoint( 6, 0, 0, 0 );
486 points->SetPoint( 7, 0, 0, 0 );
487 this->m_Cursor->Modified( );
488 this->m_CursorMapper->Modified( );
489 this->m_CursorActor->Modified( );
492 // -------------------------------------------------------------------------
493 void cpExtensions::Visualization::ImageSliceActors::
494 SetCursor( double pos[ 3 ] )
496 if( this->m_SliceMappers.size( ) == 0 )
500 int a0 = this->GetAxis( );
501 int a1 = ( a0 + 1 ) % 3;
502 int a2 = ( a0 + 2 ) % 3;
508 this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
511 p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
512 p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
514 p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
515 p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
516 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
517 p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
518 p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
519 p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
520 p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
521 p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
523 vtkPoints* points = this->m_Cursor->GetPoints( );
524 points->SetPoint( 0, p0 );
525 points->SetPoint( 1, p1 );
526 points->SetPoint( 2, p2 );
527 points->SetPoint( 3, p3 );
528 points->SetPoint( 4, p4 );
529 points->SetPoint( 5, p5 );
530 points->SetPoint( 6, p6 );
531 points->SetPoint( 7, p7 );
532 this->m_Cursor->Modified( );
533 this->m_CursorMapper->Modified( );
534 this->m_CursorActor->Modified( );
537 // -------------------------------------------------------------------------
538 vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
539 GetImageMap( unsigned int id )
541 if( id < this->m_ImageMaps.size( ) )
542 return( this->m_ImageMaps[ id ].GetPointer( ) );
547 // -------------------------------------------------------------------------
548 const vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
549 GetImageMap( unsigned int id ) const
551 if( id < this->m_ImageMaps.size( ) )
552 return( this->m_ImageMaps[ id ].GetPointer( ) );
557 // -------------------------------------------------------------------------
558 double cpExtensions::Visualization::ImageSliceActors::
559 GetMinWindow( ) const
561 return( this->m_MinWindow );
564 // -------------------------------------------------------------------------
565 double cpExtensions::Visualization::ImageSliceActors::
566 GetMaxWindow( ) const
568 return( this->m_MaxWindow );
571 // -------------------------------------------------------------------------
572 double cpExtensions::Visualization::ImageSliceActors::
575 return( this->m_MinLevel );
578 // -------------------------------------------------------------------------
579 double cpExtensions::Visualization::ImageSliceActors::
582 return( this->m_MaxLevel );
585 // -------------------------------------------------------------------------
586 double cpExtensions::Visualization::ImageSliceActors::
589 if( this->m_ImageMaps.size( ) == 0 )
590 return( double( 0 ) );
591 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
592 return( double( 0 ) );
594 vtkWindowLevelLookupTable* lut =
595 dynamic_cast< vtkWindowLevelLookupTable* >(
596 this->m_ImageMaps[ 0 ]->GetLookupTable( )
599 return( lut->GetWindow( ) );
601 return( double( 0 ) );
604 // -------------------------------------------------------------------------
605 double cpExtensions::Visualization::ImageSliceActors::
608 if( this->m_ImageMaps.size( ) == 0 )
609 return( double( 0 ) );
610 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
611 return( double( 0 ) );
613 vtkWindowLevelLookupTable* lut =
614 dynamic_cast< vtkWindowLevelLookupTable* >(
615 this->m_ImageMaps[ 0 ]->GetLookupTable( )
618 return( lut->GetLevel( ) );
620 return( double( 0 ) );
623 // -------------------------------------------------------------------------
624 void cpExtensions::Visualization::ImageSliceActors::
625 SetWindow( double w )
627 if( this->m_ImageMaps.size( ) == 0 )
629 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
631 vtkWindowLevelLookupTable* lut =
632 dynamic_cast< vtkWindowLevelLookupTable* >(
633 this->m_ImageMaps[ 0 ]->GetLookupTable( )
638 /* TODO: Min and Max values are assigned 0!!!
639 if( w < this->MinWindow )
641 if( w > this->MaxWindow )
646 this->m_ImageMaps[ 0 ]->Modified( );
650 // -------------------------------------------------------------------------
651 void cpExtensions::Visualization::ImageSliceActors::
654 if( this->m_ImageMaps.size( ) == 0 )
656 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
658 vtkWindowLevelLookupTable* lut =
659 dynamic_cast< vtkWindowLevelLookupTable* >(
660 this->m_ImageMaps[ 0 ]->GetLookupTable( )
665 /* TODO: Min and Max values are assigned 0!!!
666 if( l < this->MinLevel )
668 if( l > this->MaxLevel )
673 this->m_ImageMaps[ 0 ]->Modified( );
677 // -------------------------------------------------------------------------
678 void cpExtensions::Visualization::ImageSliceActors::
679 SetWindowLevel( double w, double l )
681 if( this->m_ImageMaps.size( ) == 0 )
683 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
685 vtkWindowLevelLookupTable* lut =
686 dynamic_cast< vtkWindowLevelLookupTable* >(
687 this->m_ImageMaps[ 0 ]->GetLookupTable( )
692 /* TODO: Min and Max values are assigned 0!!!
693 if( w < this->MinWindow )
695 if( w > this->MaxWindow )
697 if( l < this->MinLevel )
699 if( l > this->MaxLevel )
705 this->m_ImageMaps[ 0 ]->Modified( );
706 this->UpdateText( w, l );
710 // -------------------------------------------------------------------------
711 void cpExtensions::Visualization::ImageSliceActors::
714 static const double _2 = double( 2 );
715 this->SetWindowLevel(
716 this->m_MaxWindow, ( this->m_MaxLevel + this->m_MinLevel ) / _2
720 // -------------------------------------------------------------------------
721 void cpExtensions::Visualization::ImageSliceActors::
722 SetLookupTable( unsigned int id, vtkLookupTable* lut )
724 if( id < this->m_ImageMaps.size( ) && id > 0 )
726 if( this->m_ImageMaps[ id ].GetPointer( ) != NULL )
728 this->m_ImageMaps[ id ]->SetLookupTable( lut );
729 this->m_ImageMaps[ id ]->Modified( );
737 // -------------------------------------------------------------------------
738 void cpExtensions::Visualization::ImageSliceActors::
739 SetLookupTableAsColor( unsigned int id, double r, double g, double b )
741 static const double _0 = double( 0 );
742 static const double _2 = double( 2 );
743 static const double _4 = double( 4 );
744 static const double _6 = double( 6 );
745 static const double _OPACITY = double( 0.6 );
747 // Check ID consistency
748 if( id == 0 || id >= this->m_ImageMaps.size( ) )
751 // Get image scalar range
752 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
753 vtkImageData* image = dynamic_cast< vtkImageData* >(
754 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
757 image->GetScalarRange( range );
759 // Get HSV from display color
760 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
761 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
762 double d = cmax - cmin;
764 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
770 hue = std::fmod( ( g - b ) / d, _6 );
772 hue = ( ( b - r ) / d ) + _2;
774 hue = ( ( r - g ) / d ) + _4;
779 // Define new lookup table
780 vtkSmartPointer< vtkLookupTable > lut =
781 vtkSmartPointer< vtkLookupTable >::New( );
782 lut->SetScaleToLinear( );
783 lut->SetNanColor( _0, _0, _0, _0 );
784 lut->SetTableRange( range[ 0 ], range[ 1 ] );
785 lut->SetAlphaRange( _0, _OPACITY );
786 lut->SetHueRange( _0, hue );
787 lut->SetSaturationRange( _0, saturation );
788 lut->SetValueRange( _0, value );
790 this->SetLookupTable( id, lut );
793 // -------------------------------------------------------------------------
794 int cpExtensions::Visualization::ImageSliceActors::
797 if( this->m_SliceMappers.size( ) > 0 )
798 return( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
803 // -------------------------------------------------------------------------
804 int cpExtensions::Visualization::ImageSliceActors::
805 GetSliceNumber( ) const
807 if( this->m_SliceMappers.size( ) > 0 )
808 return( this->m_SliceMappers[ 0 ]->GetSliceNumber( ) );
813 // -------------------------------------------------------------------------
814 int cpExtensions::Visualization::ImageSliceActors::
815 GetSliceNumberMinValue( ) const
817 if( this->m_SliceMappers.size( ) > 0 )
818 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
823 // -------------------------------------------------------------------------
824 int cpExtensions::Visualization::ImageSliceActors::
825 GetSliceNumberMaxValue( ) const
827 if( this->m_SliceMappers.size( ) > 0 )
828 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
833 // -------------------------------------------------------------------------
834 void cpExtensions::Visualization::ImageSliceActors::
835 SetSliceNumber( const int& slice )
837 unsigned int nImages = this->m_SliceMappers.size( );
841 // Compute plane properties
842 int axis = this->m_SliceMappers[ 0 ]->GetOrientation( );
843 vtkAlgorithm* algo = this->m_SliceMappers[ 0 ]->GetInputAlgorithm( );
844 vtkInformation* info = algo->GetOutputInformation( 0 );
846 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
847 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
848 info->Get( vtkDataObject::ORIGIN( ), ori );
849 info->Get( vtkDataObject::SPACING( ), spac );
850 this->m_SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
852 // Update display extent
859 d_ext[ axis << 1 ] = slice;
860 d_ext[ ( axis << 1 ) + 1 ] = slice;
862 // Change visualization extent
863 for( unsigned int i = 0; i < nImages; ++i )
865 this->m_SliceMappers[ i ]->SetSliceNumber( slice );
866 this->m_ImageActors[ i ]->SetDisplayExtent( d_ext );
867 this->m_SliceMappers[ i ]->Modified( );
868 this->m_ImageActors[ i ]->Modified( );
869 this->m_SliceMappers[ i ]->Update( );
873 // Prevent obscuring voxels by offsetting the plane geometry
876 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
877 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
881 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
882 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
886 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
887 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
890 if( spac[ 0 ] < double( 0 ) )
892 double t = xbnds[ 0 ];
893 xbnds[ 0 ] = xbnds[ 1 ];
897 if( spac[ 1 ] < double( 0 ) )
899 double t = ybnds[ 0 ];
900 ybnds[ 0 ] = ybnds[ 1 ];
904 if( spac[ 2 ] < double( 0 ) )
906 double t = zbnds[ 0 ];
907 zbnds[ 0 ] = zbnds[ 1 ];
912 // Plane function origin
913 this->m_PlaneFunction->SetOrigin( pos );
915 // Configure visualization and implicit plane orientation
916 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
917 this->m_PlaneActor->GetProperty( )->SetLineWidth( 2 );
918 vtkPoints* plane_points = this->m_Plane->GetPoints( );
919 if( axis == 0 ) // YZ, x-normal
921 this->m_PlaneFunction->SetNormal( 1, 0, 0 );
922 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
923 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
924 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
925 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
926 this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
928 else if( axis == 1 ) // ZX, y-normal
930 this->m_PlaneFunction->SetNormal( 0, 1, 0 );
931 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
932 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
933 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
934 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
935 this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
939 this->m_PlaneFunction->SetNormal( 0, 0, 1 );
940 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
941 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
942 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
943 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
944 this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
947 this->m_PlaneFunction->Modified( );
948 this->m_Plane->Modified( );
949 this->m_PlaneMapper->Modified( );
950 this->m_PlaneActor->Modified( );
956 // -------------------------------------------------------------------------
957 void cpExtensions::Visualization::ImageSliceActors::
958 SetSlice( double* pos )
961 if( this->m_ImageMaps[ 0 ] != NULL )
963 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
965 image = this->m_SliceMappers[ 0 ]->GetInput( );
969 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
970 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
973 // -------------------------------------------------------------------------
974 void cpExtensions::Visualization::ImageSliceActors::
977 if( this->m_SliceMappers.size( ) > 0 )
980 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
981 if ( axId == 0 ) axis = 'X';
982 else if( axId == 1 ) axis = 'Y';
983 else if( axId == 2 ) axis = 'Z';
986 this->m_TextBuffer, "Axis: %c (%d)",
987 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( )
991 this->m_TextBuffer[ 0 ] = '\0';
992 this->m_TextActor->SetInput( this->m_TextBuffer );
993 this->m_TextActor->Modified( );
997 // -------------------------------------------------------------------------
998 void cpExtensions::Visualization::ImageSliceActors::
999 UpdateText( double pos[ 3 ] )
1001 if( this->m_SliceMappers.size( ) > 0 )
1004 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1005 if ( axId == 0 ) axis = 'X';
1006 else if( axId == 1 ) axis = 'Y';
1007 else if( axId == 2 ) axis = 'Z';
1008 int slice = this->GetSliceNumber( );
1010 vtkImageData* image;
1011 if( this->m_ImageMaps[ 0 ] != NULL )
1013 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
1015 image = this->m_SliceMappers[ 0 ]->GetInput( );
1018 double pcoords[ 3 ];
1019 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1020 ijk[ axId ] = slice;
1023 image->GetExtent( ext );
1025 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
1026 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
1027 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
1030 int nScl = image->GetNumberOfScalarComponents( );
1031 std::stringstream str;
1035 << "," << ijk[ 2 ] << "]=(";
1037 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
1038 for( int n = 1; n < nScl; ++n )
1041 << image->GetScalarComponentAsFloat(
1042 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
1046 this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
1047 axis, slice, str.str( ).c_str( )
1053 this->m_TextBuffer[ 0 ] = '\0';
1054 this->m_TextActor->SetInput( this->m_TextBuffer );
1055 this->m_TextActor->Modified( );
1059 // -------------------------------------------------------------------------
1060 void cpExtensions::Visualization::ImageSliceActors::
1061 UpdateText( const double& w, const double& l )
1063 if( this->m_SliceMappers.size( ) > 0 )
1066 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1067 if ( axId == 0 ) axis = 'X';
1068 else if( axId == 1 ) axis = 'Y';
1069 else if( axId == 2 ) axis = 'Z';
1072 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1073 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1077 this->m_TextBuffer[ 0 ] = '\0';
1078 this->m_TextActor->SetInput( this->m_TextBuffer );
1079 this->m_TextActor->Modified( );
1083 // -------------------------------------------------------------------------
1084 void cpExtensions::Visualization::ImageSliceActors::
1087 vtkInteractorStyle* style = this->GetStyle( );
1090 vtkRenderWindowInteractor* rwi = style->GetInteractor( );
1095 // -------------------------------------------------------------------------
1096 void cpExtensions::Visualization::ImageSliceActors::
1099 vtkInteractorStyle* style = this->GetStyle( );
1102 vtkRenderWindowInteractor* rwi = style->GetInteractor( );
1105 vtkRenderWindow* rw = rwi->GetRenderWindow( );
1108 vtkRenderer* renderer = rw->GetRenderers( )->GetFirstRenderer( );
1109 if( renderer != NULL )
1110 renderer->ResetCamera( );
1113 // -------------------------------------------------------------------------
1114 cpExtensions::Visualization::ImageSliceActors::
1117 m_Interpolate( false )
1120 this->_ConfigureStyle( );
1123 // -------------------------------------------------------------------------
1124 cpExtensions::Visualization::ImageSliceActors::
1125 ~ImageSliceActors( )
1129 // -------------------------------------------------------------------------
1130 void cpExtensions::Visualization::ImageSliceActors::
1133 // Connect this view with a controller
1134 this->m_Style = vtkSmartPointer< TStyle >::New( );
1135 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1136 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1137 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1138 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1141 // -------------------------------------------------------------------------
1142 void cpExtensions::Visualization::ImageSliceActors::
1143 _ConfigureNewLUT( vtkImageData* data )
1145 // Does the new LUT is a window-level one?
1146 unsigned int nImgs = this->m_ImageMaps.size( );
1147 unsigned int nCmps = data->GetNumberOfScalarComponents( );
1151 this->m_ImageMaps.push_back( NULL );
1156 // Create LUT filter
1157 this->m_ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1161 data->GetScalarRange( range );
1162 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1163 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1164 lut->SetScaleToLinear( );
1165 lut->SetTableRange( range );
1166 lut->SetWindow( range[ 1 ] - range[ 0 ] );
1167 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1169 this->m_ImageMaps[ 0 ]->SetLookupTable( lut );
1171 this->m_MinWindow = double( 0 );
1172 this->m_MaxWindow = range[ 1 ] - range[ 0 ];
1173 this->m_MinLevel = range[ 0 ];
1174 this->m_MaxLevel = range[ 1 ];
1177 this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1180 // -------------------------------------------------------------------------
1181 void cpExtensions::Visualization::ImageSliceActors::
1182 _ConfigureNewInput( int axis )
1184 unsigned int nImages = this->m_ImageActors.size( );
1187 vtkImageSliceMapper* mapper = this->m_SliceMappers[ nImages ];
1189 mapper->SetOrientation( axis );
1191 mapper->SetOrientation( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
1195 vtkSmartPointer< vtkImageActor > actor =
1196 vtkSmartPointer< vtkImageActor >::New( );
1197 this->m_ImageActors.push_back( actor );
1198 actor->SetMapper( mapper );
1199 actor->SetInterpolate( this->m_Interpolate );
1203 this->m_Style->AssociateImageActor( actor );
1204 this->AddItem( actor );
1207 this->SetSliceNumber( this->GetSliceNumber( ) );
1209 this->SetSliceNumber( this->GetSliceNumberMaxValue( ) );
1213 // -------------------------------------------------------------------------
1214 void cpExtensions::Visualization::ImageSliceActors::
1216 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1217 bool alt, bool ctr, bool sft
1220 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1221 if( actors == NULL )
1224 if( btn == TStyle::ButtonID_None )
1226 // Just show the pixel information
1227 actors->SetCursor( pos );
1228 actors->UpdateText( pos );
1231 else if( btn == TStyle::ButtonID_Left )
1234 unsigned int nC = actors->m_SlicesCommands.size( );
1235 if( !alt && ctr && !sft && nC > 0 )
1237 for( unsigned int i = 0; i < nC; ++i )
1238 actors->m_SlicesCommands[ i ].first(
1239 pos, actors->GetAxis( ), actors->m_SlicesCommands[ i ].second
1246 else if( btn == TStyle::ButtonID_Right )
1248 if( !alt && !ctr && sft )
1250 // Change image window level
1252 actors->m_SliceMappers[ 0 ]->GetBounds( bounds );
1254 int a0 = actors->GetAxis( );
1255 int a1 = ( a0 + 1 ) % 3;
1256 int a2 = ( a0 + 2 ) % 3;
1257 double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1258 double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1259 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1260 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1262 dx *= actors->m_StartWindowLevel[ 0 ];
1263 dy *= actors->m_StartWindowLevel[ 1 ];
1264 dx += actors->m_StartWindowLevel[ 0 ];
1265 dy += actors->m_StartWindowLevel[ 1 ];
1266 actors->SetWindowLevel( dx, dy );
1269 // Associate objects
1270 auto i = actors->m_WindowLevelCommands.begin( );
1271 for( ; i != actors->m_WindowLevelCommands.end( ); ++i )
1272 i->first( dx, dy, i->second );
1279 // -------------------------------------------------------------------------
1280 void cpExtensions::Visualization::ImageSliceActors::
1282 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1283 bool alt, bool ctr, bool sft
1286 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1287 if( actors == NULL )
1290 actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1291 actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1292 actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1293 actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1294 actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1297 // -------------------------------------------------------------------------
1298 void cpExtensions::Visualization::ImageSliceActors::
1300 void* data, const int& dir,
1301 bool alt, bool ctr, bool sft
1304 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1305 if( actors == NULL )
1310 int slice = actors->GetSliceNumber( ) + ( dir * ( ( sft )? 10: 1 ) );
1311 if( slice < actors->GetSliceNumberMinValue( ) )
1312 slice = actors->GetSliceNumberMinValue( );
1313 if( slice > actors->GetSliceNumberMaxValue( ) )
1314 slice = actors->GetSliceNumberMaxValue( );
1315 actors->SetSliceNumber( slice );
1318 // Associate objects
1319 auto i = actors->m_RenderCommands.begin( );
1320 for( ; i != actors->m_RenderCommands.end( ); ++i )
1321 i->first( i->second );
1326 // -------------------------------------------------------------------------
1327 void cpExtensions::Visualization::ImageSliceActors::
1328 _KeyCommand( void* data, const char& key )
1330 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1331 if( actors == NULL )
1338 actors->ResetCamera( );
1341 // Associate objects
1342 auto i = actors->m_RenderCommands.begin( );
1343 for( ; i != actors->m_RenderCommands.end( ); ++i )
1344 i->first( i->second );
1349 actors->ResetWindowLevel( );
1352 // Associate objects
1353 auto i = actors->m_RenderCommands.begin( );
1354 for( ; i != actors->m_RenderCommands.end( ); ++i )
1355 i->first( i->second );