]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Pipeline/ProcessObject.cxx
yet another refactoring
[cpPlugins.git] / lib / cpPlugins / Pipeline / ProcessObject.cxx
diff --git a/lib/cpPlugins/Pipeline/ProcessObject.cxx b/lib/cpPlugins/Pipeline/ProcessObject.cxx
new file mode 100644 (file)
index 0000000..c8edba2
--- /dev/null
@@ -0,0 +1,377 @@
+#include <cpPlugins/Pipeline/ProcessObject.h>
+#include <itkProcessObject.h>
+#include <itkExceptionObject.h>
+#include <cpPlugins/Pipeline/Events.h>
+#include <cpPlugins/OS/Chrono.h>
+
+#ifdef cpPlugins_QT4
+#  include <cpPlugins/QT/ParametersDialog.h>
+#endif // cpPlugins_QT4
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+PrintExecutionOn( )
+{
+  this->SetPrintExecution( true );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+PrintExecutionOff( )
+{
+  this->SetPrintExecution( false );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Pipeline::ProcessObject::
+GetPrintExecution( ) const
+{
+  return( this->m_PrintExecution );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Pipeline::ProcessObject::
+SetPrintExecution( bool v )
+{
+  this->m_PrintExecution = v;
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+SetPrintExecutionStream( std::ofstream* s )
+{
+  this->m_PrintExecutionStream = s;
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Pipeline::Parameters*
+cpPlugins::Pipeline::ProcessObject::
+GetParameters( )
+{
+  return( &( this->m_Parameters ) );
+}
+
+// -------------------------------------------------------------------------
+const cpPlugins::Pipeline::Parameters*
+cpPlugins::Pipeline::ProcessObject::
+GetParameters( ) const
+{
+  return( &( this->m_Parameters ) );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+SetITK( itk::LightObject* o )
+{
+  // Polymorphism: do nothing -> this is a filter!!!
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+SetVTK( vtkObjectBase* o )
+{
+  // Polymorphism: do nothing -> this is a filter!!!
+}
+
+// -------------------------------------------------------------------------
+std::set< std::string > cpPlugins::Pipeline::ProcessObject::
+GetInputsNames( ) const
+{
+  std::set< std::string > names;
+  for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
+    names.insert( i->first );
+  return( names );
+}
+
+// -------------------------------------------------------------------------
+std::set< std::string > cpPlugins::Pipeline::ProcessObject::
+GetOutputsNames( ) const
+{
+  std::set< std::string > names;
+  for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
+    names.insert( i->first );
+  return( names );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Pipeline::ProcessObject::
+HasInput( const std::string& n ) const
+{
+  auto i = this->m_Inputs.find( n );
+  return( i != this->m_Inputs.end( ) );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Pipeline::ProcessObject::
+HasOutput( const std::string& n ) const
+{
+  auto i = this->m_Outputs.find( n );
+  return( i != this->m_Outputs.end( ) );
+}
+
+// -------------------------------------------------------------------------
+unsigned int cpPlugins::Pipeline::ProcessObject::
+GetNumberOfInputs( ) const
+{
+  return( this->m_Inputs.size( ) );
+}
+
+// -------------------------------------------------------------------------
+unsigned int cpPlugins::Pipeline::ProcessObject::
+GetNumberOfOutputs( ) const
+{
+  return( this->m_Outputs.size( ) );
+}
+
+// -------------------------------------------------------------------------
+unsigned int cpPlugins::Pipeline::ProcessObject::
+GetInputSize( const std::string& n ) const
+{
+  auto it = this->m_Inputs.find( n );
+  if( it != this->m_Inputs.end( ) )
+    return( it->second->Size( ) );
+  else
+    return( 0 );
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Pipeline::ProcessObject::
+IsInputMultiple( const std::string& n ) const
+{
+  auto i = this->m_Inputs.find( n );
+  if( i != this->m_Inputs.end( ) )
+    return( dynamic_cast< MultipleInputsPort* >( i->second ) != NULL );
+  else
+    return( false );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+AddInput( const std::string& n, cpPlugins::Pipeline::DataObject* o )
+{
+  auto it = this->m_Inputs.find( n );
+  if( it != this->m_Inputs.end( ) )
+    it->second->Add( o );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+SetInput( const std::string& n, cpPlugins::Pipeline::DataObject* o )
+{
+  this->AddInput( n, o );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::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::Pipeline::ProcessObject::
+DisconnectInputs( )
+{
+  for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
+    i->second->Clear( );
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+DisconnectOutputs( )
+{
+  for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
+    if( i->second->IsValid( ) )
+      i->second->Get( )->DisconnectFromPipeline( );
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+Disconnect( )
+{
+  this->DisconnectInputs( );
+  this->DisconnectOutputs( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+Modified( ) const
+{
+  this->Superclass::Modified( );
+
+  cpPlugins::Pipeline::Events::Modified evt;
+  evt.Time = this->m_LastExecutionTime;
+  evt.Span = this->m_LastExecutionSpan;
+  this->InvokeEvent( evt );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+Update( )
+{
+  // Force upstream updates
+  bool update = ( this->m_LastExecutionTime < this->GetMTime( ) );
+  try
+  {
+    for( auto input : this->m_Inputs )
+    {
+      for( unsigned int i = 0; i < input.second->Size( ); ++i )
+      {
+        auto obj = input.second->Get( i );
+        if( obj != NULL )
+        {
+          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
+
+    } // rof
+  }
+  catch( std::exception& err )
+  {
+    this->_Error( err.what( ) );
+
+  } // yrt
+
+  if( update || this->m_ExplicitExecution )
+  {
+    // Show a message, if needed...
+    if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
+    {
+      *( this->m_PrintExecutionStream )
+        << "cpPlugins: Updating \""
+        << this->GetName( ) << " ("
+        << this->GetClassCategory( ) << ":" << this->GetClassName( )
+        << ")\"... ";
+      this->m_PrintExecutionStream->flush( );
+
+    } // fi
+
+    // Execute filter's algorithm and keep information about time
+    try
+    {
+      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( ) );
+
+    } // yrt
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+QDialog* cpPlugins::Pipeline::ProcessObject::
+CreateQDialog( )
+{
+#ifdef cpPlugins_QT4
+  cpPlugins::QT::ParametersDialog* dlg = NULL;
+  if( QApplication::instance( ) != NULL )
+  {
+    dlg = new cpPlugins::QT::ParametersDialog( );
+    dlg->setProcessObject( this );
+
+  } // fi
+  return( dlg );
+#else // cpPlugins_QT4
+  return( NULL );
+#endif // cpPlugins_QT4
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+AddInteractor( vtkRenderWindowInteractor* i )
+{
+}
+
+// -------------------------------------------------------------------------
+bool cpPlugins::Pipeline::ProcessObject::
+IsInteractive( )
+{
+  return( false );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Pipeline::ProcessObject::
+ProcessObject( )
+  : Superclass( ),
+    m_Name( "" ),
+    m_PluginName( "" ),
+    m_ExplicitExecution( false ),
+    m_LastExecutionTime( 0 ),
+    m_LastExecutionSpan( -1 ),
+    m_PrintExecution( false ),
+    m_PrintExecutionStream( &( std::cout ) )
+{
+  this->m_Parameters.SetProcessObject( this );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Pipeline::ProcessObject::
+~ProcessObject( )
+{
+  for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
+    delete i->second;
+  for( auto o = this->m_Outputs.begin( ); o != this->m_Outputs.end( ); ++o )
+    delete o->second;
+
+  this->m_Inputs.clear( );
+  this->m_Outputs.clear( );
+}
+
+// -------------------------------------------------------------------------
+void cpPlugins::Pipeline::ProcessObject::
+_Error( const std::string& error )
+{
+  if( error != "" )
+    itkExceptionMacro(
+      "Error: \"" << this->m_Name << "\": " << error
+      );
+}
+
+// eof - $RCSfile$