]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/ProcessObject.cxx
590de5dccc8c4d70d6714cc92beb4f4b4317ac74
[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     /*
244     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
245     {
246     */
247       *( this->m_PrintExecutionStream )
248         << "cpPlugins: Updating \""
249         << this->GetClassCategory( ) << ":" << this->GetClassName( )
250         << "\"... ";
251       this->m_PrintExecutionStream->flush( );
252
253     // } // fi
254
255     auto t_start = cpPlugins_CHRONO;
256     this->_GenerateData( );
257     auto t_end = cpPlugins_CHRONO;
258     this->m_LastExecutionSpan = long( t_end - t_start );
259     this->m_LastExecutionTime = this->GetMTime( );
260
261     /*
262     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
263     {
264     */
265       *( this->m_PrintExecutionStream )
266         << "done in "
267         << double( this->m_LastExecutionSpan ) / double( 1000 )
268         << " s." << std::endl;
269
270       // } // fi
271
272   } // fi
273 }
274
275 // -------------------------------------------------------------------------
276 cpPlugins::ParametersQtDialog* cpPlugins::ProcessObject::
277 CreateQtDialog( )
278 {
279 #ifdef cpPlugins_QT4
280   ParametersQtDialog* dlg = NULL;
281   if( QApplication::instance( ) != NULL )
282   {
283     dlg = new ParametersQtDialog( );
284     dlg->setProcessObject( this );
285
286   } // fi
287   return( dlg );
288 #else // cpPlugins_QT4  
289   return( NULL );
290 #endif // cpPlugins_QT4
291 }
292
293 // -------------------------------------------------------------------------
294 bool cpPlugins::ProcessObject::
295 IsInteractive( )
296 {
297   return( false );
298 }
299
300 // -------------------------------------------------------------------------
301 void cpPlugins::ProcessObject::
302 SetInteractionObjects( const std::vector< void* >& objs )
303 {
304   // Do nothing
305 }
306
307 // -------------------------------------------------------------------------
308 cpPlugins::ProcessObject::
309 ProcessObject( )
310   : Superclass( ),
311     m_LastExecutionTime( 0 ),
312     m_LastExecutionSpan( -1 ),
313     m_PrintExecution( false ),
314     m_PrintExecutionStream( &( std::cout ) )
315 {
316 }
317
318 // -------------------------------------------------------------------------
319 cpPlugins::ProcessObject::
320 ~ProcessObject( )
321 {
322 }
323
324 // -------------------------------------------------------------------------
325 void cpPlugins::ProcessObject::
326 _AddInput( const std::string& name, bool required )
327 {
328   auto i = this->m_Inputs.find( name );
329   if( i == this->m_Inputs.end( ) )
330   {
331     this->m_Inputs[ name ] = InputPort( required );
332     this->Modified( );
333
334   } // fi
335 }
336
337 // -------------------------------------------------------------------------
338 void cpPlugins::ProcessObject::
339 _Error( const std::string& error )
340 {
341   if( error != "" )
342   {
343     itkExceptionMacro(
344       "Error: \"" << this->GetClassCategory( ) << "::" <<
345       this->GetClassName( ) << "\": " << error
346       );
347
348   } // fi
349 }
350
351 // eof - $RCSfile$