]> Creatis software - FrontAlgorithms.git/blob - plugins/Plugins/ImageDijkstra.cxx
7c8212ef9f6e80d66ea56afc5a53c72c3602d57d
[FrontAlgorithms.git] / plugins / Plugins / ImageDijkstra.cxx
1 #include <Plugins/ImageDijkstra.h>
2 #include <cpInstances/Image.h>
3 #include <cpInstances/Image_Demanglers.h>
4
5 #include <fpa/Image/Dijkstra.h>
6 #include <vtkPolyData.h>
7
8 // -------------------------------------------------------------------------
9 fpaPlugins::ImageDijkstra::
10 ImageDijkstra( )
11   : Superclass( )
12 {
13   typedef cpPlugins::BaseObjects::DataObject _TData;
14   typedef cpInstances::Image      _TMST;
15
16   this->_ConfigureInput< _TData >( "Cost", false, false );
17   this->_ConfigureInput< _TData >( "CostConversion", false, false );
18   this->_ConfigureOutput< _TMST >( "MST" );
19
20   std::vector< std::string > choices;
21   choices.push_back( "float" );
22   choices.push_back( "double" );
23   this->m_Parameters.ConfigureAsChoices( "ResultType", choices );
24   this->m_Parameters.SetSelectedChoice( "ResultType", "float" );
25 }
26
27 // -------------------------------------------------------------------------
28 fpaPlugins::ImageDijkstra::
29 ~ImageDijkstra( )
30 {
31 }
32
33 // -------------------------------------------------------------------------
34 void fpaPlugins::ImageDijkstra::
35 _GenerateData( )
36 {
37   auto o = this->GetInputData( "Input" );
38   cpPlugins_Demangle_Image_ScalarPixels_AllDims_1( o, _GD0 )
39     this->_Error( "Invalid input image." );
40 }
41
42 // -------------------------------------------------------------------------
43 template< class _TImage >
44 void fpaPlugins::ImageDijkstra::
45 _GD0( _TImage* image )
46 {
47   typedef itk::Image< float, _TImage::ImageDimension >  _TFloat;
48   typedef itk::Image< double, _TImage::ImageDimension > _TDouble;
49
50   auto rtype = this->m_Parameters.GetSelectedChoice( "ResultType" );
51   if     ( rtype == "float"  ) this->_GD1< _TImage, _TFloat >( image );
52   else if( rtype == "double" ) this->_GD1< _TImage, _TDouble >( image );
53 }
54
55 // -------------------------------------------------------------------------
56 template< class _TInputImage, class _TOutputImage >
57 void fpaPlugins::ImageDijkstra::
58 _GD1( _TInputImage* image )
59 {
60   typedef fpa::Image::Dijkstra< _TInputImage, _TOutputImage > _TFilter;
61   typedef typename _TFilter::TCostConversionFunction _TCostConversion;
62   typedef typename _TFilter::TCostFunction           _TCost;
63   typedef typename _TFilter::TNeighborhoodFunction   _TNeighborhood;
64
65   // Get functors
66   auto neig = this->GetInputData< _TNeighborhood >( "Neighborhood" );
67   auto cost = this->GetInputData< _TCost >( "Cost" );
68   auto conv = this->GetInputData< _TCostConversion >( "CostConversion" );
69
70   // Configure filter
71   auto filter = this->_CreateITK< _TFilter >( );
72   filter->SetInput( image );
73   if( neig != NULL )
74     filter->SetNeighborhoodFunction( neig );
75   if( cost != NULL )
76     filter->SetCostFunction( cost );
77   if( conv != NULL )
78     filter->SetCostConversionFunction( conv );
79   filter->SetStopAtOneFront( this->m_Parameters.GetBool( "StopAtOneFront" ) );
80
81   // Assign seeds
82   auto seeds = this->GetInputData< vtkPolyData >( "Seeds" );
83   if( seeds != NULL )
84   {
85     typename _TInputImage::PointType pnt;
86     typename _TInputImage::IndexType idx;
87     unsigned int dim =
88       ( _TInputImage::ImageDimension < 3 )? _TInputImage::ImageDimension: 3;
89
90     for( int i = 0; i < seeds->GetNumberOfPoints( ); ++i )
91     {
92       double buf[ 3 ];
93       seeds->GetPoint( i, buf );
94       pnt.Fill( 0 );
95       for( unsigned int d = 0; d < dim; ++d )
96         pnt[ d ] = buf[ d ];
97
98       if( image->TransformPhysicalPointToIndex( pnt, idx ) )
99         filter->AddSeed( idx, 0 );
100
101     } // rof
102
103   } // fi
104
105   // Assign outputs
106   filter->Update( );
107   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
108   this->GetOutput( "MST" )->SetITK( filter->GetMinimumSpanningTree( ) );
109 }
110
111 // eof - $RCSfile$