]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/BaseObjects/ProcessObject.cxx
ab1be4d9cb9fc341ae919a809604f7a50ecf9e29
[cpPlugins.git] / lib / cpPlugins / BaseObjects / ProcessObject.cxx
1 #include <cpPlugins/BaseObjects/ProcessObject.h>
2 #include <cpPlugins/QT/ParametersDialog.h>
3 #include <cpExtensions/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 void cpPlugins::BaseObjects::ProcessObject::
84 AddInput( const std::string& n, cpPlugins::BaseObjects::DataObject* o )
85 {
86   auto it = this->m_Inputs.find( n );
87   if( it != this->m_Inputs.end( ) )
88     it->second->Add( o );
89 }
90
91 // -------------------------------------------------------------------------
92 void cpPlugins::BaseObjects::ProcessObject::
93 SetInput( const std::string& n, cpPlugins::BaseObjects::DataObject* o )
94 {
95   this->AddInput( n, o );
96 }
97
98 // -------------------------------------------------------------------------
99 void cpPlugins::BaseObjects::ProcessObject::
100 DisconnectInputs( )
101 {
102   for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
103     i->second->Clear( );
104   this->Modified( );
105 }
106
107 // -------------------------------------------------------------------------
108 void cpPlugins::BaseObjects::ProcessObject::
109 DisconnectOutputs( )
110 {
111   for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
112     if( i->second->IsValid( ) )
113       i->second->Get( )->DisconnectFromPipeline( );
114   this->Modified( );
115 }
116
117 // -------------------------------------------------------------------------
118 void cpPlugins::BaseObjects::ProcessObject::
119 Disconnect( )
120 {
121   this->DisconnectInputs( );
122   this->DisconnectOutputs( );
123 }
124
125 // -------------------------------------------------------------------------
126 void cpPlugins::BaseObjects::ProcessObject::
127 Modified( ) const
128 {
129   this->Superclass::Modified( );
130   this->m_LastExecutionSpan = -1;
131 }
132
133 // -------------------------------------------------------------------------
134 itk::ModifiedTimeType cpPlugins::BaseObjects::ProcessObject::
135 GetMTime( ) const
136 {
137   auto params_time = this->m_Parameters.GetMTime( );
138   auto filter_time = this->Superclass::GetMTime( );
139   return( ( params_time > filter_time )? params_time: filter_time );
140 }
141
142 // -------------------------------------------------------------------------
143 void cpPlugins::BaseObjects::ProcessObject::
144 Update( )
145 {
146   // Force upstream updates
147   bool need_to_update = this->m_ExplicitExecution;
148   for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
149   {
150     bool iv = i->second->IsValid( );
151     bool ir = i->second->IsRequired( );
152     if( !iv && ir )
153       this->_Error(
154         std::string( "Required input \"" ) + i->first +
155         std::string( "\" is not valid." )
156         );
157     if( iv )
158     {
159       unsigned int n = i->second->Size( );
160       for( unsigned int j = 0; j < n; ++j )
161       {
162         Self* src = i->second->Get( j )->GetSource( );
163         if( src != NULL )
164         {
165           need_to_update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
166           src->Update( );
167
168         } // fi
169
170       } // rof
171
172     } // fi
173
174   } // rof
175
176   // Current update
177   if( this->m_LastExecutionTime < this->GetMTime( ) || need_to_update )
178   {
179     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
180     {
181       *( this->m_PrintExecutionStream )
182         << "cpPlugins: Updating \""
183         << this->GetName( ) << " ("
184         << this->GetClassCategory( ) << ":" << this->GetClassName( )
185         << ")\"... ";
186       this->m_PrintExecutionStream->flush( );
187
188     } // fi
189
190     auto t_start = cpExtensions_CHRONO;
191     this->_GenerateData( );
192     auto t_end = cpExtensions_CHRONO;
193     this->m_LastExecutionSpan = long( t_end - t_start );
194     this->m_LastExecutionTime = this->GetMTime( );
195
196     if( this->m_PrintExecution && this->m_PrintExecutionStream != NULL )
197     {
198       *( this->m_PrintExecutionStream )
199         << "done in "
200         << double( this->m_LastExecutionSpan ) / double( 1000 )
201         << " s." << std::endl;
202
203     } // fi
204
205   } // fi
206 }
207
208 // -------------------------------------------------------------------------
209 QDialog* cpPlugins::BaseObjects::ProcessObject::
210 CreateQDialog( )
211 {
212 #ifdef cpPlugins_QT4
213   cpPlugins::QT::ParametersDialog* dlg = NULL;
214   if( QApplication::instance( ) != NULL )
215   {
216     dlg = new cpPlugins::QT::ParametersDialog( );
217     dlg->setProcessObject( this );
218
219   } // fi
220   return( dlg );
221 #else // cpPlugins_QT4
222   return( NULL );
223 #endif // cpPlugins_QT4
224 }
225
226 // -------------------------------------------------------------------------
227 void cpPlugins::BaseObjects::ProcessObject::
228 AddInteractor( vtkRenderWindowInteractor* i )
229 {
230   if( this->m_Interactors.insert( i ).second )
231     this->Modified( );
232 }
233
234 // -------------------------------------------------------------------------
235 bool cpPlugins::BaseObjects::ProcessObject::
236 IsInteractive( )
237 {
238   return( false );
239 }
240
241 // -------------------------------------------------------------------------
242 cpPlugins::BaseObjects::ProcessObject::
243 ProcessObject( )
244   : Superclass( ),
245     m_Name( "" ),
246     m_PluginName( "" ),
247     m_ExplicitExecution( false ),
248     m_LastExecutionTime( 0 ),
249     m_LastExecutionSpan( -1 ),
250     m_PrintExecution( false ),
251     m_PrintExecutionStream( &( std::cout ) )
252 {
253 }
254
255 // -------------------------------------------------------------------------
256 cpPlugins::BaseObjects::ProcessObject::
257 ~ProcessObject( )
258 {
259   for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
260     delete i->second;
261   for( auto o = this->m_Outputs.begin( ); o != this->m_Outputs.end( ); ++o )
262     delete o->second;
263
264   this->m_Inputs.clear( );
265   this->m_Outputs.clear( );
266 }
267
268 // -------------------------------------------------------------------------
269 void cpPlugins::BaseObjects::ProcessObject::
270 _Error( const std::string& error )
271 {
272   if( error != "" )
273   {
274     itkExceptionMacro(
275       "Error: \"" << this->GetClassCategory( ) << "::" <<
276       this->GetClassName( ) << "\": " << error
277       );
278
279   } // fi
280 }
281
282 // eof - $RCSfile$