LCOV - code coverage report
Current view: top level - source/maths - BoundingBoxOriented.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 15 18 83.3 %
Date: 2023-01-19 00:18:29 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* Copyright (C) 2011 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             : #ifndef INCLUDED_BOX
      19             : #define INCLUDED_BOX
      20             : 
      21             : #include "maths/Vector3D.h"
      22             : 
      23             : class CBoundingBoxAligned;
      24             : 
      25             : /*
      26             :  * Generic oriented box. Originally intended to be used an Oriented Bounding Box (OBB),
      27             :  * as opposed to CBoundingBoxAligned which is always aligned to the world-space axes (AABB).
      28             :  * However, it could also be used to represent more generic shapes, such as parallelepipeds.
      29             :  */
      30             : class CBoundingBoxOriented
      31             : {
      32             : public:
      33             : 
      34             :     /// Empty constructor; creates an empty box
      35           8 :     CBoundingBoxOriented() { SetEmpty(); }
      36             : 
      37             :     /**
      38             :      * Constructs a new oriented box centered at @p center and with normalized side vectors @p u,
      39             :      * @p v and @p w. These vectors should be mutually orthonormal for a proper rectangular box.
      40             :      * The half-widths of the box in each dimension are given by the corresponding components of
      41             :      * @p halfSizes.
      42             :      */
      43             :     CBoundingBoxOriented(const CVector3D& center, const CVector3D& u, const CVector3D& v, const CVector3D& w, const CVector3D& halfSizes)
      44             :         : m_Center(center), m_HalfSizes(halfSizes)
      45             :     {
      46             :         m_Basis[0] = u;
      47             :         m_Basis[1] = v;
      48             :         m_Basis[2] = w;
      49             :     }
      50             : 
      51             :     /// Constructs a new box from an axis-aligned bounding box (AABB).
      52             :     explicit CBoundingBoxOriented(const CBoundingBoxAligned& bound);
      53             : 
      54             :     /**
      55             :      * Check if a given ray intersects this box. Must not be used if IsEmpty() is true.
      56             :      * See Real-Time Rendering, Third Edition by T. Akenine-Moller, p. 741--744.
      57             :      *
      58             :      * @param[in] origin Origin of the ray.
      59             :      * @param[in] dir Direction vector of the ray, defining the positive direction of the ray.
      60             :      *            Must be of unit length.
      61             :      * @param[out] tMin,tMax Distance in the positive direction from the origin of the ray to the
      62             :      *             entry and exit points in the box, provided that the ray intersects the box. if
      63             :      *             the ray does not intersect the box, no values are written to these variables.
      64             :      *             If the origin is inside the box, then this is counted as an intersection and one
      65             :      *             of @p tMin and @p tMax may be negative.
      66             :      *
      67             :      * @return true If the ray originating in @p origin and with unit direction vector @p dir intersects
      68             :      *         this box, false otherwise.
      69             :      */
      70             :     bool RayIntersect(const CVector3D& origin, const CVector3D& dir, float& tMin, float& tMax) const;
      71             : 
      72             :     /**
      73             :      * Returns the corner at coordinate (@p u, @p v, @p w). Each of @p u, @p v and @p w must be exactly 1 or -1.
      74             :      * Must not be used if IsEmpty() is true.
      75             :      */
      76           0 :     void GetCorner(int u, int v, int w, CVector3D& out) const
      77             :     {
      78           0 :         out = m_Center + m_Basis[0]*(u*m_HalfSizes[0]) + m_Basis[1]*(v*m_HalfSizes[1]) + m_Basis[2]*(w*m_HalfSizes[2]);
      79           0 :     }
      80             : 
      81           9 :     void SetEmpty()
      82             :     {
      83             :         // everything is zero
      84           9 :         m_Center = CVector3D();
      85           9 :         m_Basis[0] = CVector3D();
      86           9 :         m_Basis[1] = CVector3D();
      87           9 :         m_Basis[2] = CVector3D();
      88           9 :         m_HalfSizes = CVector3D();
      89           9 :     }
      90             : 
      91           3 :     bool IsEmpty() const
      92             :     {
      93           3 :         CVector3D empty;
      94           6 :         return (m_Center == empty &&
      95           5 :                 m_Basis[0] == empty &&
      96           4 :                 m_Basis[1] == empty &&
      97           7 :                 m_Basis[2] == empty &&
      98           5 :                 m_HalfSizes == empty);
      99             :     }
     100             : 
     101             : public:
     102             :     CVector3D m_Center; ///< Centroid location of the box
     103             :     CVector3D m_HalfSizes; ///< Half the sizes of the box in each dimension (u,v,w). Positive values are expected.
     104             :     /// Basis vectors (u,v,w) of the sides. Must always be normalized, and should be
     105             :     /// orthogonal for a proper rectangular cuboid.
     106             :     CVector3D m_Basis[3];
     107             : 
     108             :     static const CBoundingBoxOriented EMPTY;
     109             : };
     110             : 
     111             : #endif // INCLUDED_BOX

Generated by: LCOV version 1.13