LCOV - code coverage report
Current view: top level - source/simulation2/helpers - Geometry.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 2 2 100.0 %
Date: 2026-12-03 01:19:39 Functions: 0 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             : #ifndef INCLUDED_HELPER_GEOMETRY
      19             : #define INCLUDED_HELPER_GEOMETRY
      20             : 
      21             : /**
      22             :  * @file
      23             :  * Helper functions related to geometry algorithms
      24             :  */
      25             : 
      26             : #include "maths/Fixed.h"
      27             : #include "maths/FixedVector2D.h"
      28             : 
      29             : #include <utility>
      30             : 
      31             : namespace Geometry
      32             : {
      33             : 
      34             : /**
      35             :  * Checks if a point is inside the given rotated rectangle.
      36             :  * Points precisely on an edge are considered to be inside.
      37             :  *
      38             :  * The rectangle is defined by the four vertexes
      39             :  * (+/-u*halfSize.X +/-v*halfSize.Y)
      40             :  *
      41             :  * The @p u and @p v vectors must be perpendicular.
      42             :  */
      43        3337 : inline bool PointIsInSquare(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize)
      44             : {
      45        3331 :     return point.Dot(u).Absolute() <= halfSize.X && point.Dot(v).Absolute() <= halfSize.Y;
      46             : }
      47             : 
      48             : /**
      49             :  * Returns a vector (bx,by) such that every point inside
      50             :  * the given rotated rectangle has coordinates
      51             :  * (x,y) with -bx <= x <= bx, -by <= y < by.
      52             :  *
      53             :  * The rectangle is defined by the four vertexes
      54             :  * (+/-u*halfSize.X +/-v*halfSize.Y).
      55             :  */
      56             : CFixedVector2D GetHalfBoundingBox(const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize);
      57             : 
      58             : /**
      59             :  * Returns the minimum Euclidean distance from the given point to
      60             :  * any point on the boundary of the given rotated rectangle.
      61             :  *
      62             :  * If @p countInsideAsZero is true, and the point is inside the rectangle,
      63             :  * it will return 0.
      64             :  * If @p countInsideAsZero is false, the (positive) distance to the boundary
      65             :  * will be returned regardless of where the point is.
      66             :  *
      67             :  * The rectangle is defined by the four vertexes
      68             :  * (+/-u*halfSize.X +/-v*halfSize.Y).
      69             :  *
      70             :  * The @p u and @p v vectors must be perpendicular and unit length.
      71             :  */
      72             : fixed DistanceToSquare(const CFixedVector2D& point,
      73             :     const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize,
      74             :     bool countInsideAsZero = false);
      75             : 
      76             : /**
      77             :  * Similar to above but never uses sqrt, so it returns the squared distance.
      78             :  */
      79             : fixed DistanceToSquareSquared(const CFixedVector2D& point,
      80             :                        const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize,
      81             :                        bool countInsideAsZero = false);
      82             : /**
      83             :  * Returns a point on the boundary of the given rotated rectangle
      84             :  * that is closest (or equally closest) to the given point
      85             :  * in Euclidean distance.
      86             :  *
      87             :  * The rectangle is defined by the four vertexes
      88             :  * (+/-u*halfSize.X +/-v*halfSize.Y).
      89             :  *
      90             :  * The @p u and @p v vectors must be perpendicular and unit length.
      91             :  */
      92             : CFixedVector2D NearestPointOnSquare(const CFixedVector2D& point,
      93             :     const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize);
      94             : 
      95             : /**
      96             :  * Returns the shortest distance between two squares.
      97             :  */
      98             : fixed DistanceSquareToSquare(const CFixedVector2D& relativePos,
      99             :     const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1,
     100             :     const CFixedVector2D& u2, const CFixedVector2D& v2, const CFixedVector2D& halfSize2);
     101             : 
     102             : /**
     103             :  * Returns the greatest straight line distance from a point to a square.
     104             :  *
     105             :  * If @p countInsideAsZero is true, and the point is inside the rectangle,
     106             :  * it will return 0.
     107             :  * If @p countInsideAsZero is false, the greatest (positive) distance to the boundary
     108             :  * will be returned regardless of where the point is.
     109             :  */
     110             : fixed MaxDistanceToSquare(const CFixedVector2D& point,
     111             :     const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize,
     112             :     bool countInsideAsZero = false);
     113             : 
     114             : /**
     115             :  * Return the greatest straight line distance between two squares.
     116             :  */
     117             : fixed MaxDistanceSquareToSquare(const CFixedVector2D& relativePos,
     118             :     const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1,
     119             :     const CFixedVector2D& u2, const CFixedVector2D& v2, const CFixedVector2D& halfSize2);
     120             : 
     121             : bool TestRaySquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize);
     122             : 
     123             : bool TestRayAASquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& halfSize);
     124             : 
     125             : bool TestSquareSquare(
     126             :         const CFixedVector2D& c0, const CFixedVector2D& u0, const CFixedVector2D& v0, const CFixedVector2D& halfSize0,
     127             :         const CFixedVector2D& c1, const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1);
     128             : 
     129             : /**
     130             :  * Used in Footprint when spawning units:
     131             :  * Given a grid point (x, y) on the rectangle [-x_max, x_max] x [-y_max, y_max],
     132             :  * this returns the distance travelled in moving from (x_max, 0) to the the point while
     133             :  * walking counter-clockwise along the perimeter of the rectangle.
     134             :  */
     135             : int GetPerimeterDistance(int x_max, int y_max, int x, int y);
     136             : 
     137             : /**
     138             :  * Used in Footprint when spawning units:
     139             :  * This returns the grid point on the rectangle [-x_max, x_max] x [-y_max, y_max]
     140             :  * reached after starting at (x_max, 0) and walking a distance k
     141             :  * counter-clockwise along the perimeter of the rectangle.
     142             :  */
     143             : std::pair<int, int> GetPerimeterCoordinates(int x_max, int y_max, int k);
     144             : 
     145             : /**
     146             :  * Returns the minimum Euclidean distance from the given point to
     147             :  * any point on the given segment.
     148             :  *
     149             :  * @a and @b represents segment's points.
     150             :  *
     151             :  */
     152             : fixed DistanceToSegment(
     153             :     const CFixedVector2D& point, const CFixedVector2D& a, const CFixedVector2D& b);
     154             : 
     155             : } // namespace Geometry
     156             : 
     157             : #endif // INCLUDED_HELPER_GEOMETRY

Generated by: LCOV version 1.16