Line data Source code
1 : /* Copyright (C) 2019 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"
21 : #include "maths/BoundingBoxAligned.h"
22 :
23 : #include <float.h>
24 :
25 1 : const CBoundingBoxOriented CBoundingBoxOriented::EMPTY = CBoundingBoxOriented();
26 :
27 0 : CBoundingBoxOriented::CBoundingBoxOriented(const CBoundingBoxAligned& bound)
28 : {
29 0 : if (bound.IsEmpty())
30 : {
31 0 : SetEmpty();
32 : }
33 : else
34 : {
35 0 : bound.GetCenter(m_Center);
36 :
37 : // the axes of an AABB are the world-space axes
38 0 : m_Basis[0].X = 1.f; m_Basis[0].Y = 0.f; m_Basis[0].Z = 0.f;
39 0 : m_Basis[1].X = 0.f; m_Basis[1].Y = 1.f; m_Basis[1].Z = 0.f;
40 0 : 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 0 : m_HalfSizes = (bound[1] - bound[0]) * 0.5f;
44 : }
45 0 : }
46 :
47 2 : 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 2 : float tMin = -FLT_MAX;
51 2 : float tMax = FLT_MAX;
52 :
53 2 : CVector3D p = m_Center - origin;
54 :
55 6 : 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 5 : float e = m_Basis[i].Dot(p); // distance between the ray origin and the box center projected onto the slab normal
59 5 : float f = m_Basis[i].Dot(dir); // cosine of the angle between the slab normal and the ray direction
60 :
61 5 : 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 4 : float invF = 1.f/f;
66 4 : float t1 = (e + m_HalfSizes[i]) * invF;
67 4 : float t2 = (e - m_HalfSizes[i]) * invF;
68 :
69 : // make sure t1 <= t2, swap if necessary
70 4 : if (t1 > t2)
71 : {
72 2 : float tmp = t1;
73 2 : t1 = t2;
74 2 : t2 = tmp;
75 : }
76 :
77 : // update the overall tMin and tMax if necessary
78 4 : if (t1 > tMin) tMin = t1;
79 4 : if (t2 < tMax) tMax = t2;
80 :
81 : // try to break out of the loop as fast as possible by checking for some conditions
82 4 : if (tMin > tMax) return false; // ray misses the box
83 3 : 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 1 : if (e > m_HalfSizes[i] || -e > m_HalfSizes[i])
90 0 : return false;
91 : }
92 : }
93 :
94 1 : tMin_out = tMin;
95 1 : tMax_out = tMax;
96 1 : return true;
97 3 : }
|