1 #include <cpExtensions/Visualization/ImageSliceActors.h>
6 #include <vtkAlgorithmOutput.h>
8 #include <vtkCellArray.h>
9 #include <vtkImageData.h>
10 #include <vtkInformation.h>
12 #include <vtkPoints.h>
13 #include <vtkProperty.h>
14 #include <vtkRenderer.h>
15 #include <vtkRendererCollection.h>
16 #include <vtkRenderWindow.h>
17 #include <vtkRenderWindowInteractor.h>
18 #include <vtkStreamingDemandDrivenPipeline.h>
19 #include <vtkTextProperty.h>
20 #include <vtkWindowLevelLookupTable.h>
22 // -------------------------------------------------------------------------
23 cpExtensions::Visualization::ImageSliceActors*
24 cpExtensions::Visualization::ImageSliceActors::
27 return( new Self( ) );
30 // -------------------------------------------------------------------------
31 void cpExtensions::Visualization::ImageSliceActors::
32 AddInputConnection( vtkAlgorithmOutput* aout, int axis )
34 // Get input vtkImageData
37 vtkAlgorithm* producer = aout->GetProducer( );
38 vtkImageData* data = dynamic_cast< vtkImageData* >(
39 producer->GetOutputDataObject( aout->GetIndex( ) )
44 // Try to infere if input comes from a color mapping filter
45 vtkImageMapToColors* new_map =
46 dynamic_cast< vtkImageMapToColors* >( producer );
49 // Configure LUT, if possible (NULL is returned if not)
50 this->_ConfigureNewLUT( data );
51 new_map = *( this->m_ImageMaps.rbegin( ) );
54 new_map->SetInputConnection( aout );
60 this->m_ImageMaps.push_back( new_map );
62 // Update window level values
66 dynamic_cast< vtkImageData* >( new_map->GetInput( ) )->
67 GetScalarRange( range );
68 this->m_MinWindow = double( 0 );
69 this->m_MaxWindow = range[ 1 ] - range[ 0 ];
70 this->m_MinLevel = range[ 0 ];
71 this->m_MaxLevel = range[ 1 ];
75 // Create mapper and actors
76 vtkSmartPointer< vtkImageSliceMapper > mapper =
77 vtkSmartPointer< vtkImageSliceMapper >::New( );
79 mapper->SetInputConnection( new_map->GetOutputPort( ) );
81 mapper->SetInputConnection( aout );
82 this->m_SliceMappers.push_back( mapper );
83 this->_ConfigureNewInput( axis );
86 // -------------------------------------------------------------------------
87 void cpExtensions::Visualization::ImageSliceActors::
88 AddInputData( vtkImageData* data, int axis )
90 // Update window level values
91 if( this->m_ImageMaps.size( ) == 0 )
94 data->GetScalarRange( range );
95 this->m_MinWindow = double( 0 );
96 this->m_MaxWindow = range[ 1 ] - range[ 0 ];
97 this->m_MinLevel = range[ 0 ];
98 this->m_MaxLevel = range[ 1 ];
102 // Configure LUT, if possible (NULL is returned if not)
103 this->_ConfigureNewLUT( data );
104 vtkImageMapToColors* new_map = *( this->m_ImageMaps.rbegin( ) );
105 if( new_map != NULL )
107 new_map->SetInputData( data );
112 // Create mapper and actors
113 vtkSmartPointer< vtkImageSliceMapper > mapper =
114 vtkSmartPointer< vtkImageSliceMapper >::New( );
115 if( new_map != NULL )
116 mapper->SetInputConnection( new_map->GetOutputPort( ) );
118 mapper->SetInputData( data );
119 this->m_SliceMappers.push_back( mapper );
120 this->_ConfigureNewInput( axis );
123 // -------------------------------------------------------------------------
124 void cpExtensions::Visualization::ImageSliceActors::
128 this->m_MinWindow = double( 0 );
129 this->m_MaxWindow = double( 0 );
130 this->m_MinLevel = double( 0 );
131 this->m_MaxLevel = double( 0 );
133 // Unbind from container
134 this->RemoveAllItems( );
137 this->m_ImageMaps.clear( );
138 this->m_SliceMappers.clear( );
139 this->m_ImageActors.clear( );
141 // Reconfigure unique objects
142 this->m_Cursor = vtkSmartPointer< vtkPolyData >::New( );
143 this->m_CursorMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
144 this->m_CursorActor = vtkSmartPointer< vtkActor >::New( );
145 this->m_Plane = vtkSmartPointer< vtkPolyData >::New( );
146 this->m_PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
147 this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
148 this->m_PlaneActor = vtkSmartPointer< vtkActor >::New( );
149 this->m_TextBuffer[ 0 ] = '\0';
151 // Unique objects configuration
152 vtkSmartPointer< vtkPoints > cursor_points =
153 vtkSmartPointer< vtkPoints >::New( );
154 vtkSmartPointer< vtkCellArray > cursor_lines =
155 vtkSmartPointer< vtkCellArray >::New( );
156 cursor_points->InsertNextPoint( 0, 0, 0 );
157 cursor_points->InsertNextPoint( 0, 0, 0 );
158 cursor_points->InsertNextPoint( 0, 0, 0 );
159 cursor_points->InsertNextPoint( 0, 0, 0 );
160 cursor_points->InsertNextPoint( 0, 0, 0 );
161 cursor_points->InsertNextPoint( 0, 0, 0 );
162 cursor_points->InsertNextPoint( 0, 0, 0 );
163 cursor_points->InsertNextPoint( 0, 0, 0 );
164 cursor_lines->InsertNextCell( 2 );
165 cursor_lines->InsertCellPoint( 0 );
166 cursor_lines->InsertCellPoint( 1 );
167 cursor_lines->InsertNextCell( 2 );
168 cursor_lines->InsertCellPoint( 2 );
169 cursor_lines->InsertCellPoint( 3 );
170 cursor_lines->InsertNextCell( 2 );
171 cursor_lines->InsertCellPoint( 4 );
172 cursor_lines->InsertCellPoint( 5 );
173 cursor_lines->InsertNextCell( 2 );
174 cursor_lines->InsertCellPoint( 6 );
175 cursor_lines->InsertCellPoint( 7 );
176 this->m_Cursor->SetPoints( cursor_points );
177 this->m_Cursor->SetLines( cursor_lines );
178 this->m_CursorMapper->SetInputData( this->m_Cursor );
179 this->m_CursorActor->SetMapper( this->m_CursorMapper );
181 vtkSmartPointer< vtkPoints > plane_points =
182 vtkSmartPointer< vtkPoints >::New( );
183 vtkSmartPointer< vtkCellArray > plane_lines =
184 vtkSmartPointer< vtkCellArray >::New( );
186 plane_points->InsertNextPoint( 0, 0, 0 );
187 plane_points->InsertNextPoint( 0, 1, 0 );
188 plane_points->InsertNextPoint( 1, 1, 0 );
189 plane_points->InsertNextPoint( 1, 0, 0 );
190 plane_lines->InsertNextCell( 5 );
191 plane_lines->InsertCellPoint( 0 );
192 plane_lines->InsertCellPoint( 1 );
193 plane_lines->InsertCellPoint( 2 );
194 plane_lines->InsertCellPoint( 3 );
195 plane_lines->InsertCellPoint( 0 );
196 this->m_Plane->SetPoints( plane_points );
197 this->m_Plane->SetLines( plane_lines );
199 this->m_PlaneMapper->SetInputData( this->m_Plane );
200 this->m_PlaneActor->SetMapper( this->m_PlaneMapper );
202 this->m_TextActor->SetTextScaleModeToNone( );
203 vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
204 textprop->SetColor( 1, 1, 1 );
205 textprop->SetFontFamilyToCourier( );
206 textprop->SetFontSize( 18 );
207 textprop->BoldOff( );
208 textprop->ItalicOff( );
209 textprop->ShadowOff( );
210 textprop->SetJustificationToLeft( );
211 textprop->SetVerticalJustificationToBottom( );
212 vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
213 coord->SetCoordinateSystemToNormalizedViewport( );
214 coord->SetValue( 0.01, 0.01 );
216 // Update actor collection
217 this->AddItem( this->m_CursorActor );
218 this->AddItem( this->m_TextActor );
219 this->AddItem( this->m_PlaneActor );
222 // -------------------------------------------------------------------------
223 void cpExtensions::Visualization::ImageSliceActors::
224 AssociateSlice( Self* slice )
226 this->m_AssociatedSlices.push_back( slice );
230 // -------------------------------------------------------------------------
231 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
234 return( this->m_Style.GetPointer( ) );
237 // -------------------------------------------------------------------------
238 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
241 return( this->m_Style.GetPointer( ) );
244 // -------------------------------------------------------------------------
245 vtkImageData* cpExtensions::Visualization::ImageSliceActors::
246 GetInputImage( unsigned int id )
248 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
249 vtkImageData* image = dynamic_cast< vtkImageData* >(
250 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
255 // -------------------------------------------------------------------------
256 const vtkImageData* cpExtensions::Visualization::ImageSliceActors::
257 GetInputImage( unsigned int id ) const
259 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
260 const vtkImageData* image = dynamic_cast< const vtkImageData* >(
261 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
266 // -------------------------------------------------------------------------
267 void cpExtensions::Visualization::ImageSliceActors::
268 PushActorsInto( vtkRenderWindow* window, bool force_style )
270 this->m_Window = window;
273 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
274 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
275 if( rwi == NULL || renderer == NULL )
279 if( this->m_Style.GetPointer( ) != NULL && force_style )
280 rwi->SetInteractorStyle( this->m_Style );
284 this->InitTraversal( );
285 while( prop = this->GetNextProp( ) )
286 renderer->AddViewProp( prop );
287 renderer->Modified( );
290 renderer->RemoveViewProp( this->m_CursorActor );
291 renderer->RemoveViewProp( this->m_TextActor );
296 vtkCamera* camera = renderer->GetActiveCamera( );
297 if( camera != NULL && force_style )
299 // Parallel projections are better when displaying 2D images
300 int axis = this->GetAxis( );
301 camera->ParallelProjectionOn( );
302 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
305 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
306 camera->SetViewUp ( double( 0 ), double( 0 ), double( 1 ) );
310 camera->SetPosition( double( 0 ), double( -1 ), double( 0 ) );
311 camera->SetViewUp ( double( 0 ), double( 0 ), double( 1 ) );
313 else // if( axis == 2 )
315 camera->SetPosition( double( 0 ), double( 0 ), double( -1 ) );
316 camera->SetViewUp ( double( 0 ), double( -1 ), double( 0 ) );
321 renderer->ResetCamera( );
325 // -------------------------------------------------------------------------
326 void cpExtensions::Visualization::ImageSliceActors::
327 PopActorsFrom( vtkRenderWindow* window )
329 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
330 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
332 if( renderer != NULL )
336 this->InitTraversal( );
337 while( prop = this->GetNextProp( ) )
338 renderer->RemoveViewProp( prop );
339 renderer->Modified( );
346 // -------------------------------------------------------------------------
347 unsigned int cpExtensions::Visualization::ImageSliceActors::
348 GetNumberOfImageActors( ) const
350 return( this->m_ImageActors.size( ) );
353 // -------------------------------------------------------------------------
354 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
355 GetImageActor( unsigned int id )
357 if( id < this->m_ImageActors.size( ) )
358 return( this->m_ImageActors[ id ] );
363 // -------------------------------------------------------------------------
364 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
365 GetImageActor( unsigned int id ) const
367 if( id < this->m_ImageActors.size( ) )
368 return( this->m_ImageActors[ id ] );
373 // -------------------------------------------------------------------------
374 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
377 return( this->m_TextActor );
380 // -------------------------------------------------------------------------
381 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
382 GetTextActor( ) const
384 return( this->m_TextActor );
387 // -------------------------------------------------------------------------
388 vtkActor* cpExtensions::Visualization::ImageSliceActors::
391 return( this->m_PlaneActor );
394 // -------------------------------------------------------------------------
395 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
396 GetPlaneActor( ) const
398 return( this->m_PlaneActor );
401 // -------------------------------------------------------------------------
402 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
405 return( this->m_SliceMappers[ 0 ]->GetSlicePlane( ) );
408 // -------------------------------------------------------------------------
409 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
410 GetPlaneFunction( ) const
412 return( this->m_SliceMappers[ 0 ]->GetSlicePlane( ) );
415 // -------------------------------------------------------------------------
416 void cpExtensions::Visualization::ImageSliceActors::
417 SetInterpolate( bool v )
419 if( this->m_Interpolate != v )
421 for( unsigned int i = 0; i < this->m_ImageActors.size( ); ++i )
422 this->m_ImageActors[ i ]->SetInterpolate( v );
423 this->m_Interpolate = v;
429 // -------------------------------------------------------------------------
430 void cpExtensions::Visualization::ImageSliceActors::
433 this->SetInterpolate( true );
436 // -------------------------------------------------------------------------
437 void cpExtensions::Visualization::ImageSliceActors::
440 this->SetInterpolate( false );
443 // -------------------------------------------------------------------------
444 double* cpExtensions::Visualization::ImageSliceActors::
445 GetDisplayBounds( ) const
447 if( this->m_ImageActors.size( ) > 0 )
448 return( this->m_ImageActors[ 0 ]->GetDisplayBounds( ) );
453 // -------------------------------------------------------------------------
454 void cpExtensions::Visualization::ImageSliceActors::
455 GetDisplayBounds( double bounds[ 6 ] ) const
457 if( this->m_ImageActors.size( ) == 0 )
459 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
460 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
463 this->m_ImageActors[ 0 ]->GetDisplayBounds( bounds );
466 // -------------------------------------------------------------------------
467 void cpExtensions::Visualization::ImageSliceActors::
470 if( this->m_SliceMappers.size( ) > 0 )
473 this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
474 double pos[] = { bounds[ 0 ], bounds[ 2 ], bounds[ 4 ] };
475 this->SetCursor( pos );
479 vtkPoints* points = this->m_Cursor->GetPoints( );
480 points->SetPoint( 0, 0, 0, 0 );
481 points->SetPoint( 1, 0, 0, 0 );
482 points->SetPoint( 2, 0, 0, 0 );
483 points->SetPoint( 3, 0, 0, 0 );
484 points->SetPoint( 4, 0, 0, 0 );
485 points->SetPoint( 5, 0, 0, 0 );
486 points->SetPoint( 6, 0, 0, 0 );
487 points->SetPoint( 7, 0, 0, 0 );
488 this->m_Cursor->Modified( );
489 this->m_CursorMapper->Modified( );
490 this->m_CursorActor->Modified( );
495 // -------------------------------------------------------------------------
496 void cpExtensions::Visualization::ImageSliceActors::
497 SetCursor( double pos[ 3 ] )
499 if( this->m_SliceMappers.size( ) == 0 )
503 int a0 = this->GetAxis( );
504 int a1 = ( a0 + 1 ) % 3;
505 int a2 = ( a0 + 2 ) % 3;
511 this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
514 p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
515 p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
517 p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
518 p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
519 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
520 p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
521 p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
522 p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
523 p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
524 p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
526 vtkPoints* points = this->m_Cursor->GetPoints( );
527 points->SetPoint( 0, p0 );
528 points->SetPoint( 1, p1 );
529 points->SetPoint( 2, p2 );
530 points->SetPoint( 3, p3 );
531 points->SetPoint( 4, p4 );
532 points->SetPoint( 5, p5 );
533 points->SetPoint( 6, p6 );
534 points->SetPoint( 7, p7 );
535 this->m_Cursor->Modified( );
536 this->m_CursorMapper->Modified( );
537 this->m_CursorActor->Modified( );
540 // -------------------------------------------------------------------------
541 vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
542 GetImageMap( unsigned int id )
544 if( id < this->m_ImageMaps.size( ) )
545 return( this->m_ImageMaps[ id ].GetPointer( ) );
550 // -------------------------------------------------------------------------
551 const vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
552 GetImageMap( unsigned int id ) const
554 if( id < this->m_ImageMaps.size( ) )
555 return( this->m_ImageMaps[ id ].GetPointer( ) );
560 // -------------------------------------------------------------------------
561 double cpExtensions::Visualization::ImageSliceActors::
562 GetMinWindow( ) const
564 return( this->m_MinWindow );
567 // -------------------------------------------------------------------------
568 double cpExtensions::Visualization::ImageSliceActors::
569 GetMaxWindow( ) const
571 return( this->m_MaxWindow );
574 // -------------------------------------------------------------------------
575 double cpExtensions::Visualization::ImageSliceActors::
578 return( this->m_MinLevel );
581 // -------------------------------------------------------------------------
582 double cpExtensions::Visualization::ImageSliceActors::
585 return( this->m_MaxLevel );
588 // -------------------------------------------------------------------------
589 double cpExtensions::Visualization::ImageSliceActors::
592 if( this->m_ImageMaps.size( ) == 0 )
593 return( double( 0 ) );
594 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
595 return( double( 0 ) );
597 vtkWindowLevelLookupTable* lut =
598 dynamic_cast< vtkWindowLevelLookupTable* >(
599 this->m_ImageMaps[ 0 ]->GetLookupTable( )
602 return( lut->GetWindow( ) );
604 return( double( 0 ) );
607 // -------------------------------------------------------------------------
608 double cpExtensions::Visualization::ImageSliceActors::
611 if( this->m_ImageMaps.size( ) == 0 )
612 return( double( 0 ) );
613 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
614 return( double( 0 ) );
616 vtkWindowLevelLookupTable* lut =
617 dynamic_cast< vtkWindowLevelLookupTable* >(
618 this->m_ImageMaps[ 0 ]->GetLookupTable( )
621 return( lut->GetLevel( ) );
623 return( double( 0 ) );
626 // -------------------------------------------------------------------------
627 void cpExtensions::Visualization::ImageSliceActors::
628 SetWindow( double w )
630 if( this->m_ImageMaps.size( ) == 0 )
632 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
634 vtkWindowLevelLookupTable* lut =
635 dynamic_cast< vtkWindowLevelLookupTable* >(
636 this->m_ImageMaps[ 0 ]->GetLookupTable( )
641 if( w < this->m_MinWindow )
642 w = this->m_MinWindow;
643 if( w > this->m_MaxWindow )
644 w = this->m_MaxWindow;
648 this->m_ImageMaps[ 0 ]->Modified( );
652 // -------------------------------------------------------------------------
653 void cpExtensions::Visualization::ImageSliceActors::
656 if( this->m_ImageMaps.size( ) == 0 )
658 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
660 vtkWindowLevelLookupTable* lut =
661 dynamic_cast< vtkWindowLevelLookupTable* >(
662 this->m_ImageMaps[ 0 ]->GetLookupTable( )
667 if( l < this->m_MinLevel )
668 l = this->m_MinLevel;
669 if( l > this->m_MaxLevel )
670 l = this->m_MaxLevel;
674 this->m_ImageMaps[ 0 ]->Modified( );
678 // -------------------------------------------------------------------------
679 void cpExtensions::Visualization::ImageSliceActors::
680 SetWindowLevel( double w, double l )
682 if( this->m_ImageMaps.size( ) == 0 )
684 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
686 vtkWindowLevelLookupTable* lut =
687 dynamic_cast< vtkWindowLevelLookupTable* >(
688 this->m_ImageMaps[ 0 ]->GetLookupTable( )
693 if( w < this->m_MinWindow )
694 w = this->m_MinWindow;
695 if( w > this->m_MaxWindow )
696 w = this->m_MaxWindow;
697 if( l < this->m_MinLevel )
698 l = this->m_MinLevel;
699 if( l > this->m_MaxLevel )
700 l = this->m_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 // Change visualization extent
842 for( unsigned int i = 0; i < nImages; ++i )
844 this->m_SliceMappers[ i ]->SetSliceNumber( slice );
845 this->m_SliceMappers[ i ]->Modified( );
846 this->m_ImageActors[ i ]->Modified( );
847 this->m_SliceMappers[ i ]->Update( );
852 this->m_SliceMappers[ 0 ]->GetBounds( bnds );
855 { bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] },
856 { bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] }
860 vtkPlane* plane = this->m_SliceMappers[ 0 ]->GetSlicePlane( );
861 plane->GeneralizedProjectPoint( x0[ 0 ], p0[ 0 ] );
862 plane->GeneralizedProjectPoint( x0[ 1 ], p0[ 1 ] );
864 int axis = this->GetAxis( );
865 bnds[ 0 ] = p0[ 0 ][ 0 ];
866 bnds[ 1 ] = p0[ 1 ][ 0 ];
867 bnds[ 2 ] = p0[ 0 ][ 1 ];
868 bnds[ 3 ] = p0[ 1 ][ 1 ];
869 bnds[ 4 ] = p0[ 0 ][ 2 ];
870 bnds[ 5 ] = p0[ 1 ][ 2 ];
872 // Configure visualization and implicit plane orientation
873 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
874 this->m_PlaneActor->GetProperty( )->SetLineWidth( 3 );
875 vtkPoints* plane_points = this->m_Plane->GetPoints( );
876 plane_points->SetPoint( 0, bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] );
877 plane_points->SetPoint( 2, bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] );
878 if( axis == 0 || axis == 2 ) // YZ, x-normal
880 plane_points->SetPoint( 1, bnds[ 1 ], bnds[ 2 ], bnds[ 5 ] );
881 plane_points->SetPoint( 3, bnds[ 0 ], bnds[ 3 ], bnds[ 4 ] );
883 this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
885 this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
887 else if( axis == 1 ) // ZX, y-normal
889 plane_points->SetPoint( 1, bnds[ 0 ], bnds[ 2 ], bnds[ 5 ] );
890 plane_points->SetPoint( 3, bnds[ 1 ], bnds[ 3 ], bnds[ 4 ] );
891 this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
894 this->m_Plane->Modified( );
895 this->m_PlaneMapper->Modified( );
896 this->m_PlaneActor->Modified( );
902 // -------------------------------------------------------------------------
903 void cpExtensions::Visualization::ImageSliceActors::
904 SetSlice( double* pos )
907 if( this->m_ImageMaps[ 0 ] != NULL )
909 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
911 image = this->m_SliceMappers[ 0 ]->GetInput( );
915 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
916 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
919 // -------------------------------------------------------------------------
920 void cpExtensions::Visualization::ImageSliceActors::
923 if( this->m_SliceMappers.size( ) > 0 )
926 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
927 if ( axId == 0 ) axis = 'X';
928 else if( axId == 1 ) axis = 'Y';
929 else if( axId == 2 ) axis = 'Z';
932 this->m_TextBuffer, "Axis: %c (%d)",
933 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( )
937 this->m_TextBuffer[ 0 ] = '\0';
938 this->m_TextActor->SetInput( this->m_TextBuffer );
939 this->m_TextActor->Modified( );
943 // -------------------------------------------------------------------------
944 void cpExtensions::Visualization::ImageSliceActors::
945 UpdateText( double pos[ 3 ] )
947 if( this->m_SliceMappers.size( ) > 0 )
950 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
951 if ( axId == 0 ) axis = 'X';
952 else if( axId == 1 ) axis = 'Y';
953 else if( axId == 2 ) axis = 'Z';
954 int slice = this->GetSliceNumber( );
957 if( this->m_ImageMaps[ 0 ] != NULL )
959 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
961 image = this->m_SliceMappers[ 0 ]->GetInput( );
965 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
969 image->GetExtent( ext );
971 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
972 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
973 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
976 int nScl = image->GetNumberOfScalarComponents( );
977 std::stringstream str;
981 << "," << ijk[ 2 ] << "]=(";
983 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
984 for( int n = 1; n < nScl; ++n )
987 << image->GetScalarComponentAsFloat(
988 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
992 this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
993 axis, slice, str.str( ).c_str( )
999 this->m_TextBuffer[ 0 ] = '\0';
1000 this->m_TextActor->SetInput( this->m_TextBuffer );
1001 this->m_TextActor->Modified( );
1005 // -------------------------------------------------------------------------
1006 void cpExtensions::Visualization::ImageSliceActors::
1007 UpdateText( const double& w, const double& l )
1009 if( this->m_SliceMappers.size( ) > 0 )
1012 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1013 if ( axId == 0 ) axis = 'X';
1014 else if( axId == 1 ) axis = 'Y';
1015 else if( axId == 2 ) axis = 'Z';
1018 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1019 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1023 this->m_TextBuffer[ 0 ] = '\0';
1024 this->m_TextActor->SetInput( this->m_TextBuffer );
1025 this->m_TextActor->Modified( );
1029 // -------------------------------------------------------------------------
1030 void cpExtensions::Visualization::ImageSliceActors::
1031 Render( const double& t )
1033 if( this->m_Window != NULL )
1035 vtkRenderer* renderer =
1036 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1037 if( renderer != NULL )
1038 renderer->SetAllocatedRenderTime( t );
1039 this->m_Window->Render( );
1044 // -------------------------------------------------------------------------
1045 void cpExtensions::Visualization::ImageSliceActors::
1048 if( this->m_Window == NULL )
1050 vtkRenderer* renderer =
1051 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1052 if( renderer != NULL )
1053 renderer->ResetCamera( );
1056 // -------------------------------------------------------------------------
1057 cpExtensions::Visualization::ImageSliceActors::
1061 m_Interpolate( false )
1064 this->_ConfigureStyle( );
1067 // -------------------------------------------------------------------------
1068 cpExtensions::Visualization::ImageSliceActors::
1069 ~ImageSliceActors( )
1073 // -------------------------------------------------------------------------
1074 void cpExtensions::Visualization::ImageSliceActors::
1077 // Connect this view with a controller
1078 this->m_Style = vtkSmartPointer< TStyle >::New( );
1079 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1080 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1081 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1082 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1083 this->m_Style->AddEnterCommand( Self::_EnterCommand, this );
1084 this->m_Style->AddLeaveCommand( Self::_LeaveCommand, this );
1087 // -------------------------------------------------------------------------
1088 void cpExtensions::Visualization::ImageSliceActors::
1089 _ConfigureNewLUT( vtkImageData* data )
1091 // Does the new LUT is a window-level one?
1092 unsigned int nImgs = this->m_ImageMaps.size( );
1093 unsigned int nCmps = data->GetNumberOfScalarComponents( );
1097 this->m_ImageMaps.push_back( NULL );
1102 // Create LUT filter
1103 this->m_ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1107 data->GetScalarRange( range );
1108 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1109 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1110 lut->SetScaleToLinear( );
1111 lut->SetTableRange( range );
1112 lut->SetWindow( range[ 1 ] - range[ 0 ] );
1113 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1115 this->m_ImageMaps[ 0 ]->SetLookupTable( lut );
1118 this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1121 // -------------------------------------------------------------------------
1122 void cpExtensions::Visualization::ImageSliceActors::
1123 _ConfigureNewInput( int axis )
1125 unsigned int nImages = this->m_ImageActors.size( );
1128 vtkImageSliceMapper* mapper = this->m_SliceMappers[ nImages ];
1130 mapper->SetOrientation( axis );
1132 mapper->SetOrientation( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
1136 vtkSmartPointer< vtkImageActor > actor =
1137 vtkSmartPointer< vtkImageActor >::New( );
1138 this->m_ImageActors.push_back( actor );
1139 actor->SetMapper( mapper );
1140 actor->SetInterpolate( this->m_Interpolate );
1144 this->m_Style->AssociateImageActor( actor );
1145 this->AddItem( actor );
1148 this->SetSliceNumber( this->GetSliceNumber( ) );
1150 this->SetSliceNumber( this->GetSliceNumberMaxValue( ) );
1151 this->ResetCursor( );
1155 // -------------------------------------------------------------------------
1156 void cpExtensions::Visualization::ImageSliceActors::
1158 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1159 bool alt, bool ctr, bool sft
1162 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1163 if( actors == NULL )
1166 if( btn == TStyle::ButtonID_None )
1168 // Just show the pixel information
1169 actors->SetCursor( pos );
1170 actors->UpdateText( pos );
1171 actors->Render( 1e-3 );
1173 else if( btn == TStyle::ButtonID_Left )
1175 if( !alt && ctr && !sft )
1177 // Interactively move slices
1178 auto i = actors->m_SlicesCommands.begin( );
1179 for( ; i != actors->m_SlicesCommands.end( ); ++i )
1180 i->first( pos, actors->GetAxis( ), i->second );
1181 actors->Render( 1e-3 );
1185 else if( btn == TStyle::ButtonID_Right )
1187 if( !alt && !ctr && sft )
1189 // Change image window level
1191 actors->m_SliceMappers[ 0 ]->GetBounds( bounds );
1193 int a0 = actors->GetAxis( );
1194 int a1 = ( a0 + 1 ) % 3;
1195 int a2 = ( a0 + 2 ) % 3;
1196 double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1197 double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1198 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1199 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1201 dx *= actors->m_StartWindowLevel[ 0 ];
1202 dy *= actors->m_StartWindowLevel[ 1 ];
1203 dx += actors->m_StartWindowLevel[ 0 ];
1204 dy += actors->m_StartWindowLevel[ 1 ];
1205 actors->SetWindowLevel( dx, dy );
1206 actors->Render( 1e-3 );
1208 // Associate objects
1209 auto i = actors->m_WindowLevelCommands.begin( );
1210 for( ; i != actors->m_WindowLevelCommands.end( ); ++i )
1211 i->first( dx, dy, i->second );
1218 // -------------------------------------------------------------------------
1219 void cpExtensions::Visualization::ImageSliceActors::
1221 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1222 bool alt, bool ctr, bool sft
1225 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1226 if( actors == NULL )
1229 actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1230 actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1231 actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1232 actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1233 actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1236 // -------------------------------------------------------------------------
1237 void cpExtensions::Visualization::ImageSliceActors::
1239 void* data, const int& dir,
1240 bool alt, bool ctr, bool sft
1243 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1244 if( actors == NULL )
1249 int slice = actors->GetSliceNumber( ) + ( dir * ( ( sft )? 10: 1 ) );
1250 if( slice < actors->GetSliceNumberMinValue( ) )
1251 slice = actors->GetSliceNumberMinValue( );
1252 if( slice > actors->GetSliceNumberMaxValue( ) )
1253 slice = actors->GetSliceNumberMaxValue( );
1254 actors->SetSliceNumber( slice );
1256 auto a = actors->m_AssociatedSlices.begin( );
1257 for( ; a != actors->m_AssociatedSlices.end( ); ++a )
1259 ( *a )->SetSliceNumber( slice );
1260 ( *a )->Render( 1e-3 );
1263 actors->Render( 1e-3 );
1265 // Associate objects
1266 auto i = actors->m_RenderCommands.begin( );
1267 for( ; i != actors->m_RenderCommands.end( ); ++i )
1268 i->first( i->second );
1273 // -------------------------------------------------------------------------
1274 void cpExtensions::Visualization::ImageSliceActors::
1275 _KeyCommand( void* data, const char& key )
1277 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1278 if( actors == NULL )
1285 actors->ResetCamera( );
1286 actors->Render( 1e-3 );
1288 // Associate objects
1289 auto i = actors->m_RenderCommands.begin( );
1290 for( ; i != actors->m_RenderCommands.end( ); ++i )
1291 i->first( i->second );
1296 actors->ResetWindowLevel( );
1297 actors->Render( 1e-3 );
1299 // Associate objects
1300 auto i = actors->m_RenderCommands.begin( );
1301 for( ; i != actors->m_RenderCommands.end( ); ++i )
1302 i->first( i->second );
1310 // -------------------------------------------------------------------------
1311 void cpExtensions::Visualization::ImageSliceActors::
1312 _EnterCommand( void* data )
1314 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1315 if( actors == NULL )
1318 actors->ResetCursor( );
1319 actors->m_CursorActor->VisibilityOn( );
1320 actors->Render( 1e-3 );
1323 // -------------------------------------------------------------------------
1324 void cpExtensions::Visualization::ImageSliceActors::
1325 _LeaveCommand( void* data )
1327 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1328 if( actors == NULL )
1331 actors->ResetCursor( );
1332 actors->m_CursorActor->VisibilityOff( );
1333 actors->Render( 1e-3 );