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