Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BoundingBoxAligned.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 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 // necessary includes
26 #include "Vector3D.h"
27 #include "graphics/ShaderProgram.h"
28 
29 class CFrustum;
30 class CMatrix3D;
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 // basic axis aligned bounding box (AABB) class
36 {
37 public:
38 
40  CBoundingBoxAligned(const CVector3D& min, const CVector3D& max) {
41  m_Data[0] = min;
42  m_Data[1] = max;
43  }
44 
45  /**
46  * Transforms these bounds according to the specified transformation matrix @p m, and writes the axis-aligned bounds
47  * of that result to @p result.
48  */
49  void Transform(const CMatrix3D& m, CBoundingBoxAligned& result) const;
50 
51  /**
52  * Transform these bounds using the matrix @p transform, and write out the result as an oriented (i.e. non-axis-aligned) box.
53  * The difference with @ref Transform(const CMatrix3D&, CBoundingBoxAligned&) is that that method is equivalent to first
54  * computing this result, and then taking the axis-aligned bounding boxes from the result again.
55  */
56  void Transform(const CMatrix3D& m, CBoundingBoxOriented& result) const;
57 
58  CVector3D& operator[](int index) { return m_Data[index]; }
59  const CVector3D& operator[](int index) const { return m_Data[index]; }
60 
61  void SetEmpty();
62  bool IsEmpty() const;
63 
64  void Extend(const CVector3D& min, const CVector3D& max)
65  {
66  if (min.X < m_Data[0].X) m_Data[0].X = min.X;
67  if (min.Y < m_Data[0].Y) m_Data[0].Y = min.Y;
68  if (min.Z < m_Data[0].Z) m_Data[0].Z = min.Z;
69  if (max.X > m_Data[1].X) m_Data[1].X = max.X;
70  if (max.Y > m_Data[1].Y) m_Data[1].Y = max.Y;
71  if (max.Z > m_Data[1].Z) m_Data[1].Z = max.Z;
72  }
73 
74  // operator+=: extend this bound to include given bound
76  {
77  Extend(b.m_Data[0], b.m_Data[1]);
78  return *this;
79  }
80 
81  // operator+=: extend this bound to include given point
83  {
84  Extend(pt, pt);
85  return *this;
86  }
87 
88  /**
89  * Check if a given ray intersects this AABB.
90  * See also Real-Time Rendering, Third Edition by T. Akenine-Moller, p. 741--742.
91  *
92  * @param[in] origin Origin of the ray.
93  * @param[in] dir Direction vector of the ray, defining the positive direction of the ray. Must be of unit length.
94  * @param[out] tmin,tmax distance in the positive direction from the origin of the ray to the entry and exit points in
95  * 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.
96  *
97  * @return true if the ray originating in @p origin and with unit direction vector @p dir intersects this AABB, false otherwise.
98  */
99  bool RayIntersect(const CVector3D& origin, const CVector3D& dir, float& tmin, float& tmax) const;
100 
101  // return the volume of this bounding box
102  float GetVolume() const
103  {
104  CVector3D v = m_Data[1] - m_Data[0];
105  return (std::max(v.X, 0.0f) * std::max(v.Y, 0.0f) * std::max(v.Z, 0.0f));
106  }
107 
108  // return the centre of this bounding box
109  void GetCentre(CVector3D& centre) const
110  {
111  centre = (m_Data[0] + m_Data[1]) * 0.5f;
112  }
113 
114  /**
115  * Expand the bounding box by the given amount in every direction.
116  */
117  void Expand(float amount);
118 
119  /**
120  * IntersectFrustumConservative: Approximate the intersection of this bounds object
121  * with the given frustum. The bounds object is overwritten with the results.
122  *
123  * The approximation is conservative in the sense that the result will always contain
124  * the actual intersection, but it may be larger than the intersection itself.
125  * The result will always be fully contained within the original bounds.
126  *
127  * @note While not in the spirit of this function's purpose, a no-op would be a correct
128  * implementation of this function.
129  * @note If this bound is empty, the result is the empty bound.
130  *
131  * @param frustum the frustum to intersect with
132  */
133  void IntersectFrustumConservative(const CFrustum& frustum);
134 
135  /**
136  * Render: Render the surfaces of the bound object as triangles.
137  */
138  void Render(CShaderProgramPtr& shader) const;
139 
140  /**
141  * Render: Render the outline of the bound object as lines.
142  */
143  void RenderOutline(CShaderProgramPtr& shader) const;
144 
145 private:
146  // Holds the minimal and maximal coordinate points in m_Data[0] and m_Data[1], respectively.
148 
149 public:
151 
152 };
153 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
154 
155 
156 #endif
void Transform(const CMatrix3D &m, CBoundingBoxAligned &result) const
Transforms these bounds according to the specified transformation matrix m, and writes the axis-align...
void RenderOutline(CShaderProgramPtr &shader) const
Render: Render the outline of the bound object as lines.
CVector3D & operator[](int index)
static const CBoundingBoxAligned EMPTY
CBoundingBoxAligned(const CVector3D &min, const CVector3D &max)
float GetVolume() const
void GetCentre(CVector3D &centre) const
CBoundingBoxAligned & operator+=(const CBoundingBoxAligned &b)
void Extend(const CVector3D &min, const CVector3D &max)
float X
Definition: Vector3D.h:31
float Y
Definition: Vector3D.h:31
void IntersectFrustumConservative(const CFrustum &frustum)
IntersectFrustumConservative: Approximate the intersection of this bounds object with the given frust...
CBoundingBoxAligned & operator+=(const CVector3D &pt)
bool RayIntersect(const CVector3D &origin, const CVector3D &dir, float &tmin, float &tmax) const
Check if a given ray intersects this AABB.
const CVector3D & operator[](int index) const
void Expand(float amount)
Expand the bounding box by the given amount in every direction.
float Z
Definition: Vector3D.h:31
void Render(CShaderProgramPtr &shader) const
Render: Render the surfaces of the bound object as triangles.
shared_ptr< CShaderProgram > CShaderProgramPtr