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