DotRecastNetSim/src/DotRecast.Recast/RecastArea.cs

681 lines
29 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
2023-03-15 17:00:29 +03:00
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
2023-03-14 08:02:43 +03:00
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System;
2023-03-28 18:03:33 +03:00
using DotRecast.Core;
2023-03-14 08:02:43 +03:00
2023-03-16 19:09:10 +03:00
namespace DotRecast.Recast
{
using static RcConstants;
2023-03-16 19:09:10 +03:00
public static class RecastArea
2023-03-14 08:02:43 +03:00
{
/// Erodes the walkable area within the heightfield by the specified radius.
///
/// Basically, any spans that are closer to a boundary or obstruction than the specified radius
/// are marked as un-walkable.
2023-03-16 19:48:49 +03:00
///
/// This method is usually called immediately after the heightfield has been built.
///
2023-03-16 19:48:49 +03:00
/// @see rcCompactHeightfield, rcBuildCompactHeightfield, rcConfig::walkableRadius
/// @ingroup recast
///
/// @param[in,out] context The build context to use during the operation.
/// @param[in] erosionRadius The radius of erosion. [Limits: 0 < value < 255] [Units: vx]
/// @param[in,out] compactHeightfield The populated compact heightfield to erode.
/// @returns True if the operation completed successfully.
public static void ErodeWalkableArea(RcTelemetry context, int erosionRadius, RcCompactHeightfield compactHeightfield)
2023-03-14 08:02:43 +03:00
{
int xSize = compactHeightfield.width;
int zSize = compactHeightfield.height;
int zStride = xSize; // For readability
using var timer = context.ScopedTimer(RcTimerLabel.RC_TIMER_ERODE_AREA);
int[] distanceToBoundary = new int[compactHeightfield.spanCount];
Array.Fill(distanceToBoundary, 255);
2023-03-16 19:48:49 +03:00
// Mark boundary cells.
for (int z = 0; z < zSize; ++z)
2023-03-14 08:02:43 +03:00
{
for (int x = 0; x < xSize; ++x)
2023-03-14 08:02:43 +03:00
{
RcCompactCell cell = compactHeightfield.cells[x + z * zStride];
for (int spanIndex = cell.index, maxSpanIndex = cell.index + cell.count; spanIndex < maxSpanIndex; ++spanIndex)
2023-03-14 08:02:43 +03:00
{
if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
2023-03-14 08:02:43 +03:00
{
distanceToBoundary[spanIndex] = 0;
2023-03-16 19:48:49 +03:00
}
else
{
RcCompactSpan span = compactHeightfield.spans[spanIndex];
// Check that there is a non-null adjacent span in each of the 4 cardinal directions.
int neighborCount = 0;
for (int direction = 0; direction < 4; ++direction)
2023-03-14 08:02:43 +03:00
{
int neighborConnection = RecastCommon.GetCon(span, direction);
if (neighborConnection == RC_NOT_CONNECTED)
2023-03-14 08:02:43 +03:00
{
break;
2023-03-14 08:02:43 +03:00
}
int neighborX = x + RecastCommon.GetDirOffsetX(direction);
int neighborZ = z + RecastCommon.GetDirOffsetY(direction);
int neighborSpanIndex = compactHeightfield.cells[neighborX + neighborZ * zStride].index + RecastCommon.GetCon(span, direction);
if (compactHeightfield.areas[neighborSpanIndex] == RC_NULL_AREA)
{
break;
}
neighborCount++;
2023-03-14 08:02:43 +03:00
}
// At least one missing neighbour, so this is a boundary cell.
if (neighborCount != 4)
{
distanceToBoundary[spanIndex] = 0;
}
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
}
}
int newDistance;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
// Pass 1
for (int z = 0; z < zSize; ++z)
2023-03-14 08:02:43 +03:00
{
for (int x = 0; x < xSize; ++x)
2023-03-14 08:02:43 +03:00
{
RcCompactCell cell = compactHeightfield.cells[x + z * zStride];
int maxSpanIndex = cell.index + cell.count;
for (int spanIndex = cell.index; spanIndex < maxSpanIndex; ++spanIndex)
2023-03-14 08:02:43 +03:00
{
RcCompactSpan span = compactHeightfield.spans[spanIndex];
2023-03-16 19:48:49 +03:00
if (RecastCommon.GetCon(span, 0) != RC_NOT_CONNECTED)
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
// (-1,0)
int aX = x + RecastCommon.GetDirOffsetX(0);
int aY = z + RecastCommon.GetDirOffsetY(0);
int aIndex = compactHeightfield.cells[aX + aY * xSize].index + RecastCommon.GetCon(span, 0);
RcCompactSpan aSpan = compactHeightfield.spans[aIndex];
newDistance = Math.Min(distanceToBoundary[aIndex] + 2, 255);
if (newDistance < distanceToBoundary[spanIndex])
{
distanceToBoundary[spanIndex] = newDistance;
}
2023-03-16 19:48:49 +03:00
// (-1,-1)
if (RecastCommon.GetCon(aSpan, 3) != RC_NOT_CONNECTED)
2023-03-16 19:48:49 +03:00
{
int bX = aX + RecastCommon.GetDirOffsetX(3);
int bY = aY + RecastCommon.GetDirOffsetY(3);
int bIndex = compactHeightfield.cells[bX + bY * xSize].index + RecastCommon.GetCon(aSpan, 3);
newDistance = Math.Min(distanceToBoundary[bIndex] + 3, 255);
if (newDistance < distanceToBoundary[spanIndex])
{
distanceToBoundary[spanIndex] = newDistance;
}
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
if (RecastCommon.GetCon(span, 3) != RC_NOT_CONNECTED)
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
// (0,-1)
int aX = x + RecastCommon.GetDirOffsetX(3);
int aY = z + RecastCommon.GetDirOffsetY(3);
int aIndex = compactHeightfield.cells[aX + aY * xSize].index + RecastCommon.GetCon(span, 3);
RcCompactSpan aSpan = compactHeightfield.spans[aIndex];
newDistance = Math.Min(distanceToBoundary[aIndex] + 2, 255);
if (newDistance < distanceToBoundary[spanIndex])
{
distanceToBoundary[spanIndex] = newDistance;
}
2023-03-16 19:48:49 +03:00
// (1,-1)
if (RecastCommon.GetCon(aSpan, 2) != RC_NOT_CONNECTED)
2023-03-16 19:48:49 +03:00
{
int bX = aX + RecastCommon.GetDirOffsetX(2);
int bY = aY + RecastCommon.GetDirOffsetY(2);
int bIndex = compactHeightfield.cells[bX + bY * xSize].index + RecastCommon.GetCon(aSpan, 2);
newDistance = Math.Min(distanceToBoundary[bIndex] + 3, 255);
if (newDistance < distanceToBoundary[spanIndex])
{
distanceToBoundary[spanIndex] = newDistance;
}
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
}
}
}
2023-03-16 19:48:49 +03:00
// Pass 2
for (int z = zSize - 1; z >= 0; --z)
2023-03-14 08:02:43 +03:00
{
for (int x = xSize - 1; x >= 0; --x)
2023-03-14 08:02:43 +03:00
{
RcCompactCell cell = compactHeightfield.cells[x + z * zStride];
int maxSpanIndex = cell.index + cell.count;
for (int i = cell.index; i < maxSpanIndex; ++i)
2023-03-14 08:02:43 +03:00
{
RcCompactSpan span = compactHeightfield.spans[i];
2023-03-16 19:48:49 +03:00
if (RecastCommon.GetCon(span, 2) != RC_NOT_CONNECTED)
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
// (1,0)
int aX = x + RecastCommon.GetDirOffsetX(2);
int aY = z + RecastCommon.GetDirOffsetY(2);
int aIndex = compactHeightfield.cells[aX + aY * xSize].index + RecastCommon.GetCon(span, 2);
RcCompactSpan aSpan = compactHeightfield.spans[aIndex];
newDistance = Math.Min(distanceToBoundary[aIndex] + 2, 255);
if (newDistance < distanceToBoundary[i])
{
distanceToBoundary[i] = newDistance;
}
2023-03-16 19:48:49 +03:00
// (1,1)
if (RecastCommon.GetCon(aSpan, 1) != RC_NOT_CONNECTED)
2023-03-16 19:48:49 +03:00
{
int bX = aX + RecastCommon.GetDirOffsetX(1);
int bY = aY + RecastCommon.GetDirOffsetY(1);
int bIndex = compactHeightfield.cells[bX + bY * xSize].index + RecastCommon.GetCon(aSpan, 1);
newDistance = Math.Min(distanceToBoundary[bIndex] + 3, 255);
if (newDistance < distanceToBoundary[i])
{
distanceToBoundary[i] = newDistance;
}
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
if (RecastCommon.GetCon(span, 1) != RC_NOT_CONNECTED)
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
// (0,1)
int aX = x + RecastCommon.GetDirOffsetX(1);
int aY = z + RecastCommon.GetDirOffsetY(1);
int aIndex = compactHeightfield.cells[aX + aY * xSize].index + RecastCommon.GetCon(span, 1);
RcCompactSpan aSpan = compactHeightfield.spans[aIndex];
newDistance = Math.Min(distanceToBoundary[aIndex] + 2, 255);
if (newDistance < distanceToBoundary[i])
{
distanceToBoundary[i] = newDistance;
}
2023-03-16 19:48:49 +03:00
// (-1,1)
if (RecastCommon.GetCon(aSpan, 0) != RC_NOT_CONNECTED)
2023-03-16 19:48:49 +03:00
{
int bX = aX + RecastCommon.GetDirOffsetX(0);
int bY = aY + RecastCommon.GetDirOffsetY(0);
int bIndex = compactHeightfield.cells[bX + bY * xSize].index + RecastCommon.GetCon(aSpan, 0);
newDistance = Math.Min(distanceToBoundary[bIndex] + 3, 255);
if (newDistance < distanceToBoundary[i])
{
distanceToBoundary[i] = newDistance;
}
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
}
}
}
int minBoundaryDistance = erosionRadius * 2;
for (int spanIndex = 0; spanIndex < compactHeightfield.spanCount; ++spanIndex)
{
if (distanceToBoundary[spanIndex] < minBoundaryDistance)
{
compactHeightfield.areas[spanIndex] = RC_NULL_AREA;
}
}
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
/// Applies a median filter to walkable area types (based on area id), removing noise.
///
2023-03-16 19:48:49 +03:00
/// This filter is usually applied after applying area id's using functions
/// such as #rcMarkBoxArea, #rcMarkConvexPolyArea, and #rcMarkCylinderArea.
///
2023-03-16 19:48:49 +03:00
/// @see rcCompactHeightfield
/// @ingroup recast
///
/// @param[in,out] context The build context to use during the operation.
/// @param[in,out] compactHeightfield A populated compact heightfield.
/// @returns True if the operation completed successfully.
public static bool MedianFilterWalkableArea(RcTelemetry context, RcCompactHeightfield compactHeightfield)
2023-03-16 19:48:49 +03:00
{
int xSize = compactHeightfield.width;
int zSize = compactHeightfield.height;
int zStride = xSize; // For readability
2023-03-14 08:02:43 +03:00
using var timer = context.ScopedTimer(RcTimerLabel.RC_TIMER_MEDIAN_AREA);
2023-03-14 08:02:43 +03:00
int[] areas = new int[compactHeightfield.spanCount];
2023-03-14 08:02:43 +03:00
for (int z = 0; z < zSize; ++z)
2023-03-14 08:02:43 +03:00
{
for (int x = 0; x < xSize; ++x)
2023-03-14 08:02:43 +03:00
{
RcCompactCell cell = compactHeightfield.cells[x + z * zStride];
int maxSpanIndex = cell.index + cell.count;
for (int spanIndex = cell.index; spanIndex < maxSpanIndex; ++spanIndex)
2023-03-14 08:02:43 +03:00
{
RcCompactSpan span = compactHeightfield.spans[spanIndex];
if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
2023-03-16 19:48:49 +03:00
{
areas[spanIndex] = compactHeightfield.areas[spanIndex];
2023-03-16 19:48:49 +03:00
continue;
}
2023-03-14 08:02:43 +03:00
int[] neighborAreas = new int[9];
for (int neighborIndex = 0; neighborIndex < 9; ++neighborIndex)
{
neighborAreas[neighborIndex] = compactHeightfield.areas[spanIndex];
}
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
for (int dir = 0; dir < 4; ++dir)
2023-03-14 08:02:43 +03:00
{
if (RecastCommon.GetCon(span, dir) == RC_NOT_CONNECTED)
2023-03-14 08:02:43 +03:00
{
continue;
}
int aX = x + RecastCommon.GetDirOffsetX(dir);
int aZ = z + RecastCommon.GetDirOffsetY(dir);
int aIndex = compactHeightfield.cells[aX + aZ * zStride].index + RecastCommon.GetCon(span, dir);
if (compactHeightfield.areas[aIndex] != RC_NULL_AREA)
{
neighborAreas[dir * 2 + 0] = compactHeightfield.areas[aIndex];
}
RcCompactSpan aSpan = compactHeightfield.spans[aIndex];
int dir2 = (dir + 1) & 0x3;
int neighborConnection2 = RecastCommon.GetCon(aSpan, dir2);
if (neighborConnection2 != RC_NOT_CONNECTED)
{
int bX = aX + RecastCommon.GetDirOffsetX(dir2);
int bZ = aZ + RecastCommon.GetDirOffsetY(dir2);
int bIndex = compactHeightfield.cells[bX + bZ * zStride].index + RecastCommon.GetCon(aSpan, dir2);
if (compactHeightfield.areas[bIndex] != RC_NULL_AREA)
2023-03-16 19:48:49 +03:00
{
neighborAreas[dir * 2 + 1] = compactHeightfield.areas[bIndex];
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
}
2023-07-25 09:44:57 +03:00
//Array.Sort(neighborAreas);
neighborAreas.InsertSort();
areas[spanIndex] = neighborAreas[4];
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
}
compactHeightfield.areas = areas;
2023-03-14 08:02:43 +03:00
2023-03-16 19:48:49 +03:00
return true;
}
2023-03-14 08:02:43 +03:00
/// Applies an area id to all spans within the specified bounding box. (AABB)
///
2023-03-16 19:48:49 +03:00
/// @see rcCompactHeightfield, rcMedianFilterWalkableArea
/// @ingroup recast
///
/// @param[in,out] context The build context to use during the operation.
/// @param[in] boxMinBounds The minimum extents of the bounding box. [(x, y, z)] [Units: wu]
/// @param[in] boxMaxBounds The maximum extents of the bounding box. [(x, y, z)] [Units: wu]
/// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
/// @param[in,out] compactHeightfield A populated compact heightfield.
public static void MarkBoxArea(RcTelemetry context, float[] boxMinBounds, float[] boxMaxBounds, RcAreaModification areaId, RcCompactHeightfield compactHeightfield)
2023-03-14 08:02:43 +03:00
{
using var timer = context.ScopedTimer(RcTimerLabel.RC_TIMER_MARK_BOX_AREA);
2023-03-16 19:48:49 +03:00
int xSize = compactHeightfield.width;
int zSize = compactHeightfield.height;
int zStride = xSize; // For readability
2023-03-16 19:48:49 +03:00
// Find the footprint of the box area in grid cell coordinates.
int minX = (int)((boxMinBounds[0] - compactHeightfield.bmin.x) / compactHeightfield.cs);
int minY = (int)((boxMinBounds[1] - compactHeightfield.bmin.y) / compactHeightfield.ch);
int minZ = (int)((boxMinBounds[2] - compactHeightfield.bmin.z) / compactHeightfield.cs);
int maxX = (int)((boxMaxBounds[0] - compactHeightfield.bmin.x) / compactHeightfield.cs);
int maxY = (int)((boxMaxBounds[1] - compactHeightfield.bmin.y) / compactHeightfield.ch);
int maxZ = (int)((boxMaxBounds[2] - compactHeightfield.bmin.z) / compactHeightfield.cs);
if (maxX < 0)
{
2023-03-16 19:48:49 +03:00
return;
}
if (minX >= xSize)
{
2023-03-16 19:48:49 +03:00
return;
}
if (maxZ < 0)
{
2023-03-16 19:48:49 +03:00
return;
}
if (minZ >= zSize)
{
2023-03-16 19:48:49 +03:00
return;
}
2023-03-16 19:48:49 +03:00
if (minX < 0)
{
minX = 0;
}
2023-03-16 19:48:49 +03:00
if (maxX >= xSize)
2023-03-14 08:02:43 +03:00
{
maxX = xSize - 1;
}
if (minZ < 0)
{
minZ = 0;
}
if (maxZ >= zSize)
{
maxZ = zSize - 1;
}
for (int z = minZ; z <= maxZ; ++z)
{
for (int x = minX; x <= maxX; ++x)
2023-03-14 08:02:43 +03:00
{
RcCompactCell cell = compactHeightfield.cells[x + z * zStride];
int maxSpanIndex = cell.index + cell.count;
for (int spanIndex = cell.index; spanIndex < maxSpanIndex; ++spanIndex)
2023-03-14 08:02:43 +03:00
{
RcCompactSpan span = compactHeightfield.spans[spanIndex];
// Skip if the span is outside the box extents.
if (span.y < minY || span.y > maxY)
{
continue;
}
// Skip if the span has been removed.
if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
2023-03-16 19:48:49 +03:00
{
continue;
2023-03-16 19:48:49 +03:00
}
// Mark the span.
compactHeightfield.areas[spanIndex] = areaId.Apply(compactHeightfield.areas[spanIndex]);
2023-03-14 08:02:43 +03:00
}
}
}
}
/// Applies the area id to the all spans within the specified convex polygon.
2023-03-16 19:48:49 +03:00
///
/// The value of spacial parameters are in world units.
///
/// The y-values of the polygon vertices are ignored. So the polygon is effectively
/// projected onto the xz-plane, translated to @p minY, and extruded to @p maxY.
///
2023-03-16 19:48:49 +03:00
/// @see rcCompactHeightfield, rcMedianFilterWalkableArea
/// @ingroup recast
///
/// @param[in,out] context The build context to use during the operation.
/// @param[in] verts The vertices of the polygon [For: (x, y, z) * @p numVerts]
/// @param[in] numVerts The number of vertices in the polygon.
/// @param[in] minY The height of the base of the polygon. [Units: wu]
/// @param[in] maxY The height of the top of the polygon. [Units: wu]
/// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
/// @param[in,out] compactHeightfield A populated compact heightfield.
public static void MarkConvexPolyArea(RcTelemetry context, float[] verts,
float minY, float maxY, RcAreaModification areaId,
RcCompactHeightfield compactHeightfield)
2023-03-14 08:02:43 +03:00
{
using var timer = context.ScopedTimer(RcTimerLabel.RC_TIMER_MARK_CONVEXPOLY_AREA);
int xSize = compactHeightfield.width;
int zSize = compactHeightfield.height;
int zStride = xSize; // For readability
2023-03-16 19:48:49 +03:00
// Compute the bounding box of the polygon
RcVec3f bmin = new RcVec3f();
RcVec3f bmax = new RcVec3f();
RcVec3f.Copy(ref bmin, verts, 0);
RcVec3f.Copy(ref bmax, verts, 0);
2023-03-16 19:48:49 +03:00
for (int i = 3; i < verts.Length; i += 3)
{
2023-07-08 06:20:20 +03:00
bmin.Min(verts, i);
bmax.Max(verts, i);
2023-03-16 19:48:49 +03:00
}
bmin.y = minY;
bmax.y = maxY;
2023-03-16 19:48:49 +03:00
// Compute the grid footprint of the polygon
int minx = (int)((bmin.x - compactHeightfield.bmin.x) / compactHeightfield.cs);
int miny = (int)((bmin.y - compactHeightfield.bmin.y) / compactHeightfield.ch);
int minz = (int)((bmin.z - compactHeightfield.bmin.z) / compactHeightfield.cs);
int maxx = (int)((bmax.x - compactHeightfield.bmin.x) / compactHeightfield.cs);
int maxy = (int)((bmax.y - compactHeightfield.bmin.y) / compactHeightfield.ch);
int maxz = (int)((bmax.z - compactHeightfield.bmin.z) / compactHeightfield.cs);
2023-03-16 19:48:49 +03:00
// Early-out if the polygon lies entirely outside the grid.
2023-03-16 19:48:49 +03:00
if (maxx < 0)
{
2023-03-16 19:48:49 +03:00
return;
}
if (minx >= xSize)
{
2023-03-16 19:48:49 +03:00
return;
}
2023-03-16 19:48:49 +03:00
if (maxz < 0)
{
2023-03-16 19:48:49 +03:00
return;
}
if (minz >= zSize)
{
2023-03-16 19:48:49 +03:00
return;
}
2023-03-16 19:48:49 +03:00
// Clamp the polygon footprint to the grid
2023-03-16 19:48:49 +03:00
if (minx < 0)
{
2023-03-16 19:48:49 +03:00
minx = 0;
}
if (maxx >= xSize)
{
maxx = xSize - 1;
}
2023-03-16 19:48:49 +03:00
if (minz < 0)
{
2023-03-16 19:48:49 +03:00
minz = 0;
}
if (maxz >= zSize)
{
maxz = zSize - 1;
}
2023-03-16 19:48:49 +03:00
// TODO: Optimize.
for (int z = minz; z <= maxz; ++z)
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
for (int x = minx; x <= maxx; ++x)
2023-03-14 08:02:43 +03:00
{
RcCompactCell cell = compactHeightfield.cells[x + z * zStride];
int maxSpanIndex = cell.index + cell.count;
for (int spanIndex = cell.index; spanIndex < maxSpanIndex; ++spanIndex)
2023-03-14 08:02:43 +03:00
{
RcCompactSpan span = compactHeightfield.spans[spanIndex];
// Skip if span is removed.
if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
2023-03-16 19:48:49 +03:00
continue;
// Skip if y extents don't overlap.
if (span.y < miny || span.y > maxy)
2023-03-14 08:02:43 +03:00
{
continue;
}
2023-03-16 19:48:49 +03:00
RcVec3f point = new RcVec3f(
compactHeightfield.bmin.x + (x + 0.5f) * compactHeightfield.cs,
0,
compactHeightfield.bmin.z + (z + 0.5f) * compactHeightfield.cs
);
if (PolyUtils.PointInPoly(verts, point))
{
compactHeightfield.areas[spanIndex] = areaId.Apply(compactHeightfield.areas[spanIndex]);
2023-03-14 08:02:43 +03:00
}
}
}
}
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
/// Applies the area id to all spans within the specified y-axis-aligned cylinder.
///
2023-03-16 19:48:49 +03:00
/// @see rcCompactHeightfield, rcMedianFilterWalkableArea
///
/// @ingroup recast
///
/// @param[in,out] context The build context to use during the operation.
/// @param[in] position The center of the base of the cylinder. [Form: (x, y, z)] [Units: wu]
/// @param[in] radius The radius of the cylinder. [Units: wu] [Limit: > 0]
/// @param[in] height The height of the cylinder. [Units: wu] [Limit: > 0]
/// @param[in] areaId The area id to apply. [Limit: <= #RC_WALKABLE_AREA]
/// @param[in,out] compactHeightfield A populated compact heightfield.
public static void MarkCylinderArea(RcTelemetry context, float[] position, float radius, float height,
RcAreaModification areaId, RcCompactHeightfield compactHeightfield)
2023-03-14 08:02:43 +03:00
{
using var timer = context.ScopedTimer(RcTimerLabel.RC_TIMER_MARK_CYLINDER_AREA);
int xSize = compactHeightfield.width;
int zSize = compactHeightfield.height;
int zStride = xSize; // For readability
// Compute the bounding box of the cylinder
RcVec3f cylinderBBMin = new RcVec3f(
position[0] - radius,
position[1],
position[2] - radius
);
RcVec3f cylinderBBMax = new RcVec3f(
position[0] + radius,
position[1] + height,
position[2] + radius
);
// Compute the grid footprint of the cylinder
int minx = (int)((cylinderBBMin.x - compactHeightfield.bmin.x) / compactHeightfield.cs);
int miny = (int)((cylinderBBMin.y - compactHeightfield.bmin.y) / compactHeightfield.ch);
int minz = (int)((cylinderBBMin.z - compactHeightfield.bmin.z) / compactHeightfield.cs);
int maxx = (int)((cylinderBBMax.x - compactHeightfield.bmin.x) / compactHeightfield.cs);
int maxy = (int)((cylinderBBMax.y - compactHeightfield.bmin.y) / compactHeightfield.ch);
int maxz = (int)((cylinderBBMax.z - compactHeightfield.bmin.z) / compactHeightfield.cs);
// Early-out if the cylinder is completely outside the grid bounds.
2023-03-16 19:48:49 +03:00
if (maxx < 0)
{
2023-03-16 19:48:49 +03:00
return;
}
if (minx >= xSize)
{
2023-03-16 19:48:49 +03:00
return;
}
2023-03-16 19:48:49 +03:00
if (maxz < 0)
{
2023-03-16 19:48:49 +03:00
return;
}
if (minz >= zSize)
{
2023-03-16 19:48:49 +03:00
return;
}
2023-03-16 19:48:49 +03:00
// Clamp the cylinder bounds to the grid.
2023-03-16 19:48:49 +03:00
if (minx < 0)
{
2023-03-16 19:48:49 +03:00
minx = 0;
}
if (maxx >= xSize)
{
maxx = xSize - 1;
}
2023-03-16 19:48:49 +03:00
if (minz < 0)
{
2023-03-16 19:48:49 +03:00
minz = 0;
}
if (maxz >= zSize)
{
maxz = zSize - 1;
}
2023-03-16 19:48:49 +03:00
float radiusSq = radius * radius;
2023-03-16 19:48:49 +03:00
for (int z = minz; z <= maxz; ++z)
2023-03-14 08:02:43 +03:00
{
2023-03-16 19:48:49 +03:00
for (int x = minx; x <= maxx; ++x)
2023-03-14 08:02:43 +03:00
{
RcCompactCell cell = compactHeightfield.cells[x + z * zStride];
int maxSpanIndex = cell.index + cell.count;
float cellX = compactHeightfield.bmin[0] + ((float)x + 0.5f) * compactHeightfield.cs;
float cellZ = compactHeightfield.bmin[2] + ((float)z + 0.5f) * compactHeightfield.cs;
float deltaX = cellX - position[0];
float deltaZ = cellZ - position[2];
// Skip this column if it's too far from the center point of the cylinder.
if (RcMath.Sqr(deltaX) + RcMath.Sqr(deltaZ) >= radiusSq)
2023-03-14 08:02:43 +03:00
{
continue;
}
2023-03-14 08:02:43 +03:00
// Mark all overlapping spans
for (int spanIndex = cell.index; spanIndex < maxSpanIndex; ++spanIndex)
{
RcCompactSpan span = compactHeightfield.spans[spanIndex];
2023-03-16 19:48:49 +03:00
// Skip if span is removed.
if (compactHeightfield.areas[spanIndex] == RC_NULL_AREA)
2023-03-14 08:02:43 +03:00
{
continue;
}
2023-03-16 19:48:49 +03:00
// Mark if y extents overlap.
if (span.y >= miny && span.y <= maxy)
{
compactHeightfield.areas[spanIndex] = areaId.Apply(compactHeightfield.areas[spanIndex]);
2023-03-14 08:02:43 +03:00
}
}
}
}
2023-03-16 19:48:49 +03:00
}
2023-03-14 08:02:43 +03:00
}
2023-07-08 06:20:20 +03:00
}