]> Creatis software - cpPlugins.git/blobdiff - plugins/cpPluginsWidgets/SeedWidget.cxx
...
[cpPlugins.git] / plugins / cpPluginsWidgets / SeedWidget.cxx
index 9e7a408b33d0d559622991c825438cea27bb1fff..18bcdeb4b4fac7f2983268dd0bd47f20203ad48f 100644 (file)
@@ -1,28 +1,83 @@
 #include "SeedWidget.h"
 
-#include <cpPlugins/Image.h>
-#include <cpExtensions/DataStructures/ImageIndexesContainer.h>
-#include <cpExtensions/QT/SimpleMPRWidget.h>
-#include <cpExtensions/Interaction/ImageInteractorStyle.h>
+#include <vtkCommand.h>
+#include <vtkProperty.h>
 #include <vtkRenderWindowInteractor.h>
-#include <itkSimpleDataObjectDecorator.h>
-#include <itkSimpleDataObjectDecorator.hxx>
+#include <cpExtensions/QT/SimpleMPRWidget.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;
+  };
+
+} // ecapseman
+
+// -------------------------------------------------------------------------
+void cpPluginsWidgets::SeedWidget::WidgetData::
+Configure(
+  cpPluginsWidgets::SeedWidget* parent,
+  vtkRenderWindowInteractor* interactor, vtkImageActor* actor
+  )
+{
+  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 );
+}
 
 // -------------------------------------------------------------------------
