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
|
#include "StarWorldGeometry.hpp"
namespace Star {
function<float(float, float)> WorldGeometry::xDiffFunction() const {
if (m_size[0] == 0) {
return [](float x1, float x2) -> float { return x1 - x2; };
} else {
unsigned xsize = m_size[0];
return [xsize](float x1, float x2) -> float { return wrapDiffF<float>(x1, x2, xsize); };
}
}
function<Vec2F(Vec2F, Vec2F)> WorldGeometry::diffFunction() const {
if (m_size[0] == 0) {
return [](Vec2F const& a, Vec2F const& b) -> Vec2F { return a - b; };
} else {
unsigned xsize = m_size[0];
return [xsize](Vec2F const& a, Vec2F const& b) -> Vec2F {
return Vec2F(wrapDiffF<float>(a[0], b[0], xsize), a[1] - b[1]);
};
}
}
function<float(float, float, float)> WorldGeometry::xLerpFunction(Maybe<float> discontinuityThreshold) const {
if (m_size[0] == 0) {
return [](float, float min, float) -> float { return min; };
} else {
unsigned xsize = m_size[0];
return [discontinuityThreshold, xsize](float offset, float min, float max) -> float {
float distance = wrapDiffF<float>(max, min, xsize);
if (discontinuityThreshold && abs(distance) > *discontinuityThreshold)
return min + distance;
return min + offset * distance;
};
}
}
function<Vec2F(float, Vec2F, Vec2F)> WorldGeometry::lerpFunction(Maybe<float> discontinuityThreshold) const {
if (m_size[0] == 0) {
return [](float, Vec2F const& min, Vec2F const&) -> Vec2F { return min; };
} else {
unsigned xsize = m_size[0];
return [discontinuityThreshold, xsize](float offset, Vec2F const& min, Vec2F const& max) -> Vec2F {
Vec2F distance = Vec2F(wrapDiffF<float>(max[0], min[0], xsize), max[1] - min[1]);
if (discontinuityThreshold && distance.magnitude() > *discontinuityThreshold)
return min + distance;
return min + offset * distance;
};
}
}
StaticList<RectF, 2> WorldGeometry::splitRect(RectF const& bbox) const {
if (bbox.isNull() || m_size[0] == 0)
return {bbox};
Vec2F minWrap = xwrap(bbox.min());
RectF bboxWrap = RectF(minWrap, minWrap + bbox.size());
// This does not work for ranges greater than m_size[0] wide!
starAssert(bbox.xMax() - bbox.xMin() <= (float)m_size[0]);
// Since min is wrapped, we're only checking to see if max is on the other
// side of the wrap point
if (bboxWrap.xMax() > m_size[0]) {
return {RectF(bboxWrap.xMin(), bboxWrap.yMin(), m_size[0], bboxWrap.yMax()),
RectF(0, bboxWrap.yMin(), bboxWrap.xMax() - m_size[0], bboxWrap.yMax())};
} else {
return {bboxWrap};
}
}
StaticList<RectF, 2> WorldGeometry::splitRect(RectF bbox, Vec2F const& position) const {
bbox.translate(position);
return splitRect(bbox);
}
StaticList<RectI, 2> WorldGeometry::splitRect(RectI const bbox) const {
if (bbox.isNull() || m_size[0] == 0)
return {bbox};
Vec2I minWrap = xwrap(bbox.min());
RectI bboxWrap = RectI(minWrap, minWrap + bbox.size());
// This does not work for ranges greater than m_size[0] wide!
starAssert(bbox.xMax() - bbox.xMin() <= (int)m_size[0]);
// Since min is wrapped, we're only checking to see if max is on the other
// side of the wrap point
if (bboxWrap.xMax() > (int)m_size[0]) {
return {RectI(bboxWrap.xMin(), bboxWrap.yMin(), m_size[0], bboxWrap.yMax()),
RectI(0, bboxWrap.yMin(), bboxWrap.xMax() - m_size[0], bboxWrap.yMax())};
} else {
return {bboxWrap};
}
}
StaticList<Line2F, 2> WorldGeometry::splitLine(Line2F line, bool preserveDirection) const {
if (m_size[0] == 0)
return {line};
bool swapDirection = line.makePositive() && preserveDirection;
Vec2F minWrap = xwrap(line.min());
// diff is safe because we're looking for the line gnostic diff
Line2F lineWrap = Line2F(minWrap, minWrap + line.diff());
// Since min is wrapped, we're only checking to see if max is on the other
// side of the wrap point
if (lineWrap.max()[0] > m_size[0]) {
Vec2F intersection = lineWrap.intersection(Line2F(Vec2F(m_size[0], 0), Vec2F(m_size)), true).point;
if (swapDirection)
return {Line2F(lineWrap.max() - Vec2F(m_size[0], 0), Vec2F(0, intersection[1])),
Line2F(Vec2F(m_size[0], intersection[1]), lineWrap.min())};
else
return {Line2F(lineWrap.min(), Vec2F(m_size[0], intersection[1])),
Line2F(Vec2F(0, intersection[1]), lineWrap.max() - Vec2F(m_size[0], 0))};
} else {
if (swapDirection)
lineWrap.reverse();
return {lineWrap};
}
}
StaticList<Line2F, 2> WorldGeometry::splitLine(Line2F line, Vec2F const& position, bool preserveDirection) const {
line.translate(position);
return splitLine(line, preserveDirection);
}
StaticList<PolyF, 2> WorldGeometry::splitPoly(PolyF const& poly) const {
if (poly.isNull() || m_size[0] == 0)
return {poly};
Array<PolyF, 2> res;
bool polySelect = false;
Line2F worldBoundRight = {Vec2F(m_size[0], 0), Vec2F(m_size[0], 1)};
Line2F worldBoundLeft = {Vec2F(0, 0), Vec2F(0, 1)};
for (unsigned i = 0; i < poly.sides(); i++) {
Line2F segment = poly.side(i);
if ((segment.min()[0] < 0) ^ (segment.max()[0] < 0)) {
Vec2F worldCorrect = {(float)m_size[0], 0};
Vec2F intersect = segment.intersection(worldBoundLeft, true).point;
if (segment.min()[0] < 0) {
res[polySelect].add(segment.min() + worldCorrect);
res[polySelect].add(Vec2F(m_size[0], intersect[1]));
polySelect = !polySelect;
res[polySelect].add(Vec2F(0, intersect[1]));
} else {
res[polySelect].add(segment.min());
res[polySelect].add(Vec2F(0, intersect[1]));
polySelect = !polySelect;
res[polySelect].add(Vec2F(m_size[0], intersect[1]));
}
} else if ((segment.min()[0] > m_size[0]) ^ (segment.max()[0] > m_size[0])) {
Vec2F worldCorrect = {(float)m_size[0], 0};
Vec2F intersect = segment.intersection(worldBoundRight, true).point;
if (segment.min()[0] > m_size[0]) {
res[polySelect].add(segment.min() - worldCorrect);
res[polySelect].add(Vec2F(0, intersect[1]));
polySelect = !polySelect;
res[polySelect].add(Vec2F(m_size[0], intersect[1]));
} else {
res[polySelect].add(segment.min());
res[polySelect].add(Vec2F(m_size[0], intersect[1]));
polySelect = !polySelect;
res[polySelect].add(Vec2F(0, intersect[1]));
}
} else {
if (segment.min()[0] < 0) {
res[polySelect].add(segment.min() + Vec2F((float)m_size[0], 0));
} else if (segment.min()[0] > m_size[0]) {
res[polySelect].add(segment.min() - Vec2F((float)m_size[0], 0));
} else {
res[polySelect].add(segment.min());
}
}
}
if (res[1].isNull())
return {res[0]};
if (res[0].isNull())
return {res[1]};
else
return {res[0], res[1]};
}
StaticList<PolyF, 2> WorldGeometry::splitPoly(PolyF poly, Vec2F const& position) const {
poly.translate(position);
return splitPoly(poly);
}
StaticList<Vec2I, 2> WorldGeometry::splitXRegion(Vec2I const& xRegion) const {
if (m_size[0] == 0)
return {xRegion};
starAssert(xRegion[1] >= xRegion[0]);
// This does not work for ranges greater than m_size[0] wide!
starAssert(xRegion[1] - xRegion[0] <= (int)m_size[0]);
int x1 = xwrap(xRegion[0]);
int x2 = x1 + xRegion[1] - xRegion[0];
if (x2 > (int)m_size[0]) {
return {Vec2I(x1, m_size[0]), Vec2I(0.0f, x2 - m_size[0])};
} else {
return {{x1, x2}};
}
}
StaticList<Vec2F, 2> WorldGeometry::splitXRegion(Vec2F const& xRegion) const {
if (m_size[0] == 0)
return {xRegion};
starAssert(xRegion[1] >= xRegion[0]);
// This does not work for ranges greater than m_size[0] wide!
starAssert(xRegion[1] - xRegion[0] <= (float)m_size[0]);
float x1 = xwrap(xRegion[0]);
float x2 = x1 + xRegion[1] - xRegion[0];
if (x2 > m_size[0]) {
return {Vec2F(x1, m_size[0]), Vec2F(0.0f, x2 - m_size[0])};
} else {
return {{x1, x2}};
}
}
bool WorldGeometry::rectContains(RectF const& rect, Vec2F const& pos) const {
auto wpos = xwrap(pos);
for (auto const& r : splitRect(rect)) {
if (r.contains(wpos))
return true;
}
return false;
}
bool WorldGeometry::rectIntersectsRect(RectF const& rect1, RectF const& rect2) const {
for (auto const& r1 : splitRect(rect1)) {
for (auto const& r2 : splitRect(rect2)) {
if (r1.intersects(r2))
return true;
}
}
return false;
}
RectF WorldGeometry::rectOverlap(RectF const& rect1, RectF const& rect2) const {
return rect1.overlap(RectF::withSize(nearestTo(rect1.min(), rect2.min()), rect2.size()));
}
bool WorldGeometry::polyContains(PolyF const& poly, Vec2F const& pos) const {
auto wpos = xwrap(pos);
for (auto const& p : splitPoly(poly)) {
if (p.contains(wpos))
return true;
}
return false;
}
float WorldGeometry::polyOverlapArea(PolyF const& poly1, PolyF const& poly2) const {
float area = 0.0f;
for (auto const& p1 : splitPoly(poly1)) {
for (auto const& p2 : splitPoly(poly2))
area += PolyF::clip(p1, p2).convexArea();
}
return area;
}
bool WorldGeometry::lineIntersectsRect(Line2F const& line, RectF const& rect) const {
for (auto l : splitLine(line)) {
for (auto box : splitRect(rect)) {
if (box.intersects(l)) {
return true;
}
}
}
return false;
}
bool WorldGeometry::lineIntersectsPoly(Line2F const& line, PolyF const& poly) const {
for (auto a : splitLine(line)) {
for (auto b : splitPoly(poly)) {
if (b.intersects(a)) {
return true;
}
}
}
return false;
}
bool WorldGeometry::polyIntersectsPoly(PolyF const& polyA, PolyF const& polyB) const {
for (auto a : splitPoly(polyA)) {
for (auto b : splitPoly(polyB)) {
if (b.intersects(a))
return true;
}
}
return false;
}
bool WorldGeometry::rectIntersectsCircle(RectF const& rect, Vec2F const& center, float radius) const {
if (rect.contains(center))
return true;
for (auto const& e : rect.edges()) {
if (lineIntersectsCircle(e, center, radius))
return true;
}
return false;
}
bool WorldGeometry::lineIntersectsCircle(Line2F const& line, Vec2F const& center, float radius) const {
for (auto const& sline : splitLine(line)) {
if (sline.distanceTo(nearestTo(sline.center(), center)) <= radius)
return true;
}
return false;
}
Maybe<Vec2F> WorldGeometry::lineIntersectsPolyAt(Line2F const& line, PolyF const& poly) const {
for (auto a : splitLine(line, true)) {
for (auto b : splitPoly(poly)) {
if (auto intersection = b.lineIntersection(a))
return intersection->point;
}
}
return {};
}
float WorldGeometry::polyDistance(PolyF const& poly, Vec2F const& point) const {
auto spoint = nearestTo(poly.center(), point);
return poly.distance(spoint);
}
Vec2F WorldGeometry::nearestCoordInBox(RectF const& box, Vec2F const& pos) const {
RectF t(box);
auto offset = t.center();
auto r = diff(pos, offset);
t.setCenter({});
return t.nearestCoordTo(r) + offset;
}
Vec2F WorldGeometry::diffToNearestCoordInBox(RectF const& box, Vec2F const& pos) const {
RectF t(box);
auto offset = t.center();
auto r = diff(pos, offset);
t.setCenter({});
auto coord = t.nearestCoordTo(r) + offset;
return diff(pos, coord);
}
}
|