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