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