1 #include <plugins/Plugins/ExtractPathFromMinimumSpanningTree.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4 #include <fpa_DataObjects.h>
7 #include <vtkFloatArray.h>
8 #include <vtkPointData.h>
11 // -------------------------------------------------------------------------
12 fpaPlugins::ExtractPathFromMinimumSpanningTree::
13 ExtractPathFromMinimumSpanningTree( )
16 typedef cpPlugins::BaseObjects::DataObject _TData;
17 typedef cpPlugins::DataObjects::Image _TMST;
18 typedef cpPlugins::DataObjects::Mesh _TMesh;
20 this->_ConfigureInput< _TMST >( "MST", true, false );
21 this->_ConfigureInput< _TData >( "Seeds", true, false );
22 this->_ConfigureOutput< _TMesh >( "Paths" );
24 this->m_Parameters.ConfigureAsUintList( "Indices" );
27 // -------------------------------------------------------------------------
28 fpaPlugins::ExtractPathFromMinimumSpanningTree::
29 ~ExtractPathFromMinimumSpanningTree( )
33 // -------------------------------------------------------------------------
34 void fpaPlugins::ExtractPathFromMinimumSpanningTree::
37 typedef fpa::Image::MinimumSpanningTree< 2 > _TMST2;
38 typedef fpa::Image::MinimumSpanningTree< 3 > _TMST3;
40 auto mst2 = this->GetInputData< _TMST2 >( "MST" );
41 auto mst3 = this->GetInputData< _TMST3 >( "MST" );
42 if ( mst2 != NULL ) this->_GD0( mst2 );
43 else if( mst3 != NULL ) this->_GD0( mst3 );
44 else this->_Error( "Invalid input spanning tree." );
47 // -------------------------------------------------------------------------
48 template< class _TMST >
49 void fpaPlugins::ExtractPathFromMinimumSpanningTree::
52 typedef typename _TMST::IndexType _TIndex;
55 std::vector< _TIndex > seeds;
56 auto points = this->GetInputData< vtkPolyData >( "Seeds" );
59 typename _TMST::PointType pnt;
60 typename _TMST::IndexType idx;
62 ( _TMST::ImageDimension < 3 )? _TMST::ImageDimension: 3;
63 for( unsigned int i = 0; i < points->GetNumberOfPoints( ); ++i )
66 points->GetPoint( i, buf );
68 for( unsigned int d = 0; d < dim; ++d )
70 if( mst->TransformPhysicalPointToIndex( pnt, idx ) )
71 seeds.push_back( idx );
78 auto mesh = this->_CreateVTK< vtkPolyData >( );
79 mesh->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
80 mesh->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
81 mesh->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
82 mesh->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
83 mesh->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
85 vtkSmartPointer< vtkFloatArray > data =
86 vtkSmartPointer< vtkFloatArray >::New( );
87 data->SetNumberOfComponents( 1 );
88 mesh->GetPointData( )->SetScalars( data );
92 auto indices = this->m_Parameters.GetUintList( "Indices" );
93 for( unsigned int i = 0; i < indices.size( ); i += 2 )
95 if( i < indices.size( ) - 1 )
97 _TIndex a = seeds[ indices[ i ] ];
98 _TIndex b = seeds[ indices[ i + 1 ] ];
100 auto path = mst->GetEuclideanPath( a, b );
101 for( unsigned long j = 0; j < path.size( ); ++j )
104 if( _TMST::ImageDimension == 2 )
105 mesh->GetPoints( )->InsertNextPoint( p[ 0 ], p[ 1 ], 0 );
106 else if( _TMST::ImageDimension == 3 )
107 mesh->GetPoints( )->InsertNextPoint( p[ 0 ], p[ 1 ], p[ 2 ] );
110 mesh->GetLines( )->InsertNextCell( 2 );
111 mesh->GetLines( )->InsertCellPoint( j - 1 );
112 mesh->GetLines( )->InsertCellPoint( j );
121 this->GetOutput( "Paths" )->SetVTK( mesh );