]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/Algorithm.hxx
Major refactoring
[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 <itkConstNeighborhoodIterator.h>
6
7 // -------------------------------------------------------------------------
8 template< class I, class O, class A >
9 fpa::Image::Algorithm< I, O, A >::
10 Algorithm( )
11   : Superclass( ),
12     m_NeighborhoodOrder( 1 )
13 {
14 }
15
16 // -------------------------------------------------------------------------
17 template< class I, class O, class A >
18 fpa::Image::Algorithm< I, O, A >::
19 ~Algorithm( )
20 {
21 }
22
23 // -------------------------------------------------------------------------
24 template< class I, class O, class A >
25 void fpa::Image::Algorithm< I, O, A >::
26 _BeforeGenerateData( )
27 {
28   this->Superclass::_BeforeGenerateData( );
29   this->AllocateOutputs( );
30 }
31
32 // -------------------------------------------------------------------------
33 template< class I, class O, class A >
34 unsigned long fpa::Image::Algorithm< I, O, A >::
35 _NumberOfVertices( ) const
36 {
37   return( this->GetInput( )->GetRequestedRegion( ).GetNumberOfPixels( ) );
38 }
39
40 // -------------------------------------------------------------------------
41 template< class I, class O, class A >
42 const typename fpa::Image::Algorithm< I, O, A >::
43 TValue& fpa::Image::Algorithm< I, O, A >::
44 _VertexValue( const TVertex& v ) const
45 {
46   return( this->GetInput( )->GetPixel( v ) );
47 }
48
49 // -------------------------------------------------------------------------
50 template< class I, class O, class A >
51 double fpa::Image::Algorithm< I, O, A >::
52 _Distance( const TVertex& a, const TVertex& b ) const
53 {
54   typename I::PointType pa, pb;
55   this->GetInput( )->TransformIndexToPhysicalPoint( a, pa );
56   this->GetInput( )->TransformIndexToPhysicalPoint( b, pb );
57   return( double( pa.EuclideanDistanceTo( pb ) ) );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class I, class O, class A >
62 bool fpa::Image::Algorithm< I, O, A >::
63 _HasEdge( const TVertex& a, const TVertex& b ) const
64 {
65   unsigned long dist = 0;
66   for( unsigned int d = 0; d < I::ImageDimension; d++ )
67     dist += std::abs( long( a[ d ] ) - long( b[ d ] ) );
68   if( this->m_NeighborhoodOrder == 1 )
69     return( dist <= 1 );
70   else
71     return( dist <= I::ImageDimension );
72 }
73
74 // -------------------------------------------------------------------------
75 template< class I, class O, class A >
76 void fpa::Image::Algorithm< I, O, A >::
77 _Neighborhood( _TVertices& neighborhood, const TVertex& v ) const
78 {
79   typename I::RegionType reg = this->GetInput( )->GetRequestedRegion( );
80
81   neighborhood.clear( );
82   if( this->m_NeighborhoodOrder == 1 )
83   {
84     for( unsigned int d = 0; d < I::ImageDimension; d++ )
85     {
86       for( int i = -1; i <= 1; i += 2 )
87       {
88         TVertex n = v;
89         n[ d ] += i;
90         if( reg.IsInside( n ) )
91           neighborhood.push_back( n );
92
93       } // rof
94
95     } // rof
96   }
97   else
98   {
99     typedef itk::ConstNeighborhoodIterator< I > TNeighIt;
100     typename I::SizeType nSize;
101     nSize.Fill( 1 );
102
103     TNeighIt nIt( nSize, this->GetInput( ), reg );
104     nIt.SetLocation( v );
105     for( unsigned int i = 0; i < nIt.Size( ); i++ )
106     {
107       TVertex n = nIt.GetIndex( i );
108       if( n == v )
109         continue;
110       if( reg.IsInside( n ) )
111         neighborhood.push_back( n );
112
113     } // rof
114
115   } // fi
116 }
117
118 // -------------------------------------------------------------------------
119 template< class I, class O, class A >
120 void fpa::Image::Algorithm< I, O, A >::
121 _InitResults( )
122 {
123 }
124
125 // -------------------------------------------------------------------------
126 template< class I, class O, class A >
127 const typename fpa::Image::Algorithm< I, O, A >::
128 TResult& fpa::Image::Algorithm< I, O, A >::
129 _Result( const TVertex& v ) const
130 {
131   return( this->GetOutput( )->GetPixel( v ) );
132 }
133
134 // -------------------------------------------------------------------------
135 template< class I, class O, class A >
136 void fpa::Image::Algorithm< I, O, A >::
137 _SetResult( const TVertex& v, const TResult& r )
138 {
139   this->GetOutput( )->SetPixel( v, r );
140 }
141
142 // -------------------------------------------------------------------------
143 template< class I, class O, class A >
144 const typename fpa::Image::Algorithm< I, O, A >::
145 _TNode& fpa::Image::Algorithm< I, O, A >::
146 _Node( const TVertex& v ) const
147 {
148   return( this->m_Marks->GetPixel( v ) );
149 }
150
151 // -------------------------------------------------------------------------
152 template< class I, class O, class A >
153 void fpa::Image::Algorithm< I, O, A >::
154 _InitMarks( )
155 {
156   const I* in = this->GetInput( );
157
158   this->m_Marks = _TMarks::New( );
159   this->m_Marks->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
160   this->m_Marks->SetRequestedRegion( in->GetRequestedRegion( ) );
161   this->m_Marks->SetBufferedRegion( in->GetBufferedRegion( ) );
162   this->m_Marks->SetOrigin( in->GetOrigin( ) );
163   this->m_Marks->SetSpacing( in->GetSpacing( ) );
164   this->m_Marks->SetDirection( in->GetDirection( ) );
165   this->m_Marks->Allocate( );
166
167   _TNode far_node;
168   far_node.Label = Self::FarLabel;
169   this->m_Marks->FillBuffer( far_node );
170 }
171
172 // -------------------------------------------------------------------------
173 template< class I, class O, class A >
174 void fpa::Image::Algorithm< I, O, A >::
175 _Mark( const _TNode& node )
176 {
177   this->m_Marks->SetPixel( node.Vertex, node );
178 }
179
180 #endif // __FPA__IMAGE__ALGORITHM__HXX__
181
182 // eof - $RCSfile$