#include <plugins/Widgets/SeedWidget.h>
-#include <vtkCommand.h>
-#include <vtkProperty.h>
-#include <vtkRenderer.h>
-#include <vtkRenderWindowInteractor.h>
-#include <cpExtensions/QT/SimpleMPRWidget.h>
+#include <cpPlugins/DataObjects/Image.h>
+#include <cpPlugins/DataObjects/Mesh.h>
-// -------------------------------------------------------------------------
-// This callback is responsible for changing update time
-namespace cpPluginsWidgets
-{
- /**
- */
- 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;
- };
+#include <cpExtensions/Interaction/SeedWidget.h>
+#include <cpExtensions/Interaction/ImageSlicePointPlacer.h>
+#include <cpExtensions/Visualization/WindowLevelImageActor.h>
-} // ecapseman
+#include <vtkHandleWidget.h>
+#include <vtkImageData.h>
+#include <vtkPlane.h>
+#include <vtkPointHandleRepresentation3D.h>
+#include <vtkProperty.h>
+#include <vtkRendererCollection.h>
+#include <vtkRenderWindow.h>
+#include <vtkSeedRepresentation.h>
// -------------------------------------------------------------------------
-void cpPluginsWidgets::SeedWidget::WidgetData::
-Configure(
- cpPluginsWidgets::SeedWidget* parent,
- vtkRenderWindowInteractor* interactor, vtkImageActor* actor
- )
+void cpPluginsWidgets::SeedWidget::
+Clear( )
{
- 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 );
}
// -------------------------------------------------------------------------
-void cpPluginsWidgets::SeedWidget::WidgetData::
-On( )
+void cpPluginsWidgets::SeedWidget::
+SetEnabled( bool v )
{
- this->Widget->On( );
+ for( auto w = this->m_Widgets.begin( ); w != this->m_Widgets.end( ); ++w )
+ {
+ if( v ) w->second->RestartInteraction( );
+ else w->second->CompleteInteraction( );
+ w->second->SetEnabled( v );
+ w->second->Render( );
+
+ } // rof
}
// -------------------------------------------------------------------------
-void cpPluginsWidgets::SeedWidget::WidgetData::
-Off( )
+bool cpPluginsWidgets::SeedWidget::
+GetEnabled( ) const
{
- this->Widget->Off( );
+ if( this->m_Widgets.size( ) > 0 )
+ {
+ auto i = this->m_Widgets.begin( );
+ TWidget* w = const_cast< TWidget* >( i->second.GetPointer( ) );
+ return( w->GetEnabled( ) != 0 );
+ }
+ else
+ return( false );
}
// -------------------------------------------------------------------------
cpPluginsWidgets::SeedWidget::
SeedWidget( )
- : Superclass( ),
- m_Configured( false ),
- m_InitialNumberOfSeeds( 0 )
+ : Superclass( )
{
- this->_AddOutput< cpPlugins::DataObject >( "Output" );
+ typedef cpPlugins::BaseObjects::DataObject _TData;
+ typedef cpPlugins::DataObjects::Mesh _TMesh;
+
+ // Create ports
+ this->_ConfigureInput< _TData >( "Input", false, false );
+ this->_ConfigureOutput< _TMesh >( "Output" );
+
+ // Create output data
+ auto out = this->_CreateVTK< vtkPolyData >( );
+ out->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
+ out->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
+ out->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
+ out->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
+ out->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
+ this->GetOutput( "Output" )->SetVTK( out );
}
// -------------------------------------------------------------------------
cpPluginsWidgets::SeedWidget::
~SeedWidget( )
{
+ /* TODO
+ for( auto w = this->m_Widgets.begin( ); w != this->m_Widgets.end( ); ++w )
+ w->second->EnabledOff( );
+ this->m_Widgets.clear( );
+ */
}
// -------------------------------------------------------------------------
void cpPluginsWidgets::SeedWidget::
_GenerateData( )
{
- auto points = this->_CreateVTK< vtkPoints >( );
- if( this->m_Configured )
+ auto image = this->GetInputData< vtkImageData >( "Input" );
+ if( image != NULL )
+ this->_GD0_Image( image );
+ else
+ this->_Error( "Invalid input image." );
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsWidgets::SeedWidget::
+_GD0_Image( vtkImageData* image )
+{
+ auto seeds = this->GetOutputData< vtkPolyData >( "Output" );
+ if( this->m_Widgets.size( ) == 0 )
{
- std::stringstream text;
- points->Resize( this->m_InitialNumberOfSeeds );
+ this->m_Command = vtkSmartPointer< TCallback >::New( );
+ this->m_Command->SetSeeds( seeds );
for(
- auto wIt = this->m_Widgets.begin( );
- wIt != this->m_Widgets.end( );
- ++wIt
+ auto inIt = this->m_Interactors.begin( );
+ inIt != this->m_Interactors.end( );
+ ++inIt
)
{
- double pos[ 3 ];
- for( unsigned int i = 0; i < wIt->Seed->GetNumberOfSeeds( ); ++i )
+ auto rends = ( *inIt )->GetRenderWindow( )->GetRenderers( );
+ std::set< TImageActor* > all_props;
+ if( rends != NULL )
{
- wIt->Seed->GetSeedWorldPosition( i, pos );
- if( i > 0 )
- text << "#";
- text << pos[ 0 ] << " " << pos[ 1 ] << " " << pos[ 2 ];
- points->InsertNextPoint( pos );
+ rends->InitTraversal( );
+ while( vtkRenderer* ren = rends->GetNextItem( ) )
+ {
+ auto props = ren->GetViewProps( );
+ props->InitTraversal( );
+ while( vtkProp* p = props->GetNextProp( ) )
+ {
+ auto image_actor = dynamic_cast< TImageActor* >( p );
+ if( image_actor != NULL )
+ {
+ if( image_actor->GetImage( ) == image )
+ all_props.insert( image_actor );
- } // rof
+ } // fi
- } // rof
- this->m_Parameters.SetString( "Text", text.str( ) );
- }
- else
- {
- 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 )
- {
- 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 )
+ } // elihw
+
+ } // elihw
+
+ } // fi
+ if( all_props.size( ) == 1 )
{
- pos[ d ] = double( 0 );
- if( d < dim )
+ if( this->m_Widgets.find( *inIt ) == this->m_Widgets.end( ) )
{
- std::istringstream value( coords[ d ] );
- value >> pos[ d ];
+ auto act = *( all_props.begin( ) );
+ auto pla = vtkSmartPointer< TPlacer >::New( );
+ auto hnd = vtkSmartPointer< THandleRep >::New( );
+ auto rep = vtkSmartPointer< TSeedRep >::New( );
+ auto wdg = vtkSmartPointer< TWidget >::New( );
+
+ pla->SetImageSlice( act );
+ hnd->GetProperty( )->SetColor( 1, 0, 0 );
+ hnd->SetPointPlacer( pla );
+ rep->SetHandleRepresentation( hnd );
+ wdg->SetRepresentation( rep );
+ wdg->SetInteractor( *inIt );
+ act->AddObserver( vtkCommand::InteractionEvent, this->m_Command );
+ wdg->AddObserver( vtkCommand::PlacePointEvent, this->m_Command );
+ wdg->AddObserver( vtkCommand::CursorChangedEvent, this->m_Command );
+ this->m_Widgets[ *inIt ] = wdg;
} // fi
- } // rof
- points->InsertNextPoint( pos );
- this->m_Configured = true;
+ } // fi
} // rof
- std::vector< vtkRenderWindowInteractor* > ints;
- if( this->m_MPRViewer != NULL )
+ // Associate input text
+ std::string text = this->m_Parameters.GetString( "Text" );
+ std::vector< std::string > tok1, tok2;
+ cpExtensions::Tokenize( tok1, text, "#" );
+ for( auto t1 = tok1.begin( ); t1 != tok1.end( ); ++t1 )
+ {
+ if( *t1 != "" )
+ {
+ cpExtensions::Tokenize( tok2, *t1, " " );
+ double x[ 3 ];
+ for( unsigned int d = 0; d < 3; ++d )
+ {
+ if( d < tok2.size( ) )
+ {
+ std::istringstream str( tok2[ d ] );
+ str >> x[ d ];
+ }
+ else
+ x[ d ] = double( 0 );
+
+ } // rof
+
+ seeds->GetPoints( )->InsertNextPoint( x );
+ seeds->GetVerts( )->InsertNextCell( 1 );
+ seeds->GetVerts( )->InsertCellPoint(
+ seeds->GetPoints( )->GetNumberOfPoints( ) - 1
+ );
+ seeds->Modified( );
+
+ } // fi
+
+ } // rof
+ }
+ else
+ {
+ double x[ 3 ];
+ std::stringstream text;
+ for( long i = 0; i < seeds->GetNumberOfPoints( ); ++i )
{
- ints.push_back( this->m_MPRViewer->GetInteractor( 0 ) );
- ints.push_back( this->m_MPRViewer->GetInteractor( 1 ) );
- ints.push_back( this->m_MPRViewer->GetInteractor( 2 ) );
+ seeds->GetPoint( i, x );
+ text << x[ 0 ] << " " << x[ 1 ] << " " << x[ 2 ] << "#";
- } // fi
- if( this->m_SingleInteractor != NULL )
- ints.push_back( this->m_SingleInteractor );
+ } // rof
+ this->m_Parameters.SetString( "Text", text.str( ) );
+
+ } // fi
+}
+
+// -------------------------------------------------------------------------
+cpPluginsWidgets::SeedWidget::TCallback*
+cpPluginsWidgets::SeedWidget::TCallback::
+New( )
+{
+ return( new TCallback );
+}
- for( auto iIt = ints.begin( ); iIt != ints.end( ); ++iIt )
+// -------------------------------------------------------------------------
+void cpPluginsWidgets::SeedWidget::TCallback::
+Execute( vtkObject* caller, unsigned long id, void* data )
+{
+ static const double EPS = 1e-5;
+ auto src_act = dynamic_cast< TImageActor* >( caller );
+ auto src_wdg = dynamic_cast< TWidget* >( caller );
+
+ if( id == vtkCommand::InteractionEvent && src_act != NULL )
+ {
+ TWidget* widget = NULL;
+ TSeedRep* seed_rep = NULL;
+
+ auto w = this->m_Widgets.begin( );
+ while( w != this->m_Widgets.end( ) && widget == NULL )
{
- auto ren = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
- if( ren != NULL )
+ auto rep =
+ dynamic_cast< vtkSeedRepresentation* >(
+ ( *w )->GetRepresentation( )
+ );
+ if( rep != NULL )
{
- auto props = ren->GetViewProps( );
- if( props != NULL )
+ auto hnd = rep->GetHandleRepresentation( );
+ if( hnd != NULL )
{
- props->InitTraversal( );
- vtkProp* prop;
- while( ( prop = props->GetNextProp( ) ) != NULL )
+ auto pla =
+ dynamic_cast< SeedWidget::TPlacer* >(
+ hnd->GetPointPlacer( )
+ );
+ if( pla != NULL )
{
- auto actor = dynamic_cast< vtkImageActor* >( prop );
- if( actor != NULL )
+ auto act = pla->GetImageSlice( );
+ if( act == src_act )
{
- WidgetData d;
- d.Configure( this, *iIt, actor );
- d.On( );
- this->m_Widgets.push_back( d );
- this->m_Configured = true;
+ widget = *w;
+ seed_rep = rep;
} // fi
- } // elihw
+ } // fi
} // fi
} // fi
+ w++;
- } // rof
+ } // elihw
+
+ if( widget != NULL )
+ {
+ // Cut through given seeds
+ auto plane = src_act->GetSlicePlane( );
+ double x[ 3 ];
+ std::vector< long > ids;
+ for( long i = 0; i < this->m_Seeds->GetNumberOfPoints( ); ++i )
+ {
+ this->m_Seeds->GetPoint( i, x );
+ double d = plane->DistanceToPlane( x );
+ if( d <= EPS )
+ ids.push_back( i );
+
+ } // rof
+
+ // Erase seeds
+ widget->CompleteInteraction( );
+ widget->EnabledOff( );
+ int nSeeds = seed_rep->GetNumberOfSeeds ();
+ for( int i = 0; i < nSeeds; ++i )
+ {
+ seed_rep->RemoveLastHandle( );
+ widget->DeleteSeed( seed_rep->GetNumberOfSeeds( ) );
+
+ } // rof
+
+ if( ids.size( ) > 0 )
+ {
+ auto ren = widget->GetInteractor( )->FindPokedRenderer(
+ widget->GetInteractor( )->GetEventPosition( )[ 0 ],
+ widget->GetInteractor( )->GetEventPosition( )[ 1 ]
+ );
+
+ // Add seeds
+ seed_rep->BuildRepresentation( );
+ for( auto id = ids.begin( ); id != ids.end( ); ++id )
+ {
+ this->m_Seeds->GetPoint( *id, x );
+ widget->ComputeWorldToDisplay(
+ ren, x[ 0 ], x[ 1 ], x[ 2 ], x
+ );
+ x[ 2 ] = double( 0 );
+ int s = seed_rep->CreateHandle( x );
+ vtkHandleWidget* curr_hnd = widget->CreateNewHandle( );
+ seed_rep->SetSeedDisplayPosition( s, x );
+ curr_hnd->SetEnabled( 1 );
+
+ } // rof
+
+ } // fi
+
+ // Reactivate widget
+ widget->RestartInteraction( );
+ widget->EnabledOn( );
+ widget->Render( );
+
+ } // fi
+ }
+ else if( id == vtkCommand::PlacePointEvent && src_wdg != NULL )
+ {
+ this->m_Widgets.insert( src_wdg );
+ auto rep =
+ dynamic_cast< vtkSeedRepresentation* >(
+ src_wdg->GetRepresentation( )
+ );
+ if( rep != NULL )
+ {
+ unsigned long nSeeds = rep->GetNumberOfSeeds( );
+ if( nSeeds > 0 )
+ {
+ double pos[ 3 ];
+ rep->GetSeedWorldPosition( nSeeds - 1, pos );
+
+ this->m_Seeds->GetPoints( )->InsertNextPoint( pos );
+ this->m_Seeds->GetVerts( )->InsertNextCell( 1 );
+ this->m_Seeds->GetVerts( )->InsertCellPoint(
+ this->m_Seeds->GetPoints( )->GetNumberOfPoints( ) - 1
+ );
+ this->m_Seeds->Modified( );
+
+ } // fi
+
+ } // fi
} // fi
- if( this->m_Configured )
- this->Modified( );
- else
- this->_Error(
- "Could not create valid widget: are there any valid actors?"
- );
- this->GetOutput( "Output" )->SetVTK( points );
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsWidgets::SeedWidget::TCallback::
+SetSeeds( vtkPolyData* seeds )
+{
+ this->m_Seeds = seeds;
+}
+
+// -------------------------------------------------------------------------
+cpPluginsWidgets::SeedWidget::TCallback::
+TCallback( )
+ : vtkCommand( ),
+ m_Seeds( NULL )
+{
+}
+
+// -------------------------------------------------------------------------
+cpPluginsWidgets::SeedWidget::TCallback::
+~TCallback( )
+{
}
// eof - $RCSfile$