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