Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/math/aabb.h
20888 views
1
/**************************************************************************/
2
/* aabb.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/math/plane.h"
34
#include "core/math/vector3.h"
35
#include "core/templates/hashfuncs.h"
36
37
/**
38
* AABB (Axis Aligned Bounding Box)
39
* This is implemented by a point (position) and the box size.
40
*/
41
42
class Variant;
43
44
struct [[nodiscard]] AABB {
45
Vector3 position;
46
Vector3 size;
47
48
real_t get_volume() const;
49
_FORCE_INLINE_ bool has_volume() const {
50
return size.x > 0.0f && size.y > 0.0f && size.z > 0.0f;
51
}
52
53
_FORCE_INLINE_ bool has_surface() const {
54
return size.x > 0.0f || size.y > 0.0f || size.z > 0.0f;
55
}
56
57
const Vector3 &get_position() const { return position; }
58
void set_position(const Vector3 &p_pos) { position = p_pos; }
59
const Vector3 &get_size() const { return size; }
60
void set_size(const Vector3 &p_size) { size = p_size; }
61
62
constexpr bool operator==(const AABB &p_rval) const {
63
return position == p_rval.position && size == p_rval.size;
64
}
65
constexpr bool operator!=(const AABB &p_rval) const {
66
return position != p_rval.position || size != p_rval.size;
67
}
68
69
bool is_equal_approx(const AABB &p_aabb) const;
70
bool is_same(const AABB &p_aabb) const;
71
bool is_finite() const;
72
_FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap
73
_FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap
74
_FORCE_INLINE_ bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this
75
76
AABB merge(const AABB &p_with) const;
77
void merge_with(const AABB &p_aabb); ///merge with another AABB
78
AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs
79
_FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t p_t0, real_t p_t1) const;
80
81
bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_intersection_point = nullptr, Vector3 *r_normal = nullptr) const;
82
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir) const {
83
bool inside;
84
return find_intersects_ray(p_from, p_dir, inside);
85
}
86
bool find_intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, bool &r_inside, Vector3 *r_intersection_point = nullptr, Vector3 *r_normal = nullptr) const;
87
88
_FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const;
89
_FORCE_INLINE_ bool inside_convex_shape(const Plane *p_planes, int p_plane_count) const;
90
bool intersects_plane(const Plane &p_plane) const;
91
92
_FORCE_INLINE_ bool has_point(const Vector3 &p_point) const;
93
_FORCE_INLINE_ Vector3 get_support(const Vector3 &p_direction) const;
94
95
Vector3 get_longest_axis() const;
96
int get_longest_axis_index() const;
97
_FORCE_INLINE_ real_t get_longest_axis_size() const;
98
99
Vector3 get_shortest_axis() const;
100
int get_shortest_axis_index() const;
101
_FORCE_INLINE_ real_t get_shortest_axis_size() const;
102
103
AABB grow(real_t p_by) const;
104
_FORCE_INLINE_ void grow_by(real_t p_amount);
105
106
void get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const;
107
_FORCE_INLINE_ Vector3 get_endpoint(int p_point) const;
108
109
AABB expand(const Vector3 &p_vector) const;
110
_FORCE_INLINE_ void project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const;
111
_FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necessary */
112
113
_FORCE_INLINE_ AABB abs() const {
114
return AABB(position + size.minf(0), size.abs());
115
}
116
117
Variant intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const;
118
Variant intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const;
119
120
_FORCE_INLINE_ void quantize(real_t p_unit);
121
_FORCE_INLINE_ AABB quantized(real_t p_unit) const;
122
123
_FORCE_INLINE_ void set_end(const Vector3 &p_end) {
124
size = p_end - position;
125
}
126
127
_FORCE_INLINE_ Vector3 get_end() const {
128
return position + size;
129
}
130
131
_FORCE_INLINE_ Vector3 get_center() const {
132
return position + (size * 0.5f);
133
}
134
135
uint32_t hash() const {
136
uint32_t h = hash_murmur3_one_real(position.x);
137
h = hash_murmur3_one_real(position.y, h);
138
h = hash_murmur3_one_real(position.z, h);
139
h = hash_murmur3_one_real(size.x, h);
140
h = hash_murmur3_one_real(size.y, h);
141
h = hash_murmur3_one_real(size.z, h);
142
return hash_fmix32(h);
143
}
144
145
explicit operator String() const;
146
147
AABB() = default;
148
constexpr AABB(const Vector3 &p_pos, const Vector3 &p_size) :
149
position(p_pos),
150
size(p_size) {
151
}
152
};
153
154
inline bool AABB::intersects(const AABB &p_aabb) const {
155
#ifdef MATH_CHECKS
156
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) {
157
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
158
}
159
#endif
160
if (position.x >= (p_aabb.position.x + p_aabb.size.x)) {
161
return false;
162
}
163
if ((position.x + size.x) <= p_aabb.position.x) {
164
return false;
165
}
166
if (position.y >= (p_aabb.position.y + p_aabb.size.y)) {
167
return false;
168
}
169
if ((position.y + size.y) <= p_aabb.position.y) {
170
return false;
171
}
172
if (position.z >= (p_aabb.position.z + p_aabb.size.z)) {
173
return false;
174
}
175
if ((position.z + size.z) <= p_aabb.position.z) {
176
return false;
177
}
178
179
return true;
180
}
181
182
inline bool AABB::intersects_inclusive(const AABB &p_aabb) const {
183
#ifdef MATH_CHECKS
184
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) {
185
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
186
}
187
#endif
188
if (position.x > (p_aabb.position.x + p_aabb.size.x)) {
189
return false;
190
}
191
if ((position.x + size.x) < p_aabb.position.x) {
192
return false;
193
}
194
if (position.y > (p_aabb.position.y + p_aabb.size.y)) {
195
return false;
196
}
197
if ((position.y + size.y) < p_aabb.position.y) {
198
return false;
199
}
200
if (position.z > (p_aabb.position.z + p_aabb.size.z)) {
201
return false;
202
}
203
if ((position.z + size.z) < p_aabb.position.z) {
204
return false;
205
}
206
207
return true;
208
}
209
210
inline bool AABB::encloses(const AABB &p_aabb) const {
211
#ifdef MATH_CHECKS
212
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) {
213
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
214
}
215
#endif
216
Vector3 src_min = position;
217
Vector3 src_max = position + size;
218
Vector3 dst_min = p_aabb.position;
219
Vector3 dst_max = p_aabb.position + p_aabb.size;
220
221
return (
222
(src_min.x <= dst_min.x) &&
223
(src_max.x >= dst_max.x) &&
224
(src_min.y <= dst_min.y) &&
225
(src_max.y >= dst_max.y) &&
226
(src_min.z <= dst_min.z) &&
227
(src_max.z >= dst_max.z));
228
}
229
230
Vector3 AABB::get_support(const Vector3 &p_direction) const {
231
Vector3 support = position;
232
if (p_direction.x > 0.0f) {
233
support.x += size.x;
234
}
235
if (p_direction.y > 0.0f) {
236
support.y += size.y;
237
}
238
if (p_direction.z > 0.0f) {
239
support.z += size.z;
240
}
241
return support;
242
}
243
244
Vector3 AABB::get_endpoint(int p_point) const {
245
switch (p_point) {
246
case 0:
247
return Vector3(position.x, position.y, position.z);
248
case 1:
249
return Vector3(position.x, position.y, position.z + size.z);
250
case 2:
251
return Vector3(position.x, position.y + size.y, position.z);
252
case 3:
253
return Vector3(position.x, position.y + size.y, position.z + size.z);
254
case 4:
255
return Vector3(position.x + size.x, position.y, position.z);
256
case 5:
257
return Vector3(position.x + size.x, position.y, position.z + size.z);
258
case 6:
259
return Vector3(position.x + size.x, position.y + size.y, position.z);
260
case 7:
261
return Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
262
}
263
264
ERR_FAIL_V(Vector3());
265
}
266
267
bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const {
268
Vector3 half_extents = size * 0.5f;
269
Vector3 ofs = position + half_extents;
270
271
for (int i = 0; i < p_plane_count; i++) {
272
const Plane &p = p_planes[i];
273
Vector3 point(
274
(p.normal.x > 0) ? -half_extents.x : half_extents.x,
275
(p.normal.y > 0) ? -half_extents.y : half_extents.y,
276
(p.normal.z > 0) ? -half_extents.z : half_extents.z);
277
point += ofs;
278
if (p.is_point_over(point)) {
279
return false;
280
}
281
}
282
283
// Make sure all points in the shape aren't fully separated from the AABB on
284
// each axis.
285
int bad_point_counts_positive[3] = { 0 };
286
int bad_point_counts_negative[3] = { 0 };
287
288
for (int k = 0; k < 3; k++) {
289
for (int i = 0; i < p_point_count; i++) {
290
if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) {
291
bad_point_counts_positive[k]++;
292
}
293
if (p_points[i].coord[k] < ofs.coord[k] - half_extents.coord[k]) {
294
bad_point_counts_negative[k]++;
295
}
296
}
297
298
if (bad_point_counts_negative[k] == p_point_count) {
299
return false;
300
}
301
if (bad_point_counts_positive[k] == p_point_count) {
302
return false;
303
}
304
}
305
306
return true;
307
}
308
309
bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const {
310
Vector3 half_extents = size * 0.5f;
311
Vector3 ofs = position + half_extents;
312
313
for (int i = 0; i < p_plane_count; i++) {
314
const Plane &p = p_planes[i];
315
Vector3 point(
316
(p.normal.x < 0) ? -half_extents.x : half_extents.x,
317
(p.normal.y < 0) ? -half_extents.y : half_extents.y,
318
(p.normal.z < 0) ? -half_extents.z : half_extents.z);
319
point += ofs;
320
if (p.is_point_over(point)) {
321
return false;
322
}
323
}
324
325
return true;
326
}
327
328
bool AABB::has_point(const Vector3 &p_point) const {
329
#ifdef MATH_CHECKS
330
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
331
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
332
}
333
#endif
334
if (p_point.x < position.x) {
335
return false;
336
}
337
if (p_point.y < position.y) {
338
return false;
339
}
340
if (p_point.z < position.z) {
341
return false;
342
}
343
if (p_point.x > position.x + size.x) {
344
return false;
345
}
346
if (p_point.y > position.y + size.y) {
347
return false;
348
}
349
if (p_point.z > position.z + size.z) {
350
return false;
351
}
352
353
return true;
354
}
355
356
inline void AABB::expand_to(const Vector3 &p_vector) {
357
#ifdef MATH_CHECKS
358
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
359
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
360
}
361
#endif
362
Vector3 begin = position;
363
Vector3 end = position + size;
364
365
if (p_vector.x < begin.x) {
366
begin.x = p_vector.x;
367
}
368
if (p_vector.y < begin.y) {
369
begin.y = p_vector.y;
370
}
371
if (p_vector.z < begin.z) {
372
begin.z = p_vector.z;
373
}
374
375
if (p_vector.x > end.x) {
376
end.x = p_vector.x;
377
}
378
if (p_vector.y > end.y) {
379
end.y = p_vector.y;
380
}
381
if (p_vector.z > end.z) {
382
end.z = p_vector.z;
383
}
384
385
position = begin;
386
size = end - begin;
387
}
388
389
void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const {
390
Vector3 half_extents(size.x * 0.5f, size.y * 0.5f, size.z * 0.5f);
391
Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z);
392
393
real_t length = p_plane.normal.abs().dot(half_extents);
394
real_t distance = p_plane.distance_to(center);
395
r_min = distance - length;
396
r_max = distance + length;
397
}
398
399
inline real_t AABB::get_longest_axis_size() const {
400
real_t max_size = size.x;
401
402
if (size.y > max_size) {
403
max_size = size.y;
404
}
405
406
if (size.z > max_size) {
407
max_size = size.z;
408
}
409
410
return max_size;
411
}
412
413
inline real_t AABB::get_shortest_axis_size() const {
414
real_t max_size = size.x;
415
416
if (size.y < max_size) {
417
max_size = size.y;
418
}
419
420
if (size.z < max_size) {
421
max_size = size.z;
422
}
423
424
return max_size;
425
}
426
427
bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t p_t0, real_t p_t1) const {
428
#ifdef MATH_CHECKS
429
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
430
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
431
}
432
#endif
433
real_t divx = 1.0f / p_dir.x;
434
real_t divy = 1.0f / p_dir.y;
435
real_t divz = 1.0f / p_dir.z;
436
437
Vector3 upbound = position + size;
438
real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
439
if (p_dir.x >= 0) {
440
tmin = (position.x - p_from.x) * divx;
441
tmax = (upbound.x - p_from.x) * divx;
442
} else {
443
tmin = (upbound.x - p_from.x) * divx;
444
tmax = (position.x - p_from.x) * divx;
445
}
446
if (p_dir.y >= 0) {
447
tymin = (position.y - p_from.y) * divy;
448
tymax = (upbound.y - p_from.y) * divy;
449
} else {
450
tymin = (upbound.y - p_from.y) * divy;
451
tymax = (position.y - p_from.y) * divy;
452
}
453
if ((tmin > tymax) || (tymin > tmax)) {
454
return false;
455
}
456
if (tymin > tmin) {
457
tmin = tymin;
458
}
459
if (tymax < tmax) {
460
tmax = tymax;
461
}
462
if (p_dir.z >= 0) {
463
tzmin = (position.z - p_from.z) * divz;
464
tzmax = (upbound.z - p_from.z) * divz;
465
} else {
466
tzmin = (upbound.z - p_from.z) * divz;
467
tzmax = (position.z - p_from.z) * divz;
468
}
469
if ((tmin > tzmax) || (tzmin > tmax)) {
470
return false;
471
}
472
if (tzmin > tmin) {
473
tmin = tzmin;
474
}
475
if (tzmax < tmax) {
476
tmax = tzmax;
477
}
478
return ((tmin < p_t1) && (tmax > p_t0));
479
}
480
481
void AABB::grow_by(real_t p_amount) {
482
position.x -= p_amount;
483
position.y -= p_amount;
484
position.z -= p_amount;
485
size.x += 2.0f * p_amount;
486
size.y += 2.0f * p_amount;
487
size.z += 2.0f * p_amount;
488
}
489
490
void AABB::quantize(real_t p_unit) {
491
size += position;
492
493
position.x -= Math::fposmodp(position.x, p_unit);
494
position.y -= Math::fposmodp(position.y, p_unit);
495
position.z -= Math::fposmodp(position.z, p_unit);
496
497
size.x -= Math::fposmodp(size.x, p_unit);
498
size.y -= Math::fposmodp(size.y, p_unit);
499
size.z -= Math::fposmodp(size.z, p_unit);
500
501
size.x += p_unit;
502
size.y += p_unit;
503
size.z += p_unit;
504
505
size -= position;
506
}
507
508
AABB AABB::quantized(real_t p_unit) const {
509
AABB ret = *this;
510
ret.quantize(p_unit);
511
return ret;
512
}
513
514
template <>
515
struct is_zero_constructible<AABB> : std::true_type {};
516
517