ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkRLEImageConstIterator.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 itkRLEImageConstIterator_h
19#define itkRLEImageConstIterator_h
20
21#include "itkImage.h"
26#include "itkIndex.h"
27#include "itkNumericTraits.h"
28#include "itkRLEImage.h"
29
30class MultiLabelMeshPipeline;
31
32namespace itk
33{
40
41template <typename TPixel, unsigned int VImageDimension, typename CounterType>
42class ImageConstIterator<RLEImage<TPixel, VImageDimension, CounterType>>
43{
44 friend class ::MultiLabelMeshPipeline;
45
46public:
49
54 static constexpr unsigned int ImageIteratorDimension = VImageDimension;
55
57 itkVirtualGetNameOfClassMacro(ImageConstIterator);
58
60 using ImageType = RLEImage<TPixel, VImageDimension, CounterType>;
61
63 using RLLine = typename ImageType::RLLine;
64
66 using BufferType = typename ImageType::BufferType;
67
69 using BufferIterator = ImageRegionIterator<BufferType>;
70
72 using IndexType = typename ImageType::IndexType;
73
75 using IndexValueType = typename ImageType::IndexValueType;
76
78 using SizeType = typename ImageType::SizeType;
79
81 using OffsetType = typename ImageType::OffsetType;
82
84 using RegionType = typename ImageType::RegionType;
85
87 using InternalPixelType = typename ImageType::InternalPixelType;
88
90 using PixelType = typename ImageType::PixelType;
91
95 : m_RunLengthLine(nullptr)
96 , m_Buffer(nullptr)
97 {
98 m_Image = nullptr;
99 m_Index0 = 0;
100 m_BeginIndex0 = 0;
101 m_EndIndex0 = 0;
102 m_RealIndex = 0;
103 m_SegmentRemainder = 0;
104 }
105
107 virtual ~ImageConstIterator() = default;
110 ImageConstIterator(const Self & it)
111 : m_Buffer(const_cast<ImageType *>(it.GetImage())->GetBuffer())
112 {
113 m_RunLengthLine = it.m_RunLengthLine;
114 m_Image = it.m_Image; // copy the smart pointer
115 m_Index0 = it.m_Index0;
116 this->m_BI = it.m_BI;
117
118 m_RealIndex = it.m_RealIndex;
119 m_SegmentRemainder = it.m_SegmentRemainder;
120 m_BeginIndex0 = it.m_BeginIndex0;
121 m_EndIndex0 = it.m_EndIndex0;
122 }
123
126 ImageConstIterator(const ImageType * ptr, const RegionType & region)
127 : m_Buffer(const_cast<ImageType *>(ptr)->GetBuffer())
128 {
129 m_Image = ptr;
130 SetRegion(region);
131 }
132
135 Self &
136 operator=(const Self & it)
137 {
138 if (this != &it)
139 {
140 m_Buffer = it.m_Buffer;
141 m_RunLengthLine = it.m_RunLengthLine;
142 m_Image = it.m_Image; // copy the smart pointer
143 m_Index0 = it.m_Index0;
144 m_BI = it.m_BI;
145
146 m_RealIndex = it.m_RealIndex;
147 m_SegmentRemainder = it.m_SegmentRemainder;
148 m_BeginIndex0 = it.m_BeginIndex0;
149 m_EndIndex0 = it.m_EndIndex0;
150 }
151 return *this;
152 }
153
155 virtual void
156 SetRegion(const RegionType & region)
157 {
158 // m_Region = region;
159
160 if (region.GetNumberOfPixels() > 0) // If region is non-empty
161 {
162 const RegionType & bufferedRegion = m_Image->GetBufferedRegion();
163 itkAssertOrThrowMacro((bufferedRegion.IsInside(region)),
164 "Region " << region << " is outside of buffered region " << bufferedRegion);
165 }
166
167 m_BI = BufferIterator(m_Buffer, region.Slice(0));
168 m_Index0 = region.GetIndex(0);
169 m_BeginIndex0 = m_Index0 - m_Image->GetBufferedRegion().GetIndex(0);
170 m_EndIndex0 = m_BeginIndex0 + region.GetSize(0);
171 SetIndexInternal(m_BeginIndex0); // sets m_RealIndex and m_SegmentRemainder
172 }
173
175 static unsigned int
177 {
178 return VImageDimension;
179 }
180
183 bool
184 operator!=(const Self & it) const
185 {
186 return m_BI != it.m_BI || m_Index0 + m_BeginIndex0 != it.m_Index0 + it.m_BeginIndex0;
187 }
188
191 bool
192 operator==(const Self & it) const
193 {
194 return m_BI == it.m_BI && m_Index0 + m_BeginIndex0 == it.m_Index0 + it.m_BeginIndex0;
195 }
196
199 bool
200 operator<=(const Self & it) const
201 {
202 if (m_BI < it.m_BI)
203 {
204 return true;
205 }
206 else if (m_BI > it.m_BI)
207 {
208 return false;
209 }
210 return m_Index0 + m_BeginIndex0 <= it.m_Index0 + it.m_BeginIndex0;
211 }
212
215 bool
216 operator<(const Self & it) const
217 {
218 if (m_BI < it.m_BI)
219 {
220 return true;
221 }
222 else if (m_BI > it.m_BI)
223 {
224 return false;
225 }
226 return m_Index0 + m_BeginIndex0 < it.m_Index0 + it.m_BeginIndex0;
227 }
228
231 bool
232 operator>=(const Self & it) const
233 {
234 if (m_BI > it.m_BI)
235 {
236 return true;
237 }
238 else if (m_BI < it.m_BI)
239 {
240 return false;
241 }
242 return m_Index0 + m_BeginIndex0 >= it.m_Index0 + it.m_BeginIndex0;
243 }
244
247 bool
248 operator>(const Self & it) const
249 {
250 if (m_BI > it.m_BI)
251 {
252 return true;
253 }
254 else if (m_BI < it.m_BI)
255 {
256 return false;
257 }
258 return m_Index0 + m_BeginIndex0 > it.m_Index0 + it.m_BeginIndex0;
259 }
260
262 const IndexType
263 GetIndex() const
264 {
265 IndexType indR(m_Image->GetBufferedRegion().GetIndex());
266
267 indR[0] += m_Index0;
268 typename BufferType::IndexType bufInd = m_BI.ComputeIndex();
269 for (IndexValueType i = 1; i < VImageDimension; i++)
270 {
271 indR[i] = bufInd[i - 1];
272 }
273 return indR;
274 }
275
277 virtual void
278 SetIndex(const IndexType & ind)
279 {
280 typename BufferType::IndexType bufInd;
281 for (IndexValueType i = 1; i < VImageDimension; i++)
282 {
283 bufInd[i - 1] = ind[i];
284 }
285 m_BI.SetIndex(bufInd);
286 SetIndexInternal(ind[0] - m_Image->GetBufferedRegion().GetIndex(0));
287 }
288
291 const RegionType
292 GetRegion() const
293 {
294 RegionType r;
295
296 r.SetIndex(0, m_BeginIndex0 + m_Image->GetBufferedRegion().GetIndex(0));
297 r.SetSize(0, m_EndIndex0 - m_BeginIndex0);
298 typename BufferType::RegionType ir = m_BI.GetRegion();
299 for (IndexValueType i = 1; i < VImageDimension; i++)
300 {
301 r.SetIndex(i, ir.GetIndex(i - 1));
302 r.SetSize(i, ir.GetSize(i - 1));
303 }
304 return r;
305 }
306
308 const ImageType *
309 GetImage() const
310 {
311 return m_Image.GetPointer();
312 }
313
316 Get() const
317 {
318 return Value();
319 }
320
324 const PixelType &
325 Value() const
326 {
327 RLLine & line = const_cast<Self *>(this)->m_BI.Value();
328
329 return line[m_RealIndex].second;
330 }
331
334 void
335 GoToBegin()
336 {
337 m_BI.GoToBegin();
338 SetIndexInternal(m_BeginIndex0);
339 }
340
343 void
344 GoToEnd()
345 {
346 m_BI.GoToEnd();
347 m_Index0 = m_BeginIndex0;
348 }
349
352 bool
353 IsAtBegin() const
354 {
355 return m_Index0 == m_BeginIndex0 && m_BI.IsAtBegin();
356 }
357
360 bool
361 IsAtEnd() const
362 {
363 return m_Index0 == m_BeginIndex0 && m_BI.IsAtEnd();
364 }
365
366protected: // made protected so other iterators can access
368 virtual void
369 SetIndexInternal(const IndexValueType ind0)
370 {
371 m_Index0 = ind0;
372 m_RunLengthLine = &m_BI.Value();
373
374 CounterType t = 0;
375 SizeValueType x = 0;
376 for (; x < (*m_RunLengthLine).size(); x++)
377 {
378 t += (*m_RunLengthLine)[x].first;
379 if (t > m_Index0)
380 {
381 break;
382 }
383 }
384 m_RealIndex = x;
385 m_SegmentRemainder = t - m_Index0;
386 } // SetIndexInternal
387
388 typename ImageType::ConstWeakPointer m_Image;
389
390 IndexValueType m_Index0; // index into the RLLine
391
392 const RLLine * m_RunLengthLine;
393
394 mutable SizeValueType m_RealIndex; // index into line's segment
395 mutable IndexValueType m_SegmentRemainder; // how many pixels remain in current segment
396
397 IndexValueType m_BeginIndex0; // index to first pixel in region in relation to buffer start
398 IndexValueType m_EndIndex0; // index to one pixel past last pixel in region in relation to buffer start
399 BufferIterator m_BI; // iterator over internal buffer image
400
401 typename BufferType::Pointer m_Buffer;
402};
403
404template <typename TPixel, unsigned int VImageDimension, typename CounterType>
405class ImageConstIteratorWithIndex<RLEImage<TPixel, VImageDimension, CounterType>>
406 : public ImageConstIterator<RLEImage<TPixel, VImageDimension, CounterType>>
407{
408 // just inherit constructors
409
410public:
413
415
416 void
418 {
419 this->m_BI.GoToReverseBegin();
420 this->m_Index0 = this->m_EndIndex0 - 1;
421 SetIndexInternal(this->m_Index0);
422 }
423
424 bool
426 {
427 return this->m_BI.IsAtReverseEnd();
428 }
429
435
439
443 : ImageConstIterator<ImageType>(ptr, region)
444 {}
445}; // no additional implementation required
446
447template <typename TPixel, unsigned int VImageDimension, typename CounterType>
448class ImageConstIteratorWithOnlyIndex<RLEImage<TPixel, VImageDimension, CounterType>>
449 : public ImageConstIteratorWithIndex<RLEImage<TPixel, VImageDimension, CounterType>>
450{
451 // just inherit constructors
452
453public:
456
458
464
471
475 : ImageConstIterator<ImageType>(ptr, region)
476 {}
477}; // no additional implementation required
478} // end namespace itk
479
480#endif // itkRLEImageConstIterator_h
typename itk::ImageConstIterator< RLEImage< TPixel, VImageDimension, CounterType > >::RegionType RegionType
typename itk::ImageConstIterator< RLEImage< TPixel, VImageDimension, CounterType > >::RegionType RegionType
Read-only ImageIterator: ImageConstIterator<TImage> behaves as ImageIterator<const TImage>.
ImageConstIterator Self
virtual void SetIndex(const IndexType &ind)
std::conditional_t< std::is_const_v< TImage >, const InternalPixelType, InternalPixelType > * m_Buffer
detail::IteratorImageType_t< TImage > ImageType
static constexpr unsigned int ImageIteratorDimension
bool operator>=(const ImageIterator< TOtherImage > &it) const
virtual void SetRegion(const RegionType &region)
typename TImage::InternalPixelType InternalPixelType
bool operator>(const ImageIterator< TOtherImage > &it) const
bool operator==(const ImageIterator< TOtherImage > &it) const
bool operator!=(const ImageIterator< TOtherImage > &it) const
bool operator<=(const ImageIterator< TOtherImage > &it) const
bool operator<(const ImageIterator< TOtherImage > &it) const
Run-Length Encoded image. It saves memory for label images at the expense of processing times....
Definition itkRLEImage.h:53
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition itkIntTypes.h:82
long IndexValueType
Definition itkIntTypes.h:85