LCOV - code coverage report
Current view: top level - source/maths - BoundingBoxAligned.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 29 110 26.4 %
Date: 2026-12-03 01:19:39 Functions: 4 9 44.4 %

          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             : /*
      19             :  * Axis-aligned bounding box
      20             :  */
      21             : 
      22             : #include "precompiled.h"
      23             : 
      24             : #include "BoundingBoxAligned.h"
      25             : 
      26             : #include "lib/debug.h"
      27             : #include "maths/BoundingBoxOriented.h"
      28             : #include "maths/Brush.h"
      29             : #include "maths/Frustum.h"
      30             : #include "maths/Matrix3D.h"
      31             : #include "maths/Plane.h"
      32             : 
      33             : #include <limits>
      34             : #include <utility>
      35             : 
      36             : const CBoundingBoxAligned CBoundingBoxAligned::EMPTY = CBoundingBoxAligned(); // initializes to an empty bound
      37             : 
      38             : ///////////////////////////////////////////////////////////////////////////////
      39             : // RayIntersect: intersect ray with this bound; return true
      40             : // if ray hits (and store entry and exit times), or false
      41             : // otherwise
      42             : // note: incoming ray direction must be normalised
      43           0 : bool CBoundingBoxAligned::RayIntersect(
      44             :     const CVector3D& origin, const CVector3D& dir, float& tmin, float& tmax) const
      45             : {
      46           0 :     float t1, t2;
      47           0 :     float tnear, tfar;
      48             : 
      49           0 :     if (dir[0] == 0)
      50             :     {
      51           0 :         if (origin[0] < m_Data[0][0] || origin[0] > m_Data[1][0])
      52             :             return false;
      53             :         else
      54             :         {
      55             :             tnear = -std::numeric_limits<float>::max();
      56             :             tfar = std::numeric_limits<float>::max();
      57             :         }
      58             :     }
      59             :     else
      60             :     {
      61           0 :         t1 = (m_Data[0][0] - origin[0]) / dir[0];
      62           0 :         t2 = (m_Data[1][0] - origin[0]) / dir[0];
      63             : 
      64           0 :         if (dir[0] < 0)
      65             :         {
      66             :             tnear = t2;
      67             :             tfar = t1;
      68             :         }
      69             :         else
      70             :         {
      71           0 :             tnear = t1;
      72           0 :             tfar = t2;
      73             :         }
      74             : 
      75           0 :         if (tfar < 0)
      76             :             return false;
      77             :     }
      78             : 
      79           0 :     if (dir[1] == 0 && (origin[1] < m_Data[0][1] || origin[1] > m_Data[1][1]))
      80             :         return false;
      81             :     else
      82             :     {
      83           0 :         t1 = (m_Data[0][1] - origin[1]) / dir[1];
      84           0 :         t2 = (m_Data[1][1] - origin[1]) / dir[1];
      85             : 
      86           0 :         if (dir[1] < 0)
      87             :         {
      88           0 :             if (t2 > tnear)
      89           0 :                 tnear = t2;
      90           0 :             if (t1 < tfar)
      91           0 :                 tfar = t1;
      92             :         }
      93             :         else
      94             :         {
      95           0 :             if (t1 > tnear)
      96           0 :                 tnear = t1;
      97           0 :             if (t2 < tfar)
      98           0 :                 tfar = t2;
      99             :         }
     100             : 
     101           0 :         if (tnear > tfar || tfar < 0)
     102             :             return false;
     103             :     }
     104             : 
     105           0 :     if (dir[2] == 0 && (origin[2] < m_Data[0][2] || origin[2] > m_Data[1][2]))
     106             :         return false;
     107             :     else
     108             :     {
     109           0 :         t1 = (m_Data[0][2] - origin[2]) / dir[2];
     110           0 :         t2 = (m_Data[1][2] - origin[2]) / dir[2];
     111             : 
     112           0 :         if (dir[2] < 0)
     113             :         {
     114           0 :             if (t2 > tnear)
     115           0 :                 tnear = t2;
     116           0 :             if (t1 < tfar)
     117           0 :                 tfar = t1;
     118             :         }
     119             :         else
     120             :         {
     121           0 :             if (t1 > tnear)
     122           0 :                 tnear = t1;
     123           0 :             if (t2 < tfar)
     124           0 :                 tfar = t2;
     125             :         }
     126             : 
     127           0 :         if (tnear > tfar || tfar < 0)
     128             :             return false;
     129             :     }
     130             : 
     131           0 :     tmin = tnear;
     132           0 :     tmax = tfar;
     133             : 
     134           0 :     return true;
     135             : }
     136             : 
     137             : ///////////////////////////////////////////////////////////////////////////////
     138             : // SetEmpty: initialise this bound as empty
     139        2579 : void CBoundingBoxAligned::SetEmpty()
     140             : {
     141        2579 :     m_Data[0] = CVector3D::Max();
     142        2579 :     m_Data[1] = CVector3D::Min();
     143        2578 : }
     144             : 
     145             : ///////////////////////////////////////////////////////////////////////////////
     146             : // IsEmpty: tests whether this bound is empty
     147          18 : bool CBoundingBoxAligned::IsEmpty() const
     148             : {
     149          28 :     return m_Data[0] == CVector3D::Max() && m_Data[1] == CVector3D::Min();
     150             : }
     151             : 
     152             : ///////////////////////////////////////////////////////////////////////////////
     153             : // Transform: transform this bound by given matrix; return transformed bound
     154             : // in 'result' parameter - slightly modified version of code in Graphic Gems
     155             : // (can't remember which one it was, though)
     156           0 : void CBoundingBoxAligned::Transform(const CMatrix3D& m, CBoundingBoxAligned& result) const
     157             : {
     158           0 :     ENSURE(this != &result);
     159             : 
     160           0 :     for (int i = 0; i < 3; ++i)
     161             :     {
     162             :         // handle translation
     163           0 :         result[0][i] = result[1][i] = m(i, 3);
     164             : 
     165             :         // Now find the extreme points by considering the product of the
     166             :         // min and max with each component of matrix
     167           0 :         for (int j = 0; j < 3; ++j)
     168             :         {
     169           0 :             float a = m(i, j) * m_Data[0][j];
     170           0 :             float b = m(i, j) * m_Data[1][j];
     171             : 
     172           0 :             if (a >= b)
     173           0 :                 std::swap(a, b);
     174             : 
     175           0 :             result[0][i] += a;
     176           0 :             result[1][i] += b;
     177             :         }
     178             :     }
     179           0 : }
     180             : 
     181           5 : void CBoundingBoxAligned::Transform(const CMatrix3D& transform, CBoundingBoxOriented& result) const
     182             : {
     183           5 :     const CVector3D& pMin = m_Data[0];
     184           5 :     const CVector3D& pMax = m_Data[1];
     185             : 
     186             :     // the basis vectors of the OBB are the normalized versions of the transformed AABB basis vectors, which
     187             :     // are the columns of the identity matrix, so the unnormalized OBB basis vectors are the transformation
     188             :     // matrix columns:
     189           5 :     CVector3D u(transform._11, transform._21, transform._31);
     190           5 :     CVector3D v(transform._12, transform._22, transform._32);
     191           5 :     CVector3D w(transform._13, transform._23, transform._33);
     192             : 
     193             :     // the half-sizes are scaled by whatever factor the AABB unit vectors end up scaled by
     194           5 :     result.m_HalfSizes = CVector3D(
     195           5 :         (pMax.X - pMin.X) / 2.f * u.Length(),
     196           5 :         (pMax.Y - pMin.Y) / 2.f * v.Length(),
     197           5 :         (pMax.Z - pMin.Z) / 2.f * w.Length()
     198             :     );
     199             : 
     200           5 :     u.Normalize();
     201           5 :     v.Normalize();
     202           5 :     w.Normalize();
     203             : 
     204           5 :     result.m_Basis[0] = u;
     205           5 :     result.m_Basis[1] = v;
     206           5 :     result.m_Basis[2] = w;
     207             : 
     208           5 :     result.m_Center = transform.Transform((pMax + pMin) * 0.5f);
     209           5 : }
     210             : 
     211             : ///////////////////////////////////////////////////////////////////////////////
     212             : // Intersect with the given frustum in a conservative manner
     213           0 : void CBoundingBoxAligned::IntersectFrustumConservative(const CFrustum& frustum)
     214             : {
     215             :     // if this bound is empty, then the result must be empty (we should not attempt to intersect with
     216             :     // a brush, may cause crashes due to the numeric representation of empty bounds -- see
     217             :     // https://gitea.wildfiregames.com/0ad/0ad/issues/1027)
     218           0 :     if (IsEmpty())
     219           0 :         return;
     220             : 
     221           0 :     CBrush brush(*this);
     222           0 :     CBrush buf;
     223             : 
     224           0 :     brush.Intersect(frustum, buf);
     225             : 
     226           0 :     buf.Bounds(*this);
     227           0 : }
     228             : 
     229             : ///////////////////////////////////////////////////////////////////////////////
     230           0 : CFrustum CBoundingBoxAligned::ToFrustum() const
     231             : {
     232           0 :     CFrustum frustum;
     233             : 
     234           0 :     frustum.SetNumPlanes(6);
     235             : 
     236             :     // get the LEFT plane
     237           0 :     frustum[0].m_Norm = CVector3D(1, 0, 0);
     238           0 :     frustum[0].m_Dist = -m_Data[0].X;
     239             : 
     240             :     // get the RIGHT plane
     241           0 :     frustum[1].m_Norm = CVector3D(-1, 0, 0);
     242           0 :     frustum[1].m_Dist = m_Data[1].X;
     243             : 
     244             :     // get the BOTTOM plane
     245           0 :     frustum[2].m_Norm = CVector3D(0, 1, 0);
     246           0 :     frustum[2].m_Dist = -m_Data[0].Y;
     247             : 
     248             :     // get the TOP plane
     249           0 :     frustum[3].m_Norm = CVector3D(0, -1, 0);
     250           0 :     frustum[3].m_Dist = m_Data[1].Y;
     251             : 
     252             :     // get the NEAR plane
     253           0 :     frustum[4].m_Norm = CVector3D(0, 0, 1);
     254           0 :     frustum[4].m_Dist = -m_Data[0].Z;
     255             : 
     256             :     // get the FAR plane
     257           0 :     frustum[5].m_Norm = CVector3D(0, 0, -1);
     258           0 :     frustum[5].m_Dist = m_Data[1].Z;
     259             : 
     260           0 :     return frustum;
     261           0 : }
     262             : 
     263             : ///////////////////////////////////////////////////////////////////////////////
     264           0 : void CBoundingBoxAligned::Expand(float amount)
     265             : {
     266           0 :     m_Data[0] -= CVector3D(amount, amount, amount);
     267           0 :     m_Data[1] += CVector3D(amount, amount, amount);
     268           0 : }
     269             : 
     270          69 : bool CBoundingBoxAligned::IsPointInside(const CVector3D& point) const
     271             : {
     272          69 :     return
     273          59 :         m_Data[0].X <= point.X && point.X <= m_Data[1].X &&
     274          48 :         m_Data[0].Y <= point.Y && point.Y <= m_Data[1].Y &&
     275         106 :         m_Data[0].Z <= point.Z && point.Z <= m_Data[1].Z;
     276             : }

Generated by: LCOV version 1.16