-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr2geom.h
647 lines (540 loc) · 14.8 KB
/
r2geom.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#ifndef R2GEOM_H
#define R2GEOM_H
#include <vector>
#include <utility>
#include <cmath>
#include "pi.h"
using std::size_t;
// const double R2_EPSILON = 1e-8;
const int MAX_INTERPOLATION_RAYS = 256;
// Predefinitions
class Bitmask;
class I2Contour;
class R2Contour;
template <class V = double> class G2Vector {
public:
V x;
V y;
G2Vector(V px = V(), V py = V()):
x(px),
y(py)
{}
G2Vector<V> operator+(const G2Vector<V>& v) const {
return G2Vector<V>(x + v.x, y + v.y);
}
G2Vector<V>& operator+=(const G2Vector<V>& v) {
x += v.x;
y += v.y;
return *this;
}
G2Vector<V> operator-(const G2Vector<V>& v) const {
return G2Vector<V>(x - v.x, y - v.y);
}
G2Vector<V>& operator-=(const G2Vector<V>& v) {
x -= v.x;
y -= v.y;
return *this;
}
G2Vector<V> operator*(V c) const {
return G2Vector<V>(x*c, y*c);
}
V operator*(const G2Vector<V>& v) const { // Dot product
return (x*v.x + y*v.y);
}
V signedArea(const G2Vector<V>& v) const {
return (x*v.y - v.x*y);
}
V area(const G2Vector<V>& v) const {
V s = x*v.y - v.x*y;
if (s < 0)
s = (-s);
return s;
}
bool operator==(const G2Vector<V>& v) const {
return (x == v.x && y == v.y);
}
bool operator!=(const G2Vector<V>& v) const {
return !(*this == v);
}
bool operator<=(const G2Vector<V>& v) const {
return (y < v.y || (y <= v.y && x <= v.x));
}
bool operator>(const G2Vector<V>& v) const {
return !(*this <= v);
}
bool operator<(const G2Vector<V>& v) const {
return (y < v.y || (y <= v.y && x < v.x));
}
bool operator>=(const G2Vector<V>& v) const {
return !(*this < v);
}
double norm() const {
return sqrt(double(x)*double(x) + double(y)*double(y));
}
G2Vector<V> normal() const {
return G2Vector<V>(-y, x);
}
G2Vector<V> normalized() const {
double l = norm();
if (l <= 0.)
return *this;
return G2Vector<V>(V(x/l), V(y/l));
}
G2Vector<V>& normalize() const {
double l = norm();
if (l > 0.) {
x = V(x/l);
y = V(y/l);
}
return *this;
}
double angle(const G2Vector<V>& v) const {
G2Vector<V> n = normal();
double x = double(v*(*this));
double y = double(v*n);
return atan2(y, x);
}
};
template <class V = double> class G2Point {
public:
V x;
V y;
G2Point(V px = V(), V py = V()):
x(px),
y(py)
{}
G2Point<V> operator+(const G2Vector<V>& v) const {
return G2Point<V>(x + v.x, y + v.y);
}
G2Point<V>& operator+=(const G2Vector<V>& v) {
x += v.x;
y += v.y;
return *this;
}
G2Point<V> operator+(const G2Point<V>& v) const {
return G2Point<V>(x + v.x, y + v.y);
}
G2Point<V>& operator+=(const G2Point<V>& v) {
x += v.x;
y += v.y;
return *this;
}
G2Vector<V> operator-(const G2Point<V>& v) const {
return G2Vector<V>(x - v.x, y - v.y);
}
G2Point<V> operator-(const G2Vector<V>& v) const {
return G2Point<V>(x - v.x, y - v.y);
}
G2Point<V>& operator-=(const G2Vector<V>& v) {
x -= v.x;
y -= v.y;
return *this;
}
G2Point<V> operator*(V c) const {
return G2Point<V>(x*c, y*c);
}
bool operator==(const G2Point<V>& v) const {
return (x == v.x && y == v.y);
}
bool operator!=(const G2Point<V>& v) const {
return !(*this == v);
}
bool operator<=(const G2Point<V>& v) const {
return (y < v.y || (y <= v.y && x <= v.x));
}
bool operator>(const G2Point<V>& v) const {
return !(*this <= v);
}
bool operator<(const G2Point<V>& v) const {
return (y < v.y || (y == v.y && x < v.x));
}
bool operator>=(const G2Point<V>& v) const {
return !(*this < v);
}
static double signedArea(
const G2Point<V>& a,
const G2Point<V>& b,
const G2Point<V>& c
) {
G2Vector<V> ab = b - a;
G2Vector<V> ac = c - a;
return double(ab.signedArea(ac))/2.;
}
static double area(
const G2Point<V>& a,
const G2Point<V>& b,
const G2Point<V>& c
) {
return fabs(G2Point<V>::signedArea(a, b, c));
}
double distance(const G2Point& p) const {
return (p - *this).norm();
}
static double distance(const G2Point& p0, const G2Point& p1) {
return p0.distance(p1);
}
};
template <class V = double> class G2Rectangle {
public:
V x;
V y;
V w;
V h;
G2Rectangle(
V xx = V(), V yy = V(),
V ww = V(), V hh = V()
):
x(xx),
y(yy),
w(ww),
h(hh)
{}
G2Rectangle(
const G2Point<V>& p,
V ww, V hh
):
x(p.x),
y(p.y),
w(ww),
h(hh)
{}
G2Rectangle(
const G2Point<V>& left_bottom,
const G2Point<V>& right_top
):
x(left_bottom.x),
y(left_bottom.y),
w(right_top.x - left_bottom.x),
h(right_top.y - left_bottom.y)
{}
G2Point<V> leftBottom() const {
return G2Point<V>(x, y);
}
G2Point<V> rightTop() const {
return G2Point<V>(x + w, y + h);
}
V width() const { return w; }
V height() const { return h; }
V left() const { return x; }
V bottom() const { return y; }
V right() const { return x + w; }
V top() const { return y + h; }
void setLeft(V l) { x = l; }
void setBottom(V b) { y = b; }
void setLeftBottom(const G2Point<V> p) {
x = p.x; y = p.y;
}
void setWidth(V ww) { w = ww; }
void setHeight(V hh) { h = hh; }
bool operator==(const G2Rectangle<V>& r) const {
return (x == r.x && y == r.y && w == r.w && h == r.h);
}
bool operator!=(const G2Rectangle<V>& r) const {
return !(*this == r);
}
bool operator<(const G2Rectangle<V>& r) const {
return (
y < r.y || (
y <= r.y && (
x < r.x || (
x <= r.x && (
w < r.w || (
w <= r.w && h < r.h
)
)
)
)
)
);
}
bool operator<=(const G2Rectangle<V>& r) const {
return (
y < r.y || (
y <= r.y && (
x < r.x || (
x <= r.x && (
w < r.w || (
w <= r.w && h <= r.h
)
)
)
)
)
);
}
bool operator>(const G2Rectangle<V>& r) const {
return !(*this <= r);
}
bool operator>=(const G2Rectangle<V>& r) const {
return !(*this < r);
}
bool contains(const G2Point<V>& p) const {
return (
x <= p.x && p.x <= x + w &&
y <= p.y && p.y <= y + h
);
}
bool empty() const {
return (w < V(0) || h < V(0));
}
V area() const {
if (empty())
return V(0);
return w*h;
}
G2Rectangle<V> intersection(const G2Rectangle<V>& r) const {
V x0 = left();
if (r.left() > x0)
x0 = r.left();
V x1 = right();
if (r.right() < x1)
x1 = r.right();
V y0 = bottom();
if (r.bottom() > y0)
y0 = r.bottom();
V y1 = top();
if (r.top() < y1)
y1 = r.top();
return G2Rectangle<V>(x0, y0, x1 - x0, y1 - y0);
}
G2Rectangle<V> combination(const G2Rectangle<V>& r) const {
V x0 = left();
if (r.left() < x0)
x0 = r.left();
V x1 = right();
if (r.right() > x1)
x1 = r.right();
V y0 = bottom();
if (r.bottom() < y0)
y0 = r.bottom();
V y1 = top();
if (r.top() > y1)
y1 = r.top();
return G2Rectangle<V>(x0, y0, x1 - x0, y1 - y0);
}
};
typedef G2Vector<int> I2Vector;
typedef G2Point<int> I2Point;
typedef G2Vector<double> R2Vector;
typedef G2Point<double> R2Point;
typedef G2Rectangle<int> I2Rectangle;
typedef G2Rectangle<double> R2Rectangle;
template <class T = double> class G2Contour:
public std::vector< G2Point<T> > {
public:
mutable bool areaComputed;
mutable double signed_area;
mutable bool rectComputed;
mutable G2Rectangle<T> framing_rect;
G2Contour():
std::vector< G2Point<T> >(),
areaComputed(false),
signed_area(0.),
rectComputed(false),
framing_rect()
{}
G2Contour(int n):
std::vector< G2Point<T> >(n),
areaComputed(false),
signed_area(0.),
rectComputed(false),
framing_rect()
{}
size_t size() const {
return std::vector< G2Point<T> >::size();
}
void clear() {
std::vector< G2Point<T> >::clear();
areaComputed = false;
rectComputed = false;
signed_area = 0.;
}
void push_back(const G2Point<T>& p) {
std::vector< G2Point<T> >::push_back(p);
areaComputed = false;
rectComputed = false;
}
void computeArea() const {
G2Point<T> center;
double s = 0.;
for (size_t i = 0; i < size(); ++i) {
size_t j = i+1;
if (j >= size())
j = 0;
s += G2Point<T>::signedArea(center, (*this)[i], (*this)[j]);
}
signed_area = s;
areaComputed = true;
}
double signedArea() const {
if (!areaComputed)
computeArea();
return signed_area;
}
double area() const {
return fabs(signedArea());
}
int orientation() const {
double sa = signedArea();
if (sa > 0.)
return 1;
else if (sa < 0.)
return (-1);
else
return 0;
}
G2Contour<T>& orientate() {
if (orientation() < 0) {
size_t n2 = size()/2;
for (int i = 0; i < int(n2); ++i) {
std::swap(this->at(i), this->at(size() - 1 - i));
}
}
return *this;
}
void computeFramingRect() const {
if (size() == 0)
return;
T xmin = (*this)[0].x;
T xmax = xmin;
T ymin = (*this)[0].y;
T ymax = ymin;
for (size_t i = 0; i < size(); ++i) {
T x = (*this)[i].x;
T y = (*this)[i].y;
if (x < xmin)
xmin = x;
if (x > xmax)
xmax = x;
if (y < ymin)
ymin = y;
if (y > ymax)
ymax = y;
}
framing_rect.setLeft(xmin);
framing_rect.setBottom(ymin);
framing_rect.setWidth(xmax - xmin);
framing_rect.setHeight(ymax - ymin);
rectComputed = true;
}
G2Rectangle<T>& framingRect() {
if (!rectComputed)
computeFramingRect();
return framing_rect;
}
const G2Rectangle<T>& framingRect() const {
if (!rectComputed)
computeFramingRect();
return framing_rect;
}
bool onBorder(const G2Point<T>& q) const {
for (int i = 0; i < int(size()); ++i) {
int j = i + 1;
if (j >= int(size()))
j = 0;
const G2Point<T>& p0 = this->at(i);
const G2Point<T>& p1 = this->at(j);
if (q == p0 || q == p1)
return true;
if (p0 != p1) {
G2Vector<T> v = p1 - p0;
G2Vector<T> n = v.normal();
G2Vector<T> w = q - p0;
if (
n*w == T(0) &&
w*v >= T(0) &&
w*w <= v*v
) {
return true;
}
}
}
return false;
}
bool contains(const G2Point<T>& p) const {
if (size() <= 1)
return false;
if (!framingRect().contains(p))
return false;
double alpha = 0.;
G2Vector<T> v0 = (*this)[0] - p;
G2Vector<T> v_prev = v0;
for (size_t i = 1; i < size(); ++i) {
G2Vector<T> v = (*this)[i] - p;
alpha += v_prev.angle(v);
v_prev = v;
}
alpha += v_prev.angle(v0);
return (fabs(alpha) >= PI); // Must be either 2*pi or 0
}
bool containsStrictly(const G2Point<T>& p) const {
return (contains(p) && !onBorder(p));
}
R2Point centroid() const {
if (size() == 0)
return R2Point();
double x = 0., y = 0.;
for (size_t i = 0; i < size(); ++i) {
x += double(this->at(i).x);
y += double(this->at(i).y);
}
return R2Point(x/double(size()), y/double(size()));
}
};
// typedef G2Contour<int> I2Contour;
// typedef G2Contour<double> R2Contour;
class R2Contour: public G2Contour<double> {
public:
int contourType;
mutable bool rayDistancesCalculated;
mutable std::vector<double> rayDistances;
static R2Vector ray_directions[MAX_INTERPOLATION_RAYS];
static bool ray_directions_calculated;
public:
R2Contour():
contourType(0),
rayDistancesCalculated(false)
{}
R2Contour(int n):
G2Contour<double>(n),
contourType(0),
rayDistancesCalculated(false)
{}
R2Contour(const I2Contour& c);
R2Contour& operator=(const I2Contour& c);
// Interpolate contours that have star-like shapes.
// 0 <= t <= 1. For t=0 the result equals *this,
// for t=1 the result equals c.
R2Contour starInterpolation(const R2Contour& c, double t) const;
void calculateRayDistances() const;
static void calculate_ray_directions();
};
class I2Contour: public G2Contour<int> {
public:
int contourType;
public:
I2Contour():
contourType(0)
{}
I2Contour(const R2Contour& c);
I2Contour& operator=(const R2Contour& c);
bool canAdd(const I2Point& p) const;
bool canClose() const;
};
// Global functions
bool intersectStraightLines(
const R2Point& p, const R2Vector& v, // First line
const R2Point& q, const R2Vector& w, // Second line
R2Point& intersection // Result
);
bool intersectLineSegmentAndLine(
const R2Point& p0, const R2Point& p1, // Line segment
const R2Point& q, const R2Vector& v, // Straight line
R2Point& intersection // Result
);
bool intersectLineSegments(
const R2Point& p0, const R2Point& p1, // First line segment
const R2Point& q0, const R2Point& q1, // Second line segmant
R2Point& intersection // Result
);
#endif