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