]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/Algorithm.hxx
...
[FrontAlgorithms.git] / lib / fpa / Image / Algorithm.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #ifndef __fpa__Image__Algorithm__hxx__
7 #define __fpa__Image__Algorithm__hxx__
8
9 // -------------------------------------------------------------------------
10 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
11 typename
12 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
13 TMarks*
14 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
15 GetMarks( )
16 {
17   return(
18     dynamic_cast< TMarks* >(
19       this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
20       )
21     );
22 }
23
24 // -------------------------------------------------------------------------
25 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
26 const typename
27 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
28 TMarks*
29 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
30 GetMarks( ) const
31 {
32   return(
33     dynamic_cast< const TMarks* >(
34       this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
35       )
36     );
37 }
38
39 // -------------------------------------------------------------------------
40 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
41 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
42 Algorithm( )
43   : Superclass( ),
44     m_NeigborhoodOrder( 1 )
45 {
46   this->m_MarksIdx = this->GetNumberOfRequiredOutputs( );
47   this->itk::ProcessObject::SetNumberOfRequiredOutputs( this->m_MarksIdx + 1 );
48   this->SetNthOutput( this->m_MarksIdx, TMarks::New( ) );
49 }
50
51 // -------------------------------------------------------------------------
52 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
53 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
54 ~Algorithm( )
55 {
56 }
57
58 // -------------------------------------------------------------------------
59 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
60 typename
61 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
62 TNodes fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
63 _UnifySeeds( )
64 {
65   const TInputImage* input = this->GetInput( );
66   typename TInputImage::RegionType region = input->GetRequestedRegion( );
67   TSeeds seeds = this->GetSeeds( );
68   TNodes nodes;
69
70   typename TSeeds::iterator sIt = seeds.begin( );
71   for( ; sIt != seeds.end( ); ++sIt )
72   {
73     TNode node;
74     if( sIt->IsPoint )
75       input->TransformPhysicalPointToIndex( sIt->Point, sIt->Vertex );
76     else
77       input->TransformIndexToPhysicalPoint( sIt->Vertex, sIt->Point );
78     if( region.IsInside( sIt->Vertex ) )
79     {
80       sIt->IsUnified = true;
81       node.Vertex = sIt->Vertex;
82       node.Parent = node.Vertex;
83       if( sIt->FrontId == 0 )
84         node.FrontId = nodes.size( ) + 1;
85       else
86         node.FrontId = sIt->FrontId;
87       nodes.insert( node );
88     }
89     else
90       sIt->IsUnified = false;
91       
92   } // rof
93
94   return( nodes );
95 }
96
97 // -------------------------------------------------------------------------
98 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
99 void
100 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
101 _ConfigureOutput( const TOutputValue& v )
102 {
103   const TInputImage* in = this->GetInput( );
104
105   TOutputImage* out = this->GetOutput( );
106   out->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
107   out->SetRequestedRegion( in->GetRequestedRegion( ) );
108   out->SetBufferedRegion( in->GetBufferedRegion( ) );
109   out->SetSpacing( in->GetSpacing( ) );
110   out->SetOrigin( in->GetOrigin( ) );
111   out->SetDirection( in->GetDirection( ) );
112   out->Allocate( );
113   out->FillBuffer( v );
114
115   TMarks* marks = this->GetMarks( );
116   marks->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
117   marks->SetRequestedRegion( in->GetRequestedRegion( ) );
118   marks->SetBufferedRegion( in->GetBufferedRegion( ) );
119   marks->SetSpacing( in->GetSpacing( ) );
120   marks->SetOrigin( in->GetOrigin( ) );
121   marks->SetDirection( in->GetDirection( ) );
122   marks->Allocate( );
123   marks->FillBuffer( TFrontId( 0 ) );
124 }
125
126 // -------------------------------------------------------------------------
127 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
128 typename
129 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
130 TNeighborhood fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
131 _GetNeighbors( const TVertex& v ) const
132 {
133   typename TInputImage::RegionType region =
134     this->GetInput( )->GetRequestedRegion( );
135   TNeighborhood neighborhood;
136   if( this->m_NeigborhoodOrder != 1 )
137   {
138     // TODO
139   }
140   else
141   {
142     for( unsigned int d = 0; d < TInputImage::ImageDimension; ++d )
143     {
144       for( int s = -1; s <= 1; s += 2 )
145       {
146         TVertex n = v;
147         n[ d ] += s;
148         if( region.IsInside( n ) )
149           neighborhood.push_back( n );
150
151       } // rof
152
153     } // rof
154
155   } // fi
156   return( neighborhood );
157 }
158
159 // -------------------------------------------------------------------------
160 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
161 typename
162 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
163 TInputValue
164 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
165 _GetInputValue( const TVertex& v ) const
166 {
167   return( this->GetInput( )->GetPixel( v ) );
168 }
169
170 // -------------------------------------------------------------------------
171 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
172 typename
173 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
174 TOutputValue fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
175 _GetOutputValue( const TVertex& v ) const
176 {
177   return( this->GetOutput( )->GetPixel( v ) );
178 }
179
180 // -------------------------------------------------------------------------
181 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
182 void
183 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
184 _UpdateOutputValue( const TNode& n )
185 {
186   this->GetOutput( )->SetPixel( n.Vertex, n.Value );
187 }
188
189 // -------------------------------------------------------------------------
190 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
191 bool
192 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
193 _IsMarked( const TVertex& v ) const
194 {
195   return( this->GetMarks( )->GetPixel( v ) > 0 );
196 }
197
198 // -------------------------------------------------------------------------
199 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
200 unsigned long
201 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
202 _GetMark( const TVertex& v ) const
203 {
204   return( ( unsigned long )( this->GetMarks( )->GetPixel( v ) ) );
205 }
206
207 // -------------------------------------------------------------------------
208 template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
209 bool
210 fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
211 _Mark( const TVertex& v, unsigned long frontId )
212 {
213   this->GetMarks( )->SetPixel( v, TFrontId( frontId ) );
214   return( true );
215 }
216
217 #endif // __fpa__Image__Algorithm__hxx__
218
219 // eof - $RCSfile$