]> 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
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 bool cpPlugins::ProcessObject::
123 SetInputPort( const std::string& id, const OutputPort& port )
124 {
125   auto i = this->m_Inputs.find( id );
126   if( i != this->m_Inputs.end( ) )
127   {
128     if( i->second.GetPointer( ) != port.GetPointer( ) )
129     {
130       i->second = port;
131       this->Modified( );
132
133     } // fi
134     return( true );
135   }
136   else
137     return( false );
138 }
139
140 // -------------------------------------------------------------------------
141 void cpPlugins::ProcessObject::
142 DisconnectInputs( )
143 {
144   auto i = this->m_Inputs.begin( );
145   for( ; i != this->m_Inputs.end( ); ++i )
146     i->second = NULL;
147   this->Modified( );
148 }
149
150 // -------------------------------------------------------------------------
151 void cpPlugins::ProcessObject::
152 DisconnectOutputs( )
153 {
154   auto i = this->m_Outputs.begin( );
155   for( ; i != this->m_Outputs.end( ); ++i )
156     if( i->second.IsValid( ) )
157       i->second->DisconnectFromPipeline( );
158   this->Modified( );
159 }
160
161 // -------------------------------------------------------------------------
162 void cpPlugins::ProcessObject::
163 Disconnect( )
164 {
165   this->DisconnectInputs( );
166   this->DisconnectOutputs( );
167 }
168
169 // -------------------------------------------------------------------------
170 void cpPlugins::ProcessObject::
171 Modified( ) const
172 {
173   this->Superclass::Modified( );
174   this->m_LastExecutionSpan = -1;
175 }
176
177 // -------------------------------------------------------------------------
178 itk::ModifiedTimeType cpPlugins::ProcessObject::
179 GetMTime( ) const
180 {
181   auto params_time = this->m_Parameters.GetMTime( );
182   auto filter_time = this->Superclass::GetMTime( );
183   return( ( params_time > filter_time )? params_time: filter_time );
184 }
185
186 // -------------------------------------------------------------------------
187 void cpPlugins::ProcessObject::
188 Update( )
189 {
190   // Force upstream updates
191   auto i = this->m_Inputs.begin( );
192   bool need_to_update = false;
193   for( ; i != this->m_Inputs.end( ); ++i )
194   {
195     bool iv = i->second.IsValid( );
196     bool ir = i->second.IsRequired( );
197     if( !iv && ir )
198       this->_Error(
199         std::string( "Required input \"" ) + i->first +
200         std::string( "\" is not valid." )
201         );
202     if( iv )
203     {
204       Self* src = dynamic_cast< Self* >( i->second->GetSource( ) );
205       if( src != NULL )
206       {
207         need_to_update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
208         src->Update( );
209
210       } // fi
211
212     } // fi
213
214   } // rof
215
216   // Current update
217   if( this->m_LastExecutionTime < this->GetMTime( ) || need_to_update )
218   {
219     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
220     {
221       *( this->m_PrintExecutionStream )
222         << "cpPlugins: Updating \""
223         << this->GetClassCategory( ) << ":" << this->GetClassName( )
224         << "\"... ";
225       this->m_PrintExecutionStream->flush( );
226
227     } // fi
228
229     auto t_start = cpPlugins_CHRONO;
230     this->_GenerateData( );
231     auto t_end = cpPlugins_CHRONO;
232     this->m_LastExecutionSpan = long( t_end - t_start );
233     this->m_LastExecutionTime = this->GetMTime( );
234
235     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
236     {
237       *( this->m_PrintExecutionStream )
238         << "done in "
239         << double( this->m_LastExecutionSpan ) / double( 1000 )
240         << " s." << std::endl;
241
242     } // fi
243
244   } // fi
245 }
246
247 // -------------------------------------------------------------------------
248 cpPlugins::ParametersQtDialog* cpPlugins::ProcessObject::
249 CreateQtDialog( )
250 {
251 #ifdef cpPlugins_QT4
252   ParametersQtDialog* dlg = NULL;
253   if( QApplication::instance( ) != NULL )
254   {
255     dlg = new ParametersQtDialog( );
256     dlg->setProcessObject( this );
257
258   } // fi
259   return( dlg );
260 #else // cpPlugins_QT4  
261   return( NULL );
262 #endif // cpPlugins_QT4
263 }
264
265 // -------------------------------------------------------------------------
266 bool cpPlugins::ProcessObject::
267 IsInteractive( )
268 {
269   return( false );
270 }
271
272 // -------------------------------------------------------------------------
273 void cpPlugins::ProcessObject::
274 SetInteractionObjects( const std::vector< void* >& objs )
275 {
276   // Do nothing
277 }
278
279 // -------------------------------------------------------------------------
280 cpPlugins::ProcessObject::
281 ProcessObject( )
282   : Superclass( ),
283     m_LastExecutionTime( 0 ),
284     m_LastExecutionSpan( -1 ),
285     m_PrintExecution( false ),
286     m_PrintExecutionStream( &( std::cout ) )
287 {
288 }
289
290 // -------------------------------------------------------------------------
291 cpPlugins::ProcessObject::
292 ~ProcessObject( )
293 {
294 }
295
296 // -------------------------------------------------------------------------
297 void cpPlugins::ProcessObject::
298 _AddInput( const std::string& name, bool required )
299 {
300   auto i = this->m_Inputs.find( name );
301   if( i == this->m_Inputs.end( ) )
302   {
303     this->m_Inputs[ name ] = InputPort( required );
304     this->Modified( );
305
306   } // fi
307 }
308
309 // -------------------------------------------------------------------------
310 void cpPlugins::ProcessObject::
311 _Error( const std::string& error )
312 {
313   if( error != "" )
314   {
315     itkExceptionMacro(
316       "Error: \"" << this->GetClassCategory( ) << "::" <<
317       this->GetClassName( ) << "\": " << error
318       );
319
320   } // fi
321 }
322
323 // eof - $RCSfile$