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, LUTType lut )
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, lut );
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, LUTType lut )
73 this->_ConfigureNewLUT( data, lut );
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->PlaneSource = 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->PlaneSource->SetPoints( plane_points );
164 this->PlaneSource->SetLines( plane_lines );
166 this->PlaneMapper->SetInputData( this->PlaneSource );
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::
520 GetWindow( unsigned int id ) const
522 if( this->ImageMaps[ id ].GetPointer( ) != NULL )
524 vtkWindowLevelLookupTable* lut =
525 dynamic_cast< vtkWindowLevelLookupTable* >(
526 this->ImageMaps[ id ]->GetLookupTable( )
529 return( lut->GetWindow( ) );
531 return( double( 0 ) );
534 return( double( 0 ) );
537 // -------------------------------------------------------------------------
538 double cpExtensions::Visualization::ImageSliceActors::
539 GetLevel( unsigned int id ) const
541 if( this->ImageMaps[ id ].GetPointer( ) != NULL )
543 vtkWindowLevelLookupTable* lut =
544 dynamic_cast< vtkWindowLevelLookupTable* >(
545 this->ImageMaps[ id ]->GetLookupTable( )
548 return( lut->GetLevel( ) );
550 return( double( 0 ) );
553 return( double( 0 ) );
556 // -------------------------------------------------------------------------
557 void cpExtensions::Visualization::ImageSliceActors::
558 SetWindow( unsigned int id, double w )
560 if( this->ImageMaps[ id ].GetPointer( ) != NULL )
562 vtkWindowLevelLookupTable* lut =
563 dynamic_cast< vtkWindowLevelLookupTable* >(
564 this->ImageMaps[ id ]->GetLookupTable( )
570 this->ImageMaps[ id ]->Modified( );
578 // -------------------------------------------------------------------------
579 void cpExtensions::Visualization::ImageSliceActors::
580 SetLevel( unsigned int id, double l )
582 if( this->ImageMaps[ id ].GetPointer( ) != NULL )
584 vtkWindowLevelLookupTable* lut =
585 dynamic_cast< vtkWindowLevelLookupTable* >(
586 this->ImageMaps[ id ]->GetLookupTable( )
592 this->ImageMaps[ id ]->Modified( );
600 // -------------------------------------------------------------------------
601 void cpExtensions::Visualization::ImageSliceActors::
602 SetWindowLevel( unsigned int id, double w, double l )
604 if( this->ImageMaps[ id ].GetPointer( ) != NULL )
606 vtkWindowLevelLookupTable* lut =
607 dynamic_cast< vtkWindowLevelLookupTable* >(
608 this->ImageMaps[ id ]->GetLookupTable( )
615 this->ImageMaps[ id ]->Modified( );
616 this->UpdateText( w, l );
624 // -------------------------------------------------------------------------
625 void cpExtensions::Visualization::ImageSliceActors::
626 ResetWindowLevel( unsigned int id )
630 // -------------------------------------------------------------------------
631 int cpExtensions::Visualization::ImageSliceActors::
634 if( this->SliceMappers.size( ) > 0 )
635 return( this->SliceMappers[ 0 ]->GetOrientation( ) );
640 // -------------------------------------------------------------------------
641 int cpExtensions::Visualization::ImageSliceActors::
642 GetSliceNumber( ) const
644 if( this->SliceMappers.size( ) > 0 )
645 return( this->SliceMappers[ 0 ]->GetSliceNumber( ) );
650 // -------------------------------------------------------------------------
651 int cpExtensions::Visualization::ImageSliceActors::
652 GetSliceNumberMinValue( ) const
654 if( this->SliceMappers.size( ) > 0 )
655 return( this->SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
660 // -------------------------------------------------------------------------
661 int cpExtensions::Visualization::ImageSliceActors::
662 GetSliceNumberMaxValue( ) const
664 if( this->SliceMappers.size( ) > 0 )
665 return( this->SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
670 // -------------------------------------------------------------------------
671 void cpExtensions::Visualization::ImageSliceActors::
672 SetSliceNumber( const int& slice )
674 unsigned int nImages = this->SliceMappers.size( );
678 // Change visualization extent
679 for( unsigned int i = 0; i < nImages; ++i )
681 this->SliceMappers[ i ]->SetSliceNumber( slice );
682 this->SliceMappers[ i ]->Modified( );
683 this->ImageActors[ i ]->Modified( );
684 this->SliceMappers[ i ]->Update( );
689 vtkAlgorithm* algo = this->SliceMappers[ 0 ]->GetInputAlgorithm( );
690 vtkInformation* info = algo->GetOutputInformation( 0 );
692 double ori[ 3 ], spac[ 3 ], pos[ 3 ];
693 info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
694 info->Get( vtkDataObject::ORIGIN( ), ori );
695 info->Get( vtkDataObject::SPACING( ), spac );
696 this->SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
698 // Prevent obscuring voxels by offsetting the plane geometry
701 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
702 ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
706 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
707 ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
711 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
712 ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
715 if( spac[ 0 ] < double( 0 ) )
717 double t = xbnds[ 0 ];
718 xbnds[ 0 ] = xbnds[ 1 ];
722 if( spac[ 1 ] < double( 0 ) )
724 double t = ybnds[ 0 ];
725 ybnds[ 0 ] = ybnds[ 1 ];
729 if( spac[ 2 ] < double( 0 ) )
731 double t = zbnds[ 0 ];
732 zbnds[ 0 ] = zbnds[ 1 ];
737 // Plane function origin
738 this->PlaneFunction->SetOrigin( pos );
740 // Configure visualization and implicit plane orientation
741 int axis = this->SliceMappers[ 0 ]->GetOrientation( );
742 this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
743 this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
744 vtkPoints* plane_points = this->PlaneSource->GetPoints( );
745 if( axis == 0 ) // YZ, x-normal
747 this->PlaneFunction->SetNormal( 1, 0, 0 );
748 plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
749 plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
750 plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
751 plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
752 this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
754 else if( axis == 1 ) // ZX, y-normal
756 this->PlaneFunction->SetNormal( 0, 1, 0 );
757 plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
758 plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
759 plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
760 plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
761 this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
765 this->PlaneFunction->SetNormal( 0, 0, 1 );
766 plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
767 plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
768 plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
769 plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
770 this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
773 this->PlaneFunction->Modified( );
774 this->PlaneSource->Modified( );
775 this->PlaneMapper->Modified( );
776 this->PlaneActor->Modified( );
778 // Prepare other actors to update
780 for( unsigned int i = 0; i < this->OtherActors.size( ); ++i )
782 if( this->OtherActors[ i ].first.GetPointer( ) != NULL )
784 this->OtherActors[ i ].first->Modified( );
785 this->OtherActors[ i ].first->Update( );
789 if( this->OtherActors[ i ].second.GetPointer( ) != NULL )
791 this->OtherActors[ i ].second->GetMapper( )->Modified( );
792 this->OtherActors[ i ].second->Modified( );
798 if( this->m_UpdateCommand != NULL )
799 this->m_UpdateCommand( this->m_UpdateData );
806 for( unsigned int i = 0; i < this->AssociatedSlices.size( ); ++i )
807 if( this->AssociatedSlices[ i ]->GetAxis( ) == this->GetAxis( ) )
808 this->AssociatedSlices[ i ]->SetSliceNumber( slice );
811 // -------------------------------------------------------------------------
812 void cpExtensions::Visualization::ImageSliceActors::
813 SetSlice( double* pos )
816 if( this->ImageMaps[ 0 ] != NULL )
818 dynamic_cast< vtkImageData* >( this->ImageMaps[ 0 ]->GetInput( ) );
820 image = this->SliceMappers[ 0 ]->GetInput( );
824 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
825 this->SetSliceNumber( ijk[ this->GetAxis( ) ] );
828 // -------------------------------------------------------------------------
830 void cpExtensions::Visualization::ImageSliceActors::
831 SetSlices( double pos[ 3 ] )
836 // -------------------------------------------------------------------------
837 void cpExtensions::Visualization::ImageSliceActors::
840 if( this->SliceMappers.size( ) > 0 )
843 int axId = this->SliceMappers[ 0 ]->GetOrientation( );
844 if ( axId == 0 ) axis = 'X';
845 else if( axId == 1 ) axis = 'Y';
846 else if( axId == 2 ) axis = 'Z';
849 this->TextBuffer, "Axis: %c (%d)",
850 axis, this->SliceMappers[ 0 ]->GetSliceNumber( )
854 this->TextBuffer[ 0 ] = '\0';
855 this->TextActor->SetInput( this->TextBuffer );
856 this->TextActor->Modified( );
860 // -------------------------------------------------------------------------
861 void cpExtensions::Visualization::ImageSliceActors::
862 UpdateText( double pos[ 3 ] )
864 if( this->SliceMappers.size( ) > 0 )
867 int axId = this->SliceMappers[ 0 ]->GetOrientation( );
868 if ( axId == 0 ) axis = 'X';
869 else if( axId == 1 ) axis = 'Y';
870 else if( axId == 2 ) axis = 'Z';
871 int slice = this->GetSliceNumber( );
874 if( this->ImageMaps[ 0 ] != NULL )
876 dynamic_cast< vtkImageData* >( this->ImageMaps[ 0 ]->GetInput( ) );
878 image = this->SliceMappers[ 0 ]->GetInput( );
882 image->ComputeStructuredCoordinates( pos, ijk, pcoords );
886 image->GetExtent( ext );
888 ext[ 0 ] <= ijk[ 0 ] && ijk[ 0 ] <= ext[ 1 ] &&
889 ext[ 2 ] <= ijk[ 1 ] && ijk[ 1 ] <= ext[ 3 ] &&
890 ext[ 4 ] <= ijk[ 2 ] && ijk[ 2 ] <= ext[ 5 ]
893 int nScl = image->GetNumberOfScalarComponents( );
894 std::stringstream str;
898 << "," << ijk[ 2 ] << "]=(";
900 image->GetScalarComponentAsFloat( ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], 0 );
901 for( int n = 1; n < nScl; ++n )
904 << image->GetScalarComponentAsFloat(
905 ijk[ 0 ], ijk[ 1 ], ijk[ 2 ], n
909 this->TextBuffer, "Axis: %c (%d) | Pixel %s",
910 axis, slice, str.str( ).c_str( )
916 this->TextBuffer[ 0 ] = '\0';
917 this->TextActor->SetInput( this->TextBuffer );
918 this->TextActor->Modified( );
922 // -------------------------------------------------------------------------
923 void cpExtensions::Visualization::ImageSliceActors::
924 UpdateText( const double& w, const double& l )
926 if( this->SliceMappers.size( ) > 0 )
929 int axId = this->SliceMappers[ 0 ]->GetOrientation( );
930 if ( axId == 0 ) axis = 'X';
931 else if( axId == 1 ) axis = 'Y';
932 else if( axId == 2 ) axis = 'Z';
935 this->TextBuffer, "Axis: %c (%d) | W/L (%.2f/%.2f)",
936 axis, this->SliceMappers[ 0 ]->GetSliceNumber( ), w, l
940 this->TextBuffer[ 0 ] = '\0';
941 this->TextActor->SetInput( this->TextBuffer );
942 this->TextActor->Modified( );
946 // -------------------------------------------------------------------------
947 cpExtensions::Visualization::ImageSliceActors::
954 this->SlicesCommand = NULL;
956 // Connect this view with a controller
957 this->Style = vtkSmartPointer< ImageInteractorStyle >::New( );
958 this->Style->AssociateView( this );
959 this->Style->SetMouseMoveCommand( Self::_MouseMoveCommand );
960 this->Style->SetMouseClickCommand( Self::_MouseClickCommand );
961 this->Style->SetMouseDoubleClickCommand( Self::_MouseDoubleClickCommand );
962 this->Style->SetMouseWheelCommand( Self::_MouseWheelCommand );
963 this->Style->SetKeyCommand( Self::_KeyCommand );
966 // -------------------------------------------------------------------------
967 cpExtensions::Visualization::ImageSliceActors::
972 // -------------------------------------------------------------------------
973 void cpExtensions::Visualization::ImageSliceActors::
974 _ConfigureNewLUT( vtkImageData* data, LUTType lut_t )
976 static const double _0 = double( 0 );
977 static const double _1 = double( 1 );
978 static const double _2 = double( 2 );
979 static const double _4 = double( 4 );
980 static const double _6 = double( 6 );
981 static const double _OPACITY = double( 0.6 );
984 vtkSmartPointer< vtkImageMapToColors > new_map( NULL );
985 if( data->GetNumberOfScalarComponents( ) == 1 )
988 data->GetScalarRange( range );
989 if( lut_t == Self::LUTType_WindowLevel )
991 vtkSmartPointer< vtkWindowLevelLookupTable > lut =
992 vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
993 lut->SetScaleToLinear( );
994 lut->SetTableRange( range );
995 lut->SetWindow( range[ 1 ] - range[ 0 ] );
996 lut->SetLevel( ( range[ 1 ] + range[ 0 ] ) / double( 2 ) );
999 new_map = vtkSmartPointer< vtkImageMapToColors >::New( );
1000 new_map->SetLookupTable( lut );
1002 else if( lut_t == LUTType_Colors )
1004 // Get HSV from display color
1005 double r = 1, g = 0, b = 0;
1006 double cmax = ( r > g )? r: g; cmax = ( b > cmax )? b: cmax;
1007 double cmin = ( r < g )? r: g; cmin = ( b < cmin )? b: cmin;
1008 double d = cmax - cmin;
1010 double saturation = ( std::fabs( cmax ) > _0 )? d / cmax: _0;
1011 double value = cmax;
1016 hue = std::fmod( ( g - b ) / d, _6 );
1017 else if( g == cmax )
1018 hue = ( ( b - r ) / d ) + _2;
1019 else if( b == cmax )
1020 hue = ( ( r - g ) / d ) + _4;
1025 // Define new lookup table
1026 vtkSmartPointer< vtkLookupTable > lut =
1027 vtkSmartPointer< vtkLookupTable >::New( );
1028 lut->SetScaleToLinear( );
1029 lut->SetNanColor( _0, _0, _0, _0 );
1030 lut->SetTableRange( range[ 0 ], range[ 1 ] );
1031 lut->SetAlphaRange( _0, _OPACITY );
1032 lut->SetHueRange( _0, hue );
1033 lut->SetSaturationRange( _0, saturation );
1034 lut->SetValueRange( _0, value );
1037 new_map = vtkSmartPointer< vtkImageMapToColors >::New( );
1038 new_map->SetLookupTable( lut );
1043 this->ImageMaps.push_back( new_map );
1046 // -------------------------------------------------------------------------
1047 void cpExtensions::Visualization::ImageSliceActors::
1048 _ConfigureNewInput( int axis )
1050 unsigned int nImages = this->ImageActors.size( );
1053 vtkImageSliceMapper* mapper = this->SliceMappers[ nImages ];
1055 mapper->SetOrientation( axis );
1057 mapper->SetOrientation( this->SliceMappers[ 0 ]->GetOrientation( ) );
1061 vtkSmartPointer< vtkImageActor > actor =
1062 vtkSmartPointer< vtkImageActor >::New( );
1063 this->ImageActors.push_back( actor );
1064 actor->SetMapper( mapper );
1065 actor->SetInterpolate( this->Interpolate );
1070 this->AddItem( this->CursorActor );
1071 this->AddItem( this->PlaneActor );
1072 this->AddItem( this->TextActor );
1073 this->Style->AssociateImageActor( actor );
1076 this->AddItem( actor );
1079 this->SetSliceNumber( this->GetSliceNumber( ) );
1083 // -------------------------------------------------------------------------
1084 void cpExtensions::Visualization::ImageSliceActors::
1087 const ImageInteractorStyle::ButtonID& btn, double* pos,
1088 bool alt, bool ctr, bool sft
1091 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1092 if( actors == NULL )
1095 if( btn == ImageInteractorStyle::ButtonID_None )
1097 actors->UpdateText( pos );
1099 else if( btn == ImageInteractorStyle::ButtonID_Left )
1101 if( !alt && ctr && !sft && actors->SlicesCommand != NULL )
1102 actors->SlicesCommand( pos, actors->GetAxis( ), actors->SlicesData );
1104 else if( btn == ImageInteractorStyle::ButtonID_Middle )
1107 else if( btn == ImageInteractorStyle::ButtonID_Right )
1109 if( !alt && !ctr && sft )
1112 actors->SliceMappers[ 0 ]->GetBounds( bounds );
1114 int a0 = actors->GetAxis( );
1115 int a1 = ( a0 + 1 ) % 3;
1116 int a2 = ( a0 + 2 ) % 3;
1117 double dx = pos[ a1 ] - actors->StartWindowLevelPos[ a1 ];
1118 double dy = pos[ a2 ] - actors->StartWindowLevelPos[ a2 ];
1119 dx /= bounds[ ( a1 << 1 ) + 1 ] - bounds[ a1 << 1 ];
1120 dy /= bounds[ ( a2 << 1 ) + 1 ] - bounds[ a2 << 1 ];
1122 dx *= actors->StartWindowLevel[ 0 ];
1123 dy *= actors->StartWindowLevel[ 1 ];
1124 actors->SetWindowLevel(
1126 actors->StartWindowLevel[ 0 ] + dx,
1127 actors->StartWindowLevel[ 1 ] + dy
1135 // -------------------------------------------------------------------------
1136 void cpExtensions::Visualization::ImageSliceActors::
1139 const ImageInteractorStyle::ButtonID& btn, double* pos,
1140 bool alt, bool ctr, bool sft
1143 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1144 if( actors == NULL )
1147 actors->StartWindowLevelPos[ 0 ] = pos[ 0 ];
1148 actors->StartWindowLevelPos[ 1 ] = pos[ 1 ];
1149 actors->StartWindowLevelPos[ 2 ] = pos[ 2 ];
1150 actors->StartWindowLevel[ 0 ] = actors->GetWindow( 0 );
1151 actors->StartWindowLevel[ 1 ] = actors->GetLevel( 0 );
1154 // -------------------------------------------------------------------------
1155 void cpExtensions::Visualization::ImageSliceActors::
1156 _MouseDoubleClickCommand(
1158 const ImageInteractorStyle::ButtonID& btn, double* pos,
1159 bool alt, bool ctr, bool sft
1162 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1163 if( actors == NULL )
1166 if( btn == ImageInteractorStyle::ButtonID_Left )
1168 if( !alt && !ctr && !sft )
1169 actors->SetCursor( pos );
1170 else if( !alt && ctr && !sft && actors->SlicesCommand != NULL )
1171 actors->SlicesCommand( pos, actors->GetAxis( ), actors->SlicesData );
1173 else if( btn == ImageInteractorStyle::ButtonID_Middle )
1176 else if( btn == ImageInteractorStyle::ButtonID_Right )
1181 // -------------------------------------------------------------------------
1182 void cpExtensions::Visualization::ImageSliceActors::
1185 const int& dir, bool alt, bool ctr, bool sft
1188 ImageSliceActors* actors = reinterpret_cast< ImageSliceActors* >( data );
1189 if( actors == NULL )
1192 if( !alt && !ctr && !sft )
1194 actors->SetSliceNumber( actors->GetSliceNumber( ) + dir );
1196 else if( !alt && !ctr && sft )
1198 actors->SetSliceNumber( actors->GetSliceNumber( ) + ( dir * 10 ) );
1203 // -------------------------------------------------------------------------
1204 void cpExtensions::Visualization::ImageSliceActors::