]> Creatis software - FrontAlgorithms.git/blob - lib/fpaPlugins/ImageRegionGrow.cxx
0780fe028c1b604ca7494f4157494e11ec396ace
[FrontAlgorithms.git] / lib / fpaPlugins / ImageRegionGrow.cxx
1 #include "ImageRegionGrow.h"
2
3 #include <cpPlugins/Interface/Image.h>
4 #include <cpPlugins/Interface/PointList.h>
5 #include <fpa/Image/RegionGrow.h>
6 #include <fpa/Image/Functors/RegionGrowAllBelongsFunction.h>
7 #include <fpaPlugins/GrowFunction.h>
8
9 // -------------------------------------------------------------------------
10 fpaPlugins::ImageRegionGrow::
11 ImageRegionGrow( )
12   : Superclass( )
13 {
14   this->_AddInput( "Input" );
15   this->_AddInput( "GrowFunction" );
16   this->_AddInput( "Seeds" );
17   this->_MakeOutput< cpPlugins::Interface::Image >( "Output" );
18
19   this->m_Parameters->ConfigureAsBool( "VisualDebug" );
20   this->m_Parameters->ConfigureAsBool( "StopAtOneFront" );
21   this->m_Parameters->ConfigureAsReal( "InsideValue" );
22   this->m_Parameters->ConfigureAsReal( "OutsideValue" );
23   // TODO: this->m_Parameters->ConfigureAsPointList( "Seeds" );
24
25   this->m_Parameters->SetBool( "VisualDebug", false );
26   this->m_Parameters->SetBool( "StopAtOneFront", false );
27   this->m_Parameters->SetReal( "InsideValue", 1 );
28   this->m_Parameters->SetReal( "OutsideValue", 0 );
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::ImageRegionGrow::
39 ~ImageRegionGrow( )
40 {
41 }
42
43 // -------------------------------------------------------------------------
44 std::string fpaPlugins::ImageRegionGrow::
45 _GenerateData( )
46 {
47   cpPlugins::Interface::Image* input =
48     this->GetInput< cpPlugins::Interface::Image >( "Input" );
49   if( input == NULL )
50     return( "fpaPlugins::ImageRegionGrow: 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::ImageRegionGrow: Input image type not supported.";
57   return( r );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class I >
62 std::string fpaPlugins::ImageRegionGrow::
63 _GD0( itk::DataObject* data )
64 {
65   typedef unsigned char                               _TOutPixel;
66   typedef itk::Image< _TOutPixel, I::ImageDimension > _TOut;
67   typedef fpa::Image::RegionGrow< I, _TOut >          _TFilter;
68   typedef typename _TFilter::TGrowingFunction         _TFunctor;
69   typedef typename I::PointType                       _TPoint;
70
71   cpPlugins::Interface::PointList* seeds =
72     this->GetInput< cpPlugins::Interface::PointList >( "Seeds" );
73   if( seeds == NULL )
74     return( "fpaPlugins::ImageRegionGrow: No given seeds." );
75   I* image = dynamic_cast< I* >( data );
76
77   // Create filter and connect input
78   _TFilter* filter = this->_CreateITK< _TFilter >( );
79   filter->SetInput( image );
80
81   // Connect grow functor (or create a tautology)
82   typename _TFunctor::Pointer functor;
83   cpPlugins::Interface::DataObject* functor_wrapper =
84     this->GetInput< cpPlugins::Interface::DataObject >( "GrowFunction" );
85   if( functor_wrapper != NULL )
86     functor = functor_wrapper->GetITK< _TFunctor >( );
87   if( functor.IsNull( ) )
88     functor =
89       fpa::Image::Functors::RegionGrowAllBelongsFunction< I >::New( );
90   filter->SetGrowingFunction( functor );
91
92   // Set numeric parameters
93   Superclass::TParameters* params = this->m_Parameters;
94   std::string order = params->GetSelectedChoice( "NeighborhoodOrder" );
95   filter->SetNeighborhoodOrder( order[ 0 ] - '0' );
96   filter->SetStopAtOneFront( params->GetBool( "StopAtOneFront" ) );
97   filter->SetInsideValue( _TOutPixel( params->GetReal( "InsideValue" ) ) );
98   filter->SetOutsideValue( _TOutPixel( params->GetReal( "OutsideValue" ) ) );
99
100   // Assign seeds
101   for( unsigned int s = 0; s < seeds->GetNumberOfPoints( ); ++s )
102   {
103     _TPoint pnt = seeds->GetPoint< _TPoint >( s );
104     typename I::IndexType idx;
105     if( image->TransformPhysicalPointToIndex( pnt, idx ) )
106       filter->AddSeed( idx, 0 );
107
108   } // rof
109
110   // Connect visual debugger
111   /* TODO
112      this->m_Parameters->ConfigureAsBool( "VisualDebug", false );
113    */
114
115   // Go!!!
116   filter->Update( );
117
118   // Connect output
119   cpPlugins::Interface::Image* out =
120     this->GetOutput< cpPlugins::Interface::Image >( "Output" );
121   if( out != NULL )
122   {
123     out->SetITK< _TOut >( filter->GetOutput( ) );
124     return( "" );
125   }
126   else
127     return( "fpaPlugins::ImageRegionGrow: output not correctly created." );
128 }
129
130 // eof - $RCSfile$