iterators_impl.inc.h
Go to the documentation of this file.
1 /*
2 * ECVL - European Computer Vision Library
3 * Version: 0.3.4
4 * copyright (c) 2021, Università degli Studi di Modena e Reggio Emilia (UNIMORE), AImageLab
5 * Authors:
6 * Costantino Grana (costantino.grana@unimore.it)
7 * Federico Bolelli (federico.bolelli@unimore.it)
8 * Michele Cancilla (michele.cancilla@unimore.it)
9 * Laura Canalini (laura.canalini@unimore.it)
10 * Stefano Allegretti (stefano.allegretti@unimore.it)
11 * All rights reserved.
12 */
13 
14 
15 template <typename T>
16 Iterator<T>::Iterator(Image& img, std::vector<int> pos) : img_{ &img }, pos_{ move(pos) }
17 {
18  if (img_->contiguous_)
19  incrementor = &Iterator::ContiguousIncrementPos;
20  if (pos_.empty()) { // Begin
21  pos_.resize(img_->dims_.size(), 0);
22  ptr_ = img.data_;
23  }
24  else {
25  if (pos_.size() != img_->dims_.size()) {
26  throw std::runtime_error("Iterator starting pos has a wrong size");
27  }
28  if (pos_ == img_->dims_) { // End
29  if (img_->contiguous_) {
30  ptr_ = img_->data_ + img_->datasize_;
31  }
32  else {
33  ptr_ = nullptr;
34  }
35  }
36  else {
37  ptr_ = img_->Ptr(pos_);
38  }
39  }
40 }
41 template <typename T>
42 ConstIterator<T>::ConstIterator(const Image& img, std::vector<int> pos) : img_{ &img }, pos_{ move(pos) }
43 {
44  if (img_->contiguous_)
45  incrementor = &ConstIterator::ContiguousIncrementPos;
46  if (pos_.empty()) { // Begin
47  pos_.resize(img_->dims_.size(), 0);
48  ptr_ = img.data_;
49  }
50  else {
51  if (pos_.size() != img_->dims_.size()) {
52  throw std::runtime_error("ConstIterator starting pos has a wrong size");
53  }
54  if (pos_ == img_->dims_) { // End
55  if (img_->contiguous_) {
56  ptr_ = img_->data_ + img_->datasize_;
57  }
58  else {
59  ptr_ = nullptr;
60  }
61  }
62  else {
63  ptr_ = img_->Ptr(pos_);
64  }
65  }
66 }
67 template <typename T>
68 ContiguousIterator<T>::ContiguousIterator(Image& img, std::vector<int> pos) : img_{ &img }
69 {
70  if (!img_->contiguous_) {
71  throw std::runtime_error("ContiguousIterator used on a non contiguous Image");
72  }
73 
74  if (pos.empty()) { // Begin
75  ptr_ = img.data_;
76  }
77  else {
78  if (pos.size() != img_->dims_.size()) {
79  throw std::runtime_error("ContiguousIterator starting pos has a wrong size");
80  }
81  if (pos == img_->dims_) { // End
82  ptr_ = img_->data_ + img_->datasize_;
83  }
84  else {
85  ptr_ = img_->Ptr(pos);
86  }
87  }
88 }
89 template <typename T>
90 ConstContiguousIterator<T>::ConstContiguousIterator(const Image& img, std::vector<int> pos) : img_{ &img }
91 {
92  if (!img_->contiguous_) {
93  throw std::runtime_error("ConstContiguousIterator used on a non contiguous Image");
94  }
95 
96  if (pos.empty()) { // Begin
97  ptr_ = img.data_;
98  }
99  else {
100  if (pos.size() != img_->dims_.size()) {
101  throw std::runtime_error("ConstContiguousIterator starting pos has a wrong size");
102  }
103  if (pos == img_->dims_) { // End
104  ptr_ = img_->data_ + img_->datasize_;
105  }
106  else {
107  ptr_ = img_->Ptr(pos);
108  }
109  }
110 }
111 
112 template <typename T>
113 Iterator<T>& Iterator<T>::IncrementPos()
114 {
115  int spos = vsize(pos_);
116  int dim;
117  for (dim = 0; dim < spos; ++dim) {
118  ++pos_[dim];
119  ptr_ += img_->strides_[dim];
120  if (pos_[dim] != img_->dims_[dim])
121  break;
122  // Back to dimension starting position
123  pos_[dim] = 0;
124  ptr_ -= img_->dims_[dim] * img_->strides_[dim];
125  }
126  if (dim == spos)
127  ptr_ = nullptr;
128  return *this;
129 }
130 template <typename T>
131 ConstIterator<T>& ConstIterator<T>::IncrementPos()
132 {
133  int spos = vsize(pos_);
134  int dim;
135  for (dim = 0; dim < spos; ++dim) {
136  ++pos_[dim];
137  ptr_ += img_->strides_[dim];
138  if (pos_[dim] != img_->dims_[dim])
139  break;
140  // Back to dimension starting position
141  pos_[dim] = 0;
142  ptr_ -= img_->dims_[dim] * img_->strides_[dim];
143  }
144  if (dim == spos)
145  ptr_ = nullptr;
146  return *this;
147 }
148 
149 
150 template <typename T>
151 Iterator<T>& Iterator<T>::ContiguousIncrementPos()
152 {
153  ptr_ += img_->elemsize_;
154  return *this;
155 }
156 template <typename T>
157 ConstIterator<T>& ConstIterator<T>::ContiguousIncrementPos()
158 {
159  ptr_ += img_->elemsize_;
160  return *this;
161 }
162 template <typename T>
163 ContiguousIterator<T>& ContiguousIterator<T>::ContiguousIncrementPos()
164 {
165  ptr_ += img_->elemsize_;
166  return *this;
167 }
168 template <typename T>
169 ConstContiguousIterator<T>& ConstContiguousIterator<T>::ContiguousIncrementPos()
170 {
171  ptr_ += img_->elemsize_;
172  return *this;
173 }
ConstContiguousIterator(const Image &img, std::vector< int > pos={})
Definition: image.h:91
ContiguousIterator(Image &img, std::vector< int > pos={})
Definition: image.h:69
int vsize(const std::vector< T > &v)
Definition: image.h:34
ConstIterator(const Image &img, std::vector< int > pos={})
Definition: image.h:43