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