]> Creatis software - FrontAlgorithms.git/blob - lib/fpaPlugins/ImageDijkstra.cxx
...
[FrontAlgorithms.git] / lib / fpaPlugins / ImageDijkstra.cxx
1 #include "ImageDijkstra.h"
2
3 #include <cpPlugins/Interface/Image.h>
4 #include <cpPlugins/Interface/PointList.h>
5 #include <fpaPlugins/MinimumSpanningTree.h>
6
7 #include <fpa/Image/Dijkstra.h>
8 #include <fpa/VTK/Image2DObserver.h>
9 #include <fpa/VTK/Image3DObserver.h>
10 #include <fpa/Base/Functors/InvertCostFunction.h>
11
12 #include <vtkRenderWindow.h>
13 #include <vtkRenderWindowInteractor.h>
14
15 // -------------------------------------------------------------------------
16 fpaPlugins::ImageDijkstra::
17 ImageDijkstra( )
18   : Superclass( )
19 {
20   this->_AddInput( "Input" );
21   this->_AddInput( "Seeds" );
22   this->_AddOutput< cpPlugins::Interface::Image >( "Output" );
23   this->_AddOutput< fpaPlugins::MinimumSpanningTree >( "MinimumSpanningTree" );
24
25   this->m_Parameters->ConfigureAsBool( "VisualDebug" );
26   this->m_Parameters->ConfigureAsBool( "StopAtOneFront" );
27   this->m_Parameters->SetBool( "VisualDebug", false );
28   this->m_Parameters->SetBool( "StopAtOneFront", false );
29
30   std::vector< std::string > orders;
31   orders.push_back( "1" );
32   orders.push_back( "2" );
33   this->m_Parameters->ConfigureAsChoices( "NeighborhoodOrder", orders );
34   this->m_Parameters->SetSelectedChoice( "NeighborhoodOrder", "1" );
35 }
36
37 // -------------------------------------------------------------------------
38 fpaPlugins::ImageDijkstra::
39 ~ImageDijkstra( )
40 {
41 }
42
43 // -------------------------------------------------------------------------
44 std::string fpaPlugins::ImageDijkstra::
45 _GenerateData( )
46 {
47   auto input =
48     this->GetInputData< cpPlugins::Interface::Image >( "Input" );
49   if( input == NULL )
50     return( "fpaPlugins::ImageDijkstra: No input image." );
51
52   itk::DataObject* image = NULL;
53   std::string r = "";
54   cpPlugins_Image_Demangle_AllScalarTypes( 2, input, image, r, _GD0 );
55   else cpPlugins_Image_Demangle_AllScalarTypes( 3, input, image, r, _GD0 );
56   else r = "fpaPlugins::ImageDijkstra: Input image type not supported.";
57   return( r );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class I >
62 std::string fpaPlugins::ImageDijkstra::
63 _GD0( itk::DataObject* data )
64 {
65   typedef typename I::PixelType                             _TOutPixel;
66   typedef itk::Image< _TOutPixel, I::ImageDimension >       _TOut;
67   typedef fpa::Image::Dijkstra< I, _TOut >                  _TFilter;
68   typedef typename _TFilter::TResult                        _TCost;
69   typedef fpa::Base::Functors::InvertCostFunction< _TCost > _TCostFunctor;
70   typedef typename I::PointType                             _TPoint;
71   typedef typename _TFilter::TMinimumSpanningTree           _TMST;
72
73   auto seeds =
74     this->GetInputData< cpPlugins::Interface::PointList >( "Seeds" );
75   if( seeds == NULL )
76     return( "fpaPlugins::ImageRegionGrow: No given seeds." );
77   I* image = dynamic_cast< I* >( data );
78
79   // Create filter and connect input
80   _TFilter* filter = this->_CreateITK< _TFilter >( );
81   filter->SetInput( image );
82
83   // Connect cost functor
84   typename _TCostFunctor::Pointer functor = _TCostFunctor::New( );
85   filter->SetConversionFunction( functor );
86
87   // Set numeric parameters
88   Superclass::TParameters* params = this->m_Parameters;
89   std::string order = params->GetSelectedChoice( "NeighborhoodOrder" );
90   filter->SetNeighborhoodOrder( order[ 0 ] - '0' );
91   filter->SetStopAtOneFront( params->GetBool( "StopAtOneFront" ) );
92
93   // Assign seeds
94   filter->ClearSeeds( );
95   for( unsigned int s = 0; s < seeds->GetNumberOfPoints( ); ++s )
96   {
97     _TPoint pnt = seeds->GetPoint< _TPoint >( s );
98     typename I::IndexType idx;
99     if( image->TransformPhysicalPointToIndex( pnt, idx ) )
100       filter->AddSeed( idx, 0 );
101
102   } // rof
103
104   // Go!!!
105   this->_ConfigureDebugger( filter );
106   filter->Update( );
107   this->_DeconfigureDebugger( filter );
108
109   // Connect output
110   auto out =
111     this->GetOutputData< cpPlugins::Interface::Image >( "Output" );
112   auto mst =
113     this->GetOutputData< fpaPlugins::MinimumSpanningTree >( "MinimumSpanningTree" );
114   if( out != NULL )
115     out->SetITK< _TOut >( filter->GetOutput( ) );
116   else
117     return( "fpaPlugins::ImageDijkstra: output not correctly created." );
118
119   if( mst != NULL )
120   {
121     mst->SetITK< _TMST >( filter->GetMinimumSpanningTree( ) );
122     return( "" );
123   }
124   else
125     return( "fpaPlugins::ImageDijkstra: minimum spanning tree." );
126 }
127
128 // eof - $RCSfile$