1 #include <cpPlugins/Workspace.h>
2 #include <cpPlugins/BaseWidget.h>
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <vtkRenderWindowInteractor.h>
6 // -------------------------------------------------------------------------
12 this->m_Graph = TGraph::New( );
15 // -------------------------------------------------------------------------
16 cpPlugins::Workspace::
19 this->m_Graph->Clear( );
22 // -------------------------------------------------------------------------
23 cpPlugins::Interface* cpPlugins::Workspace::
26 return( this->m_Interface );
29 // -------------------------------------------------------------------------
30 const cpPlugins::Interface* cpPlugins::Workspace::
33 return( this->m_Interface );
36 // -------------------------------------------------------------------------
37 void cpPlugins::Workspace::
38 SetInterface( cpPlugins::Interface* i )
40 if( this->m_Interface != i )
41 this->m_Interface = i;
44 // -------------------------------------------------------------------------
45 cpPlugins::Workspace::
46 TGraph* cpPlugins::Workspace::
49 return( this->m_Graph );
52 // -------------------------------------------------------------------------
53 const cpPlugins::Workspace::
54 TGraph* cpPlugins::Workspace::
57 return( this->m_Graph );
60 // -------------------------------------------------------------------------
61 void cpPlugins::Workspace::
64 if( this->m_Graph.IsNotNull( ) )
65 this->m_Graph->Clear( );
68 // -------------------------------------------------------------------------
69 void cpPlugins::Workspace::
74 // -------------------------------------------------------------------------
75 cpPlugins::ProcessObject* cpPlugins::Workspace::
76 GetFilter( const std::string& name )
79 dynamic_cast< ProcessObject* >(
80 this->m_Graph->GetVertex( name ).GetPointer( )
85 // -------------------------------------------------------------------------
86 const cpPlugins::ProcessObject* cpPlugins::Workspace::
87 GetFilter( const std::string& name ) const
89 const ProcessObject* f =
90 dynamic_cast< const ProcessObject* >(
91 this->m_Graph->GetVertex( name ).GetPointer( )
96 // -------------------------------------------------------------------------
97 bool cpPlugins::Workspace::
98 HasFilter( const std::string& name ) const
100 if( this->m_Graph->HasVertexIndex( name ) )
101 return( this->GetFilter( name ) != NULL );
106 // -------------------------------------------------------------------------
107 cpPlugins::ProcessObject* cpPlugins::Workspace::
109 const std::string& category,
110 const std::string& filter,
111 const std::string& name
114 if( this->m_Interface == NULL )
117 // Get or create new filter from name
118 if( !( this->m_Graph->HasVertexIndex( name ) ) )
120 ProcessObject::Pointer f =
121 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 Object::Pointer o = f.GetPointer( );
133 this->m_Graph->SetVertex( name, o );
136 return( f.GetPointer( ) );
139 return( this->GetFilter( name ) );
142 // -------------------------------------------------------------------------
143 bool cpPlugins::Workspace::
144 RenameFilter( const std::string& old_name, const std::string& new_name )
146 return( this->m_Graph->RenameVertex( old_name, new_name ) );
149 // -------------------------------------------------------------------------
150 void cpPlugins::Workspace::
151 RemoveFilter( const std::string& name )
155 // -------------------------------------------------------------------------
156 vtkRenderWindowInteractor* cpPlugins::Workspace::
157 GetSingleInteractor( )
159 return( this->m_SingleInteractor );
162 // -------------------------------------------------------------------------
163 const vtkRenderWindowInteractor* cpPlugins::Workspace::
164 GetSingleInteractor( ) const
166 return( this->m_SingleInteractor );
169 // -------------------------------------------------------------------------
170 void cpPlugins::Workspace::
171 SetSingleInteractor( vtkRenderWindowInteractor* interactor )
173 this->m_SingleInteractor = interactor;
176 // -------------------------------------------------------------------------
177 cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
180 return( this->m_MPRViewer );
183 // -------------------------------------------------------------------------
184 const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
185 GetMPRViewer( ) const
187 return( this->m_MPRViewer );
190 // -------------------------------------------------------------------------
191 void cpPlugins::Workspace::
192 SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
194 this->m_MPRViewer = wdg;
197 // -------------------------------------------------------------------------
198 bool cpPlugins::Workspace::
200 const std::string& orig_filter, const std::string& dest_filter,
201 const std::string& output_name, const std::string& input_name
205 ProcessObject* orig = this->GetFilter( orig_filter );
206 ProcessObject* dest = this->GetFilter( dest_filter );
207 if( orig == NULL || dest == NULL )
211 if( dest->SetInput( input_name, orig->GetOutput( output_name ) ) )
213 this->m_Graph->AddEdge(
214 orig_filter, dest_filter,
215 TConnection( output_name, input_name )
223 // -------------------------------------------------------------------------
224 bool cpPlugins::Workspace::
225 Connect( const OutputPort& port, const std::string& exposed_port )
227 auto i = this->m_ExposedInputPorts.find( exposed_port );
228 if( i != this->m_ExposedInputPorts.end( ) )
230 ProcessObject* filter = this->GetFilter( i->second.first );
232 return( filter->SetInput( i->second.second, port ) );
240 // -------------------------------------------------------------------------
241 bool cpPlugins::Workspace::
242 Reduce( const std::string& name )
247 // -------------------------------------------------------------------------
248 void cpPlugins::Workspace::
250 const std::string& name,
251 const std::string& filter, const std::string& filter_input
254 this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
257 // -------------------------------------------------------------------------
258 void cpPlugins::Workspace::
260 const std::string& name,
261 const std::string& filter, const std::string& filter_output
264 this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
267 // -------------------------------------------------------------------------
268 void cpPlugins::Workspace::
269 HideInputPort( const std::string& name )
271 auto i = this->m_ExposedInputPorts.find( name );
272 if( i != this->m_ExposedInputPorts.end( ) )
273 this->m_ExposedInputPorts.erase( i );
276 // -------------------------------------------------------------------------
277 void cpPlugins::Workspace::
278 HideOutputPort( const std::string& name )
280 auto i = this->m_ExposedOutputPorts.find( name );
281 if( i != this->m_ExposedOutputPorts.end( ) )
282 this->m_ExposedOutputPorts.erase( i );
285 // -------------------------------------------------------------------------
286 bool cpPlugins::Workspace::
287 RenameExposedInputPort(
288 const std::string& old_name,
289 const std::string& new_name
292 auto o = this->m_ExposedInputPorts.find( old_name );
293 auto n = this->m_ExposedInputPorts.find( new_name );
295 o != this->m_ExposedInputPorts.end( ) &&
296 n == this->m_ExposedInputPorts.end( )
299 this->m_ExposedInputPorts[ new_name ] = o->second;
300 this->m_ExposedInputPorts.erase( o );
307 // -------------------------------------------------------------------------
308 bool cpPlugins::Workspace::
309 RenameExposedOutputPort(
310 const std::string& old_name,
311 const std::string& new_name
314 auto o = this->m_ExposedOutputPorts.find( old_name );
315 auto n = this->m_ExposedOutputPorts.find( new_name );
317 o != this->m_ExposedOutputPorts.end( ) &&
318 n == this->m_ExposedOutputPorts.end( )
321 this->m_ExposedOutputPorts[ new_name ] = o->second;
322 this->m_ExposedOutputPorts.erase( o );
329 // -------------------------------------------------------------------------
330 const cpPlugins::Workspace::
331 TExposedPorts& cpPlugins::Workspace::
332 GetExposedInputPorts( ) const
334 return( this->m_ExposedInputPorts );
337 // -------------------------------------------------------------------------
338 const cpPlugins::Workspace::
339 TExposedPorts& cpPlugins::Workspace::
340 GetExposedOutputPorts( ) const
342 return( this->m_ExposedOutputPorts );
345 // -------------------------------------------------------------------------
347 OutputPort& cpPlugins::Workspace::
348 GetExposedOutput( const std::string& name )
350 static OutputPort null_port;
352 auto i = this->m_ExposedOutputPorts.find( name );
353 if( i != this->m_ExposedOutputPorts.end( ) )
355 ProcessObject* filter = this->GetFilter( i->second.first );
357 return( filter->GetOutput( i->second.second ) );
363 // -------------------------------------------------------------------------
365 OutputPort& cpPlugins::Workspace::
366 GetExposedOutput( const std::string& name ) const
368 static const OutputPort null_port;
370 auto i = this->m_ExposedOutputPorts.find( name );
371 if( i != this->m_ExposedOutputPorts.end( ) )
373 const ProcessObject* filter = this->GetFilter( i->second.first );
375 return( filter->GetOutput( i->second.second ) );
381 // -------------------------------------------------------------------------
382 std::string cpPlugins::Workspace::
386 std::set< std::string > sinks = this->m_Graph->GetSinks( );
389 std::string err = "";
390 for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
392 std::string lerr = this->Execute( *sIt );
394 err += lerr + std::string( "\n" );
400 // -------------------------------------------------------------------------
401 std::string cpPlugins::Workspace::
402 Execute( const std::string& name )
405 ProcessObject* f = this->GetFilter( name );
408 std::string( "cpPlugins::Workspace: Vertex \"" ) +
409 name + std::string( "\" is not a filter." )
412 // Execute and return
413 return( f->Update( ) );