File: PatchIterators.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (340 lines) | stat: -rw-r--r-- 10,812 bytes parent folder | download | duplicates (4)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#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 (in defined order)
class SinglePatchRowIteratorBase :
    public PatchControlIterator
{
public:
    SinglePatchRowIteratorBase(IPatch& patch, std::size_t row, std::size_t startCol, int delta) :
        PatchControlIterator(patch, static_cast<int>(row), static_cast<int>(startCol), 
            std::bind(SinglePatchRowIteratorBase::moveToNextCol, std::placeholders::_1, delta))
    {}

private:
    static void moveToNextCol(PatchControlIterator& it, int delta)
    {
        it.set(it.getRow(), it.getColumn() + delta);
    }
};

// An iterator traversing a single row of a patch
class SinglePatchRowIterator :
    public SinglePatchRowIteratorBase
{
public:
    SinglePatchRowIterator(IPatch& patch, std::size_t row) :
        SinglePatchRowIteratorBase(patch, static_cast<int>(row), 0, +1)
    {}
};

// An iterator traversing a single row of a patch in reverse order (starting from the highest column)
class SinglePatchRowReverseIterator :
    public SinglePatchRowIteratorBase
{
public:
    SinglePatchRowReverseIterator(IPatch& patch, std::size_t row) :
        SinglePatchRowIteratorBase(patch, static_cast<int>(row), static_cast<int>(patch.getWidth() - 1), -1)
    {}
};

// An iterator traversing a single column of a patch (in defined order)
class SinglePatchColumnIteratorBase :
    public PatchControlIterator
{
public:
    SinglePatchColumnIteratorBase(IPatch& patch, std::size_t col, std::size_t startRow, int delta) :
        PatchControlIterator(patch, static_cast<int>(startRow), static_cast<int>(col), 
            std::bind(SinglePatchColumnIteratorBase::moveToNextRow, std::placeholders::_1, delta))
    {}

private:
    static void moveToNextRow(PatchControlIterator& it, int delta)
    {
        it.set(it.getRow() + delta, it.getColumn());
    }
};

// An iterator traversing a single column of a patch
class SinglePatchColumnIterator :
    public SinglePatchColumnIteratorBase
{
public:
    SinglePatchColumnIterator(IPatch& patch, std::size_t row) :
        SinglePatchColumnIteratorBase(patch, static_cast<int>(row), 0, +1)
    {}
};

// An iterator traversing a single column of a patch in reverse order (starting from the highest row)
class SinglePatchColumnReverseIterator :
    public SinglePatchColumnIteratorBase
{
public:
    SinglePatchColumnReverseIterator(IPatch& patch, std::size_t row) :
        SinglePatchColumnIteratorBase(patch, static_cast<int>(row), static_cast<int>(patch.getHeight() - 1), -1)
    {}
};

// An iterator traversing a given patch column-wise, iterating over
// one column after the other (which are optionally constrained to [startColumn..endColumn])
// in the given row direction (+1 == Forward, -1 == Backwards)
class ColumnWisePatchIteratorBase :
    public PatchControlIterator
{
public:
    ColumnWisePatchIteratorBase(IPatch& patch, int rowDelta) :
        ColumnWisePatchIteratorBase(patch, 0, patch.getWidth() - 1, rowDelta)
    {}

    ColumnWisePatchIteratorBase(IPatch& patch, std::size_t startColumn, std::size_t endColumn, int rowDelta) :
        PatchControlIterator(patch, rowDelta > 0 ? 0 : static_cast<int>(patch.getHeight()) - 1, static_cast<int>(startColumn), 
            std::bind(ColumnWisePatchIteratorBase::moveNext, std::placeholders::_1, std::ref(patch), 
                endColumn, startColumn <= endColumn ? +1 : -1, rowDelta))
    {}

private:
    static void moveNext(PatchControlIterator& it, const IPatch& patch, std::size_t endColumn, int columnDelta, int rowDelta)
    {
        auto nextRow = it.getRow() + rowDelta;
        auto nextColumn = it.getColumn();

        if (rowDelta < 0 && nextRow < 0 ||
            rowDelta > 0 && nextRow >= patch.getHeight())
        {
            // Advance to the next column
            // If that doesn't succeed, just leave the indices out of bounds
            nextColumn += columnDelta;

            if (columnDelta > 0 && nextColumn <= endColumn ||
                columnDelta < 0 && nextColumn >= endColumn)
            {
                nextRow = rowDelta > 0 ? 0 : static_cast<int>(patch.getHeight()) - 1;
            }
        }

        it.set(nextRow, nextColumn);
    }
};

