]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/Algorithm.hxx
Segmentation guided by a gaussian estimator added -- Not yet finished
[FrontAlgorithms.git] / lib / fpa / Image / Algorithm.hxx
1 #ifndef __FPA__IMAGE__ALGORITHM__HXX__
2 #define __FPA__IMAGE__ALGORITHM__HXX__
3
4 #include <cmath>
5 #include <limits>
6 #include <itkConstNeighborhoodIterator.h>
7
8 // -------------------------------------------------------------------------
9 template< class I, class A, class CC >
10 fpa::Image::Algorithm< I, A, CC >::
11 Algorithm( )
12   : Superclass( ),
13     m_NeighborhoodOrder( 1 )
14 {
15   this->m_CostConversion = TCostConversionFunction::New( );
16 }
17
18 // -------------------------------------------------------------------------
19 template< class I, class A, class CC >
20 fpa::Image::Algorithm< I, A, CC >::
21 ~Algorithm( )
22 {
23 }
24
25 // -------------------------------------------------------------------------
26 template< class I, class A, class CC >
27 bool fpa::Image::Algorithm< I, A, CC >::
28 _UpdateResult( _TNode& n )
29 {
30   bool ret = this->Superclass::_UpdateResult( n );
31   this->GetOutput( )->SetPixel( n.Vertex, n.Result );
32   return( ret );
33 }
34
35 // -------------------------------------------------------------------------
36 template< class I, class A, class CC >
37 unsigned long fpa::Image::Algorithm< I, A, CC >::
38 _NumberOfVertices( ) const
39 {
40   return(
41     this->GetInput( )->GetLargestPossibleRegion( ).GetNumberOfPixels( )
42     );
43 }
44
45 // -------------------------------------------------------------------------
46 template< class I, class A, class CC >
47 typename fpa::Image::Algorithm< I, A, CC >::
48 TVertexValue fpa::Image::Algorithm< I, A, CC >::
49 _Value( const TVertex& v ) const
50 {
51   return( this->GetInput( )->GetPixel( v ) );
52 }
53
54 // -------------------------------------------------------------------------
55 template< class I, class A, class CC >
56 typename fpa::Image::Algorithm< I, A, CC >::
57 TResult fpa::Image::Algorithm< I, A, CC >::
58 _Result( const TVertex& v ) const
59 {
60   return( this->GetOutput( )->GetPixel( v ) );
61 }
62
63 // -------------------------------------------------------------------------
64 template< class I, class A, class CC >
65 double fpa::Image::Algorithm< I, A, CC >::
66 _Norm( const TVertex& a, const TVertex& b ) const
67 {
68   typename I::PointType pa, pb;
69   this->GetInput( )->TransformIndexToPhysicalPoint( a, pa );
70   this->GetInput( )->TransformIndexToPhysicalPoint( b, pb );
71   return( double( pa.EuclideanDistanceTo( pb ) ) );
72 }
73
74 // -------------------------------------------------------------------------
75 template< class I, class A, class CC >
76 bool fpa::Image::Algorithm< I, A, CC >::
77 _Edge( const TVertex& a, const TVertex& b ) const
78 {
79   unsigned long dist = 0;
80   for( unsigned int d = 0; d < I::ImageDimension; d++ )
81     dist += std::abs( long( a[ d ] ) - long( b[ d ] ) );
82   if( this->m_NeighborhoodOrder == 1 )
83     return( dist <= 1 );
84   else
85     return( dist <= I::ImageDimension );
86 }
87
88 // -------------------------------------------------------------------------
89 template< class I, class A, class CC >
90 typename fpa::Image::Algorithm< I, A, CC >::
91 TCost fpa::Image::Algorithm< I, A, CC >::
92 _Cost( const TVertex& a, const TVertex& b ) const
93 {
94   static const TCost INF_COST = std::numeric_limits< TCost >::max( );
95   if( this->_Edge( a, b ) )
96   {
97     return(
98       this->m_CostConversion->Evaluate( this->GetInput( )->GetPixel( b ) )
99       );
100   }
101   else
102     return( INF_COST );
103 }
104
105 // -------------------------------------------------------------------------
106 template< class I, class A, class CC >
107 void fpa::Image::Algorithm< I, A, CC >::
108 _Neighs( const _TNode& n, _TNodes& N ) const
109 {
110   typename I::RegionType reg = this->GetInput( )->GetRequestedRegion( );
111
112   N.clear( );
113   if( this->m_NeighborhoodOrder == 1 )
114   {
115     for( unsigned int d = 0; d < I::ImageDimension; d++ )
116     {
117       for( int i = -1; i <= 1; i += 2 )
118       {
119         TVertex v = n.Vertex;
120         v[ d ] += i;
121         if( reg.IsInside( v ) )
122           N.push_back( _TNode( v, n.FrontId ) );
123         else
124           N.push_back( n );
125
126       } // rof
127
128     } // rof
129   }
130   else
131   {
132     typedef itk::ConstNeighborhoodIterator< I > TNeighIt;
133     typename I::SizeType nSize;
134     nSize.Fill( 1 );
135
136     TNeighIt nIt( nSize, this->GetInput( ), reg );
137     nIt.SetLocation( n.Vertex );
138     for( unsigned int i = 0; i < nIt.Size( ); i++ )
139     {
140       TVertex idxN = nIt.GetIndex( i );
141       if( idxN == n.Vertex )
142         continue;
143       if( !reg.IsInside( idxN ) )
144         continue;
145       N.push_back( _TNode( idxN, n.FrontId ) );
146
147     } // rof
148
149   } // fi
150 }
151
152 // -------------------------------------------------------------------------
153 template< class I, class A, class CC >
154 void fpa::Image::Algorithm< I, A, CC >::
155 _NeighsInDim( const _TNode& n, const unsigned int& d, _TNodes& N )
156 {
157   typename I::RegionType reg = this->GetInput( )->GetRequestedRegion( );
158
159   N.clear( );
160   for( int i = -1; i <= 1; i += 2 )
161   {
162     TVertex v = n.Vertex;
163     v[ d ] += i;
164     if( reg.IsInside( v ) )
165       N.push_back( _TNode( v, n.FrontId ) );
166
167   } // rof
168 }
169
170 // -------------------------------------------------------------------------
171 template< class I, class A, class CC >
172 void fpa::Image::Algorithm< I, A, CC >::
173 _InitializeResults( )
174 {
175 }
176
177 #endif // __FPA__IMAGE__ALGORITHM__HXX__
178
179 // eof - $RCSfile$