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;
41 this->GetInputData< cpPlugins::Interface::Image >( "ReferenceImage" );
42 _2D* im2d = input->GetITK< _2D >( );
43 _3D* im3d = input->GetITK< _3D >( );
45 return( this->_GD0( im2d ) );
46 else if( im3d != NULL )
47 return( this->_GD0( im3d ) );
49 return( "fpaPlugins::MinimumSpanningTreeToMesh: Reference image type not supported." );
52 // -------------------------------------------------------------------------
54 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;
64 this->GetInputData< fpaPlugins::MinimumSpanningTree >( "Input" );
65 _MST* mst = mst_wrapper->GetITK< _MST >( );
67 return( "fpaPlugins::MinimumSpanningTreeToMesh: Input MST type not supported." );
69 this->GetInputData< cpPlugins::Interface::PointList >( "Seeds" );
70 if( seeds->GetNumberOfPoints( ) < 2 )
71 return( "fpaPlugins::MinimumSpanningTreeToMesh: Not enough seeds (<2)." );
75 this->GetOutputData< cpPlugins::Interface::Mesh >( "Output" );
76 vtkSmartPointer< vtkPolyData > pd = out->GetVTK< vtkPolyData >( );
77 if( pd.GetPointer( ) == NULL )
79 pd = vtkPolyData::New( );
84 _P pa = seeds->GetPoint< _P >( 0 );
85 _P pb = seeds->GetPoint< _P >( 1 );
87 image->TransformPhysicalPointToIndex( pa, va );
88 image->TransformPhysicalPointToIndex( pb, vb );
90 std::vector< _P > path =
91 mst->GetPathFromImage(
93 this->m_Parameters->GetUint( "Kernel" )
96 vtkSmartPointer< vtkPoints > points =
97 vtkSmartPointer< vtkPoints >::New( );
98 vtkSmartPointer< vtkCellArray > array =
99 vtkSmartPointer< vtkCellArray >::New( );
100 for( unsigned int i = 0; i < path.size( ); ++i )
102 if( I::ImageDimension == 2 )
103 points->InsertNextPoint( path[ i ][ 0 ], path[ i ][ 1 ], 0 );
104 else if( I::ImageDimension == 3 )
105 points->InsertNextPoint(
106 path[ i ][ 0 ], path[ i ][ 1 ], path[ i ][ 2 ]
109 points->InsertNextPoint( 0, 0, 0 );
112 array->InsertNextCell( 2 );
113 array->InsertCellPoint( i - 1 );
114 array->InsertCellPoint( i );
119 pd->SetPoints( points );
120 pd->SetLines( array );