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 = this->m_Interface->Create( category, filter );
146 if( f->IsInteractive( ) )
148 std::vector< void* > interactive_objects;
149 interactive_objects.push_back( this->m_SingleInteractor );
150 interactive_objects.push_back( this->m_MPRViewer );
151 f->SetInteractionObjects( interactive_objects );
154 f->SetPrintExecution( this->m_PrintExecution );
155 Object::Pointer o = f.GetPointer( );
156 this->m_Graph->SetVertex( name, o );
159 return( f.GetPointer( ) );
162 return( this->GetFilter( name ) );
165 // -------------------------------------------------------------------------
166 bool cpPlugins::Workspace::
167 RenameFilter( const std::string& old_name, const std::string& new_name )
169 return( this->m_Graph->RenameVertex( old_name, new_name ) );
172 // -------------------------------------------------------------------------
173 void cpPlugins::Workspace::
174 RemoveFilter( const std::string& name )
178 // -------------------------------------------------------------------------
179 void cpPlugins::Workspace::
180 SetParameter( const std::string& name, const std::string& value )
182 std::vector< std::string > tokens;
183 cpPlugins::TokenizeString( tokens, name, "@" );
185 if( this->HasFilter( tokens[ 1 ] ) )
187 auto filter = this->GetFilter( tokens[ 1 ] );
188 filter->GetParameters( )->SetString( tokens[ 0 ], value );
193 // -------------------------------------------------------------------------
194 void cpPlugins::Workspace::
195 SetPrintExecution( bool b )
197 this->m_PrintExecution = b;
198 auto vIt = this->m_Graph->BeginVertices( );
199 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
201 auto f = dynamic_cast< ProcessObject* >( vIt->second.GetPointer( ) );
203 f->SetPrintExecution( b );
208 // -------------------------------------------------------------------------
209 void cpPlugins::Workspace::
212 this->SetPrintExecution( true );
215 // -------------------------------------------------------------------------
216 void cpPlugins::Workspace::
219 this->SetPrintExecution( false );
222 // -------------------------------------------------------------------------
223 vtkRenderWindowInteractor* cpPlugins::Workspace::
224 GetSingleInteractor( )
226 return( this->m_SingleInteractor );
229 // -------------------------------------------------------------------------
230 const vtkRenderWindowInteractor* cpPlugins::Workspace::
231 GetSingleInteractor( ) const
233 return( this->m_SingleInteractor );
236 // -------------------------------------------------------------------------
237 void cpPlugins::Workspace::
238 SetSingleInteractor( vtkRenderWindowInteractor* interactor )
240 this->m_SingleInteractor = interactor;
243 // -------------------------------------------------------------------------
244 cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
247 return( this->m_MPRViewer );
250 // -------------------------------------------------------------------------
251 const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
252 GetMPRViewer( ) const
254 return( this->m_MPRViewer );
257 // -------------------------------------------------------------------------
258 void cpPlugins::Workspace::
259 SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
261 this->m_MPRViewer = wdg;
264 // -------------------------------------------------------------------------
265 bool cpPlugins::Workspace::
267 const std::string& orig_filter, const std::string& dest_filter,
268 const std::string& output_name, const std::string& input_name
272 ProcessObject* orig = this->GetFilter( orig_filter );
273 ProcessObject* dest = this->GetFilter( dest_filter );
274 if( orig == NULL || dest == NULL )
278 if( dest->SetInputPort( input_name, orig->GetOutputPort( output_name ) ) )
280 this->m_Graph->AddEdge(
281 orig_filter, dest_filter,
282 TConnection( output_name, input_name )
290 // -------------------------------------------------------------------------
291 bool cpPlugins::Workspace::
292 Connect( const OutputPort& port, const std::string& exposed_port )
294 auto i = this->m_ExposedInputPorts.find( exposed_port );
295 if( i != this->m_ExposedInputPorts.end( ) )
297 ProcessObject* filter = this->GetFilter( i->second.first );
299 return( filter->SetInputPort( i->second.second, port ) );
307 // -------------------------------------------------------------------------
308 bool cpPlugins::Workspace::
309 Reduce( const std::string& name )
314 // -------------------------------------------------------------------------
315 void cpPlugins::Workspace::
317 const std::string& name,
318 const std::string& filter, const std::string& filter_input
321 this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
324 // -------------------------------------------------------------------------
325 void cpPlugins::Workspace::
327 const std::string& name,
328 const std::string& filter, const std::string& filter_output
331 this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
334 // -------------------------------------------------------------------------
335 void cpPlugins::Workspace::
336 HideInputPort( const std::string& name )
338 auto i = this->m_ExposedInputPorts.find( name );
339 if( i != this->m_ExposedInputPorts.end( ) )
340 this->m_ExposedInputPorts.erase( i );
343 // -------------------------------------------------------------------------
344 void cpPlugins::Workspace::
345 HideOutputPort( const std::string& name )
347 auto i = this->m_ExposedOutputPorts.find( name );
348 if( i != this->m_ExposedOutputPorts.end( ) )
349 this->m_ExposedOutputPorts.erase( i );
352 // -------------------------------------------------------------------------
353 bool cpPlugins::Workspace::
354 RenameExposedInputPort(
355 const std::string& old_name,
356 const std::string& new_name
359 auto o = this->m_ExposedInputPorts.find( old_name );
360 auto n = this->m_ExposedInputPorts.find( new_name );
362 o != this->m_ExposedInputPorts.end( ) &&
363 n == this->m_ExposedInputPorts.end( )
366 this->m_ExposedInputPorts[ new_name ] = o->second;
367 this->m_ExposedInputPorts.erase( o );
374 // -------------------------------------------------------------------------
375 bool cpPlugins::Workspace::
376 RenameExposedOutputPort(
377 const std::string& old_name,
378 const std::string& new_name
381 auto o = this->m_ExposedOutputPorts.find( old_name );
382 auto n = this->m_ExposedOutputPorts.find( new_name );
384 o != this->m_ExposedOutputPorts.end( ) &&
385 n == this->m_ExposedOutputPorts.end( )
388 this->m_ExposedOutputPorts[ new_name ] = o->second;
389 this->m_ExposedOutputPorts.erase( o );
396 // -------------------------------------------------------------------------
397 const cpPlugins::Workspace::
398 TExposedPorts& cpPlugins::Workspace::
399 GetExposedInputPorts( ) const
401 return( this->m_ExposedInputPorts );
404 // -------------------------------------------------------------------------
405 const cpPlugins::Workspace::
406 TExposedPorts& cpPlugins::Workspace::
407 GetExposedOutputPorts( ) const
409 return( this->m_ExposedOutputPorts );
412 // -------------------------------------------------------------------------
414 OutputPort& cpPlugins::Workspace::
415 GetExposedOutput( const std::string& name )
417 static OutputPort null_port;
419 auto i = this->m_ExposedOutputPorts.find( name );
420 if( i != this->m_ExposedOutputPorts.end( ) )
422 ProcessObject* filter = this->GetFilter( i->second.first );
424 return( filter->GetOutputPort( i->second.second ) );
430 // -------------------------------------------------------------------------
432 OutputPort& cpPlugins::Workspace::
433 GetExposedOutput( const std::string& name ) const
435 static const OutputPort null_port;
437 auto i = this->m_ExposedOutputPorts.find( name );
438 if( i != this->m_ExposedOutputPorts.end( ) )
440 const ProcessObject* filter = this->GetFilter( i->second.first );
442 return( filter->GetOutputPort( i->second.second ) );
448 // -------------------------------------------------------------------------
449 void cpPlugins::Workspace::
453 std::set< std::string > sinks = this->m_Graph->GetSinks( );
456 for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
457 this->Execute( *sIt );
460 // -------------------------------------------------------------------------
461 void cpPlugins::Workspace::
462 Execute( const std::string& name )
465 ProcessObject* f = this->GetFilter( name );
468 itkGenericExceptionMacro(
469 "cpPlugins::Workspace: Vertex \"" << name << "\" is not a filter."