From 3e94c3b6fcb5361bc30e87cacfa0ff46bde37a2c Mon Sep 17 00:00:00 2001 From: Cupcake <1357715449@qq.com> Date: Mon, 1 Jan 2024 03:27:07 +0800 Subject: [PATCH 22/36] Code cleanup and small optimizations in RecastFilter.cpp rcFilterLedgeSpans (#672) * Code cleanup and minor refactor in RecastFilter.cpp rcFilterLedgeSpans Because span.smax is always > 0, bot > 0 as well, and (-walkableClimb - bot) is always < -walkableClimb. Furthermore, as long as minNeighborHeight < -walkableClimb' at least once, there is no need to continue the traversal. * Code cleanup and minor refactor in RecastFilter.cpp rcFilterLedgeSpans Because span.smax is always > 0, bot > 0 as well, and (-walkableClimb - bot) is always < -walkableClimb. Furthermore, as long as minNeighborHeight < -walkableClimb' at least once, there is no need to continue the traversal. * Update RecastFilter.cpp Revise Comment --- Recast/Source/RecastFilter.cpp | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/Recast/Source/RecastFilter.cpp b/Recast/Source/RecastFilter.cpp index b5adba4..8f3414b 100644 --- a/Recast/Source/RecastFilter.cpp +++ b/Recast/Source/RecastFilter.cpp @@ -96,42 +96,47 @@ void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int for (int direction = 0; direction < 4; ++direction) { int dx = x + rcGetDirOffsetX(direction); - int dy = z + rcGetDirOffsetY(direction); + int dz = z + rcGetDirOffsetY(direction); // Skip neighbours which are out of bounds. - if (dx < 0 || dy < 0 || dx >= xSize || dy >= zSize) + if (dx < 0 || dz < 0 || dx >= xSize || dz >= zSize) { - minNeighborHeight = rcMin(minNeighborHeight, -walkableClimb - bot); - continue; + minNeighborHeight = (-walkableClimb - 1) ; + break; } // From minus infinity to the first span. - const rcSpan* neighborSpan = heightfield.spans[dx + dy * xSize]; - int neighborBot = -walkableClimb; + const rcSpan* neighborSpan = heightfield.spans[dx + dz * xSize]; int neighborTop = neighborSpan ? (int)neighborSpan->smin : MAX_HEIGHT; // Skip neighbour if the gap between the spans is too small. - if (rcMin(top, neighborTop) - rcMax(bot, neighborBot) > walkableHeight) + if (rcMin(top, neighborTop) - bot >= walkableHeight) { - minNeighborHeight = rcMin(minNeighborHeight, neighborBot - bot); + minNeighborHeight = (-walkableClimb - 1); + break; } // Rest of the spans. - for (neighborSpan = heightfield.spans[dx + dy * xSize]; neighborSpan; neighborSpan = neighborSpan->next) + for (neighborSpan = heightfield.spans[dx + dz * xSize]; neighborSpan; neighborSpan = neighborSpan->next) { - neighborBot = (int)neighborSpan->smax; + int neighborBot = (int)neighborSpan->smax; neighborTop = neighborSpan->next ? (int)neighborSpan->next->smin : MAX_HEIGHT; // Skip neighbour if the gap between the spans is too small. - if (rcMin(top, neighborTop) - rcMax(bot, neighborBot) > walkableHeight) + if (rcMin(top, neighborTop) - rcMax(bot, neighborBot) >= walkableHeight) { - minNeighborHeight = rcMin(minNeighborHeight, neighborBot - bot); + int accessibleNeighbourHeight = neighborBot - bot; + minNeighborHeight = rcMin(minNeighborHeight, accessibleNeighbourHeight); // Find min/max accessible neighbour height. - if (rcAbs(neighborBot - bot) <= walkableClimb) + if (rcAbs(accessibleNeighbourHeight) <= walkableClimb) { if (neighborBot < accessibleNeighborMinHeight) accessibleNeighborMinHeight = neighborBot; if (neighborBot > accessibleNeighborMaxHeight) accessibleNeighborMaxHeight = neighborBot; } + else if (accessibleNeighbourHeight < -walkableClimb) + { + break; + } } } -- 2.43.0