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