1 #include <cpExtensions/Visualization/ImageSliceActors.h>
6 #include <vtkAlgorithmOutput.h>
8 #include <vtkCellArray.h>
9 #include <vtkImageData.h>
10 #include <vtkInformation.h>
12 #include <vtkPoints.h>
13 #include <vtkProperty.h>
14 #include <vtkRenderer.h>
15 #include <vtkRendererCollection.h>
16 #include <vtkRenderWindow.h>
17 #include <vtkRenderWindowInteractor.h>
18 #include <vtkStreamingDemandDrivenPipeline.h>
19 #include <vtkTextProperty.h>
20 #include <vtkWindowLevelLookupTable.h>
22 // -------------------------------------------------------------------------
23 cpExtensions::Visualization::ImageSliceActors*
24 cpExtensions::Visualization::ImageSliceActors::
27 return( new Self( ) );
30 // -------------------------------------------------------------------------
31 void cpExtensions::Visualization::ImageSliceActors::
32 AddSlicesCommand( TCursorCommand command, void* data )
34 this->m_SlicesCommands.push_back(
35 std::pair< TCursorCommand, void* >( command, data )
40 // -------------------------------------------------------------------------
41 void cpExtensions::Visualization::ImageSliceActors::
42 AddSeedCommand( TCursorCommand command, void* data )
44 this->m_SeedCommands.push_back(
45 std::pair< TCursorCommand, void* >( command, data )
50 // -------------------------------------------------------------------------
51 void cpExtensions::Visualization::ImageSliceActors::
52 AddInputConnection( vtkAlgorithmOutput* aout, int axis )
54 // Get input vtkImageData
57 vtkAlgorithm* producer = aout->GetProducer( );
58 vtkImageData* data = dynamic_cast< vtkImageData* >(
59 producer->GetOutputDataObject( aout->GetIndex( ) )
64 // Try to infere if input comes from a color mapping filter
65 vtkImageMapToColors* new_map =
66 dynamic_cast< vtkImageMapToColors* >( producer );
69 // Configure LUT, if possible (NULL is returned if not)
70 this->_ConfigureNewLUT( data );
71 new_map = *( this->m_ImageMaps.rbegin( ) );
74 new_map->SetInputConnection( aout );
80 this->m_ImageMaps.push_back( new_map );
82 // Create mapper and actors
83 vtkSmartPointer< vtkImageSliceMapper > mapper =
84 vtkSmartPointer< vtkImageSliceMapper >::New( );
86 mapper->SetInputConnection( new_map->GetOutputPort( ) );
88 mapper->SetInputConnection( aout );
89 this->m_SliceMappers.push_back( mapper );
90 this->_ConfigureNewInput( axis );
93 // -------------------------------------------------------------------------
94 void cpExtensions::Visualization::ImageSliceActors::
95 AddInputData( vtkImageData* data, int axis )
97 // Configure LUT, if possible (NULL is returned if not)
98 this->_ConfigureNewLUT( data );
99 vtkImageMapToColors* new_map = *( this->m_ImageMaps.rbegin( ) );
100 if( new_map != NULL )
102 new_map->SetInputData( data );
107 // Create mapper and actors
108 vtkSmartPointer< vtkImageSliceMapper > mapper =
109 vtkSmartPointer< vtkImageSliceMapper >::New( );
110 if( new_map != NULL )
111 mapper->SetInputConnection( new_map->GetOutputPort( ) );
113 mapper->SetInputData( data );
114 this->m_SliceMappers.push_back( mapper );
115 this->_ConfigureNewInput( axis );
118 // -------------------------------------------------------------------------
119 void cpExtensions::Visualization::ImageSliceActors::
122 // Unbind from container
123 this->RemoveAllItems( );
126 this->m_ImageMaps.clear( );
127 this->m_SliceMappers.clear( );
128 this->m_ImageActors.clear( );
129 this->m_AssociatedSlices.clear( );
130 this->m_AssociatedActors.clear( );
132 // Reconfigure unique objects
133 this->m_Cursor = vtkSmartPointer< vtkPolyData >::New( );
134 this->m_CursorMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
135 this->m_CursorActor = vtkSmartPointer< vtkActor >::New( );
136 this->m_PlaneFunction = vtkSmartPointer< vtkPlane >::New( );
137 this->m_Plane = vtkSmartPointer< vtkPolyData >::New( );
138 this->m_PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
139 this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
140 this->m_PlaneActor = vtkSmartPointer< vtkActor >::New( );
141 this->m_TextBuffer[ 0 ] = '\0';
143 // Unique objects configuration
144 vtkSmartPointer< vtkPoints > cursor_points =
145 vtkSmartPointer< vtkPoints >::New( );
146 vtkSmartPointer< vtkCellArray > cursor_lines =
147 vtkSmartPointer< vtkCellArray >::New( );
148 cursor_points->InsertNextPoint( 0, 0, 0 );
149 cursor_points->InsertNextPoint( 0, 0, 0 );
150 cursor_points->InsertNextPoint( 0, 0, 0 );
151 cursor_points->InsertNextPoint( 0, 0, 0 );
152 cursor_points->InsertNextPoint( 0, 0, 0 );
153 cursor_points->InsertNextPoint( 0, 0, 0 );
154 cursor_points->InsertNextPoint( 0, 0, 0 );
155 cursor_points->InsertNextPoint( 0, 0, 0 );
156 cursor_lines->InsertNextCell( 2 );
157 cursor_lines->InsertCellPoint( 0 );
158 cursor_lines->InsertCellPoint( 1 );
159 cursor_lines->InsertNextCell( 2 );
160 cursor_lines->InsertCellPoint( 2 );
161 cursor_lines->InsertCellPoint( 3 );
162 cursor_lines->InsertNextCell( 2 );
163 cursor_lines->InsertCellPoint( 4 );
164 cursor_lines->InsertCellPoint( 5 );
165 cursor_lines->InsertNextCell( 2 );
166 cursor_lines->InsertCellPoint( 6 );
167 cursor_lines->InsertCellPoint( 7 );
168 this->m_Cursor->SetPoints( cursor_points );
169 this->m_Cursor->SetLines( cursor_lines );
170 this->m_CursorMapper->SetInputData( this->m_Cursor );
171 this->m_CursorActor->SetMapper( this->m_CursorMapper );
173 vtkSmartPointer< vtkPoints > plane_points =
174 vtkSmartPointer< vtkPoints >::New( );
175 vtkSmartPointer< vtkCellArray > plane_lines =
176 vtkSmartPointer< vtkCellArray >::New( );
178 plane_points->InsertNextPoint( 0, 0, 0 );
179 plane_points->InsertNextPoint( 0, 1, 0 );
180 plane_points->InsertNextPoint( 1, 1, 0 );
181 plane_points->InsertNextPoint( 1, 0, 0 );
182 plane_lines->InsertNextCell( 5 );
183 plane_lines->InsertCellPoint( 0 );
184 plane_lines->InsertCellPoint( 1 );
185 plane_lines->InsertCellPoint( 2 );
186 plane_lines->InsertCellPoint( 3 );
187 plane_lines->InsertCellPoint( 0 );
188 this->m_Plane->SetPoints( plane_points );
189 this->m_Plane->SetLines( plane_lines );
191 this->m_PlaneMapper->SetInputData( this->m_Plane );
192 this->m_PlaneActor->SetMapper( this->m_PlaneMapper );
194 this->m_TextActor->SetTextScaleModeToNone( );
195 vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
196 textprop->SetColor( 1, 1, 1 );
197 textprop->SetFontFamilyToCourier( );
198 textprop->SetFontSize( 18 );
199 textprop->BoldOff( );
200 textprop->ItalicOff( );
201 textprop->ShadowOff( );
202 textprop->SetJustificationToLeft( );
203 textprop->SetVerticalJustificationToBottom( );
204 vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
205 coord->SetCoordinateSystemToNormalizedViewport( );
206 coord->SetValue( 0.01, 0.01 );
208 // Update actor collection
209 this->AddItem( this->m_CursorActor );
210 this->AddItem( this->m_TextActor );
211 this->AddItem( this->m_PlaneActor );
214 // -------------------------------------------------------------------------
215 void cpExtensions::Visualization::ImageSliceActors::
216 AssociateSlice( Self* other )
218 this->m_AssociatedSlices.push_back( other );
222 // -------------------------------------------------------------------------
223 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
226 return( this->m_Style.GetPointer( ) );
229 // -------------------------------------------------------------------------
230 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
233 return( this->m_Style.GetPointer( ) );
236 // -------------------------------------------------------------------------
237 void cpExtensions::Visualization::ImageSliceActors::
238 PushActorsInto( vtkRenderWindow* window, bool force_style )
242 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
243 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
244 if( rwi == NULL || renderer == NULL )
248 if( this->m_Style.GetPointer( ) != NULL && force_style )
249 rwi->SetInteractorStyle( this->m_Style );
253 this->InitTraversal( );
254 while( prop = this->GetNextProp( ) )
255 renderer->AddViewProp( prop );
256 renderer->Modified( );
259 vtkCamera* camera = renderer->GetActiveCamera( );
260 if( camera != NULL && force_style )
262 // Parallel projections are better when displaying 2D images
263 int axis = this->GetAxis( );
264 camera->ParallelProjectionOn( );
265 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
268 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
269 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
273 camera->SetPosition( double( 0 ), double( 1 ), double( 0 ) );
274 camera->SetViewUp ( double( 0 ), double( 0 ), double( -1 ) );
276 else // if( axis == 2 )
278 camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
279 camera->SetViewUp ( double( 0 ), double( 1 ), double( 0 ) );
284 renderer->ResetCamera( );
288 // -------------------------------------------------------------------------
289 void cpExtensions::Visualization::ImageSliceActors::
290 PopActorsFrom( vtkRenderWindow* window )
292 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
293 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
295 if( renderer != NULL )
299 this->InitTraversal( );
300 while( prop = this->GetNextProp( ) )
301 renderer->RemoveViewProp( prop );
302 renderer->Modified( );
309 // -------------------------------------------------------------------------
310 unsigned int cpExtensions::Visualization::ImageSliceActors::
311 GetNumberOfImageActors( ) const
313 return( this->m_ImageActors.size( ) );
316 // -------------------------------------------------------------------------
317 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
318 GetImageActor( unsigned int id )
320 if( id < this->m_ImageActors.size( ) )
321 return( this->m_ImageActors[ id ] );
326 // -------------------------------------------------------------------------
327 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
328 GetImageActor( unsigned int id ) const
330 if( id < this->m_ImageActors.size( ) )
331 return( this->m_ImageActors[ id ] );
336 // -------------------------------------------------------------------------
337 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
340 return( this->m_TextActor );
343 // -------------------------------------------------------------------------
344 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
345 GetTextActor( ) const
347 return( this->m_TextActor );
350 // -------------------------------------------------------------------------
351 vtkActor* cpExtensions::Visualization::ImageSliceActors::
354 return( this->m_PlaneActor );
357 // -------------------------------------------------------------------------
358 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
359 GetPlaneActor( ) const
361 return( this->m_PlaneActor );
364 // -------------------------------------------------------------------------
365 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
368 return( this->m_PlaneFunction );
371 // -------------------------------------------------------------------------
372 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
373 GetPlaneFunction( ) const
375 return( this->m_PlaneFunction );
378 // -------------------------------------------------------------------------
379 void cpExtensions::Visualization::ImageSliceActors::
380 AddActor( vtkAlgorithm* algorithm, vtkActor* actor )
382 this->m_AssociatedActors.push_back( TAssociatedActor( algorithm, actor ) );
383 this->AddItem( actor );
386 // -------------------------------------------------------------------------
387 void cpExtensions::Visualization::ImageSliceActors::
388 AddActor( vtkActor* actor )
390 this->AddActor( NULL, actor );
393 // -------------------------------------------------------------------------
394 void cpExtensions::Visualization::ImageSliceActors::
395 SetInterpolate( bool v )
397 if( this->m_Interpolate != v )
399 for( unsigned int i = 0; i < this->m_ImageActors.size( ); ++i )
400 this->m_ImageActors[ i ]->SetInterpolate( v );
401 this->m_Interpolate = v;
407 // -------------------------------------------------------------------------
408 void cpExtensions::Visualization::ImageSliceActors::
411 this->SetInterpolate( true );
414 // -------------------------------------------------------------------------
415 void cpExtensions::Visualization::ImageSliceActors::
418 this->SetInterpolate( false );
421 // -------------------------------------------------------------------------
422 double* cpExtensions::Visualization::ImageSliceActors::
423 GetDisplayBounds( ) const
425 if( this->m_ImageActors.size( ) > 0 )
426 return( this->m_ImageActors[ 0 ]->GetDisplayBounds( ) );
431 // -------------------------------------------------------------------------
432 void cpExtensions::Visualization::ImageSliceActors::
433 GetDisplayBounds( double bounds[ 6 ] ) const
435 if( this->m_ImageActors.size( ) == 0 )
437 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
438 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
441 this->m_ImageActors[ 0 ]->GetDisplayBounds( bounds );
444 // -------------------------------------------------------------------------
445 void cpExtensions::Visualization::ImageSliceActors::
448 vtkPoints* points = this->m_Cursor->GetPoints( );
449 points->SetPoint( 0, 0, 0, 0 );
450 points->SetPoint( 1, 0, 0, 0 );
451 points->SetPoint( 2, 0, 0, 0 );
452 points->SetPoint( 3, 0, 0, 0 );
453 points->SetPoint( 4, 0, 0, 0 );
454 points->SetPoint( 5, 0, 0, 0 );
455 points->SetPoint( 6, 0, 0, 0 );
456 points->SetPoint( 7, 0, 0, 0 );
457 this->m_Cursor->Modified( );
458 this->m_CursorMapper->Modified( );
459 this->m_CursorActor->Modified( );
462 // -------------------------------------------------------------------------
463 void cpExtensions::Visualization::ImageSliceActors::
464 SetCursor( double pos[ 3 ] )
466 if( this->m_SliceMappers.size( ) == 0 )
470 int a0 = this->GetAxis( );
471 int a1 = ( a0 + 1 ) % 3;
472 int a2 = ( a0 + 2 ) % 3;
478 this->m_SliceMappers[ 0 ]->GetInput( )->GetBounds( bounds );
481 p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
482 p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
484 p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
485 p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
486 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
487 p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
488 p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
489 p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
490 p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
491 p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
493 vtkPoints* points = this->m_Cursor->GetPoints( );
494 points->SetPoint( 0, p0 );
495 points->SetPoint( 1, p1 );
496 points->SetPoint( 2, p2 );
497 points->SetPoint( 3, p3 );
498 points->SetPoint( 4, p4 );
499 points->SetPoint( 5, p5 );
500 points->SetPoint( 6, p6 );
501 points->SetPoint( 7, p7 );
502 this->m_Cursor->Modified( );
503 this->m_CursorMapper->Modified( );
504 this->m_CursorActor->Modified( );
507 // -------------------------------------------------------------------------
508 vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
509 GetImageMap( unsigned int id )
511 if( id < this->m_ImageMaps.size( ) )
512 return( this->m_ImageMaps[ id ].GetPointer( ) );
517 // -------------------------------------------------------------------------
518 const vtkImageMapToColors* cpExtensions::Visualization::ImageSliceActors::
519 GetImageMap( unsigned int id ) const
521 if( id < this->m_ImageMaps.size( ) )
522 return( this->m_ImageMaps[ id ].GetPointer( ) );
527 // -------------------------------------------------------------------------
528 double cpExtensions::Visualization::ImageSliceActors::
529 GetMinWindow( ) const
531 return( this->m_MinWindow );
534 // -------------------------------------------------------------------------
535 double cpExtensions::Visualization::ImageSliceActors::
536 GetMaxWindow( ) const
538 return( this->m_MaxWindow );
541 // -------------------------------------------------------------------------
542 double cpExtensions::Visualization::ImageSliceActors::
545 return( this->m_MinLevel );
548 // -------------------------------------------------------------------------
549 double cpExtensions::Visualization::ImageSliceActors::
552 return( this->m_MaxLevel );
555 // -------------------------------------------------------------------------
556 double cpExtensions::Visualization::ImageSliceActors::
559 if( this->m_ImageMaps.size( ) == 0 )
560 return( double( 0 ) );
561 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
562 return( double( 0 ) );
564 vtkWindowLevelLookupTable* lut =
565 dynamic_cast< vtkWindowLevelLookupTable* >(
566 this->m_ImageMaps[ 0 ]->GetLookupTable( )
569 return( lut->GetWindow( ) );
571 return( double( 0 ) );
574 // -------------------------------------------------------------------------
575 double cpExtensions::Visualization::ImageSliceActors::
578 if( this->m_ImageMaps.size( ) == 0 )
579 return( double( 0 ) );
580 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
581 return( double( 0 ) );
583 vtkWindowLevelLookupTable* lut =
584 dynamic_cast< vtkWindowLevelLookupTable* >(
585 this->m_ImageMaps[ 0 ]->GetLookupTable( )
588 return( lut->GetLevel( ) );
590 return( double( 0 ) );
593 // -------------------------------------------------------------------------
594 void cpExtensions::Visualization::ImageSliceActors::
595 SetWindow( double w )
597 if( this->m_ImageMaps.size( ) == 0 )
599 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
601 vtkWindowLevelLookupTable* lut =
602 dynamic_cast< vtkWindowLevelLookupTable* >(
603 this->m_ImageMaps[ 0 ]->GetLookupTable( )
608 /* TODO: Min and Max values are assigned 0!!!
609 if( w < this->MinWindow )
611 if( w > this->MaxWindow )
616 this->m_ImageMaps[ 0 ]->Modified( );
620 // -------------------------------------------------------------------------
621 void cpExtensions::Visualization::ImageSliceActors::
624 if( this->m_ImageMaps.size( ) == 0 )
626 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
628 vtkWindowLevelLookupTable* lut =
629 dynamic_cast< vtkWindowLevelLookupTable* >(
630 this->m_ImageMaps[ 0 ]->GetLookupTable( )
635 /* TODO: Min and Max values are assigned 0!!!
636 if( l < this->MinLevel )
638 if( l > this->MaxLevel )
643 this->m_ImageMaps[ 0 ]->Modified( );
647 // -------------------------------------------------------------------------
648 void cpExtensions::Visualization::ImageSliceActors::
649 SetWindowLevel( double w, double l )
651 if( this->m_ImageMaps.size( ) == 0 )
653 if( this->m_ImageMaps[ 0 ].GetPointer( ) == NULL )
655 vtkWindowLevelLookupTable* lut =
656 dynamic_cast< vtkWindowLevelLookupTable* >(
657 this->m_ImageMaps[ 0 ]->GetLookupTable( )
662 /* TODO: Min and Max values are assigned 0!!!
663 if( w < this->MinWindow )
665 if( w > this->MaxWindow )
667 if( l < this->MinLevel )
669 if( l > this->MaxLevel )
675 this->m_ImageMaps[ 0 ]->Modified( );
676 this->UpdateText( w, l );
680 // -------------------------------------------------------------------------
681 void cpExtensions::Visualization::ImageSliceActors::
684 static const double _2 = double( 2 );
685 this->SetWindowLevel(
686 this->m_MaxWindow, ( this->m_MaxLevel + this->m_MinLevel ) / _2
690 // -------------------------------------------------------------------------
691 void cpExtensions::Visualization::ImageSliceActors::
692 SetLookupTable( unsigned int id, vtkLookupTable* lut )
694 if( id < this->m_ImageMaps.size( ) && id > 0 )
696 if( this->m_ImageMaps[ id ].GetPointer( ) != NULL )
698 this->m_ImageMaps[ id ]->SetLookupTable( lut );
699 this->m_ImageMaps[ id ]->Modified( );
707 // -------------------------------------------------------------------------
708 void cpExtensions::Visualization::ImageSliceActors::
709 SetLookupTableAsColor( unsigned int id, double r, double g, double b )
711 static const double _0 = double( 0 );
712 static const double _2 = double( 2 );
713 static const double _4 = double( 4 );
714 static const double _6 = double( 6 );
715 static const double _OPACITY = double( 0.6 );
717 // Check ID consistency
718 if( id == 0 || id >= this->m_ImageMaps.size( ) )
721 // Get image scalar range
722 vtkAlgorithmOutput* aout = this->m_ImageMaps[ id ]->GetOutputPort( );
723 vtkImageData* image = dynamic_cast< vtkImageData* >(
724 aout->GetProducer( )->GetOutputDataObject( aout->GetIndex( ) )
727 image->GetScalarRange( range );
729 // Get HSV from display color
730 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
731 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
732 double d = cmax - cmin;
734 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
740 hue = std::fmod( ( g - b ) / d, _6 );
742 hue = ( ( b - r ) / d ) + _2;
744 hue = ( ( r - g ) / d ) + _4;
749 // Define new lookup table
750 vtkSmartPointer< vtkLookupTable > lut =
751 vtkSmartPointer< vtkLookupTable >::New( );
752 lut->SetScaleToLinear( );
753 lut->SetNanColor( _0, _0, _0, _0 );
754 lut->SetTableRange( range[ 0 ], range[ 1 ] );
755 lut->SetAlphaRange( _0, _OPACITY );
756 lut->SetHueRange( _0, hue );
757 lut->SetSaturationRange( _0, saturation );
758 lut->SetValueRange( _0, value );
760 this->SetLookupTable( id, lut );
763 // -------------------------------------------------------------------------
764 int cpExtensions::Visualization::ImageSliceActors::
767 if( this->m_SliceMappers.size( ) > 0 )
768 return( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
773 // -------------------------------------------------------------------------
774 int cpExtensions::Visualization::ImageSliceActors::
775 GetSliceNumber( ) const
777 if( this->m_SliceMappers.size( ) > 0 )
778 return( this->m_SliceMappers[ 0 ]->GetSliceNumber( ) );
783 // -------------------------------------------------------------------------
784 int cpExtensions::Visualization::ImageSliceActors::
785 GetSliceNumberMinValue( ) const
787 if( this->m_SliceMappers.size( ) > 0 )
788 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
793 // -------------------------------------------------------------------------
794 int cpExtensions::Visualization::ImageSliceActors::
795 GetSliceNumberMaxValue( ) const
797 if( this->m_SliceMappers.size( ) > 0 )
798 return( this->m_SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
803 // -------------------------------------------------------------------------
804 void cpExtensions::Visualization::ImageSliceActors::
805 SetSliceNumber( const int& slice )
807 unsigned int nImages = this->m_SliceMappers.size( );
811 // Change visualization extent
812 for( unsigned int i = 0; i < nImages; ++i )
814 this->m_SliceMappers[ i ]->SetSliceNumber( slice );
815 this->m_SliceMappers[ i ]->Modified( );
816 this->m_ImageActors[ i ]->Modified( );
817 this->m_SliceMappers[ i ]->Update( );
822 vtkAlgorithm* algo = this->m_SliceMappers[ 0 ]->GetInputAlgorithm( );
823 vtkInformation* info = algo->GetOutputInformation( 0 );
825 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
826 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
827 info->Get( vtkDataObject::ORIGIN( ), ori );
828 info->Get( vtkDataObject::SPACING( ), spac );
829 this->m_SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
831 // Prevent obscuring voxels by offsetting the plane geometry
834 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
835 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
839 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
840 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
844 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
845 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
848 if( spac[ 0 ] < double( 0 ) )
850 double t = xbnds[ 0 ];
851 xbnds[ 0 ] = xbnds[ 1 ];
855 if( spac[ 1 ] < double( 0 ) )
857 double t = ybnds[ 0 ];
858 ybnds[ 0 ] = ybnds[ 1 ];
862 if( spac[ 2 ] < double( 0 ) )
864 double t = zbnds[ 0 ];
865 zbnds[ 0 ] = zbnds[ 1 ];
870 // Plane function origin
871 this->m_PlaneFunction->SetOrigin( pos );
873 // Configure visualization and implicit plane orientation
874 int axis = this->m_SliceMappers[ 0 ]->GetOrientation( );
875 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
876 this->m_PlaneActor->GetProperty( )->SetLineWidth( 2 );
877 vtkPoints* plane_points = this->m_Plane->GetPoints( );
878 if( axis == 0 ) // YZ, x-normal
880 this->m_PlaneFunction->SetNormal( 1, 0, 0 );
881 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
882 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
883 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
884 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
885 this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
887 else if( axis == 1 ) // ZX, y-normal
889 this->m_PlaneFunction->SetNormal( 0, 1, 0 );
890 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
891 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
892 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
893 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
894 this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
898 this->m_PlaneFunction->SetNormal( 0, 0, 1 );
899 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
900 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
901 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
902 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
903 this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
906 this->m_PlaneFunction->Modified( );
907 this->m_Plane->Modified( );
908 this->m_PlaneMapper->Modified( );
909 this->m_PlaneActor->Modified( );
911 // Prepare other actors to update
913 for( unsigned int i = 0; i < this->OtherActors.size( ); ++i )
915 if( this->OtherActors[ i ].first.GetPointer( ) != NULL )
917 this->OtherActors[ i ].first->Modified( );
918 this->OtherActors[ i ].first->Update( );
922 if( this->OtherActors[ i ].second.GetPointer( ) != NULL )
924 this->OtherActors[ i ].second->GetMapper( )->Modified( );
925 this->OtherActors[ i ].second->Modified( );
931 if( this->m_UpdateCommand != NULL )
932 this->m_UpdateCommand( this->m_UpdateData );
939 for( unsigned int i = 0; i < this->m_AssociatedSlices.size( ); ++i )
940 if( this->m_AssociatedSlices[ i ]->GetAxis( ) == this->GetAxis( ) )
941 this->m_AssociatedSlices[ i ]->SetSliceNumber( slice );
944 // -------------------------------------------------------------------------
945 void cpExtensions::Visualization::ImageSliceActors::
946 SetSlice( double* pos )
949 if( this->m_ImageMaps[ 0 ] != NULL )
951 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
953 image = this->m_SliceMappers[ 0 ]->GetInput( );
957 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
958 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
961 // -------------------------------------------------------------------------
962 void cpExtensions::Visualization::ImageSliceActors::
965 if( this->m_SliceMappers.size( ) > 0 )
968 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
969 if ( axId == 0 ) axis = 'X';
970 else if( axId == 1 ) axis = 'Y';
971 else if( axId == 2 ) axis = 'Z';
974 this->m_TextBuffer, "Axis: %c (%d)",
975 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( )
979 this->m_TextBuffer[ 0 ] = '\0';
980 this->m_TextActor->SetInput( this->m_TextBuffer );
981 this->m_TextActor->Modified( );
985 // -------------------------------------------------------------------------
986 void cpExtensions::Visualization::ImageSliceActors::
987 UpdateText( double pos[ 3 ] )
989 if( this->m_SliceMappers.size( ) > 0 )
992 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
993 if ( axId == 0 ) axis = 'X';
994 else if( axId == 1 ) axis = 'Y';
995 else if( axId == 2 ) axis = 'Z';
996 int slice = this->GetSliceNumber( );
999 if( this->m_ImageMaps[ 0 ] != NULL )
1001 dynamic_cast< vtkImageData* >( this->m_ImageMaps[ 0 ]->GetInput( ) );
1003 image = this->m_SliceMappers[ 0 ]->GetInput( );
1006 double pcoords[ 3 ];
1007 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
1008 ijk[ axId ] = slice;
1011 image->GetExtent( ext );
1013 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
1014 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
1015 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
1018 int nScl = image->GetNumberOfScalarComponents( );
1019 std::stringstream str;
1023 << "," << ijk[ 2 ] << "]=(";
1025 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
1026 for( int n = 1; n < nScl; ++n )
1029 << image->GetScalarComponentAsFloat(
1030 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
1034 this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
1035 axis, slice, str.str( ).c_str( )
1041 this->m_TextBuffer[ 0 ] = '\0';
1042 this->m_TextActor->SetInput( this->m_TextBuffer );
1043 this->m_TextActor->Modified( );
1047 // -------------------------------------------------------------------------
1048 void cpExtensions::Visualization::ImageSliceActors::
1049 UpdateText( const double& w, const double& l )
1051 if( this->m_SliceMappers.size( ) > 0 )
1054 int axId = this->m_SliceMappers[ 0 ]->GetOrientation( );
1055 if ( axId == 0 ) axis = 'X';
1056 else if( axId == 1 ) axis = 'Y';
1057 else if( axId == 2 ) axis = 'Z';
1060 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
1061 axis, this->m_SliceMappers[ 0 ]->GetSliceNumber( ), w, l
1065 this->m_TextBuffer[ 0 ] = '\0';
1066 this->m_TextActor->SetInput( this->m_TextBuffer );
1067 this->m_TextActor->Modified( );
1071 // -------------------------------------------------------------------------
1072 cpExtensions::Visualization::ImageSliceActors::
1075 m_Interpolate( false )
1078 this->_ConfigureStyle( );
1081 // -------------------------------------------------------------------------
1082 cpExtensions::Visualization::ImageSliceActors::
1083 ~ImageSliceActors( )
1087 // -------------------------------------------------------------------------
1088 void cpExtensions::Visualization::ImageSliceActors::
1091 // Connect this view with a controller
1092 this->m_Style = vtkSmartPointer< ImageInteractorStyle >::New( );
1093 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1094 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1095 this->m_Style->AddMouseDoubleClickCommand(
1096 Self::_MouseDoubleClickCommand, this
1098 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1099 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1102 // -------------------------------------------------------------------------
1103 void cpExtensions::Visualization::ImageSliceActors::
1104 _ConfigureNewLUT( vtkImageData* data )
1106 // Does the new LUT is a window-level one?
1107 unsigned int nImgs = this->m_ImageMaps.size( );
1108 unsigned int nCmps = data->GetNumberOfScalarComponents( );
1112 this->m_ImageMaps.push_back( NULL );
1117 // Create LUT filter
1118 this->m_ImageMaps.push_back( vtkSmartPointer< vtkImageMapToColors >::New( ) );
1122 data->GetScalarRange( range );
1123 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
1124 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
1125 lut->SetScaleToLinear( );
1126 lut->SetTableRange( range );
1127 lut->SetWindow( range[ 1 ] - range[ 0 ] );
1128 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
1130 this->m_ImageMaps[ 0 ]->SetLookupTable( lut );
1132 this->m_MinWindow = double( 0 );
1133 this->m_MaxWindow = range[ 1 ] - range[ 0 ];
1134 this->m_MinLevel = range[ 0 ];
1135 this->m_MaxLevel = range[ 1 ];
1138 this->SetLookupTableAsColor( nImgs, 1, 0, 0 );
1141 // -------------------------------------------------------------------------
1142 void cpExtensions::Visualization::ImageSliceActors::
1143 _ConfigureNewInput( int axis )
1145 unsigned int nImages = this->m_ImageActors.size( );
1148 vtkImageSliceMapper* mapper = this->m_SliceMappers[ nImages ];
1150 mapper->SetOrientation( axis );
1152 mapper->SetOrientation( this->m_SliceMappers[ 0 ]->GetOrientation( ) );
1156 vtkSmartPointer< vtkImageActor > actor =
1157 vtkSmartPointer< vtkImageActor >::New( );
1158 this->m_ImageActors.push_back( actor );
1159 actor->SetMapper( mapper );
1160 actor->SetInterpolate( this->m_Interpolate );
1164 this->m_Style->AssociateImageActor( actor );
1165 this->AddItem( actor );
1168 this->SetSliceNumber( this->GetSliceNumber( ) );
1170 this->SetSliceNumber( this->GetSliceNumberMaxValue( ) );
1174 // -------------------------------------------------------------------------
1175 void cpExtensions::Visualization::ImageSliceActors::
1178 const ImageInteractorStyle::ButtonID& btn, double* pos,
1179 bool alt, bool ctr, bool sft
1182 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1183 if( actors == NULL )
1186 if( btn == ImageInteractorStyle::ButtonID_None )
1188 actors->SetCursor( pos );
1189 actors->UpdateText( pos );
1190 actors->GetStyle( )->GetInteractor( )->Render( );
1192 else if( btn == ImageInteractorStyle::ButtonID_Left )
1194 unsigned int nC = actors->m_SlicesCommands.size( );
1195 if( !alt && ctr && !sft && nC > 0 )
1197 for( unsigned int i = 0; i < nC; ++i )
1198 actors->m_SlicesCommands[ i ].first(
1199 pos, actors->GetAxis( ), actors->m_SlicesCommands[ i ].second
1201 actors->GetStyle( )->GetInteractor( )->Render( );
1205 else if( btn == ImageInteractorStyle::ButtonID_Middle )
1208 else if( btn == ImageInteractorStyle::ButtonID_Right )
1210 if( !alt && !ctr && sft )
1213 actors->m_SliceMappers[ 0 ]->GetBounds( bounds );
1215 int a0 = actors->GetAxis( );
1216 int a1 = ( a0 + 1 ) % 3;
1217 int a2 = ( a0 + 2 ) % 3;
1218 double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1219 double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1220 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1221 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1223 dx *= actors->m_StartWindowLevel[ 0 ];
1224 dy *= actors->m_StartWindowLevel[ 1 ];
1225 actors->SetWindowLevel(
1226 actors->m_StartWindowLevel[ 0 ] + dx,
1227 actors->m_StartWindowLevel[ 1 ] + dy
1229 actors->GetStyle( )->GetInteractor( )->Render( );
1236 // -------------------------------------------------------------------------
1237 void cpExtensions::Visualization::ImageSliceActors::
1240 const ImageInteractorStyle::ButtonID& btn, double* pos,
1241 bool alt, bool ctr, bool sft
1244 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1245 if( actors == NULL )
1248 actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1249 actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1250 actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1251 actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1252 actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1255 // -------------------------------------------------------------------------
1256 void cpExtensions::Visualization::ImageSliceActors::
1257 _MouseDoubleClickCommand(
1259 const ImageInteractorStyle::ButtonID& btn, double* pos,
1260 bool alt, bool ctr, bool sft
1263 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1264 if( actors == NULL )
1267 if( btn == ImageInteractorStyle::ButtonID_Left )
1269 unsigned int nC = actors->m_SeedCommands.size( );
1270 if( !alt && ctr && !sft && nC > 0 )
1272 for( unsigned int i = 0; i < nC; ++i )
1273 actors->m_SeedCommands[ i ].first(
1274 pos, actors->GetAxis( ), actors->m_SeedCommands[ i ].second
1276 actors->GetStyle( )->GetInteractor( )->Render( );
1280 else if( btn == ImageInteractorStyle::ButtonID_Middle )
1283 else if( btn == ImageInteractorStyle::ButtonID_Right )
1288 // -------------------------------------------------------------------------
1289 void cpExtensions::Visualization::ImageSliceActors::
1292 const int& dir, bool alt, bool ctr, bool sft
1295 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1296 if( actors == NULL )
1299 if( !alt && !ctr && !sft )
1301 actors->SetSliceNumber( actors->GetSliceNumber( ) + dir );
1302 actors->GetStyle( )->GetInteractor( )->Render( );
1304 else if( !alt && !ctr && sft )
1306 actors->SetSliceNumber( actors->GetSliceNumber( ) + ( dir * 10 ) );
1307 actors->GetStyle( )->GetInteractor( )->Render( );
1312 // -------------------------------------------------------------------------
1313 void cpExtensions::Visualization::ImageSliceActors::
1314 _KeyCommand( void* data, const char& key )