1 #include "MinimumSpanningTreeToMesh.h"
3 #include <cpPlugins/Interface/Image.h>
4 #include <cpPlugins/Interface/Mesh.h>
5 #include <cpPlugins/Interface/PointList.h>
6 #include <fpaPlugins/MinimumSpanningTree.h>
7 #include <fpa/Base/MinimumSpanningTree.h>
9 #include <vtkCellArray.h>
10 #include <vtkPoints.h>
11 #include <vtkPolyData.h>
13 // -------------------------------------------------------------------------
14 fpaPlugins::MinimumSpanningTreeToMesh::
15 MinimumSpanningTreeToMesh( )
18 this->_AddInput( "Input" );
19 this->_AddInput( "Seeds" );
20 this->_AddInput( "ReferenceImage" );
21 this->_AddOutput< cpPlugins::Interface::Mesh >( "Output" );
23 this->m_Parameters->ConfigureAsUint( "Kernel" );
24 this->m_Parameters->SetUint( "Kernel", 0 );
27 // -------------------------------------------------------------------------
28 fpaPlugins::MinimumSpanningTreeToMesh::
29 ~MinimumSpanningTreeToMesh( )
33 // -------------------------------------------------------------------------
34 std::string fpaPlugins::MinimumSpanningTreeToMesh::
37 typedef itk::ImageBase< 2 > _2D;
38 typedef itk::ImageBase< 3 > _3D;
40 auto input = this->GetInputData( "ReferenceImage" );
41 _2D* im2d = input->GetITK< _2D >( );
42 _3D* im3d = input->GetITK< _3D >( );
44 return( this->_GD0( im2d ) );
45 else if( im3d != NULL )
46 return( this->_GD0( im3d ) );
48 return( "fpaPlugins::MinimumSpanningTreeToMesh: Reference image type not supported." );
51 // -------------------------------------------------------------------------
53 std::string fpaPlugins::MinimumSpanningTreeToMesh::
57 typedef typename I::IndexType _V;
58 typedef typename I::PointType _P;
59 typedef itk::Functor::IndexLexicographicCompare< I::ImageDimension > _VC;
60 typedef fpa::Base::MinimumSpanningTree< _V, _VC > _MST;
63 auto mst_wrapper = this->GetInputData( "Input" );
64 _MST* mst = mst_wrapper->GetITK< _MST >( );
66 return( "fpaPlugins::MinimumSpanningTreeToMesh: Input MST type not supported." );
67 auto seeds = dynamic_cast< cpPlugins::Interface::PointList* >(
68 this->GetInputData( "Seeds" )
71 return( "fpaPlugins::MinimumSpanningTreeToMesh: Not valid seeds." );
72 if( seeds->GetNumberOfPoints( ) < 2 )
73 return( "fpaPlugins::MinimumSpanningTreeToMesh: Not enough seeds (<2)." );
76 auto out = this->GetOutputData( "Output" );
77 vtkSmartPointer< vtkPolyData > pd = out->GetVTK< vtkPolyData >( );
78 if( pd.GetPointer( ) == NULL )
80 pd = vtkPolyData::New( );
85 _P pa = seeds->GetPoint< _P >( 0 );
86 _P pb = seeds->GetPoint< _P >( 1 );
88 image->TransformPhysicalPointToIndex( pa, va );
89 image->TransformPhysicalPointToIndex( pb, vb );
91 std::vector< _P > path =
92 mst->GetPathFromImage(
94 this->m_Parameters->GetUint( "Kernel" )
97 vtkSmartPointer< vtkPoints > points =
98 vtkSmartPointer< vtkPoints >::New( );
99 vtkSmartPointer< vtkCellArray > array =
100 vtkSmartPointer< vtkCellArray >::New( );
101 for( unsigned int i = 0; i < path.size( ); ++i )
103 if( I::ImageDimension == 2 )
104 points->InsertNextPoint( path[ i ][ 0 ], path[ i ][ 1 ], 0 );
105 else if( I::ImageDimension == 3 )
106 points->InsertNextPoint(
107 path[ i ][ 0 ], path[ i ][ 1 ], path[ i ][ 2 ]
110 points->InsertNextPoint( 0, 0, 0 );
113 array->InsertNextCell( 2 );
114 array->InsertCellPoint( i - 1 );
115 array->InsertCellPoint( i );
120 pd->SetPoints( points );
121 pd->SetLines( array );
125 return( "Not yet implemented." );