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 bool cpPlugins::Workspace::
99 HasFilter( const std::string& name ) const
101 if( this->m_Graph->HasVertexIndex( name ) )
102 return( this->GetFilter( name ) != NULL );
107 // -------------------------------------------------------------------------
108 cpPlugins::ProcessObject* cpPlugins::Workspace::
110 const std::string& category,
111 const std::string& filter,
112 const std::string& name
115 if( this->m_Interface == NULL )
118 // Get or create new filter from name
119 if( !( this->m_Graph->HasVertexIndex( name ) ) )
121 ProcessObject::Pointer f =
122 this->m_Interface->Create( category, filter );
125 if( f->IsInteractive( ) )
127 std::vector< void* > interactive_objects;
128 interactive_objects.push_back( this->m_SingleInteractor );
129 interactive_objects.push_back( this->m_MPRViewer );
130 f->SetInteractionObjects( interactive_objects );
133 f->SetPrintExecution( this->m_PrintExecution );
134 Object::Pointer o = f.GetPointer( );
135 this->m_Graph->SetVertex( name, o );
138 return( f.GetPointer( ) );
141 return( this->GetFilter( name ) );
144 // -------------------------------------------------------------------------
145 bool cpPlugins::Workspace::
146 RenameFilter( const std::string& old_name, const std::string& new_name )
148 return( this->m_Graph->RenameVertex( old_name, new_name ) );
151 // -------------------------------------------------------------------------
152 void cpPlugins::Workspace::
153 RemoveFilter( const std::string& name )
157 // -------------------------------------------------------------------------
158 void cpPlugins::Workspace::
159 SetParameter( const std::string& name, const std::string& value )
161 std::vector< std::string > tokens;
162 cpPlugins::TokenizeString( tokens, name, "@" );
164 if( this->HasFilter( tokens[ 1 ] ) )
166 auto filter = this->GetFilter( tokens[ 1 ] );
167 filter->GetParameters( )->SetString( tokens[ 0 ], value );
172 // -------------------------------------------------------------------------
173 void cpPlugins::Workspace::
174 SetPrintExecution( bool b )
176 this->m_PrintExecution = b;
177 auto vIt = this->m_Graph->BeginVertices( );
178 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
180 auto f = dynamic_cast< ProcessObject* >( vIt->second.GetPointer( ) );
182 f->SetPrintExecution( b );
187 // -------------------------------------------------------------------------
188 void cpPlugins::Workspace::
191 this->SetPrintExecution( true );
194 // -------------------------------------------------------------------------
195 void cpPlugins::Workspace::
198 this->SetPrintExecution( false );
201 // -------------------------------------------------------------------------
202 vtkRenderWindowInteractor* cpPlugins::Workspace::
203 GetSingleInteractor( )
205 return( this->m_SingleInteractor );
208 // -------------------------------------------------------------------------
209 const vtkRenderWindowInteractor* cpPlugins::Workspace::
210 GetSingleInteractor( ) const
212 return( this->m_SingleInteractor );
215 // -------------------------------------------------------------------------
216 void cpPlugins::Workspace::
217 SetSingleInteractor( vtkRenderWindowInteractor* interactor )
219 this->m_SingleInteractor = interactor;
222 // -------------------------------------------------------------------------
223 cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
226 return( this->m_MPRViewer );
229 // -------------------------------------------------------------------------
230 const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
231 GetMPRViewer( ) const
233 return( this->m_MPRViewer );
236 // -------------------------------------------------------------------------
237 void cpPlugins::Workspace::
238 SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
240 this->m_MPRViewer = wdg;
243 // -------------------------------------------------------------------------
244 bool cpPlugins::Workspace::
246 const std::string& orig_filter, const std::string& dest_filter,
247 const std::string& output_name, const std::string& input_name
251 ProcessObject* orig = this->GetFilter( orig_filter );
252 ProcessObject* dest = this->GetFilter( dest_filter );
253 if( orig == NULL || dest == NULL )
257 if( dest->SetInputPort( input_name, orig->GetOutputPort( output_name ) ) )
259 this->m_Graph->AddEdge(
260 orig_filter, dest_filter,
261 TConnection( output_name, input_name )
269 // -------------------------------------------------------------------------
270 bool cpPlugins::Workspace::
271 Connect( const OutputPort& port, const std::string& exposed_port )
273 auto i = this->m_ExposedInputPorts.find( exposed_port );
274 if( i != this->m_ExposedInputPorts.end( ) )
276 ProcessObject* filter = this->GetFilter( i->second.first );
278 return( filter->SetInputPort( i->second.second, port ) );
286 // -------------------------------------------------------------------------
287 bool cpPlugins::Workspace::
288 Reduce( const std::string& name )
293 // -------------------------------------------------------------------------
294 void cpPlugins::Workspace::
296 const std::string& name,
297 const std::string& filter, const std::string& filter_input
300 this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
303 // -------------------------------------------------------------------------
304 void cpPlugins::Workspace::
306 const std::string& name,
307 const std::string& filter, const std::string& filter_output
310 this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
313 // -------------------------------------------------------------------------
314 void cpPlugins::Workspace::
315 HideInputPort( const std::string& name )
317 auto i = this->m_ExposedInputPorts.find( name );
318 if( i != this->m_ExposedInputPorts.end( ) )
319 this->m_ExposedInputPorts.erase( i );
322 // -------------------------------------------------------------------------
323 void cpPlugins::Workspace::
324 HideOutputPort( const std::string& name )
326 auto i = this->m_ExposedOutputPorts.find( name );
327 if( i != this->m_ExposedOutputPorts.end( ) )
328 this->m_ExposedOutputPorts.erase( i );
331 // -------------------------------------------------------------------------
332 bool cpPlugins::Workspace::
333 RenameExposedInputPort(
334 const std::string& old_name,
335 const std::string& new_name
338 auto o = this->m_ExposedInputPorts.find( old_name );
339 auto n = this->m_ExposedInputPorts.find( new_name );
341 o != this->m_ExposedInputPorts.end( ) &&
342 n == this->m_ExposedInputPorts.end( )
345 this->m_ExposedInputPorts[ new_name ] = o->second;
346 this->m_ExposedInputPorts.erase( o );
353 // -------------------------------------------------------------------------
354 bool cpPlugins::Workspace::
355 RenameExposedOutputPort(
356 const std::string& old_name,
357 const std::string& new_name
360 auto o = this->m_ExposedOutputPorts.find( old_name );
361 auto n = this->m_ExposedOutputPorts.find( new_name );
363 o != this->m_ExposedOutputPorts.end( ) &&
364 n == this->m_ExposedOutputPorts.end( )
367 this->m_ExposedOutputPorts[ new_name ] = o->second;
368 this->m_ExposedOutputPorts.erase( o );
375 // -------------------------------------------------------------------------
376 const cpPlugins::Workspace::
377 TExposedPorts& cpPlugins::Workspace::
378 GetExposedInputPorts( ) const
380 return( this->m_ExposedInputPorts );
383 // -------------------------------------------------------------------------
384 const cpPlugins::Workspace::
385 TExposedPorts& cpPlugins::Workspace::
386 GetExposedOutputPorts( ) const
388 return( this->m_ExposedOutputPorts );
391 // -------------------------------------------------------------------------
393 OutputPort& cpPlugins::Workspace::
394 GetExposedOutput( const std::string& name )
396 static OutputPort null_port;
398 auto i = this->m_ExposedOutputPorts.find( name );
399 if( i != this->m_ExposedOutputPorts.end( ) )
401 ProcessObject* filter = this->GetFilter( i->second.first );
403 return( filter->GetOutputPort( i->second.second ) );
409 // -------------------------------------------------------------------------
411 OutputPort& cpPlugins::Workspace::
412 GetExposedOutput( const std::string& name ) const
414 static const OutputPort null_port;
416 auto i = this->m_ExposedOutputPorts.find( name );
417 if( i != this->m_ExposedOutputPorts.end( ) )
419 const ProcessObject* filter = this->GetFilter( i->second.first );
421 return( filter->GetOutputPort( i->second.second ) );
427 // -------------------------------------------------------------------------
428 void cpPlugins::Workspace::
432 std::set< std::string > sinks = this->m_Graph->GetSinks( );
435 for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
436 this->Execute( *sIt );
439 // -------------------------------------------------------------------------
440 void cpPlugins::Workspace::
441 Execute( const std::string& name )
444 ProcessObject* f = this->GetFilter( name );
447 itkGenericExceptionMacro(
448 "cpPlugins::Workspace: Vertex \"" << name << "\" is not a filter."