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 = 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 f->SetPrintExecution( this->m_PrintExecution );
133 Object::Pointer o = f.GetPointer( );
134 this->m_Graph->SetVertex( name, o );
137 return( f.GetPointer( ) );
140 return( this->GetFilter( name ) );
143 // -------------------------------------------------------------------------
144 bool cpPlugins::Workspace::
145 RenameFilter( const std::string& old_name, const std::string& new_name )
147 return( this->m_Graph->RenameVertex( old_name, new_name ) );
150 // -------------------------------------------------------------------------
151 void cpPlugins::Workspace::
152 RemoveFilter( const std::string& name )
156 // -------------------------------------------------------------------------
157 void cpPlugins::Workspace::
158 SetParameter( const std::string& name, const std::string& value )
160 std::vector< std::string > tokens;
161 cpPlugins::TokenizeString( tokens, name, "@" );
163 if( this->HasFilter( tokens[ 1 ] ) )
165 auto filter = this->GetFilter( tokens[ 1 ] );
166 filter->GetParameters( )->SetString( tokens[ 0 ], value );
171 // -------------------------------------------------------------------------
172 void cpPlugins::Workspace::
173 SetPrintExecution( bool b )
175 this->m_PrintExecution = b;
176 auto vIt = this->m_Graph->BeginVertices( );
177 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
179 auto f = dynamic_cast< ProcessObject* >( vIt->second.GetPointer( ) );
181 f->SetPrintExecution( b );
186 // -------------------------------------------------------------------------
187 void cpPlugins::Workspace::
190 this->SetPrintExecution( true );
193 // -------------------------------------------------------------------------
194 void cpPlugins::Workspace::
197 this->SetPrintExecution( false );
200 // -------------------------------------------------------------------------
201 vtkRenderWindowInteractor* cpPlugins::Workspace::
202 GetSingleInteractor( )
204 return( this->m_SingleInteractor );
207 // -------------------------------------------------------------------------
208 const vtkRenderWindowInteractor* cpPlugins::Workspace::
209 GetSingleInteractor( ) const
211 return( this->m_SingleInteractor );
214 // -------------------------------------------------------------------------
215 void cpPlugins::Workspace::
216 SetSingleInteractor( vtkRenderWindowInteractor* interactor )
218 this->m_SingleInteractor = interactor;
221 // -------------------------------------------------------------------------
222 cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
225 return( this->m_MPRViewer );
228 // -------------------------------------------------------------------------
229 const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
230 GetMPRViewer( ) const
232 return( this->m_MPRViewer );
235 // -------------------------------------------------------------------------
236 void cpPlugins::Workspace::
237 SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
239 this->m_MPRViewer = wdg;
242 // -------------------------------------------------------------------------
243 bool cpPlugins::Workspace::
245 const std::string& orig_filter, const std::string& dest_filter,
246 const std::string& output_name, const std::string& input_name
250 ProcessObject* orig = this->GetFilter( orig_filter );
251 ProcessObject* dest = this->GetFilter( dest_filter );
252 if( orig == NULL || dest == NULL )
256 if( dest->SetInputPort( input_name, orig->GetOutputPort( output_name ) ) )
258 this->m_Graph->AddEdge(
259 orig_filter, dest_filter,
260 TConnection( output_name, input_name )
268 // -------------------------------------------------------------------------
269 bool cpPlugins::Workspace::
270 Connect( const OutputPort& port, const std::string& exposed_port )
272 auto i = this->m_ExposedInputPorts.find( exposed_port );
273 if( i != this->m_ExposedInputPorts.end( ) )
275 ProcessObject* filter = this->GetFilter( i->second.first );
277 return( filter->SetInputPort( i->second.second, port ) );
285 // -------------------------------------------------------------------------
286 bool cpPlugins::Workspace::
287 Reduce( const std::string& name )
292 // -------------------------------------------------------------------------
293 void cpPlugins::Workspace::
295 const std::string& name,
296 const std::string& filter, const std::string& filter_input
299 this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
302 // -------------------------------------------------------------------------
303 void cpPlugins::Workspace::
305 const std::string& name,
306 const std::string& filter, const std::string& filter_output
309 this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
312 // -------------------------------------------------------------------------
313 void cpPlugins::Workspace::
314 HideInputPort( const std::string& name )
316 auto i = this->m_ExposedInputPorts.find( name );
317 if( i != this->m_ExposedInputPorts.end( ) )
318 this->m_ExposedInputPorts.erase( i );
321 // -------------------------------------------------------------------------
322 void cpPlugins::Workspace::
323 HideOutputPort( const std::string& name )
325 auto i = this->m_ExposedOutputPorts.find( name );
326 if( i != this->m_ExposedOutputPorts.end( ) )
327 this->m_ExposedOutputPorts.erase( i );
330 // -------------------------------------------------------------------------
331 bool cpPlugins::Workspace::
332 RenameExposedInputPort(
333 const std::string& old_name,
334 const std::string& new_name
337 auto o = this->m_ExposedInputPorts.find( old_name );
338 auto n = this->m_ExposedInputPorts.find( new_name );
340 o != this->m_ExposedInputPorts.end( ) &&
341 n == this->m_ExposedInputPorts.end( )
344 this->m_ExposedInputPorts[ new_name ] = o->second;
345 this->m_ExposedInputPorts.erase( o );
352 // -------------------------------------------------------------------------
353 bool cpPlugins::Workspace::
354 RenameExposedOutputPort(
355 const std::string& old_name,
356 const std::string& new_name
359 auto o = this->m_ExposedOutputPorts.find( old_name );
360 auto n = this->m_ExposedOutputPorts.find( new_name );
362 o != this->m_ExposedOutputPorts.end( ) &&
363 n == this->m_ExposedOutputPorts.end( )
366 this->m_ExposedOutputPorts[ new_name ] = o->second;
367 this->m_ExposedOutputPorts.erase( o );
374 // -------------------------------------------------------------------------
375 const cpPlugins::Workspace::
376 TExposedPorts& cpPlugins::Workspace::
377 GetExposedInputPorts( ) const
379 return( this->m_ExposedInputPorts );
382 // -------------------------------------------------------------------------
383 const cpPlugins::Workspace::
384 TExposedPorts& cpPlugins::Workspace::
385 GetExposedOutputPorts( ) const
387 return( this->m_ExposedOutputPorts );
390 // -------------------------------------------------------------------------
392 OutputPort& cpPlugins::Workspace::
393 GetExposedOutput( const std::string& name )
395 static OutputPort null_port;
397 auto i = this->m_ExposedOutputPorts.find( name );
398 if( i != this->m_ExposedOutputPorts.end( ) )
400 ProcessObject* filter = this->GetFilter( i->second.first );
402 return( filter->GetOutputPort( i->second.second ) );
408 // -------------------------------------------------------------------------
410 OutputPort& cpPlugins::Workspace::
411 GetExposedOutput( const std::string& name ) const
413 static const OutputPort null_port;
415 auto i = this->m_ExposedOutputPorts.find( name );
416 if( i != this->m_ExposedOutputPorts.end( ) )
418 const ProcessObject* filter = this->GetFilter( i->second.first );
420 return( filter->GetOutputPort( i->second.second ) );
426 // -------------------------------------------------------------------------
427 void cpPlugins::Workspace::
431 std::set< std::string > sinks = this->m_Graph->GetSinks( );
434 for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
435 this->Execute( *sIt );
438 // -------------------------------------------------------------------------
439 void cpPlugins::Workspace::
440 Execute( const std::string& name )
443 ProcessObject* f = this->GetFilter( name );
446 itkGenericExceptionMacro(
447 "cpPlugins::Workspace: Vertex \"" << name << "\" is not a filter."