+#include <plugins/Widgets/SplineWidget.h>
+#include <cpPlugins/DataObjects/Image.h>
+#include <cpPlugins/DataObjects/Mesh.h>
+#include <cpExtensions/Visualization/WindowLevelImageActor.h>
+
+#include <vtkRenderer.h>
+#include <vtkSplineWidget.h>
+
+// -------------------------------------------------------------------------
+cpPluginsWidgets::SplineWidget::
+SplineWidget( )
+ : Superclass( ),
+ m_Configured( false )
+{
+ typedef cpPlugins::BaseObjects::DataObject _TData;
+ typedef cpPlugins::DataObjects::Mesh _TMesh;
+
+ this->_ConfigureInput< _TData >( "Input", false, false );
+ this->_ConfigureOutput< _TMesh >( "Output" );
+ this->m_Contour = vtkSmartPointer< vtkPolyData >::New( );
+ this->m_Contour->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
+ this->m_Contour->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
+ this->m_Contour->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
+ this->m_Contour->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
+ this->m_Contour->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
+ this->GetOutput( "Output" )->SetVTK( this->m_Contour );
+}
+
+// -------------------------------------------------------------------------
+cpPluginsWidgets::SplineWidget::
+~SplineWidget( )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsWidgets::SplineWidget::
+_GenerateData( )
+{
+ typedef cpExtensions::Visualization::WindowLevelImageActor _TActor;
+
+ auto image = this->GetInputData< vtkImageData >( "Input" );
+ if( image == NULL )
+ this->_Error( "Invalid input image." );
+ if( this->m_Interactors.size( ) == 0 )
+ this->_Error( "Give at least one interactor." );
+
+ auto wdg = this->_CreateVTK< vtkSplineWidget >( );
+ if( this->m_Configured )
+ {
+ wdg->GetPolyData( this->m_Contour.GetPointer( ) );
+ wdg->Off( );
+ }
+ else
+ {
+ auto iIt = this->m_Interactors.begin( );
+ vtkRenderWindowInteractor* iren = NULL;
+ vtkRenderer* ren = NULL;
+ _TActor* actor = NULL;
+ for( ; iIt != this->m_Interactors.end( ); ++iIt )
+ {
+ auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
+ if( r != NULL )
+ {
+ auto props = r->GetViewProps( );
+ if( props != NULL )
+ {
+ props->InitTraversal( );
+ while( vtkProp* prop = props->GetNextProp( ) )
+ {
+ auto a = dynamic_cast< _TActor* >( prop );
+ if( a != NULL )
+ if( a->GetImage( ) == image )
+ {
+ iren = *iIt;
+ actor = a;
+ ren = r;
+
+ } // fi
+
+ } // elihw
+
+ } // fi
+
+ } // fi
+
+ } // rof
+ if( actor == NULL || ren == NULL || iren == NULL )
+ this->_Error( "Invalid actor and/or renderer." );
+
+ // Widget configuration
+ wdg->SetCurrentRenderer( ren );
+ wdg->SetDefaultRenderer( ren );
+ wdg->SetInputData( image );
+ wdg->SetProp3D( actor );
+ wdg->SetInteractor( iren );
+ double bnds[ 6 ];
+ image->GetBounds( bnds );
+ wdg->PlaceWidget(
+ bnds[ 0 ], bnds[ 1 ],
+ bnds[ 2 ], bnds[ 3 ],
+ bnds[ 4 ], bnds[ 5 ]
+ );
+ wdg->ProjectToPlaneOn( );
+ wdg->SetProjectionNormalToZAxes( );
+ wdg->SetProjectionPosition(
+ (
+ actor->GetBounds( )[ 4 ] +
+ actor->GetBounds( )[ 5 ]
+ ) / double( 2 )
+ );
+ wdg->SetHandleSize( 0.005 );
+ wdg->SetNumberOfHandles( 3 );
+ wdg->On( );
+ this->m_Configured = true;
+
+ } // fi
+
+ /* TODO
+
+ if( this->m_Configured )
+ {
+ if( points->GetNumberOfPoints( ) == 0 )
+ {
+ std::stringstream text;
+ bool start = true;
+ for( auto w = this->m_Widgets.begin( ); w != this->m_Widgets.end( ); ++w )
+ {
+ for( auto r = ( *w )->Widgets.begin( ); r != ( *w )->Widgets.end( ); ++r )
+ {
+ auto rep =
+ dynamic_cast< vtkSeedRepresentation* >(
+ ( *r )->GetRepresentation( )
+ );
+ if( rep != NULL )
+ {
+ double pos[ 3 ];
+ for( unsigned int i = 0; i < rep->GetNumberOfSeeds( ); ++i )
+ {
+ rep->GetSeedWorldPosition( i, pos );
+ if( !start )
+ text << "#";
+ start = false;
+ text << pos[ 0 ] << " " << pos[ 1 ] << " " << pos[ 2 ];
+ points->InsertNextPoint( pos );
+
+ } // rof
+
+ } // rof
+ ( *r )->EnabledOff( );
+
+ } // rof
+
+ } // rof
+ this->m_Parameters.SetString( "Text", text.str( ) );
+
+ } // fi
+ }
+ else
+ {
+ auto init_seeds = this->m_Parameters.GetString( "Text" );
+ std::vector< std::string > tokens;
+ cpExtensions::Tokenize( tokens, init_seeds, "#" );
+ for( auto tIt = tokens.begin( ); tIt != tokens.end( ); ++tIt )
+ {
+ std::vector< std::string > coords;
+ cpExtensions::Tokenize( coords, *tIt, " \t" );
+ int dim = ( coords.size( ) < 3 )? coords.size( ): 3;
+ double pos[ 3 ];
+ for( unsigned int d = 0; d < 3; ++d )
+ {
+ pos[ d ] = double( 0 );
+ if( d < dim )
+ {
+ std::istringstream value( coords[ d ] );
+ value >> pos[ d ];
+
+ } // fi
+
+ } // rof
+ verts->InsertNextCell( 1 );
+ verts->InsertCellPoint( points->GetNumberOfPoints( ) );
+ points->InsertNextPoint( pos );
+
+ } // rof
+ this->_Configure( );
+ this->Modified( );
+ this->m_Configured = true;
+
+ } // fi
+ this->GetOutput( "Output" )->SetVTK( pdata );
+ */
+}
+
+// -------------------------------------------------------------------------
+/* TODO
+void cpPluginsWidgets::SplineWidget::
+_Configure( )
+{
+ typedef cpPlugins::DataObjects::Image _TImage;
+
+ auto image = this->GetInput< _TImage >( "Input" );
+ if( image != NULL )
+ {
+ // Update actors
+ auto vtk_image = image->GetVTK< vtkImageData >( );
+ auto iIt = this->m_Interactors.begin( );
+ for( ; iIt != this->m_Interactors.end( ); ++iIt )
+ {
+ auto ren = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
+ if( ren != NULL )
+ {
+ auto props = ren->GetViewProps( );
+ if( props != NULL )
+ {
+ props->InitTraversal( );
+ while( vtkProp* prop = props->GetNextProp( ) )
+ {
+ auto actor = dynamic_cast< TImageActor* >( prop );
+ if( actor != NULL )
+ if( actor->GetImage( ) == vtk_image )
+ this->m_Props[ actor ] = *iIt;
+
+ } // elihw
+
+ } // fi
+
+ } // fi
+
+ } // rof
+
+ // Process image
+ if( this->m_Props.size( ) > 0 )
+ {
+ cpPlugins_Demangle_ImageVisualDims( image->GetITK( ), _GD0_Image );
+ else this->_Error( "Invalid input image." );
+ }
+ else
+ this->_Error( "Could not create a valid widget: no actors." );
+ }
+ else
+ this->_Error( "Could not create a valid widget: no input." );
+}
+
+// -------------------------------------------------------------------------
+template< class _TImage >
+void cpPluginsWidgets::SplineWidget::
+_GD0_Image( _TImage* image )
+{
+ for( auto p = this->m_Props.begin( ); p != this->m_Props.end( ); ++p )
+ {
+ TWidgetData* d =
+ new TWidgetData(
+ this, dynamic_cast< TImageActor* >( p->first ), p->second
+ );
+ this->m_Widgets.push_back( d );
+
+ } // rof
+}
+
+// -------------------------------------------------------------------------
+cpPluginsWidgets::SplineWidget::TWidgetData::
+TWidgetData(
+ SplineWidget* seedWidget,
+ TImageActor* actor,
+ vtkRenderWindowInteractor* iren
+ )
+{
+ auto cb = vtkSmartPointer< SplineWidgetCallback >::New( );
+ cb->Widget = seedWidget;
+ cb->Data = this;
+ this->Command = cb;
+ actor->AddObserver( vtkCommand::InteractionEvent, cb );
+
+ auto image = actor->GetImage( );
+ int ori = actor->GetOrientation( );
+ int ext[ 6 ];
+ image->GetExtent( ext );
+ for( int i = ext[ ori << 1 ]; i <= ext[ ( ori << 1 ) + 1 ]; ++i )
+ {
+ auto placer = vtkSmartPointer< _TPlacer >::New( );
+ auto handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
+ auto rep = vtkSmartPointer< vtkSeedRepresentation >::New( );
+ auto wdg = vtkSmartPointer< _TWidget >::New( );
+
+ placer->SetImageSlice( actor );
+ handle->GetProperty( )->SetColor( 1, 0, 0 );
+ handle->SetPointPlacer( placer );
+ rep->SetHandleRepresentation( handle );
+ wdg->SetRepresentation( rep );
+ wdg->SetInteractor( iren );
+ wdg->AddObserver( vtkCommand::PlacePointEvent, cb );
+ wdg->AddObserver( vtkCommand::CursorChangedEvent, cb );
+ wdg->EnabledOff( );
+
+ this->Widgets.push_back( wdg );
+ this->Placers.push_back( placer );
+ this->Handles.push_back( handle );
+ this->Representations.push_back( rep );
+
+ } // rof
+
+ this->ActualWidgetId = actor->GetSliceNumber( );
+ this->Widgets[ this->ActualWidgetId ]->EnabledOn( );
+}
+
+// -------------------------------------------------------------------------
+cpPluginsWidgets::SplineWidget::TWidgetData::
+~TWidgetData( )
+{
+}
+*/
+
+// eof - $RCSfile$