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 void cpPlugins::Workspace::
176 RemoveFilter( const std::string& name )
180 // -------------------------------------------------------------------------
181 void cpPlugins::Workspace::
182 SetParameter( const std::string& name, const std::string& value )
184 std::vector< std::string > tokens;
185 cpPlugins::TokenizeString( tokens, name, "@" );
187 if( this->HasFilter( tokens[ 1 ] ) )
189 auto filter = this->GetFilter( tokens[ 1 ] );
190 filter->GetParameters( )->SetString( tokens[ 0 ], value );
195 // -------------------------------------------------------------------------
196 void cpPlugins::Workspace::
197 SetPrintExecution( bool b )
199 this->m_PrintExecution = b;
200 auto vIt = this->m_Graph->BeginVertices( );
201 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
203 auto f = dynamic_cast< ProcessObject* >( vIt->second.GetPointer( ) );
205 f->SetPrintExecution( b );
210 // -------------------------------------------------------------------------
211 void cpPlugins::Workspace::
214 this->SetPrintExecution( true );
217 // -------------------------------------------------------------------------
218 void cpPlugins::Workspace::
221 this->SetPrintExecution( false );
224 // -------------------------------------------------------------------------
225 vtkRenderWindowInteractor* cpPlugins::Workspace::
226 GetSingleInteractor( )
228 return( this->m_SingleInteractor );
231 // -------------------------------------------------------------------------
232 const vtkRenderWindowInteractor* cpPlugins::Workspace::
233 GetSingleInteractor( ) const
235 return( this->m_SingleInteractor );
238 // -------------------------------------------------------------------------
239 void cpPlugins::Workspace::
240 SetSingleInteractor( vtkRenderWindowInteractor* interactor )
242 this->m_SingleInteractor = interactor;
245 // -------------------------------------------------------------------------
246 cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
249 return( this->m_MPRViewer );
252 // -------------------------------------------------------------------------
253 const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
254 GetMPRViewer( ) const
256 return( this->m_MPRViewer );
259 // -------------------------------------------------------------------------
260 void cpPlugins::Workspace::
261 SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
263 this->m_MPRViewer = wdg;
266 // -------------------------------------------------------------------------
267 bool cpPlugins::Workspace::
269 const std::string& orig_filter, const std::string& dest_filter,
270 const std::string& output_name, const std::string& input_name
274 ProcessObject* orig = this->GetFilter( orig_filter );
275 ProcessObject* dest = this->GetFilter( dest_filter );
276 if( orig == NULL || dest == NULL )
280 if( dest->SetInputPort( input_name, orig->GetOutputPort( output_name ) ) )
282 this->m_Graph->AddEdge(
283 orig_filter, dest_filter,
284 TConnection( output_name, input_name )
292 // -------------------------------------------------------------------------
293 bool cpPlugins::Workspace::
294 Connect( const OutputPort& port, const std::string& exposed_port )
296 auto i = this->m_ExposedInputPorts.find( exposed_port );
297 if( i != this->m_ExposedInputPorts.end( ) )
299 ProcessObject* filter = this->GetFilter( i->second.first );
301 return( filter->SetInputPort( i->second.second, port ) );
309 // -------------------------------------------------------------------------
310 bool cpPlugins::Workspace::
311 Reduce( const std::string& name )
316 // -------------------------------------------------------------------------
317 void cpPlugins::Workspace::
319 const std::string& name,
320 const std::string& filter, const std::string& filter_input
323 this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
326 // -------------------------------------------------------------------------
327 void cpPlugins::Workspace::
329 const std::string& name,
330 const std::string& filter, const std::string& filter_output
333 this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
336 // -------------------------------------------------------------------------
337 void cpPlugins::Workspace::
338 HideInputPort( const std::string& name )
340 auto i = this->m_ExposedInputPorts.find( name );
341 if( i != this->m_ExposedInputPorts.end( ) )
342 this->m_ExposedInputPorts.erase( i );
345 // -------------------------------------------------------------------------
346 void cpPlugins::Workspace::
347 HideOutputPort( const std::string& name )
349 auto i = this->m_ExposedOutputPorts.find( name );
350 if( i != this->m_ExposedOutputPorts.end( ) )
351 this->m_ExposedOutputPorts.erase( i );
354 // -------------------------------------------------------------------------
355 bool cpPlugins::Workspace::
356 RenameExposedInputPort(
357 const std::string& old_name,
358 const std::string& new_name
361 auto o = this->m_ExposedInputPorts.find( old_name );
362 auto n = this->m_ExposedInputPorts.find( new_name );
364 o != this->m_ExposedInputPorts.end( ) &&
365 n == this->m_ExposedInputPorts.end( )
368 this->m_ExposedInputPorts[ new_name ] = o->second;
369 this->m_ExposedInputPorts.erase( o );
376 // -------------------------------------------------------------------------
377 bool cpPlugins::Workspace::
378 RenameExposedOutputPort(
379 const std::string& old_name,
380 const std::string& new_name
383 auto o = this->m_ExposedOutputPorts.find( old_name );
384 auto n = this->m_ExposedOutputPorts.find( new_name );
386 o != this->m_ExposedOutputPorts.end( ) &&
387 n == this->m_ExposedOutputPorts.end( )
390 this->m_ExposedOutputPorts[ new_name ] = o->second;
391 this->m_ExposedOutputPorts.erase( o );
398 // -------------------------------------------------------------------------
399 const cpPlugins::Workspace::
400 TExposedPorts& cpPlugins::Workspace::
401 GetExposedInputPorts( ) const
403 return( this->m_ExposedInputPorts );
406 // -------------------------------------------------------------------------
407 const cpPlugins::Workspace::
408 TExposedPorts& cpPlugins::Workspace::
409 GetExposedOutputPorts( ) const
411 return( this->m_ExposedOutputPorts );
414 // -------------------------------------------------------------------------
416 OutputPort& cpPlugins::Workspace::
417 GetExposedOutput( const std::string& name )
419 static OutputPort null_port;
421 auto i = this->m_ExposedOutputPorts.find( name );
422 if( i != this->m_ExposedOutputPorts.end( ) )
424 ProcessObject* filter = this->GetFilter( i->second.first );
426 return( filter->GetOutputPort( i->second.second ) );
432 // -------------------------------------------------------------------------
434 OutputPort& cpPlugins::Workspace::
435 GetExposedOutput( const std::string& name ) const
437 static const OutputPort null_port;
439 auto i = this->m_ExposedOutputPorts.find( name );
440 if( i != this->m_ExposedOutputPorts.end( ) )
442 const ProcessObject* filter = this->GetFilter( i->second.first );
444 return( filter->GetOutputPort( i->second.second ) );
450 // -------------------------------------------------------------------------
451 void cpPlugins::Workspace::
455 std::set< std::string > sinks = this->m_Graph->GetSinks( );
458 for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
459 this->Execute( *sIt );
462 // -------------------------------------------------------------------------
463 void cpPlugins::Workspace::
464 Execute( const std::string& name )
467 ProcessObject* f = this->GetFilter( name );
470 itkGenericExceptionMacro(
471 "cpPlugins::Workspace: Vertex \"" << name << "\" is not a filter."