1 #include <plugins/Widgets/SeedWidget.h>
3 #include <cpPlugins/DataObjects/Image.h>
4 #include <cpPlugins/DataObjects/Mesh.h>
6 #include <cpExtensions/Interaction/SeedWidget.h>
7 #include <cpExtensions/Interaction/ImageSlicePointPlacer.h>
8 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
10 #include <vtkHandleWidget.h>
11 #include <vtkImageData.h>
13 #include <vtkPointHandleRepresentation3D.h>
14 #include <vtkProperty.h>
15 #include <vtkRendererCollection.h>
16 #include <vtkRenderWindow.h>
17 #include <vtkSeedRepresentation.h>
19 // -------------------------------------------------------------------------
20 cpPluginsWidgets::SeedWidget::
24 typedef cpPlugins::BaseObjects::DataObject _TData;
25 typedef cpPlugins::DataObjects::Mesh _TMesh;
28 this->_ConfigureInput< _TData >( "Input", false, false );
29 this->_ConfigureOutput< _TMesh >( "Output" );
32 auto out = this->_CreateVTK< vtkPolyData >( );
33 out->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
34 out->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
35 out->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
36 out->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
37 out->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
38 this->GetOutput( "Output" )->SetVTK( out );
41 // -------------------------------------------------------------------------
42 cpPluginsWidgets::SeedWidget::
46 for( auto w = this->m_Widgets.begin( ); w != this->m_Widgets.end( ); ++w )
47 w->second->EnabledOff( );
48 this->m_Widgets.clear( );
52 // -------------------------------------------------------------------------
53 void cpPluginsWidgets::SeedWidget::
56 auto image = this->GetInputData< vtkImageData >( "Input" );
58 this->_GD0_Image( image );
60 this->_Error( "Invalid input image." );
63 // -------------------------------------------------------------------------
64 void cpPluginsWidgets::SeedWidget::
65 _GD0_Image( vtkImageData* image )
67 auto seeds = this->GetOutputData< vtkPolyData >( "Output" );
68 if( this->m_Widgets.size( ) == 0 )
70 this->m_Command = vtkSmartPointer< TCallback >::New( );
71 this->m_Command->SetSeeds( seeds );
73 auto inIt = this->m_Interactors.begin( );
74 inIt != this->m_Interactors.end( );
78 auto rends = ( *inIt )->GetRenderWindow( )->GetRenderers( );
79 std::set< TImageActor* > all_props;
82 rends->InitTraversal( );
83 while( vtkRenderer* ren = rends->GetNextItem( ) )
85 auto props = ren->GetViewProps( );
86 props->InitTraversal( );
87 while( vtkProp* p = props->GetNextProp( ) )
89 auto image_actor = dynamic_cast< TImageActor* >( p );
90 if( image_actor != NULL )
92 if( image_actor->GetImage( ) == image )
93 all_props.insert( image_actor );
102 if( all_props.size( ) == 1 )
104 if( this->m_Widgets.find( *inIt ) == this->m_Widgets.end( ) )
106 auto act = *( all_props.begin( ) );
107 auto pla = vtkSmartPointer< TPlacer >::New( );
108 auto hnd = vtkSmartPointer< THandleRep >::New( );
109 auto rep = vtkSmartPointer< TSeedRep >::New( );
110 auto wdg = vtkSmartPointer< TWidget >::New( );
112 pla->SetImageSlice( act );
113 hnd->GetProperty( )->SetColor( 1, 0, 0 );
114 hnd->SetPointPlacer( pla );
115 rep->SetHandleRepresentation( hnd );
116 wdg->SetRepresentation( rep );
117 wdg->SetInteractor( *inIt );
118 act->AddObserver( vtkCommand::InteractionEvent, this->m_Command );
119 wdg->AddObserver( vtkCommand::PlacePointEvent, this->m_Command );
120 wdg->AddObserver( vtkCommand::CursorChangedEvent, this->m_Command );
122 this->m_Widgets[ *inIt ] = wdg;
130 // Associate input text
131 std::string text = this->m_Parameters.GetString( "Text" );
132 std::vector< std::string > tok1, tok2;
133 cpExtensions::Tokenize( tok1, text, "#" );
134 for( auto t1 = tok1.begin( ); t1 != tok1.end( ); ++t1 )
138 cpExtensions::Tokenize( tok2, *t1, " " );
140 for( unsigned int d = 0; d < 3; ++d )
142 if( d < tok2.size( ) )
144 std::istringstream str( tok2[ d ] );
148 x[ d ] = double( 0 );
152 seeds->GetPoints( )->InsertNextPoint( x );
153 seeds->GetVerts( )->InsertNextCell( 1 );
154 seeds->GetVerts( )->InsertCellPoint(
155 seeds->GetPoints( )->GetNumberOfPoints( ) - 1
166 std::stringstream text;
167 for( long i = 0; i < seeds->GetNumberOfPoints( ); ++i )
169 seeds->GetPoint( i, x );
170 text << x[ 0 ] << " " << x[ 1 ] << " " << x[ 2 ] << "#";
173 this->m_Parameters.SetString( "Text", text.str( ) );
178 // -------------------------------------------------------------------------
179 cpPluginsWidgets::SeedWidget::TCallback*
180 cpPluginsWidgets::SeedWidget::TCallback::
183 return( new TCallback );
186 // -------------------------------------------------------------------------
187 void cpPluginsWidgets::SeedWidget::TCallback::
188 Execute( vtkObject* caller, unsigned long id, void* data )
190 static const double EPS = 1e-5;
191 auto src_act = dynamic_cast< TImageActor* >( caller );
192 auto src_wdg = dynamic_cast< TWidget* >( caller );
194 if( id == vtkCommand::InteractionEvent && src_act != NULL )
196 TWidget* widget = NULL;
197 TSeedRep* seed_rep = NULL;
199 auto w = this->m_Widgets.begin( );
200 while( w != this->m_Widgets.end( ) && widget == NULL )
203 dynamic_cast< vtkSeedRepresentation* >(
204 ( *w )->GetRepresentation( )
208 auto hnd = rep->GetHandleRepresentation( );
212 dynamic_cast< SeedWidget::TPlacer* >(
213 hnd->GetPointPlacer( )
217 auto act = pla->GetImageSlice( );
236 // Cut through given seeds
237 auto plane = src_act->GetSlicePlane( );
239 std::vector< long > ids;
240 for( long i = 0; i < this->m_Seeds->GetNumberOfPoints( ); ++i )
242 this->m_Seeds->GetPoint( i, x );
243 double d = plane->DistanceToPlane( x );
250 widget->CompleteInteraction( );
251 widget->EnabledOff( );
252 int nSeeds = seed_rep->GetNumberOfSeeds ();
253 for( int i = 0; i < nSeeds; ++i )
255 seed_rep->RemoveLastHandle( );
256 widget->DeleteSeed( seed_rep->GetNumberOfSeeds( ) );
260 if( ids.size( ) > 0 )
262 auto ren = widget->GetInteractor( )->FindPokedRenderer(
263 widget->GetInteractor( )->GetEventPosition( )[ 0 ],
264 widget->GetInteractor( )->GetEventPosition( )[ 1 ]
268 seed_rep->BuildRepresentation( );
269 for( auto id = ids.begin( ); id != ids.end( ); ++id )
271 this->m_Seeds->GetPoint( *id, x );
272 widget->ComputeWorldToDisplay(
273 ren, x[ 0 ], x[ 1 ], x[ 2 ], x
275 x[ 2 ] = double( 0 );
276 int s = seed_rep->CreateHandle( x );
277 vtkHandleWidget* curr_hnd = widget->CreateNewHandle( );
278 seed_rep->SetSeedDisplayPosition( s, x );
279 curr_hnd->SetEnabled( 1 );
286 widget->RestartInteraction( );
287 widget->EnabledOn( );
292 else if( id == vtkCommand::PlacePointEvent && src_wdg != NULL )
294 this->m_Widgets.insert( src_wdg );
296 dynamic_cast< vtkSeedRepresentation* >(
297 src_wdg->GetRepresentation( )
301 unsigned long nSeeds = rep->GetNumberOfSeeds( );
305 rep->GetSeedWorldPosition( nSeeds - 1, pos );
307 this->m_Seeds->GetPoints( )->InsertNextPoint( pos );
308 this->m_Seeds->GetVerts( )->InsertNextCell( 1 );
309 this->m_Seeds->GetVerts( )->InsertCellPoint(
310 this->m_Seeds->GetPoints( )->GetNumberOfPoints( ) - 1
312 this->m_Seeds->Modified( );
321 // -------------------------------------------------------------------------
322 void cpPluginsWidgets::SeedWidget::TCallback::
323 SetSeeds( vtkPolyData* seeds )
325 this->m_Seeds = seeds;
328 // -------------------------------------------------------------------------
329 cpPluginsWidgets::SeedWidget::TCallback::
336 // -------------------------------------------------------------------------
337 cpPluginsWidgets::SeedWidget::TCallback::