1 #include <cpPlugins/Workspace.h>
2 #include <cpPlugins/BaseWidget.h>
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <vtkRenderWindowInteractor.h>
6 // -------------------------------------------------------------------------
12 this->m_Graph = TGraph::New( );
15 // -------------------------------------------------------------------------
16 cpPlugins::Workspace::
19 this->m_Graph->Clear( );
22 // -------------------------------------------------------------------------
23 cpPlugins::Interface* cpPlugins::Workspace::
26 return( this->m_Interface );
29 // -------------------------------------------------------------------------
30 const cpPlugins::Interface* cpPlugins::Workspace::
33 return( this->m_Interface );
36 // -------------------------------------------------------------------------
37 void cpPlugins::Workspace::
38 SetInterface( cpPlugins::Interface* i )
40 if( this->m_Interface != i )
41 this->m_Interface = i;
44 // -------------------------------------------------------------------------
45 cpPlugins::Workspace::
46 TGraph* cpPlugins::Workspace::
49 return( this->m_Graph );
52 // -------------------------------------------------------------------------
53 const cpPlugins::Workspace::
54 TGraph* cpPlugins::Workspace::
57 return( this->m_Graph );
60 // -------------------------------------------------------------------------
61 void cpPlugins::Workspace::
64 if( this->m_Graph.IsNotNull( ) )
65 this->m_Graph->Clear( );
68 // -------------------------------------------------------------------------
69 void cpPlugins::Workspace::
74 // -------------------------------------------------------------------------
75 cpPlugins::ProcessObject* cpPlugins::Workspace::
76 GetFilter( const std::string& name )
79 dynamic_cast< ProcessObject* >(
80 this->m_Graph->GetVertex( name ).GetPointer( )
85 // -------------------------------------------------------------------------
86 const cpPlugins::ProcessObject* cpPlugins::Workspace::
87 GetFilter( const std::string& name ) const
89 const ProcessObject* f =
90 dynamic_cast< const ProcessObject* >(
91 this->m_Graph->GetVertex( name ).GetPointer( )
96 // -------------------------------------------------------------------------
97 bool cpPlugins::Workspace::
98 HasFilter( const std::string& name ) const
100 if( this->m_Graph->HasVertexIndex( name ) )
101 return( this->GetFilter( name ) != NULL );
106 // -------------------------------------------------------------------------
107 cpPlugins::ProcessObject* cpPlugins::Workspace::
109 const std::string& category,
110 const std::string& filter,
111 const std::string& name
114 if( this->m_Interface == NULL )
117 // Get or create new filter from name
118 if( !( this->m_Graph->HasVertexIndex( name ) ) )
120 ProcessObject::Pointer f =
121 this->m_Interface->Create( category, filter );
124 if( f->IsInteractive( ) )
126 std::vector< void* > interactive_objects;
127 interactive_objects.push_back( this->m_SingleInteractor );
128 interactive_objects.push_back( this->m_MPRViewer );
129 f->SetInteractionObjects( interactive_objects );
132 Object::Pointer o = f.GetPointer( );
133 this->m_Graph->SetVertex( name, o );
136 return( f.GetPointer( ) );
139 return( this->GetFilter( name ) );
142 // -------------------------------------------------------------------------
143 bool cpPlugins::Workspace::
144 RenameFilter( const std::string& old_name, const std::string& new_name )
146 return( this->m_Graph->RenameVertex( old_name, new_name ) );
149 // -------------------------------------------------------------------------
150 void cpPlugins::Workspace::
151 RemoveFilter( const std::string& name )
155 // -------------------------------------------------------------------------
156 void cpPlugins::Workspace::
157 SetParameter( const std::string& name, const std::string& value )
159 std::vector< std::string > tokens;
160 cpPlugins::TokenizeString( tokens, name, "@" );
162 if( this->HasFilter( tokens[ 1 ] ) )
164 auto filter = this->GetFilter( tokens[ 1 ] );
165 filter->GetParameters( )->SetString( tokens[ 0 ], value );
170 // -------------------------------------------------------------------------
171 vtkRenderWindowInteractor* cpPlugins::Workspace::
172 GetSingleInteractor( )
174 return( this->m_SingleInteractor );
177 // -------------------------------------------------------------------------
178 const vtkRenderWindowInteractor* cpPlugins::Workspace::
179 GetSingleInteractor( ) const
181 return( this->m_SingleInteractor );
184 // -------------------------------------------------------------------------
185 void cpPlugins::Workspace::
186 SetSingleInteractor( vtkRenderWindowInteractor* interactor )
188 this->m_SingleInteractor = interactor;
191 // -------------------------------------------------------------------------
192 cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
195 return( this->m_MPRViewer );
198 // -------------------------------------------------------------------------
199 const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
200 GetMPRViewer( ) const
202 return( this->m_MPRViewer );
205 // -------------------------------------------------------------------------
206 void cpPlugins::Workspace::
207 SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
209 this->m_MPRViewer = wdg;
212 // -------------------------------------------------------------------------
213 bool cpPlugins::Workspace::
215 const std::string& orig_filter, const std::string& dest_filter,
216 const std::string& output_name, const std::string& input_name
220 ProcessObject* orig = this->GetFilter( orig_filter );
221 ProcessObject* dest = this->GetFilter( dest_filter );
222 if( orig == NULL || dest == NULL )
226 if( dest->SetInput( input_name, orig->GetOutput( output_name ) ) )
228 this->m_Graph->AddEdge(
229 orig_filter, dest_filter,
230 TConnection( output_name, input_name )
238 // -------------------------------------------------------------------------
239 bool cpPlugins::Workspace::
240 Connect( const OutputPort& port, const std::string& exposed_port )
242 auto i = this->m_ExposedInputPorts.find( exposed_port );
243 if( i != this->m_ExposedInputPorts.end( ) )
245 ProcessObject* filter = this->GetFilter( i->second.first );
247 return( filter->SetInput( i->second.second, port ) );
255 // -------------------------------------------------------------------------
256 bool cpPlugins::Workspace::
257 Reduce( const std::string& name )
262 // -------------------------------------------------------------------------
263 void cpPlugins::Workspace::
265 const std::string& name,
266 const std::string& filter, const std::string& filter_input
269 this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
272 // -------------------------------------------------------------------------
273 void cpPlugins::Workspace::
275 const std::string& name,
276 const std::string& filter, const std::string& filter_output
279 this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
282 // -------------------------------------------------------------------------
283 void cpPlugins::Workspace::
284 HideInputPort( const std::string& name )
286 auto i = this->m_ExposedInputPorts.find( name );
287 if( i != this->m_ExposedInputPorts.end( ) )
288 this->m_ExposedInputPorts.erase( i );
291 // -------------------------------------------------------------------------
292 void cpPlugins::Workspace::
293 HideOutputPort( const std::string& name )
295 auto i = this->m_ExposedOutputPorts.find( name );
296 if( i != this->m_ExposedOutputPorts.end( ) )
297 this->m_ExposedOutputPorts.erase( i );
300 // -------------------------------------------------------------------------
301 bool cpPlugins::Workspace::
302 RenameExposedInputPort(
303 const std::string& old_name,
304 const std::string& new_name
307 auto o = this->m_ExposedInputPorts.find( old_name );
308 auto n = this->m_ExposedInputPorts.find( new_name );
310 o != this->m_ExposedInputPorts.end( ) &&
311 n == this->m_ExposedInputPorts.end( )
314 this->m_ExposedInputPorts[ new_name ] = o->second;
315 this->m_ExposedInputPorts.erase( o );
322 // -------------------------------------------------------------------------
323 bool cpPlugins::Workspace::
324 RenameExposedOutputPort(
325 const std::string& old_name,
326 const std::string& new_name
329 auto o = this->m_ExposedOutputPorts.find( old_name );
330 auto n = this->m_ExposedOutputPorts.find( new_name );
332 o != this->m_ExposedOutputPorts.end( ) &&
333 n == this->m_ExposedOutputPorts.end( )
336 this->m_ExposedOutputPorts[ new_name ] = o->second;
337 this->m_ExposedOutputPorts.erase( o );
344 // -------------------------------------------------------------------------
345 const cpPlugins::Workspace::
346 TExposedPorts& cpPlugins::Workspace::
347 GetExposedInputPorts( ) const
349 return( this->m_ExposedInputPorts );
352 // -------------------------------------------------------------------------
353 const cpPlugins::Workspace::
354 TExposedPorts& cpPlugins::Workspace::
355 GetExposedOutputPorts( ) const
357 return( this->m_ExposedOutputPorts );
360 // -------------------------------------------------------------------------
362 OutputPort& cpPlugins::Workspace::
363 GetExposedOutput( const std::string& name )
365 static OutputPort null_port;
367 auto i = this->m_ExposedOutputPorts.find( name );
368 if( i != this->m_ExposedOutputPorts.end( ) )
370 ProcessObject* filter = this->GetFilter( i->second.first );
372 return( filter->GetOutput( i->second.second ) );
378 // -------------------------------------------------------------------------
380 OutputPort& cpPlugins::Workspace::
381 GetExposedOutput( const std::string& name ) const
383 static const OutputPort null_port;
385 auto i = this->m_ExposedOutputPorts.find( name );
386 if( i != this->m_ExposedOutputPorts.end( ) )
388 const ProcessObject* filter = this->GetFilter( i->second.first );
390 return( filter->GetOutput( i->second.second ) );
396 // -------------------------------------------------------------------------
397 void cpPlugins::Workspace::
401 std::set< std::string > sinks = this->m_Graph->GetSinks( );
404 for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
405 this->Execute( *sIt );
408 // -------------------------------------------------------------------------
409 void cpPlugins::Workspace::
410 Execute( const std::string& name )
413 ProcessObject* f = this->GetFilter( name );
416 itkGenericExceptionMacro(
417 "cpPlugins::Workspace: Vertex \"" << name << "\" is not a filter."