/
PatchIterators.h
226 lines (191 loc) · 5.82 KB
/
PatchIterators.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once
#include "ipatch.h"
#include <fmt/format.h>
namespace patch
{
/**
* Forward iterator used to traverse the control points of a patch.
* The visited points, stride or ordering are defined by the forward function
* object passed to the constructor.
*
* Each iterator instance refers to a single control of the patch, defined
* by the row and col indices. An iterator whose coordinates are out of bounds
* will return isValid() == false, throwing exceptions when dereferenced.
*
* The state of the iterator is protected allowing subclasses to access
* it in their forward function implementation.
*/
class PatchControlIterator
{
public:
// The function object implementing the move-forward logic of this iterator
using Forwarder = std::function<void(PatchControlIterator&)>;
protected:
IPatch& _patch;
// Using signed indices to allow out-of-bounds iterators
int _row;
int _col;
// Moves this iterator forward by one step
Forwarder _forward;
public:
PatchControlIterator(IPatch& patch, int row, int col, Forwarder forward) :
_patch(patch),
_row(row),
_col(col),
_forward(forward)
{}
PatchControlIterator(const PatchControlIterator& other) = default;
PatchControlIterator& operator=(const PatchControlIterator& other) = default;
// Post-increment i++
PatchControlIterator operator++(int)
{
PatchControlIterator previous = *this;
++(*this);
return previous;
}
// Pre-increment ++i
PatchControlIterator& operator++()
{
// Call the logic moving our iterator forward by one step
_forward(*this);
return *this;
}
PatchControl& operator*()
{
throwIfOutOfBounds();
return _patch.ctrlAt(static_cast<std::size_t>(_row), static_cast<std::size_t>(_col));
}
const PatchControl& operator*() const
{
throwIfOutOfBounds();
return _patch.ctrlAt(_row, _col);
}
PatchControl* operator->()
{
return &**this;
}
const PatchControl* operator->() const
{
return &**this;
}
bool isValid() const
{
return _col >= 0 && _row >= 0 &&
_col < static_cast<int>(_patch.getWidth()) &&
_row < static_cast<int>(_patch.getHeight());
}
// Interface to set or reset the iterator coordinates
int getRow() const
{
return _row;
}
int getColumn() const
{
return _col;
}
void set(int row, int col)
{
_row = row;
_col = col;
}
private:
void throwIfOutOfBounds() const
{
// Dereferencing an out-of-bounds iterator?
if (!isValid())
{
throw std::runtime_error(fmt::format("Iterator (row={0},col={1}) is out of bounds", _row, _col));
}
}
};
// An iterator traversing a single row of a patch
class SinglePatchRowIterator :
public PatchControlIterator
{
public:
SinglePatchRowIterator(IPatch& patch, std::size_t row) :
PatchControlIterator(patch, static_cast<int>(row), 0, moveToNextCol)
{}
private:
static void moveToNextCol(PatchControlIterator& it)
{
it.set(it.getRow(), it.getColumn() + 1);
}
};
// An iterator traversing a single column of a patch
class SinglePatchColumnIterator :
public PatchControlIterator
{
public:
SinglePatchColumnIterator(IPatch& patch, std::size_t col) :
PatchControlIterator(patch, 0, static_cast<int>(col), moveToNextRow)
{}
private:
static void moveToNextRow(PatchControlIterator& it)
{
it.set(it.getRow() + 1, it.getColumn());
}
};
// An iterator traversing a the given patch column-wise, iterating over
// one row after the other (which are optionally constrained to [startRow..endRow])
class ColumnWisePatchIterator :
public PatchControlIterator
{
public:
ColumnWisePatchIterator(IPatch& patch) :
ColumnWisePatchIterator(patch, 0, patch.getHeight() - 1)
{
assert(patch.getHeight() > 0);
}
ColumnWisePatchIterator(IPatch& patch, std::size_t startRow, std::size_t endRow) :
PatchControlIterator(patch, startRow, 0, std::bind(ColumnWisePatchIterator::moveNext, std::placeholders::_1, patch, endRow))
{}
private:
static void moveNext(PatchControlIterator& it, const IPatch& patch, std::size_t endRow)
{
auto nextColumn = it.getColumn() + 1;
auto nextRow = it.getRow();
if (nextColumn >= patch.getWidth())
{
// Advance to the next row
// If that doesn't succeed, just leave the indices out of bounds
if (++nextRow <= endRow)
{
nextColumn = 0;
}
}
it.set(nextRow, nextColumn);
}
};
// An iterator traversing a the given patch row-wise, iterating over
// one column after the other (which are optionally constrained to [startColumn..endColumn])
class RowWisePatchIterator :
public PatchControlIterator
{
public:
RowWisePatchIterator(IPatch& patch) :
RowWisePatchIterator(patch, 0, patch.getWidth() - 1)
{
assert(patch.getWidth() > 0);
}
RowWisePatchIterator(IPatch& patch, std::size_t startColumn, std::size_t endColumn) :
PatchControlIterator(patch, startColumn, 0, std::bind(RowWisePatchIterator::moveNext, std::placeholders::_1, patch, endColumn))
{}
private:
static void moveNext(PatchControlIterator& it, const IPatch& patch, std::size_t endColumn)
{
auto nextRow = it.getRow() + 1;
auto nextColumn = it.getColumn();
if (nextRow >= patch.getHeight())
{
// Advance to the next column
// If that doesn't succeed, just leave the indices out of bounds
if (++nextColumn <= endColumn)
{
nextRow = 0;
}
}
it.set(nextRow, nextColumn);
}
};
}