Orfeo Toolbox  3.16
itkNeighborhoodIterator.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkNeighborhoodIterator.txx,v $
5  Language: C++
6  Date: $Date: 2009-02-06 20:53:13 $
7  Version: $Revision: 1.29 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkNeighborhoodIterator_txx
18 #define __itkNeighborhoodIterator_txx
19 namespace itk {
20 
21 template<class TImage, class TBoundaryCondition>
22 void
24 ::SetPixel(const unsigned n, const PixelType& v)
25 {
26  register unsigned int i;
27  OffsetType OverlapLow, OverlapHigh, temp, offset;
28  bool flag;
29 
30  if (this->m_NeedToUseBoundaryCondition == false)
31  { this->m_NeighborhoodAccessorFunctor.Set( this->operator[](n), v ); }
32 
33  // Is this whole neighborhood in bounds?
34  else if (this->InBounds()) this->m_NeighborhoodAccessorFunctor.Set( this->operator[](n), v );
35  else
36  {
37  temp = this->ComputeInternalIndex(n);
38 
39  // Calculate overlap
40  for (i=0; i<Superclass::Dimension; i++)
41  {
42  OverlapLow[i] = this->m_InnerBoundsLow[i] - this->m_Loop[i];
43  OverlapHigh[i]=
44  static_cast<OffsetValueType>(this->GetSize(i)
45  - ( (this->m_Loop[i]+2) - this->m_InnerBoundsHigh[i]) );
46  }
47 
48  flag = true;
49 
50  // Is this pixel in bounds?
51  for (i=0; i<Superclass::Dimension; ++i)
52  {
53  if (this->m_InBounds[i]) offset[i] = 0; // this dimension in bounds
54  else // part of this dimension spills out of bounds
55  {
56  if (temp[i] < OverlapLow[i])
57  {
58  flag = false;
59  offset[i] = OverlapLow[i] - temp[i];
60  }
61  else if ( OverlapHigh[i] < temp[i] )
62  {
63  flag = false;
64  offset[i] = OverlapHigh[i] - temp[i];
65  }
66  else offset[i] = 0;
67  }
68  }
69 
70  if (flag)
71  {
72  this->m_NeighborhoodAccessorFunctor.Set( this->operator[](n), v );
73  }
74  else
75  { // Attempt to write out of bounds
76  RangeError e(__FILE__, __LINE__);
77  e.SetLocation(ITK_LOCATION);
78  e.SetDescription("Attempt to write out of bounds.");
79  throw e;
80  };
81  }
82 }
83 
84 template<class TImage, class TBoundaryCondition>
85 void
87 ::SetPixel(const unsigned n, const PixelType& v, bool &status)
88 {
89  register unsigned int i;
90  OffsetType temp;
91 
92  typename OffsetType::OffsetValueType OverlapLow, OverlapHigh;
93 
94  if (this->m_NeedToUseBoundaryCondition == false)
95  {
96  status = true;
97  this->m_NeighborhoodAccessorFunctor.Set( this->operator[](n), v );
98  }
99 
100  // Is this whole neighborhood in bounds?
101  else if (this->InBounds())
102  {
103  this->m_NeighborhoodAccessorFunctor.Set( this->operator[](n), v );
104  status = true;
105  return;
106  }
107  else
108  {
109  temp = this->ComputeInternalIndex(n);
110 
111  // Calculate overlap.
112  // Here, we are checking whether the particular pixel in the
113  // neighborhood is within the bounds (when the neighborhood is not
114  // completely in bounds, it is usually partly in bounds)
115  for (i=0; i<Superclass::Dimension; i++)
116  {
117  if (! this->m_InBounds[i]) // Part of dimension spills out of bounds
118  {
119  OverlapLow = this->m_InnerBoundsLow[i] - this->m_Loop[i];
120  OverlapHigh=
121  static_cast<OffsetValueType>(this->GetSize(i)
122  - ( (this->m_Loop[i]+2) - this->m_InnerBoundsHigh[i]) );
123  if (temp[i] < OverlapLow || OverlapHigh < temp[i])
124  {
125  status = false;
126  return;
127  }
128  }
129  }
130 
131  this->m_NeighborhoodAccessorFunctor.Set( this->operator[](n), v );
132  status = true;
133  }
134 }
135 
136 
137 template<class TImage, class TBoundaryCondition>
138 void
140 ::PrintSelf(std::ostream &os, Indent indent) const
141 {
142  os << indent;
143  os << "NeighborhoodIterator {this= " << this << "}" << std::endl;
144  Superclass::PrintSelf(os, indent.GetNextIndent());
145 }
146 
147 template<class TImage, class TBoundaryCondition>
148 void
151 {
152  register unsigned int i;
153  OffsetType OverlapLow, OverlapHigh, temp;
154  bool flag;
155 
156  const Iterator _end = this->End();
157  Iterator this_it;
158  typename NeighborhoodType::ConstIterator N_it;
159 
160  if (this->m_NeedToUseBoundaryCondition == false)
161  {
162  for (N_it = N.Begin(), this_it = this->Begin(); this_it < _end;
163  this_it++, N_it++)
164  {
165  this->m_NeighborhoodAccessorFunctor.Set(*this_it, *N_it);
166  }
167  }
168  else if (this->InBounds())
169  {
170  for (N_it = N.Begin(), this_it = this->Begin(); this_it < _end;
171  this_it++, N_it++)
172  {
173  this->m_NeighborhoodAccessorFunctor.Set(*this_it, *N_it);
174  }
175  }
176  else
177  {
178  // Calculate overlap & initialize index
179  for (i=0; i<Superclass::Dimension; i++)
180  {
181  OverlapLow[i] =this->m_InnerBoundsLow[i] - this->m_Loop[i];
182  OverlapHigh[i]=
183  static_cast<OffsetValueType>(this->GetSize(i) - (this->m_Loop[i]-this->m_InnerBoundsHigh[i])-1);
184  temp[i] = 0;
185  }
186 
187  // Iterate through neighborhood
188  for (N_it = N.Begin(), this_it = this->Begin();
189  this_it < _end; N_it++, this_it++)
190  {
191  flag = true;
192  for (i=0; i<Superclass::Dimension; ++i)
193  {
194  if (!this->m_InBounds[i] && ((temp[i] < OverlapLow[i])
195  || (temp[i] >= OverlapHigh[i])) )
196  {
197  flag=false;
198  break;
199  }
200  }
201 
202  if (flag)
203  {
204  this->m_NeighborhoodAccessorFunctor.Set(*this_it, *N_it);
205  }
206 
207  for (i=0; i<Superclass::Dimension; ++i) // Update index
208  {
209  temp[i]++;
210  if ( (unsigned int)(temp[i]) == this->GetSize(i) ) temp[i]= 0;
211  else break;
212  }
213  }
214 
215  }
216 }
217 
218 } // namespace itk
219 
220 #endif

Generated at Sat Feb 2 2013 23:56:14 for Orfeo Toolbox with doxygen 1.8.1.1