]> Creatis software - cpPlugins.git/blobdiff - plugins/cpPluginsWidgets/SeedWidget.cxx
...
[cpPlugins.git] / plugins / cpPluginsWidgets / SeedWidget.cxx
index 64069e4ce4602c971cde376a7600b3a23b5c411f..18bcdeb4b4fac7f2983268dd0bd47f20203ad48f 100644 (file)
@@ -1,12 +1,41 @@
 #include "SeedWidget.h"
 
+#include <vtkCommand.h>
 #include <vtkProperty.h>
 #include <vtkRenderWindowInteractor.h>
 #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( vtkRenderWindowInteractor* interactor, vtkImageActor* actor )
+Configure(
+  cpPluginsWidgets::SeedWidget* parent,
+  vtkRenderWindowInteractor* interactor, vtkImageActor* actor
+  )
 {
   this->Placer = vtkSmartPointer< vtkImageActorPointPlacer >::New( );
   this->Handle = vtkSmartPointer< vtkPointHandleRepresentation3D >::New( );
@@ -20,6 +49,12 @@ Configure( vtkRenderWindowInteractor* interactor, vtkImageActor* actor )
   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 );
 }
 
 // -------------------------------------------------------------------------
@@ -36,19 +71,12 @@ Off( )
   this->Widget->Off( );
 }
 
-// -------------------------------------------------------------------------
-itk::ModifiedTimeType cpPluginsWidgets::SeedWidget::
-GetMTime( ) const
-{
-  this->Modified( );
-  return( this->Superclass::GetMTime( ) );
-}
-
 // -------------------------------------------------------------------------
 cpPluginsWidgets::SeedWidget::
 SeedWidget( )
   : Superclass( ),
-    m_Configured( false )
+    m_Configured( false ),
+    m_InitialNumberOfSeeds( 0 )
 {
   this->_AddOutput< cpPlugins::DataObject >( "Output" );
 }
@@ -63,9 +91,11 @@ cpPluginsWidgets::SeedWidget::
 void cpPluginsWidgets::SeedWidget::
 _GenerateData( )
 {
+  auto points = this->_CreateVTK< vtkPoints >( );
   if( this->m_Configured )
   {
-    auto points = this->_CreateVTK< vtkPoints >( );
+    std::stringstream text;
+    points->Resize( this->m_InitialNumberOfSeeds );
     for(
       auto wIt = this->m_Widgets.begin( );
       wIt != this->m_Widgets.end( );
@@ -76,15 +106,46 @@ _GenerateData( )
       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 );
 
       } // rof
 
     } // rof
-    this->GetOutput( "Output" )->SetVTK( points );
+    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 )
     {
@@ -112,7 +173,7 @@ _GenerateData( )
             if( actor != NULL )
             {
               WidgetData d;
-              d.Configure( *iIt, actor );
+              d.Configure( this, *iIt, actor );
               d.On( );
               this->m_Widgets.push_back( d );
               this->m_Configured = true;
@@ -128,10 +189,13 @@ _GenerateData( )
     } // rof
 
   } // fi
-  if( !( this->m_Configured ) )
+  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$