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