1 #include <VTKWidgets/SeedWidget.h>
3 #include <cpInstances/Image.h>
4 #include <cpInstances/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 void cpPluginsVTKWidgets::SeedWidget::
25 // -------------------------------------------------------------------------
26 void cpPluginsVTKWidgets::SeedWidget::
29 for( auto w = this->m_Widgets.begin( ); w != this->m_Widgets.end( ); ++w )
31 if( v ) w->second->RestartInteraction( );
32 else w->second->CompleteInteraction( );
33 w->second->SetEnabled( v );
39 // -------------------------------------------------------------------------
40 bool cpPluginsVTKWidgets::SeedWidget::
43 if( this->m_Widgets.size( ) > 0 )
45 auto i = this->m_Widgets.begin( );
46 TWidget* w = const_cast< TWidget* >( i->second.GetPointer( ) );
47 return( w->GetEnabled( ) != 0 );
53 // -------------------------------------------------------------------------
54 cpPluginsVTKWidgets::SeedWidget::
58 typedef cpPlugins::BaseObjects::DataObject _TData;
59 typedef cpInstances::Mesh _TMesh;
62 this->_ConfigureInput< _TData >( "Input", false, false );
63 this->_ConfigureOutput< _TMesh >( "Output" );
66 auto out = this->_CreateVTK< vtkPolyData >( );
67 out->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
68 out->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
69 out->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
70 out->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
71 out->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
72 this->GetOutput( "Output" )->SetVTK( out );
75 // -------------------------------------------------------------------------
76 cpPluginsVTKWidgets::SeedWidget::
80 for( auto w = this->m_Widgets.begin( ); w != this->m_Widgets.end( ); ++w )
81 w->second->EnabledOff( );
82 this->m_Widgets.clear( );
86 // -------------------------------------------------------------------------
87 void cpPluginsVTKWidgets::SeedWidget::
90 auto image = this->GetInputData< vtkImageData >( "Input" );
92 this->_GD0_Image( image );
94 this->_Error( "Invalid input image." );
97 // -------------------------------------------------------------------------
98 void cpPluginsVTKWidgets::SeedWidget::
99 _GD0_Image( vtkImageData* image )
101 auto seeds = this->GetOutputData< vtkPolyData >( "Output" );
102 if( this->m_Widgets.size( ) == 0 )
104 this->m_Command = vtkSmartPointer< TCallback >::New( );
105 this->m_Command->SetSeeds( seeds );
107 auto inIt = this->m_Interactors.begin( );
108 inIt != this->m_Interactors.end( );
112 auto rends = ( *inIt )->GetRenderWindow( )->GetRenderers( );
113 std::set< TImageActor* > all_props;
116 rends->InitTraversal( );
117 while( vtkRenderer* ren = rends->GetNextItem( ) )
119 auto props = ren->GetViewProps( );
120 props->InitTraversal( );
121 while( vtkProp* p = props->GetNextProp( ) )
123 auto image_actor = dynamic_cast< TImageActor* >( p );
124 if( image_actor != NULL )
126 if( image_actor->GetImage( ) == image )
127 all_props.insert( image_actor );
136 if( all_props.size( ) == 1 )
138 if( this->m_Widgets.find( *inIt ) == this->m_Widgets.end( ) )
140 auto act = *( all_props.begin( ) );
141 auto pla = vtkSmartPointer< TPlacer >::New( );
142 auto hnd = vtkSmartPointer< THandleRep >::New( );
143 auto rep = vtkSmartPointer< TSeedRep >::New( );
144 auto wdg = vtkSmartPointer< TWidget >::New( );
146 pla->SetImageSlice( act );
147 hnd->GetProperty( )->SetColor( 1, 0, 0 );
148 hnd->SetPointPlacer( pla );
149 rep->SetHandleRepresentation( hnd );
150 wdg->SetRepresentation( rep );
151 wdg->SetInteractor( *inIt );
152 act->AddObserver( vtkCommand::InteractionEvent, this->m_Command );
153 wdg->AddObserver( vtkCommand::PlacePointEvent, this->m_Command );
154 wdg->AddObserver( vtkCommand::CursorChangedEvent, this->m_Command );
155 this->m_Widgets[ *inIt ] = wdg;
163 // Associate input text
164 std::string text = this->m_Parameters.GetString( "Text" );
165 std::vector< std::string > tok1, tok2;
166 cpExtensions::Tokenize( tok1, text, "#" );
167 if( tok1.size( ) > seeds->GetPoints( )->GetNumberOfPoints( ) )
169 for( auto t1 = tok1.begin( ); t1 != tok1.end( ); ++t1 )
173 cpExtensions::Tokenize( tok2, *t1, " " );
175 for( unsigned int d = 0; d < 3; ++d )
177 if( d < tok2.size( ) )
179 std::istringstream str( tok2[ d ] );
183 x[ d ] = double( 0 );
187 seeds->GetPoints( )->InsertNextPoint( x );
188 seeds->GetVerts( )->InsertNextCell( 1 );
189 seeds->GetVerts( )->InsertCellPoint(
190 seeds->GetPoints( )->GetNumberOfPoints( ) - 1
203 std::stringstream text;
204 for( long i = 0; i < seeds->GetNumberOfPoints( ); ++i )
206 seeds->GetPoint( i, x );
207 text << x[ 0 ] << " " << x[ 1 ] << " " << x[ 2 ] << "#";
210 this->m_Parameters.SetString( "Text", text.str( ) );
215 // -------------------------------------------------------------------------
216 cpPluginsVTKWidgets::SeedWidget::TCallback*
217 cpPluginsVTKWidgets::SeedWidget::TCallback::
220 return( new TCallback );
223 // -------------------------------------------------------------------------
224 void cpPluginsVTKWidgets::SeedWidget::TCallback::
225 Execute( vtkObject* caller, unsigned long id, void* data )
227 static const double EPS = 1e-5;
228 auto src_act = dynamic_cast< TImageActor* >( caller );
229 auto src_wdg = dynamic_cast< TWidget* >( caller );
231 if( id == vtkCommand::InteractionEvent && src_act != NULL )
233 TWidget* widget = NULL;
234 TSeedRep* seed_rep = NULL;
236 auto w = this->m_Widgets.begin( );
237 while( w != this->m_Widgets.end( ) && widget == NULL )
240 dynamic_cast< vtkSeedRepresentation* >(
241 ( *w )->GetRepresentation( )
245 auto hnd = rep->GetHandleRepresentation( );
249 dynamic_cast< SeedWidget::TPlacer* >(
250 hnd->GetPointPlacer( )
254 auto act = pla->GetImageSlice( );
273 // Cut through given seeds
274 auto plane = src_act->GetSlicePlane( );
276 std::vector< long > ids;
277 for( long i = 0; i < this->m_Seeds->GetNumberOfPoints( ); ++i )
279 this->m_Seeds->GetPoint( i, x );
280 double d = plane->DistanceToPlane( x );
287 widget->CompleteInteraction( );
288 widget->EnabledOff( );
289 int nSeeds = seed_rep->GetNumberOfSeeds ();
290 for( int i = 0; i < nSeeds; ++i )
292 seed_rep->RemoveLastHandle( );
293 widget->DeleteSeed( seed_rep->GetNumberOfSeeds( ) );
297 if( ids.size( ) > 0 )
299 auto ren = widget->GetInteractor( )->FindPokedRenderer(
300 widget->GetInteractor( )->GetEventPosition( )[ 0 ],
301 widget->GetInteractor( )->GetEventPosition( )[ 1 ]
305 seed_rep->BuildRepresentation( );
306 for( auto id = ids.begin( ); id != ids.end( ); ++id )
308 this->m_Seeds->GetPoint( *id, x );
309 widget->ComputeWorldToDisplay(
310 ren, x[ 0 ], x[ 1 ], x[ 2 ], x
312 x[ 2 ] = double( 0 );
313 int s = seed_rep->CreateHandle( x );
314 vtkHandleWidget* curr_hnd = widget->CreateNewHandle( );
315 seed_rep->SetSeedDisplayPosition( s, x );
316 curr_hnd->SetEnabled( 1 );
323 widget->RestartInteraction( );
324 widget->EnabledOn( );
329 else if( id == vtkCommand::PlacePointEvent && src_wdg != NULL )
331 this->m_Widgets.insert( src_wdg );
333 dynamic_cast< vtkSeedRepresentation* >(
334 src_wdg->GetRepresentation( )
338 unsigned long nSeeds = rep->GetNumberOfSeeds( );
342 rep->GetSeedWorldPosition( nSeeds - 1, pos );
344 this->m_Seeds->GetPoints( )->InsertNextPoint( pos );
345 this->m_Seeds->GetVerts( )->InsertNextCell( 1 );
346 this->m_Seeds->GetVerts( )->InsertCellPoint(
347 this->m_Seeds->GetPoints( )->GetNumberOfPoints( ) - 1
349 this->m_Seeds->Modified( );
358 // -------------------------------------------------------------------------
359 void cpPluginsVTKWidgets::SeedWidget::TCallback::
360 SetSeeds( vtkPolyData* seeds )
362 this->m_Seeds = seeds;
365 // -------------------------------------------------------------------------
366 cpPluginsVTKWidgets::SeedWidget::TCallback::
373 // -------------------------------------------------------------------------
374 cpPluginsVTKWidgets::SeedWidget::TCallback::