ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkImageConstIterator.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 itkImageConstIterator_h
19#define itkImageConstIterator_h
20
21#include "itkImage.h"
22#include "itkIndex.h"
23#include "itkNumericTraits.h"
24#include <type_traits> // For remove_const_t.
25
26namespace itk
27{
83template <typename TImage>
84class ITK_TEMPLATE_EXPORT ImageConstIterator
86public:
94 static constexpr unsigned int ImageIteratorDimension = TImage::ImageDimension;
97 itkVirtualGetNameOfClassMacro(ImageConstIterator);
98
100 using IndexType = typename TImage::IndexType;
101
103 using SizeType = typename TImage::SizeType;
104
106 using OffsetType = typename TImage::OffsetType;
107
109 using RegionType = typename TImage::RegionType;
110
112 using ImageType = TImage;
113
117 using PixelContainer = typename TImage::PixelContainer;
118 using PixelContainerPointer = typename PixelContainer::Pointer;
119
121 using InternalPixelType = typename TImage::InternalPixelType;
122
124 using PixelType = typename TImage::PixelType;
125
128 using AccessorType = typename TImage::AccessorType;
129 using AccessorFunctorType = typename TImage::AccessorFunctorType;
142
144 virtual ~ImageConstIterator() = default;
145
149 : m_Image(it.m_Image)
150 , m_Region(it.m_Region)
151 , m_Offset(it.m_Offset)
154 , m_Buffer(it.m_Buffer)
157 {
158 // copy the smart pointer
159
160
162 }
163
166 ImageConstIterator(const TImage * ptr, const RegionType & region)
167 : m_Image(ptr)
168 , m_Buffer(m_Image->GetBufferPointer())
169 , m_PixelAccessor(ptr->GetPixelAccessor())
170 {
171
172
173 SetRegion(region);
174
175
176 m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
178 }
179
182 Self &
183 operator=(const Self & it)
184 {
185 if (this != &it)
186 {
187 m_Image = it.m_Image; // copy the smart pointer
188 m_Region = it.m_Region;
189
190 m_Buffer = it.m_Buffer;
191 m_Offset = it.m_Offset;
197 }
198 return *this;
199 }
200
202 virtual void
203 SetRegion(const RegionType & region)
204 {
205 m_Region = region;
206
207 if (region.GetNumberOfPixels() > 0) // If region is non-empty
208 {
209 const RegionType & bufferedRegion = m_Image->GetBufferedRegion();
210 itkAssertOrThrowMacro((bufferedRegion.IsInside(m_Region)),
211 "Region " << m_Region << " is outside of buffered region " << bufferedRegion);
212 }
213
214 // Compute the start offset
215 m_Offset = m_Image->ComputeOffset(m_Region.GetIndex());
217
218 // Compute the end offset. If any component of m_Region.GetSize()
219 // is zero, the region is not valid and we set the EndOffset
220 // to be same as BeginOffset so that iterator end condition is met
221 // immediately.
222 IndexType ind(m_Region.GetIndex());
223 SizeType size(m_Region.GetSize());
224 if (m_Region.GetNumberOfPixels() == 0)
225 {
226 // region is empty, probably has a size of 0 along one dimension
228 }
229 else
230 {
231 for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
232 {
233 ind[i] += (static_cast<IndexValueType>(size[i]) - 1);
234 }
235 m_EndOffset = m_Image->ComputeOffset(ind);
236 ++m_EndOffset;
237 }
238 }
239
241 static unsigned int
243 {
244 return TImage::ImageDimension;
245 }
246
249 bool
250 operator==(const Self & it) const
251 {
252 // two iterators are the same if they "point to" the same memory location
253 return (m_Buffer + m_Offset) == (it.m_Buffer + it.m_Offset);
254 }
255
257
260 bool
261 operator<=(const Self & it) const
262 {
263 // an iterator is "less than" another if it "points to" a lower
264 // memory location
265 return (m_Buffer + m_Offset) <= (it.m_Buffer + it.m_Offset);
266 }
267
270 bool
271 operator<(const Self & it) const
272 {
273 // an iterator is "less than" another if it "points to" a lower
274 // memory location
275 return (m_Buffer + m_Offset) < (it.m_Buffer + it.m_Offset);
276 }
277
280 bool
281 operator>=(const Self & it) const
282 {
283 // an iterator is "greater than" another if it "points to" a higher
284 // memory location
285 return (m_Buffer + m_Offset) >= (it.m_Buffer + it.m_Offset);
286 }
287
290 bool
291 operator>(const Self & it) const
292 {
293 // an iterator is "greater than" another if it "points to" a higher
294 // memory location
295 return (m_Buffer + m_Offset) > (it.m_Buffer + it.m_Offset);
296 }
297
300 [[nodiscard]] IndexType
302 {
303 return m_Image->ComputeIndex(m_Offset);
304 }
305
306#ifndef ITK_FUTURE_LEGACY_REMOVE
309 ITK_FUTURE_DEPRECATED(
310 "Please use `ComputeIndex()` instead, or use an iterator with index, like `ImageIteratorWithIndex`!")
311 [[nodiscard]] IndexType
312 GetIndex() const
313 {
314 return this->ComputeIndex();
315 }
316#endif
317
320 virtual void
321 SetIndex(const IndexType & ind)
322 {
323 m_Offset = m_Image->ComputeOffset(ind);
324 }
325
328 [[nodiscard]] const RegionType &
329 GetRegion() const
330 {
331 return m_Region;
332 }
333
335 [[nodiscard]] const ImageType *
336 GetImage() const
337 {
338 return m_Image.GetPointer();
339 }
340
342 [[nodiscard]] PixelType
343 Get() const
344 {
346 }
347
351 [[nodiscard]] const PixelType &
352 Value() const
353 {
354 return *(m_Buffer + m_Offset);
355 }
356
359 void
361 {
363 }
364
367 void
369 {
371 }
372
375 [[nodiscard]] bool
376 IsAtBegin() const
377 {
378 return m_Offset == m_BeginOffset;
379 }
380
383 [[nodiscard]] bool
384 IsAtEnd() const
385 {
386 return m_Offset == m_EndOffset;
387 }
388
389protected: // made protected so other iterators can access
390 typename TImage::ConstWeakPointer m_Image{};
391
392 RegionType m_Region{}; // region to iterate over
393
395 OffsetValueType m_BeginOffset{}; // offset to first pixel in region
396 OffsetValueType m_EndOffset{}; // offset to one pixel past last pixel in region
397
399
402};
403
404// Deduction guide for class template argument deduction (CTAD).
405template <typename TImage>
406ImageConstIterator(SmartPointer<TImage>, const typename TImage::RegionType &)
408
409} // end namespace itk
410
411#endif
A multi-dimensional image iterator templated over image type.
typename TImage::IndexType IndexType
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
const RegionType & GetRegion() const
TImage::ConstWeakPointer m_Image
const PixelType & Value() const
virtual ~ImageConstIterator()=default
bool operator==(const Self &it) const
typename TImage::PixelContainer PixelContainer
bool operator>(const Self &it) const
virtual void SetIndex(const IndexType &ind)
typename TImage::SizeType SizeType
bool operator<=(const Self &it) const
typename TImage::OffsetType OffsetType
ImageConstIterator(const TImage *ptr, const RegionType &region)
virtual void SetRegion(const RegionType &region)
typename TImage::InternalPixelType InternalPixelType
typename PixelContainer::Pointer PixelContainerPointer
bool operator<(const Self &it) const
const ImageType * GetImage() const
typename TImage::AccessorType AccessorType
typename TImage::AccessorFunctorType AccessorFunctorType
typename TImage::RegionType RegionType
static unsigned int GetImageIteratorDimension()
AccessorFunctorType m_PixelAccessorFunctor
Self & operator=(const Self &it)
const InternalPixelType * m_Buffer
typename TImage::PixelType PixelType
bool operator>=(const Self &it) const
static constexpr unsigned int ImageIteratorDimension
Implements transparent reference counting.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ImageConstIterator(SmartPointer< TImage >, const typename TImage::RegionType &) -> ImageConstIterator< std::remove_const_t< TImage > >
long OffsetValueType
Definition itkIntTypes.h:97
long IndexValueType
Definition itkIntTypes.h:93