Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
BoundingBoxAligned.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18/*
19 * Axis-aligned bounding box
20 */
21
22#ifndef INCLUDED_BOUND
23#define INCLUDED_BOUND
24
25#include "maths/Vector3D.h"
26
27#include <algorithm>
28
30class CFrustum;
31class CMatrix3D;
32
33// Basic axis aligned bounding box (AABB) class
35{
36public:
38
40 CBoundingBoxAligned(const CVector3D& min, const CVector3D& max)
41 {
42 m_Data[0] = min;
43 m_Data[1] = max;
44 }
45
46 /**
47 * Transforms these bounds according to the specified transformation matrix @p m, and writes the axis-aligned bounds
48 * of that result to @p result.
49 */
50 void Transform(const CMatrix3D& m, CBoundingBoxAligned& result) const;
51
52 /**
53 * Transform these bounds using the matrix @p transform, and write out the result as an oriented (i.e. non-axis-aligned) box.
54 * The difference with @ref Transform(const CMatrix3D&, CBoundingBoxAligned&) is that that method is equivalent to first
55 * computing this result, and then taking the axis-aligned bounding boxes from the result again.
56 */
57 void Transform(const CMatrix3D& m, CBoundingBoxOriented& result) const;
58
59 /**
60 * Translates these bounds by @p v, and writes the result to @p result.
61 */
62 void Translate(const CVector3D& v, CBoundingBoxAligned& result) const
63 {
64 result.m_Data[0] = m_Data[0] + v;
65 result.m_Data[1] = m_Data[1] + v;
66 }
67
68 CVector3D& operator[](int index) { return m_Data[index]; }
69 const CVector3D& operator[](int index) const { return m_Data[index]; }
70
71 void SetEmpty();
72 bool IsEmpty() const;
73
74 void Extend(const CVector3D& min, const CVector3D& max)
75 {
76 if (min.X < m_Data[0].X) m_Data[0].X = min.X;
77 if (min.Y < m_Data[0].Y) m_Data[0].Y = min.Y;
78 if (min.Z < m_Data[0].Z) m_Data[0].Z = min.Z;
79 if (max.X > m_Data[1].X) m_Data[1].X = max.X;
80 if (max.Y > m_Data[1].Y) m_Data[1].Y = max.Y;
81 if (max.Z > m_Data[1].Z) m_Data[1].Z = max.Z;
82 }
83
84 // operator+=: extend this bound to include given bound
86 {
87 Extend(b.m_Data[0], b.m_Data[1]);
88 return *this;
89 }
90
91 // operator+=: extend this bound to include given point
93 {
94 Extend(pt, pt);
95 return *this;
96 }
97
98 /**
99 * Check if a given ray intersects this AABB.
100 * See also Real-Time Rendering, Third Edition by T. Akenine-Moller, p. 741--742.
101 *
102 * @param[in] origin Origin of the ray.
103 * @param[in] dir Direction vector of the ray, defining the positive direction of the ray. Must be of unit length.
104 * @param[out] tmin,tmax distance in the positive direction from the origin of the ray to the entry and exit points in
105 * the bounding box. If the origin is inside the box, then this is counted as an intersection and one of @p tMin and @p tMax may be negative.
106 *
107 * @return true if the ray originating in @p origin and with unit direction vector @p dir intersects this AABB, false otherwise.
108 */
109 bool RayIntersect(const CVector3D& origin, const CVector3D& dir, float& tmin, float& tmax) const;
110
111 bool IsPointInside(const CVector3D& point) const;
112
113 // return the volume of this bounding box
114 float GetVolume() const
115 {
116 CVector3D v = m_Data[1] - m_Data[0];
117 return (std::max(v.X, 0.0f) * std::max(v.Y, 0.0f) * std::max(v.Z, 0.0f));
118 }
119
120 // return the center of this bounding box
121 void GetCenter(CVector3D& center) const
122 {
123 center = (m_Data[0] + m_Data[1]) * 0.5f;
124 }
125
126 /**
127 * Expand the bounding box by the given amount in every direction.
128 */
129 void Expand(float amount);
130
131 /**
132 * IntersectFrustumConservative: Approximate the intersection of this bounds object
133 * with the given frustum. The bounds object is overwritten with the results.
134 *
135 * The approximation is conservative in the sense that the result will always contain
136 * the actual intersection, but it may be larger than the intersection itself.
137 * The result will always be fully contained within the original bounds.
138 *
139 * @note While not in the spirit of this function's purpose, a no-op would be a correct
140 * implementation of this function.
141 * @note If this bound is empty, the result is the empty bound.
142 *
143 * @param frustum the frustum to intersect with
144 */
145 void IntersectFrustumConservative(const CFrustum& frustum);
146
147 /**
148 * Construct a CFrustum that describes the same volume as this bounding box.
149 * Only valid for non-empty bounding boxes - check IsEmpty() first.
150 */
151 CFrustum ToFrustum() const;
152
153private:
154 // Holds the minimal and maximal coordinate points in m_Data[0] and m_Data[1], respectively.
156};
157
158#endif // INCLUDED_BOUND
Definition: BoundingBoxAligned.h:35
const CVector3D & operator[](int index) const
Definition: BoundingBoxAligned.h:69
void Translate(const CVector3D &v, CBoundingBoxAligned &result) const
Translates these bounds by v, and writes the result to result.
Definition: BoundingBoxAligned.h:62
CBoundingBoxAligned()
Definition: BoundingBoxAligned.h:39
CBoundingBoxAligned & operator+=(const CVector3D &pt)
Definition: BoundingBoxAligned.h:92
bool IsPointInside(const CVector3D &point) const
Definition: BoundingBoxAligned.cpp:270
void IntersectFrustumConservative(const CFrustum &frustum)
IntersectFrustumConservative: Approximate the intersection of this bounds object with the given frust...
Definition: BoundingBoxAligned.cpp:213
float GetVolume() const
Definition: BoundingBoxAligned.h:114
void SetEmpty()
Definition: BoundingBoxAligned.cpp:139
void Extend(const CVector3D &min, const CVector3D &max)
Definition: BoundingBoxAligned.h:74
CVector3D & operator[](int index)
Definition: BoundingBoxAligned.h:68
bool RayIntersect(const CVector3D &origin, const CVector3D &dir, float &tmin, float &tmax) const
Check if a given ray intersects this AABB.
Definition: BoundingBoxAligned.cpp:43
CBoundingBoxAligned(const CVector3D &min, const CVector3D &max)
Definition: BoundingBoxAligned.h:40
void Expand(float amount)
Expand the bounding box by the given amount in every direction.
Definition: BoundingBoxAligned.cpp:264
void Transform(const CMatrix3D &m, CBoundingBoxAligned &result) const
Transforms these bounds according to the specified transformation matrix m, and writes the axis-align...
Definition: BoundingBoxAligned.cpp:156
void GetCenter(CVector3D &center) const
Definition: BoundingBoxAligned.h:121
CFrustum ToFrustum() const
Construct a CFrustum that describes the same volume as this bounding box.
Definition: BoundingBoxAligned.cpp:230
CBoundingBoxAligned & operator+=(const CBoundingBoxAligned &b)
Definition: BoundingBoxAligned.h:85
bool IsEmpty() const
Definition: BoundingBoxAligned.cpp:147
CVector3D m_Data[2]
Definition: BoundingBoxAligned.h:155
static const CBoundingBoxAligned EMPTY
Definition: BoundingBoxAligned.h:37
Definition: BoundingBoxOriented.h:31
Definition: Frustum.h:40
Definition: Matrix3D.h:36
Definition: Vector3D.h:32
float Z
Definition: Vector3D.h:34
float Y
Definition: Vector3D.h:34
float X
Definition: Vector3D.h:34