]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/Algorithm.hxx
Almost there...
[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 void fpa::Image::Algorithm< I, O, A >::
35 _AfterGenerateData( )
36 {
37   this->Superclass::_AfterGenerateData( );
38   this->GetMinimumSpanningTree( )->SetCollisions( this->m_Collisions );
39 }
40
41 // -------------------------------------------------------------------------
42 template< class I, class O, class A >
43 unsigned long fpa::Image::Algorithm< I, O, A >::
44 _NumberOfVertices( ) const
45 {
46   return( this->GetInput( )->GetRequestedRegion( ).GetNumberOfPixels( ) );
47 }
48
49 // -------------------------------------------------------------------------
50 template< class I, class O, class A >
51 const typename fpa::Image::Algorithm< I, O, A >::
52 TValue& fpa::Image::Algorithm< I, O, A >::
53 _VertexValue( const TVertex& v ) const
54 {
55   return( this->GetInput( )->GetPixel( v ) );
56 }
57
58 // -------------------------------------------------------------------------
59 template< class I, class O, class A >
60 double fpa::Image::Algorithm< I, O, A >::
61 _Distance( const TVertex& a, const TVertex& b ) const
62 {
63   typename I::PointType pa, pb;
64   this->GetInput( )->TransformIndexToPhysicalPoint( a, pa );
65   this->GetInput( )->TransformIndexToPhysicalPoint( b, pb );
66   return( double( pa.EuclideanDistanceTo( pb ) ) );
67 }
68
69 // -------------------------------------------------------------------------
70 template< class I, class O, class A >
71 bool fpa::Image::Algorithm< I, O, A >::
72 _HasEdge( const TVertex& a, const TVertex& b ) const
73 {
74   unsigned long dist = 0;
75   for( unsigned int d = 0; d < I::ImageDimension; d++ )
76     dist += std::abs( long( a[ d ] ) - long( b[ d ] ) );
77   if( this->m_NeighborhoodOrder == 1 )
78     return( dist <= 1 );
79   else
80     return( dist <= I::ImageDimension );
81 }
82
83 // -------------------------------------------------------------------------
84 template< class I, class O, class A >
85 void fpa::Image::Algorithm< I, O, A >::
86 _Neighborhood( _TVertices& neighborhood, const TVertex& v ) const
87 {
88   typename I::RegionType reg = this->GetInput( )->GetRequestedRegion( );
89
90   neighborhood.clear( );
91   if( this->m_NeighborhoodOrder == 1 )
92   {
93     for( unsigned int d = 0; d < I::ImageDimension; d++ )
94     {
95       for( int i = -1; i <= 1; i += 2 )
96       {
97         TVertex n = v;
98         n[ d ] += i;
99         if( reg.IsInside( n ) )
100           neighborhood.push_back( n );
101
102       } // rof
103
104     } // rof
105   }
106   else
107   {
108     typedef itk::ConstNeighborhoodIterator< I > TNeighIt;
109     typename I::SizeType nSize;
110     nSize.Fill( 1 );
111
112     TNeighIt nIt( nSize, this->GetInput( ), reg );
113     nIt.SetLocation( v );
114     for( unsigned int i = 0; i < nIt.Size( ); i++ )
115     {
116       TVertex n = nIt.GetIndex( i );
117       if( n == v )
118         continue;
119       if( reg.IsInside( n ) )
120         neighborhood.push_back( n );
121
122     } // rof
123
124   } // fi
125 }
126
127 // -------------------------------------------------------------------------
128 template< class I, class O, class A >
129 void fpa::Image::Algorithm< I, O, A >::
130 _InitResults( )
131 {
132 }
133
134 // -------------------------------------------------------------------------
135 template< class I, class O, class A >
136 const typename fpa::Image::Algorithm< I, O, A >::
137 TResult& fpa::Image::Algorithm< I, O, A >::
138 _Result( const TVertex& v ) const
139 {
140   return( this->GetOutput( )->GetPixel( v ) );
141 }
142
143 // -------------------------------------------------------------------------
144 template< class I, class O, class A >
145 void fpa::Image::Algorithm< I, O, A >::
146 _SetResult( const TVertex& v, const _TNode& n )
147 {
148   this->GetOutput( )->SetPixel( v, n.Result );
149   this->GetMinimumSpanningTree( )->SetParent( v, n.Parent, n.FrontId );
150 }
151
152 #endif // __FPA__IMAGE__ALGORITHM__HXX__
153
154 // eof - $RCSfile$