-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHatcher.h
More file actions
390 lines (348 loc) · 10.2 KB
/
Hatcher.h
File metadata and controls
390 lines (348 loc) · 10.2 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* Hatcher.h
* Morph
*
* Created by Christian Brunschen on 01/12/2010.
* Copyright 2010 Christian Brunschen. All rights reserved.
*
*/
#ifndef __Hatcher_h__
#define __Hatcher_h__
#include "Primitives.h"
#include "Line.h"
#include "Chain.h"
#include <list>
#include <algorithm>
namespace Hatching {
// to defeat Xcode's indenting
#if 0
}
#endif
using namespace std;
using namespace Chaining;
using namespace Primitives;
// |angle| is in pi-radians: useful range is [0 .. 1)
template <typename F> void hatch(int width, int height, double angle, double period, double phase, F &f) {
// ensure that 0.0 <= angle < 1.0
if (angle >= 1.0) {
angle -= floor(angle);
} else if (angle < 0.0) {
angle += ceil(angle);
}
// translate from pi-radians to radians
double theta = angle * M_PI;
// get the bounding box of the rotated rectangle - extended by 1
// in the x direction to handle rounding
double llx, lly, lrx, lry, ulx, uly, urx, ury;
rotate(llx, lly, 0, 0, theta);
rotate(lrx, lry, width-1, 0, theta);
rotate(ulx, uly, 0, height-1, theta);
rotate(urx, ury, width-1, height-1, theta);
// get the minimum and maximum extents of the bounding box
double xmin = min(min(min(llx, lrx), urx), ulx) - 1;
double xmax = max(max(max(llx, lrx), urx), ulx) + 1;
double ymin = min(min(min(lly, lry), ury), uly);
double ymax = max(max(max(lly, lry), ury), uly);
// calculate how far below zero we need to start in order to cover every part
// of the rotated rectangle
int n = floor((phase - ymin) / period);
for (double y = phase - n * period;
y <= ymax;
y += period) {
double x0, x1, y0, y1;
rotate(x0, y0, xmin, y, -theta);
rotate(x1, y1, xmax, y, -theta);
line(rint(x0), rint(y0), rint(x1), rint(y1), f);
}
}
template <typename Include> struct Hatcher {
Include include_;
struct Extent : public Chain {
int from_;
int to_;
Extent(int from) : Chain(), from_(from), to_(-1) { }
};
typedef list<Extent> Row;
typedef typename Row::iterator RowIterator;
typedef list<Row> Grid;
typedef typename Grid::iterator GridIterator;
struct GridCursor {
GridIterator row;
RowIterator extent;
bool isFrom;
const IPoint &point() {
return isFrom ? extent->front() : extent->back();
}
double squareDistance(const IPoint &p) {
return point().squareDistance(p);
}
double squareDistance(const GridCursor &c) {
return point().squareDistance(c.point());
}
};
Grid grid_;
Row *row_;
Extent *extent_;
IPoint previous_;
int along_;
Hatcher(Include include) : include_(include), previous_(numeric_limits<int>::min(), -numeric_limits<int>::min()) { }
void operator() (int x, int y) {
IPoint p(x, y);
if (!p.isNeighbour(previous_)) {
// started a new row!
grid_.push_back(Row());
row_ = &(grid_.back());
along_ = 0;
extent_ = NULL;
}
previous_ = p;
if (include_(p)) {
if (!extent_) {
row_->push_back(Extent(along_));
extent_ = &(row_->back());
}
extent_->addPoint(p);
extent_->to_ = along_;
} else {
if (extent_) {
extent_ = NULL;
}
}
++along_;
}
bool start(GridIterator &r, RowIterator &e) {
for (r = grid_.begin(); r != grid_.end(); r++) {
e = r->begin();
if (e != r->end()) {
return true;
}
}
return false;
}
static double minDistance(IPoint &p, const Extent &e, bool &isFrom) {
double dFrom = p.squareDistance(e.front());
double dTo = p.squareDistance(e.back());
if (dFrom <= dTo) {
isFrom = true;
return dFrom;
} else {
isFrom = false;
return dTo;
}
}
double findNearestExtentInRow(Row &row, IPoint &p, /*out*/ RowIterator &eOut, bool &isFrom) {
int dMin = std::numeric_limits<int>::max();
for (RowIterator e = row.begin(); e != row.end(); ++e) {
double dFrom = p.squareDistance(e->front());
if (dFrom < dMin) {
eOut = e;
dMin = dFrom;
isFrom = true;
}
double dTo = p.squareDistance(e->back());
if (dTo < dMin) {
eOut = e;
dMin = dTo;
isFrom = false;
}
}
return dMin;
}
template<typename I> static void move(I &i, int delta) {
while (delta > 0) {
++i;
--delta;
}
while (delta < 0) {
--i;
++delta;
}
}
double findNearerExtent(GridIterator r0, int direction, IPoint &p, double period, double dMin, /*out*/ GridCursor &c) {
GridIterator r = r0;
move(r, direction);
double dy = period;
double d2y = dy*dy;
while (r != grid_.end() && d2y <= dMin) {
bool isFrom;
RowIterator e;
double d = findNearestExtentInRow(*r, p, e, isFrom);
if (d < dMin) {
c.row = r;
c.extent = e;
c.isFrom = isFrom;
dMin = d;
}
move(r, direction);
dy += period;
d2y = dy * dy;
}
return dMin;
}
double findNearestExtent(GridIterator r, IPoint &p, double period, /*out*/ GridCursor &c) {
bool isFrom;
RowIterator e;
double d = findNearestExtentInRow(*r, p, e, isFrom);
if (e != r->end()) {
c.row = r;
c.extent = e;
} else {
c.row = grid_.end();
}
// search positive
d = findNearerExtent(r, +1, p, period, d, c);
// search negative
d = findNearerExtent(r, -1, p, period, d, c);
return d;
}
void addToChains(Chains &chains, bool front, GridCursor &c) {
Chain &chain = front ? chains.prependChain() : chains.addChain();
chain.splice(chain.begin(), *c.extent);
if (!c.isFrom) {
chain.reverse();
}
c.row->erase(c.extent);
c.row = grid_.end();
}
template <typename CanRetract> void retractGrid(CanRetract &canRetract) {
for (GridIterator r = grid_.begin(); r != grid_.end(); ++r) {
RowIterator e = r->begin();
while (e != r->end()) {
// cerr << "Retracting " << *e << "; ";
// retract front
typename Extent::iterator i = e->begin();
while (i != e->end()) {
typename Extent::iterator ni = i; ++ni;
int neighbours = ni == e->end() ? 0 : (1 << i->directionTo(*ni));
if (canRetract(*i, neighbours)) {
i = e->erase(i);
i = ni;
} else {
break;
}
}
// retract back
typename Extent::reverse_iterator j = e->rbegin();
while (j != e->rend()) {
typename Extent::reverse_iterator nj = j; ++nj;
int neighbours = nj == e->rend() ? 0 : (1 << j->directionTo(*nj));
if (canRetract(*j, neighbours)) {
j = nj;
} else {
break;
}
}
for (i = j.base(); i != e->end();) {
i = e->erase(i);
}
if (e->empty()) {
// if we've retracted everything, remove this extent from its containing row
e = r->erase(e);
// cerr << "all retracted!" << endl;
} else {
// otherwise, simply step to the next extent
// cerr << "remaining: " << *e << endl;
++e;
}
}
}
}
template<typename CanRetract> void getChains(Chains &chains, double period, CanRetract &canRetract) {
retractGrid(canRetract);
int remaining = 0;
for (GridIterator r = grid_.begin(); r != grid_.end(); ++r) {
remaining += r->size();
}
if (remaining > 0) {
GridIterator rFront, rBack;
RowIterator e;
start(rFront, e);
rBack = rFront;
IPoint pFront = e->front(), pBack = e->back();
// insert the current extent as the first chain of the sequence of chains
Chain &chain = chains.addChain();
chain.splice(chain.begin(), *e);
// and remove the extent from this row (as it's already been handled)
rFront->erase(e);
--remaining;
// the cursors for finding the next candidate
GridCursor cFront, cBack;
cFront.row = cBack.row = grid_.end();
double dFront, dBack;
while (remaining > 0) {
if (cFront.row == grid_.end()) {
// find the nearest extent relative to the front
dFront = findNearestExtent(rFront, pFront, period, cFront);
}
dFront = numeric_limits<double>::max();
if (cBack.row == grid_.end()) {
// find the nearest extent relative to the back
dBack = findNearestExtent(rBack, pBack, period, cBack);
}
if (dFront < dBack) {
rFront = cFront.row;
pFront = cFront.isFrom ? cFront.extent->back() : cFront.extent->front();
if (cBack.extent == cFront.extent) cBack.row = grid_.end();
addToChains(chains, true, cFront);
} else {
rBack = cBack.row;
pBack = cBack.isFrom ? cBack.extent->back() : cBack.extent->front();
if (cFront.extent == cBack.extent) cFront.row = grid_.end();
addToChains(chains, false, cBack);
}
--remaining;
}
}
}
int gridWidth() {
int maxX = 0;
for (GridIterator row = grid_.begin(); row != grid_.end(); ++row) {
if (row->size() > 0) {
if (row->back().to_ > maxX) {
maxX = row->back().to_;
}
}
}
return maxX + 1;
}
int gridHeight() {
return grid_.size();
}
template<typename B> void printGrid(B &b, int period) {
int y = 0;
for (GridIterator row = grid_.begin(); row != grid_.end(); ++row) {
cerr << y << ": ";
for (RowIterator extent = row->begin(); extent != row->end(); ++extent) {
cerr << extent->from_ << "->" << extent->to_ << " ";
for (int x = extent->from_; x <= extent->to_; ++x) {
IPoint p(x, y);
b.at(p) = true;
}
}
cerr << endl;
y += period;
}
}
void verifyGrid() {
for (GridIterator r = grid_.begin(); r != grid_.end(); ++r) {
for (RowIterator e = r->begin(); e != r->end(); ++e) {
if (e->size() > 1) {
Chain::iterator j = e->begin();
IPoint p = *j;
for (++j; j != e->end(); ++j) {
if (!p.isNeighbour(*j)) {
cerr << "not neighbours: " << p << " <-> " << *j << endl;
}
p = *j;
}
}
}
}
}
};
// to defeat Xcode's indenting
#if 0
{
#endif
}
#endif // __Hatcher_h__