1 #include <cpExtensions/Visualization/ImageSliceActors.h>
5 #include <vtkAlgorithmOutput.h>
7 #include <vtkCellArray.h>
8 #include <vtkImageData.h>
10 #include <vtkProperty.h>
11 #include <vtkRenderer.h>
12 #include <vtkRendererCollection.h>
13 #include <vtkRenderWindow.h>
14 #include <vtkRenderWindowInteractor.h>
15 #include <vtkTextProperty.h>
17 // -------------------------------------------------------------------------
18 cpExtensions::Visualization::ImageSliceActors*
19 cpExtensions::Visualization::ImageSliceActors::
22 return( new Self( ) );
25 // -------------------------------------------------------------------------
26 cpExtensions::Visualization::
27 ImageBlender* cpExtensions::Visualization::ImageSliceActors::
30 return( this->m_ImageBlender );
33 // -------------------------------------------------------------------------
34 const cpExtensions::Visualization::
35 ImageBlender* cpExtensions::Visualization::ImageSliceActors::
38 return( this->m_ImageBlender );
41 // -------------------------------------------------------------------------
42 void cpExtensions::Visualization::ImageSliceActors::
43 SetBlender( ImageBlender* blender )
45 this->m_ImageBlender = blender;
47 // Create mapper and actors
48 this->m_ImageMapper = vtkSmartPointer< vtkImageSliceMapper >::New( );
49 this->m_ImageMapper->SetInputConnection(
50 this->m_ImageBlender->GetOutputPort( )
52 this->m_ImageMapper->SetOrientation( 0 );
53 this->m_ImageMapper->Update( );
56 this->m_ImageActor = vtkSmartPointer< vtkImageActor >::New( );
57 this->m_ImageActor->SetMapper( this->m_ImageMapper );
58 this->m_ImageActor->SetInterpolate( this->m_Interpolate );
59 this->m_ImageActor->Modified( );
61 if( this->m_Style.GetPointer( ) != NULL )
62 this->m_Style->AssociateImageActor( this->m_ImageActor );
63 this->AddItem( this->m_ImageActor );
65 this->SetSliceNumber( this->GetSliceNumberMinValue( ) );
70 // -------------------------------------------------------------------------
71 void cpExtensions::Visualization::ImageSliceActors::
74 this->m_ImageMapper->SetOrientation( axis );
75 this->m_ImageMapper->Update( );
76 this->SetSliceNumber( this->GetSliceNumberMinValue( ) );
77 this->m_ImageActor->Modified( );
82 // -------------------------------------------------------------------------
83 void cpExtensions::Visualization::ImageSliceActors::
84 AddInputConnection( vtkAlgorithmOutput* aout )
86 // Get input vtkImageData
90 if( this->m_ImageBlender.GetPointer( ) == NULL )
92 // Try to infere if input comes from a color mapping filter
93 vtkAlgorithm* producer = aout->GetProducer( );
94 vtkSmartPointer< ImageBlender > blender =
95 dynamic_cast< ImageBlender* >( producer );
96 if( blender.GetPointer( ) == NULL )
98 // Ok, create a new blender with default window level
99 vtkImageData* data = dynamic_cast< vtkImageData* >(
100 producer->GetOutputDataObject( aout->GetIndex( ) )
103 data->GetScalarRange( r );
105 blender = vtkSmartPointer< ImageBlender >::New( );
106 blender->AddInputConnection( aout );
107 blender->SetWindow( r[ 1 ] - r[ 0 ] );
108 blender->SetLevel( ( r[ 1 ] + r[ 0 ] ) / double( 2 ) );
111 this->SetBlender( blender );
114 this->m_ImageBlender->AddInputConnection( aout );
117 // -------------------------------------------------------------------------
118 void cpExtensions::Visualization::ImageSliceActors::
119 AddInputData( vtkImageData* data )
121 // Get input vtkImageData
125 if( this->m_ImageBlender.GetPointer( ) == NULL )
127 // Create a new blender with default window level
129 data->GetScalarRange( r );
131 vtkSmartPointer< ImageBlender > blender =
132 vtkSmartPointer< ImageBlender >::New( );
133 blender->AddInputData( data );
134 blender->SetWindow( r[ 1 ] - r[ 0 ] );
135 blender->SetLevel( ( r[ 1 ] + r[ 0 ] ) / double( 2 ) );
136 this->SetBlender( blender );
139 this->m_ImageBlender->AddInputData( data );
142 // -------------------------------------------------------------------------
143 void cpExtensions::Visualization::ImageSliceActors::
147 this->m_VisibleExtent[ 0 ] =
148 this->m_VisibleExtent[ 2 ] =
149 this->m_VisibleExtent[ 4 ] = -1;
150 this->m_VisibleExtent[ 1 ] =
151 this->m_VisibleExtent[ 3 ] =
152 this->m_VisibleExtent[ 5 ] = 0;
153 this->m_VisibleBounds[ 0 ] =
154 this->m_VisibleBounds[ 2 ] =
155 this->m_VisibleBounds[ 4 ] = double( 0 );
156 this->m_VisibleBounds[ 1 ] =
157 this->m_VisibleBounds[ 3 ] =
158 this->m_VisibleBounds[ 5 ] = double( 0 );
160 // Unbind from container
161 this->RemoveAllItems( );
164 this->m_ImageActor = NULL;
165 this->m_ImageMapper = NULL;
166 this->m_ImageBlender = NULL;
168 // Reconfigure unique objects
169 this->m_Cursor = vtkSmartPointer< vtkPolyData >::New( );
170 this->m_CursorMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
171 this->m_CursorActor = vtkSmartPointer< vtkActor >::New( );
172 this->m_HorizontalLine = vtkSmartPointer< vtkPolyData >::New( );
173 this->m_HorizontalLineMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
174 this->m_HorizontalLineActor = vtkSmartPointer< vtkActor >::New( );
175 this->m_VerticalLine = vtkSmartPointer< vtkPolyData >::New( );
176 this->m_VerticalLineMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
177 this->m_VerticalLineActor = vtkSmartPointer< vtkActor >::New( );
178 this->m_Plane = vtkSmartPointer< vtkPolyData >::New( );
179 this->m_PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
180 this->m_TextActor = vtkSmartPointer< vtkTextActor >::New( );
181 this->m_PlaneActor = vtkSmartPointer< vtkActor >::New( );
182 this->m_TextBuffer[ 0 ] = '\0';
184 // Unique objects configuration
185 vtkSmartPointer< vtkPoints > cursor_points =
186 vtkSmartPointer< vtkPoints >::New( );
187 vtkSmartPointer< vtkCellArray > cursor_lines =
188 vtkSmartPointer< vtkCellArray >::New( );
189 cursor_points->InsertNextPoint( 0, 0, 0 );
190 cursor_points->InsertNextPoint( 0, 0, 0 );
191 cursor_points->InsertNextPoint( 0, 0, 0 );
192 cursor_points->InsertNextPoint( 0, 0, 0 );
193 cursor_points->InsertNextPoint( 0, 0, 0 );
194 cursor_points->InsertNextPoint( 0, 0, 0 );
195 cursor_points->InsertNextPoint( 0, 0, 0 );
196 cursor_points->InsertNextPoint( 0, 0, 0 );
197 cursor_lines->InsertNextCell( 2 );
198 cursor_lines->InsertCellPoint( 0 );
199 cursor_lines->InsertCellPoint( 1 );
200 cursor_lines->InsertNextCell( 2 );
201 cursor_lines->InsertCellPoint( 2 );
202 cursor_lines->InsertCellPoint( 3 );
203 cursor_lines->InsertNextCell( 2 );
204 cursor_lines->InsertCellPoint( 4 );
205 cursor_lines->InsertCellPoint( 5 );
206 cursor_lines->InsertNextCell( 2 );
207 cursor_lines->InsertCellPoint( 6 );
208 cursor_lines->InsertCellPoint( 7 );
209 this->m_Cursor->SetPoints( cursor_points );
210 this->m_Cursor->SetLines( cursor_lines );
211 this->m_CursorMapper->SetInputData( this->m_Cursor );
212 this->m_CursorActor->SetMapper( this->m_CursorMapper );
214 vtkSmartPointer< vtkPoints > h_points =
215 vtkSmartPointer< vtkPoints >::New( );
216 vtkSmartPointer< vtkCellArray > h_lines =
217 vtkSmartPointer< vtkCellArray >::New( );
218 h_points->InsertNextPoint( 0, 0, 0 );
219 h_points->InsertNextPoint( 0, 0, 0 );
220 h_lines->InsertNextCell( 2 );
221 h_lines->InsertCellPoint( 0 );
222 h_lines->InsertCellPoint( 1 );
223 this->m_HorizontalLine->SetPoints( h_points );
224 this->m_HorizontalLine->SetLines( h_lines );
225 this->m_HorizontalLineMapper->SetInputData( this->m_HorizontalLine );
226 this->m_HorizontalLineActor->SetMapper( this->m_HorizontalLineMapper );
228 vtkSmartPointer< vtkPoints > v_points =
229 vtkSmartPointer< vtkPoints >::New( );
230 vtkSmartPointer< vtkCellArray > v_lines =
231 vtkSmartPointer< vtkCellArray >::New( );
232 v_points->InsertNextPoint( 0, 0, 0 );
233 v_points->InsertNextPoint( 0, 0, 0 );
234 v_lines->InsertNextCell( 2 );
235 v_lines->InsertCellPoint( 0 );
236 v_lines->InsertCellPoint( 1 );
237 this->m_VerticalLine->SetPoints( v_points );
238 this->m_VerticalLine->SetLines( v_lines );
239 this->m_VerticalLineMapper->SetInputData( this->m_VerticalLine );
240 this->m_VerticalLineActor->SetMapper( this->m_VerticalLineMapper );
242 vtkSmartPointer< vtkPoints > plane_points =
243 vtkSmartPointer< vtkPoints >::New( );
244 vtkSmartPointer< vtkCellArray > plane_lines =
245 vtkSmartPointer< vtkCellArray >::New( );
247 plane_points->InsertNextPoint( 0, 0, 0 );
248 plane_points->InsertNextPoint( 0, 1, 0 );
249 plane_points->InsertNextPoint( 1, 1, 0 );
250 plane_points->InsertNextPoint( 1, 0, 0 );
251 plane_lines->InsertNextCell( 5 );
252 plane_lines->InsertCellPoint( 0 );
253 plane_lines->InsertCellPoint( 1 );
254 plane_lines->InsertCellPoint( 2 );
255 plane_lines->InsertCellPoint( 3 );
256 plane_lines->InsertCellPoint( 0 );
257 this->m_Plane->SetPoints( plane_points );
258 this->m_Plane->SetLines( plane_lines );
260 this->m_PlaneMapper->SetInputData( this->m_Plane );
261 this->m_PlaneActor->SetMapper( this->m_PlaneMapper );
263 this->m_TextActor->SetTextScaleModeToNone( );
264 vtkTextProperty* textprop = this->m_TextActor->GetTextProperty( );
265 textprop->SetColor( 1, 1, 1 );
266 textprop->SetFontFamilyToCourier( );
267 textprop->SetFontSize( 18 );
268 textprop->BoldOff( );
269 textprop->ItalicOff( );
270 textprop->ShadowOff( );
271 textprop->SetJustificationToLeft( );
272 textprop->SetVerticalJustificationToBottom( );
273 vtkCoordinate* coord = this->m_TextActor->GetPositionCoordinate( );
274 coord->SetCoordinateSystemToNormalizedViewport( );
275 coord->SetValue( 0.01, 0.01 );
277 // Update actor collection
278 this->AddItem( this->m_CursorActor );
279 this->AddItem( this->m_HorizontalLineActor );
280 this->AddItem( this->m_VerticalLineActor );
281 this->AddItem( this->m_TextActor );
282 this->AddItem( this->m_PlaneActor );
285 // -------------------------------------------------------------------------
286 void cpExtensions::Visualization::ImageSliceActors::
287 AssociateSlice( Self* slice )
289 this->m_AssociatedSlices.push_back( slice );
293 // -------------------------------------------------------------------------
294 vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
297 return( this->m_Style.GetPointer( ) );
300 // -------------------------------------------------------------------------
301 const vtkInteractorStyle* cpExtensions::Visualization::ImageSliceActors::
304 return( this->m_Style.GetPointer( ) );
307 // -------------------------------------------------------------------------
308 vtkImageData* cpExtensions::Visualization::ImageSliceActors::
309 GetInputImage( unsigned int id )
311 if( this->m_ImageBlender.GetPointer( ) != NULL )
313 dynamic_cast< vtkImageData* >( this->m_ImageBlender->GetInput( id ) )
319 // -------------------------------------------------------------------------
320 const vtkImageData* cpExtensions::Visualization::ImageSliceActors::
321 GetInputImage( unsigned int id ) const
323 if( this->m_ImageBlender.GetPointer( ) != NULL )
325 dynamic_cast< const vtkImageData* >(
326 this->m_ImageBlender->GetInput( )
333 // -------------------------------------------------------------------------
334 void cpExtensions::Visualization::ImageSliceActors::
335 PushActorsInto( vtkRenderWindow* window, bool force_style )
337 this->m_Window = window;
340 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
341 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
342 if( rwi == NULL || renderer == NULL )
346 if( this->m_Style.GetPointer( ) != NULL && force_style )
347 rwi->SetInteractorStyle( this->m_Style );
351 this->InitTraversal( );
352 while( prop = this->GetNextProp( ) )
353 renderer->AddViewProp( prop );
354 renderer->Modified( );
357 renderer->RemoveViewProp( this->m_CursorActor );
358 renderer->RemoveViewProp( this->m_TextActor );
363 vtkCamera* camera = renderer->GetActiveCamera( );
364 if( camera != NULL && force_style )
366 // Parallel projections are better when displaying 2D images
367 int axis = this->GetAxis( );
368 camera->ParallelProjectionOn( );
369 camera->SetFocalPoint( double( 0 ), double( 0 ), double( 0 ) );
372 camera->SetPosition( double( 1 ), double( 0 ), double( 0 ) );
373 camera->SetViewUp ( double( 0 ), double( 0 ), double( 1 ) );
377 camera->SetPosition( double( 0 ), double( -1 ), double( 0 ) );
378 camera->SetViewUp ( double( 0 ), double( 0 ), double( 1 ) );
380 else // if( axis == 2 )
382 camera->SetPosition( double( 0 ), double( 0 ), double( 1 ) );
383 camera->SetViewUp ( double( 0 ), double( -1 ), double( 0 ) );
388 this->ResetCamera( );
392 // -------------------------------------------------------------------------
393 void cpExtensions::Visualization::ImageSliceActors::
394 PopActorsFrom( vtkRenderWindow* window )
396 vtkRenderWindowInteractor* rwi = window->GetInteractor( );
397 vtkRenderer* renderer = window->GetRenderers( )->GetFirstRenderer( );
399 if( renderer != NULL )
403 this->InitTraversal( );
404 while( prop = this->GetNextProp( ) )
405 renderer->RemoveViewProp( prop );
406 renderer->Modified( );
413 // -------------------------------------------------------------------------
414 vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
417 return( this->m_ImageActor );
420 // -------------------------------------------------------------------------
421 const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
422 GetImageActor( ) const
424 return( this->m_ImageActor );
427 // -------------------------------------------------------------------------
428 vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
431 return( this->m_TextActor );
434 // -------------------------------------------------------------------------
435 const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
436 GetTextActor( ) const
438 return( this->m_TextActor );
441 // -------------------------------------------------------------------------
442 vtkActor* cpExtensions::Visualization::ImageSliceActors::
445 return( this->m_PlaneActor );
448 // -------------------------------------------------------------------------
449 const vtkActor* cpExtensions::Visualization::ImageSliceActors::
450 GetPlaneActor( ) const
452 return( this->m_PlaneActor );
455 // -------------------------------------------------------------------------
456 vtkPlane* cpExtensions::Visualization::ImageSliceActors::
459 if( this->m_ImageMapper.GetPointer( ) != NULL )
460 return( this->m_ImageMapper->GetSlicePlane( ) );
465 // -------------------------------------------------------------------------
466 const vtkPlane* cpExtensions::Visualization::ImageSliceActors::
467 GetPlaneFunction( ) const
469 if( this->m_ImageMapper.GetPointer( ) != NULL )
470 return( this->m_ImageMapper->GetSlicePlane( ) );
475 // -------------------------------------------------------------------------
476 void cpExtensions::Visualization::ImageSliceActors::
477 SetInterpolate( bool v )
479 if( this->m_Interpolate != v )
481 this->m_ImageActor->SetInterpolate( v );
482 this->m_Interpolate = v;
488 // -------------------------------------------------------------------------
489 void cpExtensions::Visualization::ImageSliceActors::
492 this->SetInterpolate( true );
495 // -------------------------------------------------------------------------
496 void cpExtensions::Visualization::ImageSliceActors::
499 this->SetInterpolate( false );
502 // -------------------------------------------------------------------------
503 double* cpExtensions::Visualization::ImageSliceActors::
504 GetDisplayBounds( ) const
506 if( this->m_ImageActor.GetPointer( ) != NULL )
507 return( this->m_ImageActor->GetDisplayBounds( ) );
512 // -------------------------------------------------------------------------
513 void cpExtensions::Visualization::ImageSliceActors::
514 GetDisplayBounds( double bounds[ 6 ] ) const
516 if( this->m_ImageActor.GetPointer( ) == NULL )
518 bounds[ 0 ] = bounds[ 2 ] = bounds[ 4 ] = double( -1 );
519 bounds[ 1 ] = bounds[ 3 ] = bounds[ 5 ] = double( -1 );
522 this->m_ImageActor->GetDisplayBounds( bounds );
525 // -------------------------------------------------------------------------
526 void cpExtensions::Visualization::ImageSliceActors::
529 if( this->m_ImageMapper.GetPointer( ) != NULL )
532 this->m_ImageMapper->GetInput( )->GetBounds( bounds );
535 this->m_VisibleBounds[ 0 ],
536 this->m_VisibleBounds[ 2 ],
537 this->m_VisibleBounds[ 4 ]
539 this->SetCursor( pos );
543 vtkPoints* points = this->m_Cursor->GetPoints( );
544 points->SetPoint( 0, 0, 0, 0 );
545 points->SetPoint( 1, 0, 0, 0 );
546 points->SetPoint( 2, 0, 0, 0 );
547 points->SetPoint( 3, 0, 0, 0 );
548 points->SetPoint( 4, 0, 0, 0 );
549 points->SetPoint( 5, 0, 0, 0 );
550 points->SetPoint( 6, 0, 0, 0 );
551 points->SetPoint( 7, 0, 0, 0 );
552 this->m_Cursor->Modified( );
553 this->m_CursorMapper->Modified( );
554 this->m_CursorActor->Modified( );
559 // -------------------------------------------------------------------------
560 void cpExtensions::Visualization::ImageSliceActors::
561 SetCursor( double pos[ 3 ] )
563 if( this->m_ImageMapper.GetPointer( ) == NULL )
567 int a0 = this->GetAxis( );
568 int a1 = ( a0 + 1 ) % 3;
569 int a2 = ( a0 + 2 ) % 3;
575 double* bounds = this->m_VisibleBounds;
577 p0[ 3 ], p1[ 3 ], p2[ 3 ], p3[ 3 ],
578 p4[ 3 ], p5[ 3 ], p6[ 3 ], p7[ 3 ];
580 p0[ a2 ] = p1[ a2 ] = p4[ a2 ] = p5[ a2 ] = pos[ a2 ];
581 p2[ a1 ] = p3[ a1 ] = p6[ a1 ] = p7[ a1 ] = pos[ a1 ];
582 p0[ a0 ] = p1[ a0 ] = p2[ a0 ] = p3[ a0 ] = bounds[ ma0 ];
583 p4[ a0 ] = p5[ a0 ] = p6[ a0 ] = p7[ a0 ] = bounds[ ma0 + 1 ];
584 p0[ a1 ] = p4[ a1 ] = bounds[ ma1 ];
585 p1[ a1 ] = p5[ a1 ] = bounds[ ma1 + 1 ];
586 p2[ a2 ] = p6[ a2 ] = bounds[ ma2 ];
587 p3[ a2 ] = p7[ a2 ] = bounds[ ma2 + 1 ];
589 vtkPoints* points = this->m_Cursor->GetPoints( );
590 points->SetPoint( 0, p0 );
591 points->SetPoint( 1, p1 );
592 points->SetPoint( 2, p2 );
593 points->SetPoint( 3, p3 );
594 points->SetPoint( 4, p4 );
595 points->SetPoint( 5, p5 );
596 points->SetPoint( 6, p6 );
597 points->SetPoint( 7, p7 );
598 this->m_Cursor->Modified( );
599 this->m_CursorMapper->Modified( );
600 this->m_CursorActor->Modified( );
603 // -------------------------------------------------------------------------
604 double cpExtensions::Visualization::ImageSliceActors::
605 GetMinWindow( ) const
607 if( this->m_ImageBlender.GetPointer( ) != NULL )
608 return( this->m_ImageBlender->GetMinWindow( ) );
610 return( double( 0 ) );
613 // -------------------------------------------------------------------------
614 double cpExtensions::Visualization::ImageSliceActors::
615 GetMaxWindow( ) const
617 if( this->m_ImageBlender.GetPointer( ) != NULL )
618 return( this->m_ImageBlender->GetMaxWindow( ) );
620 return( double( 0 ) );
623 // -------------------------------------------------------------------------
624 double cpExtensions::Visualization::ImageSliceActors::
627 if( this->m_ImageBlender.GetPointer( ) != NULL )
628 return( this->m_ImageBlender->GetMinLevel( ) );
630 return( double( 0 ) );
633 // -------------------------------------------------------------------------
634 double cpExtensions::Visualization::ImageSliceActors::
637 if( this->m_ImageBlender.GetPointer( ) != NULL )
638 return( this->m_ImageBlender->GetMaxLevel( ) );
640 return( double( 0 ) );
643 // -------------------------------------------------------------------------
644 double cpExtensions::Visualization::ImageSliceActors::
647 if( this->m_ImageBlender.GetPointer( ) != NULL )
648 return( this->m_ImageBlender->GetWindow( ) );
650 return( double( 0 ) );
653 // -------------------------------------------------------------------------
654 double cpExtensions::Visualization::ImageSliceActors::
657 if( this->m_ImageBlender.GetPointer( ) != NULL )
658 return( this->m_ImageBlender->GetLevel( ) );
660 return( double( 0 ) );
663 // -------------------------------------------------------------------------
664 void cpExtensions::Visualization::ImageSliceActors::
665 SetWindow( double w )
667 if( this->m_ImageBlender.GetPointer( ) != NULL )
669 this->m_ImageBlender->SetWindow( w );
675 // -------------------------------------------------------------------------
676 void cpExtensions::Visualization::ImageSliceActors::
679 if( this->m_ImageBlender.GetPointer( ) != NULL )
681 this->m_ImageBlender->SetLevel( l );
687 // -------------------------------------------------------------------------
688 void cpExtensions::Visualization::ImageSliceActors::
689 SetWindowLevel( double w, double l )
691 if( this->m_ImageBlender.GetPointer( ) != NULL )
693 this->m_ImageBlender->SetWindowLevel( w, l );
694 this->UpdateText( w, l );
700 // -------------------------------------------------------------------------
701 void cpExtensions::Visualization::ImageSliceActors::
704 std::cerr << "ACA Resetear" << std::endl;
708 // -------------------------------------------------------------------------
709 int cpExtensions::Visualization::ImageSliceActors::
712 if( this->m_ImageMapper.GetPointer( ) != NULL )
713 return( this->m_ImageMapper->GetOrientation( ) );
718 // -------------------------------------------------------------------------
719 int cpExtensions::Visualization::ImageSliceActors::
720 GetSliceNumber( ) const
722 if( this->m_ImageMapper.GetPointer( ) != NULL )
723 return( this->m_ImageMapper->GetSliceNumber( ) );
728 // -------------------------------------------------------------------------
729 int cpExtensions::Visualization::ImageSliceActors::
730 GetSliceNumberMinValue( ) const
732 if( this->m_ImageMapper.GetPointer( ) != NULL )
733 return( this->m_ImageMapper->GetSliceNumberMinValue( ) );
738 // -------------------------------------------------------------------------
739 int cpExtensions::Visualization::ImageSliceActors::
740 GetSliceNumberMaxValue( ) const
742 if( this->m_ImageMapper.GetPointer( ) != NULL )
743 return( this->m_ImageMapper->GetSliceNumberMaxValue( ) );
748 // -------------------------------------------------------------------------
749 void cpExtensions::Visualization::ImageSliceActors::
750 SetSliceNumber( const int& slice )
752 if( this->m_ImageMapper.GetPointer( ) == NULL )
755 int axis = this->GetAxis( );
756 double prev_pos = this->m_VisibleBounds[ axis << 1 ];
758 // Update mappers and display bounds
759 this->m_ImageMapper->SetSliceNumber( slice );
760 this->m_ImageMapper->Modified( );
761 this->m_ImageActor->Modified( );
762 this->m_ImageMapper->Update( );
764 // Update display extent (this isn't done automatically)
765 this->m_ImageMapper->GetInput( )->GetExtent( this->m_VisibleExtent );
766 this->m_VisibleExtent[ axis << 1 ] = slice;
767 this->m_VisibleExtent[ ( axis << 1 ) + 1 ] = slice;
768 this->m_ImageActor->SetDisplayExtent( this->m_VisibleExtent );
770 // Prepare plane data
771 this->m_ImageMapper->GetBounds( this->m_VisibleBounds );
775 this->m_VisibleBounds[ 0 ],
776 this->m_VisibleBounds[ 2 ],
777 this->m_VisibleBounds[ 4 ]
780 this->m_VisibleBounds[ 1 ],
781 this->m_VisibleBounds[ 3 ],
782 this->m_VisibleBounds[ 5 ]
787 vtkPlane* plane = this->m_ImageMapper->GetSlicePlane( );
788 plane->GeneralizedProjectPoint( x0[ 0 ], p0[ 0 ] );
789 plane->GeneralizedProjectPoint( x0[ 1 ], p0[ 1 ] );
791 this->m_VisibleBounds[ 0 ] = p0[ 0 ][ 0 ];
792 this->m_VisibleBounds[ 1 ] = p0[ 1 ][ 0 ];
793 this->m_VisibleBounds[ 2 ] = p0[ 0 ][ 1 ];
794 this->m_VisibleBounds[ 3 ] = p0[ 1 ][ 1 ];
795 this->m_VisibleBounds[ 4 ] = p0[ 0 ][ 2 ];
796 this->m_VisibleBounds[ 5 ] = p0[ 1 ][ 2 ];
797 double* bnds = this->m_VisibleBounds;
799 // Configure visualization and implicit plane orientation
800 this->m_PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
801 this->m_PlaneActor->GetProperty( )->SetLineWidth( 3 );
802 vtkPoints* plane_points = this->m_Plane->GetPoints( );
803 plane_points->SetPoint( 0, bnds[ 0 ], bnds[ 2 ], bnds[ 4 ] );
804 plane_points->SetPoint( 2, bnds[ 1 ], bnds[ 3 ], bnds[ 5 ] );
805 if( axis == 0 || axis == 2 ) // YZ, x-normal
807 plane_points->SetPoint( 1, bnds[ 1 ], bnds[ 2 ], bnds[ 5 ] );
808 plane_points->SetPoint( 3, bnds[ 0 ], bnds[ 3 ], bnds[ 4 ] );
810 this->m_PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
812 this->m_PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
814 else if( axis == 1 ) // ZX, y-normal
816 plane_points->SetPoint( 1, bnds[ 0 ], bnds[ 2 ], bnds[ 5 ] );
817 plane_points->SetPoint( 3, bnds[ 1 ], bnds[ 3 ], bnds[ 4 ] );
818 this->m_PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
821 this->m_Plane->Modified( );
822 this->m_PlaneMapper->Modified( );
823 this->m_PlaneActor->Modified( );
828 // Update lines from associated slices
830 auto sIt = this->m_AssociatedSlices.begin( );
831 for( ; sIt != this->m_AssociatedSlices.end( ); ++sIt )
834 for( unsigned int id = 0; id < slice->m_AssociatedSlices.size( ); ++id )
836 std::cout << id << std::endl;
837 if( slice->m_AssociatedSlices[ id ] != this )
840 std::cout << "id : " << id << std::endl;
847 // Update camera position
848 if( this->m_Window == NULL )
850 vtkRenderer* renderer =
851 this->m_Window->GetRenderers( )->GetFirstRenderer( );
852 if( renderer == NULL )
854 vtkCamera* camera = renderer->GetActiveCamera( );
858 camera->GetPosition( cam_pos );
859 cam_pos[ axis ] += this->m_VisibleBounds[ axis << 1 ] - prev_pos;
860 camera->SetPosition( cam_pos );
863 // -------------------------------------------------------------------------
864 void cpExtensions::Visualization::ImageSliceActors::
865 SetSlice( double* pos )
867 vtkImageData* image = this->GetInputImage( 0 );
873 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
874 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
877 // -------------------------------------------------------------------------
878 void cpExtensions::Visualization::ImageSliceActors::
881 if( this->m_ImageMapper.GetPointer( ) != NULL )
884 int axId = this->m_ImageMapper->GetOrientation( );
885 if ( axId == 0 ) axis = 'X';
886 else if( axId == 1 ) axis = 'Y';
887 else if( axId == 2 ) axis = 'Z';
890 this->m_TextBuffer, "Axis: %c (%d)",
891 axis, this->m_ImageMapper->GetSliceNumber( )
895 this->m_TextBuffer[ 0 ] = '\0';
896 this->m_TextActor->SetInput( this->m_TextBuffer );
897 this->m_TextActor->Modified( );
901 // -------------------------------------------------------------------------
902 void cpExtensions::Visualization::ImageSliceActors::
903 UpdateText( double pos[ 3 ] )
905 if( this->m_ImageMapper.GetPointer( ) != NULL )
908 int axId = this->m_ImageMapper->GetOrientation( );
909 if ( axId == 0 ) axis = 'X';
910 else if( axId == 1 ) axis = 'Y';
911 else if( axId == 2 ) axis = 'Z';
912 int slice = this->GetSliceNumber( );
914 vtkImageData* image = this->GetInputImage( 0 );
917 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
921 image->GetExtent( ext );
923 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
924 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
925 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
928 int nScl = image->GetNumberOfScalarComponents( );
929 std::stringstream str;
933 << "," << ijk[ 2 ] << "]=(";
935 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
936 for( int n = 1; n < nScl; ++n )
939 << image->GetScalarComponentAsFloat(
940 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
944 this->m_TextBuffer, "Axis: %c (%d)\nPixel %s",
945 axis, slice, str.str( ).c_str( )
951 this->m_TextBuffer[ 0 ] = '\0';
952 this->m_TextActor->SetInput( this->m_TextBuffer );
953 this->m_TextActor->Modified( );
957 // -------------------------------------------------------------------------
958 void cpExtensions::Visualization::ImageSliceActors::
959 UpdateText( const double& w, const double& l )
961 if( this->m_ImageMapper.GetPointer( ) != NULL )
964 int axId = this->m_ImageMapper->GetOrientation( );
965 if ( axId == 0 ) axis = 'X';
966 else if( axId == 1 ) axis = 'Y';
967 else if( axId == 2 ) axis = 'Z';
970 this->m_TextBuffer, "Axis: %c (%d)\nW/L (%.2f/%.2f)",
971 axis, this->m_ImageMapper->GetSliceNumber( ), w, l
975 this->m_TextBuffer[ 0 ] = '\0';
976 this->m_TextActor->SetInput( this->m_TextBuffer );
977 this->m_TextActor->Modified( );
981 // -------------------------------------------------------------------------
982 void cpExtensions::Visualization::ImageSliceActors::
983 Render( const double& t )
985 if( this->m_Window != NULL )
987 vtkRenderer* renderer =
988 this->m_Window->GetRenderers( )->GetFirstRenderer( );
989 if( renderer != NULL )
990 renderer->SetAllocatedRenderTime( t );
991 this->m_Window->Render( );
996 // -------------------------------------------------------------------------
997 void cpExtensions::Visualization::ImageSliceActors::
1000 if( this->m_Window == NULL )
1002 vtkRenderer* renderer =
1003 this->m_Window->GetRenderers( )->GetFirstRenderer( );
1004 if( renderer != NULL )
1005 renderer->ResetCamera( this->m_VisibleBounds );
1008 // -------------------------------------------------------------------------
1009 cpExtensions::Visualization::ImageSliceActors::
1013 m_Interpolate( false )
1016 this->_ConfigureStyle( );
1019 // -------------------------------------------------------------------------
1020 cpExtensions::Visualization::ImageSliceActors::
1021 ~ImageSliceActors( )
1025 // -------------------------------------------------------------------------
1026 void cpExtensions::Visualization::ImageSliceActors::
1029 // Connect this view with a controller
1030 this->m_Style = vtkSmartPointer< TStyle >::New( );
1031 this->m_Style->AddMouseMoveCommand( Self::_MouseMoveCommand, this );
1032 this->m_Style->AddMouseClickCommand( Self::_MouseClickCommand, this );
1033 this->m_Style->AddMouseWheelCommand( Self::_MouseWheelCommand, this );
1034 this->m_Style->AddKeyCommand( Self::_KeyCommand, this );
1035 this->m_Style->AddEnterCommand( Self::_EnterCommand, this );
1036 this->m_Style->AddLeaveCommand( Self::_LeaveCommand, this );
1039 // -------------------------------------------------------------------------
1040 void cpExtensions::Visualization::ImageSliceActors::
1042 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1043 bool alt, bool ctr, bool sft
1046 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1047 if( actors == NULL )
1050 if( btn == TStyle::ButtonID_None )
1052 // Just show the pixel information
1053 actors->SetCursor( pos );
1054 actors->UpdateText( pos );
1055 actors->Render( 1e-3 );
1057 else if( btn == TStyle::ButtonID_Left )
1059 if( !alt && ctr && !sft )
1061 // Interactively move slices
1062 auto i = actors->m_SlicesCommands.begin( );
1063 for( ; i != actors->m_SlicesCommands.end( ); ++i )
1064 i->first( pos, actors->GetAxis( ), i->second );
1065 actors->Render( 1e-3 );
1069 else if( btn == TStyle::ButtonID_Right )
1071 if( !alt && !ctr && sft )
1073 // Change image window level
1075 actors->m_ImageMapper->GetBounds( bounds );
1077 int a0 = actors->GetAxis( );
1078 int a1 = ( a0 + 1 ) % 3;
1079 int a2 = ( a0 + 2 ) % 3;
1080 double dx = pos[ a1 ] - actors->m_StartWindowLevelPos[ a1 ];
1081 double dy = pos[ a2 ] - actors->m_StartWindowLevelPos[ a2 ];
1082 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1083 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1085 dx *= actors->m_StartWindowLevel[ 0 ];
1086 dy *= actors->m_StartWindowLevel[ 1 ];
1087 dx += actors->m_StartWindowLevel[ 0 ];
1088 dy += actors->m_StartWindowLevel[ 1 ];
1089 actors->SetWindowLevel( dx, dy );
1090 actors->Render( 1e-3 );
1092 // Associate objects
1093 auto i = actors->m_WindowLevelCommands.begin( );
1094 for( ; i != actors->m_WindowLevelCommands.end( ); ++i )
1095 i->first( dx, dy, i->second );
1102 // -------------------------------------------------------------------------
1103 void cpExtensions::Visualization::ImageSliceActors::
1105 void* data, const TStyle::ButtonID& btn, int* idx, double* pos,
1106 bool alt, bool ctr, bool sft
1109 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1110 if( actors == NULL )
1113 actors->m_StartWindowLevelPos[ 0 ] = pos[ 0 ];
1114 actors->m_StartWindowLevelPos[ 1 ] = pos[ 1 ];
1115 actors->m_StartWindowLevelPos[ 2 ] = pos[ 2 ];
1116 actors->m_StartWindowLevel[ 0 ] = actors->GetWindow( );
1117 actors->m_StartWindowLevel[ 1 ] = actors->GetLevel( );
1120 // -------------------------------------------------------------------------
1121 void cpExtensions::Visualization::ImageSliceActors::
1123 void* data, const int& dir,
1124 bool alt, bool ctr, bool sft
1127 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1128 if( actors == NULL )
1133 int slice = actors->GetSliceNumber( ) + ( dir * ( ( sft )? 10: 1 ) );
1134 if( slice < actors->GetSliceNumberMinValue( ) )
1135 slice = actors->GetSliceNumberMinValue( );
1136 if( slice > actors->GetSliceNumberMaxValue( ) )
1137 slice = actors->GetSliceNumberMaxValue( );
1138 actors->SetSliceNumber( slice );
1140 auto a = actors->m_AssociatedSlices.begin( );
1141 for( ; a != actors->m_AssociatedSlices.end( ); ++a )
1143 ( *a )->SetSliceNumber( slice );
1144 ( *a )->Render( 1e-3 );
1147 actors->Render( 1e-3 );
1149 // Associate objects
1150 auto i = actors->m_RenderCommands.begin( );
1151 for( ; i != actors->m_RenderCommands.end( ); ++i )
1152 i->first( i->second );
1157 // -------------------------------------------------------------------------
1158 void cpExtensions::Visualization::ImageSliceActors::
1159 _KeyCommand( void* data, const char& key )
1161 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1162 if( actors == NULL )
1169 actors->ResetCamera( );
1170 actors->Render( 1e-3 );
1172 // Associate objects
1173 auto i = actors->m_RenderCommands.begin( );
1174 for( ; i != actors->m_RenderCommands.end( ); ++i )
1175 i->first( i->second );
1180 actors->ResetWindowLevel( );
1181 actors->Render( 1e-3 );
1183 // Associate objects
1184 auto i = actors->m_RenderCommands.begin( );
1185 for( ; i != actors->m_RenderCommands.end( ); ++i )
1186 i->first( i->second );
1194 // -------------------------------------------------------------------------
1195 void cpExtensions::Visualization::ImageSliceActors::
1196 _EnterCommand( void* data )
1198 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1199 if( actors == NULL )
1202 actors->ResetCursor( );
1203 actors->m_CursorActor->VisibilityOn( );
1204 actors->Render( 1e-3 );
1207 // -------------------------------------------------------------------------
1208 void cpExtensions::Visualization::ImageSliceActors::
1209 _LeaveCommand( void* data )
1211 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1212 if( actors == NULL )
1215 actors->ResetCursor( );
1216 actors->m_CursorActor->VisibilityOff( );
1217 actors->Render( 1e-3 );