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