[Gameplay] Reduce loom cost
[0ad.git] / source / maths / BoundingBoxOriented.h
bloba397e67e7ef97db3b72ab8c0a98ba43d5face2f4
1 /* Copyright (C) 2011 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_BOX
19 #define INCLUDED_BOX
21 #include "maths/Vector3D.h"
23 class CBoundingBoxAligned;
26 * Generic oriented box. Originally intended to be used an Oriented Bounding Box (OBB),
27 * as opposed to CBoundingBoxAligned which is always aligned to the world-space axes (AABB).
28 * However, it could also be used to represent more generic shapes, such as parallelepipeds.
30 class CBoundingBoxOriented
32 public:
34 /// Empty constructor; creates an empty box
35 CBoundingBoxOriented() { SetEmpty(); }
37 /**
38 * Constructs a new oriented box centered at @p center and with normalized side vectors @p u,
39 * @p v and @p w. These vectors should be mutually orthonormal for a proper rectangular box.
40 * The half-widths of the box in each dimension are given by the corresponding components of
41 * @p halfSizes.
43 CBoundingBoxOriented(const CVector3D& center, const CVector3D& u, const CVector3D& v, const CVector3D& w, const CVector3D& halfSizes)
44 : m_Center(center), m_HalfSizes(halfSizes)
46 m_Basis[0] = u;
47 m_Basis[1] = v;
48 m_Basis[2] = w;
51 /// Constructs a new box from an axis-aligned bounding box (AABB).
52 explicit CBoundingBoxOriented(const CBoundingBoxAligned& bound);
54 /**
55 * Check if a given ray intersects this box. Must not be used if IsEmpty() is true.
56 * See Real-Time Rendering, Third Edition by T. Akenine-Moller, p. 741--744.
58 * @param[in] origin Origin of the ray.
59 * @param[in] dir Direction vector of the ray, defining the positive direction of the ray.
60 * Must be of unit length.
61 * @param[out] tMin,tMax Distance in the positive direction from the origin of the ray to the
62 * entry and exit points in the box, provided that the ray intersects the box. if
63 * the ray does not intersect the box, no values are written to these variables.
64 * If the origin is inside the box, then this is counted as an intersection and one
65 * of @p tMin and @p tMax may be negative.
67 * @return true If the ray originating in @p origin and with unit direction vector @p dir intersects
68 * this box, false otherwise.
70 bool RayIntersect(const CVector3D& origin, const CVector3D& dir, float& tMin, float& tMax) const;
72 /**
73 * Returns the corner at coordinate (@p u, @p v, @p w). Each of @p u, @p v and @p w must be exactly 1 or -1.
74 * Must not be used if IsEmpty() is true.
76 void GetCorner(int u, int v, int w, CVector3D& out) const
78 out = m_Center + m_Basis[0]*(u*m_HalfSizes[0]) + m_Basis[1]*(v*m_HalfSizes[1]) + m_Basis[2]*(w*m_HalfSizes[2]);
81 void SetEmpty()
83 // everything is zero
84 m_Center = CVector3D();
85 m_Basis[0] = CVector3D();
86 m_Basis[1] = CVector3D();
87 m_Basis[2] = CVector3D();
88 m_HalfSizes = CVector3D();
91 bool IsEmpty() const
93 CVector3D empty;
94 return (m_Center == empty &&
95 m_Basis[0] == empty &&
96 m_Basis[1] == empty &&
97 m_Basis[2] == empty &&
98 m_HalfSizes == empty);
101 public:
102 CVector3D m_Center; ///< Centroid location of the box
103 CVector3D m_HalfSizes; ///< Half the sizes of the box in each dimension (u,v,w). Positive values are expected.
104 /// Basis vectors (u,v,w) of the sides. Must always be normalized, and should be
105 /// orthogonal for a proper rectangular cuboid.
106 CVector3D m_Basis[3];
108 static const CBoundingBoxOriented EMPTY;
111 #endif // INCLUDED_BOX