]> Creatis software - FrontAlgorithms.git/blob - plugins/Plugins/ExtractPathFromMinimumSpanningTree.cxx
...
[FrontAlgorithms.git] / plugins / Plugins / ExtractPathFromMinimumSpanningTree.cxx
1 #include <plugins/Plugins/ExtractPathFromMinimumSpanningTree.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4 #include <fpa_DataObjects.h>
5
6 /* TODO
7    #include <vtkFloatArray.h>
8    #include <vtkPointData.h>
9 */
10
11 // -------------------------------------------------------------------------
12 fpaPlugins::ExtractPathFromMinimumSpanningTree::
13 ExtractPathFromMinimumSpanningTree( )
14   : Superclass( )
15 {
16   typedef cpPlugins::BaseObjects::DataObject _TData;
17   typedef cpPlugins::DataObjects::Image _TMST;
18   typedef cpPlugins::DataObjects::Mesh  _TMesh;
19
20   this->_ConfigureInput< _TMST >( "MST", true, false );
21   this->_ConfigureInput< _TData >( "Seeds", true, false );
22   this->_ConfigureOutput< _TMesh >( "Paths" );
23
24   this->m_Parameters.ConfigureAsUintList( "Indices" );
25 }
26
27 // -------------------------------------------------------------------------
28 fpaPlugins::ExtractPathFromMinimumSpanningTree::
29 ~ExtractPathFromMinimumSpanningTree( )
30 {
31 }
32
33 // -------------------------------------------------------------------------
34 void fpaPlugins::ExtractPathFromMinimumSpanningTree::
35 _GenerateData( )
36 {
37   typedef fpa::Image::MinimumSpanningTree< 2 > _TMST2;
38   typedef fpa::Image::MinimumSpanningTree< 3 > _TMST3;
39
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." );
45 }
46
47 // -------------------------------------------------------------------------
48 template< class _TMST >
49 void fpaPlugins::ExtractPathFromMinimumSpanningTree::
50 _GD0( _TMST* mst )
51 {
52   typedef typename _TMST::IndexType _TIndex;
53
54   // Get seeds
55   std::vector< _TIndex > seeds;
56   auto points = this->GetInputData< vtkPolyData >( "Seeds" );
57   if( points != NULL )
58   {
59     typename _TMST::PointType pnt;
60     typename _TMST::IndexType idx;
61     unsigned int dim =
62       ( _TMST::ImageDimension < 3 )? _TMST::ImageDimension: 3;
63     for( unsigned int i = 0; i < points->GetNumberOfPoints( ); ++i )
64     {
65       double buf[ 3 ];
66       points->GetPoint( i, buf );
67       pnt.Fill( 0 );
68       for( unsigned int d = 0; d < dim; ++d )
69         pnt[ d ] = buf[ d ];
70       if( mst->TransformPhysicalPointToIndex( pnt, idx ) )
71         seeds.push_back( idx );
72
73     } // rof
74
75   } // fi
76
77   // Prepare result
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( ) );
84   /* TODO
85      vtkSmartPointer< vtkFloatArray > data =
86      vtkSmartPointer< vtkFloatArray >::New( );
87      data->SetNumberOfComponents( 1 );
88      mesh->GetPointData( )->SetScalars( data );
89   */
90
91   // Compute
92   auto indices = this->m_Parameters.GetUintList( "Indices" );
93   for( unsigned int i = 0; i < indices.size( ); i += 2 )
94   {
95     if( i < indices.size( ) - 1 )
96     {
97       _TIndex a = seeds[ indices[ i ] ];
98       _TIndex b = seeds[ indices[ i + 1 ] ];
99
100       auto path = mst->GetEuclideanPath( a, b );
101       for( unsigned long j = 0; j < path.size( ); ++j )
102       {
103         auto p = path[ 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 ] );
108         if( j > 0 )
109         {
110           mesh->GetLines( )->InsertNextCell( 2 );
111           mesh->GetLines( )->InsertCellPoint( j - 1 );
112           mesh->GetLines( )->InsertCellPoint( j );
113
114         } // fi
115
116       } // rof
117
118     } // fi
119
120   } // rof
121   this->GetOutput( "Paths" )->SetVTK( mesh );
122 }
123
124 // eof - $RCSfile$