From: Leonardo Florez-Valencia Date: Fri, 21 Aug 2015 00:04:44 +0000 (-0500) Subject: 3D MPR updated X-Git-Tag: v0.1~393 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=05b92946cf3e01d93bdf5f317df943baaac9ebda;p=cpPlugins.git 3D MPR updated --- diff --git a/lib/cpPlugins/Extensions/Visualization/Image3DMPR.cxx b/lib/cpPlugins/Extensions/Visualization/Image3DMPR.cxx new file mode 100644 index 0000000..9d31298 --- /dev/null +++ b/lib/cpPlugins/Extensions/Visualization/Image3DMPR.cxx @@ -0,0 +1,364 @@ +#include + +#include +#include + +#include + +/** + */ +class cpPlugins_Extensions_Visualization_InteractionModeCallback + : public vtkCommand +{ +public: + static cpPlugins_Extensions_Visualization_InteractionModeCallback* New( ) + { + return( new cpPlugins_Extensions_Visualization_InteractionModeCallback ); + } + virtual void Execute( + vtkObject* caller, unsigned long eventId, void* arguments ) + { + if( eventId == vtkCommand::KeyPressEvent ) + { + vtkRenderWindowInteractor* iren = + static_cast< vtkRenderWindowInteractor* >( caller ); + if( caller == NULL ) + return; + + switch( iren->GetKeyCode( ) ) + { + case 'x': + case 'X': + { + bool eSeed = ( this->SeedWidget->GetProcessEvents( ) == 1 ); + if( eSeed ) + { + this->SeedWidget->ProcessEventsOff( ); + this->WidgetX->InteractionOn( ); + this->WidgetY->InteractionOn( ); + this->WidgetZ->InteractionOn( ); + } + else + { + this->SeedWidget->ProcessEventsOn( ); + this->WidgetX->InteractionOff( ); + this->WidgetY->InteractionOff( ); + this->WidgetZ->InteractionOff( ); + + } // fi + } + break; + case 'z': + case 'Z': + { + this->SeedWidget->ProcessEventsOff( ); + this->WidgetX->InteractionOff( ); + this->WidgetY->InteractionOff( ); + this->WidgetZ->InteractionOff( ); + } + break; + default: + break; + + } // hctiws + + } // fi + } + +public: + vtkImagePlaneWidget* WidgetX; + vtkImagePlaneWidget* WidgetY; + vtkImagePlaneWidget* WidgetZ; + vtkSeedWidget* SeedWidget; +}; + +// ------------------------------------------------------------------------- +cpPlugins::Extensions::Visualization::Image3DMPR:: +Image3DMPR( ) +{ + this->m_Outline = vtkSmartPointer< vtkOutlineSource >::New( ); + this->m_OutlineMapper = vtkSmartPointer< vtkPolyDataMapper >::New( ); + this->m_OutlineActor = vtkSmartPointer< vtkActor >::New( ); + this->m_Picker = vtkSmartPointer< vtkCellPicker >::New( ); + this->m_WidgetX = vtkSmartPointer< vtkImagePlaneWidget >::New( ); + this->m_WidgetY = vtkSmartPointer< vtkImagePlaneWidget >::New( ); + this->m_WidgetZ = vtkSmartPointer< vtkImagePlaneWidget >::New( ); + this->m_Renderer = vtkSmartPointer< vtkRenderer >::New( ); + this->m_Window = vtkSmartPointer< vtkRenderWindow >::New( ); + this->m_Interactor = vtkSmartPointer< vtkRenderWindowInteractor >::New( ); + this->m_SeedHandleRepresentation = + vtkSmartPointer< vtkPointHandleRepresentation3D >::New( ); + this->m_SeedRepresentation = + vtkSmartPointer< vtkSeedRepresentation >::New( ); + this->m_SeedWidget = vtkSmartPointer::New(); + + this->m_SeedHandleRepresentation->GetProperty()->SetColor( 1, 0, 1 ); + this->m_SeedRepresentation-> + SetHandleRepresentation( this->m_SeedHandleRepresentation ); + this->m_SeedWidget->SetRepresentation( this->m_SeedRepresentation ); + + this->m_WidgetX->KeyPressActivationOff( ); + this->m_WidgetY->KeyPressActivationOff( ); + this->m_WidgetZ->KeyPressActivationOff( ); + this->m_SeedWidget->KeyPressActivationOff( ); + + typedef + cpPlugins_Extensions_Visualization_InteractionModeCallback + _TInteractionCB; + vtkSmartPointer< _TInteractionCB > int_cb = + vtkSmartPointer< _TInteractionCB >::New( ); + int_cb->WidgetX = this->m_WidgetX; + int_cb->WidgetY = this->m_WidgetY; + int_cb->WidgetZ = this->m_WidgetZ; + int_cb->SeedWidget = this->m_SeedWidget; + this->m_Interactor->AddObserver( vtkCommand::KeyPressEvent, int_cb ); + + typedef + cpPlugins::Extensions::Visualization::SeedWidgetCorrector + _TCorrector; + vtkSmartPointer< _TCorrector > corr_cb = + vtkSmartPointer< _TCorrector >::New( ); + corr_cb->Configure( this->m_Picker ); + this->m_SeedWidget->AddObserver( vtkCommand::PlacePointEvent, corr_cb ); +} + +// ------------------------------------------------------------------------- +cpPlugins::Extensions::Visualization::Image3DMPR:: +~Image3DMPR( ) +{ +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::Image3DMPR:: +SetImage( vtkImageData* image ) +{ + this->m_Image = image; + + // Outline + this->m_Outline->SetBounds( this->m_Image->GetBounds( ) ); + this->m_OutlineMapper-> + SetInputConnection( this->m_Outline->GetOutputPort( ) ); + this->m_OutlineActor->SetMapper( this->m_OutlineMapper ); + this->m_OutlineActor->GetProperty( )->SetColor( 1, 1, 1 ); + + // Local picker + this->m_Picker->SetTolerance( 0.005 ); + + // Image planes + this->m_WidgetX->DisplayTextOn( ); + this->m_WidgetX->SetInputData( this->m_Image ); + this->m_WidgetX->SetPlaneOrientationToXAxes( ); + this->m_WidgetX->SetSliceIndex( this->m_Image->GetExtent( )[ 0 ] ); + this->m_WidgetX->SetPicker( this->m_Picker ); + this->m_WidgetX->SetKeyPressActivationValue( 'x' ); + this->m_WidgetX->GetPlaneProperty( )->SetLineWidth( 3 ); + this->m_WidgetX->GetPlaneProperty( )->SetColor( 0, 1, 1 ); + + this->m_WidgetY->DisplayTextOn( ); + this->m_WidgetY->SetInputData( this->m_Image ); + this->m_WidgetY->SetPlaneOrientationToYAxes( ); + this->m_WidgetY->SetSliceIndex( this->m_Image->GetExtent( )[ 2 ] ); + this->m_WidgetY->SetPicker( this->m_Picker ); + this->m_WidgetY->SetKeyPressActivationValue( 'y' ); + this->m_WidgetY->GetPlaneProperty( )->SetColor( 1, 0, 1 ); + this->m_WidgetY->GetPlaneProperty( )->SetLineWidth( 3 ); + this->m_WidgetY->SetLookupTable( this->m_WidgetX->GetLookupTable( ) ); + + this->m_WidgetZ->DisplayTextOn( ); + this->m_WidgetZ->SetInputData( this->m_Image ); + this->m_WidgetZ->SetPlaneOrientationToZAxes( ); + this->m_WidgetZ->SetSliceIndex( this->m_Image->GetExtent( )[ 4 ] ); + this->m_WidgetZ->SetPicker( this->m_Picker ); + this->m_WidgetZ->SetKeyPressActivationValue( 'z' ); + this->m_WidgetZ->GetPlaneProperty( )->SetColor( 1, 1, 0 ); + this->m_WidgetZ->GetPlaneProperty( )->SetLineWidth( 3 ); + this->m_WidgetZ->SetLookupTable( this->m_WidgetX->GetLookupTable( ) ); + + // Rendering stuff + this->m_Window->AddRenderer( this->m_Renderer ); + this->m_Interactor->SetRenderWindow( this->m_Window ); + + // Command + double spac[ 3 ]; + this->m_Image->GetSpacing( spac ); + double min_spacing = spac[ 0 ]; + for( unsigned int d = 1; d < 3; d++ ) + min_spacing = ( spac[ d ] < min_spacing )? spac[ d ]: min_spacing; + + vtkInteractorStyleSwitch* iswitch = + dynamic_cast< vtkInteractorStyleSwitch* >( + this->m_Interactor->GetInteractorStyle( ) + ); + if( iswitch != NULL ) + iswitch->SetCurrentStyleToTrackballCamera( ); + + // Add actors + this->m_Renderer->AddActor( this->m_OutlineActor ); + + // Prepare widgets + this->m_WidgetX->SetInteractor( this->m_Interactor ); + this->m_WidgetY->SetInteractor( this->m_Interactor ); + this->m_WidgetZ->SetInteractor( this->m_Interactor ); + this->m_SeedWidget->SetInteractor( this->m_Interactor ); + + this->m_WidgetX->On( ); + this->m_WidgetY->On( ); + this->m_WidgetZ->On( ); + this->m_SeedWidget->On( ); + this->m_SeedWidget->ProcessEventsOff( ); + this->m_Interactor->SetPicker( this->m_Picker ); + /* + this->m_Interactor->GetPickingManager( )->AddPicker( this->m_Picker ); + this->m_Interactor->GetPickingManager( )->EnabledOn( ); + */ +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::Image3DMPR:: +SetBackground( double r, double g, double b ) +{ + this->m_Renderer->SetBackground( r, g, b ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::Image3DMPR:: +SetSize( unsigned int w, unsigned int h ) +{ + this->m_Window->SetSize( w, h ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::Image3DMPR:: +SetWindowLevel( double w, double l ) +{ + this->m_WidgetX->SetWindowLevel( w, l ); + this->m_WidgetY->SetWindowLevel( w, l ); + this->m_WidgetZ->SetWindowLevel( w, l ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::Image3DMPR:: +AddPolyData( vtkPolyData* pd, double opacity ) +{ + unsigned int i = this->m_PolyDatas.size( ); + + this->m_PolyDatas.push_back( pd ); + this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) ); + this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) ); + + this->m_Mappers[ i ]->SetInputData( pd ); + this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] ); + this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity ); + this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 ); + this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 ); + this->m_Renderer->AddActor( this->m_Actors[ i ] ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::Image3DMPR:: +AddPolyData( vtkPolyData* pd, double r, double g, double b, double opacity ) +{ + unsigned int i = this->m_PolyDatas.size( ); + + this->m_PolyDatas.push_back( pd ); + this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) ); + this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) ); + + this->m_Mappers[ i ]->SetInputData( pd ); + this->m_Mappers[ i ]->ScalarVisibilityOff( ); + this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] ); + this->m_Actors[ i ]->GetProperty( )->SetColor( r, g, b ); + this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity ); + this->m_Actors[ i ]->GetProperty( )->SetLineWidth( 3 ); + this->m_Actors[ i ]->GetProperty( )->SetPointSize( 5 ); + this->m_Renderer->AddActor( this->m_Actors[ i ] ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::Image3DMPR:: +AddPolyData( vtkPolyData* pd, vtkLookupTable* lut, double opacity ) +{ + unsigned int i = this->m_PolyDatas.size( ); + + this->m_PolyDatas.push_back( pd ); + this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) ); + this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) ); + + this->m_Mappers[ i ]->SetInputData( pd ); + this->m_Mappers[ i ]->SetLookupTable( lut ); + this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] ); + this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity ); + this->m_Renderer->AddActor( this->m_Actors[ i ] ); +} + +// ------------------------------------------------------------------------- +unsigned int cpPlugins::Extensions::Visualization::Image3DMPR:: +GetNumberOfSeeds( ) const +{ + return( this->m_SeedRepresentation->GetNumberOfSeeds( ) ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::Image3DMPR:: +GetSeed( int n, double* s ) const +{ + vtkHandleWidget* hWdg = this->m_SeedWidget->GetSeed( n ); + if( hWdg == NULL ) + return; + vtkHandleRepresentation* hRep = + dynamic_cast< vtkHandleRepresentation* >( hWdg->GetRepresentation( ) ); + if( hRep == NULL ) + return; + hRep->GetWorldPosition( s ); +} + +// ------------------------------------------------------------------------- +unsigned int cpPlugins::Extensions::Visualization::Image3DMPR:: +AddSeed( const double& x, const double& y, const double& z ) const +{ + double pos[ 3 ] = { x, y, z }; + + int hnd_id = this->m_SeedRepresentation->CreateHandle( pos ); + vtkHandleWidget* hnd = this->m_SeedWidget->CreateNewHandle( ); + this->m_SeedRepresentation->GetHandleRepresentation( hnd_id )-> + SetWorldPosition( pos ); + hnd->SetEnabled( 1 ); + + return( this->GetNumberOfSeeds( ) - 1 ); +} + +// ------------------------------------------------------------------------- +vtkRenderWindow* cpPlugins::Extensions::Visualization::Image3DMPR:: +GetWindow( ) const +{ + return( this->m_Window ); +} + +// ------------------------------------------------------------------------- +vtkRenderer* cpPlugins::Extensions::Visualization::Image3DMPR:: +GetRenderer( ) const +{ + return( this->m_Renderer ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::Image3DMPR:: +Start( ) +{ + this->m_WidgetX->On( ); + this->m_WidgetY->On( ); + this->m_WidgetZ->On( ); + + this->m_Renderer->ResetCamera( ); + this->m_Interactor->Initialize( ); + this->m_Interactor->Start( ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::Image3DMPR:: +Render( ) +{ + this->m_Window->Render( ); +} + +// eof - $RCSfile$ diff --git a/lib/cpPlugins/Extensions/Visualization/Image3DMPR.h b/lib/cpPlugins/Extensions/Visualization/Image3DMPR.h new file mode 100644 index 0000000..bc9b227 --- /dev/null +++ b/lib/cpPlugins/Extensions/Visualization/Image3DMPR.h @@ -0,0 +1,99 @@ +#ifndef __CPPLUGINS__EXTENSIONS__VISUALIZATION__IMAGE3DMPR__H__ +#define __CPPLUGINS__EXTENSIONS__VISUALIZATION__IMAGE3DMPR__H__ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace cpPlugins +{ + namespace Extensions + { + namespace Visualization + { + /** + */ + class cpPlugins_Extensions_EXPORT Image3DMPR + { + public: + Image3DMPR( ); + virtual ~Image3DMPR( ); + + void SetImage( vtkImageData* image ); + void SetBackground( double r, double g, double b ); + void SetSize( unsigned int w, unsigned int h ); + void SetWindowLevel( double w, double l ); + + void AddPolyData( vtkPolyData* pd, double opacity = double( 1 ) ); + void AddPolyData( + vtkPolyData* pd, + double r, double g, double b, double opacity = double( 1 ) + ); + void AddPolyData( + vtkPolyData* pd, + vtkLookupTable* lut, + double opacity = double( 1 ) + ); + + unsigned int GetNumberOfSeeds( ) const; + void GetSeed( int n, double* s ) const; + unsigned int AddSeed( + const double& x, const double& y, const double& z + ) const; + + vtkRenderWindow* GetWindow( ) const; + vtkRenderer* GetRenderer( ) const; + + void Start( ); + void Render( ); + + protected: + vtkSmartPointer< vtkImageData > m_Image; + vtkSmartPointer< vtkOutlineSource > m_Outline; + vtkSmartPointer< vtkPolyDataMapper > m_OutlineMapper; + vtkSmartPointer< vtkActor > m_OutlineActor; + vtkSmartPointer< vtkCellPicker > m_Picker; + vtkSmartPointer< vtkImagePlaneWidget > m_WidgetX; + vtkSmartPointer< vtkImagePlaneWidget > m_WidgetY; + vtkSmartPointer< vtkImagePlaneWidget > m_WidgetZ; + vtkSmartPointer< vtkRenderer > m_Renderer; + vtkSmartPointer< vtkRenderWindow > m_Window; + vtkSmartPointer< vtkRenderWindowInteractor > m_Interactor; + + std::vector< vtkSmartPointer< vtkPolyData > > m_PolyDatas; + std::vector< vtkSmartPointer< vtkPolyDataMapper > > m_Mappers; + std::vector< vtkSmartPointer< vtkActor > > m_Actors; + + // Create the widget and its representation + vtkSmartPointer< vtkPointHandleRepresentation3D > + m_SeedHandleRepresentation; + vtkSmartPointer< vtkSeedRepresentation > m_SeedRepresentation; + vtkSmartPointer< vtkSeedWidget > m_SeedWidget; + }; + + } // ecapseman + + } // ecapseman + +} // ecapseman + +#endif // __CPPLUGINS__EXTENSIONS__VISUALIZATION__IMAGE3DMPR__H__ + +// eof - $RCSfile$ diff --git a/lib/cpPlugins/Extensions/Visualization/SeedWidgetCorrector.cxx b/lib/cpPlugins/Extensions/Visualization/SeedWidgetCorrector.cxx new file mode 100644 index 0000000..8c043c8 --- /dev/null +++ b/lib/cpPlugins/Extensions/Visualization/SeedWidgetCorrector.cxx @@ -0,0 +1,79 @@ +#include + +#include +#include + +#include +#include +#include + +// ------------------------------------------------------------------------- +cpPlugins::Extensions::Visualization::SeedWidgetCorrector* +cpPlugins::Extensions::Visualization::SeedWidgetCorrector:: +New( ) +{ + return( new cpPlugins::Extensions::Visualization::SeedWidgetCorrector ); +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::SeedWidgetCorrector:: +Configure( vtkCellPicker* picker ) +{ + this->m_Picker = picker; +} + +// ------------------------------------------------------------------------- +void cpPlugins::Extensions::Visualization::SeedWidgetCorrector:: +Execute( vtkObject* caller, unsigned long eventId, void* arguments ) +{ + vtkSeedWidget* wdg = dynamic_cast< vtkSeedWidget* >( caller ); + if( wdg == NULL || this->m_Picker == NULL ) + return; + + if( eventId == vtkCommand::PlacePointEvent ) + { + // Get newly added handle and its associated objects + int hId = *( reinterpret_cast< int* >( arguments ) ); + vtkHandleWidget* hWdg = wdg->GetSeed( hId ); + if( hWdg == NULL ) + return; + vtkHandleRepresentation* hRep = + dynamic_cast< vtkHandleRepresentation* >( hWdg->GetRepresentation( ) ); + if( hRep == NULL ) + return; + + // Get clicked point and 3D scene + double p[ 3 ]; + hRep->GetDisplayPosition( p ); + vtkRenderWindowInteractor* iren = wdg->GetInteractor( ); + if( iren == NULL ) + return; + vtkRenderer* ren = + iren->FindPokedRenderer( int( p[ 0 ] ), int( p[ 1 ] ) ); + if( ren == NULL ) + return; + + // Correct point with given picker + double q[ 3 ]; + this->m_Picker->Pick( p[ 0 ], p[ 1 ], p[ 2 ], ren ); + this->m_Picker->GetPickPosition( q ); + hRep->SetWorldPosition( q ); + + } // fi +} + +// ------------------------------------------------------------------------- +cpPlugins::Extensions::Visualization::SeedWidgetCorrector:: +SeedWidgetCorrector( ) + : vtkCommand( ), + m_Picker( NULL ) +{ +} + +// ------------------------------------------------------------------------- +cpPlugins::Extensions::Visualization::SeedWidgetCorrector:: +~SeedWidgetCorrector( ) +{ +} + +// eof - $RCSfile$ diff --git a/lib/cpPlugins/Extensions/Visualization/SeedWidgetCorrector.h b/lib/cpPlugins/Extensions/Visualization/SeedWidgetCorrector.h new file mode 100644 index 0000000..6493c0d --- /dev/null +++ b/lib/cpPlugins/Extensions/Visualization/SeedWidgetCorrector.h @@ -0,0 +1,49 @@ +#ifndef __CPPLUGINS__EXTENSIONS__VISUALIZATION__SEEDWIDGETCORRECTOR__H__ +#define __CPPLUGINS__EXTENSIONS__VISUALIZATION__SEEDWIDGETCORRECTOR__H__ + +#include + +#include + +class vtkCellPicker; +class vtkSeedWidget; + +namespace cpPlugins +{ + namespace Extensions + { + namespace Visualization + { + /** + */ + class cpPlugins_Extensions_EXPORT SeedWidgetCorrector + : public vtkCommand + { + public: + static SeedWidgetCorrector* New( ); + + void Configure( vtkCellPicker* picker ); + + virtual void Execute( + vtkObject* caller, + unsigned long eventId, + void* arguments + ); + + protected: + SeedWidgetCorrector( ); + virtual ~SeedWidgetCorrector( ); + + protected: + vtkCellPicker* m_Picker; + }; + + } // ecapseman + + } // ecapseman + +} // ecapseman + +#endif // __CPPLUGINS__EXTENSIONS__VISUALIZATION__SEEDWIDGETCORRECTOR__H__ + +// eof - $RCSfile$