X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=plugins%2FcpPluginsWidgets%2FSeedWidget.cxx;h=18bcdeb4b4fac7f2983268dd0bd47f20203ad48f;hb=8743d2c32e8fb25da49b3875342a79f24ad6b7e1;hp=c5c014e3b0ef8f99531eb748b42e867994fb8753;hpb=1b0022070ff3b5f80f6f8c8b87f73032f5685eaf;p=cpPlugins.git diff --git a/plugins/cpPluginsWidgets/SeedWidget.cxx b/plugins/cpPluginsWidgets/SeedWidget.cxx index c5c014e..18bcdeb 100644 --- a/plugins/cpPluginsWidgets/SeedWidget.cxx +++ b/plugins/cpPluginsWidgets/SeedWidget.cxx @@ -1,214 +1,201 @@ #include "SeedWidget.h" -#include -#include -#include +#include +#include #include -#include +#include // ------------------------------------------------------------------------- -itk::ModifiedTimeType cpPluginsWidgets::SeedWidget:: -GetMTime( ) const +// This callback is responsible for changing update time +namespace cpPluginsWidgets { - // std::cout << "Seed GetMTime" << std::endl; - return( 0 /*this->Superclass::GetMTime( )*/ ); -} + /** + */ + class SeedWidgetCallback + : public vtkCommand + { + public: + static SeedWidgetCallback* New( ) + { return( new SeedWidgetCallback ); } + virtual void Execute( vtkObject* caller, unsigned long id, void* data ) + { + if( + id == vtkCommand::CursorChangedEvent || + id == vtkCommand::PlacePointEvent + ) + this->Widget->Modified( ); + } + SeedWidget* Widget; + }; + +} // ecapseman // ------------------------------------------------------------------------- -cpPluginsWidgets::SeedWidget:: -SeedWidget( ) - : Superclass( ), - m_Configured( false ) +void cpPluginsWidgets::SeedWidget::WidgetData:: +Configure( + cpPluginsWidgets::SeedWidget* parent, + vtkRenderWindowInteractor* interactor, vtkImageActor* actor + ) { - this->_AddInput( "ReferenceImage" ); - this->_AddOutput< cpPlugins::DataObject >( "Output" ); - - this->m_Parameters.ConfigureAsBool( "SeedsAreInRealSpace" ); - this->m_Parameters.SetBool( "SeedsAreInRealSpace", false ); + this->Placer = vtkSmartPointer< vtkImageActorPointPlacer >::New( ); + this->Handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( ); + this->Seed = vtkSmartPointer< vtkSeedRepresentation >::New( ); + this->Widget = + vtkSmartPointer< cpExtensions::Interaction::SeedWidget >::New( ); + + this->Placer->SetImageActor( actor ); + this->Handle->GetProperty( )->SetColor( 1, 0, 0 ); + this->Handle->SetPointPlacer( this->Placer ); + this->Seed->SetHandleRepresentation( this->Handle ); + this->Widget->SetRepresentation( this->Seed ); + this->Widget->SetInteractor( interactor ); + + vtkSmartPointer< SeedWidgetCallback > cb = + vtkSmartPointer< SeedWidgetCallback >::New( ); + cb->Widget = parent; + this->Widget->AddObserver( vtkCommand::PlacePointEvent, cb ); + this->Widget->AddObserver( vtkCommand::CursorChangedEvent, cb ); } // ------------------------------------------------------------------------- -cpPluginsWidgets::SeedWidget:: -~SeedWidget( ) +void cpPluginsWidgets::SeedWidget::WidgetData:: +On( ) { + this->Widget->On( ); } // ------------------------------------------------------------------------- -std::string cpPluginsWidgets::SeedWidget:: -_GenerateData( ) +void cpPluginsWidgets::SeedWidget::WidgetData:: +Off( ) { - auto image = this->GetInputData( "ReferenceImage" ); - std::string r = cpPlugin_Image_Demangle_Dim( _GD0, image, 3 ); - if( r != "" ) r = cpPlugin_Image_Demangle_Dim( _GD0, image, 2 ); - return( r ); + this->Widget->Off( ); } // ------------------------------------------------------------------------- -template< class I > -std::string cpPluginsWidgets::SeedWidget:: -_GD0( I* image ) +cpPluginsWidgets::SeedWidget:: +SeedWidget( ) + : Superclass( ), + m_Configured( false ), + m_InitialNumberOfSeeds( 0 ) { - if( image != NULL ) - { - if( this->m_Parameters.GetBool( "SeedsAreInRealSpace" ) ) - return( this->_GD1_Points< I >( image ) ); - else - return( this->_GD1_Vertices< I >( image ) ); - } - else - return( "Widgets::SeedWidget: Input image dimension not supported." ); + this->_AddOutput< cpPlugins::DataObject >( "Output" ); } // ------------------------------------------------------------------------- -template< class I > -std::string cpPluginsWidgets::SeedWidget:: -_GD1_Points( I* image ) +cpPluginsWidgets::SeedWidget:: +~SeedWidget( ) { - typedef cpExtensions::Interaction::ImageInteractorStyle _S; - typedef itk::Point< double, I::ImageDimension > _P; - typedef itk::SimpleDataObjectDecorator< std::vector< _P > > _Container; - - auto container = this->_CreateITK< _Container >( ); - - double aux_pnt[ 3 ]; - unsigned int dim = ( I::ImageDimension < 3 )? I::ImageDimension: 3; - - container->Get( ).clear( ); +} - // MPR - if( this->m_MPRViewer != NULL ) +// ------------------------------------------------------------------------- +void cpPluginsWidgets::SeedWidget:: +_GenerateData( ) +{ + auto points = this->_CreateVTK< vtkPoints >( ); + if( this->m_Configured ) { - for( unsigned int i = 0; i < 4; ++i ) + std::stringstream text; + points->Resize( this->m_InitialNumberOfSeeds ); + for( + auto wIt = this->m_Widgets.begin( ); + wIt != this->m_Widgets.end( ); + ++wIt + ) { - _S* s = - dynamic_cast< _S* >( - this->m_MPRViewer->GetInteractor( i )->GetInteractorStyle( ) - ); - if( s != NULL ) + double pos[ 3 ]; + for( unsigned int i = 0; i < wIt->Seed->GetNumberOfSeeds( ); ++i ) { - if( this->m_Configured ) - { - for( unsigned int i = 0; i < s->GetNumberOfSeeds( ); ++i ) - { - s->GetSeedAsPoint( i, aux_pnt ); - _P seed; - for( unsigned int d = 0; d < dim; ++d ) - seed[ d ] = aux_pnt[ d ]; - container->Get( ).push_back( seed ); - - } // rof - } - else - s->SeedWidgetOn( ); + wIt->Seed->GetSeedWorldPosition( i, pos ); + if( i > 0 ) + text << "#"; + text << pos[ 0 ] << " " << pos[ 1 ] << " " << pos[ 2 ]; + points->InsertNextPoint( pos ); - } // fi + } // rof } // rof - - } // fi - - // Single interactor - _S* s = dynamic_cast< _S* >( this->m_SingleInteractor ); - if( s != NULL ) + this->m_Parameters.SetString( "Text", text.str( ) ); + } + else { - if( this->m_Configured ) + std::vector< std::string > tokens; + cpPlugins::TokenizeString( + tokens, this->m_Parameters.GetString( "Text" ), "#" + ); + this->m_InitialNumberOfSeeds = tokens.size( ); + points->SetNumberOfPoints( 0 ); + for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt ) { - for( unsigned int i = 0; i < s->GetNumberOfSeeds( ); ++i ) + std::vector< std::string > coords; + cpPlugins::TokenizeString( coords, *tIt, " \t" ); + int dim = ( coords.size( ) < 3 )? coords.size( ): 3; + double pos[ 3 ]; + for( unsigned int d = 0; d < 3; ++d ) { - s->GetSeedAsPoint( i, aux_pnt ); - _P seed; - for( unsigned int d = 0; d < dim; ++d ) - seed[ d ] = aux_pnt[ d ]; - container->Get( ).push_back( seed ); - - } // rof - } - else - s->SeedWidgetOn( ); + pos[ d ] = double( 0 ); + if( d < dim ) + { + std::istringstream value( coords[ d ] ); + value >> pos[ d ]; - } // fi - this->m_Configured = true; + } // fi - this->GetOutputData( "Output" )->SetITK( container ); - return( "" ); -} + } // rof + points->InsertNextPoint( pos ); + this->m_Configured = true; -// ------------------------------------------------------------------------- -template< class I > -std::string cpPluginsWidgets::SeedWidget:: -_GD1_Vertices( I* image ) -{ - typedef cpExtensions::Interaction::ImageInteractorStyle _S; - typedef - itk::SimpleDataObjectDecorator< std::vector< typename I::IndexType > > - _Container; - auto container = this->_CreateITK< _Container >( ); + } // rof - double aux_pnt[ 3 ]; - unsigned int dim = ( I::ImageDimension < 3 )? I::ImageDimension: 3; + std::vector< vtkRenderWindowInteractor* > ints; + if( this->m_MPRViewer != NULL ) + { + ints.push_back( this->m_MPRViewer->GetInteractor( 0 ) ); + ints.push_back( this->m_MPRViewer->GetInteractor( 1 ) ); + ints.push_back( this->m_MPRViewer->GetInteractor( 2 ) ); - container->Get( ).clear( ); + } // fi + if( this->m_SingleInteractor != NULL ) + ints.push_back( this->m_SingleInteractor ); - // MPR - if( this->m_MPRViewer != NULL ) - { - for( unsigned int i = 0; i < 4; ++i ) + for( auto iIt = ints.begin( ); iIt != ints.end( ); ++iIt ) { - _S* s = - dynamic_cast< _S* >( - this->m_MPRViewer->GetInteractor( i )->GetInteractorStyle( ) - ); - if( s != NULL ) + auto ren = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( ); + if( ren != NULL ) { - if( this->m_Configured ) + auto props = ren->GetViewProps( ); + if( props != NULL ) { - for( unsigned int i = 0; i < s->GetNumberOfSeeds( ); ++i ) + props->InitTraversal( ); + vtkProp* prop; + while( ( prop = props->GetNextProp( ) ) != NULL ) { - s->GetSeedAsPoint( i, aux_pnt ); - typename I::PointType seed; - for( unsigned int d = 0; d < dim; ++d ) - seed[ d ] = aux_pnt[ d ]; - typename I::IndexType idx; - if( image->TransformPhysicalPointToIndex( seed, idx ) ) - container->Get( ).push_back( idx ); - - } // rof - } - else - s->SeedWidgetOn( ); + auto actor = dynamic_cast< vtkImageActor* >( prop ); + if( actor != NULL ) + { + WidgetData d; + d.Configure( this, *iIt, actor ); + d.On( ); + this->m_Widgets.push_back( d ); + this->m_Configured = true; - } // fi + } // fi - } // rof + } // elihw - } // fi + } // fi - // Single interactor - _S* s = dynamic_cast< _S* >( this->m_SingleInteractor ); - if( s != NULL ) - { - if( this->m_Configured ) - { - for( unsigned int i = 0; i < s->GetNumberOfSeeds( ); ++i ) - { - s->GetSeedAsPoint( i, aux_pnt ); - typename I::PointType seed; - for( unsigned int d = 0; d < dim; ++d ) - seed[ d ] = aux_pnt[ d ]; - typename I::IndexType idx; - if( image->TransformPhysicalPointToIndex( seed, idx ) ) - container->Get( ).push_back( idx ); + } // fi - } // rof - } - else - s->SeedWidgetOn( ); + } // rof } // fi - this->m_Configured = true; - - this->GetOutputData( "Output" )->SetITK( container ); - return( "" ); + if( this->m_Configured ) + this->Modified( ); + else + this->_Error( + "Could not create valid widget: are there any valid actors?" + ); + this->GetOutput( "Output" )->SetVTK( points ); } // eof - $RCSfile$