-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry2D.h
296 lines (220 loc) · 5.8 KB
/
geometry2D.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
#pragma once
#include <vector>
#include <cassert>
#include "cinder-lite/Vector.h"
using Vec2 = cinderlite::Vec2<double>;
int contact_3circle(Vec2 c1, double r1,
Vec2 c2, double r2,
double r3,
Vec2 &c31, Vec2 &c32,
bool inside_c2=false);
struct BBox {
Vec2 Cmin, Cmax;
BBox(double xmin = 0, double ymin = 0,
double xmax = 0, double ymax = 0):
Cmin(xmin, ymin), Cmax(xmax, ymax) {}
BBox(Vec2 Cmin, Vec2 Cmax): Cmin(Cmin), Cmax(Cmax) {}
double surface() const {
Vec2 v = Cmax - Cmin;
return v.x * v.y;
}
// shortest distance between the two
double distance(const BBox & other) const;
// nearest bbox corner w.r.t. p
Vec2 nearest_corner(Vec2 p) const;
};
struct Shape2D {
int id;
Shape2D(int id = -1): id(id) {}
virtual bool intersects(const BBox & bbox) const = 0;
virtual Shape2D * clone() const = 0;
virtual ~Shape2D() {}
};
struct Circle: Shape2D {
Vec2 c;
double r;
Circle(Vec2 c, double r, int id=-1):
Shape2D(id), c(c), r(r) {}
Circle(double x, double y, double r, int id=-1):
Shape2D(id), c(x, y), r(r) {}
bool intersects(const BBox & bbox) const final;
bool intersects(const Circle & other) const;
Shape2D * clone() const {return new Circle(*this); }
virtual ~Circle() {}
};
/********************************************
* KDTree
********************************************/
struct Node {
BBox bbox;
std::vector<Shape2D *> shapes;
Node * child1 = nullptr;
Node * child2 = nullptr;
bool is_leaf() const {
return child1 == nullptr;
}
Node(const BBox & bbox): bbox(bbox) {}
void split();
void add_shape(Shape2D *shape, int max_per_leaf);
};
struct KDTree {
Node * root;
int max_per_leaf = 10;
std::vector<Shape2D*> flat_shapes;
KDTree(const BBox & bbox) {
root = new Node(bbox);
}
~KDTree();
void add_shape(const Shape2D &shape);
/// bool check_consistent() const;
};
/********************************************
* Iterators
********************************************/
struct ShapeVectorIterator {
const std::vector<Shape2D*> &shapes;
size_t i = 0;
ShapeVectorIterator(const std::vector<Shape2D*> &shapes):
shapes(shapes) {}
bool has_next() {
return i < shapes.size();
}
const Shape2D * next() {
if(!has_next()) return nullptr;
return shapes[i++];
}
};
struct LeafIterator {
std::vector<const Node*> stack;
LeafIterator(const KDTree & tree) {
// reach leftmost leaf
stack.push_back(tree.root);
}
bool has_next() {
return !stack.empty();
}
const Node * next() {
if (!has_next()) return nullptr;
// find next leaf
while(!stack.back()->is_leaf()) {
const Node * n = stack.back();
stack.pop_back();
stack.push_back(n->child2);
stack.push_back(n->child1);
}
// we have our leaf
const Node *ret = stack.back();
stack.pop_back();
return ret;
}
};
struct IntersectingLeavesIterator {
std::vector<const Node*> stack;
const Shape2D &shape;
IntersectingLeavesIterator(const KDTree & tree, const Shape2D &shape):shape(shape) {
// reach leftmost leaf
if (shape.intersects(tree.root->bbox))
stack.push_back(tree.root);
}
bool has_next() {
return !stack.empty();
}
const Node * next() {
if (!has_next()) return nullptr;
// find next leaf
while(!stack.back()->is_leaf()) {
const Node * n = stack.back();
stack.pop_back();
if (shape.intersects(n->child2->bbox))
stack.push_back(n->child2);
if (shape.intersects(n->child1->bbox))
stack.push_back(n->child1);
if (!has_next()) return nullptr;
}
// we have our leaf
const Node *ret = stack.back();
stack.pop_back();
return ret;
}
};
struct TwoNodes {
const Node* node1;
const Node *node2;
TwoNodes(const Node* node1 = nullptr,
const Node *node2 = nullptr):
node1(node1), node2(node2) {}
};
struct TwoLeavesIterator {
double distance;
std::vector<TwoNodes> stack;
TwoLeavesIterator(const KDTree & tree, double distance):
distance(distance) {
push_single(tree.root);
while(!is_returnable()) {
step_stack();
}
}
bool is_returnable() const {
if (stack.empty())
return true;
TwoNodes top = stack.back();
if(!top.node2)
return false;
return top.node1->is_leaf() && top.node2->is_leaf();
}
void push_single(const Node *n) {
if (!n->is_leaf()) {
stack.push_back(TwoNodes(n));
}
}
void push_pair(const Node *node1, const Node *node2) {
if (node1->bbox.distance(node2->bbox) < distance) {
stack.push_back(TwoNodes(node1, node2));
}
}
void step_stack() {
assert(!is_returnable());
const TwoNodes & top = stack.back();
stack.pop_back();
const Node *root1 = top.node1;
const Node *root2 = top.node2;
if(!top.node2) {
if(root1->is_leaf()) {
// nothing
} else {
push_pair(root1->child1, root1->child2);
push_single(root1->child1);
push_single(root1->child2);
}
} else {
if (root1->is_leaf()) {
assert(!root2->is_leaf());
push_pair(root1, root2->child1);
push_pair(root1, root2->child2);
} else if(root2->is_leaf()) {
push_pair(root1->child1, root2);
push_pair(root1->child2, root2);
} else {
push_pair(root1->child1, root2->child1);
push_pair(root1->child2, root2->child1);
push_pair(root1->child1, root2->child2);
push_pair(root1->child2, root2->child2);
}
}
}
bool has_next() {
return !stack.empty();
}
TwoNodes next() {
assert(is_returnable());
if (!has_next()) {
return TwoNodes();
}
TwoNodes top = stack.back();
stack.pop_back();
while(!is_returnable()) {
step_stack();
}
return top;
}
};