1 #include <cpPlugins/BaseObjects/ProcessObject.h>
2 #include <itkProcessObject.h>
3 #include <itkExceptionObject.h>
4 #include <cpPlugins/Utility.h>
5 #include <cpPlugins/BaseObjects/Events.h>
6 #include <cpPlugins/QT/ParametersDialog.h>
8 // -------------------------------------------------------------------------
9 cpPlugins::BaseObjects::Parameters*
10 cpPlugins::BaseObjects::ProcessObject::
13 return( &( this->m_Parameters ) );
16 // -------------------------------------------------------------------------
17 const cpPlugins::BaseObjects::Parameters*
18 cpPlugins::BaseObjects::ProcessObject::
19 GetParameters( ) const
21 return( &( this->m_Parameters ) );
24 // -------------------------------------------------------------------------
25 void cpPlugins::BaseObjects::ProcessObject::
26 SetITK( itk::LightObject* o )
28 // Polymorphism: do nothing -> this is a filter!!!
31 // -------------------------------------------------------------------------
32 void cpPlugins::BaseObjects::ProcessObject::
33 SetVTK( vtkObjectBase* o )
35 // Polymorphism: do nothing -> this is a filter!!!
38 // -------------------------------------------------------------------------
39 std::set< std::string > cpPlugins::BaseObjects::ProcessObject::
40 GetInputsNames( ) const
42 std::set< std::string > names;
43 for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
44 names.insert( i->first );
48 // -------------------------------------------------------------------------
49 std::set< std::string > cpPlugins::BaseObjects::ProcessObject::
50 GetOutputsNames( ) const
52 std::set< std::string > names;
53 for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
54 names.insert( i->first );
58 // -------------------------------------------------------------------------
59 bool cpPlugins::BaseObjects::ProcessObject::
60 HasInput( const std::string& n ) const
62 auto i = this->m_Inputs.find( n );
63 return( i != this->m_Inputs.end( ) );
66 // -------------------------------------------------------------------------
67 bool cpPlugins::BaseObjects::ProcessObject::
68 HasOutput( const std::string& n ) const
70 auto i = this->m_Outputs.find( n );
71 return( i != this->m_Outputs.end( ) );
74 // -------------------------------------------------------------------------
75 unsigned int cpPlugins::BaseObjects::ProcessObject::
76 GetNumberOfInputs( ) const
78 return( this->m_Inputs.size( ) );
81 // -------------------------------------------------------------------------
82 unsigned int cpPlugins::BaseObjects::ProcessObject::
83 GetNumberOfOutputs( ) const
85 return( this->m_Outputs.size( ) );
88 // -------------------------------------------------------------------------
89 unsigned int cpPlugins::BaseObjects::ProcessObject::
90 GetInputSize( const std::string& n ) const
92 auto it = this->m_Inputs.find( n );
93 if( it != this->m_Inputs.end( ) )
94 return( it->second->Size( ) );
99 // -------------------------------------------------------------------------
100 bool cpPlugins::BaseObjects::ProcessObject::
101 IsInputMultiple( const std::string& n ) const
103 auto i = this->m_Inputs.find( n );
104 if( i != this->m_Inputs.end( ) )
105 return( dynamic_cast< MultipleInputsPort* >( i->second ) != NULL );
110 // -------------------------------------------------------------------------
111 void cpPlugins::BaseObjects::ProcessObject::
112 AddInput( const std::string& n, cpPlugins::BaseObjects::DataObject* o )
114 auto it = this->m_Inputs.find( n );
115 if( it != this->m_Inputs.end( ) )
116 it->second->Add( o );
119 // -------------------------------------------------------------------------
120 void cpPlugins::BaseObjects::ProcessObject::
121 SetInput( const std::string& n, cpPlugins::BaseObjects::DataObject* o )
123 this->AddInput( n, o );
126 // -------------------------------------------------------------------------
127 void cpPlugins::BaseObjects::ProcessObject::
128 DisconnectInput( const std::string& n, unsigned int id )
130 auto inIt = this->m_Inputs.find( n );
131 if( inIt != this->m_Inputs.end( ) )
133 auto multi = dynamic_cast< MultipleInputsPort* >( inIt->second );
134 auto single = dynamic_cast< InputPort* >( inIt->second );
137 else if( single != NULL )
143 // -------------------------------------------------------------------------
144 void cpPlugins::BaseObjects::ProcessObject::
147 for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
152 // -------------------------------------------------------------------------
153 void cpPlugins::BaseObjects::ProcessObject::
156 for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
157 if( i->second->IsValid( ) )
158 i->second->Get( )->DisconnectFromPipeline( );
162 // -------------------------------------------------------------------------
163 void cpPlugins::BaseObjects::ProcessObject::
166 this->DisconnectInputs( );
167 this->DisconnectOutputs( );
170 // -------------------------------------------------------------------------
171 void cpPlugins::BaseObjects::ProcessObject::
174 this->Superclass::Modified( );
176 cpPlugins::BaseObjects::Events::Modified evt;
177 evt.Time = this->m_LastExecutionTime;
178 evt.Span = this->m_LastExecutionSpan;
179 this->InvokeEvent( evt );
182 // -------------------------------------------------------------------------
183 void cpPlugins::BaseObjects::ProcessObject::
186 // Force upstream updates
187 bool need_to_update = this->m_ExplicitExecution;
188 for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
190 bool iv = i->second->IsValid( );
191 bool ir = i->second->IsRequired( );
194 std::string( "Required input \"" ) + i->first +
195 std::string( "\" is not valid." )
199 unsigned int n = i->second->Size( );
200 for( unsigned int j = 0; j < n; ++j )
202 Self* src = i->second->Get( j )->GetSource( );
205 need_to_update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
217 if( this->m_LastExecutionTime < this->GetMTime( ) || need_to_update )
219 if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
221 *( this->m_PrintExecutionStream )
222 << "cpPlugins: Updating \""
223 << this->GetName( ) << " ("
224 << this->GetClassCategory( ) << ":" << this->GetClassName( )
226 this->m_PrintExecutionStream->flush( );
230 auto t_start = cpPlugins_CHRONO;
231 this->_GenerateData( );
232 auto t_end = cpPlugins_CHRONO;
233 this->m_LastExecutionSpan = long( t_end - t_start );
235 this->m_LastExecutionTime = this->GetMTime( );
237 if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
239 *( this->m_PrintExecutionStream )
241 << double( this->m_LastExecutionSpan ) / double( 1000 )
242 << " s." << std::endl;
249 // -------------------------------------------------------------------------
250 QDialog* cpPlugins::BaseObjects::ProcessObject::
254 cpPlugins::QT::ParametersDialog* dlg = NULL;
255 if( QApplication::instance( ) != NULL )
257 dlg = new cpPlugins::QT::ParametersDialog( );
258 dlg->setProcessObject( this );
262 #else // cpPlugins_QT4
264 #endif // cpPlugins_QT4
267 // -------------------------------------------------------------------------
268 void cpPlugins::BaseObjects::ProcessObject::
269 AddInteractor( vtkRenderWindowInteractor* i )
273 // -------------------------------------------------------------------------
274 bool cpPlugins::BaseObjects::ProcessObject::
280 // -------------------------------------------------------------------------
281 cpPlugins::BaseObjects::ProcessObject::
286 m_ExplicitExecution( false ),
287 m_LastExecutionTime( 0 ),
288 m_LastExecutionSpan( -1 ),
289 m_PrintExecution( false ),
290 m_PrintExecutionStream( &( std::cout ) )
292 this->m_Parameters.SetProcessObject( this );
295 // -------------------------------------------------------------------------
296 cpPlugins::BaseObjects::ProcessObject::
299 for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
301 for( auto o = this->m_Outputs.begin( ); o != this->m_Outputs.end( ); ++o )
304 this->m_Inputs.clear( );
305 this->m_Outputs.clear( );
308 // -------------------------------------------------------------------------
309 void cpPlugins::BaseObjects::ProcessObject::
310 _Error( const std::string& error )
315 "Error: \"" << this->GetClassCategory( ) << "::" <<
316 this->GetClassName( ) << "\": " << error