-itk::ModifiedTimeType cpPluginsWidgets::SeedWidget::
-GetMTime( ) const
+void cpPluginsWidgets::SeedWidget::WidgetData::
+On( )
 {
-  // std::cout << "Seed GetMTime" << std::endl;
-  return( 0 /*this->Superclass::GetMTime( )*/ );
+  this->Widget->On( );
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsWidgets::SeedWidget::WidgetData::
+Off( )
+{
+  this->Widget->Off( );
 }
 
 // -------------------------------------------------------------------------
 cpPluginsWidgets::SeedWidget::
 SeedWidget( )
   : Superclass( ),
-    m_Configured( false )
+    m_Configured( false ),
+    m_InitialNumberOfSeeds( 0 )
 {
-  this->_AddInput( "ReferenceImage" );
   this->_AddOutput< cpPlugins::DataObject >( "Output" );
 }
 
@@ -36,94 +91,111 @@ cpPluginsWidgets::SeedWidget::
 void cpPluginsWidgets::SeedWidget::
 _GenerateData( )
 {
-  auto image = this->GetInputData< itk::DataObject >( "ReferenceImage" );
-  cpPlugins_Image_Demangle_Dim     ( _GD0, image, 2 );
-  else cpPlugins_Image_Demangle_Dim( _GD0, image, 3 );
-  else this->_Error( "No valid input image." );
-}
-
-// -------------------------------------------------------------------------
-template< class _TImage >
-void cpPluginsWidgets::SeedWidget::
-_GD0( _TImage* image )
-{
-
-  /*
-    typedef
-    cpExtensions::DataStructures::ImageIndexesContainer< _TImage::ImageDimension >
-    _TContainer;
-    typedef cpExtensions::Interaction::ImageInteractorStyle _S;
-
-    if( image != NULL )
+  auto points = this->_CreateVTK< vtkPoints >( );
+  if( this->m_Configured )
+  {
+    std::stringstream text;
+    points->Resize( this->m_InitialNumberOfSeeds );
+    for(
+      auto wIt = this->m_Widgets.begin( );
+      wIt != this->m_Widgets.end( );
+      ++wIt
+      )
     {
-    auto container = this->_CreateITK< _TContainer >( );
-    double aux_pnt[ 3 ];
-    unsigned int dim = ( _TImage::ImageDimension < 3 )? _TImage::ImageDimension: 3;
-    container->Get( ).clear( );
+      double pos[ 3 ];
+      for( unsigned int i = 0; i < wIt->Seed->GetNumberOfSeeds( ); ++i )
+      {
+        wIt->Seed->GetSeedWorldPosition( i, pos );
+        if( i > 0 )
+          text << "#";
+        text << pos[ 0 ] << " " << pos[ 1 ] << " " << pos[ 2 ];
+        points->InsertNextPoint( pos );
 
-    // MPR
-    if( this->m_MPRViewer != NULL )
-    {
-    for( unsigned int i = 0; i < 4; ++i )
-    {
-    _S* s =
-    dynamic_cast< _S* >(
-    this->m_MPRViewer->GetInteractor( i )->GetInteractorStyle( )
-    );
-    if( s != NULL )
-    {
-    if( this->m_Configured )
-    {
-    for( unsigned int i = 0; i < s->GetNumberOfSeeds( ); ++i )
-    {
-    s->GetSeedAsPoint( i, aux_pnt );
-    typename _TImage::PointType seed;
-    for( unsigned int d = 0; d < dim; ++d )
-    seed[ d ] = aux_pnt[ d ];
-    typename _TImage::IndexType idx;
-    if( image->TransformPhysicalPointToIndex( seed, idx ) )
-    container->Get( ).push_back( idx );
+      } // rof
 
     } // rof
-    }
-    else
-    s->SeedWidgetOn( );
-
-    } // fi
+    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 )
+      {
+        pos[ d ] = double( 0 );
+        if( d < dim )
+        {
+          std::istringstream value( coords[ d ] );
+          value >> pos[ d ];
+
+        } // fi
+
+      } // rof
+      points->InsertNextPoint( pos );
+      this->m_Configured = true;
 
     } // rof
 
+    std::vector< vtkRenderWindowInteractor* > ints;
+    if( this->m_MPRViewer != NULL )
+    {
+      ints.push_back( this->m_MPRViewer->GetInteractor( 0 ) );
+      ints.push_back( this->m_MPRViewer->GetInteractor( 1 ) );
+      ints.push_back( this->m_MPRViewer->GetInteractor( 2 ) );
+
     } // fi
+    if( this->m_SingleInteractor != NULL )
+      ints.push_back( this->m_SingleInteractor );
 
-    // Single interactor
-    _S* s = dynamic_cast< _S* >( this->m_SingleInteractor );
-    if( s != NULL )
-    {
-    if( this->m_Configured )
+    for( auto iIt = ints.begin( ); iIt !=  ints.end( ); ++iIt )
     {
-    for( unsigned int i = 0; i < s->GetNumberOfSeeds( ); ++i )
-    {
-    s->GetSeedAsPoint( i, aux_pnt );
-    typename _TImage::PointType seed;
-    for( unsigned int d = 0; d < dim; ++d )
-    seed[ d ] = aux_pnt[ d ];
-    typename _TImage::IndexType idx;
-    if( image->TransformPhysicalPointToIndex( seed, idx ) )
-    container->Get( ).push_back( idx );
+      auto ren = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
+      if( ren != NULL )
+      {
+        auto props = ren->GetViewProps( );
+        if( props != NULL )
+        {
+          props->InitTraversal( );
+          vtkProp* prop;
+          while( ( prop = props->GetNextProp( ) ) != NULL )
+          {
+            auto actor = dynamic_cast< vtkImageActor* >( prop );
+            if( actor != NULL )
+            {
+              WidgetData d;
+              d.Configure( this, *iIt, actor );
+              d.On( );
+              this->m_Widgets.push_back( d );
+              this->m_Configured = true;
+
+            } // fi
+
+          } // elihw
+
+        } // fi
+
+      } // fi
 
     } // rof
-    }
-    else
-    s->SeedWidgetOn( );
 
-    } // fi
-    this->m_Configured = true;
-    container->SetReferenceImage( image );
-    this->GetOutputData( "Output" )->SetITK( container );
-    }
-    else
-    this->_Error( "Input image dimension not supported." );
-  */
+  } // 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 );
 }
 
 // eof - $RCSfile$