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: 13 13 100.0 %
Date: 2023-01-19 00:18:29 Functions: 4 4 100.0 %

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

Generated by: LCOV version 1.13