Biome specific map previews for Mainland, which for some reason is still the favorite...
[0ad.git] / source / maths / BoundingBoxOriented.cpp
blobe2e3932844c89dd80580b210de03a48ff1dded7a
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 #include "precompiled.h"
20 #include "BoundingBoxOriented.h"
21 #include "maths/BoundingBoxAligned.h"
23 #include <float.h>
25 const CBoundingBoxOriented CBoundingBoxOriented::EMPTY = CBoundingBoxOriented();
27 CBoundingBoxOriented::CBoundingBoxOriented(const CBoundingBoxAligned& bound)
29 if (bound.IsEmpty())
31 SetEmpty();
33 else
35 bound.GetCentre(m_Center);
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;
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;
47 bool CBoundingBoxOriented::RayIntersect(const CVector3D& origin, const CVector3D& dir, float& tMin_out, float& tMax_out) const
49 // See Real-Time Rendering, Third Edition, p. 743
50 float tMin = -FLT_MAX;
51 float tMax = FLT_MAX;
53 CVector3D p = m_Center - origin;
55 for (int i = 0; i < 3; ++i)
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
61 if(fabsf(f) > 1e-10f)
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;
69 // make sure t1 <= t2, swap if necessary
70 if (t1 > t2)
72 float tmp = t1;
73 t1 = t2;
74 t2 = tmp;
77 // update the overall tMin and tMax if necessary
78 if (t1 > tMin) tMin = t1;
79 if (t2 < tMax) tMax = t2;
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
85 else
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;
94 tMin_out = tMin;
95 tMax_out = tMax;
96 return true;