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 void cpPlugins::BaseObjects::ProcessObject::
12 this->SetPrintExecution( true );
15 // -------------------------------------------------------------------------
16 void cpPlugins::BaseObjects::ProcessObject::
19 this->SetPrintExecution( false );
22 // -------------------------------------------------------------------------
23 bool cpPlugins::BaseObjects::ProcessObject::
24 GetPrintExecution( ) const
26 return( this->m_PrintExecution );
29 // -------------------------------------------------------------------------
30 bool cpPlugins::BaseObjects::ProcessObject::
31 SetPrintExecution( bool v )
33 this->m_PrintExecution = v;
36 // -------------------------------------------------------------------------
37 void cpPlugins::BaseObjects::ProcessObject::
38 SetPrintExecutionStream( std::ofstream* s )
40 this->m_PrintExecutionStream = s;
43 // -------------------------------------------------------------------------
44 cpPlugins::BaseObjects::Parameters*
45 cpPlugins::BaseObjects::ProcessObject::
48 return( &( this->m_Parameters ) );
51 // -------------------------------------------------------------------------
52 const cpPlugins::BaseObjects::Parameters*
53 cpPlugins::BaseObjects::ProcessObject::
54 GetParameters( ) const
56 return( &( this->m_Parameters ) );
59 // -------------------------------------------------------------------------
60 void cpPlugins::BaseObjects::ProcessObject::
61 SetITK( itk::LightObject* o )
63 // Polymorphism: do nothing -> this is a filter!!!
66 // -------------------------------------------------------------------------
67 void cpPlugins::BaseObjects::ProcessObject::
68 SetVTK( vtkObjectBase* o )
70 // Polymorphism: do nothing -> this is a filter!!!
73 // -------------------------------------------------------------------------
74 std::set< std::string > cpPlugins::BaseObjects::ProcessObject::
75 GetInputsNames( ) const
77 std::set< std::string > names;
78 for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
79 names.insert( i->first );
83 // -------------------------------------------------------------------------
84 std::set< std::string > cpPlugins::BaseObjects::ProcessObject::
85 GetOutputsNames( ) const
87 std::set< std::string > names;
88 for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
89 names.insert( i->first );
93 // -------------------------------------------------------------------------
94 bool cpPlugins::BaseObjects::ProcessObject::
95 HasInput( const std::string& n ) const
97 auto i = this->m_Inputs.find( n );
98 return( i != this->m_Inputs.end( ) );
101 // -------------------------------------------------------------------------
102 bool cpPlugins::BaseObjects::ProcessObject::
103 HasOutput( const std::string& n ) const
105 auto i = this->m_Outputs.find( n );
106 return( i != this->m_Outputs.end( ) );
109 // -------------------------------------------------------------------------
110 unsigned int cpPlugins::BaseObjects::ProcessObject::
111 GetNumberOfInputs( ) const
113 return( this->m_Inputs.size( ) );
116 // -------------------------------------------------------------------------
117 unsigned int cpPlugins::BaseObjects::ProcessObject::
118 GetNumberOfOutputs( ) const
120 return( this->m_Outputs.size( ) );
123 // -------------------------------------------------------------------------
124 unsigned int cpPlugins::BaseObjects::ProcessObject::
125 GetInputSize( const std::string& n ) const
127 auto it = this->m_Inputs.find( n );
128 if( it != this->m_Inputs.end( ) )
129 return( it->second->Size( ) );
134 // -------------------------------------------------------------------------
135 bool cpPlugins::BaseObjects::ProcessObject::
136 IsInputMultiple( const std::string& n ) const
138 auto i = this->m_Inputs.find( n );
139 if( i != this->m_Inputs.end( ) )
140 return( dynamic_cast< MultipleInputsPort* >( i->second ) != NULL );
145 // -------------------------------------------------------------------------
146 void cpPlugins::BaseObjects::ProcessObject::
147 AddInput( const std::string& n, cpPlugins::BaseObjects::DataObject* o )
149 auto it = this->m_Inputs.find( n );
150 if( it != this->m_Inputs.end( ) )
151 it->second->Add( o );
154 // -------------------------------------------------------------------------
155 void cpPlugins::BaseObjects::ProcessObject::
156 SetInput( const std::string& n, cpPlugins::BaseObjects::DataObject* o )
158 this->AddInput( n, o );
161 // -------------------------------------------------------------------------
162 void cpPlugins::BaseObjects::ProcessObject::
163 DisconnectInput( const std::string& n, unsigned int id )
165 auto inIt = this->m_Inputs.find( n );
166 if( inIt != this->m_Inputs.end( ) )
168 auto multi = dynamic_cast< MultipleInputsPort* >( inIt->second );
169 auto single = dynamic_cast< InputPort* >( inIt->second );
172 else if( single != NULL )
178 // -------------------------------------------------------------------------
179 void cpPlugins::BaseObjects::ProcessObject::
182 for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
187 // -------------------------------------------------------------------------
188 void cpPlugins::BaseObjects::ProcessObject::
191 for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
192 if( i->second->IsValid( ) )
193 i->second->Get( )->DisconnectFromPipeline( );
197 // -------------------------------------------------------------------------
198 void cpPlugins::BaseObjects::ProcessObject::
201 this->DisconnectInputs( );
202 this->DisconnectOutputs( );
205 // -------------------------------------------------------------------------
206 void cpPlugins::BaseObjects::ProcessObject::
209 this->Superclass::Modified( );
211 cpPlugins::BaseObjects::Events::Modified evt;
212 evt.Time = this->m_LastExecutionTime;
213 evt.Span = this->m_LastExecutionSpan;
214 this->InvokeEvent( evt );
217 // -------------------------------------------------------------------------
218 void cpPlugins::BaseObjects::ProcessObject::
221 // Force upstream updates
222 bool update = ( this->m_LastExecutionTime < this->GetMTime( ) );
225 for( auto input : this->m_Inputs )
227 for( unsigned int i = 0; i < input.second->Size( ); ++i )
229 auto obj = input.second->Get( i );
232 auto src = obj->GetSource( );
236 update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
242 if( input.second->IsRequired( ) )
244 std::string( "Required input \"" ) + input.first +
245 std::string( "\" in filter \"" ) +
247 std::string( "\" is not valid." )
256 catch( std::exception& err )
258 this->_Error( err.what( ) );
262 if( update || this->m_ExplicitExecution )
264 // Show a message, if needed...
265 if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
267 *( this->m_PrintExecutionStream )
268 << "cpPlugins: Updating \""
269 << this->GetName( ) << " ("
270 << this->GetClassCategory( ) << ":" << this->GetClassName( )
272 this->m_PrintExecutionStream->flush( );
276 // Execute filter's algorithm and keep information about time
279 auto t_start = cpPlugins_CHRONO;
280 this->_GenerateData( );
281 auto t_end = cpPlugins_CHRONO;
283 this->m_LastExecutionSpan = long( t_end - t_start );
284 this->m_LastExecutionTime = this->GetMTime( );
286 // End the message, if needed...
287 if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
289 *( this->m_PrintExecutionStream )
291 << double( this->m_LastExecutionSpan ) / double( 1000 )
292 << " s." << std::endl;
296 catch( std::exception& err )
298 this->_Error( err.what( ) );
305 // -------------------------------------------------------------------------
306 QDialog* cpPlugins::BaseObjects::ProcessObject::
310 cpPlugins::QT::ParametersDialog* dlg = NULL;
311 if( QApplication::instance( ) != NULL )
313 dlg = new cpPlugins::QT::ParametersDialog( );
314 dlg->setProcessObject( this );
318 #else // cpPlugins_QT4
320 #endif // cpPlugins_QT4
323 // -------------------------------------------------------------------------
324 void cpPlugins::BaseObjects::ProcessObject::
325 AddInteractor( vtkRenderWindowInteractor* i )
329 // -------------------------------------------------------------------------
330 bool cpPlugins::BaseObjects::ProcessObject::
336 // -------------------------------------------------------------------------
337 cpPlugins::BaseObjects::ProcessObject::
342 m_ExplicitExecution( false ),
343 m_LastExecutionTime( 0 ),
344 m_LastExecutionSpan( -1 ),
345 m_PrintExecution( false ),
346 m_PrintExecutionStream( &( std::cout ) )
348 this->m_Parameters.SetProcessObject( this );
351 // -------------------------------------------------------------------------
352 cpPlugins::BaseObjects::ProcessObject::
355 for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
357 for( auto o = this->m_Outputs.begin( ); o != this->m_Outputs.end( ); ++o )
360 this->m_Inputs.clear( );
361 this->m_Outputs.clear( );
364 // -------------------------------------------------------------------------
365 void cpPlugins::BaseObjects::ProcessObject::
366 _Error( const std::string& error )
370 "Error: \"" << this->m_Name << "\": " << error