// An iterator traversing a given patch column-wise, iterating over
// one column after the other (which are optionally constrained to [startColumn..endColumn])
class ColumnWisePatchIterator :
    public ColumnWisePatchIteratorBase
{
public:
    ColumnWisePatchIterator(IPatch& patch) :
        ColumnWisePatchIteratorBase(patch, +1)
    {}

    ColumnWisePatchIterator(IPatch& patch, std::size_t startColumn, std::size_t endColumn) :
        ColumnWisePatchIteratorBase(patch, startColumn, endColumn, +1)
    {}
};

// An iterator traversing a given patch column-wise, iterating over
// one column after the other (which are optionally constrained to [startColumn..endColumn])
// with each column traversed backwards, row=[height-1...0]
class ColumnWisePatchReverseIterator :
    public ColumnWisePatchIteratorBase
{
public:
    ColumnWisePatchReverseIterator(IPatch& patch) :
        ColumnWisePatchIteratorBase(patch, -1)
    {}

    ColumnWisePatchReverseIterator(IPatch& patch, std::size_t startColumn, std::size_t endColumn) :
        ColumnWisePatchIteratorBase(patch, startColumn, endColumn, -1)
    {}
};

// An iterator traversing a given patch row-wise, iterating over
// one row after the other (which are optionally constrained to [startRow..endRow])
class RowWisePatchIteratorBase :
    public PatchControlIterator
{
public:
    RowWisePatchIteratorBase(IPatch& patch, int columnDelta) :
        RowWisePatchIteratorBase(patch, 0, patch.getHeight() - 1, columnDelta)
    {}

    RowWisePatchIteratorBase(IPatch& patch, std::size_t startRow, std::size_t endRow, int columnDelta) :
        PatchControlIterator(patch, static_cast<int>(startRow), columnDelta > 0 ? 0 : static_cast<int>(patch.getWidth()) - 1,
            std::bind(RowWisePatchIteratorBase::moveNext, std::placeholders::_1, std::ref(patch), 
                endRow, startRow <= endRow ? +1 : -1, columnDelta))
    {}

private:
    static void moveNext(PatchControlIterator& it, const IPatch& patch, std::size_t endRow, int rowDelta, int columnDelta)
    {
        auto nextColumn = it.getColumn() + columnDelta;
        auto nextRow = it.getRow();

        if (columnDelta > 0 && nextColumn >= patch.getWidth() ||
            columnDelta < 0 && nextColumn < 0)
        {
            // Advance to the next row
            // If that doesn't succeed, just leave the indices out of bounds
            nextRow += rowDelta;

            if (rowDelta > 0 && nextRow <= endRow ||
                rowDelta < 0 && nextRow >= endRow)
            {
                nextColumn = columnDelta > 0 ? 0 : static_cast<int>(patch.getWidth()) - 1;
            }
        }

        it.set(nextRow, nextColumn);
    }
};

// An iterator traversing a given patch row-wise, iterating over
// one row after the other (which are optionally constrained to [startRow..endRow])
// columns are traversed in reverse order, col=[width-1...0]
class RowWisePatchIterator :
    public RowWisePatchIteratorBase
{
public:
    RowWisePatchIterator(IPatch& patch) :
        RowWisePatchIteratorBase(patch, +1)
    {}

    RowWisePatchIterator(IPatch& patch, std::size_t startRow, std::size_t endRow) :
        RowWisePatchIteratorBase(patch, startRow, endRow, +1)
    {}
};

// An iterator traversing a given patch row-wise, iterating over
// one row after the other (which are optionally constrained to [startRow..endRow])
// columns are traversed in reverse order, col=[width-1...0]
class RowWisePatchReverseIterator :
    public RowWisePatchIteratorBase
{
public:
    RowWisePatchReverseIterator(IPatch& patch) :
        RowWisePatchIteratorBase(patch, -1)
    {}

    RowWisePatchReverseIterator(IPatch& patch, std::size_t startRow, std::size_t endRow) :
        RowWisePatchIteratorBase(patch, startRow, endRow, -1)
    {}
};

}