Line data Source code
1 : /* Copyright (C) 2025 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 <cfloat> 24 : #include <cmath> 25 : 26 : const CBoundingBoxOriented CBoundingBoxOriented::EMPTY = CBoundingBoxOriented(); 27 : 28 0 : CBoundingBoxOriented::CBoundingBoxOriented(const CBoundingBoxAligned& bound) 29 : { 30 0 : if (bound.IsEmpty()) 31 : { 32 0 : SetEmpty(); 33 : } 34 : else 35 : { 36 0 : bound.GetCenter(m_Center); 37 : 38 : // the axes of an AABB are the world-space axes 39 0 : m_Basis[0].X = 1.f; m_Basis[0].Y = 0.f; m_Basis[0].Z = 0.f; 40 0 : m_Basis[1].X = 0.f; m_Basis[1].Y = 1.f; m_Basis[1].Z = 0.f; 41 0 : m_Basis[2].X = 0.f; m_Basis[2].Y = 0.f; m_Basis[2].Z = 1.f; 42 : 43 : // element-wise division by two to get half sizes (remember, [1] and [0] are the max and min coord points) 44 0 : m_HalfSizes = (bound[1] - bound[0]) * 0.5f; 45 : } 46 0 : } 47 : 48 2 : bool CBoundingBoxOriented::RayIntersect(const CVector3D& origin, const CVector3D& dir, float& tMin_out, float& tMax_out) const 49 : { 50 : // See Real-Time Rendering, Third Edition, p. 743 51 2 : float tMin = -FLT_MAX; 52 2 : float tMax = FLT_MAX; 53 : 54 2 : CVector3D p = m_Center - origin; 55 : 56 6 : for (int i = 0; i < 3; ++i) 57 : { 58 : // test the ray for intersections with the slab whose normal vector is m_Basis[i] 59 5 : float e = m_Basis[i].Dot(p); // distance between the ray origin and the box center projected onto the slab normal 60 5 : float f = m_Basis[i].Dot(dir); // cosine of the angle between the slab normal and the ray direction 61 : 62 5 : if(fabsf(f) > 1e-10f) 63 : { 64 : // Determine the distances t1 and t2 from the origin of the ray to the points where it intersects 65 : // the slab. See docs/ray_intersect.pdf for why/how this works. 66 4 : float invF = 1.f/f; 67 4 : float t1 = (e + m_HalfSizes[i]) * invF; 68 4 : float t2 = (e - m_HalfSizes[i]) * invF; 69 : 70 : // make sure t1 <= t2, swap if necessary 71 4 : if (t1 > t2) 72 : { 73 2 : float tmp = t1; 74 2 : t1 = t2; 75 2 : t2 = tmp; 76 : } 77 : 78 : // update the overall tMin and tMax if necessary 79 4 : if (t1 > tMin) tMin = t1; 80 4 : if (t2 < tMax) tMax = t2; 81 : 82 : // try to break out of the loop as fast as possible by checking for some conditions 83 4 : if (tMin > tMax) return false; // ray misses the box 84 3 : if (tMax < 0) return false; // box is behind the ray origin 85 : } 86 : else 87 : { 88 : // the ray is parallel to the slab currently being tested, or is as close to parallel 89 : // as makes no difference; return false if the ray is outside of the slab. 90 1 : if (e > m_HalfSizes[i] || -e > m_HalfSizes[i]) 91 : return false; 92 : } 93 : } 94 : 95 1 : tMin_out = tMin; 96 1 : tMax_out = tMax; 97 1 : return true; 98 : }