5#include <glm/matrix.hpp>
25 explicit AABB(
const glm::vec3& extents):
26 min(-0.5f*glm::abs(extents)),
27 max(0.5f*glm::abs(extents))
36 AABB(
const glm::vec3& mini,
const glm::vec3& maxi):
57 void setBounds(
const glm::vec3& mini,
const glm::vec3& maxi)
noexcept
70 if (point.x <
min.x)
min.x = point.x;
71 if (point.y <
min.y)
min.y = point.y;
72 if (point.z <
min.z)
min.z = point.z;
74 if (point.x >
max.x)
max.x = point.x;
75 if (point.y >
max.y)
max.y = point.y;
76 if (point.z >
max.z)
max.z = point.z;
90 glm::vec3 demi_extents =
max - center;
92 glm::vec3 transformed_center = glm::vec3(model * glm::vec4(center,1.0));
94 glm::mat3 abs_mat = glm::mat3(glm::abs(glm::vec3(model[0])), glm::abs(glm::vec3(model[1])), glm::abs(glm::vec3(model[2])));
95 glm::vec3 transformed_extents = abs_mat * demi_extents;
97 glm::vec3
min = transformed_center - transformed_extents;
98 glm::vec3
max = transformed_center + transformed_extents;
134 glm::vec3 demi_extents =
max - center;
136 min = center - demi_extents *
scale;
137 max = center + demi_extents *
scale;
158 return std::abs(size.x) * std::abs(size.y) * std::abs(size.z);
180 return (
min.x <= box.max.x &&
min.x >= box.min.x) &&
181 (
min.y <= box.max.y &&
min.y >= box.min.y) &&
182 (
min.z <= box.max.z &&
min.z >= box.min.z)
194 bool hasCollision(
const glm::vec3& center,
const float& radius)
const noexcept
197 float x = glm::max(
min.x, glm::min(center.x,
max.x));
198 float y = glm::max(
min.y, glm::min(center.y,
max.y));
199 float z = glm::max(
min.z, glm::min(center.z,
max.z));
201 float distance = (x - center.x) * (x - center.x)
202 + (y - center.y) * (y - center.y)
203 + (z - center.z) * (z - center.z);
205 return distance < radius * radius;
215 bool contains(
const glm::vec3& point)
const noexcept
217 return (point.x <=
max.x && point.x >=
min.x) &&
218 (point.y <=
max.y && point.y >=
min.y) &&
219 (point.z <=
max.z && point.z >=
min.z)
238 glm::vec3
min = glm::vec3(0.0);
243 glm::vec3
max = glm::vec3(0.0);
Axis-aligned bounding box.
Definition aabb.h:13
AABB()=default
Create an empty box centered at origin.
bool contains(const glm::vec3 &point) const noexcept
Check if a point is in this box.
Definition aabb.h:215
bool hasCollision(const glm::vec3 ¢er, const float &radius) const noexcept
Check a sphere collision with this box.
Definition aabb.h:194
glm::vec3 getExtents() const noexcept
Get the box distance between min and max.
Definition aabb.h:145
AABB(const glm::vec3 &extents)
Create a box centered at origin.
Definition aabb.h:25
void scale(const glm::vec3 &scale) noexcept
Scale the bounding box.
Definition aabb.h:131
bool isEmpty() const noexcept
Check if the box is empty.
Definition aabb.h:166
glm::vec3 max
Maximum point of bounds.
Definition aabb.h:243
AABB(const glm::vec3 &mini, const glm::vec3 &maxi)
Create a box by providing bounds.
Definition aabb.h:36
void extendToAABB(const oe::scene::AABB &aabb) noexcept
Increase box size to contain another AABB.
Definition aabb.h:108
void extendToPoint(const glm::vec3 &point) noexcept
Update box size to contain the point.
Definition aabb.h:68
bool contains(const AABB &box) const noexcept
Check if another AABB is completely inside this box.
Definition aabb.h:230
glm::vec3 getCenter() const noexcept
Get coordinates of the box center.
Definition aabb.h:46
glm::vec3 min
Minimum point of bounds.
Definition aabb.h:238
bool hasCollision(const AABB &box) const noexcept
Check an box collision with this box.
Definition aabb.h:178
float getVolume() const noexcept
Get the box volume.
Definition aabb.h:155
AABB transform(const glm::mat4 &model) const noexcept
Returns a new AABB transformed according to a model matrix.
Definition aabb.h:87
void setBounds(const glm::vec3 &mini, const glm::vec3 &maxi) noexcept
Update box bounds.
Definition aabb.h:57
AABB & operator+=(const AABB &aabb) noexcept
Increase box size to contain another AABB.
Definition aabb.h:119
Scene related management (Render-agnostic Geometry, Manger, etc...)
Definition debug.h:19
AABB operator+(const AABB &op1, const AABB &op2) noexcept
return an AABB containing both AABB
Definition aabb.h:252