2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* aabb.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifndef AABB_H
|
|
|
|
#define AABB_H
|
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/math/plane.h"
|
|
|
|
#include "core/math/vector3.h"
|
2017-01-14 20:35:39 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
/**
|
2022-02-04 13:30:17 +00:00
|
|
|
* AABB (Axis Aligned Bounding Box)
|
|
|
|
* This is implemented by a point (position) and the box size.
|
2014-02-10 01:10:30 +00:00
|
|
|
*/
|
2022-02-04 13:30:17 +00:00
|
|
|
|
2020-10-13 18:59:37 +00:00
|
|
|
class Variant;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-02-04 13:30:17 +00:00
|
|
|
struct _NO_DISCARD_ AABB {
|
2017-06-06 18:33:51 +00:00
|
|
|
Vector3 position;
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector3 size;
|
|
|
|
|
2021-11-05 01:01:59 +00:00
|
|
|
real_t get_volume() const;
|
2022-08-15 02:48:13 +00:00
|
|
|
_FORCE_INLINE_ bool has_volume() const {
|
|
|
|
return size.x > 0.0f && size.y > 0.0f && size.z > 0.0f;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 02:48:13 +00:00
|
|
|
_FORCE_INLINE_ bool has_surface() const {
|
|
|
|
return size.x > 0.0f || size.y > 0.0f || size.z > 0.0f;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-06 18:33:51 +00:00
|
|
|
const Vector3 &get_position() const { return position; }
|
|
|
|
void set_position(const Vector3 &p_pos) { position = p_pos; }
|
2017-03-05 15:44:50 +00:00
|
|
|
const Vector3 &get_size() const { return size; }
|
|
|
|
void set_size(const Vector3 &p_size) { size = p_size; }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
bool operator==(const AABB &p_rval) const;
|
|
|
|
bool operator!=(const AABB &p_rval) const;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-10-14 20:33:45 +00:00
|
|
|
bool is_equal_approx(const AABB &p_aabb) const;
|
2022-08-11 08:12:27 +00:00
|
|
|
bool is_finite() const;
|
2017-11-17 02:09:00 +00:00
|
|
|
_FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap
|
|
|
|
_FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap
|
|
|
|
_FORCE_INLINE_ bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB merge(const AABB &p_with) const;
|
|
|
|
void merge_with(const AABB &p_aabb); ///merge with another AABB
|
|
|
|
AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs
|
2024-02-17 22:24:59 +00:00
|
|
|
_FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t p_t0, real_t p_t1) const;
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2024-01-03 13:24:40 +00:00
|
|
|
bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_intersection_point = nullptr, Vector3 *r_normal = nullptr) const;
|
|
|
|
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir) const {
|
|
|
|
bool inside;
|
|
|
|
return find_intersects_ray(p_from, p_dir, inside);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2020-04-29 00:14:06 +00:00
|
|
|
_FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const;
|
2018-05-06 18:49:22 +00:00
|
|
|
_FORCE_INLINE_ bool inside_convex_shape(const Plane *p_planes, int p_plane_count) const;
|
2014-02-10 01:10:30 +00:00
|
|
|
bool intersects_plane(const Plane &p_plane) const;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ bool has_point(const Vector3 &p_point) const;
|
|
|
|
_FORCE_INLINE_ Vector3 get_support(const Vector3 &p_normal) const;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Vector3 get_longest_axis() const;
|
|
|
|
int get_longest_axis_index() const;
|
|
|
|
_FORCE_INLINE_ real_t get_longest_axis_size() const;
|
|
|
|
|
|
|
|
Vector3 get_shortest_axis() const;
|
|
|
|
int get_shortest_axis_index() const;
|
|
|
|
_FORCE_INLINE_ real_t get_shortest_axis_size() const;
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB grow(real_t p_by) const;
|
2014-06-11 13:41:03 +00:00
|
|
|
_FORCE_INLINE_ void grow_by(real_t p_amount);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const;
|
2014-02-10 01:10:30 +00:00
|
|
|
_FORCE_INLINE_ Vector3 get_endpoint(int p_point) const;
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB expand(const Vector3 &p_vector) const;
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const;
|
2017-03-24 20:45:31 +00:00
|
|
|
_FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necessary */
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-07-21 18:07:00 +00:00
|
|
|
_FORCE_INLINE_ AABB abs() const {
|
2024-03-03 13:37:52 +00:00
|
|
|
return AABB(position + size.minf(0), size.abs());
|
2020-07-21 18:07:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:59:37 +00:00
|
|
|
Variant intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const;
|
|
|
|
Variant intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const;
|
|
|
|
|
2021-01-25 19:46:35 +00:00
|
|
|
_FORCE_INLINE_ void quantize(real_t p_unit);
|
|
|
|
_FORCE_INLINE_ AABB quantized(real_t p_unit) const;
|
2020-12-24 15:18:28 +00:00
|
|
|
|
2020-11-07 01:29:22 +00:00
|
|
|
_FORCE_INLINE_ void set_end(const Vector3 &p_end) {
|
|
|
|
size = p_end - position;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector3 get_end() const {
|
|
|
|
return position + size;
|
|
|
|
}
|
|
|
|
|
2021-09-20 18:48:52 +00:00
|
|
|
_FORCE_INLINE_ Vector3 get_center() const {
|
2022-02-06 11:14:58 +00:00
|
|
|
return position + (size * 0.5f);
|
2021-09-20 18:48:52 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
operator String() const;
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
_FORCE_INLINE_ AABB() {}
|
2017-12-06 20:36:34 +00:00
|
|
|
inline AABB(const Vector3 &p_pos, const Vector3 &p_size) :
|
|
|
|
position(p_pos),
|
|
|
|
size(p_size) {
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
|
|
|
};
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
inline bool AABB::intersects(const AABB &p_aabb) const {
|
2020-04-06 08:34:18 +00:00
|
|
|
#ifdef MATH_CHECKS
|
|
|
|
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)) {
|
|
|
|
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
|
|
|
|
}
|
|
|
|
#endif
|
2020-05-14 14:41:43 +00:00
|
|
|
if (position.x >= (p_aabb.position.x + p_aabb.size.x)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if ((position.x + size.x) <= p_aabb.position.x) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (position.y >= (p_aabb.position.y + p_aabb.size.y)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if ((position.y + size.y) <= p_aabb.position.y) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (position.z >= (p_aabb.position.z + p_aabb.size.z)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if ((position.z + size.z) <= p_aabb.position.z) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
inline bool AABB::intersects_inclusive(const AABB &p_aabb) const {
|
2020-04-06 08:34:18 +00:00
|
|
|
#ifdef MATH_CHECKS
|
|
|
|
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)) {
|
|
|
|
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
|
|
|
|
}
|
|
|
|
#endif
|
2020-05-14 14:41:43 +00:00
|
|
|
if (position.x > (p_aabb.position.x + p_aabb.size.x)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if ((position.x + size.x) < p_aabb.position.x) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (position.y > (p_aabb.position.y + p_aabb.size.y)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if ((position.y + size.y) < p_aabb.position.y) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (position.z > (p_aabb.position.z + p_aabb.size.z)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if ((position.z + size.z) < p_aabb.position.z) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
|
|
|
return true;
|
2016-02-17 19:06:40 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
inline bool AABB::encloses(const AABB &p_aabb) const {
|
2020-04-06 08:34:18 +00:00
|
|
|
#ifdef MATH_CHECKS
|
|
|
|
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)) {
|
|
|
|
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-06 18:33:51 +00:00
|
|
|
Vector3 src_min = position;
|
|
|
|
Vector3 src_max = position + size;
|
|
|
|
Vector3 dst_min = p_aabb.position;
|
|
|
|
Vector3 dst_max = p_aabb.position + p_aabb.size;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return (
|
|
|
|
(src_min.x <= dst_min.x) &&
|
2024-01-12 16:58:42 +00:00
|
|
|
(src_max.x >= dst_max.x) &&
|
2014-02-10 01:10:30 +00:00
|
|
|
(src_min.y <= dst_min.y) &&
|
2024-01-12 16:58:42 +00:00
|
|
|
(src_max.y >= dst_max.y) &&
|
2014-02-10 01:10:30 +00:00
|
|
|
(src_min.z <= dst_min.z) &&
|
2024-01-12 16:58:42 +00:00
|
|
|
(src_max.z >= dst_max.z));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
Vector3 AABB::get_support(const Vector3 &p_normal) const {
|
2022-02-06 11:14:58 +00:00
|
|
|
Vector3 half_extents = size * 0.5f;
|
2017-06-06 18:33:51 +00:00
|
|
|
Vector3 ofs = position + half_extents;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return Vector3(
|
2020-12-17 18:56:59 +00:00
|
|
|
(p_normal.x > 0) ? half_extents.x : -half_extents.x,
|
|
|
|
(p_normal.y > 0) ? half_extents.y : -half_extents.y,
|
|
|
|
(p_normal.z > 0) ? half_extents.z : -half_extents.z) +
|
2021-10-28 13:19:35 +00:00
|
|
|
ofs;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
Vector3 AABB::get_endpoint(int p_point) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_point) {
|
2020-05-10 11:00:47 +00:00
|
|
|
case 0:
|
|
|
|
return Vector3(position.x, position.y, position.z);
|
|
|
|
case 1:
|
|
|
|
return Vector3(position.x, position.y, position.z + size.z);
|
|
|
|
case 2:
|
|
|
|
return Vector3(position.x, position.y + size.y, position.z);
|
|
|
|
case 3:
|
|
|
|
return Vector3(position.x, position.y + size.y, position.z + size.z);
|
|
|
|
case 4:
|
|
|
|
return Vector3(position.x + size.x, position.y, position.z);
|
|
|
|
case 5:
|
|
|
|
return Vector3(position.x + size.x, position.y, position.z + size.z);
|
|
|
|
case 6:
|
|
|
|
return Vector3(position.x + size.x, position.y + size.y, position.z);
|
|
|
|
case 7:
|
|
|
|
return Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
|
2020-05-19 13:46:49 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_V(Vector3());
|
|
|
|
}
|
|
|
|
|
2020-04-29 00:14:06 +00:00
|
|
|
bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const {
|
2022-02-06 11:14:58 +00:00
|
|
|
Vector3 half_extents = size * 0.5f;
|
2017-06-06 18:33:51 +00:00
|
|
|
Vector3 ofs = position + half_extents;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < p_plane_count; i++) {
|
|
|
|
const Plane &p = p_planes[i];
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector3 point(
|
2017-03-05 15:44:50 +00:00
|
|
|
(p.normal.x > 0) ? -half_extents.x : half_extents.x,
|
|
|
|
(p.normal.y > 0) ? -half_extents.y : half_extents.y,
|
|
|
|
(p.normal.z > 0) ? -half_extents.z : half_extents.z);
|
|
|
|
point += ofs;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p.is_point_over(point)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-06 18:49:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 00:14:06 +00:00
|
|
|
// Make sure all points in the shape aren't fully separated from the AABB on
|
|
|
|
// each axis.
|
|
|
|
int bad_point_counts_positive[3] = { 0 };
|
|
|
|
int bad_point_counts_negative[3] = { 0 };
|
|
|
|
|
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
for (int i = 0; i < p_point_count; i++) {
|
|
|
|
if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) {
|
|
|
|
bad_point_counts_positive[k]++;
|
|
|
|
}
|
|
|
|
if (p_points[i].coord[k] < ofs.coord[k] - half_extents.coord[k]) {
|
|
|
|
bad_point_counts_negative[k]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bad_point_counts_negative[k] == p_point_count) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (bad_point_counts_positive[k] == p_point_count) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 18:49:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const {
|
2022-02-06 11:14:58 +00:00
|
|
|
Vector3 half_extents = size * 0.5f;
|
2018-05-06 18:49:22 +00:00
|
|
|
Vector3 ofs = position + half_extents;
|
|
|
|
|
|
|
|
for (int i = 0; i < p_plane_count; i++) {
|
|
|
|
const Plane &p = p_planes[i];
|
|
|
|
Vector3 point(
|
|
|
|
(p.normal.x < 0) ? -half_extents.x : half_extents.x,
|
|
|
|
(p.normal.y < 0) ? -half_extents.y : half_extents.y,
|
|
|
|
(p.normal.z < 0) ? -half_extents.z : half_extents.z);
|
|
|
|
point += ofs;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p.is_point_over(point)) {
|
2018-05-06 18:49:22 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
bool AABB::has_point(const Vector3 &p_point) const {
|
2020-04-06 08:34:18 +00:00
|
|
|
#ifdef MATH_CHECKS
|
|
|
|
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
|
|
|
|
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
|
|
|
|
}
|
|
|
|
#endif
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_point.x < position.x) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (p_point.y < position.y) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (p_point.z < position.z) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (p_point.x > position.x + size.x) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (p_point.y > position.y + size.y) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (p_point.z > position.z + size.z) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
inline void AABB::expand_to(const Vector3 &p_vector) {
|
2020-04-06 08:34:18 +00:00
|
|
|
#ifdef MATH_CHECKS
|
|
|
|
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
|
|
|
|
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-06 18:33:51 +00:00
|
|
|
Vector3 begin = position;
|
|
|
|
Vector3 end = position + size;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_vector.x < begin.x) {
|
2017-03-05 15:44:50 +00:00
|
|
|
begin.x = p_vector.x;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (p_vector.y < begin.y) {
|
2017-03-05 15:44:50 +00:00
|
|
|
begin.y = p_vector.y;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (p_vector.z < begin.z) {
|
2017-03-05 15:44:50 +00:00
|
|
|
begin.z = p_vector.z;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_vector.x > end.x) {
|
2017-03-05 15:44:50 +00:00
|
|
|
end.x = p_vector.x;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (p_vector.y > end.y) {
|
2017-03-05 15:44:50 +00:00
|
|
|
end.y = p_vector.y;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (p_vector.z > end.z) {
|
2017-03-05 15:44:50 +00:00
|
|
|
end.z = p_vector.z;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-06 18:33:51 +00:00
|
|
|
position = begin;
|
2017-03-05 15:44:50 +00:00
|
|
|
size = end - begin;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const {
|
2022-02-06 11:14:58 +00:00
|
|
|
Vector3 half_extents(size.x * 0.5f, size.y * 0.5f, size.z * 0.5f);
|
2017-06-06 18:33:51 +00:00
|
|
|
Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-01-14 20:35:39 +00:00
|
|
|
real_t length = p_plane.normal.abs().dot(half_extents);
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t distance = p_plane.distance_to(center);
|
2014-02-10 01:10:30 +00:00
|
|
|
r_min = distance - length;
|
2016-03-08 23:00:52 +00:00
|
|
|
r_max = distance + length;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
inline real_t AABB::get_longest_axis_size() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t max_size = size.x;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (size.y > max_size) {
|
|
|
|
max_size = size.y;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (size.z > max_size) {
|
|
|
|
max_size = size.z;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return max_size;
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
inline real_t AABB::get_shortest_axis_size() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
real_t max_size = size.x;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (size.y < max_size) {
|
|
|
|
max_size = size.y;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (size.z < max_size) {
|
|
|
|
max_size = size.z;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return max_size;
|
|
|
|
}
|
|
|
|
|
2024-02-17 22:24:59 +00:00
|
|
|
bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t p_t0, real_t p_t1) const {
|
2020-04-06 08:34:18 +00:00
|
|
|
#ifdef MATH_CHECKS
|
|
|
|
if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
|
|
|
|
ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
|
|
|
|
}
|
|
|
|
#endif
|
2022-02-06 11:14:58 +00:00
|
|
|
real_t divx = 1.0f / p_dir.x;
|
|
|
|
real_t divy = 1.0f / p_dir.y;
|
|
|
|
real_t divz = 1.0f / p_dir.z;
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-06-06 18:33:51 +00:00
|
|
|
Vector3 upbound = position + size;
|
2017-01-14 20:35:39 +00:00
|
|
|
real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
|
2017-08-11 19:10:05 +00:00
|
|
|
if (p_dir.x >= 0) {
|
|
|
|
tmin = (position.x - p_from.x) * divx;
|
|
|
|
tmax = (upbound.x - p_from.x) * divx;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2017-08-11 19:10:05 +00:00
|
|
|
tmin = (upbound.x - p_from.x) * divx;
|
|
|
|
tmax = (position.x - p_from.x) * divx;
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
2017-08-11 19:10:05 +00:00
|
|
|
if (p_dir.y >= 0) {
|
|
|
|
tymin = (position.y - p_from.y) * divy;
|
|
|
|
tymax = (upbound.y - p_from.y) * divy;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2017-08-11 19:10:05 +00:00
|
|
|
tymin = (upbound.y - p_from.y) * divy;
|
|
|
|
tymax = (position.y - p_from.y) * divy;
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
if ((tmin > tymax) || (tymin > tmax)) {
|
2014-06-11 13:41:03 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (tymin > tmin) {
|
2017-03-05 15:44:50 +00:00
|
|
|
tmin = tymin;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (tymax < tmax) {
|
2014-06-11 13:41:03 +00:00
|
|
|
tmax = tymax;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-08-11 19:10:05 +00:00
|
|
|
if (p_dir.z >= 0) {
|
|
|
|
tzmin = (position.z - p_from.z) * divz;
|
|
|
|
tzmax = (upbound.z - p_from.z) * divz;
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2017-08-11 19:10:05 +00:00
|
|
|
tzmin = (upbound.z - p_from.z) * divz;
|
|
|
|
tzmax = (position.z - p_from.z) * divz;
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
if ((tmin > tzmax) || (tzmin > tmax)) {
|
2014-06-11 13:41:03 +00:00
|
|
|
return false;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (tzmin > tmin) {
|
2014-06-11 13:41:03 +00:00
|
|
|
tmin = tzmin;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (tzmax < tmax) {
|
2014-06-11 13:41:03 +00:00
|
|
|
tmax = tzmax;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2024-02-17 22:24:59 +00:00
|
|
|
return ((tmin < p_t1) && (tmax > p_t0));
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
void AABB::grow_by(real_t p_amount) {
|
2017-06-06 18:33:51 +00:00
|
|
|
position.x -= p_amount;
|
|
|
|
position.y -= p_amount;
|
|
|
|
position.z -= p_amount;
|
2022-02-06 11:14:58 +00:00
|
|
|
size.x += 2.0f * p_amount;
|
|
|
|
size.y += 2.0f * p_amount;
|
|
|
|
size.z += 2.0f * p_amount;
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 19:46:35 +00:00
|
|
|
void AABB::quantize(real_t p_unit) {
|
2020-12-24 15:18:28 +00:00
|
|
|
size += position;
|
|
|
|
|
|
|
|
position.x -= Math::fposmodp(position.x, p_unit);
|
|
|
|
position.y -= Math::fposmodp(position.y, p_unit);
|
|
|
|
position.z -= Math::fposmodp(position.z, p_unit);
|
|
|
|
|
|
|
|
size.x -= Math::fposmodp(size.x, p_unit);
|
|
|
|
size.y -= Math::fposmodp(size.y, p_unit);
|
|
|
|
size.z -= Math::fposmodp(size.z, p_unit);
|
|
|
|
|
|
|
|
size.x += p_unit;
|
|
|
|
size.y += p_unit;
|
|
|
|
size.z += p_unit;
|
|
|
|
|
|
|
|
size -= position;
|
|
|
|
}
|
|
|
|
|
2021-01-25 19:46:35 +00:00
|
|
|
AABB AABB::quantized(real_t p_unit) const {
|
2020-12-24 15:18:28 +00:00
|
|
|
AABB ret = *this;
|
|
|
|
ret.quantize(p_unit);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif // AABB_H
|