OTB  9.0.0
Orfeo Toolbox
otbZipIterator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbZipIterator_h
22 #define otbZipIterator_h
23 
24 #include "otbIteratorHelpers.h"
25 #include "otbSpan.h"
26 #include "itkMacro.h"
27 #include <type_traits>
28 #include <vector>
29 #include <cassert>
30 
31 namespace otb
32 {
33 namespace internals
34 {
35 
46 template <typename TImageIterator, typename ConstOrMutable>
48 {
49 public:
50 
53 
55  using ImageIteratorType = TImageIterator;
56 
58  using ImageType = typename TImageIterator::ImageType;
59 
64  static constexpr unsigned int ImageIteratorDimension = ImageIteratorType::ImageIteratorDimension;
65 
66  using Self = ZipIterator;
67 
70 
72  using IndexType = typename ImageIteratorType::IndexType;
73 
75  using SizeType = typename ImageIteratorType::SizeType;
76 
78  using OffsetType = typename ImageIteratorType::OffsetType;
79 
81  using RegionType = typename ImageIteratorType::RegionType;
82 
86  using PixelContainer = typename ImageIteratorType::PixelContainer;
87  using PixelContainerPointer = typename PixelContainer::Pointer;
88 
90  using InternalPixelType = typename ImageIteratorType::InternalPixelType;
91 
93  using PixelType = typename ImageIteratorType::PixelType;
94 
97  using AccessorType = typename ImageIteratorType::AccessorType;
98  using AccessorFunctorType = typename ImageIteratorType::AccessorFunctorType;
100 
106  ZipIterator () = default;
107  ~ZipIterator () = default;
108  ZipIterator (ZipIterator const&) = default;
109  ZipIterator (ZipIterator &&) = default;
110  ZipIterator& operator=(ZipIterator const&) = default;
111  ZipIterator& operator=(ZipIterator &&) = default;
113 
118  template <bool IsConst_ = std::is_same<ConstOrMutable, ConstTag>::value, class = std::enable_if<IsConst_>>
120  : m_iterators(rhs.m_iterators())
121  {}
122 
127  template <bool IsConst_ = std::is_same<ConstOrMutable, ConstTag>::value, class = std::enable_if<IsConst_>>
129  : m_iterators(move(rhs.m_iterators()))
130  {}
131 
140  {
141  assert(! images.empty());
142  m_iterators.reserve(images.size());
144 
145  for (auto & im: images)
146  m_iterators.emplace_back(im, region);
147  }
148 
149  // static_assert(std::is_copy_constructible<Self>::value, "Requires copy construction");
150  // static_assert(std::is_trivially_copy_constructible<Self>::value, "Requires trivial copy construction");
152 
155  friend bool operator==(ZipIterator const& lhs, ZipIterator const& rhs)
156  {
157  assert(!lhs.m_iterators.empty());
158  assert(lhs.m_iterators.size() == rhs.m_iterators.size());
160 
161  return lhs.m_iterators.front() == rhs.m_iterators.front();
162  }
163  friend bool operator!=(ZipIterator const& lhs, ZipIterator const& rhs)
164  { return ! (lhs == rhs); }
165 
166  friend bool operator<=(ZipIterator const& lhs, ZipIterator const& rhs)
167  {
168  assert(!lhs.m_iterators.empty());
169  assert(lhs.m_iterators.size() == rhs.m_iterators.size());
170 
171  return lhs.m_iterators.front() <= rhs.m_iterators.front();
172  }
173  friend bool operator<(ZipIterator const& lhs, ZipIterator const& rhs)
174  {
175  assert(!lhs.m_iterators.empty());
176  assert(lhs.m_iterators.size() == rhs.m_iterators.size());
177 
178  return lhs.m_iterators.front() < rhs.m_iterators.front();
179  }
180  friend bool operator>=(ZipIterator const& lhs, ZipIterator const& rhs)
181  { return ! (lhs < rhs); }
182  friend bool operator>(ZipIterator const& lhs, ZipIterator const& rhs)
183  { return ! (lhs <= rhs); }
185 
188  // What about GetIndex() and SetIndex ?
189 
191  auto const& GetRegion() const
192  {
193  assert(!m_iterators.empty());
194  return m_iterators.front().GetRegion();
195  }
197 
199  void SetRegion(RegionType const& region)
200  {
201  for (auto & it : m_iterators)
202  it.SetRegion(region);
203  }
204 
207  for (auto & it : m_iterators)
208  it.GoToBegin();
209  return *this;
210  }
211 
214  for (auto & it : m_iterators)
215  it.GoToEnd();
216  return *this;
217  }
219 
221  bool IsAtBegin() const {
222  assert(!m_iterators.empty());
223  return m_iterators.front().IsAtBegin();
224  }
226 
228  bool IsAtEnd() const {
229  assert(!m_iterators.empty());
230  return m_iterators.front().IsAtEnd();
231  }
233 
238  assert(!IsAtEnd());
239  for (auto & it : m_iterators)
240  ++it;
241  return *this;
242  }
243 
247  Self operator++(int) = delete;
248 
250  using ImageIteratorList_t = std::vector<ImageIteratorType>;
251 
258  {
259 
263  explicit PixelListProxy(ImageIteratorList_t const& iterators) noexcept
264  : m_iterators(iterators)
265  {}
266  bool empty() const noexcept {return m_iterators.empty();}
267  auto size() const noexcept {return m_iterators.size();}
269 
275  struct iterator__
276  {
277  using difference_type = typename ImageIteratorList_t::difference_type;
278  using value_type = decltype(typename ImageIteratorList_t::const_iterator{}->Get());
279  using pointer = value_type*;
281  using iterator_category = std::forward_iterator_tag;
283 
284  explicit iterator__(typename ImageIteratorList_t::const_iterator ref)
285  : reference_to_value(ref){}
286  friend bool operator==(iterator__ const& lhs, iterator__ const& rhs) noexcept
287  { return lhs.reference_to_value == rhs.reference_to_value;}
288  friend bool operator!=(iterator__ const& lhs, iterator__ const& rhs) noexcept
289  { return ! (lhs == rhs);}
290  iterator__ & operator++() noexcept {
292  return *this;
293  }
294  iterator__ & operator--() noexcept {
296  return *this;
297  }
298  iterator__ operator+(std::ptrdiff_t offset) const noexcept{
299  return iterator__{reference_to_value + offset};
300  }
301  iterator__ operator-(std::ptrdiff_t offset) const noexcept{
302  return iterator__{reference_to_value - offset};
303  }
304  decltype(auto) operator*() const {
305  return reference_to_value->Get();
306  }
307 
308  private:
309  typename ImageIteratorList_t::const_iterator reference_to_value;
310  };
311 
312  auto begin() noexcept { return iterator__(m_iterators.begin()); }
313  auto end() noexcept { return iterator__{m_iterators.end()}; }
314  auto begin() const noexcept { return iterator__{m_iterators.begin()}; }
315  auto end() const noexcept { return iterator__{m_iterators.end()}; }
316  auto cbegin() const noexcept { return iterator__{m_iterators.cbegin()}; }
317  auto cend() const noexcept { return iterator__{m_iterators.cend()}; }
318 
319  decltype(auto) operator[](std::size_t idx) const {
320  assert(idx < size());
321  return m_iterators[idx].Get();
322  }
323  decltype(auto) operator[](std::size_t idx) {
324  assert(idx < size());
325  return m_iterators[idx].Get();
326  }
327 
328  decltype(auto) front() const {
329  assert(!empty());
330  return m_iterators.front().Get();
331  }
332  decltype(auto) front() {
333  assert(!empty());
334  return m_iterators.front().Get();
335  }
336  decltype(auto) back() const {
337  assert(!empty());
338  return m_iterators.back().Get();
339  }
340  decltype(auto) back() {
341  assert(!empty());
342  return m_iterators.back().Get();
343  }
344  private:
346  };
347 
351  PixelListProxy Get() const {
352  return PixelListProxy{m_iterators};
353  }
354 
356 
359  template <typename MultiCompPixelType>
360  void Set(MultiCompPixelType const& p)
361  {
362  assert(p.size() == m_iterators.size());
363  for (std::size_t i = 0; i!=m_iterators.size(); ++i)
364  {
365  m_iterators[i].Set(p[i]);
366  }
367  }
368  // PixelType & Value(); -- cannot be defined and still preserve direct access
369  // to memory => we don't provide it.
371 
372  // ImageType * GetImages();
374 
377 
380  for (auto & it : m_iterators)
381  it.NextLine();
382  return *this;
383  }
384 
387  for (auto & it : m_iterators)
388  it.GoToBeginOfLine();
389  return *this;
390  }
391 
394  for (auto & it : m_iterators)
395  it.GoToEndOfLine();
396  return *this;
397  }
398 
402  bool IsAtEndOfLine() const {
403  assert(!m_iterators.empty());
404  // Const qualifier has been added to ScanLineIterator::IsAtEndOfLine in ITK
405  // 5.1 => Use const_cast in the mean time...
406  return const_cast<typename ImageIteratorList_t::value_type &>(m_iterators.front()).IsAtEndOfLine();
407  }
409 
410 
411 private:
413 };
414 
415 } // otb::internal namespace
416 
424 template <typename TImageIterator>
426 
434 template <typename TImageIterator>
436 
437 } // otb namespace
438 
439 #endif // otbZipIterator_h
otb::internals::ZipIterator::PixelListProxy::iterator__::iterator__
iterator__(typename ImageIteratorList_t::const_iterator ref)
Definition: otbZipIterator.h:284
otb::internals::ZipIterator::PixelListProxy::iterator__::operator+
iterator__ operator+(std::ptrdiff_t offset) const noexcept
Definition: otbZipIterator.h:298
otb::internals::ZipIterator::Self
ZipIterator Self
Definition: otbZipIterator.h:66
otb::internals::ZipIterator::ZipIterator
ZipIterator()=default
otb::internals::ZipIterator::PixelListProxy::iterator__::iterator_category
std::forward_iterator_tag iterator_category
Definition: otbZipIterator.h:281
otb::internals::ZipIterator::operator=
ZipIterator & operator=(ZipIterator const &)=default
otb::internals::ZipIterator::SizeType
typename ImageIteratorType::SizeType SizeType
Definition: otbZipIterator.h:75
otb::internals::ZipIterator::operator<=
friend bool operator<=(ZipIterator const &lhs, ZipIterator const &rhs)
Definition: otbZipIterator.h:166
otb::internals::ZipIterator::SetRegion
void SetRegion(RegionType const &region)
Definition: otbZipIterator.h:199
otb::internals::ZipIterator::~ZipIterator
~ZipIterator()=default
otb::internals::ZipIterator::PixelListProxy::front
decltype(auto) front() const
Definition: otbZipIterator.h:328
otb::internals::ZipIterator::PixelListProxy::end
auto end() noexcept
Definition: otbZipIterator.h:313
otb::internals::ZipIterator::PixelListProxy::size
auto size() const noexcept
Definition: otbZipIterator.h:267
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::internals::ZipIterator::IsAtEnd
bool IsAtEnd() const
Definition: otbZipIterator.h:228
otb::internals::ZipIterator::AccessorFunctorType
typename ImageIteratorType::AccessorFunctorType AccessorFunctorType
Definition: otbZipIterator.h:98
otb::internals::ZipIterator::operator!=
friend bool operator!=(ZipIterator const &lhs, ZipIterator const &rhs)
Definition: otbZipIterator.h:163
otb::Span::empty
constexpr bool empty() const noexcept
Definition: otbSpan.h:175
otb::internals::ZipIterator::operator==
friend bool operator==(ZipIterator const &lhs, ZipIterator const &rhs)
Definition: otbZipIterator.h:155
otbIteratorHelpers.h
otb::internals::ZipIterator::Get
PixelListProxy Get() const
Definition: otbZipIterator.h:351
otb::internals::ZipIterator::PixelListProxy::cend
auto cend() const noexcept
Definition: otbZipIterator.h:317
otb::internals::ZipIterator::operator++
Self & operator++()
Definition: otbZipIterator.h:237
otb::internals::ZipIterator::PixelListProxy::empty
bool empty() const noexcept
Definition: otbZipIterator.h:266
otb::internals::ZipIterator::m_iterators
ImageIteratorList_t m_iterators
Definition: otbZipIterator.h:412
otb::internals::ZipIterator::IsAtEndOfLine
bool IsAtEndOfLine() const
Definition: otbZipIterator.h:402
otb::internals::ZipIterator::PixelListProxy::begin
auto begin() const noexcept
Definition: otbZipIterator.h:314
otb::internals::ZipIterator::PixelListProxy::PixelListProxy
PixelListProxy(ImageIteratorList_t const &iterators) noexcept
Definition: otbZipIterator.h:263
otb::internals::ZipIterator::PixelType
typename ImageIteratorType::PixelType PixelType
Definition: otbZipIterator.h:93
otb::internals::ZipIterator::ZipIterator
ZipIterator(Span< ImageType *const > images, RegionType const &region)
Definition: otbZipIterator.h:139
otb::internals::ZipIterator::RegionType
typename ImageIteratorType::RegionType RegionType
Definition: otbZipIterator.h:81
otb::internals::ZipIterator::PixelListProxy::iterator__::operator-
iterator__ operator-(std::ptrdiff_t offset) const noexcept
Definition: otbZipIterator.h:301
otb::internals::ZipIterator::PixelListProxy::iterator__::operator!=
friend bool operator!=(iterator__ const &lhs, iterator__ const &rhs) noexcept
Definition: otbZipIterator.h:288
otb::internals::ZipIterator::PixelListProxy::iterator__::operator==
friend bool operator==(iterator__ const &lhs, iterator__ const &rhs) noexcept
Definition: otbZipIterator.h:286
otb::internals::ZipIterator::operator<
friend bool operator<(ZipIterator const &lhs, ZipIterator const &rhs)
Definition: otbZipIterator.h:173
otb::internals::ZipIterator::PixelListProxy::iterator__::reference_to_value
ImageIteratorList_t::const_iterator reference_to_value
Definition: otbZipIterator.h:309
otb::internals::ZipIterator::operator>
friend bool operator>(ZipIterator const &lhs, ZipIterator const &rhs)
Definition: otbZipIterator.h:182
otb::internals::ZipIterator::IndexType
typename ImageIteratorType::IndexType IndexType
Definition: otbZipIterator.h:72
otb::internals::ZipIterator
Definition: otbZipIterator.h:47
otb::Span
Definition: otbSpan.h:60
otb::internals::ZipIterator::ImageIteratorList_t
std::vector< ImageIteratorType > ImageIteratorList_t
Definition: otbZipIterator.h:250
otb::internals::ZipIterator::PixelListProxy::cbegin
auto cbegin() const noexcept
Definition: otbZipIterator.h:316
otb::internals::ZipIterator::PixelListProxy::iterator__::difference_type
typename ImageIteratorList_t::difference_type difference_type
Definition: otbZipIterator.h:277
otb::Span::size
constexpr index_type size() const noexcept
Definition: otbSpan.h:174
otb::internals::ZipIterator::PixelListProxy::m_iterators
ImageIteratorList_t const & m_iterators
Definition: otbZipIterator.h:345
otb::internals::ZipIterator::PixelListProxy
Definition: otbZipIterator.h:257
otb::internals::ZipIterator::PixelContainer
typename ImageIteratorType::PixelContainer PixelContainer
Definition: otbZipIterator.h:86
otb::internals::ZipIterator::GetRegion
auto const & GetRegion() const
Definition: otbZipIterator.h:191
otb::internals::ZipIterator::PixelListProxy::end
auto end() const noexcept
Definition: otbZipIterator.h:315
otb::internals::ZipIterator::InternalPixelType
typename ImageIteratorType::InternalPixelType InternalPixelType
Definition: otbZipIterator.h:90
otb::internals::ZipIterator::ImageIteratorDimension
static constexpr unsigned int ImageIteratorDimension
Definition: otbZipIterator.h:64
otb::internals::ZipIterator::GoToBegin
Self & GoToBegin()
Definition: otbZipIterator.h:206
otb::internals::ZipIterator::GoToBeginOfLine
Self & GoToBeginOfLine()
Definition: otbZipIterator.h:386
otb::internals::ZipIterator::PixelListProxy::iterator__::operator++
iterator__ & operator++() noexcept
Definition: otbZipIterator.h:290
otb::internals::ZipIterator::PixelListProxy::iterator__::pointer
value_type * pointer
Definition: otbZipIterator.h:279
otb::internals::ZipIterator::GoToEnd
Self & GoToEnd()
Definition: otbZipIterator.h:213
otb::internals::ZipIterator::GoToEndOfLine
Self & GoToEndOfLine()
Definition: otbZipIterator.h:393
otb::internals::ZipIterator::PixelListProxy::iterator__::value_type
decltype(typename ImageIteratorList_t::const_iterator{}->Get()) value_type
Definition: otbZipIterator.h:278
otb::internals::ZipIterator::itkTypeMacroNoParent
itkTypeMacroNoParent(ZipIterator)
otb::internals::ZipIterator::PixelListProxy::iterator__::reference
value_type & reference
Definition: otbZipIterator.h:280
otb::internals::ZipIterator::IsAtBegin
bool IsAtBegin() const
Definition: otbZipIterator.h:221
otb::internals::ZipIterator::Set
void Set(MultiCompPixelType const &p)
Definition: otbZipIterator.h:360
otbSpan.h
otb::internals::MutableTag
Definition: otbIteratorHelpers.h:30
otb::internals::ZipIterator::PixelListProxy::iterator__
Definition: otbZipIterator.h:275
otb::internals::ZipIterator::ImageType
typename TImageIterator::ImageType ImageType
Definition: otbZipIterator.h:58
otb::internals::ZipIterator::PixelContainerPointer
typename PixelContainer::Pointer PixelContainerPointer
Definition: otbZipIterator.h:87
otb::internals::ZipIterator::OffsetType
typename ImageIteratorType::OffsetType OffsetType
Definition: otbZipIterator.h:78
otb::internals::ZipIterator::operator>=
friend bool operator>=(ZipIterator const &lhs, ZipIterator const &rhs)
Definition: otbZipIterator.h:180
otb::internals::ZipIterator::PixelListProxy::begin
auto begin() noexcept
Definition: otbZipIterator.h:312
otb::internals::ZipIterator::PixelListProxy::back
decltype(auto) back() const
Definition: otbZipIterator.h:336
otb::internals::ZipIterator::PixelListProxy::iterator__::operator--
iterator__ & operator--() noexcept
Definition: otbZipIterator.h:294
otb::internals::ZipIterator::NextLine
Self & NextLine()
Definition: otbZipIterator.h:379
otb::internals::ZipIterator::ImageIteratorType
TImageIterator ImageIteratorType
Definition: otbZipIterator.h:55
otb::internals::ZipIterator::AccessorType
typename ImageIteratorType::AccessorType AccessorType
Definition: otbZipIterator.h:97