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