Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BoundingBoxOriented.cpp
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 #include "precompiled.h"
19 
20 #include "BoundingBoxOriented.h"
22 
23 #include <float.h>
24 
26 
28 {
29  if (bound.IsEmpty())
30  {
31  SetEmpty();
32  }
33  else
34  {
35  bound.GetCentre(m_Center);
36 
37  // the axes of an AABB are the world-space axes
38  m_Basis[0].X = 1.f; m_Basis[0].Y = 0.f; m_Basis[0].Z = 0.f;
39  m_Basis[1].X = 0.f; m_Basis[1].Y = 1.f; m_Basis[1].Z = 0.f;
40  m_Basis[2].X = 0.f; m_Basis[2].Y = 0.f; m_Basis[2].Z = 1.f;
41 
42  // element-wise division by two to get half sizes (remember, [1] and [0] are the max and min coord points)
43  m_HalfSizes = (bound[1] - bound[0]) * 0.5f;
44  }
45 }
46 
47 bool CBoundingBoxOriented::RayIntersect(const CVector3D& origin, const CVector3D& dir, float& tMin_out, float& tMax_out) const
48 {
49  // See Real-Time Rendering, Third Edition, p. 743
50  float tMin = -FLT_MAX;
51  float tMax = FLT_MAX;
52 
53  CVector3D p = m_Center - origin;
54 
55  for (int i = 0; i < 3; ++i)
56  {
57  // test the ray for intersections with the slab whose normal vector is m_Basis[i]
58  float e = m_Basis[i].Dot(p); // distance between the ray origin and the box center projected onto the slab normal
59  float f = m_Basis[i].Dot(dir); // cosine of the angle between the slab normal and the ray direction
60 
61  if(fabsf(f) > 1e-10f)
62  {
63  // Determine the distances t1 and t2 from the origin of the ray to the points where it intersects
64  // the slab. See docs/ray_intersect.pdf for why/how this works.
65  float invF = 1.f/f;
66  float t1 = (e + m_HalfSizes[i]) * invF;
67  float t2 = (e - m_HalfSizes[i]) * invF;
68 
69  // make sure t1 <= t2, swap if necessary
70  if (t1 > t2)
71  {
72  float tmp = t1;
73  t1 = t2;
74  t2 = tmp;
75  }
76 
77  // update the overall tMin and tMax if necessary
78  if (t1 > tMin) tMin = t1;
79  if (t2 < tMax) tMax = t2;
80 
81  // try to break out of the loop as fast as possible by checking for some conditions
82  if (tMin > tMax) return false; // ray misses the box
83  if (tMax < 0) return false; // box is behind the ray origin
84  }
85  else
86  {
87  // the ray is parallel to the slab currently being tested, or is as close to parallel
88  // as makes no difference; return false if the ray is outside of the slab.
89  if (e > m_HalfSizes[i] || -e > m_HalfSizes[i])
90  return false;
91  }
92  }
93 
94  tMin_out = tMin;
95  tMax_out = tMax;
96  return true;
97 }
bool RayIntersect(const CVector3D &origin, const CVector3D &dir, float &tMin, float &tMax) const
Check if a given ray intersects this box.
float Dot(const CVector3D &vector) const
Definition: Vector3D.cpp:48
void GetCentre(CVector3D &centre) const
CVector3D m_Center
Centroid location of the box.
float X
Definition: Vector3D.h:31
CVector3D m_Basis[3]
Basis vectors (u,v,w) of the sides.
float Y
Definition: Vector3D.h:31
static const CBoundingBoxOriented EMPTY
CVector3D m_HalfSizes
Half the sizes of the box in each dimension (u,v,w).
CBoundingBoxOriented()
Empty constructor; creates an empty box.
float Z
Definition: Vector3D.h:31