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 void cpPlugins::Workspace::
172 SetPrintExecution( bool b )
174 auto vIt = this->m_Graph->BeginVertices( );
175 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
177 auto f = dynamic_cast< ProcessObject* >( vIt->second.GetPointer( ) );
179 f->SetPrintExecution( b );
184 // -------------------------------------------------------------------------
185 void cpPlugins::Workspace::
188 this->SetPrintExecution( true );
191 // -------------------------------------------------------------------------
192 void cpPlugins::Workspace::
195 this->SetPrintExecution( false );
198 // -------------------------------------------------------------------------
199 vtkRenderWindowInteractor* cpPlugins::Workspace::
200 GetSingleInteractor( )
202 return( this->m_SingleInteractor );
205 // -------------------------------------------------------------------------
206 const vtkRenderWindowInteractor* cpPlugins::Workspace::
207 GetSingleInteractor( ) const
209 return( this->m_SingleInteractor );
212 // -------------------------------------------------------------------------
213 void cpPlugins::Workspace::
214 SetSingleInteractor( vtkRenderWindowInteractor* interactor )
216 this->m_SingleInteractor = interactor;
219 // -------------------------------------------------------------------------
220 cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
223 return( this->m_MPRViewer );
226 // -------------------------------------------------------------------------
227 const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
228 GetMPRViewer( ) const
230 return( this->m_MPRViewer );
233 // -------------------------------------------------------------------------
234 void cpPlugins::Workspace::
235 SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
237 this->m_MPRViewer = wdg;
240 // -------------------------------------------------------------------------
241 bool cpPlugins::Workspace::
243 const std::string& orig_filter, const std::string& dest_filter,
244 const std::string& output_name, const std::string& input_name
248 ProcessObject* orig = this->GetFilter( orig_filter );
249 ProcessObject* dest = this->GetFilter( dest_filter );
250 if( orig == NULL || dest == NULL )
254 if( dest->SetInput( input_name, orig->GetOutput( output_name ) ) )
256 this->m_Graph->AddEdge(
257 orig_filter, dest_filter,
258 TConnection( output_name, input_name )
266 // -------------------------------------------------------------------------
267 bool cpPlugins::Workspace::
268 Connect( const OutputPort& port, const std::string& exposed_port )
270 auto i = this->m_ExposedInputPorts.find( exposed_port );
271 if( i != this->m_ExposedInputPorts.end( ) )
273 ProcessObject* filter = this->GetFilter( i->second.first );
275 return( filter->SetInput( i->second.second, port ) );
283 // -------------------------------------------------------------------------
284 bool cpPlugins::Workspace::
285 Reduce( const std::string& name )
290 // -------------------------------------------------------------------------
291 void cpPlugins::Workspace::
293 const std::string& name,
294 const std::string& filter, const std::string& filter_input
297 this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
300 // -------------------------------------------------------------------------
301 void cpPlugins::Workspace::
303 const std::string& name,
304 const std::string& filter, const std::string& filter_output
307 this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
310 // -------------------------------------------------------------------------
311 void cpPlugins::Workspace::
312 HideInputPort( const std::string& name )
314 auto i = this->m_ExposedInputPorts.find( name );
315 if( i != this->m_ExposedInputPorts.end( ) )
316 this->m_ExposedInputPorts.erase( i );
319 // -------------------------------------------------------------------------
320 void cpPlugins::Workspace::
321 HideOutputPort( const std::string& name )
323 auto i = this->m_ExposedOutputPorts.find( name );
324 if( i != this->m_ExposedOutputPorts.end( ) )
325 this->m_ExposedOutputPorts.erase( i );
328 // -------------------------------------------------------------------------
329 bool cpPlugins::Workspace::
330 RenameExposedInputPort(
331 const std::string& old_name,
332 const std::string& new_name
335 auto o = this->m_ExposedInputPorts.find( old_name );
336 auto n = this->m_ExposedInputPorts.find( new_name );
338 o != this->m_ExposedInputPorts.end( ) &&
339 n == this->m_ExposedInputPorts.end( )
342 this->m_ExposedInputPorts[ new_name ] = o->second;
343 this->m_ExposedInputPorts.erase( o );
350 // -------------------------------------------------------------------------
351 bool cpPlugins::Workspace::
352 RenameExposedOutputPort(
353 const std::string& old_name,
354 const std::string& new_name
357 auto o = this->m_ExposedOutputPorts.find( old_name );
358 auto n = this->m_ExposedOutputPorts.find( new_name );
360 o != this->m_ExposedOutputPorts.end( ) &&
361 n == this->m_ExposedOutputPorts.end( )
364 this->m_ExposedOutputPorts[ new_name ] = o->second;
365 this->m_ExposedOutputPorts.erase( o );
372 // -------------------------------------------------------------------------
373 const cpPlugins::Workspace::
374 TExposedPorts& cpPlugins::Workspace::
375 GetExposedInputPorts( ) const
377 return( this->m_ExposedInputPorts );
380 // -------------------------------------------------------------------------
381 const cpPlugins::Workspace::
382 TExposedPorts& cpPlugins::Workspace::
383 GetExposedOutputPorts( ) const
385 return( this->m_ExposedOutputPorts );
388 // -------------------------------------------------------------------------
390 OutputPort& cpPlugins::Workspace::
391 GetExposedOutput( const std::string& name )
393 static OutputPort null_port;
395 auto i = this->m_ExposedOutputPorts.find( name );
396 if( i != this->m_ExposedOutputPorts.end( ) )
398 ProcessObject* filter = this->GetFilter( i->second.first );
400 return( filter->GetOutput( i->second.second ) );
406 // -------------------------------------------------------------------------
408 OutputPort& cpPlugins::Workspace::
409 GetExposedOutput( const std::string& name ) const
411 static const OutputPort null_port;
413 auto i = this->m_ExposedOutputPorts.find( name );
414 if( i != this->m_ExposedOutputPorts.end( ) )
416 const ProcessObject* filter = this->GetFilter( i->second.first );
418 return( filter->GetOutput( i->second.second ) );
424 // -------------------------------------------------------------------------
425 void cpPlugins::Workspace::
429 std::set< std::string > sinks = this->m_Graph->GetSinks( );
432 for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
433 this->Execute( *sIt );
436 // -------------------------------------------------------------------------
437 void cpPlugins::Workspace::
438 Execute( const std::string& name )
441 ProcessObject* f = this->GetFilter( name );
444 itkGenericExceptionMacro(
445 "cpPlugins::Workspace: Vertex \"" << name << "\" is not a filter."