]> Creatis software - FrontAlgorithms.git/blob - lib/fpaPlugins/MinimumSpanningTreeToMesh.cxx
c474429f3ba3e3f8a405819b82191d85ea1aa60d
[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   auto input =
41     this->GetInputData< cpPlugins::Interface::Image >( "ReferenceImage" );
42   _2D* im2d = input->GetITK< _2D >( );
43   _3D* im3d = input->GetITK< _3D >( );
44   if( im2d != NULL )
45     return( this->_GD0( im2d ) );
46   else if( im3d != NULL )
47     return( this->_GD0( im3d ) );
48   else
49     return( "fpaPlugins::MinimumSpanningTreeToMesh: Reference image type not supported." );
50 }
51
52 // -------------------------------------------------------------------------
53 template< class I >
54 std::string fpaPlugins::MinimumSpanningTreeToMesh::
55 _GD0( I* image )
56 {
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;
61
62   // Get inputs
63   auto mst_wrapper =
64     this->GetInputData< fpaPlugins::MinimumSpanningTree >( "Input" );
65   _MST* mst = mst_wrapper->GetITK< _MST >( );
66   if( mst == NULL )
67     return( "fpaPlugins::MinimumSpanningTreeToMesh: Input MST type not supported." );
68   auto seeds =
69     this->GetInputData< cpPlugins::Interface::PointList >( "Seeds" );
70   if( seeds->GetNumberOfPoints( ) < 2 )
71     return( "fpaPlugins::MinimumSpanningTreeToMesh: Not enough seeds (<2)." );
72
73   // Get output
74   auto out =
75     this->GetOutputData< cpPlugins::Interface::Mesh >( "Output" );
76   vtkSmartPointer< vtkPolyData > pd = out->GetVTK< vtkPolyData >( );
77   if( pd.GetPointer( ) == NULL )
78   {
79     pd = vtkPolyData::New( );
80     out->SetVTK( pd );
81
82   } // fi
83
84   _P pa = seeds->GetPoint< _P >( 0 );
85   _P pb = seeds->GetPoint< _P >( 1 );
86   _V va, vb;
87   image->TransformPhysicalPointToIndex( pa, va );
88   image->TransformPhysicalPointToIndex( pb, vb );
89
90   std::vector< _P > path =
91     mst->GetPathFromImage(
92       va, vb, image,
93       this->m_Parameters->GetUint( "Kernel" )
94       );
95
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 )
101   {
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 ]
107         );
108     else
109       points->InsertNextPoint( 0, 0, 0 );
110     if( i > 0 )
111     {
112       array->InsertNextCell( 2 );
113       array->InsertCellPoint( i - 1 );
114       array->InsertCellPoint( i );
115
116     } // fi
117     
118   } // rof
119   pd->SetPoints( points );
120   pd->SetLines( array );
121
122   return( "" );
123 }
124
125 // eof - $RCSfile$