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 this->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 int axis = this->GetAxis( );
843 for( unsigned int i = 0; i < nImages; ++i )
845 // Update mappers and display bounds
846 this->m_SliceMappers[ i ]->SetSliceNumber( slice );
847 this->m_SliceMappers[ i ]->Modified( );
848 this->m_ImageActors[ i ]->Modified( );
849 this->m_SliceMappers[ i ]->Update( );
851 // Update display extent (this isn't done automatically)
852 vtkImageData* image = this->m_SliceMappers[ i ]->GetInput( );
854 image->GetExtent( ext );
855 ext[ axis << 1 ] = slice;
856 ext[ ( axis << 1 ) + 1 ] = slice;
857 this->m_ImageActors[ i ]->SetDisplayExtent( ext );
862 this->m_SliceMappers[ 0 ]->GetBounds( bnds );
865 { bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] },
866 { bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] }
870 vtkPlane* plane = this->m_SliceMappers[ 0 ]->GetSlicePlane( );
871 plane->GeneralizedProjectPoint( x0[ 0 ], p0[ 0 ] );
872 plane->GeneralizedProjectPoint( x0[ 1 ], p0[ 1 ] );
874 bnds[ 0 ] = p0[ 0 ][ 0 ];
875 bnds[ 1 ] = p0[ 1 ][ 0 ];
876 bnds[ 2 ] = p0[ 0 ][ 1 ];
877 bnds[ 3 ] = p0[ 1 ][ 1 ];
878 bnds[ 4 ] = p0[ 0 ][ 2 ];
879 bnds[ 5 ] = p0[ 1 ][ 2 ];
881 // Configure visualization and implicit plane orientation
882 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
883 this->m_PlaneActor->GetProperty( )->SetLineWidth( 3 );
884 vtkPoints* plane_points = this->m_Plane->GetPoints( );
885 plane_points->SetPoint( 0, bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] );
886 plane_points->SetPoint( 2, bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] );
887 if( axis == 0 || axis == 2 ) // YZ, x-normal
889 plane_points->SetPoint( 1, bnds[ 1 ], bnds[ 2 ], bnds[ 5 ] );
890 plane_points->SetPoint( 3, bnds[ 0 ], bnds[ 3 ], bnds[ 4 ] );
892 this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
894 this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
896 else if( axis == 1 ) // ZX, y-normal
898 plane_points->SetPoint( 1, bnds[ 0 ], bnds[ 2 ], bnds[ 5 ] );
899 plane_points->SetPoint( 3, bnds[ 1 ], bnds[ 3 ], bnds[ 4 ] );
900 this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
903 this->m_Plane->Modified( );
904 this->m_PlaneMapper->Modified( );
905 this->m_PlaneActor->Modified( );
911 // -------------------------------------------------------------------------
912 void cpExtensions::Visualization::ImageSliceActors::
913 SetSlice( double* pos )
916 if( this->m_ImageMaps[ 0 ] != NULL )
918 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
920 image = this->m_SliceMappers[ 0 ]->GetInput( );
924 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
925 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
928 // -------------------------------------------------------------------------
929 void cpExtensions::Visualization::ImageSliceActors::
932 if( this->m_SliceMappers.size( ) > 0 )
935 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
936 if ( axId == 0 ) axis = 'X';
937 else if( axId == 1 ) axis = 'Y';
938 else if( axId == 2 ) axis = 'Z';
941 this->m_TextBuffer, "Axis: %c (%d)",
942 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( )
946 this->m_TextBuffer[ 0 ] = '\0';
947 this->m_TextActor->SetInput( this->m_TextBuffer );
948 this->m_TextActor->Modified( );
952 // -------------------------------------------------------------------------
953 void cpExtensions::Visualization::ImageSliceActors::
954 UpdateText( double pos[ 3 ] )
956 if( this->m_SliceMappers.size( ) > 0 )
959 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
960 if ( axId == 0 ) axis = 'X';
961 else if( axId == 1 ) axis = 'Y';
962 else if( axId == 2 ) axis = 'Z';
963 int slice = this->GetSliceNumber( );
966 if( this->m_ImageMaps[ 0 ] != NULL )
968 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
970 image = this->m_SliceMappers[ 0 ]->GetInput( );
974 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
978 image->GetExtent( ext );
980 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
981 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
982 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
985 int nScl = image->GetNumberOfScalarComponents( );
986 std::stringstream str;
990 << "," << ijk[ 2 ] << "]=(";
992 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
993 for( int n = 1; n < nScl; ++n )
996 << image->GetScalarComponentAsFloat(
997 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
1001 this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
1002 axis, slice, str.str( ).c_str( )
1008 this->m_TextBuffer[ 0 ] = '\0';
1009 this->m_TextActor->SetInput( this->m_TextBuffer );
1010 this->m_TextActor->Modified( );
1014 // -------------------------------------------------------------------------
1015 void cpExtensions::Visualization::ImageSliceActors::
1016 UpdateText( const double& w, const double& l )
1018 if( this->m_SliceMappers.size( ) > 0 )
1021 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1022 if ( axId == 0 ) axis = 'X';
1023 else if( axId == 1 ) axis = 'Y';
1024 else if( axId == 2 ) axis = 'Z';
1027 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1028 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1032 this->m_TextBuffer[ 0 ] = '\0';
1033 this->m_TextActor->SetInput( this->m_TextBuffer );
1034 this->m_TextActor->Modified( );
1038 // -------------------------------------------------------------------------
1039 void cpExtensions::Visualization::ImageSliceActors::
1040 Render( const double& t )
1042 if( this->m_Window != NULL )
1044 vtkRenderer* renderer =
1045 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1046 if( renderer != NULL )
1047 renderer->SetAllocatedRenderTime( t );
1048 this->m_Window->Render( );
1053 // -------------------------------------------------------------------------
1054 void cpExtensions::Visualization::ImageSliceActors::
1057 if( this->m_Window == NULL )
1059 vtkRenderer* renderer =
1060 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1061 if( renderer != NULL )
1062 renderer->ResetCamera( );
1063 #error ACA VOY: reconfigurar la camara alrededor únicamente del plano.
1066 // -------------------------------------------------------------------------
1067 cpExtensions::Visualization::ImageSliceActors::
1071 m_Interpolate( false )
1074 this->_ConfigureStyle( );
1077 // -------------------------------------------------------------------------
1078 cpExtensions::Visualization::ImageSliceActors::
1079 ~ImageSliceActors( )
1083 // -------------------------------------------------------------------------
1084 void cpExtensions::Visualization::ImageSliceActors::
1087 // Connect this view with a controller
1088 this->m_Style = vtkSmartPointer< TStyle >::New( );
1089 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1090 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1091 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1092 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1093 this->m_Style->AddEnterCommand( Self::_EnterCommand, this );
1094 this->m_Style->AddLeaveCommand( Self::_LeaveCommand, this );
1097 // -------------------------------------------------------------------------
1098 void cpExtensions::Visualization::ImageSliceActors::
1099 _ConfigureNewLUT( vtkImageData* data )
1101 // Does the new LUT is a window-level one?
1102 unsigned int nImgs = this->m_ImageMaps.size( );
1103 unsigned int nCmps = data->GetNumberOfScalarComponents( );
1107 this->m_ImageMaps.push_back( NULL );
1112 // Create LUT filter
1113 this->m_ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1117 data->GetScalarRange( range );
1118 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1119 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1120 lut->SetScaleToLinear( );
1121 lut->SetTableRange( range );
1122 lut->SetWindow( range[ 1 ] - range[ 0 ] );
1123 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1125 this->m_ImageMaps[ 0 ]->SetLookupTable( lut );
1128 this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1131 // -------------------------------------------------------------------------
1132 void cpExtensions::Visualization::ImageSliceActors::
1133 _ConfigureNewInput( int axis )
1135 unsigned int nImages = this->m_ImageActors.size( );
1138 vtkImageSliceMapper* mapper = this->m_SliceMappers[ nImages ];
1140 mapper->SetOrientation( axis );
1142 mapper->SetOrientation( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
1146 vtkSmartPointer< vtkImageActor > actor =
1147 vtkSmartPointer< vtkImageActor >::New( );
1148 this->m_ImageActors.push_back( actor );
1149 actor->SetMapper( mapper );
1150 actor->SetInterpolate( this->m_Interpolate );
1154 this->m_Style->AssociateImageActor( actor );
1155 this->AddItem( actor );
1158 this->SetSliceNumber( this->GetSliceNumber( ) );
1160 this->SetSliceNumber( this->GetSliceNumberMaxValue( ) );
1161 this->ResetCursor( );
1165 // -------------------------------------------------------------------------
1166 void cpExtensions::Visualization::ImageSliceActors::
1168 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1169 bool alt, bool ctr, bool sft
1172 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1173 if( actors == NULL )
1176 if( btn == TStyle::ButtonID_None )
1178 // Just show the pixel information
1179 actors->SetCursor( pos );
1180 actors->UpdateText( pos );
1181 actors->Render( 1e-3 );
1183 else if( btn == TStyle::ButtonID_Left )
1185 if( !alt && ctr && !sft )
1187 // Interactively move slices
1188 auto i = actors->m_SlicesCommands.begin( );
1189 for( ; i != actors->m_SlicesCommands.end( ); ++i )
1190 i->first( pos, actors->GetAxis( ), i->second );
1191 actors->Render( 1e-3 );
1195 else if( btn == TStyle::ButtonID_Right )
1197 if( !alt && !ctr && sft )
1199 // Change image window level
1201 actors->m_SliceMappers[ 0 ]->GetBounds( bounds );
1203 int a0 = actors->GetAxis( );
1204 int a1 = ( a0 + 1 ) % 3;
1205 int a2 = ( a0 + 2 ) % 3;
1206 double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1207 double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1208 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1209 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1211 dx *= actors->m_StartWindowLevel[ 0 ];
1212 dy *= actors->m_StartWindowLevel[ 1 ];
1213 dx += actors->m_StartWindowLevel[ 0 ];
1214 dy += actors->m_StartWindowLevel[ 1 ];
1215 actors->SetWindowLevel( dx, dy );
1216 actors->Render( 1e-3 );
1218 // Associate objects
1219 auto i = actors->m_WindowLevelCommands.begin( );
1220 for( ; i != actors->m_WindowLevelCommands.end( ); ++i )
1221 i->first( dx, dy, i->second );
1228 // -------------------------------------------------------------------------
1229 void cpExtensions::Visualization::ImageSliceActors::
1231 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1232 bool alt, bool ctr, bool sft
1235 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1236 if( actors == NULL )
1239 actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1240 actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1241 actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1242 actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1243 actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1246 // -------------------------------------------------------------------------
1247 void cpExtensions::Visualization::ImageSliceActors::
1249 void* data, const int& dir,
1250 bool alt, bool ctr, bool sft
1253 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1254 if( actors == NULL )
1259 int slice = actors->GetSliceNumber( ) + ( dir * ( ( sft )? 10: 1 ) );
1260 if( slice < actors->GetSliceNumberMinValue( ) )
1261 slice = actors->GetSliceNumberMinValue( );
1262 if( slice > actors->GetSliceNumberMaxValue( ) )
1263 slice = actors->GetSliceNumberMaxValue( );
1264 actors->SetSliceNumber( slice );
1266 auto a = actors->m_AssociatedSlices.begin( );
1267 for( ; a != actors->m_AssociatedSlices.end( ); ++a )
1269 ( *a )->SetSliceNumber( slice );
1270 ( *a )->Render( 1e-3 );
1273 actors->Render( 1e-3 );
1275 // Associate objects
1276 auto i = actors->m_RenderCommands.begin( );
1277 for( ; i != actors->m_RenderCommands.end( ); ++i )
1278 i->first( i->second );
1283 // -------------------------------------------------------------------------
1284 void cpExtensions::Visualization::ImageSliceActors::
1285 _KeyCommand( void* data, const char& key )
1287 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1288 if( actors == NULL )
1295 actors->ResetCamera( );
1296 actors->Render( 1e-3 );
1298 // Associate objects
1299 auto i = actors->m_RenderCommands.begin( );
1300 for( ; i != actors->m_RenderCommands.end( ); ++i )
1301 i->first( i->second );
1306 actors->ResetWindowLevel( );
1307 actors->Render( 1e-3 );
1309 // Associate objects
1310 auto i = actors->m_RenderCommands.begin( );
1311 for( ; i != actors->m_RenderCommands.end( ); ++i )
1312 i->first( i->second );
1320 // -------------------------------------------------------------------------
1321 void cpExtensions::Visualization::ImageSliceActors::
1322 _EnterCommand( void* data )
1324 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1325 if( actors == NULL )
1328 actors->ResetCursor( );
1329 actors->m_CursorActor->VisibilityOn( );
1330 actors->Render( 1e-3 );
1333 // -------------------------------------------------------------------------
1334 void cpExtensions::Visualization::ImageSliceActors::
1335 _LeaveCommand( void* data )
1337 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1338 if( actors == NULL )
1341 actors->ResetCursor( );
1342 actors->m_CursorActor->VisibilityOff( );
1343 actors->Render( 1e-3 );