1 #include "SeedWidget.h"
3 #include <vtkCommand.h>
4 #include <vtkProperty.h>
5 #include <vtkRenderWindowInteractor.h>
6 #include <cpExtensions/QT/SimpleMPRWidget.h>
8 // -------------------------------------------------------------------------
9 // This callback is responsible for changing update time
10 namespace cpPluginsWidgets
14 class SeedWidgetCallback
18 static SeedWidgetCallback* New( )
19 { return( new SeedWidgetCallback ); }
20 virtual void Execute( vtkObject* caller, unsigned long id, void* data )
23 id == vtkCommand::CursorChangedEvent ||
24 id == vtkCommand::PlacePointEvent
26 this->Widget->Modified( );
33 // -------------------------------------------------------------------------
34 void cpPluginsWidgets::SeedWidget::WidgetData::
36 cpPluginsWidgets::SeedWidget* parent,
37 vtkRenderWindowInteractor* interactor, vtkImageActor* actor
40 this->Placer = vtkSmartPointer< vtkImageActorPointPlacer >::New( );
41 this->Handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
42 this->Seed = vtkSmartPointer< vtkSeedRepresentation >::New( );
44 vtkSmartPointer< cpExtensions::Interaction::SeedWidget >::New( );
46 this->Placer->SetImageActor( actor );
47 this->Handle->GetProperty( )->SetColor( 1, 0, 0 );
48 this->Handle->SetPointPlacer( this->Placer );
49 this->Seed->SetHandleRepresentation( this->Handle );
50 this->Widget->SetRepresentation( this->Seed );
51 this->Widget->SetInteractor( interactor );
53 vtkSmartPointer< SeedWidgetCallback > cb =
54 vtkSmartPointer< SeedWidgetCallback >::New( );
56 this->Widget->AddObserver( vtkCommand::PlacePointEvent, cb );
57 this->Widget->AddObserver( vtkCommand::CursorChangedEvent, cb );
60 // -------------------------------------------------------------------------
61 void cpPluginsWidgets::SeedWidget::WidgetData::
67 // -------------------------------------------------------------------------
68 void cpPluginsWidgets::SeedWidget::WidgetData::
74 // -------------------------------------------------------------------------
75 cpPluginsWidgets::SeedWidget::
78 m_Configured( false ),
79 m_InitialNumberOfSeeds( 0 )
81 this->_AddOutput< cpPlugins::DataObject >( "Output" );
84 // -------------------------------------------------------------------------
85 cpPluginsWidgets::SeedWidget::
90 // -------------------------------------------------------------------------
91 void cpPluginsWidgets::SeedWidget::
94 auto points = this->_CreateVTK< vtkPoints >( );
95 if( this->m_Configured )
97 std::stringstream text;
98 points->Resize( this->m_InitialNumberOfSeeds );
100 auto wIt = this->m_Widgets.begin( );
101 wIt != this->m_Widgets.end( );
106 for( unsigned int i = 0; i < wIt->Seed->GetNumberOfSeeds( ); ++i )
108 wIt->Seed->GetSeedWorldPosition( i, pos );
111 text << pos[ 0 ] << " " << pos[ 1 ] << " " << pos[ 2 ];
112 points->InsertNextPoint( pos );
117 this->m_Parameters.SetString( "Text", text.str( ) );
121 std::vector< std::string > tokens;
122 cpPlugins::TokenizeString(
123 tokens, this->m_Parameters.GetString( "Text" ), "#"
125 this->m_InitialNumberOfSeeds = tokens.size( );
126 points->SetNumberOfPoints( 0 );
127 for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
129 std::vector< std::string > coords;
130 cpPlugins::TokenizeString( coords, *tIt, " \t" );
131 int dim = ( coords.size( ) < 3 )? coords.size( ): 3;
133 for( unsigned int d = 0; d < 3; ++d )
135 pos[ d ] = double( 0 );
138 std::istringstream value( coords[ d ] );
144 points->InsertNextPoint( pos );
145 this->m_Configured = true;
149 std::vector< vtkRenderWindowInteractor* > ints;
150 if( this->m_MPRViewer != NULL )
152 ints.push_back( this->m_MPRViewer->GetInteractor( 0 ) );
153 ints.push_back( this->m_MPRViewer->GetInteractor( 1 ) );
154 ints.push_back( this->m_MPRViewer->GetInteractor( 2 ) );
157 if( this->m_SingleInteractor != NULL )
158 ints.push_back( this->m_SingleInteractor );
160 for( auto iIt = ints.begin( ); iIt != ints.end( ); ++iIt )
162 auto ren = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
165 auto props = ren->GetViewProps( );
168 props->InitTraversal( );
170 while( ( prop = props->GetNextProp( ) ) != NULL )
172 auto actor = dynamic_cast< vtkImageActor* >( prop );
176 d.Configure( this, *iIt, actor );
178 this->m_Widgets.push_back( d );
179 this->m_Configured = true;
192 if( this->m_Configured )
196 "Could not create valid widget: are there any valid actors?"
198 this->GetOutput( "Output" )->SetVTK( points );