]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/Algorithm.hxx
...
[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 _TNode& n )
138 {
139   this->Superclass::_SetResult( v, n );
140   this->GetOutput( )->SetPixel( v, n.Result );
141 }
142
143 #endif // __FPA__IMAGE__ALGORITHM__HXX__
144
145 // eof - $RCSfile$