OTB  6.7.0
Orfeo Toolbox
otbSubsampledImageRegionConstIterator.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  * Copyright (C) 2007-2012 Institut Mines Telecom / Telecom Bretagne
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 
23 #ifndef otbSubsampledImageRegionConstIterator_hxx
24 #define otbSubsampledImageRegionConstIterator_hxx
25 
27 
28 namespace otb {
29 
30 template <class TImage>
33  : itk::ImageRegionConstIterator<TImage> ()
34 {
35  m_SubsampleFactor.Fill(1);
36  m_SubSampledEndOffset = this->m_EndOffset;
37 
38  const IndexType& startIndex = this->m_Region.GetIndex();
39  const SizeType& size = this->m_Region.GetSize();
40 
41  for (unsigned int i = 0; i < ImageIteratorDimension; ++i)
42  {
43  m_FirstUsableIndex[i] = startIndex[i];
44  m_LastUsableIndex[i] = startIndex[i] + static_cast<IndexValueType>(size[i] - 1);
45  }
46 }
47 
48 template <class TImage>
51  : itk::ImageRegionConstIterator<TImage> (ptr, region)
52 {
53  m_SubsampleFactor.Fill(1);
54  m_SubSampledEndOffset = this->m_EndOffset;
55 
56  const IndexType& startIndex = this->m_Region.GetIndex();
57  const SizeType& size = this->m_Region.GetSize();
58 
59  for (unsigned int i = 0; i < ImageIteratorDimension; ++i)
60  {
61  m_FirstUsableIndex[i] = startIndex[i];
62  m_LastUsableIndex[i] = startIndex[i] + static_cast<IndexValueType>(size[i] - 1);
63  }
64 }
65 
66 template <class TImage>
69  : itk::ImageRegionConstIterator<TImage>(it)
70 {
71  m_SubsampleFactor.Fill(1);
72  m_SubSampledEndOffset = this->m_EndOffset;
73 
74  const IndexType& startIndex = this->m_Region.GetIndex();
75  const SizeType& size = this->m_Region.GetSize();
76 
77  for (unsigned int i = 0; i < ImageIteratorDimension; ++i)
78  {
79  m_FirstUsableIndex[i] = startIndex[i];
80  m_LastUsableIndex[i] = startIndex[i] + static_cast<IndexValueType>(size[i] - 1);
81  }
82 }
83 
84 template <class TImage>
87  : itk::ImageRegionConstIterator<TImage>(it)
88 {
89  m_SubsampleFactor.Fill(1);
90 
91  const IndexType& startIndex = this->m_Region.GetIndex();
92  const SizeType& size = this->m_Region.GetSize();
93 
94  for (unsigned int i = 0; i < ImageIteratorDimension; ++i)
95  {
96  m_FirstUsableIndex[i] = startIndex[i];
97  m_LastUsableIndex[i] = startIndex[i] + static_cast<IndexValueType>(size[i] - 1);
98  }
99 
100  m_SubSampledEndOffset = this->m_Image->ComputeOffset(m_LastUsableIndex) + 1;
101 }
102 
103 template <class TImage>
104 void
106 ::SetSubsampleFactor(typename IndexType::IndexValueType factor)
107 {
108  IndexType index;
109  index.Fill(factor);
110  SetSubsampleFactor(index);
111 }
112 
113 template <class TImage>
114 void
117 {
118  this->m_SubsampleFactor = factor;
119 
120  // Evaluate the last possible pixel.
121  const IndexType& startIndex = this->m_Region.GetIndex();
122  const SizeType& size = this->m_Region.GetSize();
123 
124  for (unsigned int i = 0; i < ImageIteratorDimension; ++i)
125  {
126  m_FirstUsableIndex[i] = startIndex[i];
127  while (m_FirstUsableIndex[i]
128  != (m_SubsampleFactor[i] * (m_FirstUsableIndex[i] / m_SubsampleFactor[i])))
129  {
130  ++m_FirstUsableIndex[i];
131  }
132  m_LastUsableIndex[i] = startIndex[i]
133  + static_cast<IndexValueType>(m_SubsampleFactor[i] * ((size[i] - 1) / m_SubsampleFactor[i]));
134  }
135 
136  m_SubSampledBeginOffset = this->m_Image->ComputeOffset(m_FirstUsableIndex);
137 
138  //m_SubSampledReverseEndOffset = m_SubSampledBeginOffset - 1;
139  m_SubSampledEndOffset = this->m_Image->ComputeOffset(m_LastUsableIndex) + 1;
140 }
141 
142 template <class TImage>
143 void
146 {
147  this->m_Offset = m_SubSampledBeginOffset;
148 
149  const SizeType& size = this->m_Region.GetSize();
150 
151  this->m_SpanBeginOffset = this->m_Offset;
152  this->m_SpanEndOffset = this->m_Offset
153  + static_cast<IndexValueType>(m_SubsampleFactor[0] * ((size[0] - 1) / m_SubsampleFactor[0]))
154  + 1;
155 }
156 
157 template <class TImage>
158 void
161 {
162  this->m_Offset = m_SubSampledEndOffset - 1;
163  this->m_SpanEndOffset = this->m_Offset + 1;
164  this->m_SpanBeginOffset = this->m_Offset - m_LastUsableIndex[0];
165 }
166 
167 template <class TImage>
168 void
171 {
172  IndexType theIndex = ind;
173 
174  for (unsigned int i = 0; i < ImageIteratorDimension; ++i)
175  {
176  while (theIndex[i]
177  != (m_SubsampleFactor[i] * (theIndex[i] / m_SubsampleFactor[i])))
178  {
179  ++theIndex[i];
180  }
181 
182  if (theIndex[i] > static_cast<IndexValueType>(this->m_Region.GetIndex()[i] + this->m_Region.GetSize()[i]))
183  {
184  theIndex[i] = ind[i];
185  while (theIndex[i]
186  != (m_SubsampleFactor[i] * (theIndex[i] / m_SubsampleFactor[i])))
187  {
188  --theIndex[i];
189  }
190 
191  if (theIndex[i] < this->m_Region.GetIndex()[i]) theIndex[i] = ind[i];
192  }
193  }
194  //Superclass::SetIndex( theIndex );
195 
196  OffsetType theOffset = this->m_Image->ComputeOffset(theIndex);
197  SetOffset(theOffset);
198 }
199 
200 template <class TImage>
201 void
203 ::SetOffset(const OffsetType& offset)
204 {
205  this->m_Offset = offset;
206 
207  const SizeType& size = this->m_Region.GetSize();
208 
209  this->m_SpanBeginOffset = this->m_Offset;
210  this->m_SpanEndOffset = this->m_Offset
211  + static_cast<IndexValueType>(m_SubsampleFactor[0] * ((size[0] - 1) / m_SubsampleFactor[0]))
212  + 1;
213 
214 }
215 
216 template <class TImage>
220 {
221  IndexType startIndex = this->m_Region.GetIndex();
222  SizeType size = this->m_Region.GetSize();
223 
224  for (unsigned int i = 0; i < ImageIteratorDimension; ++i)
225  {
226  startIndex[i] /= m_SubsampleFactor[i];
227  --size[i];
228  size[i] /= m_SubsampleFactor[i];
229  ++size[i];
230  }
231 
232  RegionType newRegion;
233  newRegion.SetIndex(startIndex);
234  newRegion.SetSize(size);
235 
236 #if 0
237 // #ifndef NDEBUG
238  std::cerr << "InitialImageSize (";
239  for (unsigned int i = 0; i < ImageIteratorDimension - 1; ++i)
240  std::cerr << this->m_Region.GetSize()[i] << ", ";
241  std::cerr << this->m_Region.GetSize()[ImageIteratorDimension - 1] << ") with index (";
242  for (unsigned int i = 0; i < ImageIteratorDimension - 1; ++i)
243  std::cerr << this->m_Region.GetIndex()[i] << ", ";
244  std::cerr << this->m_Region.GetIndex()[ImageIteratorDimension - 1] << ")\n";
245 
246  std::cerr << "NewRegionSize (";
247  for (unsigned int i = 0; i < ImageIteratorDimension - 1; ++i)
248  std::cerr << size[i] << ", ";
249  std::cerr << size[ImageIteratorDimension - 1] << ") with index (";
250  for (unsigned int i = 0; i < ImageIteratorDimension - 1; ++i)
251  std::cerr << startIndex[i] << ", ";
252  std::cerr << startIndex[ImageIteratorDimension - 1] << ")\n";
253 
254  std::cerr << "FirstIndex (";
255  for (unsigned int i = 0; i < ImageIteratorDimension - 1; ++i)
256  std::cerr << m_FirstUsableIndex[i] << ", ";
257  std::cerr << m_FirstUsableIndex[ImageIteratorDimension - 1] << ") offset=";
258  std::cerr << m_SubSampledBeginOffset << "\n";
259 
260  std::cerr << "LastIndex (";
261  for (unsigned int i = 0; i < ImageIteratorDimension - 1; ++i)
262  std::cerr << m_LastUsableIndex[i] << ", ";
263  std::cerr << m_LastUsableIndex[ImageIteratorDimension - 1] << ") offset=";
264  std::cerr << m_SubSampledEndOffset << "\n";
265 #endif
266 
267  return newRegion;
268 }
269 
270 template <class TImage>
271 void
274 {
275  // Get the index of the last pixel on the span (row)
276  IndexType ind = this->m_Image->ComputeIndex(
277  static_cast<typename TImage::OffsetValueType>(this->m_Offset));
278 
279  const IndexType& startIndex = this->m_Region.GetIndex();
280  const SizeType& size = this->m_Region.GetSize();
281 
282  // Increment along a row, then wrap at the end of the region row.
283  unsigned int dim;
284 
285  // Check to see if we are past the last pixel in the region
286  // Note that ++ind[0] moves to the next pixel along the row.
287  ind[0] += m_SubsampleFactor[0];
288  bool done = (ind[0] > m_LastUsableIndex[0]);
289  for (unsigned int i = 1; done && i < ImageIteratorDimension; ++i)
290  {
291  done = (ind[i] >= m_LastUsableIndex[i]);
292  }
293 
294  // if the iterator is outside the region (but not past region end) then
295  // we need to wrap around the region
296  dim = 0;
297  if (!done)
298  {
299  while ((dim + 1 < ImageIteratorDimension)
300  && (ind[dim] > m_LastUsableIndex[dim]))
301  {
302  ind[dim] = startIndex[dim];
303  ++dim;
304  ind[dim] += m_SubsampleFactor[dim];
305  }
306  }
307  this->m_Offset = this->m_Image->ComputeOffset(ind);
308  this->m_SpanEndOffset = this->m_Offset
309  + static_cast<IndexValueType>(m_SubsampleFactor[0] * ((size[0] - 1) / m_SubsampleFactor[0]))
310  + 1;
311  this->m_SpanBeginOffset = this->m_Offset;
312 }
313 
314 template <class TImage>
315 void
318 {
319  // Get the index of the first pixel on the span (row)
320  IndexType ind = this->m_Image->ComputeIndex(static_cast<IndexValueType>(this->m_Offset));
321  const IndexType& startIndex = this->m_Region.GetIndex();
322 
323  // Deccrement along a row, then wrap at the beginning of the region row.
324  bool done;
325  unsigned int dim;
326 
327  // Check to see if we are past the first pixel in the region
328  // Note that --ind[0] moves to the previous pixel along the row.
329  ind[0] -= m_SubsampleFactor[0];
330  done = (ind[0] <= startIndex[0] - 1);
331  for (unsigned int i = 1; done && i < ImageIteratorDimension; ++i)
332  {
333  done = (ind[i] <= startIndex[i]);
334  }
335 
336  // if the iterator is outside the region (but not past region begin) then
337  // we need to wrap around the region
338  dim = 0;
339  if (!done)
340  {
341  while ((dim < ImageIteratorDimension - 1)
342  && (ind[dim] < startIndex[dim]))
343  {
344  ind[dim] = m_LastUsableIndex[dim];
345  ++dim;
346  ind[dim] -= m_SubsampleFactor[dim];
347  }
348  }
349  this->m_Offset = this->m_Image->ComputeOffset(ind);
350  this->m_SpanEndOffset = this->m_Offset + 1;
351  this->m_SpanBeginOffset = this->m_Offset - m_LastUsableIndex[0];
352 }
353 
354 } // end of namespace otb
355 
356 #endif
TImage::IndexType IndexType
TImage::RegionType RegionType
TImage::OffsetType OffsetType
TImage::SizeType SizeType