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