12 AABB_CORNER_NEG_X_NEG_Y_NEG_Z = 0,
13 AABB_CORNER_POS_X_NEG_Y_NEG_Z = 1,
14 AABB_CORNER_POS_X_NEG_Y_POS_Z = 2,
15 AABB_CORNER_NEG_X_NEG_Y_POS_Z = 3,
16 AABB_CORNER_NEG_X_POS_Y_NEG_Z = 4,
17 AABB_CORNER_POS_X_POS_Y_NEG_Z = 5,
18 AABB_CORNER_POS_X_POS_Y_POS_Z = 6,
19 AABB_CORNER_NEG_X_POS_Y_POS_Z = 7
27 void set_min_max(
const Vec3& min,
const Vec3& max) {
28 extents_ = (max - min) * 0.5f;
29 center_ = min + extents_;
33 return center_ - extents_;
37 return center_ + extents_;
43 center_(center), extents_(extents) {}
45 AABB(
const Vec3& centre,
float width);
46 AABB(
const Vec3& centre,
float xsize,
float ysize,
float zsize);
47 AABB(
const Vec3* vertices,
const std::size_t count);
51 return extents_.x * 2.0f;
54 float height()
const {
55 return extents_.y * 2.0f;
59 return extents_.z * 2.0f;
62 const Vec3 half_dimensions()
const {
66 const Vec3 dimensions()
const {
67 return Vec3(width(), height(), depth());
70 float max_dimension()
const {
71 return fast_max(width(), fast_max(height(), depth()));
74 float min_dimension()
const {
75 return fast_min(width(), fast_min(height(), depth()));
78 bool intersects_aabb(
const AABB& other)
const;
79 bool intersects_sphere(
const smlt::Vec3& center,
float radius)
const;
85 bool has_zero_area()
const {
89 bool empty_x = width() == 0.0f;
90 bool empty_y = height() == 0.0f;
91 bool empty_z = depth() == 0.0f;
93 return (empty_x && empty_y) || (empty_x && empty_z) || (empty_y && empty_z);
96 bool contains_point(
const Vec3& p)
const {
98 if(p.x >= min().x && p.x <= max().x &&
99 p.y >= min().y && p.y <= max().y &&
100 p.z >= min().z && p.z <= max().z) {
107 bool contains_points(
const Vec3* vertices, std::size_t count)
const {
108 for(std::size_t i = 0; i < count; ++i) {
109 if(!contains_point(vertices[i])) {
117 Vec3 closest_point(
const Vec3& p)
const {
120 closest.x = fast_max(min().x, fast_min(p.x, max().x));
121 closest.y = fast_max(min().y, fast_min(p.y, max().y));
122 closest.z = fast_max(min().z, fast_min(p.z, max().z));
127 bool contains_points(
const std::vector<Vec3>& points)
const {
128 return contains_points(&points[0], points.size());
131 std::array<Vec3, 8> corners()
const {
133 center_ +
Vec3(-extents_.x, -extents_.y, -extents_.z),
134 center_ +
Vec3( extents_.x, -extents_.y, -extents_.z),
135 center_ +
Vec3( extents_.x, -extents_.y, extents_.z),
136 center_ +
Vec3(-extents_.x, -extents_.y, extents_.z),
137 center_ +
Vec3(-extents_.x, extents_.y, -extents_.z),
138 center_ +
Vec3( extents_.x, extents_.y, -extents_.z),
139 center_ +
Vec3( extents_.x, extents_.y, extents_.z),
140 center_ +
Vec3(-extents_.x, extents_.y, extents_.z),
144 void encapsulate(
const AABB& other);
145 void encapsulate(
const Vec3& point);
147 bool operator==(
const AABB& rhs)
const {
148 return min().equals(rhs.min()) && max().equals(rhs.max());
151 bool operator!=(
const AABB& rhs)
const {
152 return !(*
this == rhs);
155 friend std::ostream& operator<<(std::ostream& stream,
const AABB& aabb);
158 std::ostream& operator<<(std::ostream& stream,
const AABB& aabb);