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 AddCursorCommand( TCursorCommand command, void* data )
34 this->m_CursorCommands.push_back(
35 std::pair< TCursorCommand, void* >( command, data )
40 // -------------------------------------------------------------------------
41 void cpExtensions::Visualization::ImageSliceActors::
42 AddInputConnection( vtkAlgorithmOutput* aout, int axis )
44 // Get input vtkImageData
47 vtkAlgorithm* producer = aout->GetProducer( );
48 vtkImageData* data = dynamic_cast< vtkImageData* >(
49 producer->GetOutputDataObject( aout->GetIndex( ) )
54 // Try to infere if input comes from a color mapping filter
55 vtkImageMapToColors* new_map =
56 dynamic_cast< vtkImageMapToColors* >( producer );
59 // Configure LUT, if possible (NULL is returned if not)
60 this->_ConfigureNewLUT( data );
61 new_map = *( this->m_ImageMaps.rbegin( ) );
64 new_map->SetInputConnection( aout );
70 this->m_ImageMaps.push_back( new_map );
72 // Create mapper and actors
73 vtkSmartPointer< vtkImageSliceMapper > mapper =
74 vtkSmartPointer< vtkImageSliceMapper >::New( );
76 mapper->SetInputConnection( new_map->GetOutputPort( ) );
78 mapper->SetInputConnection( aout );
79 this->m_SliceMappers.push_back( mapper );
80 this->_ConfigureNewInput( axis );
83 // -------------------------------------------------------------------------
84 void cpExtensions::Visualization::ImageSliceActors::
85 AddInputData( vtkImageData* data, int axis )
87 // Configure LUT, if possible (NULL is returned if not)
88 this->_ConfigureNewLUT( data );
89 vtkImageMapToColors* new_map = *( this->m_ImageMaps.rbegin( ) );
92 new_map->SetInputData( data );
97 // Create mapper and actors
98 vtkSmartPointer< vtkImageSliceMapper > mapper =
99 vtkSmartPointer< vtkImageSliceMapper >::New( );
100 if( new_map != NULL )
101 mapper->SetInputConnection( new_map->GetOutputPort( ) );
103 mapper->SetInputData( data );
104 this->m_SliceMappers.push_back( mapper );
105 this->_ConfigureNewInput( axis );
108 // -------------------------------------------------------------------------
109 void cpExtensions::Visualization::ImageSliceActors::
112 // Unbind from container
113 this->RemoveAllItems( );
116 this->m_ImageMaps.clear( );
117 this->m_SliceMappers.clear( );
118 this->m_ImageActors.clear( );
119 this->m_AssociatedSlices.clear( );
120 this->m_AssociatedActors.clear( );
122 // Reconfigure unique objects
123 this->m_Cursor = vtkSmartPointer< vtkPolyData >::New( );
124 this->m_CursorMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
125 this->m_CursorActor = vtkSmartPointer< vtkActor >::New( );
126 this->m_PlaneFunction = vtkSmartPointer< vtkPlane >::New( );
127 this->m_Plane = vtkSmartPointer< vtkPolyData >::New( );
128 this->m_PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
129 this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
130 this->m_PlaneActor = vtkSmartPointer< vtkActor >::New( );
131 this->m_TextBuffer[ 0 ] = '\0';
133 // Unique objects configuration
134 vtkSmartPointer< vtkPoints > cursor_points =
135 vtkSmartPointer< vtkPoints >::New( );
136 vtkSmartPointer< vtkCellArray > cursor_lines =
137 vtkSmartPointer< vtkCellArray >::New( );
138 cursor_points->InsertNextPoint( 0, 0, 0 );
139 cursor_points->InsertNextPoint( 0, 0, 0 );
140 cursor_points->InsertNextPoint( 0, 0, 0 );
141 cursor_points->InsertNextPoint( 0, 0, 0 );
142 cursor_points->InsertNextPoint( 0, 0, 0 );
143 cursor_points->InsertNextPoint( 0, 0, 0 );
144 cursor_points->InsertNextPoint( 0, 0, 0 );
145 cursor_points->InsertNextPoint( 0, 0, 0 );
146 cursor_lines->InsertNextCell( 2 );
147 cursor_lines->InsertCellPoint( 0 );
148 cursor_lines->InsertCellPoint( 1 );
149 cursor_lines->InsertNextCell( 2 );
150 cursor_lines->InsertCellPoint( 2 );
151 cursor_lines->InsertCellPoint( 3 );
152 cursor_lines->InsertNextCell( 2 );
153 cursor_lines->InsertCellPoint( 4 );
154 cursor_lines->InsertCellPoint( 5 );
155 cursor_lines->InsertNextCell( 2 );
156 cursor_lines->InsertCellPoint( 6 );
157 cursor_lines->InsertCellPoint( 7 );
158 this->m_Cursor->SetPoints( cursor_points );
159 this->m_Cursor->SetLines( cursor_lines );
160 this->m_CursorMapper->SetInputData( this->m_Cursor );
161 this->m_CursorActor->SetMapper( this->m_CursorMapper );
163 vtkSmartPointer< vtkPoints > plane_points =
164 vtkSmartPointer< vtkPoints >::New( );
165 vtkSmartPointer< vtkCellArray > plane_lines =
166 vtkSmartPointer< vtkCellArray >::New( );
168 plane_points->InsertNextPoint( 0, 0, 0 );
169 plane_points->InsertNextPoint( 0, 1, 0 );
170 plane_points->InsertNextPoint( 1, 1, 0 );
171 plane_points->InsertNextPoint( 1, 0, 0 );
172 plane_lines->InsertNextCell( 5 );
173 plane_lines->InsertCellPoint( 0 );
174 plane_lines->InsertCellPoint( 1 );
175 plane_lines->InsertCellPoint( 2 );
176 plane_lines->InsertCellPoint( 3 );
177 plane_lines->InsertCellPoint( 0 );
178 this->m_Plane->SetPoints( plane_points );
179 this->m_Plane->SetLines( plane_lines );
181 this->m_PlaneMapper->SetInputData( this->m_Plane );
182 this->m_PlaneActor->SetMapper( this->m_PlaneMapper );
184 this->m_TextActor->SetTextScaleModeToNone( );
185 vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
186 textprop->SetColor( 1, 1, 1 );
187 textprop->SetFontFamilyToCourier( );
188 textprop->SetFontSize( 18 );
189 textprop->BoldOff( );
190 textprop->ItalicOff( );
191 textprop->ShadowOff( );
192 textprop->SetJustificationToLeft( );
193 textprop->SetVerticalJustificationToBottom( );
194 vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
195 coord->SetCoordinateSystemToNormalizedViewport( );
196 coord->SetValue( 0.01, 0.01 );
198 // Update actor collection
199 this->AddItem( this->m_CursorActor );
200 this->AddItem( this->m_TextActor );
201 this->AddItem( this->m_PlaneActor );
204 // -------------------------------------------------------------------------
205 void cpExtensions::Visualization::ImageSliceActors::
206 AssociateSlice( Self* other )
208 this->m_AssociatedSlices.push_back( other );
212 // -------------------------------------------------------------------------
213 void cpExtensions::Visualization::ImageSliceActors::
214 SetSlicesCommand( TCursorCommand cmd, void* data )
216 this->SlicesCommand = cmd;
217 this->SlicesData = data;
221 // -------------------------------------------------------------------------
222 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
225 return( this->m_Style.GetPointer( ) );
228 // -------------------------------------------------------------------------
229 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
232 return( this->m_Style.GetPointer( ) );
235 // -------------------------------------------------------------------------
236 void cpExtensions::Visualization::ImageSliceActors::
237 PushActorsInto( vtkRenderWindow* window, bool force_style )
241 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
242 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
243 if( rwi == NULL || renderer == NULL )
249 this->_ConfigureStyle( );
250 rwi->SetInteractorStyle( this->m_Style );
255 # error USE vtkActorCollection interface
257 for( unsigned int n = 0; n < this->GetNumberOfImageActors( ); ++n )
258 renderer->AddActor( this->GetImageActor( n ) );
259 renderer->AddActor( this->m_CursorActor );
260 renderer->AddActor( this->m_PlaneActor );
261 renderer->AddActor( this->m_TextActor );
262 renderer->Modified( );
266 vtkCamera* camera = renderer->GetActiveCamera( );
267 if( camera != NULL && force_style )
269 // Parallel projections are better when displaying 2D images
270 int axis = this->GetAxis( );
271 camera->ParallelProjectionOn( );
272 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
275 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
276 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
280 camera->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
281 camera->SetViewUp ( double( 0 ), double( 0 ), double( -1 ) );
283 else // if( axis == 2 )
285 camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
286 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
291 renderer->ResetCamera( );
295 // -------------------------------------------------------------------------
296 void cpExtensions::Visualization::ImageSliceActors::
297 PopActorsFrom( vtkRenderWindow* window )
299 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
300 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
302 if( renderer != NULL )
305 # error USE vtkActorCollection interface
307 for( unsigned int n = 0; n < this->GetNumberOfImageActors( ); ++n )
308 renderer->RemoveActor( this->GetImageActor( n ) );
309 renderer->RemoveActor( this->m_CursorActor );
310 renderer->RemoveActor( this->m_PlaneActor );
311 renderer->RemoveActor( this->m_TextActor );
312 renderer->Modified( );
320 // -------------------------------------------------------------------------
321 unsigned int cpExtensions::Visualization::ImageSliceActors::
322 GetNumberOfImageActors( ) const
324 return( this->m_ImageActors.size( ) );
327 // -------------------------------------------------------------------------
328 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
329 GetImageActor( unsigned int id )
331 if( id < this->m_ImageActors.size( ) )
332 return( this->m_ImageActors[ id ] );
337 // -------------------------------------------------------------------------
338 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
339 GetImageActor( unsigned int id ) const
341 if( id < this->m_ImageActors.size( ) )
342 return( this->m_ImageActors[ id ] );
347 // -------------------------------------------------------------------------
348 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
351 return( this->m_TextActor );
354 // -------------------------------------------------------------------------
355 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
356 GetTextActor( ) const
358 return( this->m_TextActor );
361 // -------------------------------------------------------------------------
362 vtkActor* cpExtensions::Visualization::ImageSliceActors::
365 return( this->m_PlaneActor );
368 // -------------------------------------------------------------------------
369 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
370 GetPlaneActor( ) const
372 return( this->m_PlaneActor );
375 // -------------------------------------------------------------------------
376 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
379 return( this->m_PlaneFunction );
382 // -------------------------------------------------------------------------
383 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
384 GetPlaneFunction( ) const
386 return( this->m_PlaneFunction );
389 // -------------------------------------------------------------------------
390 void cpExtensions::Visualization::ImageSliceActors::
391 AddActor( vtkAlgorithm* algorithm, vtkActor* actor )
393 this->m_AssociatedActors.push_back( TAssociatedActor( algorithm, actor ) );
394 this->AddItem( actor );
397 // -------------------------------------------------------------------------
398 void cpExtensions::Visualization::ImageSliceActors::
399 AddActor( vtkActor* actor )
401 this->AddActor( NULL, actor );
404 // -------------------------------------------------------------------------
405 void cpExtensions::Visualization::ImageSliceActors::
406 SetInterpolate( bool v )
408 if( this->Interpolate != v )
410 for( unsigned int i = 0; i < this->m_ImageActors.size( ); ++i )
411 this->m_ImageActors[ i ]->SetInterpolate( v );
412 this->Interpolate = v;
418 // -------------------------------------------------------------------------
419 void cpExtensions::Visualization::ImageSliceActors::
422 this->SetInterpolate( true );
425 // -------------------------------------------------------------------------
426 void cpExtensions::Visualization::ImageSliceActors::
429 this->SetInterpolate( false );
432 // -------------------------------------------------------------------------
433 double* cpExtensions::Visualization::ImageSliceActors::
434 GetDisplayBounds( ) const
436 if( this->m_ImageActors.size( ) > 0 )
437 return( this->m_ImageActors[ 0 ]->GetDisplayBounds( ) );
442 // -------------------------------------------------------------------------
443 void cpExtensions::Visualization::ImageSliceActors::
444 GetDisplayBounds( double bounds[ 6 ] ) const
446 if( this->m_ImageActors.size( ) == 0 )
448 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
449 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
452 this->m_ImageActors[ 0 ]->GetDisplayBounds( bounds );
455 // -------------------------------------------------------------------------
456 void cpExtensions::Visualization::ImageSliceActors::
459 vtkPoints* points = this->m_Cursor->GetPoints( );
460 points->SetPoint( 0, 0, 0, 0 );
461 points->SetPoint( 1, 0, 0, 0 );
462 points->SetPoint( 2, 0, 0, 0 );
463 points->SetPoint( 3, 0, 0, 0 );
464 points->SetPoint( 4, 0, 0, 0 );
465 points->SetPoint( 5, 0, 0, 0 );
466 points->SetPoint( 6, 0, 0, 0 );
467 points->SetPoint( 7, 0, 0, 0 );
468 this->m_Cursor->Modified( );
469 this->m_CursorMapper->Modified( );
470 this->m_CursorActor->Modified( );
473 // -------------------------------------------------------------------------
474 void cpExtensions::Visualization::ImageSliceActors::
475 SetCursor( double pos[ 3 ] )
477 if( this->m_SliceMappers.size( ) == 0 )
481 int a0 = this->GetAxis( );
482 int a1 = ( a0 + 1 ) % 3;
483 int a2 = ( a0 + 2 ) % 3;
489 this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
492 p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
493 p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
495 p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
496 p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
497 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
498 p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
499 p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
500 p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
501 p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
502 p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
504 vtkPoints* points = this->m_Cursor->GetPoints( );
505 points->SetPoint( 0, p0 );
506 points->SetPoint( 1, p1 );
507 points->SetPoint( 2, p2 );
508 points->SetPoint( 3, p3 );
509 points->SetPoint( 4, p4 );
510 points->SetPoint( 5, p5 );
511 points->SetPoint( 6, p6 );
512 points->SetPoint( 7, p7 );
513 this->m_Cursor->Modified( );
514 this->m_CursorMapper->Modified( );
515 this->m_CursorActor->Modified( );
518 // -------------------------------------------------------------------------
519 vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
520 GetImageMap( unsigned int id )
522 if( id < this->m_ImageMaps.size( ) )
523 return( this->m_ImageMaps[ id ].GetPointer( ) );
528 // -------------------------------------------------------------------------
529 const vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
530 GetImageMap( unsigned int id ) const
532 if( id < this->m_ImageMaps.size( ) )
533 return( this->m_ImageMaps[ id ].GetPointer( ) );
538 // -------------------------------------------------------------------------
539 double cpExtensions::Visualization::ImageSliceActors::
542 if( this->m_ImageMaps.size( ) > 0 )
544 if( this->m_ImageMaps[ 0 ].GetPointer( ) != NULL )
546 vtkWindowLevelLookupTable* lut =
547 dynamic_cast< vtkWindowLevelLookupTable* >(
548 this->m_ImageMaps[ 0 ]->GetLookupTable( )
551 return( lut->GetWindow( ) );
553 return( double( 0 ) );
556 return( double( 0 ) );
559 return( double( 0 ) );
562 // -------------------------------------------------------------------------
563 double cpExtensions::Visualization::ImageSliceActors::
566 if( this->m_ImageMaps.size( ) > 0 )
568 if( this->m_ImageMaps[ 0 ].GetPointer( ) != NULL )
570 vtkWindowLevelLookupTable* lut =
571 dynamic_cast< vtkWindowLevelLookupTable* >(
572 this->m_ImageMaps[ 0 ]->GetLookupTable( )
575 return( lut->GetLevel( ) );
577 return( double( 0 ) );
580 return( double( 0 ) );
583 return( double( 0 ) );
586 // -------------------------------------------------------------------------
587 void cpExtensions::Visualization::ImageSliceActors::
588 SetWindow( double w )
590 if( this->m_ImageMaps.size( ) > 0 )
592 if( this->m_ImageMaps[ 0 ].GetPointer( ) != NULL )
594 vtkWindowLevelLookupTable* lut =
595 dynamic_cast< vtkWindowLevelLookupTable* >(
596 this->m_ImageMaps[ 0 ]->GetLookupTable( )
600 /* TODO: Min and Max values are assigned 0!!!
601 if( w < this->MinWindow )
603 if( w > this->MaxWindow )
608 this->m_ImageMaps[ 0 ]->Modified( );
618 // -------------------------------------------------------------------------
619 void cpExtensions::Visualization::ImageSliceActors::
622 if( this->m_ImageMaps.size( ) > 0 )
624 if( this->m_ImageMaps[ 0 ].GetPointer( ) != NULL )
626 vtkWindowLevelLookupTable* lut =
627 dynamic_cast< vtkWindowLevelLookupTable* >(
628 this->m_ImageMaps[ 0 ]->GetLookupTable( )
632 /* TODO: Min and Max values are assigned 0!!!
633 if( l < this->MinLevel )
635 if( l > this->MaxLevel )
640 this->m_ImageMaps[ 0 ]->Modified( );
650 // -------------------------------------------------------------------------
651 void cpExtensions::Visualization::ImageSliceActors::
652 SetWindowLevel( double w, double l )
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( )
664 /* TODO: Min and Max values are assigned 0!!!
665 std::cout << this->MinWindow << " " << this->MaxWindow << std::endl;
666 std::cout << this->MinLevel << " " << this->MaxLevel << std::endl;
667 std::cout << w << " " << l << " <-> " << std::ends;
668 if( w < this->MinWindow )
670 if( w > this->MaxWindow )
672 if( l < this->MinLevel )
674 if( l > this->MaxLevel )
676 std::cout << w << " " << l << std::endl;
681 this->m_ImageMaps[ 0 ]->Modified( );
691 // -------------------------------------------------------------------------
692 void cpExtensions::Visualization::ImageSliceActors::
695 this->SetWindowLevel(
697 ( this->MaxLevel + this->MinLevel ) / double( 2 )
701 // -------------------------------------------------------------------------
702 void cpExtensions::Visualization::ImageSliceActors::
703 SetLookupTable( unsigned int id, vtkLookupTable* lut )
705 if( id < this->m_ImageMaps.size( ) && id > 0 )
707 if( this->m_ImageMaps[ id ].GetPointer( ) != NULL )
709 this->m_ImageMaps[ id ]->SetLookupTable( lut );
710 this->m_ImageMaps[ id ]->Modified( );
718 // -------------------------------------------------------------------------
719 void cpExtensions::Visualization::ImageSliceActors::
720 SetLookupTableAsColor( unsigned int id, double r, double g, double b )
722 static const double _0 = double( 0 );
723 static const double _2 = double( 2 );
724 static const double _4 = double( 4 );
725 static const double _6 = double( 6 );
726 static const double _OPACITY = double( 0.6 );
728 // Check ID consistency
729 if( id == 0 || id >= this->m_ImageMaps.size( ) )
732 // Get image scalar range
733 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
734 vtkImageData* image = dynamic_cast< vtkImageData* >(
735 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
738 image->GetScalarRange( range );
740 // Get HSV from display color
741 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
742 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
743 double d = cmax - cmin;
745 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
751 hue = std::fmod( ( g - b ) / d, _6 );
753 hue = ( ( b - r ) / d ) + _2;
755 hue = ( ( r - g ) / d ) + _4;
760 // Define new lookup table
761 vtkSmartPointer< vtkLookupTable > lut =
762 vtkSmartPointer< vtkLookupTable >::New( );
763 lut->SetScaleToLinear( );
764 lut->SetNanColor( _0, _0, _0, _0 );
765 lut->SetTableRange( range[ 0 ], range[ 1 ] );
766 lut->SetAlphaRange( _0, _OPACITY );
767 lut->SetHueRange( _0, hue );
768 lut->SetSaturationRange( _0, saturation );
769 lut->SetValueRange( _0, value );
771 this->SetLookupTable( id, lut );
774 // -------------------------------------------------------------------------
775 int cpExtensions::Visualization::ImageSliceActors::
778 if( this->m_SliceMappers.size( ) > 0 )
779 return( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
784 // -------------------------------------------------------------------------
785 int cpExtensions::Visualization::ImageSliceActors::
786 GetSliceNumber( ) const
788 if( this->m_SliceMappers.size( ) > 0 )
789 return( this->m_SliceMappers[ 0 ]->GetSliceNumber( ) );
794 // -------------------------------------------------------------------------
795 int cpExtensions::Visualization::ImageSliceActors::
796 GetSliceNumberMinValue( ) const
798 if( this->m_SliceMappers.size( ) > 0 )
799 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
804 // -------------------------------------------------------------------------
805 int cpExtensions::Visualization::ImageSliceActors::
806 GetSliceNumberMaxValue( ) const
808 if( this->m_SliceMappers.size( ) > 0 )
809 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
814 // -------------------------------------------------------------------------
815 void cpExtensions::Visualization::ImageSliceActors::
816 SetSliceNumber( const int& slice )
818 unsigned int nImages = this->m_SliceMappers.size( );
822 // Change visualization extent
823 for( unsigned int i = 0; i < nImages; ++i )
825 this->m_SliceMappers[ i ]->SetSliceNumber( slice );
826 this->m_SliceMappers[ i ]->Modified( );
827 this->m_ImageActors[ i ]->Modified( );
828 this->m_SliceMappers[ i ]->Update( );
833 vtkAlgorithm* algo = this->m_SliceMappers[ 0 ]->GetInputAlgorithm( );
834 vtkInformation* info = algo->GetOutputInformation( 0 );
836 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
837 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
838 info->Get( vtkDataObject::ORIGIN( ), ori );
839 info->Get( vtkDataObject::SPACING( ), spac );
840 this->m_SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
842 // Prevent obscuring voxels by offsetting the plane geometry
845 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
846 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
850 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
851 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
855 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
856 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
859 if( spac[ 0 ] < double( 0 ) )
861 double t = xbnds[ 0 ];
862 xbnds[ 0 ] = xbnds[ 1 ];
866 if( spac[ 1 ] < double( 0 ) )
868 double t = ybnds[ 0 ];
869 ybnds[ 0 ] = ybnds[ 1 ];
873 if( spac[ 2 ] < double( 0 ) )
875 double t = zbnds[ 0 ];
876 zbnds[ 0 ] = zbnds[ 1 ];
881 // Plane function origin
882 this->m_PlaneFunction->SetOrigin( pos );
884 // Configure visualization and implicit plane orientation
885 int axis = this->m_SliceMappers[ 0 ]->GetOrientation( );
886 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
887 this->m_PlaneActor->GetProperty( )->SetLineWidth( 2 );
888 vtkPoints* plane_points = this->m_Plane->GetPoints( );
889 if( axis == 0 ) // YZ, x-normal
891 this->m_PlaneFunction->SetNormal( 1, 0, 0 );
892 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
893 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
894 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
895 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
896 this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
898 else if( axis == 1 ) // ZX, y-normal
900 this->m_PlaneFunction->SetNormal( 0, 1, 0 );
901 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
902 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
903 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
904 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
905 this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
909 this->m_PlaneFunction->SetNormal( 0, 0, 1 );
910 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
911 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
912 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
913 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
914 this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
917 this->m_PlaneFunction->Modified( );
918 this->m_Plane->Modified( );
919 this->m_PlaneMapper->Modified( );
920 this->m_PlaneActor->Modified( );
922 // Prepare other actors to update
924 for( unsigned int i = 0; i < this->OtherActors.size( ); ++i )
926 if( this->OtherActors[ i ].first.GetPointer( ) != NULL )
928 this->OtherActors[ i ].first->Modified( );
929 this->OtherActors[ i ].first->Update( );
933 if( this->OtherActors[ i ].second.GetPointer( ) != NULL )
935 this->OtherActors[ i ].second->GetMapper( )->Modified( );
936 this->OtherActors[ i ].second->Modified( );
942 if( this->m_UpdateCommand != NULL )
943 this->m_UpdateCommand( this->m_UpdateData );
950 for( unsigned int i = 0; i < this->m_AssociatedSlices.size( ); ++i )
951 if( this->m_AssociatedSlices[ i ]->GetAxis( ) == this->GetAxis( ) )
952 this->m_AssociatedSlices[ i ]->SetSliceNumber( slice );
955 // -------------------------------------------------------------------------
956 void cpExtensions::Visualization::ImageSliceActors::
957 SetSlice( double* pos )
960 if( this->m_ImageMaps[ 0 ] != NULL )
962 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
964 image = this->m_SliceMappers[ 0 ]->GetInput( );
968 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
969 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
972 // -------------------------------------------------------------------------
973 void cpExtensions::Visualization::ImageSliceActors::
976 if( this->m_SliceMappers.size( ) > 0 )
979 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
980 if ( axId == 0 ) axis = 'X';
981 else if( axId == 1 ) axis = 'Y';
982 else if( axId == 2 ) axis = 'Z';
985 this->m_TextBuffer, "Axis: %c (%d)",
986 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( )
990 this->m_TextBuffer[ 0 ] = '\0';
991 this->m_TextActor->SetInput( this->m_TextBuffer );
992 this->m_TextActor->Modified( );
996 // -------------------------------------------------------------------------
997 void cpExtensions::Visualization::ImageSliceActors::
998 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( );
1060 // -------------------------------------------------------------------------
1061 void cpExtensions::Visualization::ImageSliceActors::
1062 UpdateText( const double& w, const double& l )
1064 if( this->m_SliceMappers.size( ) > 0 )
1067 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1068 if ( axId == 0 ) axis = 'X';
1069 else if( axId == 1 ) axis = 'Y';
1070 else if( axId == 2 ) axis = 'Z';
1073 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1074 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1078 this->m_TextBuffer[ 0 ] = '\0';
1079 this->m_TextActor->SetInput( this->m_TextBuffer );
1080 this->m_TextActor->Modified( );
1084 // -------------------------------------------------------------------------
1085 cpExtensions::Visualization::ImageSliceActors::
1088 Interpolate( false )
1092 this->SlicesCommand = NULL;
1095 // -------------------------------------------------------------------------
1096 cpExtensions::Visualization::ImageSliceActors::
1097 ~ImageSliceActors( )
1101 // -------------------------------------------------------------------------
1102 void cpExtensions::Visualization::ImageSliceActors::
1105 // Connect this view with a controller
1106 this->m_Style = vtkSmartPointer< ImageInteractorStyle >::New( );
1107 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1108 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1109 this->m_Style->AddMouseDoubleClickCommand(
1110 Self::_MouseDoubleClickCommand, this
1112 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1113 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1116 // -------------------------------------------------------------------------
1117 void cpExtensions::Visualization::ImageSliceActors::
1118 _ConfigureNewLUT( vtkImageData* data )
1120 // Does the new LUT is a window-level one?
1121 unsigned int nImgs = this->m_ImageMaps.size( );
1122 unsigned int nCmps = data->GetNumberOfScalarComponents( );
1126 this->m_ImageMaps.push_back( NULL );
1131 // Create LUT filter
1132 this->m_ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1136 data->GetScalarRange( range );
1137 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1138 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1139 lut->SetScaleToLinear( );
1140 lut->SetTableRange( range );
1141 lut->SetWindow( range[ 1 ] - range[ 0 ] );
1142 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1144 this->m_ImageMaps[ 0 ]->SetLookupTable( lut );
1146 this->MinWindow = double( 0 );
1147 this->MaxWindow = range[ 1 ] - range[ 0 ];
1148 this->MinLevel = range[ 0 ];
1149 this->MaxLevel = range[ 1 ];
1152 this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1155 // -------------------------------------------------------------------------
1156 void cpExtensions::Visualization::ImageSliceActors::
1157 _ConfigureNewInput( int axis )
1159 unsigned int nImages = this->m_ImageActors.size( );
1162 vtkImageSliceMapper* mapper = this->m_SliceMappers[ nImages ];
1164 mapper->SetOrientation( axis );
1166 mapper->SetOrientation( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
1170 vtkSmartPointer< vtkImageActor > actor =
1171 vtkSmartPointer< vtkImageActor >::New( );
1172 this->m_ImageActors.push_back( actor );
1173 actor->SetMapper( mapper );
1174 actor->SetInterpolate( this->Interpolate );
1178 this->m_Style->AssociateImageActor( actor );
1179 this->AddItem( actor );
1182 this->SetSliceNumber( this->GetSliceNumber( ) );
1186 // -------------------------------------------------------------------------
1187 void cpExtensions::Visualization::ImageSliceActors::
1190 const ImageInteractorStyle::ButtonID& btn, double* pos,
1191 bool alt, bool ctr, bool sft
1194 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1195 if( actors == NULL )
1198 if( btn == ImageInteractorStyle::ButtonID_None )
1200 actors->UpdateText( pos );
1202 else if( btn == ImageInteractorStyle::ButtonID_Left )
1204 if( !alt && ctr && !sft && actors->SlicesCommand != NULL )
1205 actors->SlicesCommand( pos, actors->GetAxis( ), actors->SlicesData );
1207 else if( btn == ImageInteractorStyle::ButtonID_Middle )
1210 else if( btn == ImageInteractorStyle::ButtonID_Right )
1212 if( !alt && !ctr && sft )
1215 actors->m_SliceMappers[ 0 ]->GetBounds( bounds );
1217 int a0 = actors->GetAxis( );
1218 int a1 = ( a0 + 1 ) % 3;
1219 int a2 = ( a0 + 2 ) % 3;
1220 double dx = pos[ a1 ] - actors->StartWindowLevelPos[ a1 ];
1221 double dy = pos[ a2 ] - actors->StartWindowLevelPos[ a2 ];
1222 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1223 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1225 dx *= actors->StartWindowLevel[ 0 ];
1226 dy *= actors->StartWindowLevel[ 1 ];
1227 actors->SetWindowLevel(
1228 actors->StartWindowLevel[ 0 ] + dx,
1229 actors->StartWindowLevel[ 1 ] + dy
1237 // -------------------------------------------------------------------------
1238 void cpExtensions::Visualization::ImageSliceActors::
1241 const ImageInteractorStyle::ButtonID& btn, double* pos,
1242 bool alt, bool ctr, bool sft
1245 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1246 if( actors == NULL )
1249 actors->StartWindowLevelPos[ 0 ] = pos[ 0 ];
1250 actors->StartWindowLevelPos[ 1 ] = pos[ 1 ];
1251 actors->StartWindowLevelPos[ 2 ] = pos[ 2 ];
1252 actors->StartWindowLevel[ 0 ] = actors->GetWindow( );
1253 actors->StartWindowLevel[ 1 ] = actors->GetLevel( );
1256 // -------------------------------------------------------------------------
1257 void cpExtensions::Visualization::ImageSliceActors::
1258 _MouseDoubleClickCommand(
1260 const ImageInteractorStyle::ButtonID& btn, double* pos,
1261 bool alt, bool ctr, bool sft
1264 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1265 if( actors == NULL )
1268 if( btn == ImageInteractorStyle::ButtonID_Left )
1270 if( !alt && !ctr && !sft )
1271 actors->SetCursor( pos );
1272 else if( !alt && ctr && !sft && actors->SlicesCommand != NULL )
1274 actors->SlicesCommand( pos, actors->GetAxis( ), actors->SlicesData );
1275 for( unsigned int i = 0; i < actors->m_CursorCommands.size( ); ++i )
1276 actors->m_CursorCommands[ i ].first(
1279 actors->m_CursorCommands[ i ].second
1284 else if( btn == ImageInteractorStyle::ButtonID_Middle )
1287 else if( btn == ImageInteractorStyle::ButtonID_Right )
1292 // -------------------------------------------------------------------------
1293 void cpExtensions::Visualization::ImageSliceActors::
1296 const int& dir, bool alt, bool ctr, bool sft
1299 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1300 if( actors == NULL )
1303 if( !alt && !ctr && !sft )
1305 actors->SetSliceNumber( actors->GetSliceNumber( ) + dir );
1307 else if( !alt && !ctr && sft )
1309 actors->SetSliceNumber( actors->GetSliceNumber( ) + ( dir * 10 ) );
1314 // -------------------------------------------------------------------------
1315 void cpExtensions::Visualization::ImageSliceActors::