1 #include <plugins/Widgets/SeedWidget.h>
3 #include <vtkCommand.h>
4 #include <vtkProperty.h>
5 #include <vtkRenderer.h>
6 #include <vtkRenderWindowInteractor.h>
7 #include <cpExtensions/QT/SimpleMPRWidget.h>
9 // -------------------------------------------------------------------------
10 // This callback is responsible for changing update time
11 namespace cpPluginsWidgets
15 class SeedWidgetCallback
19 static SeedWidgetCallback* New( )
20 { return( new SeedWidgetCallback ); }
21 virtual void Execute( vtkObject* caller, unsigned long id, void* data )
24 id == vtkCommand::CursorChangedEvent ||
25 id == vtkCommand::PlacePointEvent
27 this->Widget->Modified( );
34 // -------------------------------------------------------------------------
35 void cpPluginsWidgets::SeedWidget::WidgetData::
37 cpPluginsWidgets::SeedWidget* parent,
38 vtkRenderWindowInteractor* interactor, vtkImageActor* actor
41 this->Placer = vtkSmartPointer< vtkImageActorPointPlacer >::New( );
42 this->Handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
43 this->Seed = vtkSmartPointer< vtkSeedRepresentation >::New( );
45 vtkSmartPointer< cpExtensions::Interaction::SeedWidget >::New( );
47 this->Placer->SetImageActor( actor );
48 this->Handle->GetProperty( )->SetColor( 1, 0, 0 );
49 this->Handle->SetPointPlacer( this->Placer );
50 this->Seed->SetHandleRepresentation( this->Handle );
51 this->Widget->SetRepresentation( this->Seed );
52 this->Widget->SetInteractor( interactor );
54 vtkSmartPointer< SeedWidgetCallback > cb =
55 vtkSmartPointer< SeedWidgetCallback >::New( );
57 this->Widget->AddObserver( vtkCommand::PlacePointEvent, cb );
58 this->Widget->AddObserver( vtkCommand::CursorChangedEvent, cb );
61 // -------------------------------------------------------------------------
62 void cpPluginsWidgets::SeedWidget::WidgetData::
68 // -------------------------------------------------------------------------
69 void cpPluginsWidgets::SeedWidget::WidgetData::
75 // -------------------------------------------------------------------------
76 cpPluginsWidgets::SeedWidget::
79 m_Configured( false ),
80 m_InitialNumberOfSeeds( 0 )
82 this->_AddOutput< cpPlugins::DataObject >( "Output" );
85 // -------------------------------------------------------------------------
86 cpPluginsWidgets::SeedWidget::
91 // -------------------------------------------------------------------------
92 void cpPluginsWidgets::SeedWidget::
95 auto points = this->_CreateVTK< vtkPoints >( );
96 if( this->m_Configured )
98 std::stringstream text;
99 points->Resize( this->m_InitialNumberOfSeeds );
101 auto wIt = this->m_Widgets.begin( );
102 wIt != this->m_Widgets.end( );
107 for( unsigned int i = 0; i < wIt->Seed->GetNumberOfSeeds( ); ++i )
109 wIt->Seed->GetSeedWorldPosition( i, pos );
112 text << pos[ 0 ] << " " << pos[ 1 ] << " " << pos[ 2 ];
113 points->InsertNextPoint( pos );
118 this->m_Parameters.SetString( "Text", text.str( ) );
122 std::vector< std::string > tokens;
123 cpPlugins::TokenizeString(
124 tokens, this->m_Parameters.GetString( "Text" ), "#"
126 this->m_InitialNumberOfSeeds = tokens.size( );
127 points->SetNumberOfPoints( 0 );
128 for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
130 std::vector< std::string > coords;
131 cpPlugins::TokenizeString( coords, *tIt, " \t" );
132 int dim = ( coords.size( ) < 3 )? coords.size( ): 3;
134 for( unsigned int d = 0; d < 3; ++d )
136 pos[ d ] = double( 0 );
139 std::istringstream value( coords[ d ] );
145 points->InsertNextPoint( pos );
146 this->m_Configured = true;
150 std::vector< vtkRenderWindowInteractor* > ints;
151 if( this->m_MPRViewer != NULL )
153 ints.push_back( this->m_MPRViewer->GetInteractor( 0 ) );
154 ints.push_back( this->m_MPRViewer->GetInteractor( 1 ) );
155 ints.push_back( this->m_MPRViewer->GetInteractor( 2 ) );
158 if( this->m_SingleInteractor != NULL )
159 ints.push_back( this->m_SingleInteractor );
161 for( auto iIt = ints.begin( ); iIt != ints.end( ); ++iIt )
163 auto ren = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
166 auto props = ren->GetViewProps( );
169 props->InitTraversal( );
171 while( ( prop = props->GetNextProp( ) ) != NULL )
173 auto actor = dynamic_cast< vtkImageActor* >( prop );
177 d.Configure( this, *iIt, actor );
179 this->m_Widgets.push_back( d );
180 this->m_Configured = true;
193 if( this->m_Configured )
197 "Could not create valid widget: are there any valid actors?"
199 this->GetOutput( "Output" )->SetVTK( points );