| 1 |
// |
| 2 |
// Copyright (c) 2009-2010 Mikko Mononen [email protected] |
| 3 |
// |
| 4 |
// This software is provided 'as-is', without any express or implied |
| 5 |
// warranty. In no event will the authors be held liable for any damages |
| 6 |
// arising from the use of this software. |
| 7 |
// Permission is granted to anyone to use this software for any purpose, |
| 8 |
// including commercial applications, and to alter it and redistribute it |
| 9 |
// freely, subject to the following restrictions: |
| 10 |
// 1. The origin of this software must not be misrepresented; you must not |
| 11 |
// claim that you wrote the original software. If you use this software |
| 12 |
// in a product, an acknowledgment in the product documentation would be |
| 13 |
// appreciated but is not required. |
| 14 |
// 2. Altered source versions must be plainly marked as such, and must not be |
| 15 |
// misrepresented as being the original software. |
| 16 |
// 3. This notice may not be removed or altered from any source distribution. |
| 17 |
// |
| 18 |
|
| 19 |
#include "Recast.h" |
| 20 |
#include "RecastAssert.h" |
| 21 |
|
| 22 |
#include <stdlib.h> |
| 23 |
|
| •24 |
namespace |
| 25 |
{ |
| 26 |
const int MAX_HEIGHTFIELD_HEIGHT = 0xffff; // TODO (graham): Move this to a more visible constant and update usages. |
| 27 |
} |
| 28 |
|
| 29 |
void rcFilterLowHangingWalkableObstacles(rcContext* context, const int walkableClimb, rcHeightfield& heightfield) |
| 30 |
{ |
| 31 |
rcAssert(context); |
| 32 |
|
| 33 |
rcScopedTimer timer(context, RC_TIMER_FILTER_LOW_OBSTACLES); |
| 34 |
|
| 35 |
const int xSize = heightfield.width; |
| 36 |
const int zSize = heightfield.height; |
| 37 |
|
| 38 |
for (int z = 0; z < zSize; ++z) |
| 39 |
{ |
| 40 |
for (int x = 0; x < xSize; ++x) |
| 41 |
{ |
| 42 |
rcSpan* previousSpan = NULL; |
| 43 |
bool previousWasWalkable = false; |
| •44 |
unsigned char previousAreaID = RC_NULL_AREA; |
| 45 |
|
| •46 |
// For each span in the column... |
| 47 |
for (rcSpan* span = heightfield.spans[x + z * xSize]; span != NULL; previousSpan = span, span = span->next) |
| 48 |
{ |
| 49 |
const bool walkable = span->area != RC_NULL_AREA; |
| •50 |
|
| 51 |
// If current span is not walkable, but there is walkable span just below it and the height difference |
| 52 |
// is small enough for the agent to walk over, mark the current span as walkable too. |
| 53 |
if (!walkable && previousWasWalkable && (int)span->smax - (int)previousSpan->smax <= walkableClimb) |
| 54 |
{ |
| •55 |
span->area = previousAreaID; |
| 56 |
} |
| •57 |
|
| 58 |
// Copy the original walkable value regardless of whether we changed it. |
| 59 |
// This prevents multiple consecutive non-walkable spans from being erroneously marked as walkable. |
| 60 |
previousWasWalkable = walkable; |
| •61 |
previousAreaID = span->area; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| •67 |
void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int walkableClimb, rcHeightfield& heightfield) |
| 68 |
{ |
| 69 |
rcAssert(context); |
| 70 |
|
| 71 |
rcScopedTimer timer(context, RC_TIMER_FILTER_BORDER); |
| 72 |
|
| 73 |
const int xSize = heightfield.width; |
| 74 |
const int zSize = heightfield.height; |
| 75 |
|
| •76 |
// Mark spans that are adjacent to a ledge as unwalkable.. |
| 77 |
for (int z = 0; z < zSize; ++z) |
| 78 |
{ |
| 79 |
for (int x = 0; x < xSize; ++x) |
| 80 |
{ |
| 81 |
for (rcSpan* span = heightfield.spans[x + z * xSize]; span; span = span->next) |
| 82 |
{ |
| •83 |
// Skip non-walkable spans. |
| 84 |
if (span->area == RC_NULL_AREA) |
| 85 |
{ |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| •89 |
const int floor = (int)(span->smax); |
| •90 |
const int ceiling = span->next ? (int)(span->next->smin) : MAX_HEIGHTFIELD_HEIGHT; |
| 91 |
|
| •92 |
// The difference between this walkable area and the lowest neighbor walkable area. |
| •93 |
// This is the difference between the current span and all neighbor spans that have |
| 94 |
// enough space for an agent to move between, but not accounting at all for surface slope. |
| 95 |
int lowestNeighborFloorDifference = MAX_HEIGHTFIELD_HEIGHT; |
| 96 |
|
| 97 |
// Min and max height of accessible neighbours. |
| •98 |
int lowestTraversableNeighborFloor = span->smax; |
| 99 |
int highestTraversableNeighborFloor = span->smax; |
| 100 |
|
| 101 |
for (int direction = 0; direction < 4; ++direction) |
| •102 |
{ |
| •103 |
const int neighborX = x + rcGetDirOffsetX(direction); |
| •104 |
const int neighborZ = z + rcGetDirOffsetY(direction); |
| 105 |
|
| •106 |
// Skip neighbours which are out of bounds. |
| •107 |
if (neighborX < 0 || neighborZ < 0 || neighborX >= xSize || neighborZ >= zSize) |
| 108 |
{ |
| •109 |
lowestNeighborFloorDifference = -walkableClimb - 1; |
| 110 |
break; |
| •111 |
} |
| •112 |
|
| •113 |
const rcSpan* neighborSpan = heightfield.spans[neighborX + neighborZ * xSize]; |
| 114 |
|
| •115 |
// The most we can step down to the neighbor is the walkableClimb distance. |
| 116 |
// Start with the area under the neighbor span |
| •117 |
int neighborCeiling = neighborSpan ? (int)neighborSpan->smin : MAX_HEIGHTFIELD_HEIGHT; |
| 118 |
|
| 119 |
// Skip neighbour if the gap between the spans is too small. |
| •120 |
if (rcMin(ceiling, neighborCeiling) - floor >= walkableHeight) |
| 121 |
{ |
| •122 |
lowestNeighborFloorDifference = (-walkableClimb - 1); |
| 123 |
break; |
| •124 |
} |
| •125 |
|
| •126 |
// For each span in the neighboring column... |
| 127 |
for (; neighborSpan != NULL; neighborSpan = neighborSpan->next) |
| •128 |
{ |
| •129 |
const int neighborFloor = (int)neighborSpan->smax; |
| •130 |
neighborCeiling = neighborSpan->next ? (int)neighborSpan->next->smin : MAX_HEIGHTFIELD_HEIGHT; |
| 131 |
|
| 132 |
// Only consider neighboring areas that have enough overlap to be potentially traversable. |
| 133 |
if (rcMin(ceiling, neighborCeiling) - rcMax(floor, neighborFloor) < walkableHeight) |
| •134 |
{ |
| 135 |
// No space to traverse between them. |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| •139 |
const int neighborFloorDifference = neighborFloor - floor; |
| 140 |
lowestNeighborFloorDifference = rcMin(lowestNeighborFloorDifference, neighborFloorDifference); |
| 141 |
|
| 142 |
// Find min/max accessible neighbor height. |
| 143 |
// Only consider neighbors that are at most walkableClimb away. |
| 144 |
if (rcAbs(neighborFloorDifference) <= walkableClimb) |
| 145 |
{ |
| 146 |
// There is space to move to the neighbor cell and the slope isn't too much. |
| •147 |
lowestTraversableNeighborFloor = rcMin(lowestTraversableNeighborFloor, neighborFloor); |
| 148 |
highestTraversableNeighborFloor = rcMax(highestTraversableNeighborFloor, neighborFloor); |
| 149 |
} |
| 150 |
else if (neighborFloorDifference < -walkableClimb) |
| 151 |
{ |
| •152 |
// We already know this will be considered a ledge span so we can early-out |
| 153 |
break; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| •158 |
// The current span is close to a ledge if the magnitude of the drop to any neighbour span is greater than the walkableClimb distance. |
| 159 |
// That is, there is a gap that is large enough to let an agent move between them, but the drop (surface slope) is too large to allow it. |
| 160 |
// (If this is the case, then biggestNeighborStepDown will be negative, so compare against the negative walkableClimb as a means of checking |
| 161 |
// the magnitude of the delta) |
| 162 |
if (lowestNeighborFloorDifference < -walkableClimb) |
| 163 |
{ |
| 164 |
span->area = RC_NULL_AREA; |
| 165 |
} |
| •166 |
// If the difference between all neighbor floors is too large, this is a steep slope, so mark the span as an unwalkable ledge. |
| 167 |
else if (highestTraversableNeighborFloor - lowestTraversableNeighborFloor > walkableClimb) |
| 168 |
{ |
| 169 |
span->area = RC_NULL_AREA; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
void rcFilterWalkableLowHeightSpans(rcContext* context, const int walkableHeight, rcHeightfield& heightfield) |
| 177 |
{ |
| 178 |
rcAssert(context); |
| 179 |
rcScopedTimer timer(context, RC_TIMER_FILTER_WALKABLE); |
| •180 |
|
| 181 |
const int xSize = heightfield.width; |
| 182 |
const int zSize = heightfield.height; |
| •183 |
|
| 184 |
// Remove walkable flag from spans which do not have enough |
| 185 |
// space above them for the agent to stand there. |
| 186 |
for (int z = 0; z < zSize; ++z) |
| 187 |
{ |
| 188 |
for (int x = 0; x < xSize; ++x) |
| 189 |
{ |
| 190 |
for (rcSpan* span = heightfield.spans[x + z*xSize]; span; span = span->next) |
| 191 |
{ |
| •192 |
const int floor = (int)(span->smax); |
| •193 |
const int ceiling = span->next ? (int)(span->next->smin) : MAX_HEIGHTFIELD_HEIGHT; |
| 194 |
if (ceiling - floor < walkableHeight) |
| 195 |
{ |
| 196 |
span->area = RC_NULL_AREA; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |