1 #include <cpPlugins/Pipeline/ProcessObject.h>
2 #include <itkProcessObject.h>
3 #include <itkExceptionObject.h>
4 #include <cpPlugins/Pipeline/Events.h>
5 #include <cpPlugins/OS/Chrono.h>
8 # include <cpPlugins/QT/ParametersDialog.h>
9 #endif // cpPlugins_QT4
11 // -------------------------------------------------------------------------
12 void cpPlugins::Pipeline::ProcessObject::
15 this->SetPrintExecution( true );
18 // -------------------------------------------------------------------------
19 void cpPlugins::Pipeline::ProcessObject::
22 this->SetPrintExecution( false );
25 // -------------------------------------------------------------------------
26 bool cpPlugins::Pipeline::ProcessObject::
27 GetPrintExecution( ) const
29 return( this->m_PrintExecution );
32 // -------------------------------------------------------------------------
33 bool cpPlugins::Pipeline::ProcessObject::
34 SetPrintExecution( bool v )
36 this->m_PrintExecution = v;
39 // -------------------------------------------------------------------------
40 void cpPlugins::Pipeline::ProcessObject::
41 SetPrintExecutionStream( std::ofstream* s )
43 this->m_PrintExecutionStream = s;
46 // -------------------------------------------------------------------------
47 cpPlugins::Pipeline::Parameters*
48 cpPlugins::Pipeline::ProcessObject::
51 return( &( this->m_Parameters ) );
54 // -------------------------------------------------------------------------
55 const cpPlugins::Pipeline::Parameters*
56 cpPlugins::Pipeline::ProcessObject::
57 GetParameters( ) const
59 return( &( this->m_Parameters ) );
62 // -------------------------------------------------------------------------
63 void cpPlugins::Pipeline::ProcessObject::
64 SetITK( itk::LightObject* o )
66 // Polymorphism: do nothing -> this is a filter!!!
69 // -------------------------------------------------------------------------
70 void cpPlugins::Pipeline::ProcessObject::
71 SetVTK( vtkObjectBase* o )
73 // Polymorphism: do nothing -> this is a filter!!!
76 // -------------------------------------------------------------------------
77 std::set< std::string > cpPlugins::Pipeline::ProcessObject::
78 GetInputsNames( ) const
80 std::set< std::string > names;
81 for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
82 names.insert( i->first );
86 // -------------------------------------------------------------------------
87 std::set< std::string > cpPlugins::Pipeline::ProcessObject::
88 GetOutputsNames( ) const
90 std::set< std::string > names;
91 for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
92 names.insert( i->first );
96 // -------------------------------------------------------------------------
97 bool cpPlugins::Pipeline::ProcessObject::
98 HasInput( const std::string& n ) const
100 auto i = this->m_Inputs.find( n );
101 return( i != this->m_Inputs.end( ) );
104 // -------------------------------------------------------------------------
105 bool cpPlugins::Pipeline::ProcessObject::
106 HasOutput( const std::string& n ) const
108 auto i = this->m_Outputs.find( n );
109 return( i != this->m_Outputs.end( ) );
112 // -------------------------------------------------------------------------
113 unsigned int cpPlugins::Pipeline::ProcessObject::
114 GetNumberOfInputs( ) const
116 return( this->m_Inputs.size( ) );
119 // -------------------------------------------------------------------------
120 unsigned int cpPlugins::Pipeline::ProcessObject::
121 GetNumberOfOutputs( ) const
123 return( this->m_Outputs.size( ) );
126 // -------------------------------------------------------------------------
127 unsigned int cpPlugins::Pipeline::ProcessObject::
128 GetInputSize( const std::string& n ) const
130 auto it = this->m_Inputs.find( n );
131 if( it != this->m_Inputs.end( ) )
132 return( it->second->Size( ) );
137 // -------------------------------------------------------------------------
138 bool cpPlugins::Pipeline::ProcessObject::
139 IsInputMultiple( const std::string& n ) const
141 auto i = this->m_Inputs.find( n );
142 if( i != this->m_Inputs.end( ) )
143 return( dynamic_cast< MultipleInputsPort* >( i->second ) != NULL );
148 // -------------------------------------------------------------------------
149 void cpPlugins::Pipeline::ProcessObject::
150 AddInput( const std::string& n, cpPlugins::Pipeline::DataObject* o )
152 auto it = this->m_Inputs.find( n );
153 if( it != this->m_Inputs.end( ) )
154 it->second->Add( o );
157 // -------------------------------------------------------------------------
158 void cpPlugins::Pipeline::ProcessObject::
159 SetInput( const std::string& n, cpPlugins::Pipeline::DataObject* o )
161 this->AddInput( n, o );
164 // -------------------------------------------------------------------------
165 void cpPlugins::Pipeline::ProcessObject::
166 DisconnectInput( const std::string& n, unsigned int id )
168 auto inIt = this->m_Inputs.find( n );
169 if( inIt != this->m_Inputs.end( ) )
171 auto multi = dynamic_cast< MultipleInputsPort* >( inIt->second );
172 auto single = dynamic_cast< InputPort* >( inIt->second );
175 else if( single != NULL )
181 // -------------------------------------------------------------------------
182 void cpPlugins::Pipeline::ProcessObject::
185 for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
190 // -------------------------------------------------------------------------
191 void cpPlugins::Pipeline::ProcessObject::
194 for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
195 if( i->second->IsValid( ) )
196 i->second->Get( )->DisconnectFromPipeline( );
200 // -------------------------------------------------------------------------
201 void cpPlugins::Pipeline::ProcessObject::
204 this->DisconnectInputs( );
205 this->DisconnectOutputs( );
208 // -------------------------------------------------------------------------
209 void cpPlugins::Pipeline::ProcessObject::
212 this->Superclass::Modified( );
214 cpPlugins::Pipeline::Events::Modified evt;
215 evt.Time = this->m_LastExecutionTime;
216 evt.Span = this->m_LastExecutionSpan;
217 this->InvokeEvent( evt );
220 // -------------------------------------------------------------------------
221 void cpPlugins::Pipeline::ProcessObject::
224 // Force upstream updates
225 bool update = ( this->m_LastExecutionTime < this->GetMTime( ) );
228 for( auto input : this->m_Inputs )
230 for( unsigned int i = 0; i < input.second->Size( ); ++i )
232 auto obj = input.second->Get( i );
235 auto src = obj->GetSource( );
239 update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
245 if( input.second->IsRequired( ) )
247 std::string( "Required input \"" ) + input.first +
248 std::string( "\" in filter \"" ) +
250 std::string( "\" is not valid." )
259 catch( std::exception& err )
261 this->_Error( err.what( ) );
265 if( update || this->m_ExplicitExecution )
267 // Show a message, if needed...
268 if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
270 *( this->m_PrintExecutionStream )
271 << "cpPlugins: Updating \""
272 << this->GetName( ) << " ("
273 << this->GetClassCategory( ) << ":" << this->GetClassName( )
275 this->m_PrintExecutionStream->flush( );
279 // Execute filter's algorithm and keep information about time
282 auto t_start = cpPlugins_CHRONO;
283 this->_GenerateData( );
284 auto t_end = cpPlugins_CHRONO;
286 this->m_LastExecutionSpan = long( t_end - t_start );
287 this->m_LastExecutionTime = this->GetMTime( );
289 // End the message, if needed...
290 if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
292 *( this->m_PrintExecutionStream )
294 << double( this->m_LastExecutionSpan ) / double( 1000 )
295 << " s." << std::endl;
299 catch( std::exception& err )
301 this->_Error( err.what( ) );
308 // -------------------------------------------------------------------------
309 QDialog* cpPlugins::Pipeline::ProcessObject::
313 cpPlugins::QT::ParametersDialog* dlg = NULL;
314 if( QApplication::instance( ) != NULL )
316 dlg = new cpPlugins::QT::ParametersDialog( );
317 dlg->setProcessObject( this );
321 #else // cpPlugins_QT4
323 #endif // cpPlugins_QT4
326 // -------------------------------------------------------------------------
327 void cpPlugins::Pipeline::ProcessObject::
328 AddInteractor( vtkRenderWindowInteractor* i )
332 // -------------------------------------------------------------------------
333 bool cpPlugins::Pipeline::ProcessObject::
339 // -------------------------------------------------------------------------
340 cpPlugins::Pipeline::ProcessObject::
345 m_ExplicitExecution( false ),
346 m_LastExecutionTime( 0 ),
347 m_LastExecutionSpan( -1 ),
348 m_PrintExecution( false ),
349 m_PrintExecutionStream( &( std::cout ) )
351 this->m_Parameters.SetProcessObject( this );
354 // -------------------------------------------------------------------------
355 cpPlugins::Pipeline::ProcessObject::
358 for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
360 for( auto o = this->m_Outputs.begin( ); o != this->m_Outputs.end( ); ++o )
363 this->m_Inputs.clear( );
364 this->m_Outputs.clear( );
367 // -------------------------------------------------------------------------
368 void cpPlugins::Pipeline::ProcessObject::
369 _Error( const std::string& error )
373 "Error: \"" << this->m_Name << "\": " << error