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