SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
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
6
namespace
Sleak
{
7
namespace
Math
{
8
9
/// Axis-aligned bounding box (world or local space).
10
/// @ingroup math
11
struct
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_
Vector.hpp
Sleak::Math::Vector3D
Definition
Vector.hpp:354
Sleak::Math::Vector3D::Add
void Add(float x, float y, float z)
Definition
Vector.hpp:376
Sleak::Math::Vector3D::GetY
float GetY() const
Definition
Vector.hpp:362
Sleak::Math::Vector3D::SetY
void SetY(float val)
Definition
Vector.hpp:367
Sleak::Math::Vector3D::GetX
float GetX() const
Definition
Vector.hpp:361
Sleak::Math::Vector3D::GetZ
float GetZ() const
Definition
Vector.hpp:363
Sleak::Math::Vector3D::SetX
void SetX(float val)
Definition
Vector.hpp:366
Sleak::Math::Vector3D::Vector3D
Vector3D()
Definition
Vector.hpp:357
Sleak::Math::Vector3D::SetZ
void SetZ(float val)
Definition
Vector.hpp:368
Sleak::Math
Vectors, matrices, quaternions, colors, AABBs, and random helpers.
Definition
GameObject.hpp:13
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::Math::AABB::Extents
Vector3D Extents() const
Definition
AABB.hpp:26
Sleak::Math::AABB::Center
Vector3D Center() const
Definition
AABB.hpp:24
Sleak::Math::AABB::Corner
Vector3D Corner(int i) const
Definition
AABB.hpp:29
Sleak::Math::AABB::Contains
bool Contains(const Vector3D &p) const
True if p lies within (or on the boundary of) the box.
Definition
AABB.hpp:52
Sleak::Math::AABB::AABB
AABB(const Vector3D &mn, const Vector3D &mx)
Definition
AABB.hpp:16
Sleak::Math::AABB::Merge
void Merge(const AABB &o)
Grows this box to also cover o.
Definition
AABB.hpp:36
Sleak::Math::AABB::min
Vector3D min
Definition
AABB.hpp:12
Sleak::Math::AABB::IsValid
bool IsValid() const
True if min is componentwise <= max.
Definition
AABB.hpp:19
Sleak::Math::AABB::max
Vector3D max
Definition
AABB.hpp:13
Sleak::Math::AABB::Expand
void Expand(float amount)
Pushes min/max outward by amount on every axis.
Definition
AABB.hpp:46
Sleak::Math::AABB::AABB
AABB()=default
Sleak::Math::AABB::DistanceSq
float DistanceSq(const Vector3D &p) const
Definition
AABB.hpp:59
include
public
Math
AABB.hpp
Generated on
for SleakEngine by
1.18.0