]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/BaseObjects/ProcessObject.cxx
Cast image filter added. ROI filter modified.
[cpPlugins.git] / lib / cpPlugins / BaseObjects / ProcessObject.cxx
index ffe2cf5fe2a3ba827cd7e8959fc57599b86f4363..457835e42a9a725012fdbd8e39c40535b619059e 100644 (file)
@@ -1,8 +1,44 @@
 #include <cpPlugins/BaseObjects/ProcessObject.h>
-#include <cpPlugins/QT/ParametersDialog.h>
-#include <cpPlugins/Utility.h>
 #include <itkProcessObject.h>
 #include <itkExceptionObject.h>
+#include <cpPlugins/Utility.h>
+#include <cpPlugins/BaseObjects/Events.h>
+#include <cpPlugins/QT/ParametersDialog.h>
+
+// -------------------------------------------------------------------------
+void cpPlugins::BaseObjects::ProcessObject::
+PrintExecutionOn( )
+{
+  this->SetPrintExecution( true );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::BaseObjects::ProcessObject::
+PrintExecutionOff( )
+{
+  this->SetPrintExecution( false );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::BaseObjects::ProcessObject::
+GetPrintExecution( ) const
+{
+  return( this->m_PrintExecution );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::BaseObjects::ProcessObject::
+SetPrintExecution( bool v )
+{
+  this->m_PrintExecution = v;
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::BaseObjects::ProcessObject::
+SetPrintExecutionStream( std::ofstream* s )
+{
+  this->m_PrintExecutionStream = s;
+}
 
 // -------------------------------------------------------------------------
 cpPlugins::BaseObjects::Parameters*
@@ -54,6 +90,22 @@ GetOutputsNames( ) const
   return( names );
 }
 
+// -------------------------------------------------------------------------
+bool cpPlugins::BaseObjects::ProcessObject::
+HasInput( const std::string& n ) const
+{
+  auto i = this->m_Inputs.find( n );
+  return( i != this->m_Inputs.end( ) );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::BaseObjects::ProcessObject::
+HasOutput( const std::string& n ) const
+{
+  auto i = this->m_Outputs.find( n );
+  return( i != this->m_Outputs.end( ) );
+}
+
 // -------------------------------------------------------------------------
 unsigned int cpPlugins::BaseObjects::ProcessObject::
 GetNumberOfInputs( ) const
@@ -106,6 +158,23 @@ SetInput( const std::string& n, cpPlugins::BaseObjects::DataObject* o )
   this->AddInput( n, o );
 }
 
+// -------------------------------------------------------------------------
+void cpPlugins::BaseObjects::ProcessObject::
+DisconnectInput( const std::string& n, unsigned int id )
+{
+  auto inIt = this->m_Inputs.find( n );
+  if( inIt != this->m_Inputs.end( ) )
+  {
+    auto multi = dynamic_cast< MultipleInputsPort* >( inIt->second );
+    auto single = dynamic_cast< InputPort* >( inIt->second );
+    if( multi != NULL )
+      multi->Delete( id );
+    else if( single != NULL )
+      single->Add( NULL );
+
+  } // fi
+}
+
 // -------------------------------------------------------------------------
 void cpPlugins::BaseObjects::ProcessObject::
 DisconnectInputs( )
@@ -138,16 +207,11 @@ void cpPlugins::BaseObjects::ProcessObject::
 Modified( ) const
 {
   this->Superclass::Modified( );
-  this->m_LastExecutionSpan = -1;
-}
 
-// -------------------------------------------------------------------------
-itk::ModifiedTimeType cpPlugins::BaseObjects::ProcessObject::
-GetMTime( ) const
-{
-  auto params_time = this->m_Parameters.GetMTime( );
-  auto filter_time = this->Superclass::GetMTime( );
-  return( ( params_time > filter_time )? params_time: filter_time );
+  cpPlugins::BaseObjects::Events::Modified evt;
+  evt.Time = this->m_LastExecutionTime;
+  evt.Span = this->m_LastExecutionSpan;
+  this->InvokeEvent( evt );
 }
 
 // -------------------------------------------------------------------------
@@ -155,38 +219,49 @@ void cpPlugins::BaseObjects::ProcessObject::
 Update( )
 {
   // Force upstream updates
-  bool need_to_update = this->m_ExplicitExecution;
-  for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
+  bool update = ( this->m_LastExecutionTime < this->GetMTime( ) );
+  try
   {
-    bool iv = i->second->IsValid( );
-    bool ir = i->second->IsRequired( );
-    if( !iv && ir )
-      this->_Error(
-        std::string( "Required input \"" ) + i->first +
-        std::string( "\" is not valid." )
-        );
-    if( iv )
+    for( auto input : this->m_Inputs )
     {
-      unsigned int n = i->second->Size( );
-      for( unsigned int j = 0; j < n; ++j )
+      for( unsigned int i = 0; i < input.second->Size( ); ++i )
       {
-        Self* src = i->second->Get( j )->GetSource( );
-        if( src != NULL )
+        auto obj = input.second->Get( i );
+        if( obj != NULL )
         {
-          need_to_update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
-          src->Update( );
+          auto src = obj->GetSource( );
+          if( src != NULL )
+          {
+            src->Update( );
+            update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
+
+          } // fi
+        }
+        else
+        {
+          if( input.second->IsRequired( ) )
+            this->_Error(
+              std::string( "Required input \"" ) + input.first +
+              std::string( "\" in filter \"" ) +
+              this->m_Name +
+              std::string( "\" is not valid." )
+              );
 
         } // fi
 
       } // rof
 
-    } // fi
+    } // rof
+  }
+  catch( std::exception& err )
+  {
+    this->_Error( err.what( ) );
 
-  } // rof
+  } // yrt
 
-  // Current update
-  if( this->m_LastExecutionTime < this->GetMTime( ) || need_to_update )
+  if( update || this->m_ExplicitExecution )
   {
+    // Show a message, if needed...
     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
     {
       *( this->m_PrintExecutionStream )
@@ -198,21 +273,31 @@ Update( )
 
     } // fi
 
-    auto t_start = cpPlugins_CHRONO;
-    this->_GenerateData( );
-    this->Modified( );
-    auto t_end = cpPlugins_CHRONO;
-    this->m_LastExecutionSpan = long( t_end - t_start );
-    this->m_LastExecutionTime = this->GetMTime( );
-
-    if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
+    // Execute filter's algorithm and keep information about time
+    try
     {
-      *( this->m_PrintExecutionStream )
-        << "done in "
-        << double( this->m_LastExecutionSpan ) / double( 1000 )
-        << " s." << std::endl;
+      auto t_start = cpPlugins_CHRONO;
+      this->_GenerateData( );
+      auto t_end = cpPlugins_CHRONO;
+      this->Modified( );
+      this->m_LastExecutionSpan = long( t_end - t_start );
+      this->m_LastExecutionTime = this->GetMTime( );
+
+      // End the message, if needed...
+      if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
+      {
+        *( this->m_PrintExecutionStream )
+          << "done in "
+          << double( this->m_LastExecutionSpan ) / double( 1000 )
+          << " s." << std::endl;
+
+      } // fi
+    }
+    catch( std::exception& err )
+    {
+      this->_Error( err.what( ) );
 
-    } // fi
+    } // yrt
 
   } // fi
 }
@@ -239,8 +324,6 @@ CreateQDialog( )
 void cpPlugins::BaseObjects::ProcessObject::
 AddInteractor( vtkRenderWindowInteractor* i )
 {
-  if( this->m_Interactors.insert( i ).second )
-    this->Modified( );
 }
 
 // -------------------------------------------------------------------------
@@ -262,6 +345,7 @@ ProcessObject( )
     m_PrintExecution( false ),
     m_PrintExecutionStream( &( std::cout ) )
 {
+  this->m_Parameters.SetProcessObject( this );
 }
 
 // -------------------------------------------------------------------------
@@ -282,13 +366,9 @@ void cpPlugins::BaseObjects::ProcessObject::
 _Error( const std::string& error )
 {
   if( error != "" )
-  {
     itkExceptionMacro(
-      "Error: \"" << this->GetClassCategory( ) << "::" <<
-      this->GetClassName( ) << "\": " << error
+      "Error: \"" << this->m_Name << "\": " << error
       );
-
-  } // fi
 }
 
 // eof - $RCSfile$