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