LCOV - code coverage report
Current view: top level - source/simulation2/helpers - Rasterize.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 39 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 1 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             : #include "precompiled.h"
      19             : 
      20             : #include "Rasterize.h"
      21             : 
      22             : #include "maths/Fixed.h"
      23             : #include "maths/FixedVector2D.h"
      24             : #include "simulation2/helpers/Geometry.h"
      25             : #include "simulation2/helpers/Pathfinding.h"
      26             : 
      27             : #include <limits>
      28             : 
      29           0 : void SimRasterize::RasterizeRectWithClearance(Spans& spans,
      30             :     const ICmpObstructionManager::ObstructionSquare& shape,
      31             :     entity_pos_t clearance, entity_pos_t cellSize)
      32             : {
      33             :     // A long-standing issue with the pathfinding has been that the long-range one
      34             :     // uses a AA navcell grid, while the short-range uses an accurate vector representation.
      35             :     // This means we could get paths accepted by one but not both pathfinders.
      36             :     // Since the new pathfinder, the short-range pathfinder's representation was usually
      37             :     // encompassing the rasterisation of the long-range one for a building.
      38             :     // This means that we could never get quite as close as the long-range pathfinder wanted.
      39             :     // This could mean units tried going through impassable paths.
      40             :     // To fix this, we need to make sure that the short-range pathfinder is always mostly
      41             :     // included in the rasterisation. The easiest way is to rasterise more, thus this variable
      42             :     // Since this is a very complicated subject, check out logs on 31/10/2015 for more detailled info.
      43             :     // or ask wraitii about it.
      44             :     // If the short-range pathfinder is sufficiently changed, this could become unnecessary and thus removed.
      45             :     // A side effect is that the basic clearance has been set to 0.8, so removing this constant should be done
      46             :     // in parallel with setting clearance back to 1 for the default passability class (though this isn't strictly necessary).
      47             :     // Also: the code detecting foundation obstruction in CcmpObstructionManager had to be changed similarly.
      48           0 :     entity_pos_t rasterClearance = clearance + Pathfinding::CLEARANCE_EXTENSION_RADIUS;
      49             : 
      50             :     // Get the bounds of cells that might possibly be within the shape
      51             :     // (We'll then test each of those cells more precisely)
      52           0 :     CFixedVector2D shapeHalfSize(CFixedVector2D(shape.hw, shape.hh));
      53           0 :     CFixedVector2D halfSize(shape.hw + rasterClearance, shape.hh + rasterClearance);
      54           0 :     CFixedVector2D halfBound = Geometry::GetHalfBoundingBox(shape.u, shape.v, halfSize);
      55           0 :     std::int16_t i0 = ((shape.x - halfBound.X) / cellSize).ToInt_RoundToNegInfinity();
      56           0 :     std::int16_t j0 = ((shape.z - halfBound.Y) / cellSize).ToInt_RoundToNegInfinity();
      57           0 :     std::int16_t i1 = ((shape.x + halfBound.X) / cellSize).ToInt_RoundToInfinity();
      58           0 :     std::int16_t j1 = ((shape.z + halfBound.Y) / cellSize).ToInt_RoundToInfinity();
      59             : 
      60           0 :     if (j1 <= j0)
      61           0 :         return; // empty bounds - this shouldn't happen
      62             : 
      63             : 
      64           0 :     rasterClearance = rasterClearance.Multiply(rasterClearance);
      65             : 
      66           0 :     spans.reserve(j1 - j0);
      67             : 
      68           0 :     for (std::int16_t j = j0; j < j1; ++j)
      69             :     {
      70             :         // Find the min/max range of cells that are strictly inside the square+rasterClearance.
      71             :         // (Since the square+rasterClearance is a convex shape, we can just test each
      72             :         // corner of each cell is inside the shape.)
      73             :         // When looping on i, if the previous cell was inside, no need to check again the left corners.
      74             :         // and we can stop the loop when exiting the shape.
      75             :         // Futhermore if one of the right corners of a cell is outside, no need to check the following cell
      76             :         std::int16_t spanI0 = std::numeric_limits<std::int16_t>::max();
      77             :         std::int16_t spanI1 = std::numeric_limits<std::int16_t>::min();
      78             :         bool previousInside = false;
      79             :         bool skipNextCell = false;
      80           0 :         for (std::int16_t i = i0; i < i1; ++i)
      81             :         {
      82           0 :             if (skipNextCell)
      83             :             {
      84           0 :                 skipNextCell = false;
      85           0 :                 continue;
      86             :             }
      87             : 
      88           0 :             if (Geometry::DistanceToSquareSquared(CFixedVector2D(cellSize*(i+1)-shape.x, cellSize*j-shape.z),
      89           0 :                                     shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
      90             :             {
      91           0 :                 if (previousInside)
      92             :                     break;
      93           0 :                 skipNextCell = true;
      94           0 :                 continue;
      95             :             }
      96             : 
      97           0 :             if (Geometry::DistanceToSquareSquared(CFixedVector2D(cellSize*(i+1)-shape.x, cellSize*(j+1)-shape.z),
      98           0 :                                     shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
      99             :             {
     100           0 :                 if (previousInside)
     101             :                     break;
     102           0 :                 skipNextCell = true;
     103           0 :                 continue;
     104             :             }
     105             : 
     106           0 :             if (!previousInside)
     107             :             {
     108           0 :                 if (Geometry::DistanceToSquareSquared(CFixedVector2D(cellSize*i-shape.x, cellSize*j-shape.z),
     109           0 :                                     shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
     110           0 :                     continue;
     111             : 
     112           0 :                 if (Geometry::DistanceToSquareSquared(CFixedVector2D(cellSize*i-shape.x, cellSize*(j+1)-shape.z),
     113           0 :                                     shape.u, shape.v, shapeHalfSize, true) > rasterClearance)
     114           0 :                     continue;
     115             : 
     116             :                 previousInside = true;
     117             :                 spanI0 = i;
     118             :             }
     119             : 
     120           0 :             spanI1 = i+1;
     121             :         }
     122             : 
     123             :         // Add non-empty spans onto the list
     124           0 :         if (spanI0 < spanI1)
     125             :         {
     126           0 :             Span span = { spanI0, spanI1, j };
     127           0 :             spans.push_back(span);
     128             :         }
     129             :     }
     130             : }

Generated by: LCOV version 1.16