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