LCOV - code coverage report
Current view: top level - source/maths - BoundingSphere.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 11 11 100.0 %
Date: 2026-12-03 01:19:39 Functions: 2 2 100.0 %

          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 "maths/BoundingBoxAligned.h"
      21             : #include "maths/BoundingSphere.h"
      22             : 
      23             : #include <algorithm>
      24             : #include <cmath>
      25             : 
      26           2 : CBoundingSphere CBoundingSphere::FromSweptBox(const CBoundingBoxAligned& bbox)
      27             : {
      28           2 :     float maxX = std::max(fabsf(bbox[0].X), fabsf(bbox[1].X));
      29           2 :     float maxY = std::max(fabsf(bbox[0].Y), fabsf(bbox[1].Y));
      30           2 :     float maxZ = std::max(fabsf(bbox[0].Z), fabsf(bbox[1].Z));
      31             : 
      32           2 :     float radius = sqrtf(maxX*maxX + maxY*maxY + maxZ*maxZ);
      33             : 
      34           2 :     return CBoundingSphere(CVector3D(0.f, 0.f, 0.f), radius);
      35             : }
      36             : 
      37          30 : bool CBoundingSphere::RayIntersect(const CVector3D& origin, const CVector3D& dir) const
      38             : {
      39             :     // Vector v from the origin of the ray to the center of the sphere
      40          30 :     CVector3D v = m_Center - origin;
      41             :     // Length of the projection of v onto the direction vector of the ray
      42             :     const float pcLen = dir.Dot(v);
      43          30 :     if (pcLen > 0.0f)
      44             :     {
      45             :         // Get the shortest distance from the center of the sphere to the ray
      46          18 :         v = dir * pcLen - v;
      47             :     }
      48          30 :     return v.LengthSquared() <= m_Radius * m_Radius;
      49             : }

Generated by: LCOV version 1.16