]> Creatis software - FrontAlgorithms.git/blob - plugins/ImageAlgorithms/ExtractPathFromMinimumSpanningTree.cxx
...
[FrontAlgorithms.git] / plugins / ImageAlgorithms / ExtractPathFromMinimumSpanningTree.cxx
1 #include "ExtractPathFromMinimumSpanningTree.h"
2 #include <cpInstances/DataObjects/Image.h>
3 #include <cpInstances/DataObjects/PolyLineParametricPath.h>
4 #include <vtkPolyData.h>
5 #include <fpa/Image/MinimumSpanningTree.h>
6
7 // -------------------------------------------------------------------------
8 fpaPlugins_ImageAlgorithms::ExtractPathFromMinimumSpanningTree::
9 ExtractPathFromMinimumSpanningTree( )
10   : Superclass( )
11 {
12   typedef cpPlugins::Pipeline::DataObject                  _TData;
13   typedef cpInstances::DataObjects::Image                  _TMST;
14   typedef cpInstances::DataObjects::PolyLineParametricPath _TPath;
15
16   this->_ConfigureInput< _TMST >( "MST", true, false );
17   this->_ConfigureInput< _TData >( "Seeds", true, false );
18   this->_ConfigureOutput< _TPath >( "Output" );
19 }
20
21 // -------------------------------------------------------------------------
22 fpaPlugins_ImageAlgorithms::ExtractPathFromMinimumSpanningTree::
23 ~ExtractPathFromMinimumSpanningTree( )
24 {
25 }
26
27 // -------------------------------------------------------------------------
28 void fpaPlugins_ImageAlgorithms::ExtractPathFromMinimumSpanningTree::
29 _GenerateData( )
30 {
31   typedef fpa::Image::MinimumSpanningTree< 2 > _TMST2;
32   typedef fpa::Image::MinimumSpanningTree< 3 > _TMST3;
33
34   auto mst2 = this->GetInputData< _TMST2 >( "MST" );
35   auto mst3 = this->GetInputData< _TMST3 >( "MST" );
36   if     ( mst2 != NULL ) this->_GD0( mst2 );
37   else if( mst3 != NULL ) this->_GD0( mst3 );
38   else this->_Error( "Invalid input spanning tree." );
39 }
40
41 // -------------------------------------------------------------------------
42 template< class _TMST >
43 void fpaPlugins_ImageAlgorithms::ExtractPathFromMinimumSpanningTree::
44 _GD0( _TMST* mst )
45 {
46   typedef typename _TMST::IndexType _TIndex;
47   typedef typename _TMST::TPath     _TPath;
48
49   // Get seeds
50   std::vector< _TIndex > seeds;
51   auto points = this->GetInputData< vtkPolyData >( "Seeds" );
52   if( points != NULL )
53   {
54     if( points->GetNumberOfPoints( ) < 2 )
55       this->_Error( "Not enough seeds (<2)." );
56
57     typename _TMST::PointType pnt;
58     typename _TMST::IndexType idx;
59     unsigned int dim =
60       ( _TMST::ImageDimension < 3 )? _TMST::ImageDimension: 3;
61     for( unsigned int i = 0; i < 2; ++i )
62     {
63       double buf[ 3 ];
64       points->GetPoint( i, buf );
65       pnt.Fill( 0 );
66       for( unsigned int d = 0; d < dim; ++d )
67         pnt[ d ] = buf[ d ];
68       if( mst->TransformPhysicalPointToIndex( pnt, idx ) )
69         seeds.push_back( idx );
70
71     } // rof
72
73   } // fi
74
75   typename _TPath::Pointer path;
76   mst->GetPath( path, seeds[ 0 ], seeds[ 1 ] );
77   this->GetOutput( "Output" )->SetITK( path );
78 }
79
80 // eof - $RCSfile$