]> Creatis software - FrontAlgorithms.git/blob - lib/fpaPlugins/MinimumSpanningTreeToMesh.cxx
9079a1ce8e25ddb5d5b70b030f3532083944b57d
[FrontAlgorithms.git] / lib / fpaPlugins / MinimumSpanningTreeToMesh.cxx
1 #include "MinimumSpanningTreeToMesh.h"
2
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>
8
9 #include <vtkCellArray.h>
10 #include <vtkPoints.h>
11 #include <vtkPolyData.h>
12
13 // -------------------------------------------------------------------------
14 fpaPlugins::MinimumSpanningTreeToMesh::
15 MinimumSpanningTreeToMesh( )
16   : Superclass( )
17 {
18   this->_AddInput( "Input" );
19   this->_AddInput( "Seeds" );
20   this->_AddInput( "ReferenceImage" );
21   this->_AddOutput< cpPlugins::Interface::Mesh >( "Output" );
22
23   this->m_Parameters->ConfigureAsUint( "Kernel" );
24   this->m_Parameters->SetUint( "Kernel", 0 );
25 }
26
27 // -------------------------------------------------------------------------
28 fpaPlugins::MinimumSpanningTreeToMesh::
29 ~MinimumSpanningTreeToMesh( )
30 {
31 }
32
33 // -------------------------------------------------------------------------
34 std::string fpaPlugins::MinimumSpanningTreeToMesh::
35 _GenerateData( )
36 {
37   typedef itk::ImageBase< 2 > _2D;
38   typedef itk::ImageBase< 3 > _3D;
39
40   cpPlugins::Interface::Image* input =
41     this->GetInput< cpPlugins::Interface::Image >( "ReferenceImage" );
42
43   if( input == NULL )
44     return( "fpaPlugins::MinimumSpanningTreeToMesh: No reference image." );
45
46   _2D* im2d = input->GetITK< _2D >( );
47   _3D* im3d = input->GetITK< _3D >( );
48   if( im2d != NULL )
49     return( this->_GD0( im2d ) );
50   else if( im3d != NULL )
51     return( this->_GD0( im3d ) );
52   else
53     return( "fpaPlugins::MinimumSpanningTreeToMesh: Reference image type not supported." );
54 }
55
56 // -------------------------------------------------------------------------
57 template< class I >
58 std::string fpaPlugins::MinimumSpanningTreeToMesh::
59 _GD0( I* image )
60 {
61   typedef typename I::IndexType                                        _V;
62   typedef typename I::PointType                                        _P;
63   typedef itk::Functor::IndexLexicographicCompare< I::ImageDimension > _VC;
64   typedef fpa::Base::MinimumSpanningTree< _V, _VC >                    _MST;
65
66   // Get inputs
67   fpaPlugins::MinimumSpanningTree* mst_wrapper =
68     this->GetInput< fpaPlugins::MinimumSpanningTree >( "Input" );
69   if( mst_wrapper == NULL )
70     return( "fpaPlugins::MinimumSpanningTreeToMesh: No input MST" );
71   _MST* mst = mst_wrapper->GetITK< _MST >( );
72   if( mst == NULL )
73     return( "fpaPlugins::MinimumSpanningTreeToMesh: Input MST type not supported." );
74   cpPlugins::Interface::PointList* seeds =
75     this->GetInput< cpPlugins::Interface::PointList >( "Seeds" );
76   if( seeds == NULL )
77     return( "fpaPlugins::MinimumSpanningTreeToMesh: No given seeds." );
78   if( seeds->GetNumberOfPoints( ) < 2 )
79     return( "fpaPlugins::MinimumSpanningTreeToMesh: Not enough seeds (<2)." );
80
81   // Get output
82   cpPlugins::Interface::Mesh* out =
83     this->GetOutput< cpPlugins::Interface::Mesh >( "Output" );
84   vtkSmartPointer< vtkPolyData > pd = out->GetVTK< vtkPolyData >( );
85   if( pd.GetPointer( ) == NULL )
86   {
87     pd = vtkPolyData::New( );
88     out->SetVTK( pd );
89
90   } // fi
91
92   _P pa = seeds->GetPoint< _P >( 0 );
93   _P pb = seeds->GetPoint< _P >( 1 );
94   _V va, vb;
95   image->TransformPhysicalPointToIndex( pa, va );
96   image->TransformPhysicalPointToIndex( pb, vb );
97
98   std::vector< _P > path =
99     mst->GetPathFromImage(
100       va, vb, image,
101       this->m_Parameters->GetUint( "Kernel" )
102       );
103
104   vtkSmartPointer< vtkPoints > points =
105     vtkSmartPointer< vtkPoints >::New( );
106   vtkSmartPointer< vtkCellArray > array =
107     vtkSmartPointer< vtkCellArray >::New( );
108   for( unsigned int i = 0; i < path.size( ); ++i )
109   {
110     if( I::ImageDimension == 2 )
111       points->InsertNextPoint( path[ i ][ 0 ], path[ i ][ 1 ], 0 );
112     else if( I::ImageDimension == 3 )
113       points->InsertNextPoint(
114         path[ i ][ 0 ], path[ i ][ 1 ], path[ i ][ 2 ]
115         );
116     else
117       points->InsertNextPoint( 0, 0, 0 );
118     if( i > 0 )
119     {
120       array->InsertNextCell( 2 );
121       array->InsertCellPoint( i - 1 );
122       array->InsertCellPoint( i );
123
124     } // fi
125     
126   } // rof
127   pd->SetPoints( points );
128   pd->SetLines( array );
129
130   return( "" );
131 }
132
133 // eof - $RCSfile$