]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/BaseObjects/ProcessObject.cxx
...
[cpPlugins.git] / lib / cpPlugins / BaseObjects / ProcessObject.cxx
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>
7
8 // -------------------------------------------------------------------------
9 cpPlugins::BaseObjects::Parameters*
10 cpPlugins::BaseObjects::ProcessObject::
11 GetParameters( )
12 {
13   return( &( this->m_Parameters ) );
14 }
15
16 // -------------------------------------------------------------------------
17 const cpPlugins::BaseObjects::Parameters*
18 cpPlugins::BaseObjects::ProcessObject::
19 GetParameters( ) const
20 {
21   return( &( this->m_Parameters ) );
22 }
23
24 // -------------------------------------------------------------------------
25 void cpPlugins::BaseObjects::ProcessObject::
26 SetITK( itk::LightObject* o )
27 {
28   // Polymorphism: do nothing -> this is a filter!!!
29 }
30
31 // -------------------------------------------------------------------------
32 void cpPlugins::BaseObjects::ProcessObject::
33 SetVTK( vtkObjectBase* o )
34 {
35   // Polymorphism: do nothing -> this is a filter!!!
36 }
37
38 // -------------------------------------------------------------------------
39 std::set< std::string > cpPlugins::BaseObjects::ProcessObject::
40 GetInputsNames( ) const
41 {
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 );
45   return( names );
46 }
47
48 // -------------------------------------------------------------------------
49 std::set< std::string > cpPlugins::BaseObjects::ProcessObject::
50 GetOutputsNames( ) const
51 {
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 );
55   return( names );
56 }
57
58 // -------------------------------------------------------------------------
59 bool cpPlugins::BaseObjects::ProcessObject::
60 HasInput( const std::string& n ) const
61 {
62   auto i = this->m_Inputs.find( n );
63   return( i != this->m_Inputs.end( ) );
64 }
65
66 // -------------------------------------------------------------------------
67 bool cpPlugins::BaseObjects::ProcessObject::
68 HasOutput( const std::string& n ) const
69 {
70   auto i = this->m_Outputs.find( n );
71   return( i != this->m_Outputs.end( ) );
72 }
73
74 // -------------------------------------------------------------------------
75 unsigned int cpPlugins::BaseObjects::ProcessObject::
76 GetNumberOfInputs( ) const
77 {
78   return( this->m_Inputs.size( ) );
79 }
80
81 // -------------------------------------------------------------------------
82 unsigned int cpPlugins::BaseObjects::ProcessObject::
83 GetNumberOfOutputs( ) const
84 {
85   return( this->m_Outputs.size( ) );
86 }
87
88 // -------------------------------------------------------------------------
89 unsigned int cpPlugins::BaseObjects::ProcessObject::
90 GetInputSize( const std::string& n ) const
91 {
92   auto it = this->m_Inputs.find( n );
93   if( it != this->m_Inputs.end( ) )
94     return( it->second->Size( ) );
95   else
96     return( 0 );
97 }
98
99 // -------------------------------------------------------------------------
100 bool cpPlugins::BaseObjects::ProcessObject::
101 IsInputMultiple( const std::string& n ) const
102 {
103   auto i = this->m_Inputs.find( n );
104   if( i != this->m_Inputs.end( ) )
105     return( dynamic_cast< MultipleInputsPort* >( i->second ) != NULL );
106   else
107     return( false );
108 }
109
110 // -------------------------------------------------------------------------
111 void cpPlugins::BaseObjects::ProcessObject::
112 AddInput( const std::string& n, cpPlugins::BaseObjects::DataObject* o )
113 {
114   auto it = this->m_Inputs.find( n );
115   if( it != this->m_Inputs.end( ) )
116     it->second->Add( o );
117 }
118
119 // -------------------------------------------------------------------------
120 void cpPlugins::BaseObjects::ProcessObject::
121 SetInput( const std::string& n, cpPlugins::BaseObjects::DataObject* o )
122 {
123   this->AddInput( n, o );
124 }
125
126 // -------------------------------------------------------------------------
127 void cpPlugins::BaseObjects::ProcessObject::
128 DisconnectInput( const std::string& n, unsigned int id )
129 {
130   auto inIt = this->m_Inputs.find( n );
131   if( inIt != this->m_Inputs.end( ) )
132   {
133     auto multi = dynamic_cast< MultipleInputsPort* >( inIt->second );
134     auto single = dynamic_cast< InputPort* >( inIt->second );
135     if( multi != NULL )
136       multi->Delete( id );
137     else if( single != NULL )
138       single->Add( NULL );
139
140   } // fi
141 }
142
143 // -------------------------------------------------------------------------
144 void cpPlugins::BaseObjects::ProcessObject::
145 DisconnectInputs( )
146 {
147   for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
148     i->second->Clear( );
149   this->Modified( );
150 }
151
152 // -------------------------------------------------------------------------
153 void cpPlugins::BaseObjects::ProcessObject::
154 DisconnectOutputs( )
155 {
156   for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
157     if( i->second->IsValid( ) )
158       i->second->Get( )->DisconnectFromPipeline( );
159   this->Modified( );
160 }
161
162 // -------------------------------------------------------------------------
163 void cpPlugins::BaseObjects::ProcessObject::
164 Disconnect( )
165 {
166   this->DisconnectInputs( );
167   this->DisconnectOutputs( );
168 }
169
170 // -------------------------------------------------------------------------
171 void cpPlugins::BaseObjects::ProcessObject::
172 Modified( ) const
173 {
174   this->Superclass::Modified( );
175
176   cpPlugins::BaseObjects::Events::Modified evt;
177   evt.Time = this->m_LastExecutionTime;
178   evt.Span = this->m_LastExecutionSpan;
179   this->InvokeEvent( evt );
180 }
181
182 // -------------------------------------------------------------------------
183 void cpPlugins::BaseObjects::ProcessObject::
184 Update( )
185 {
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 )
189   {
190     bool iv = i->second->IsValid( );
191     bool ir = i->second->IsRequired( );
192     if( !iv && ir )
193       this->_Error(
194         std::string( "Required input \"" ) + i->first +
195         std::string( "\" is not valid." )
196         );
197     if( iv )
198     {
199       unsigned int n = i->second->Size( );
200       for( unsigned int j = 0; j < n; ++j )
201       {
202         Self* src = i->second->Get( j )->GetSource( );
203         if( src != NULL )
204         {
205           need_to_update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
206           src->Update( );
207
208         } // fi
209
210       } // rof
211
212     } // fi
213
214   } // rof
215
216   // Current update
217   if( this->m_LastExecutionTime < this->GetMTime( ) || need_to_update )
218   {
219     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
220     {
221       *( this->m_PrintExecutionStream )
222         << "cpPlugins: Updating \""
223         << this->GetName( ) << " ("
224         << this->GetClassCategory( ) << ":" << this->GetClassName( )
225         << ")\"... ";
226       this->m_PrintExecutionStream->flush( );
227
228     } // fi
229
230     auto t_start = cpPlugins_CHRONO;
231     this->_GenerateData( );
232     auto t_end = cpPlugins_CHRONO;
233     this->m_LastExecutionSpan = long( t_end - t_start );
234     this->Modified( );
235     this->m_LastExecutionTime = this->GetMTime( );
236
237     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
238     {
239       *( this->m_PrintExecutionStream )
240         << "done in "
241         << double( this->m_LastExecutionSpan ) / double( 1000 )
242         << " s." << std::endl;
243
244     } // fi
245
246   } // fi
247 }
248
249 // -------------------------------------------------------------------------
250 QDialog* cpPlugins::BaseObjects::ProcessObject::
251 CreateQDialog( )
252 {
253 #ifdef cpPlugins_QT4
254   cpPlugins::QT::ParametersDialog* dlg = NULL;
255   if( QApplication::instance( ) != NULL )
256   {
257     dlg = new cpPlugins::QT::ParametersDialog( );
258     dlg->setProcessObject( this );
259
260   } // fi
261   return( dlg );
262 #else // cpPlugins_QT4
263   return( NULL );
264 #endif // cpPlugins_QT4
265 }
266
267 // -------------------------------------------------------------------------
268 void cpPlugins::BaseObjects::ProcessObject::
269 AddInteractor( vtkRenderWindowInteractor* i )
270 {
271 }
272
273 // -------------------------------------------------------------------------
274 bool cpPlugins::BaseObjects::ProcessObject::
275 IsInteractive( )
276 {
277   return( false );
278 }
279
280 // -------------------------------------------------------------------------
281 cpPlugins::BaseObjects::ProcessObject::
282 ProcessObject( )
283   : Superclass( ),
284     m_Name( "" ),
285     m_PluginName( "" ),
286     m_ExplicitExecution( false ),
287     m_LastExecutionTime( 0 ),
288     m_LastExecutionSpan( -1 ),
289     m_PrintExecution( false ),
290     m_PrintExecutionStream( &( std::cout ) )
291 {
292   this->m_Parameters.SetProcessObject( this );
293 }
294
295 // -------------------------------------------------------------------------
296 cpPlugins::BaseObjects::ProcessObject::
297 ~ProcessObject( )
298 {
299   for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
300     delete i->second;
301   for( auto o = this->m_Outputs.begin( ); o != this->m_Outputs.end( ); ++o )
302     delete o->second;
303
304   this->m_Inputs.clear( );
305   this->m_Outputs.clear( );
306 }
307
308 // -------------------------------------------------------------------------
309 void cpPlugins::BaseObjects::ProcessObject::
310 _Error( const std::string& error )
311 {
312   if( error != "" )
313   {
314     itkExceptionMacro(
315       "Error: \"" << this->GetClassCategory( ) << "::" <<
316       this->GetClassName( ) << "\": " << error
317       );
318
319   } // fi
320 }
321
322 // eof - $RCSfile$