SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
AABB.hpp
Go to the documentation of this file.
1#ifndef _AABB_HPP_
2#define _AABB_HPP_
3
4#include <Math/Vector.hpp>
5
6namespace Sleak {
7namespace Math {
8
9/// Axis-aligned bounding box (world or local space).
10/// @ingroup math
11struct AABB {
12 Vector3D min{0.0f, 0.0f, 0.0f};
13 Vector3D max{0.0f, 0.0f, 0.0f};
14
15 AABB() = default;
16 AABB(const Vector3D& mn, const Vector3D& mx) : min(mn), max(mx) {}
17
18 /// True if min is componentwise <= max.
19 bool IsValid() const {
20 return min.GetX() <= max.GetX() && min.GetY() <= max.GetY() &&
21 min.GetZ() <= max.GetZ();
22 }
23
24 Vector3D Center() const { return (min + max) * 0.5f; }
25
26 Vector3D Extents() const { return (max - min) * 0.5f; }
27
28 // Corner i in [0,8): bit0 -> x, bit1 -> y, bit2 -> z.
29 Vector3D Corner(int i) const {
30 return Vector3D((i & 1) ? max.GetX() : min.GetX(),
31 (i & 2) ? max.GetY() : min.GetY(),
32 (i & 4) ? max.GetZ() : min.GetZ());
33 }
34
35 /// Grows this box to also cover o.
36 void Merge(const AABB& o) {
37 if (o.min.GetX() < min.GetX()) min.SetX(o.min.GetX());
38 if (o.min.GetY() < min.GetY()) min.SetY(o.min.GetY());
39 if (o.min.GetZ() < min.GetZ()) min.SetZ(o.min.GetZ());
40 if (o.max.GetX() > max.GetX()) max.SetX(o.max.GetX());
41 if (o.max.GetY() > max.GetY()) max.SetY(o.max.GetY());
42 if (o.max.GetZ() > max.GetZ()) max.SetZ(o.max.GetZ());
43 }
44
45 /// Pushes min/max outward by amount on every axis.
46 void Expand(float amount) {
47 min.Add(-amount, -amount, -amount);
48 max.Add(amount, amount, amount);
49 }
50
51 /// True if p lies within (or on the boundary of) the box.
52 bool Contains(const Vector3D& p) const {
53 return p.GetX() >= min.GetX() && p.GetX() <= max.GetX() &&
54 p.GetY() >= min.GetY() && p.GetY() <= max.GetY() &&
55 p.GetZ() >= min.GetZ() && p.GetZ() <= max.GetZ();
56 }
57
58 // Squared distance from a point to this box (0 if inside).
59 float DistanceSq(const Vector3D& p) const {
60 float d = 0.0f;
61 float v;
62 v = p.GetX();
63 if (v < min.GetX()) d += (min.GetX() - v) * (min.GetX() - v);
64 else if (v > max.GetX()) d += (v - max.GetX()) * (v - max.GetX());
65 v = p.GetY();
66 if (v < min.GetY()) d += (min.GetY() - v) * (min.GetY() - v);
67 else if (v > max.GetY()) d += (v - max.GetY()) * (v - max.GetY());
68 v = p.GetZ();
69 if (v < min.GetZ()) d += (min.GetZ() - v) * (min.GetZ() - v);
70 else if (v > max.GetZ()) d += (v - max.GetZ()) * (v - max.GetZ());
71 return d;
72 }
73};
74
75} // namespace Math
76} // namespace Sleak
77
78#endif // _AABB_HPP_
float GetY() const
Definition Vector.hpp:361
float GetX() const
Definition Vector.hpp:360
float GetZ() const
Definition Vector.hpp:362
Vectors, matrices, quaternions, colors, AABBs, and random helpers.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
Vector3D Extents() const
Definition AABB.hpp:26
Vector3D Center() const
Definition AABB.hpp:24
Vector3D Corner(int i) const
Definition AABB.hpp:29
bool Contains(const Vector3D &p) const
True if p lies within (or on the boundary of) the box.
Definition AABB.hpp:52
AABB(const Vector3D &mn, const Vector3D &mx)
Definition AABB.hpp:16
void Merge(const AABB &o)
Grows this box to also cover o.
Definition AABB.hpp:36
Vector3D min
Definition AABB.hpp:12
bool IsValid() const
True if min is componentwise <= max.
Definition AABB.hpp:19
Vector3D max
Definition AABB.hpp:13
void Expand(float amount)
Pushes min/max outward by amount on every axis.
Definition AABB.hpp:46
float DistanceSq(const Vector3D &p) const
Definition AABB.hpp:59