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 vtkImageData* data = dynamic_cast< vtkImageData* >(
35 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
40 vtkImageMapToColors* new_map =
41 dynamic_cast< vtkImageMapToColors* >( aout->GetProducer( ) );
45 this->_ConfigureNewLUT( data );
46 new_map = *( this->ImageMaps.rbegin( ) );
49 new_map->SetInputConnection( aout );
55 this->ImageMaps.push_back( new_map );
57 // Create mapper and actors
58 vtkSmartPointer< vtkImageSliceMapper > mapper =
59 vtkSmartPointer< vtkImageSliceMapper >::New( );
61 mapper->SetInputConnection( new_map->GetOutputPort( ) );
63 mapper->SetInputConnection( aout );
64 this->SliceMappers.push_back( mapper );
65 this->_ConfigureNewInput( axis );
68 // -------------------------------------------------------------------------
69 void cpExtensions::Visualization::ImageSliceActors::
70 AddInputData( vtkImageData* data, int axis )
73 this->_ConfigureNewLUT( data );
74 vtkImageMapToColors* new_map = *( this->ImageMaps.rbegin( ) );
77 new_map->SetInputData( data );
82 // Create mapper and actors
83 vtkSmartPointer< vtkImageSliceMapper > mapper =
84 vtkSmartPointer< vtkImageSliceMapper >::New( );
86 mapper->SetInputConnection( new_map->GetOutputPort( ) );
88 mapper->SetInputData( data );
89 this->SliceMappers.push_back( mapper );
90 this->_ConfigureNewInput( axis );
93 // -------------------------------------------------------------------------
94 void cpExtensions::Visualization::ImageSliceActors::
97 // Unbind from container
98 this->RemoveAllItems( );
101 this->ImageMaps.clear( );
102 this->SliceMappers.clear( );
103 this->ImageActors.clear( );
104 this->AssociatedSlices.clear( );
105 this->AssociatedActors.clear( );
107 // Reconfigure unique objects
108 this->Cursor = vtkSmartPointer< vtkPolyData >::New( );
109 this->CursorMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
110 this->CursorActor = vtkSmartPointer< vtkActor >::New( );
111 this->PlaneFunction = vtkSmartPointer< vtkPlane >::New( );
112 this->Plane = vtkSmartPointer< vtkPolyData >::New( );
113 this->PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
114 this->TextActor = vtkSmartPointer< vtkTextActor >::New( );
115 this->PlaneActor = vtkSmartPointer< vtkActor >::New( );
116 this->TextBuffer[ 0 ] = '\0';
118 // Unique objects configuration
119 vtkSmartPointer< vtkPoints > cursor_points =
120 vtkSmartPointer< vtkPoints >::New( );
121 vtkSmartPointer< vtkCellArray > cursor_lines =
122 vtkSmartPointer< vtkCellArray >::New( );
123 cursor_points->InsertNextPoint( 0, 0, 0 );
124 cursor_points->InsertNextPoint( 0, 0, 0 );
125 cursor_points->InsertNextPoint( 0, 0, 0 );
126 cursor_points->InsertNextPoint( 0, 0, 0 );
127 cursor_points->InsertNextPoint( 0, 0, 0 );
128 cursor_points->InsertNextPoint( 0, 0, 0 );
129 cursor_points->InsertNextPoint( 0, 0, 0 );
130 cursor_points->InsertNextPoint( 0, 0, 0 );
131 cursor_lines->InsertNextCell( 2 );
132 cursor_lines->InsertCellPoint( 0 );
133 cursor_lines->InsertCellPoint( 1 );
134 cursor_lines->InsertNextCell( 2 );
135 cursor_lines->InsertCellPoint( 2 );
136 cursor_lines->InsertCellPoint( 3 );
137 cursor_lines->InsertNextCell( 2 );
138 cursor_lines->InsertCellPoint( 4 );
139 cursor_lines->InsertCellPoint( 5 );
140 cursor_lines->InsertNextCell( 2 );
141 cursor_lines->InsertCellPoint( 6 );
142 cursor_lines->InsertCellPoint( 7 );
143 this->Cursor->SetPoints( cursor_points );
144 this->Cursor->SetLines( cursor_lines );
145 this->CursorMapper->SetInputData( this->Cursor );
146 this->CursorActor->SetMapper( this->CursorMapper );
148 vtkSmartPointer< vtkPoints > plane_points =
149 vtkSmartPointer< vtkPoints >::New( );
150 vtkSmartPointer< vtkCellArray > plane_lines =
151 vtkSmartPointer< vtkCellArray >::New( );
153 plane_points->InsertNextPoint( 0, 0, 0 );
154 plane_points->InsertNextPoint( 0, 1, 0 );
155 plane_points->InsertNextPoint( 1, 1, 0 );
156 plane_points->InsertNextPoint( 1, 0, 0 );
157 plane_lines->InsertNextCell( 5 );
158 plane_lines->InsertCellPoint( 0 );
159 plane_lines->InsertCellPoint( 1 );
160 plane_lines->InsertCellPoint( 2 );
161 plane_lines->InsertCellPoint( 3 );
162 plane_lines->InsertCellPoint( 0 );
163 this->Plane->SetPoints( plane_points );
164 this->Plane->SetLines( plane_lines );
166 this->PlaneMapper->SetInputData( this->Plane );
167 this->PlaneActor->SetMapper( this->PlaneMapper );
169 this->TextActor->SetTextScaleModeToNone( );
170 vtkTextProperty* textprop = this->TextActor->GetTextProperty( );
171 textprop->SetColor( 1, 1, 1 );
172 textprop->SetFontFamilyToCourier( );
173 textprop->SetFontSize( 18 );
174 textprop->BoldOff( );
175 textprop->ItalicOff( );
176 textprop->ShadowOff( );
177 textprop->SetJustificationToLeft( );
178 textprop->SetVerticalJustificationToBottom( );
179 vtkCoordinate* coord = this->TextActor->GetPositionCoordinate( );
180 coord->SetCoordinateSystemToNormalizedViewport( );
181 coord->SetValue( 0.01, 0.01 );
184 // -------------------------------------------------------------------------
185 void cpExtensions::Visualization::ImageSliceActors::
186 AssociateSlice( Self* other )
188 this->AssociatedSlices.push_back( other );
192 // -------------------------------------------------------------------------
193 void cpExtensions::Visualization::ImageSliceActors::
194 SetSlicesCommand( TCursorCommand cmd, void* data )
196 this->SlicesCommand = cmd;
197 this->SlicesData = data;
201 // -------------------------------------------------------------------------
202 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
205 return( this->Style.GetPointer( ) );
208 // -------------------------------------------------------------------------
209 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
212 return( this->Style.GetPointer( ) );
215 // -------------------------------------------------------------------------
216 void cpExtensions::Visualization::ImageSliceActors::
217 PushActorsInto( vtkRenderWindow* window, bool force_style )
219 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
220 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
223 if( rwi != NULL && force_style )
225 if( rwi->GetInteractorStyle( ) != this->Style.GetPointer( ) )
227 rwi->SetInteractorStyle( this->Style );
233 if( renderer != NULL )
236 unsigned int N = this->GetNumberOfImageActors( );
237 for( unsigned int n = 0; n < N; ++n )
238 renderer->AddActor( this->GetImageActor( n ) );
239 renderer->AddActor( this->CursorActor );
240 renderer->AddActor( this->PlaneActor );
241 renderer->AddActor( this->TextActor );
242 renderer->Modified( );
245 vtkCamera* camera = renderer->GetActiveCamera( );
249 // Parallel projections are better when displaying 2D images
252 int axis = this->GetAxis( );
253 camera->ParallelProjectionOn( );
254 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
257 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
258 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
262 camera->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
263 camera->SetViewUp ( double( 0 ), double( 0 ), double( -1 ) );
265 else // if( axis == 2 )
267 camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
268 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
273 renderer->ResetCamera( );
279 // -------------------------------------------------------------------------
280 void cpExtensions::Visualization::ImageSliceActors::
281 PopActorsFrom( vtkRenderWindow* window )
283 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
284 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
286 if( renderer != NULL )
289 unsigned int N = this->GetNumberOfImageActors( );
290 for( unsigned int n = 0; n < N; ++n )
291 renderer->RemoveActor( this->GetImageActor( n ) );
292 renderer->RemoveActor( this->CursorActor );
293 renderer->RemoveActor( this->PlaneActor );
294 renderer->RemoveActor( this->TextActor );
295 renderer->Modified( );
300 // -------------------------------------------------------------------------
301 unsigned int cpExtensions::Visualization::ImageSliceActors::
302 GetNumberOfImageActors( ) const
304 return( this->ImageActors.size( ) );
307 // -------------------------------------------------------------------------
308 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
309 GetImageActor( unsigned int id )
311 if( id < this->ImageActors.size( ) )
312 return( this->ImageActors[ id ] );
317 // -------------------------------------------------------------------------
318 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
319 GetImageActor( unsigned int id ) const
321 if( id < this->ImageActors.size( ) )
322 return( this->ImageActors[ id ] );
327 // -------------------------------------------------------------------------
328 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
331 return( this->TextActor );
334 // -------------------------------------------------------------------------
335 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
336 GetTextActor( ) const
338 return( this->TextActor );
341 // -------------------------------------------------------------------------
342 vtkActor* cpExtensions::Visualization::ImageSliceActors::
345 return( this->PlaneActor );
348 // -------------------------------------------------------------------------
349 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
350 GetPlaneActor( ) const
352 return( this->PlaneActor );
355 // -------------------------------------------------------------------------
356 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
359 return( this->PlaneFunction );
362 // -------------------------------------------------------------------------
363 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
364 GetPlaneFunction( ) const
366 return( this->PlaneFunction );
369 // -------------------------------------------------------------------------
370 void cpExtensions::Visualization::ImageSliceActors::
371 AddActor( vtkAlgorithm* algorithm, vtkActor* actor )
373 this->AssociatedActors.push_back( TAssociatedActor( algorithm, actor ) );
374 this->AddItem( actor );
377 // -------------------------------------------------------------------------
378 void cpExtensions::Visualization::ImageSliceActors::
379 AddActor( vtkActor* actor )
381 this->AddActor( NULL, actor );
384 // -------------------------------------------------------------------------
385 void cpExtensions::Visualization::ImageSliceActors::
386 SetInterpolate( bool v )
388 if( this->Interpolate != v )
390 for( unsigned int i = 0; i < this->ImageActors.size( ); ++i )
391 this->ImageActors[ i ]->SetInterpolate( v );
392 this->Interpolate = v;
398 // -------------------------------------------------------------------------
399 void cpExtensions::Visualization::ImageSliceActors::
402 this->SetInterpolate( true );
405 // -------------------------------------------------------------------------
406 void cpExtensions::Visualization::ImageSliceActors::
409 this->SetInterpolate( false );
412 // -------------------------------------------------------------------------
413 double* cpExtensions::Visualization::ImageSliceActors::
414 GetDisplayBounds( ) const
416 if( this->ImageActors.size( ) > 0 )
417 return( this->ImageActors[ 0 ]->GetDisplayBounds( ) );
422 // -------------------------------------------------------------------------
423 void cpExtensions::Visualization::ImageSliceActors::
424 GetDisplayBounds( double bounds[ 6 ] ) const
426 if( this->ImageActors.size( ) == 0 )
428 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
429 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
432 this->ImageActors[ 0 ]->GetDisplayBounds( bounds );
435 // -------------------------------------------------------------------------
436 void cpExtensions::Visualization::ImageSliceActors::
439 vtkPoints* points = this->Cursor->GetPoints( );
440 points->SetPoint( 0, 0, 0, 0 );
441 points->SetPoint( 1, 0, 0, 0 );
442 points->SetPoint( 2, 0, 0, 0 );
443 points->SetPoint( 3, 0, 0, 0 );
444 points->SetPoint( 4, 0, 0, 0 );
445 points->SetPoint( 5, 0, 0, 0 );
446 points->SetPoint( 6, 0, 0, 0 );
447 points->SetPoint( 7, 0, 0, 0 );
448 this->Cursor->Modified( );
449 this->CursorMapper->Modified( );
450 this->CursorActor->Modified( );
453 // -------------------------------------------------------------------------
454 void cpExtensions::Visualization::ImageSliceActors::
455 SetCursor( double pos[ 3 ] )
457 if( this->SliceMappers.size( ) == 0 )
461 int a0 = this->GetAxis( );
462 int a1 = ( a0 + 1 ) % 3;
463 int a2 = ( a0 + 2 ) % 3;
469 this->SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
472 p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
473 p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
475 p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
476 p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
477 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
478 p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
479 p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
480 p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
481 p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
482 p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
484 vtkPoints* points = this->Cursor->GetPoints( );
485 points->SetPoint( 0, p0 );
486 points->SetPoint( 1, p1 );
487 points->SetPoint( 2, p2 );
488 points->SetPoint( 3, p3 );
489 points->SetPoint( 4, p4 );
490 points->SetPoint( 5, p5 );
491 points->SetPoint( 6, p6 );
492 points->SetPoint( 7, p7 );
493 this->Cursor->Modified( );
494 this->CursorMapper->Modified( );
495 this->CursorActor->Modified( );
498 // -------------------------------------------------------------------------
499 vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
500 GetImageMap( unsigned int id )
502 if( id < this->ImageMaps.size( ) )
503 return( this->ImageMaps[ id ].GetPointer( ) );
508 // -------------------------------------------------------------------------
509 const vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
510 GetImageMap( unsigned int id ) const
512 if( id < this->ImageMaps.size( ) )
513 return( this->ImageMaps[ id ].GetPointer( ) );
518 // -------------------------------------------------------------------------
519 double cpExtensions::Visualization::ImageSliceActors::
522 if( this->ImageMaps.size( ) > 0 )
524 if( this->ImageMaps[ 0 ].GetPointer( ) != NULL )
526 vtkWindowLevelLookupTable* lut =
527 dynamic_cast< vtkWindowLevelLookupTable* >(
528 this->ImageMaps[ 0 ]->GetLookupTable( )
531 return( lut->GetWindow( ) );
533 return( double( 0 ) );
536 return( double( 0 ) );
539 return( double( 0 ) );
542 // -------------------------------------------------------------------------
543 double cpExtensions::Visualization::ImageSliceActors::
546 if( this->ImageMaps.size( ) > 0 )
548 if( this->ImageMaps[ 0 ].GetPointer( ) != NULL )
550 vtkWindowLevelLookupTable* lut =
551 dynamic_cast< vtkWindowLevelLookupTable* >(
552 this->ImageMaps[ 0 ]->GetLookupTable( )
555 return( lut->GetLevel( ) );
557 return( double( 0 ) );
560 return( double( 0 ) );
563 return( double( 0 ) );
566 // -------------------------------------------------------------------------
567 void cpExtensions::Visualization::ImageSliceActors::
568 SetWindow( double w )
570 if( this->ImageMaps.size( ) > 0 )
572 if( this->ImageMaps[ 0 ].GetPointer( ) != NULL )
574 vtkWindowLevelLookupTable* lut =
575 dynamic_cast< vtkWindowLevelLookupTable* >(
576 this->ImageMaps[ 0 ]->GetLookupTable( )
580 /* TODO: Min and Max values are assigned 0!!!
581 if( w < this->MinWindow )
583 if( w > this->MaxWindow )
588 this->ImageMaps[ 0 ]->Modified( );
598 // -------------------------------------------------------------------------
599 void cpExtensions::Visualization::ImageSliceActors::
602 if( this->ImageMaps.size( ) > 0 )
604 if( this->ImageMaps[ 0 ].GetPointer( ) != NULL )
606 vtkWindowLevelLookupTable* lut =
607 dynamic_cast< vtkWindowLevelLookupTable* >(
608 this->ImageMaps[ 0 ]->GetLookupTable( )
612 /* TODO: Min and Max values are assigned 0!!!
613 if( l < this->MinLevel )
615 if( l > this->MaxLevel )
620 this->ImageMaps[ 0 ]->Modified( );
630 // -------------------------------------------------------------------------
631 void cpExtensions::Visualization::ImageSliceActors::
632 SetWindowLevel( double w, double l )
634 if( this->ImageMaps.size( ) > 0 )
636 if( this->ImageMaps[ 0 ].GetPointer( ) != NULL )
638 vtkWindowLevelLookupTable* lut =
639 dynamic_cast< vtkWindowLevelLookupTable* >(
640 this->ImageMaps[ 0 ]->GetLookupTable( )
644 /* TODO: Min and Max values are assigned 0!!!
645 std::cout << this->MinWindow << " " << this->MaxWindow << std::endl;
646 std::cout << this->MinLevel << " " << this->MaxLevel << std::endl;
647 std::cout << w << " " << l << " <-> " << std::ends;
648 if( w < this->MinWindow )
650 if( w > this->MaxWindow )
652 if( l < this->MinLevel )
654 if( l > this->MaxLevel )
656 std::cout << w << " " << l << std::endl;
661 this->ImageMaps[ 0 ]->Modified( );
671 // -------------------------------------------------------------------------
672 void cpExtensions::Visualization::ImageSliceActors::
675 this->SetWindowLevel(
677 ( this->MaxLevel + this->MinLevel ) / double( 2 )
681 // -------------------------------------------------------------------------
682 void cpExtensions::Visualization::ImageSliceActors::
683 SetLookupTable( unsigned int id, vtkLookupTable* lut )
685 if( id < this->ImageMaps.size( ) && id > 0 )
687 if( this->ImageMaps[ id ].GetPointer( ) != NULL )
689 this->ImageMaps[ id ]->SetLookupTable( lut );
690 this->ImageMaps[ id ]->Modified( );
698 // -------------------------------------------------------------------------
699 void cpExtensions::Visualization::ImageSliceActors::
700 SetLookupTableAsColor( unsigned int id, double r, double g, double b )
702 static const double _0 = double( 0 );
703 static const double _2 = double( 2 );
704 static const double _4 = double( 4 );
705 static const double _6 = double( 6 );
706 static const double _OPACITY = double( 0.6 );
708 // Check ID consistency
709 if( id == 0 || id >= this->ImageMaps.size( ) )
712 // Get image scalar range
713 vtkAlgorithmOutput* aout = this->ImageMaps[ id ]->GetOutputPort( );
714 vtkImageData* image = dynamic_cast< vtkImageData* >(
715 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
718 image->GetScalarRange( range );
720 // Get HSV from display color
721 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
722 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
723 double d = cmax - cmin;
725 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
731 hue = std::fmod( ( g - b ) / d, _6 );
733 hue = ( ( b - r ) / d ) + _2;
735 hue = ( ( r - g ) / d ) + _4;
740 // Define new lookup table
741 vtkSmartPointer< vtkLookupTable > lut =
742 vtkSmartPointer< vtkLookupTable >::New( );
743 lut->SetScaleToLinear( );
744 lut->SetNanColor( _0, _0, _0, _0 );
745 lut->SetTableRange( range[ 0 ], range[ 1 ] );
746 lut->SetAlphaRange( _0, _OPACITY );
747 lut->SetHueRange( _0, hue );
748 lut->SetSaturationRange( _0, saturation );
749 lut->SetValueRange( _0, value );
751 this->SetLookupTable( id, lut );
754 // -------------------------------------------------------------------------
755 int cpExtensions::Visualization::ImageSliceActors::
758 if( this->SliceMappers.size( ) > 0 )
759 return( this->SliceMappers[ 0 ]->GetOrientation( ) );
764 // -------------------------------------------------------------------------
765 int cpExtensions::Visualization::ImageSliceActors::
766 GetSliceNumber( ) const
768 if( this->SliceMappers.size( ) > 0 )
769 return( this->SliceMappers[ 0 ]->GetSliceNumber( ) );
774 // -------------------------------------------------------------------------
775 int cpExtensions::Visualization::ImageSliceActors::
776 GetSliceNumberMinValue( ) const
778 if( this->SliceMappers.size( ) > 0 )
779 return( this->SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
784 // -------------------------------------------------------------------------
785 int cpExtensions::Visualization::ImageSliceActors::
786 GetSliceNumberMaxValue( ) const
788 if( this->SliceMappers.size( ) > 0 )
789 return( this->SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
794 // -------------------------------------------------------------------------
795 void cpExtensions::Visualization::ImageSliceActors::
796 SetSliceNumber( const int& slice )
798 unsigned int nImages = this->SliceMappers.size( );
802 // Change visualization extent
803 for( unsigned int i = 0; i < nImages; ++i )
805 this->SliceMappers[ i ]->SetSliceNumber( slice );
806 this->SliceMappers[ i ]->Modified( );
807 this->ImageActors[ i ]->Modified( );
808 this->SliceMappers[ i ]->Update( );
813 vtkAlgorithm* algo = this->SliceMappers[ 0 ]->GetInputAlgorithm( );
814 vtkInformation* info = algo->GetOutputInformation( 0 );
816 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
817 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
818 info->Get( vtkDataObject::ORIGIN( ), ori );
819 info->Get( vtkDataObject::SPACING( ), spac );
820 this->SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
822 // Prevent obscuring voxels by offsetting the plane geometry
825 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
826 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
830 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
831 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
835 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
836 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
839 if( spac[ 0 ] < double( 0 ) )
841 double t = xbnds[ 0 ];
842 xbnds[ 0 ] = xbnds[ 1 ];
846 if( spac[ 1 ] < double( 0 ) )
848 double t = ybnds[ 0 ];
849 ybnds[ 0 ] = ybnds[ 1 ];
853 if( spac[ 2 ] < double( 0 ) )
855 double t = zbnds[ 0 ];
856 zbnds[ 0 ] = zbnds[ 1 ];
861 // Plane function origin
862 this->PlaneFunction->SetOrigin( pos );
864 // Configure visualization and implicit plane orientation
865 int axis = this->SliceMappers[ 0 ]->GetOrientation( );
866 this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
867 this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
868 vtkPoints* plane_points = this->Plane->GetPoints( );
869 if( axis == 0 ) // YZ, x-normal
871 this->PlaneFunction->SetNormal( 1, 0, 0 );
872 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
873 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
874 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
875 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
876 this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
878 else if( axis == 1 ) // ZX, y-normal
880 this->PlaneFunction->SetNormal( 0, 1, 0 );
881 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
882 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
883 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
884 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
885 this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
889 this->PlaneFunction->SetNormal( 0, 0, 1 );
890 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
891 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
892 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
893 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
894 this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
897 this->PlaneFunction->Modified( );
898 this->Plane->Modified( );
899 this->PlaneMapper->Modified( );
900 this->PlaneActor->Modified( );
902 // Prepare other actors to update
904 for( unsigned int i = 0; i < this->OtherActors.size( ); ++i )
906 if( this->OtherActors[ i ].first.GetPointer( ) != NULL )
908 this->OtherActors[ i ].first->Modified( );
909 this->OtherActors[ i ].first->Update( );
913 if( this->OtherActors[ i ].second.GetPointer( ) != NULL )
915 this->OtherActors[ i ].second->GetMapper( )->Modified( );
916 this->OtherActors[ i ].second->Modified( );
922 if( this->m_UpdateCommand != NULL )
923 this->m_UpdateCommand( this->m_UpdateData );
930 for( unsigned int i = 0; i < this->AssociatedSlices.size( ); ++i )
931 if( this->AssociatedSlices[ i ]->GetAxis( ) == this->GetAxis( ) )
932 this->AssociatedSlices[ i ]->SetSliceNumber( slice );
935 // -------------------------------------------------------------------------
936 void cpExtensions::Visualization::ImageSliceActors::
937 SetSlice( double* pos )
940 if( this->ImageMaps[ 0 ] != NULL )
942 dynamic_cast< vtkImageData* >( this->ImageMaps[ 0 ]->GetInput( ) );
944 image = this->SliceMappers[ 0 ]->GetInput( );
948 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
949 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
952 // -------------------------------------------------------------------------
953 void cpExtensions::Visualization::ImageSliceActors::
956 if( this->SliceMappers.size( ) > 0 )
959 int axId = this->SliceMappers[ 0 ]->GetOrientation( );
960 if ( axId == 0 ) axis = 'X';
961 else if( axId == 1 ) axis = 'Y';
962 else if( axId == 2 ) axis = 'Z';
965 this->TextBuffer, "Axis: %c (%d)",
966 axis, this->SliceMappers[ 0 ]->GetSliceNumber( )
970 this->TextBuffer[ 0 ] = '\0';
971 this->TextActor->SetInput( this->TextBuffer );
972 this->TextActor->Modified( );
976 // -------------------------------------------------------------------------
977 void cpExtensions::Visualization::ImageSliceActors::
978 UpdateText( double pos[ 3 ] )
980 if( this->SliceMappers.size( ) > 0 )
983 int axId = this->SliceMappers[ 0 ]->GetOrientation( );
984 if ( axId == 0 ) axis = 'X';
985 else if( axId == 1 ) axis = 'Y';
986 else if( axId == 2 ) axis = 'Z';
987 int slice = this->GetSliceNumber( );
990 if( this->ImageMaps[ 0 ] != NULL )
992 dynamic_cast< vtkImageData* >( this->ImageMaps[ 0 ]->GetInput( ) );
994 image = this->SliceMappers[ 0 ]->GetInput( );
998 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1002 image->GetExtent( ext );
1004 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
1005 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
1006 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
1009 int nScl = image->GetNumberOfScalarComponents( );
1010 std::stringstream str;
1014 << "," << ijk[ 2 ] << "]=(";
1016 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
1017 for( int n = 1; n < nScl; ++n )
1020 << image->GetScalarComponentAsFloat(
1021 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
1025 this->TextBuffer, "Axis: %c (%d)\nPixel %s",
1026 axis, slice, str.str( ).c_str( )
1032 this->TextBuffer[ 0 ] = '\0';
1033 this->TextActor->SetInput( this->TextBuffer );
1034 this->TextActor->Modified( );
1038 // -------------------------------------------------------------------------
1039 void cpExtensions::Visualization::ImageSliceActors::
1040 UpdateText( const double& w, const double& l )
1042 if( this->SliceMappers.size( ) > 0 )
1045 int axId = this->SliceMappers[ 0 ]->GetOrientation( );
1046 if ( axId == 0 ) axis = 'X';
1047 else if( axId == 1 ) axis = 'Y';
1048 else if( axId == 2 ) axis = 'Z';
1051 this->TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1052 axis, this->SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1056 this->TextBuffer[ 0 ] = '\0';
1057 this->TextActor->SetInput( this->TextBuffer );
1058 this->TextActor->Modified( );
1062 // -------------------------------------------------------------------------
1063 cpExtensions::Visualization::ImageSliceActors::
1066 Interpolate( false )
1070 this->SlicesCommand = NULL;
1072 // Connect this view with a controller
1073 this->Style = vtkSmartPointer< ImageInteractorStyle >::New( );
1074 this->Style->AssociateView( this );
1075 this->Style->SetMouseMoveCommand( Self::_MouseMoveCommand );
1076 this->Style->SetMouseClickCommand( Self::_MouseClickCommand );
1077 this->Style->SetMouseDoubleClickCommand( Self::_MouseDoubleClickCommand );
1078 this->Style->SetMouseWheelCommand( Self::_MouseWheelCommand );
1079 this->Style->SetKeyCommand( Self::_KeyCommand );
1082 // -------------------------------------------------------------------------
1083 cpExtensions::Visualization::ImageSliceActors::
1084 ~ImageSliceActors( )
1088 // -------------------------------------------------------------------------
1089 void cpExtensions::Visualization::ImageSliceActors::
1090 _ConfigureNewLUT( vtkImageData* data )
1092 // Does the new LUT is a window-level one?
1093 unsigned int nImgs = this->ImageMaps.size( );
1094 unsigned int nCmps = data->GetNumberOfScalarComponents( );
1098 this->ImageMaps.push_back( NULL );
1103 // Create LUT filter
1104 this->ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1108 data->GetScalarRange( range );
1109 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1110 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1111 lut->SetScaleToLinear( );
1112 lut->SetTableRange( range );
1113 lut->SetWindow( range[ 1 ] - range[ 0 ] );
1114 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1116 this->ImageMaps[ 0 ]->SetLookupTable( lut );
1118 this->MinWindow = double( 0 );
1119 this->MaxWindow = range[ 1 ] - range[ 0 ];
1120 this->MinLevel = range[ 0 ];
1121 this->MaxLevel = range[ 1 ];
1124 this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1127 // -------------------------------------------------------------------------
1128 void cpExtensions::Visualization::ImageSliceActors::
1129 _ConfigureNewInput( int axis )
1131 unsigned int nImages = this->ImageActors.size( );
1134 vtkImageSliceMapper* mapper = this->SliceMappers[ nImages ];
1136 mapper->SetOrientation( axis );
1138 mapper->SetOrientation( this->SliceMappers[ 0 ]->GetOrientation( ) );
1142 vtkSmartPointer< vtkImageActor > actor =
1143 vtkSmartPointer< vtkImageActor >::New( );
1144 this->ImageActors.push_back( actor );
1145 actor->SetMapper( mapper );
1146 actor->SetInterpolate( this->Interpolate );
1151 this->AddItem( this->CursorActor );
1152 this->AddItem( this->PlaneActor );
1153 this->AddItem( this->TextActor );
1154 this->Style->AssociateImageActor( actor );
1157 this->AddItem( actor );
1160 this->SetSliceNumber( this->GetSliceNumber( ) );
1164 // -------------------------------------------------------------------------
1165 void cpExtensions::Visualization::ImageSliceActors::
1168 const ImageInteractorStyle::ButtonID& btn, double* pos,
1169 bool alt, bool ctr, bool sft
1172 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1173 if( actors == NULL )
1176 if( btn == ImageInteractorStyle::ButtonID_None )
1178 actors->UpdateText( pos );
1180 else if( btn == ImageInteractorStyle::ButtonID_Left )
1182 if( !alt && ctr && !sft && actors->SlicesCommand != NULL )
1183 actors->SlicesCommand( pos, actors->GetAxis( ), actors->SlicesData );
1185 else if( btn == ImageInteractorStyle::ButtonID_Middle )
1188 else if( btn == ImageInteractorStyle::ButtonID_Right )
1190 if( !alt && !ctr && sft )
1193 actors->SliceMappers[ 0 ]->GetBounds( bounds );
1195 int a0 = actors->GetAxis( );
1196 int a1 = ( a0 + 1 ) % 3;
1197 int a2 = ( a0 + 2 ) % 3;
1198 double dx = pos[ a1 ] - actors->StartWindowLevelPos[ a1 ];
1199 double dy = pos[ a2 ] - actors->StartWindowLevelPos[ a2 ];
1200 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1201 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1203 dx *= actors->StartWindowLevel[ 0 ];
1204 dy *= actors->StartWindowLevel[ 1 ];
1205 actors->SetWindowLevel(
1206 actors->StartWindowLevel[ 0 ] + dx,
1207 actors->StartWindowLevel[ 1 ] + dy
1215 // -------------------------------------------------------------------------
1216 void cpExtensions::Visualization::ImageSliceActors::
1219 const ImageInteractorStyle::ButtonID& btn, double* pos,
1220 bool alt, bool ctr, bool sft
1223 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1224 if( actors == NULL )
1227 actors->StartWindowLevelPos[ 0 ] = pos[ 0 ];
1228 actors->StartWindowLevelPos[ 1 ] = pos[ 1 ];
1229 actors->StartWindowLevelPos[ 2 ] = pos[ 2 ];
1230 actors->StartWindowLevel[ 0 ] = actors->GetWindow( );
1231 actors->StartWindowLevel[ 1 ] = actors->GetLevel( );
1234 // -------------------------------------------------------------------------
1235 void cpExtensions::Visualization::ImageSliceActors::
1236 _MouseDoubleClickCommand(
1238 const ImageInteractorStyle::ButtonID& btn, double* pos,
1239 bool alt, bool ctr, bool sft
1242 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1243 if( actors == NULL )
1246 if( btn == ImageInteractorStyle::ButtonID_Left )
1248 if( !alt && !ctr && !sft )
1249 actors->SetCursor( pos );
1250 else if( !alt && ctr && !sft && actors->SlicesCommand != NULL )
1251 actors->SlicesCommand( pos, actors->GetAxis( ), actors->SlicesData );
1253 else if( btn == ImageInteractorStyle::ButtonID_Middle )
1256 else if( btn == ImageInteractorStyle::ButtonID_Right )
1261 // -------------------------------------------------------------------------
1262 void cpExtensions::Visualization::ImageSliceActors::
1265 const int& dir, bool alt, bool ctr, bool sft
1268 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1269 if( actors == NULL )
1272 if( !alt && !ctr && !sft )
1274 actors->SetSliceNumber( actors->GetSliceNumber( ) + dir );
1276 else if( !alt && !ctr && sft )
1278 actors->SetSliceNumber( actors->GetSliceNumber( ) + ( dir * 10 ) );
1283 // -------------------------------------------------------------------------
1284 void cpExtensions::Visualization::ImageSliceActors::