ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkImageRegionReverseConstIterator.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkImageRegionReverseConstIterator_h
19#define itkImageRegionReverseConstIterator_h
20
23#include <type_traits> // For remove_const_t.
24
25namespace itk
26{
103template <typename TImage>
104class ITK_TEMPLATE_EXPORT ImageRegionReverseConstIterator : public ImageReverseConstIterator<TImage>
105{
106public:
110
113 using typename Superclass::IndexType;
114
117 using typename Superclass::SizeType;
118
121 using typename Superclass::OffsetType;
122
124 using typename Superclass::RegionType;
125
128 using typename Superclass::ImageType;
129
133 using typename Superclass::PixelContainer;
134 using PixelContainerPointer = typename PixelContainer::Pointer;
135
137 using typename Superclass::InternalPixelType;
138
140 using typename Superclass::PixelType;
141
144 using typename Superclass::AccessorType;
145
147 itkOverrideGetNameOfClassMacro(ImageRegionReverseConstIterator);
148
153
156 ImageRegionReverseConstIterator(const TImage * ptr, const RegionType & region)
157 : Superclass(ptr, region)
159 , m_SpanEndOffset(this->m_BeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]))
160 {}
161
170 : Superclass(it)
171 , m_SpanEndOffset(m_SpanBeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]))
172 {
173 IndexType ind = this->GetIndex();
174
175 m_SpanBeginOffset = this->m_Offset + static_cast<OffsetValueType>(this->m_Region.GetSize()[0]) -
176 (ind[0] - this->m_Region.GetIndex()[0]);
177 }
178
182 : Superclass(it)
183 , m_SpanEndOffset(m_SpanBeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]))
184 {
185 IndexType ind = this->GetIndex();
186
187 m_SpanBeginOffset = this->m_Offset + static_cast<OffsetValueType>(this->m_Region.GetSize()[0]) -
188 (ind[0] - this->m_Region.GetIndex()[0]);
189 }
190
194 : Superclass(it)
195 , m_SpanEndOffset(m_SpanBeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]))
196 {
197 IndexType ind = this->GetIndex();
198
199 m_SpanBeginOffset = this->m_Offset + static_cast<OffsetValueType>(this->m_Region.GetSize()[0]) -
200 (ind[0] - this->m_Region.GetIndex()[0]);
201 }
202
205 void
207 {
209
210 // reset the span offsets
212 m_SpanEndOffset = this->m_BeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]);
213 }
214
217 void
219 {
221
222 // reset the span offsets
224 m_SpanBeginOffset = m_SpanEndOffset + static_cast<OffsetValueType>(this->m_Region.GetSize()[0]);
225 }
226
230 void
231 SetIndex(const IndexType & ind) override
232 {
234 m_SpanBeginOffset = this->m_Offset + static_cast<OffsetValueType>(this->m_Region.GetSize()[0]) -
235 (ind[0] - this->m_Region.GetIndex()[0]);
236 m_SpanEndOffset = m_SpanBeginOffset - static_cast<OffsetValueType>(this->m_Region.GetSize()[0]);
237 }
238
247 Self &
249 {
250 if (--this->m_Offset <= m_SpanEndOffset)
251 {
252 // We have past the beginning of the span (row), need to wrap around.
253
254 // First move forward one pixel, because we are going to use a different
255 // algorithm to compute the next pixel
256 this->m_Offset++;
257
258 // Get the index of the first pixel on the span (row)
260 this->m_Image->ComputeIndex(static_cast<OffsetValueType>(this->m_Offset));
261
262 const typename ImageConstIterator<TImage>::IndexType & startIndex = this->m_Region.GetIndex();
263 const typename ImageConstIterator<TImage>::SizeType & size = this->m_Region.GetSize();
264
265 // Decrement along a row, then wrap at the beginning of the region row.
266
267 // Check to see if we are past the first pixel in the region
268 // Note that --ind[0] moves to the previous pixel along the row.
269 bool done = (--ind[0] == startIndex[0] - 1);
270 for (unsigned int i = 1; done && i < this->ImageIteratorDimension; ++i)
271 {
272 done = (ind[i] == startIndex[i]);
273 }
274
275 // if the iterator is outside the region (but not past region begin) then
276 // we need to wrap around the region
277 unsigned int dim = 0;
278 if (!done)
279 {
280 while ((dim < this->ImageIteratorDimension - 1) && (ind[dim] < startIndex[dim]))
281 {
282 ind[dim] = startIndex[dim] + static_cast<OffsetValueType>(size[dim]) - 1;
283 ind[++dim]--;
284 }
285 }
286 this->m_Offset = this->m_Image->ComputeOffset(ind);
288 m_SpanEndOffset = m_SpanBeginOffset - static_cast<OffsetValueType>(size[0]);
289 }
290 return *this;
291 }
292
301 Self &
303 {
304 if (++this->m_Offset >= m_SpanBeginOffset)
305 {
306 // We have reached the end of the span (row), need to wrap around.
307
308 // First back up one pixel, because we are going to use a different
309 // algorithm to compute the next pixel
310 --this->m_Offset;
311
312 // Get the index of the last pixel on the span (row)
314 this->m_Image->ComputeIndex(static_cast<OffsetValueType>(this->m_Offset));
315
316 const typename ImageIterator<TImage>::IndexType & startIndex = this->m_Region.GetIndex();
317 const typename ImageIterator<TImage>::SizeType & size = this->m_Region.GetSize();
318
319 // Increment along a row, then wrap at the end of the region row.
320 // Check to see if we are past the last pixel in the region
321 // Note that ++ind[0] moves to the next pixel along the row.
322 bool done = (++ind[0] == startIndex[0] + static_cast<OffsetValueType>(size[0]));
323 for (unsigned int i = 1; done && i < this->ImageIteratorDimension; ++i)
324 {
325 done = (ind[i] == startIndex[i] + static_cast<OffsetValueType>(size[i]) - 1);
326 }
327
328 // if the iterator is outside the region (but not past region end) then
329 // we need to wrap around the region
330 unsigned int dim = 0;
331 if (!done)
332 {
333 while ((dim < this->ImageIteratorDimension - 1) &&
334 (ind[dim] > startIndex[dim] + static_cast<OffsetValueType>(size[dim]) - 1))
335 {
336 ind[dim] = startIndex[dim];
337 ind[++dim]++;
338 }
339 }
340 this->m_Offset = this->m_Image->ComputeOffset(ind);
342 m_SpanEndOffset = this->m_Offset - static_cast<OffsetValueType>(size[0]);
343 }
344 return *this;
345 }
346
347protected:
348 SizeValueType m_SpanBeginOffset{}; // offset to last pixel in the row
349 SizeValueType m_SpanEndOffset{}; // offset to one pixel before the row
350};
351
352// Deduction guide for class template argument deduction (CTAD).
353template <typename TImage>
354ImageRegionReverseConstIterator(SmartPointer<TImage>, const typename TImage::RegionType &)
356
357} // end namespace itk
358
359#endif
A multi-dimensional image iterator templated over image type.
typename TImage::IndexType IndexType
typename TImage::SizeType SizeType
typename TImage::IndexType IndexType
typename TImage::SizeType SizeType
A multi-dimensional iterator templated over image type that walks a region of pixels.
A multi-dimensional image iterator designed to walk a specified image region in reverse.
ImageRegionReverseConstIterator(const TImage *ptr, const RegionType &region)
ImageRegionReverseConstIterator(const ImageRegionIterator< TImage > &it)
ImageRegionReverseConstIterator(const ImageReverseConstIterator< TImage > &it)
ImageRegionReverseConstIterator(const ImageConstIterator< TImage > &it)
virtual void SetIndex(const IndexType &ind)
static constexpr unsigned int ImageIteratorDimension
typename TImage::AccessorType AccessorType
typename TImage::PixelContainer PixelContainer
typename TImage::InternalPixelType InternalPixelType
Implements transparent reference counting.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ImageRegionReverseConstIterator(SmartPointer< TImage >, const typename TImage::RegionType &) -> ImageRegionReverseConstIterator< std::remove_const_t< TImage > >
unsigned long SizeValueType
Definition itkIntTypes.h:86
long OffsetValueType
Definition itkIntTypes.h:97