forked from grow-graphics/xy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aabb.go
491 lines (457 loc) · 14.3 KB
/
aabb.go
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
package xy
/*
AABB consists of a position, a size, and several utility functions. It is typically used for fast overlap tests.
It uses floating-point coordinates. The 2D counterpart to [AABB] is [Rect2].
Negative values for size are not supported and will not work for most methods. Use abs to get an [AABB] with a positive size.
Note: Unlike [Rect2], [AABB] does not have a variant that uses integer coordinates.
*/
type AABB struct {
Position Vector3
Size Vector3
}
// "Fields"
func (a AABB) End() Vector3 { return a.Position.Add(a.Size) }
func (a *AABB) SetEnd(end Vector3) { a.Size = end.Sub(a.Position) }
// "Methods"
// Abs returns an [AABB] with equivalent position and size, modified so that the most-negative corner is the origin and the
// size is positive.
func (a AABB) Abs() AABB { //AABB.abs
return AABB{
Position: Vector3{a.Position[X] + min(a.Size[X], 0), a.Position[Y] + min(a.Size[Y], 0), a.Position[Z] + min(a.Size[Z], 0)},
Size: a.Size.Abs(),
}
}
// Encloses returns true if this [AABB] completely encloses another one.
func (a AABB) Encloses(b AABB) bool { //AABB.encloses
var src_min = a.Position
var src_max = a.Position.Add(a.Size)
var dst_min = b.Position
var dst_max = b.Position.Add(b.Size)
return ((src_min[X] <= dst_min[X]) &&
(src_max[X] > dst_max[X]) &&
(src_min[Y] <= dst_min[Y]) &&
(src_max[Y] > dst_max[Y]) &&
(src_min[Z] <= dst_min[Z]) &&
(src_max[Z] > dst_max[Z]))
}
// Expand returns a copy of this [AABB] expanded to include a given point.
//
// // position (-3, 2, 0), size (1, 1, 1)
// var box = AABB{Vector3{-3,2,0}, Vector3{1,1,1}}
// // position (-3, -1, 0), size (3, 4, 2), so we fit both the original AABB and Vector3(0, -1, 2)
// var box2 = box.Expand(Vector3{0, -1, 2})
func (a AABB) Expand(to Vector3) AABB { //AABB.expand
var begin = a.Position
var end = a.Position.Add(a.Size)
if to[X] < begin[X] {
begin[X] = to[X]
}
if to[Y] < begin[Y] {
begin[Y] = to[Y]
}
if to[Z] < begin[Z] {
begin[Z] = to[Z]
}
if to[X] > end[X] {
end[X] = to[X]
}
if to[Y] > end[Y] {
end[Y] = to[Y]
}
if to[Z] > end[Z] {
end[Z] = to[Z]
}
return AABB{
Position: begin,
Size: end.Sub(begin),
}
}
// Center returns the center of the [AABB], which is equal to position + (size / 2).
func (a AABB) Center() Vector3 { //AABB.get_center
return a.Position.Add(a.Size.Divf(2))
}
// Endpoint gets the position of the 8 endpoints of the [AABB] in space.
func (a AABB) Endpoint(idx int) Vector3 { //AABB.get_endpoint
switch idx {
case 0:
return Vector3{a.Position[X], a.Position[Y], a.Position[Z]}
case 1:
return Vector3{a.Position[X], a.Position[Y], a.Position[Z] + a.Size[Z]}
case 2:
return Vector3{a.Position[X], a.Position[Y] + a.Size[Y], a.Position[Z]}
case 3:
return Vector3{a.Position[X], a.Position[Y] + a.Size[Y], a.Position[Z] + a.Size[Z]}
case 4:
return Vector3{a.Position[X] + a.Size[X], a.Position[Y], a.Position[Z]}
case 5:
return Vector3{a.Position[X] + a.Size[X], a.Position[Y], a.Position[Z] + a.Size[Z]}
case 6:
return Vector3{a.Position[X] + a.Size[X], a.Position[Y] + a.Size[Y], a.Position[Z]}
case 7:
return Vector3{a.Position[X] + a.Size[X], a.Position[Y] + a.Size[Y], a.Position[Z] + a.Size[Z]}
default:
panic("AABB.GetEndpoint(): invalid index")
}
}
// LongestAxis returns the normalized longest axis of the [AABB].
func (a AABB) LongestAxis() Vector3 { //AABB.get_longest_axis
var (
axis = Vector3{1, 0, 0}
max_size = a.Size[X]
)
if a.Size[Y] > max_size {
axis = Vector3{0, 1, 0}
max_size = a.Size[Y]
}
if a.Size[Z] > max_size {
axis = Vector3{0, 0, 1}
}
return axis
}
// LongestAxisIndex returns the index of the longest axis of the [AABB] (according to [Axis] constants).
func (a AABB) LongestAxisIndex() Axis { //AABB.get_longest_axis_index
var axis = X
var max_size = a.Size[X]
if a.Size[Y] > max_size {
axis = Y
max_size = a.Size[Y]
}
if a.Size[Z] > max_size {
axis = Z
}
return axis
}
// LongestAxisSize returns the scalar length of the longest axis of the AABB.
func (a AABB) LongestAxisSize() float64 { //AABB.get_longest_axis_size
var max_size = a.Size[x]
if a.Size[Y] > max_size {
max_size = a.Size[Y]
}
if a.Size[Z] > max_size {
max_size = a.Size[Z]
}
return float64(max_size)
}
// ShortestAxis returns the normalized shortest axis of the [AABB].
func (a AABB) ShortestAxis() Vector3 { //AABB.get_shortest_axis
var (
axis = Vector3{1, 0, 0}
min_size = a.Size[X]
)
if a.Size[Y] < min_size {
axis = Vector3{0, 1, 0}
min_size = a.Size[Y]
}
if a.Size[Z] < min_size {
axis = Vector3{0, 0, 1}
}
return axis
}
// ShortestAxisIndex returns the index of the shortest axis of the [AABB] (according to [Axis] constants).
func (a AABB) ShortestAxisIndex() Axis { //AABB.get_shortest_axis_index
var axis = X
var min_size = a.Size[X]
if a.Size[Y] < min_size {
axis = Y
min_size = a.Size[Y]
}
if a.Size[Z] < min_size {
axis = Z
}
return axis
}
// ShortestAxisSize returns the scalar length of the shortest axis of the [AABB].
func (a AABB) ShortestAxisSize() float64 { //AABB.get_shortest_axis_size
var min_size = a.Size[x]
if a.Size[Y] < min_size {
min_size = a.Size[Y]
}
if a.Size[Z] < min_size {
min_size = a.Size[Z]
}
return float64(min_size)
}
// Support returns the vertex of the [AABB] that's the farthest in a given direction. This point is commonly
// known as the support point in collision detection algorithms.
func (a AABB) Support(dir Vector3) Vector3 { //AABB.get_support
var (
half_extents = a.Size.Mulf(0.5)
ofs = a.Position.Add(half_extents)
)
return Vector3{
ʕ(dir[X] > 0, half_extents[X], -half_extents[X]),
ʕ(dir[Y] > 0, half_extents[Y], -half_extents[Y]),
ʕ(dir[Z] > 0, half_extents[Z], -half_extents[Z]),
}.Add(ofs)
}
// Volume returns the volume of the [AABB].
func (a AABB) Volume() float64 { return float64(a.Size[X] * a.Size[Y] * a.Size[Z]) } // AABB.get_volume
// Grow returns a copy of the AABB grown a given number of units towards all the sides.
func (a AABB) Grow(by float64) AABB { //AABB.grow
a.Position = a.Position.Subf(by)
a.Size = a.Size.Addf(by * 2)
return a
}
// HasPoint returns true if the [AABB] contains a point. Points on the faces of the [AABB] are considered included,
// though float-point precision errors may impact the accuracy of such checks.
//
// Note: This method is not reliable for [AABB] with a negative size. Use abs to get a positive sized equivalent
// [AABB] to check for contained points.
func (a AABB) HasPoint(point Vector3) bool { //AABB.has_point
if point[X] < a.Position[X] {
return false
}
if point[Y] < a.Position[Y] {
return false
}
if point[Z] < a.Position[Z] {
return false
}
if point[X] > a.Position[X]+a.Size[X] {
return false
}
if point[Y] > a.Position[Y]+a.Size[Y] {
return false
}
if point[Z] > a.Position[Z]+a.Size[Z] {
return false
}
return true
}
// HasSurface returns true if the [AABB] has a surface or a length, and false if the [AABB] is empty (all components of
// size are zero or negative).
func (a AABB) HasSurface() bool { return a.Size[X] > 0 || a.Size[Y] > 0 || a.Size[Z] > 0 } //AABB.has_surface
// HasVolume returns true if the [AABB] has a volume, and false if the [AABB] is flat, empty, or has a negative size.
func (a AABB) HasVolume() bool { return a.Size[X] > 0 && a.Size[Y] > 0 && a.Size[Z] > 0 } //AABB.has_volume
// Intersection returns the intersection between two [AABB]. An empty [AABB] (size (0, 0, 0)) is returned on failure.
func (a AABB) Intersection(b AABB) AABB { //AABB.intersection
var (
src_min = a.Position
src_max = a.Position.Add(a.Size)
dst_min = b.Position
dst_max = b.Position.Add(a.Size)
)
var (
min, max Vector3
)
if src_min[X] > dst_max[X] || src_max[X] < dst_min[X] {
return AABB{}
} else {
min[X] = ʕ(src_min[X] > dst_min[X], src_min[X], dst_min[X])
max[X] = ʕ(src_max[X] < dst_max[X], src_max[X], dst_max[X])
}
if src_min[Y] > dst_max[Y] || src_max[Y] < dst_min[Y] {
return AABB{}
} else {
min[Y] = ʕ(src_min[Y] > dst_min[Y], src_min[Y], dst_min[Y])
max[Y] = ʕ(src_max[Y] < dst_max[Y], src_max[Y], dst_max[Y])
}
if src_min[Z] > dst_max[Z] || src_max[Z] < dst_min[Z] {
return AABB{}
} else {
min[Z] = ʕ(src_min[Z] > dst_min[Z], src_min[Z], dst_min[Z])
max[Z] = ʕ(src_max[Z] < dst_max[Z], src_max[Z], dst_max[Z])
}
return AABB{min, max.Sub(min)}
}
// Intersects returns true if the [AABB] overlaps with another.
func (a AABB) Intersects(b AABB) bool { // AABB.intersects
if a.Position[X] >= (b.Position[X] + b.Size[X]) {
return false
}
if (a.Position[X] + a.Size[X]) <= b.Position[X] {
return false
}
if a.Position[Y] >= (b.Position[Y] + b.Size[Y]) {
return false
}
if (a.Position[Y] + a.Size[Y]) <= b.Position[Y] {
return false
}
if a.Position[Z] >= (b.Position[Z] + b.Size[Z]) {
return false
}
if (a.Position[Z] + a.Size[Z]) <= b.Position[Z] {
return false
}
return true
}
// IntersectsPlane returns true if the [AABB] is on both sides of a plane.
func (a AABB) IntersectsPlane(plane Plane) bool { //AABB.intersects_plane
var points = [8]Vector3{
{a.Position[X], a.Position[Y], a.Position[Z]},
{a.Position[X], a.Position[Y], a.Position[Z] + a.Size[Z]},
{a.Position[X], a.Position[Y] + a.Size[Y], a.Position[Z]},
{a.Position[X], a.Position[Y] + a.Size[Y], a.Position[Z] + a.Size[Z]},
{a.Position[X] + a.Size[X], a.Position[Y], a.Position[Z]},
{a.Position[X] + a.Size[X], a.Position[Y], a.Position[Z] + a.Size[Z]},
{a.Position[X] + a.Size[X], a.Position[Y] + a.Size[Y], a.Position[Z]},
{a.Position[X] + a.Size[X], a.Position[Y] + a.Size[Y], a.Position[Z] + a.Size[Z]},
}
var (
over = false
under = false
)
for i := range points {
if plane.DistanceTo(points[i]) > 0 {
over = true
} else {
under = true
}
}
return under && over
}
// IntersectsRay returns the point of intersection of the given ray with this [AABB] along with the normal
// or false if there is no intersection. Ray length is infinite.
func (a AABB) IntersectsRay(from, dir Vector3) (clip Vector3, normal Vector3, ok bool) { //AABB.intersects_ray
var (
c1, c2 Vector3
end = a.Position.Add(a.Size)
near float = -1e20
far float = 1e20
axis Axis = 0
)
for i := Axis(0); i < 3; i++ {
if dir[i] == 0 {
if (from[i] < a.Position[i]) || (from[i] > end[i]) {
return Vector3{}, Vector3{}, false
}
} else { // ray not parallel to planes in this direction
c1[i] = (a.Position[i] - from[i]) / dir[i]
c2[i] = (end[i] - from[i]) / dir[i]
if c1[i] > c2[i] {
c1, c2 = c2, c1
}
if c1[i] > near {
near = c1[i]
axis = i
}
if c2[i] < far {
far = c2[i]
}
if (near > far) || (far < 0) {
return Vector3{}, Vector3{}, false
}
}
}
normal[axis] = ʕ[float](dir[axis] != 0, -1, 1)
return c1, normal, true
}
// IntesectsSegment returns the point of intersection between from and to along with this AABB along with the normal
// or false if there is no intersection.
func (a AABB) IntesectsSegment(from, to Vector3) (clip Vector3, normal Vector3, ok bool) { //AABB.intersects_segment
var (
min float = 0
max float = 1
axis Axis = 0
sign float = 0
)
for i := Axis(0); i < 3; i++ {
var (
seg_from = from[i]
seg_to = to[i]
box_begin = a.Position[i]
box_end = box_begin + a.Size[i]
cmin, cmax float
csign float
)
if seg_from < seg_to {
if seg_from > box_end || seg_to < box_begin {
return Vector3{}, Vector3{}, false
}
var length = seg_to - seg_from
cmin = ʕ(seg_from < box_begin, ((box_begin - seg_from) / length), 0)
cmax = ʕ(seg_to > box_end, ((box_end - seg_from) / length), 1)
csign = -1.0
} else {
if seg_to > box_end || seg_from < box_begin {
return Vector3{}, Vector3{}, false
}
var length = seg_to - seg_from
cmin = ʕ(seg_from > box_end, (box_end-seg_from)/length, 0)
cmax = ʕ(seg_to < box_begin, (box_begin-seg_from)/length, 1)
csign = 1.0
}
if cmin > min {
min = cmin
axis = i
sign = csign
}
if cmax < max {
max = cmax
}
if max < min {
return Vector3{}, Vector3{}, false
}
}
var (
rel = to.Sub(from)
)
normal[axis] = sign
return from.Add(rel.Mulf(float64(min))), normal, true
}
// IsAproxximatelyEqual returns true if this [AABB] and other are approximately equal, by running
// [IsApproximatelyEqual] on each component.
func (a AABB) IsAproxximatelyEqual(other AABB) bool { //AABB.is_equal_approx
return a.Position.IsApproximatelyEqual(other.Position) && a.Size.IsApproximatelyEqual(other.Size)
}
// IsFinite returns true if this [AABB] is finite, by calling [IsFinite] on each component.
func (a AABB) IsFinite() bool { return a.Position.IsFinite() && a.Size.IsFinite() } //AABB.is_finite
// Merge returns the smallest AABB enclosing both this AABB and b.
func (a AABB) Merge(b AABB) AABB { //AABB.merge
var beg_1, beg_2 Vector3
var end_1, end_2 Vector3
var min, max Vector3
beg_1 = a.Position
beg_2 = b.Position
end_1 = a.Size.Add(beg_1)
end_2 = b.Size.Add(beg_2)
min[X] = ʕ(beg_1[X] < beg_2[X], beg_1[X], beg_2[X])
min[Y] = ʕ(beg_1[Y] < beg_2[Y], beg_1[Y], beg_2[Y])
min[Z] = ʕ(beg_1[Z] < beg_2[Z], beg_1[Z], beg_2[Z])
max[X] = ʕ(end_1[X] > end_2[X], end_1[X], end_2[X])
max[Y] = ʕ(end_1[Y] > end_2[Y], end_1[Y], end_2[Y])
max[Z] = ʕ(end_1[Z] > end_2[Z], end_1[Z], end_2[Z])
return AABB{
Position: min,
Size: max.Sub(min),
}
}
// Projection creates a new Projection that scales a given projection to fit around a given
// [AABB] in projection space.
func (a AABB) Projection() Projection { //Projection.create_fit_aabb
min := a.Position
max := a.Position.Add(a.Size)
return Projection{
Vector4{2 / (max[X] - min[X]), 0, 0, -(max[X] + min[X]) / (max[X] - min[X])},
Vector4{0, 2 / (max[Y] - min[Y]), 0, -(max[Y] + min[Y]) / (max[Y] - min[Y])},
Vector4{0, 0, 2 / (max[Z] - min[Z]), -(max[Z] + min[Z]) / (max[Z] - min[Z])},
Vector4{0, 0, 0, 1},
}
}
// Transform (multiplies) the AABB by the given Transform3D matrix.
func (a AABB) Transform(t Transform3D) AABB { //Transform3D * AABB
/* https://dev.theomader.com/transform-bounding-boxes/ */
var min = a.Position
var max = a.Position.Add(a.Size)
var tmin, tmax Vector3
for i := 0; i < 3; i++ {
tmin[i] = t.Origin[i]
tmax[i] = t.Origin[i]
for j := 0; j < 3; j++ {
var e = t.Basis[i][j] * min[j]
var f = t.Basis[i][j] * max[j]
if e < f {
tmin[i] += e
tmax[i] += f
} else {
tmin[i] += f
tmax[i] += e
}
}
}
return AABB{
Position: tmin,
Size: tmax.Sub(tmin),
}
}