]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/BaseObjects/ProcessObject.cxx
debug
[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   std::cout << "Modified: " << this->m_Name << std::endl;
210   this->Superclass::Modified( );
211
212   cpPlugins::BaseObjects::Events::Modified evt;
213   evt.Time = this->m_LastExecutionTime;
214   evt.Span = this->m_LastExecutionSpan;
215   this->InvokeEvent( evt );
216 }
217
218 // -------------------------------------------------------------------------
219 void cpPlugins::BaseObjects::ProcessObject::
220 Update( )
221 {
222   // Force upstream updates
223   bool update = ( this->m_LastExecutionTime < this->GetMTime( ) );
224   for( auto input : this->m_Inputs )
225   {
226     for( unsigned int i = 0; i < input.second->Size( ); ++i )
227     {
228       auto obj = input.second->Get( i );
229       if( obj != NULL )
230       {
231         auto src = obj->GetSource( );
232         if( src != NULL )
233         {
234           src->Update( );
235           update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
236
237         } // fi
238       }
239       else
240       {
241         if( input.second->IsRequired( ) )
242           this->_Error(
243             std::string( "Required input \"" ) + input.first +
244             std::string( "\" in filter \"" ) +
245             this->m_Name +
246             std::string( "\" is not valid." )
247             );
248
249       } // fi
250
251     } // rof
252
253   } // rof
254
255   if( update || this->m_ExplicitExecution )
256   {
257     // Show a message, if needed...
258     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
259     {
260       *( this->m_PrintExecutionStream )
261         << "cpPlugins: Updating \""
262         << this->GetName( ) << " ("
263         << this->GetClassCategory( ) << ":" << this->GetClassName( )
264         << ")\"... ";
265       this->m_PrintExecutionStream->flush( );
266
267     } // fi
268
269     // Execute filter's algorithm and keep information about time
270     auto t_start = cpPlugins_CHRONO;
271     this->_GenerateData( );
272     auto t_end = cpPlugins_CHRONO;
273     this->Modified( );
274     this->m_LastExecutionSpan = long( t_end - t_start );
275     this->m_LastExecutionTime = this->GetMTime( );
276
277     // End the message, if needed...
278     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
279     {
280       *( this->m_PrintExecutionStream )
281         << "done in "
282         << double( this->m_LastExecutionSpan ) / double( 1000 )
283         << " s." << std::endl;
284
285     } // fi
286
287   } // fi
288 }
289
290 // -------------------------------------------------------------------------
291 QDialog* cpPlugins::BaseObjects::ProcessObject::
292 CreateQDialog( )
293 {
294 #ifdef cpPlugins_QT4
295   cpPlugins::QT::ParametersDialog* dlg = NULL;
296   if( QApplication::instance( ) != NULL )
297   {
298     dlg = new cpPlugins::QT::ParametersDialog( );
299     dlg->setProcessObject( this );
300
301   } // fi
302   return( dlg );
303 #else // cpPlugins_QT4
304   return( NULL );
305 #endif // cpPlugins_QT4
306 }
307
308 // -------------------------------------------------------------------------
309 void cpPlugins::BaseObjects::ProcessObject::
310 AddInteractor( vtkRenderWindowInteractor* i )
311 {
312 }
313
314 // -------------------------------------------------------------------------
315 bool cpPlugins::BaseObjects::ProcessObject::
316 IsInteractive( )
317 {
318   return( false );
319 }
320
321 // -------------------------------------------------------------------------
322 cpPlugins::BaseObjects::ProcessObject::
323 ProcessObject( )
324   : Superclass( ),
325     m_Name( "" ),
326     m_PluginName( "" ),
327     m_ExplicitExecution( false ),
328     m_LastExecutionTime( 0 ),
329     m_LastExecutionSpan( -1 ),
330     m_PrintExecution( false ),
331     m_PrintExecutionStream( &( std::cout ) )
332 {
333   this->m_Parameters.SetProcessObject( this );
334 }
335
336 // -------------------------------------------------------------------------
337 cpPlugins::BaseObjects::ProcessObject::
338 ~ProcessObject( )
339 {
340   for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
341     delete i->second;
342   for( auto o = this->m_Outputs.begin( ); o != this->m_Outputs.end( ); ++o )
343     delete o->second;
344
345   this->m_Inputs.clear( );
346   this->m_Outputs.clear( );
347 }
348
349 // -------------------------------------------------------------------------
350 void cpPlugins::BaseObjects::ProcessObject::
351 _Error( const std::string& error )
352 {
353   if( error != "" )
354   {
355     itkExceptionMacro(
356       "Error: \"" << this->GetClassCategory( ) << "::" <<
357       this->GetClassName( ) << "\": " << error
358       );
359
360   } // fi
361 }
362
363 // eof - $RCSfile$