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
// Port of box2d-cpp-reference/src/hull.c, plus the b2Hull type and
// B2_MAX_POLYGON_VERTICES from include/box2d/collision.h.
//
// quickhull:
// - merges vertices based on the linear slop
// - removes collinear points using the linear slop
// - returns an empty hull if it fails
//
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT
// This is a line-by-line port of a quickhull that relies on explicit index
// arithmetic — welding by comparing ps[i] against ps[j<i], swapping removed
// points with ps[n-1], and walking modular triples (i, i+1, i+2) % count.
// Rewriting these as iterators would obscure the correspondence to the C.
#![allow(clippy::needless_range_loop)]
use crate::constants::linear_slop;
use crate::math_functions::{
aabb_center, cross, distance_squared, max, min, min_int, normalize, sub, Aabb, Vec2, VEC2_ZERO,
};
/// The maximum number of vertices on a convex polygon. Changing this affects
/// performance even if you don't use more vertices. (B2_MAX_POLYGON_VERTICES)
pub const MAX_POLYGON_VERTICES: usize = 8;
/// A convex hull. Used to construct convex polygons. (b2Hull)
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Hull {
/// The final points of the hull
pub points: [Vec2; MAX_POLYGON_VERTICES],
/// The number of points
pub count: i32,
}
impl Default for Hull {
fn default() -> Self {
Hull {
points: [VEC2_ZERO; MAX_POLYGON_VERTICES],
count: 0,
}
}
}
// quickhull recursion
fn recurse_hull(p1: Vec2, p2: Vec2, ps: &[Vec2]) -> Hull {
let mut hull = Hull::default();
let count = ps.len();
if count == 0 {
return hull;
}
// create an edge vector pointing from p1 to p2
let e = normalize(sub(p2, p1));
// discard points left of e and find point furthest to the right of e
let mut right_points = [VEC2_ZERO; MAX_POLYGON_VERTICES];
let mut right_count = 0;
let mut best_index = 0;
let mut best_distance = cross(sub(ps[best_index], p1), e);
if best_distance > 0.0 {
right_points[right_count] = ps[best_index];
right_count += 1;
}
for i in 1..count {
let distance = cross(sub(ps[i], p1), e);
if distance > best_distance {
best_index = i;
best_distance = distance;
}
if distance > 0.0 {
right_points[right_count] = ps[i];
right_count += 1;
}
}
if best_distance < 2.0 * linear_slop() {
return hull;
}
let best_point = ps[best_index];
// compute hull to the right of p1-bestPoint
let hull1 = recurse_hull(p1, best_point, &right_points[..right_count]);
// compute hull to the right of bestPoint-p2
let hull2 = recurse_hull(best_point, p2, &right_points[..right_count]);
// stitch together hulls
for i in 0..hull1.count as usize {
hull.points[hull.count as usize] = hull1.points[i];
hull.count += 1;
}
hull.points[hull.count as usize] = best_point;
hull.count += 1;
for i in 0..hull2.count as usize {
hull.points[hull.count as usize] = hull2.points[i];
hull.count += 1;
}
debug_assert!(hull.count < MAX_POLYGON_VERTICES as i32);
hull
}
/// Compute the convex hull of a set of points. Returns an empty hull if it
/// fails. (b2ComputeHull)
///
/// Some failure cases:
/// - all points very close together
/// - all points on a line
/// - fewer than 3 points
/// - more than [`MAX_POLYGON_VERTICES`] points
pub fn compute_hull(points: &[Vec2]) -> Hull {
let mut hull = Hull::default();
let mut count = points.len() as i32;
if count < 3 || count > MAX_POLYGON_VERTICES as i32 {
// check your data
return hull;
}
count = min_int(count, MAX_POLYGON_VERTICES as i32);
let mut aabb = Aabb {
lower_bound: Vec2 {
x: f32::MAX,
y: f32::MAX,
},
upper_bound: Vec2 {
x: -f32::MAX,
y: -f32::MAX,
},
};
// Perform aggressive point welding. First point always remains.
// Also compute the bounding box for later.
let mut ps = [VEC2_ZERO; MAX_POLYGON_VERTICES];
let mut n = 0;
let slop = linear_slop();
let tol_sqr = 16.0 * slop * slop;
for i in 0..count as usize {
aabb.lower_bound = min(aabb.lower_bound, points[i]);
aabb.upper_bound = max(aabb.upper_bound, points[i]);
let vi = points[i];
let mut unique = true;
for j in 0..i {
let vj = points[j];
let dist_sqr = distance_squared(vi, vj);
if dist_sqr < tol_sqr {
unique = false;
break;
}
}
if unique {
ps[n] = vi;
n += 1;
}
}
if n < 3 {
// all points very close together, check your data and check your scale
return hull;
}
// Find an extreme point as the first point on the hull
let c = aabb_center(aabb);
let mut f1 = 0;
let mut dsq1 = distance_squared(c, ps[f1]);
for i in 1..n {
let dsq = distance_squared(c, ps[i]);
if dsq > dsq1 {
f1 = i;
dsq1 = dsq;
}
}
// remove p1 from working set
let p1 = ps[f1];
ps[f1] = ps[n - 1];
n -= 1;
let mut f2 = 0;
let mut dsq2 = distance_squared(p1, ps[f2]);
for i in 1..n {
let dsq = distance_squared(p1, ps[i]);
if dsq > dsq2 {
f2 = i;
dsq2 = dsq;
}
}
// remove p2 from working set
let p2 = ps[f2];
ps[f2] = ps[n - 1];
n -= 1;
// split the points into points that are left and right of the line p1-p2.
let mut right_points = [VEC2_ZERO; MAX_POLYGON_VERTICES - 2];
let mut right_count = 0;
let mut left_points = [VEC2_ZERO; MAX_POLYGON_VERTICES - 2];
let mut left_count = 0;
let e = normalize(sub(p2, p1));
for i in 0..n {
let d = cross(sub(ps[i], p1), e);
// slop used here to skip points that are very close to the line p1-p2
if d >= 2.0 * slop {
right_points[right_count] = ps[i];
right_count += 1;
} else if d <= -2.0 * slop {
left_points[left_count] = ps[i];
left_count += 1;
}
}
// compute hulls on right and left
let hull1 = recurse_hull(p1, p2, &right_points[..right_count]);
let hull2 = recurse_hull(p2, p1, &left_points[..left_count]);
if hull1.count == 0 && hull2.count == 0 {
// all points collinear
return hull;
}
// stitch hulls together, preserving CCW winding order
hull.points[hull.count as usize] = p1;
hull.count += 1;
for i in 0..hull1.count as usize {
hull.points[hull.count as usize] = hull1.points[i];
hull.count += 1;
}
hull.points[hull.count as usize] = p2;
hull.count += 1;
for i in 0..hull2.count as usize {
hull.points[hull.count as usize] = hull2.points[i];
hull.count += 1;
}
debug_assert!(hull.count <= MAX_POLYGON_VERTICES as i32);
// merge collinear
let mut searching = true;
while searching && hull.count > 2 {
searching = false;
for i in 0..hull.count as usize {
let i1 = i;
let i2 = (i + 1) % hull.count as usize;
let i3 = (i + 2) % hull.count as usize;
let s1 = hull.points[i1];
let s2 = hull.points[i2];
let s3 = hull.points[i3];
// unit edge vector for s1-s3
let r = normalize(sub(s3, s1));
let distance = cross(sub(s2, s1), r);
if distance <= 2.0 * slop {
// remove midpoint from hull
for j in i2..(hull.count as usize - 1) {
hull.points[j] = hull.points[j + 1];
}
hull.count -= 1;
// continue searching for collinear points
searching = true;
break;
}
}
}
if hull.count < 3 {
// all points collinear, shouldn't be reached since this was validated above
hull.count = 0;
}
hull
}
/// Validate that a hull is convex, CCW, and has no collinear points.
/// (b2ValidateHull)
pub fn validate_hull(hull: &Hull) -> bool {
if hull.count < 3 || (MAX_POLYGON_VERTICES as i32) < hull.count {
return false;
}
let count = hull.count as usize;
// test that every point is behind every edge
for i in 0..count {
// create an edge vector
let i1 = i;
let i2 = if i < count - 1 { i1 + 1 } else { 0 };
let p = hull.points[i1];
let e = normalize(sub(hull.points[i2], p));
for (j, &point) in hull.points[..count].iter().enumerate() {
// skip points that subtend the current edge
if j == i1 || j == i2 {
continue;
}
let distance = cross(sub(point, p), e);
if distance >= 0.0 {
return false;
}
}
}
// test for collinear points
let slop = linear_slop();
for i in 0..count {
let i1 = i;
let i2 = (i + 1) % count;
let i3 = (i + 2) % count;
let p1 = hull.points[i1];
let p2 = hull.points[i2];
let p3 = hull.points[i3];
let e = normalize(sub(p3, p1));
let distance = cross(sub(p2, p1), e);
if distance <= slop {
// p1-p2-p3 are collinear
return false;
}
}
true
}