summaryrefslogtreecommitdiff
path: root/core/math/aabb.h
diff options
context:
space:
mode:
Diffstat (limited to 'core/math/aabb.h')
-rw-r--r--core/math/aabb.h113
1 files changed, 64 insertions, 49 deletions
diff --git a/core/math/aabb.h b/core/math/aabb.h
index f87fced12d..9bbedfe59c 100644
--- a/core/math/aabb.h
+++ b/core/math/aabb.h
@@ -47,12 +47,10 @@ public:
real_t get_area() const; /// get area
_FORCE_INLINE_ bool has_no_area() const {
-
return (size.x <= 0 || size.y <= 0 || size.z <= 0);
}
_FORCE_INLINE_ bool has_no_surface() const {
-
return (size.x <= 0 && size.y <= 0 && size.z <= 0);
}
@@ -111,43 +109,52 @@ public:
};
inline bool AABB::intersects(const AABB &p_aabb) const {
-
- if (position.x >= (p_aabb.position.x + p_aabb.size.x))
+ if (position.x >= (p_aabb.position.x + p_aabb.size.x)) {
return false;
- if ((position.x + size.x) <= p_aabb.position.x)
+ }
+ if ((position.x + size.x) <= p_aabb.position.x) {
return false;
- if (position.y >= (p_aabb.position.y + p_aabb.size.y))
+ }
+ if (position.y >= (p_aabb.position.y + p_aabb.size.y)) {
return false;
- if ((position.y + size.y) <= p_aabb.position.y)
+ }
+ if ((position.y + size.y) <= p_aabb.position.y) {
return false;
- if (position.z >= (p_aabb.position.z + p_aabb.size.z))
+ }
+ if (position.z >= (p_aabb.position.z + p_aabb.size.z)) {
return false;
- if ((position.z + size.z) <= p_aabb.position.z)
+ }
+ if ((position.z + size.z) <= p_aabb.position.z) {
return false;
+ }
return true;
}
inline bool AABB::intersects_inclusive(const AABB &p_aabb) const {
-
- if (position.x > (p_aabb.position.x + p_aabb.size.x))
+ if (position.x > (p_aabb.position.x + p_aabb.size.x)) {
return false;
- if ((position.x + size.x) < p_aabb.position.x)
+ }
+ if ((position.x + size.x) < p_aabb.position.x) {
return false;
- if (position.y > (p_aabb.position.y + p_aabb.size.y))
+ }
+ if (position.y > (p_aabb.position.y + p_aabb.size.y)) {
return false;
- if ((position.y + size.y) < p_aabb.position.y)
+ }
+ if ((position.y + size.y) < p_aabb.position.y) {
return false;
- if (position.z > (p_aabb.position.z + p_aabb.size.z))
+ }
+ if (position.z > (p_aabb.position.z + p_aabb.size.z)) {
return false;
- if ((position.z + size.z) < p_aabb.position.z)
+ }
+ if ((position.z + size.z) < p_aabb.position.z) {
return false;
+ }
return true;
}
inline bool AABB::encloses(const AABB &p_aabb) const {
-
Vector3 src_min = position;
Vector3 src_max = position + size;
Vector3 dst_min = p_aabb.position;
@@ -163,7 +170,6 @@ inline bool AABB::encloses(const AABB &p_aabb) const {
}
Vector3 AABB::get_support(const Vector3 &p_normal) const {
-
Vector3 half_extents = size * 0.5;
Vector3 ofs = position + half_extents;
@@ -175,7 +181,6 @@ Vector3 AABB::get_support(const Vector3 &p_normal) const {
}
Vector3 AABB::get_endpoint(int p_point) const {
-
switch (p_point) {
case 0:
return Vector3(position.x, position.y, position.z);
@@ -199,7 +204,6 @@ Vector3 AABB::get_endpoint(int p_point) const {
}
bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const {
-
Vector3 half_extents = size * 0.5;
Vector3 ofs = position + half_extents;
@@ -210,8 +214,9 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, con
(p.normal.y > 0) ? -half_extents.y : half_extents.y,
(p.normal.z > 0) ? -half_extents.z : half_extents.z);
point += ofs;
- if (p.is_point_over(point))
+ if (p.is_point_over(point)) {
return false;
+ }
}
// Make sure all points in the shape aren't fully separated from the AABB on
@@ -220,7 +225,6 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, con
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]++;
@@ -242,7 +246,6 @@ bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, con
}
bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const {
-
Vector3 half_extents = size * 0.5;
Vector3 ofs = position + half_extents;
@@ -253,56 +256,66 @@ bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const {
(p.normal.y < 0) ? -half_extents.y : half_extents.y,
(p.normal.z < 0) ? -half_extents.z : half_extents.z);
point += ofs;
- if (p.is_point_over(point))
+ if (p.is_point_over(point)) {
return false;
+ }
}
return true;
}
bool AABB::has_point(const Vector3 &p_point) const {
-
- if (p_point.x < position.x)
+ if (p_point.x < position.x) {
return false;
- if (p_point.y < position.y)
+ }
+ if (p_point.y < position.y) {
return false;
- if (p_point.z < position.z)
+ }
+ if (p_point.z < position.z) {
return false;
- if (p_point.x > position.x + size.x)
+ }
+ if (p_point.x > position.x + size.x) {
return false;
- if (p_point.y > position.y + size.y)
+ }
+ if (p_point.y > position.y + size.y) {
return false;
- if (p_point.z > position.z + size.z)
+ }
+ if (p_point.z > position.z + size.z) {
return false;
+ }
return true;
}
inline void AABB::expand_to(const Vector3 &p_vector) {
-
Vector3 begin = position;
Vector3 end = position + size;
- if (p_vector.x < begin.x)
+ if (p_vector.x < begin.x) {
begin.x = p_vector.x;
- if (p_vector.y < begin.y)
+ }
+ if (p_vector.y < begin.y) {
begin.y = p_vector.y;
- if (p_vector.z < begin.z)
+ }
+ if (p_vector.z < begin.z) {
begin.z = p_vector.z;
+ }
- if (p_vector.x > end.x)
+ if (p_vector.x > end.x) {
end.x = p_vector.x;
- if (p_vector.y > end.y)
+ }
+ if (p_vector.y > end.y) {
end.y = p_vector.y;
- if (p_vector.z > end.z)
+ }
+ if (p_vector.z > end.z) {
end.z = p_vector.z;
+ }
position = begin;
size = end - begin;
}
void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const {
-
Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5);
Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z);
@@ -313,7 +326,6 @@ void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r
}
inline real_t AABB::get_longest_axis_size() const {
-
real_t max_size = size.x;
if (size.y > max_size) {
@@ -328,7 +340,6 @@ inline real_t AABB::get_longest_axis_size() const {
}
inline real_t AABB::get_shortest_axis_size() const {
-
real_t max_size = size.x;
if (size.y < max_size) {
@@ -343,7 +354,6 @@ inline real_t AABB::get_shortest_axis_size() const {
}
bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const {
-
real_t divx = 1.0 / p_dir.x;
real_t divy = 1.0 / p_dir.y;
real_t divz = 1.0 / p_dir.z;
@@ -364,12 +374,15 @@ bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real
tymin = (upbound.y - p_from.y) * divy;
tymax = (position.y - p_from.y) * divy;
}
- if ((tmin > tymax) || (tymin > tmax))
+ if ((tmin > tymax) || (tymin > tmax)) {
return false;
- if (tymin > tmin)
+ }
+ if (tymin > tmin) {
tmin = tymin;
- if (tymax < tmax)
+ }
+ if (tymax < tmax) {
tmax = tymax;
+ }
if (p_dir.z >= 0) {
tzmin = (position.z - p_from.z) * divz;
tzmax = (upbound.z - p_from.z) * divz;
@@ -377,17 +390,19 @@ bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real
tzmin = (upbound.z - p_from.z) * divz;
tzmax = (position.z - p_from.z) * divz;
}
- if ((tmin > tzmax) || (tzmin > tmax))
+ if ((tmin > tzmax) || (tzmin > tmax)) {
return false;
- if (tzmin > tmin)
+ }
+ if (tzmin > tmin) {
tmin = tzmin;
- if (tzmax < tmax)
+ }
+ if (tzmax < tmax) {
tmax = tzmax;
+ }
return ((tmin < t1) && (tmax > t0));
}
void AABB::grow_by(real_t p_amount) {
-
position.x -= p_amount;
position.y -= p_amount;
position.z -= p_amount;