1 #include <cpPlugins/Workspace.h>
2 #include <cpPlugins/BaseWidget.h>
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <vtkRenderWindowInteractor.h>
6 // -------------------------------------------------------------------------
10 m_PrintExecution( false ),
13 this->m_Graph = TGraph::New( );
16 // -------------------------------------------------------------------------
17 cpPlugins::Workspace::
20 this->m_Graph->Clear( );
23 // -------------------------------------------------------------------------
24 cpPlugins::Interface* cpPlugins::Workspace::
27 return( this->m_Interface );
30 // -------------------------------------------------------------------------
31 const cpPlugins::Interface* cpPlugins::Workspace::
34 return( this->m_Interface );
37 // -------------------------------------------------------------------------
38 void cpPlugins::Workspace::
39 SetInterface( cpPlugins::Interface* i )
41 if( this->m_Interface != i )
42 this->m_Interface = i;
45 // -------------------------------------------------------------------------
46 cpPlugins::Workspace::
47 TGraph* cpPlugins::Workspace::
50 return( this->m_Graph );
53 // -------------------------------------------------------------------------
54 const cpPlugins::Workspace::
55 TGraph* cpPlugins::Workspace::
58 return( this->m_Graph );
61 // -------------------------------------------------------------------------
62 void cpPlugins::Workspace::
65 if( this->m_Graph.IsNotNull( ) )
66 this->m_Graph->Clear( );
69 // -------------------------------------------------------------------------
70 void cpPlugins::Workspace::
75 // -------------------------------------------------------------------------
76 cpPlugins::ProcessObject* cpPlugins::Workspace::
77 GetFilter( const std::string& name )
80 dynamic_cast< ProcessObject* >(
81 this->m_Graph->GetVertex( name ).GetPointer( )
86 // -------------------------------------------------------------------------
87 const cpPlugins::ProcessObject* cpPlugins::Workspace::
88 GetFilter( const std::string& name ) const
90 const ProcessObject* f =
91 dynamic_cast< const ProcessObject* >(
92 this->m_Graph->GetVertex( name ).GetPointer( )
97 // -------------------------------------------------------------------------
98 cpPlugins::DataObject* cpPlugins::Workspace::
99 GetOutput( const std::string& filter, const std::string& output )
101 auto f = this->GetFilter( filter );
103 return( f->GetOutput( output ) );
108 // -------------------------------------------------------------------------
109 const cpPlugins::DataObject* cpPlugins::Workspace::
110 GetOutput( const std::string& filter, const std::string& output ) const
112 auto f = this->GetFilter( filter );
114 return( f->GetOutput( output ) );
119 // -------------------------------------------------------------------------
120 bool cpPlugins::Workspace::
121 HasFilter( const std::string& name ) const
123 if( this->m_Graph->HasVertexIndex( name ) )
124 return( this->GetFilter( name ) != NULL );
129 // -------------------------------------------------------------------------
130 cpPlugins::ProcessObject* cpPlugins::Workspace::
132 const std::string& category,
133 const std::string& filter,
134 const std::string& name
137 if( this->m_Interface == NULL )
140 // Get or create new filter from name
141 if( !( this->m_Graph->HasVertexIndex( name ) ) )
143 ProcessObject::Pointer f =
144 this->m_Interface->CreateProcessObject( category, filter );
147 if( f->IsInteractive( ) )
149 std::vector< void* > interactive_objects;
150 interactive_objects.push_back( this->m_SingleInteractor );
151 interactive_objects.push_back( this->m_MPRViewer );
152 f->SetInteractionObjects( interactive_objects );
155 f->SetPrintExecution( this->m_PrintExecution );
156 Object::Pointer o = f.GetPointer( );
157 this->m_Graph->SetVertex( name, o );
161 return( f.GetPointer( ) );
164 return( this->GetFilter( name ) );
167 // -------------------------------------------------------------------------
168 bool cpPlugins::Workspace::
169 RenameFilter( const std::string& old_name, const std::string& new_name )
171 return( this->m_Graph->RenameVertex( old_name, new_name ) );
174 // -------------------------------------------------------------------------
175 bool cpPlugins::Workspace::
176 RemoveFilter( const std::string& name )
178 auto filter = this->GetFilter( name );
181 auto vIt = this->m_Graph->BeginVertices( );
182 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
184 if( vIt->first != name )
186 if( this->m_Graph->HasEdge( name, vIt->first ) )
188 auto edges = this->m_Graph->GetEdges( name, vIt->first );
189 auto other = this->GetFilter( vIt->first );
190 for( auto eIt = edges.begin( ); eIt != edges.end( ); ++eIt )
191 other->SetInput( eIt->first, ( DataObject* )( NULL ) );
195 if( this->m_Graph->HasEdge( vIt->first, name ) )
197 auto edges = this->m_Graph->GetEdges( vIt->first, name );
198 for( auto eIt = edges.begin( ); eIt != edges.end( ); ++eIt )
199 filter->SetInput( eIt->first, ( DataObject* )( NULL ) );
206 this->m_Graph->RemoveVertex( name );
213 // -------------------------------------------------------------------------
214 void cpPlugins::Workspace::
215 SetParameter( const std::string& name, const std::string& value )
217 std::vector< std::string > tokens;
218 cpPlugins::TokenizeString( tokens, name, "@" );
220 if( this->HasFilter( tokens[ 1 ] ) )
222 auto filter = this->GetFilter( tokens[ 1 ] );
223 filter->GetParameters( )->SetString( tokens[ 0 ], value );
228 // -------------------------------------------------------------------------
229 void cpPlugins::Workspace::
230 SetPrintExecution( bool b )
232 this->m_PrintExecution = b;
233 auto vIt = this->m_Graph->BeginVertices( );
234 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
236 auto f = dynamic_cast< ProcessObject* >( vIt->second.GetPointer( ) );
238 f->SetPrintExecution( b );
243 // -------------------------------------------------------------------------
244 void cpPlugins::Workspace::
247 this->SetPrintExecution( true );
250 // -------------------------------------------------------------------------
251 void cpPlugins::Workspace::
254 this->SetPrintExecution( false );
257 // -------------------------------------------------------------------------
258 vtkRenderWindowInteractor* cpPlugins::Workspace::
259 GetSingleInteractor( )
261 return( this->m_SingleInteractor );
264 // -------------------------------------------------------------------------
265 const vtkRenderWindowInteractor* cpPlugins::Workspace::
266 GetSingleInteractor( ) const
268 return( this->m_SingleInteractor );
271 // -------------------------------------------------------------------------
272 void cpPlugins::Workspace::
273 SetSingleInteractor( vtkRenderWindowInteractor* interactor )
275 this->m_SingleInteractor = interactor;
278 // -------------------------------------------------------------------------
279 cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
282 return( this->m_MPRViewer );
285 // -------------------------------------------------------------------------
286 const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
287 GetMPRViewer( ) const
289 return( this->m_MPRViewer );
292 // -------------------------------------------------------------------------
293 void cpPlugins::Workspace::
294 SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
296 this->m_MPRViewer = wdg;
299 // -------------------------------------------------------------------------
300 bool cpPlugins::Workspace::
302 const std::string& orig_filter, const std::string& dest_filter,
303 const std::string& output_name, const std::string& input_name
307 ProcessObject* orig = this->GetFilter( orig_filter );
308 ProcessObject* dest = this->GetFilter( dest_filter );
309 if( orig == NULL || dest == NULL )
313 if( dest->SetInputPort( input_name, orig->GetOutputPort( output_name ) ) )
315 this->m_Graph->AddEdge(
316 orig_filter, dest_filter,
317 TConnection( output_name, input_name )
325 // -------------------------------------------------------------------------
326 bool cpPlugins::Workspace::
327 Connect( const OutputPort& port, const std::string& exposed_port )
329 auto i = this->m_ExposedInputPorts.find( exposed_port );
330 if( i != this->m_ExposedInputPorts.end( ) )
332 ProcessObject* filter = this->GetFilter( i->second.first );
334 return( filter->SetInputPort( i->second.second, port ) );
342 // -------------------------------------------------------------------------
343 bool cpPlugins::Workspace::
345 const std::string& orig_filter, const std::string& dest_filter,
346 const std::string& output_name, const std::string& input_name
350 ProcessObject* orig = this->GetFilter( orig_filter );
351 ProcessObject* dest = this->GetFilter( dest_filter );
352 if( orig == NULL || dest == NULL )
355 // Real disconnection
356 if( dest->SetInput( input_name, ( DataObject* )( NULL ) ) )
358 this->m_Graph->RemoveEdge(
359 orig_filter, dest_filter,
360 TConnection( output_name, input_name )
368 // -------------------------------------------------------------------------
369 bool cpPlugins::Workspace::
370 Reduce( const std::string& name )
375 // -------------------------------------------------------------------------
376 void cpPlugins::Workspace::
378 const std::string& name,
379 const std::string& filter, const std::string& filter_input
382 this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
385 // -------------------------------------------------------------------------
386 void cpPlugins::Workspace::
388 const std::string& name,
389 const std::string& filter, const std::string& filter_output
392 this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
395 // -------------------------------------------------------------------------
396 void cpPlugins::Workspace::
397 HideInputPort( const std::string& name )
399 auto i = this->m_ExposedInputPorts.find( name );
400 if( i != this->m_ExposedInputPorts.end( ) )
401 this->m_ExposedInputPorts.erase( i );
404 // -------------------------------------------------------------------------
405 void cpPlugins::Workspace::
406 HideOutputPort( const std::string& name )
408 auto i = this->m_ExposedOutputPorts.find( name );
409 if( i != this->m_ExposedOutputPorts.end( ) )
410 this->m_ExposedOutputPorts.erase( i );
413 // -------------------------------------------------------------------------
414 bool cpPlugins::Workspace::
415 RenameExposedInputPort(
416 const std::string& old_name,
417 const std::string& new_name
420 auto o = this->m_ExposedInputPorts.find( old_name );
421 auto n = this->m_ExposedInputPorts.find( new_name );
423 o != this->m_ExposedInputPorts.end( ) &&
424 n == this->m_ExposedInputPorts.end( )
427 this->m_ExposedInputPorts[ new_name ] = o->second;
428 this->m_ExposedInputPorts.erase( o );
435 // -------------------------------------------------------------------------
436 bool cpPlugins::Workspace::
437 RenameExposedOutputPort(
438 const std::string& old_name,
439 const std::string& new_name
442 auto o = this->m_ExposedOutputPorts.find( old_name );
443 auto n = this->m_ExposedOutputPorts.find( new_name );
445 o != this->m_ExposedOutputPorts.end( ) &&
446 n == this->m_ExposedOutputPorts.end( )
449 this->m_ExposedOutputPorts[ new_name ] = o->second;
450 this->m_ExposedOutputPorts.erase( o );
457 // -------------------------------------------------------------------------
458 const cpPlugins::Workspace::
459 TExposedPorts& cpPlugins::Workspace::
460 GetExposedInputPorts( ) const
462 return( this->m_ExposedInputPorts );
465 // -------------------------------------------------------------------------
466 const cpPlugins::Workspace::
467 TExposedPorts& cpPlugins::Workspace::
468 GetExposedOutputPorts( ) const
470 return( this->m_ExposedOutputPorts );
473 // -------------------------------------------------------------------------
475 OutputPort& cpPlugins::Workspace::
476 GetExposedOutput( const std::string& name )
478 static OutputPort null_port;
480 auto i = this->m_ExposedOutputPorts.find( name );
481 if( i != this->m_ExposedOutputPorts.end( ) )
483 ProcessObject* filter = this->GetFilter( i->second.first );
485 return( filter->GetOutputPort( i->second.second ) );
491 // -------------------------------------------------------------------------
493 OutputPort& cpPlugins::Workspace::
494 GetExposedOutput( const std::string& name ) const
496 static const OutputPort null_port;
498 auto i = this->m_ExposedOutputPorts.find( name );
499 if( i != this->m_ExposedOutputPorts.end( ) )
501 const ProcessObject* filter = this->GetFilter( i->second.first );
503 return( filter->GetOutputPort( i->second.second ) );
509 // -------------------------------------------------------------------------
510 void cpPlugins::Workspace::
514 std::set< std::string > sinks = this->m_Graph->GetSinks( );
517 for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
518 this->Execute( *sIt );
521 // -------------------------------------------------------------------------
522 void cpPlugins::Workspace::
523 Execute( const std::string& name )
526 ProcessObject* f = this->GetFilter( name );
529 itkGenericExceptionMacro(
530 "cpPlugins::Workspace: Vertex \"" << name << "\" is not a filter."