Debian Codegraph @

debian/patches/0024-Cleanup-filter-code-and-improved-documentation-683.patch

raw
1 From c36a40645afe4327facc376daeb441065bdca058 Mon Sep 17 00:00:00 2001
2 From: Graham Pentheny <[email protected]>
3 Date: Mon, 1 Jan 2024 14:56:47 -0500
4 Subject: [PATCH 24/36] Cleanup filter code and improved documentation (#683)
5
6 This mostly just changes variable names and adds some comments to make the code more clear.
7
8 It also has a few small fixup changes to the unit tests.
9 ---
10 Recast/Include/Recast.h | 3 +-
11 Recast/Source/RecastFilter.cpp | 126 +++++++++++++++-------------
12 Tests/Recast/Tests_RecastFilter.cpp | 56 +------------
13 3 files changed, 75 insertions(+), 110 deletions(-)
14
15 diff --git a/Recast/Include/Recast.h b/Recast/Include/Recast.h
16 index 2104aa5..5b34ea5 100644
17 --- a/Recast/Include/Recast.h
18 +++ b/Recast/Include/Recast.h
19 @@ -1002,7 +1002,8 @@ bool rcRasterizeTriangles(rcContext* context,
20
21 /// Marks non-walkable spans as walkable if their maximum is within @p walkableClimb of the span below them.
22 ///
23 -/// This removes small obstacles that the agent would be able to walk over such as curbs, and also allows agents to move up structures such as stairs.
24 +/// This removes small obstacles and rasterization artifacts that the agent would be able to walk over
25 +/// such as curbs. It also allows agents to move up terraced structures like stairs.
26 ///
27 /// Obstacle spans are marked walkable if: <tt>obstacleSpan.smax - walkableSpan.smax < walkableClimb</tt>
28 ///
29 diff --git a/Recast/Source/RecastFilter.cpp b/Recast/Source/RecastFilter.cpp
30 index 1ecf858..7875040 100644
31 --- a/Recast/Source/RecastFilter.cpp
32 +++ b/Recast/Source/RecastFilter.cpp
33 @@ -41,24 +41,24 @@ void rcFilterLowHangingWalkableObstacles(rcContext* context, const int walkableC
34 {
35 rcSpan* previousSpan = NULL;
36 bool previousWasWalkable = false;
37 - unsigned char previousArea = RC_NULL_AREA;
38 + unsigned char previousAreaID = RC_NULL_AREA;
39
40 + // For each span in the column...
41 for (rcSpan* span = heightfield.spans[x + z * xSize]; span != NULL; previousSpan = span, span = span->next)
42 {
43 const bool walkable = span->area != RC_NULL_AREA;
44 - // If current span is not walkable, but there is walkable
45 - // span just below it, mark the span above it walkable too.
46 - if (!walkable && previousWasWalkable)
47 +
48 + // If current span is not walkable, but there is walkable span just below it and the height difference
49 + // is small enough for the agent to walk over, mark the current span as walkable too.
50 + if (!walkable && previousWasWalkable && (int)span->smax - (int)previousSpan->smax <= walkableClimb)
51 {
52 - if (rcAbs((int)span->smax - (int)previousSpan->smax) <= walkableClimb)
53 - {
54 - span->area = previousArea;
55 - }
56 + span->area = previousAreaID;
57 }
58 - // Copy walkable flag so that it cannot propagate
59 - // past multiple non-walkable objects.
60 +
61 + // Copy the original walkable value regardless of whether we changed it.
62 + // This prevents multiple consecutive non-walkable spans from being erroneously marked as walkable.
63 previousWasWalkable = walkable;
64 - previousArea = span->area;
65 + previousAreaID = span->area;
66 }
67 }
68 }
69 @@ -73,84 +73,98 @@ void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int
70 const int xSize = heightfield.width;
71 const int zSize = heightfield.height;
72
73 - // Mark border spans.
74 + // Mark spans that are adjacent to a ledge as unwalkable..
75 for (int z = 0; z < zSize; ++z)
76 {
77 for (int x = 0; x < xSize; ++x)
78 {
79 for (rcSpan* span = heightfield.spans[x + z * xSize]; span; span = span->next)
80 {
81 - // Skip non walkable spans.
82 + // Skip non-walkable spans.
83 if (span->area == RC_NULL_AREA)
84 {
85 continue;
86 }
87
88 - const int bot = (int)(span->smax);
89 - const int top = span->next ? (int)(span->next->smin) : MAX_HEIGHTFIELD_HEIGHT;
90 + const int floor = (int)(span->smax);
91 + const int ceiling = span->next ? (int)(span->next->smin) : MAX_HEIGHTFIELD_HEIGHT;
92
93 - // Find neighbours minimum height.
94 - int minNeighborHeight = MAX_HEIGHTFIELD_HEIGHT;
95 + // The difference between this walkable area and the lowest neighbor walkable area.
96 + // This is the difference between the current span and all neighbor spans that have
97 + // enough space for an agent to move between, but not accounting at all for surface slope.
98 + int lowestNeighborFloorDifference = MAX_HEIGHTFIELD_HEIGHT;
99
100 // Min and max height of accessible neighbours.
101 - int accessibleNeighborMinHeight = span->smax;
102 - int accessibleNeighborMaxHeight = span->smax;
103 + int lowestTraversableNeighborFloor = span->smax;
104 + int highestTraversableNeighborFloor = span->smax;
105
106 for (int direction = 0; direction < 4; ++direction)
107 {
108 - int dx = x + rcGetDirOffsetX(direction);
109 - int dz = z + rcGetDirOffsetY(direction);
110 + const int neighborX = x + rcGetDirOffsetX(direction);
111 + const int neighborZ = z + rcGetDirOffsetY(direction);
112 +
113 // Skip neighbours which are out of bounds.
114 - if (dx < 0 || dz < 0 || dx >= xSize || dz >= zSize)
115 + if (neighborX < 0 || neighborZ < 0 || neighborX >= xSize || neighborZ >= zSize)
116 {
117 - minNeighborHeight = (-walkableClimb - 1) ;
118 + lowestNeighborFloorDifference = -walkableClimb - 1;
119 break;
120 }
121
122 - // From minus infinity to the first span.
123 - const rcSpan* neighborSpan = heightfield.spans[dx + dz * xSize];
124 - int neighborTop = neighborSpan ? (int)neighborSpan->smin : MAX_HEIGHTFIELD_HEIGHT;
125 -
126 + const rcSpan* neighborSpan = heightfield.spans[neighborX + neighborZ * xSize];
127 +
128 + // The most we can step down to the neighbor is the walkableClimb distance.
129 + // Start with the area under the neighbor span
130 + int neighborCeiling = neighborSpan ? (int)neighborSpan->smin : MAX_HEIGHTFIELD_HEIGHT;
131 +
132 // Skip neighbour if the gap between the spans is too small.
133 - if (rcMin(top, neighborTop) - bot >= walkableHeight)
134 + if (rcMin(ceiling, neighborCeiling) - floor >= walkableHeight)
135 {
136 - minNeighborHeight = (-walkableClimb - 1);
137 + lowestNeighborFloorDifference = (-walkableClimb - 1);
138 break;
139 }
140
141 - // Rest of the spans.
142 - for (neighborSpan = heightfield.spans[dx + dz * xSize]; neighborSpan; neighborSpan = neighborSpan->next)
143 + // For each span in the neighboring column...
144 + for (; neighborSpan != NULL; neighborSpan = neighborSpan->next)
145 {
146 - int neighborBot = (int)neighborSpan->smax;
147 - neighborTop = neighborSpan->next ? (int)neighborSpan->next->smin : MAX_HEIGHTFIELD_HEIGHT;
148 -
149 - // Skip neighbour if the gap between the spans is too small.
150 - if (rcMin(top, neighborTop) - rcMax(bot, neighborBot) >= walkableHeight)
151 + const int neighborFloor = (int)neighborSpan->smax;
152 + neighborCeiling = neighborSpan->next ? (int)neighborSpan->next->smin : MAX_HEIGHTFIELD_HEIGHT;
153 +
154 + // Only consider neighboring areas that have enough overlap to be potentially traversable.
155 + if (rcMin(ceiling, neighborCeiling) - rcMax(floor, neighborFloor) < walkableHeight)
156 + {
157 + // No space to traverse between them.
158 + continue;
159 + }
160 +
161 + const int neighborFloorDifference = neighborFloor - floor;
162 + lowestNeighborFloorDifference = rcMin(lowestNeighborFloorDifference, neighborFloorDifference);
163 +
164 + // Find min/max accessible neighbor height.
165 + // Only consider neighbors that are at most walkableClimb away.
166 + if (rcAbs(neighborFloorDifference) <= walkableClimb)
167 + {
168 + // There is space to move to the neighbor cell and the slope isn't too much.
169 + lowestTraversableNeighborFloor = rcMin(lowestTraversableNeighborFloor, neighborFloor);
170 + highestTraversableNeighborFloor = rcMax(highestTraversableNeighborFloor, neighborFloor);
171 + }
172 + else if (neighborFloorDifference < -walkableClimb)
173 {
174 - int accessibleNeighbourHeight = neighborBot - bot;
175 - minNeighborHeight = rcMin(minNeighborHeight, accessibleNeighbourHeight);
176 -
177 - // Find min/max accessible neighbour height.
178 - if (rcAbs(accessibleNeighbourHeight) <= walkableClimb)
179 - {
180 - if (neighborBot < accessibleNeighborMinHeight) accessibleNeighborMinHeight = neighborBot;
181 - if (neighborBot > accessibleNeighborMaxHeight) accessibleNeighborMaxHeight = neighborBot;
182 - }
183 - else if (accessibleNeighbourHeight < -walkableClimb)
184 - {
185 - break;
186 - }
187 + // We already know this will be considered a ledge span so we can early-out
188 + break;
189 }
190 }
191 }
192
193 - // The current span is close to a ledge if the drop to any neighbour span is less than the walkableClimb.
194 - if (minNeighborHeight < -walkableClimb)
195 + // The current span is close to a ledge if the magnitude of the drop to any neighbour span is greater than the walkableClimb distance.
196 + // 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.
197 + // (If this is the case, then biggestNeighborStepDown will be negative, so compare against the negative walkableClimb as a means of checking
198 + // the magnitude of the delta)
199 + if (lowestNeighborFloorDifference < -walkableClimb)
200 {
201 span->area = RC_NULL_AREA;
202 }
203 - // If the difference between all neighbours is too large, we are at steep slope, mark the span as ledge.
204 - else if ((accessibleNeighborMaxHeight - accessibleNeighborMinHeight) > walkableClimb)
205 + // If the difference between all neighbor floors is too large, this is a steep slope, so mark the span as an unwalkable ledge.
206 + else if (highestTraversableNeighborFloor - lowestTraversableNeighborFloor > walkableClimb)
207 {
208 span->area = RC_NULL_AREA;
209 }
210 @@ -175,9 +189,9 @@ void rcFilterWalkableLowHeightSpans(rcContext* context, const int walkableHeight
211 {
212 for (rcSpan* span = heightfield.spans[x + z*xSize]; span; span = span->next)
213 {
214 - const int bot = (int)(span->smax);
215 - const int top = span->next ? (int)(span->next->smin) : MAX_HEIGHTFIELD_HEIGHT;
216 - if ((top - bot) < walkableHeight)
217 + const int floor = (int)(span->smax);
218 + const int ceiling = span->next ? (int)(span->next->smin) : MAX_HEIGHTFIELD_HEIGHT;
219 + if (ceiling - floor < walkableHeight)
220 {
221 span->area = RC_NULL_AREA;
222 }
223 diff --git a/Tests/Recast/Tests_RecastFilter.cpp b/Tests/Recast/Tests_RecastFilter.cpp
224 index 1882adb..d600835 100644
225 --- a/Tests/Recast/Tests_RecastFilter.cpp
226 +++ b/Tests/Recast/Tests_RecastFilter.cpp
227 @@ -47,7 +47,7 @@ TEST_CASE("rcFilterLowHangingWalkableObstacles", "[recast, filtering]")
228 {
229 // Put the second span just above the first one.
230 rcSpan* secondSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
231 - secondSpan->area = 1;
232 + secondSpan->area = RC_NULL_AREA;
233 secondSpan->next = NULL;
234 secondSpan->smin = 1 + walkableHeight;
235 secondSpan->smax = secondSpan->smin + 1;
236 @@ -64,7 +64,7 @@ TEST_CASE("rcFilterLowHangingWalkableObstacles", "[recast, filtering]")
237
238 // Check that nothing has changed.
239 REQUIRE(heightfield.spans[0]->area == 1);
240 - REQUIRE(heightfield.spans[0]->next->area == 1);
241 + REQUIRE(heightfield.spans[0]->next->area == RC_NULL_AREA);
242
243 // Check again but with a more clearance
244 secondSpan->smin += 10;
245 @@ -74,7 +74,7 @@ TEST_CASE("rcFilterLowHangingWalkableObstacles", "[recast, filtering]")
246
247 // Check that nothing has changed.
248 REQUIRE(heightfield.spans[0]->area == 1);
249 - REQUIRE(heightfield.spans[0]->next->area == 1);
250 + REQUIRE(heightfield.spans[0]->next->area == RC_NULL_AREA);
251
252 rcFree(span);
253 rcFree(secondSpan);
254 @@ -251,56 +251,6 @@ TEST_CASE("rcFilterLedgeSpans", "[recast, filtering]")
255 }
256 }
257 }
258 -
259 - SECTION("Edge spans are marked unwalkable")
260 - {
261 - // Create a flat plane.
262 - for (int x = 0; x < heightfield.width; ++x)
263 - {
264 - for (int z = 0; z < heightfield.height; ++z)
265 - {
266 - rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
267 - span->area = 1;
268 - span->next = NULL;
269 - span->smin = 0;
270 - span->smax = 1;
271 - heightfield.spans[x + z * heightfield.width] = span;
272 - }
273 - }
274 -
275 - rcFilterLedgeSpans(&context, walkableHeight, walkableClimb, heightfield);
276 -
277 - for (int x = 0; x < heightfield.width; ++x)
278 - {
279 - for (int z = 0; z < heightfield.height; ++z)
280 - {
281 - rcSpan* span = heightfield.spans[x + z * heightfield.width];
282 - REQUIRE(span != NULL);
283 -
284 - if (x == 0 || z == 0 || x == 9 || z == 9)
285 - {
286 - REQUIRE(span->area == RC_NULL_AREA);
287 - }
288 - else
289 - {
290 - REQUIRE(span->area == 1);
291 - }
292 -
293 - REQUIRE(span->next == NULL);
294 - REQUIRE(span->smin == 0);
295 - REQUIRE(span->smax == 1);
296 - }
297 - }
298 -
299 - // Free all the heightfield spans
300 - for (int x = 0; x < heightfield.width; ++x)
301 - {
302 - for (int z = 0; z < heightfield.height; ++z)
303 - {
304 - rcFree(heightfield.spans[x + z * heightfield.width]);
305 - }
306 - }
307 - }
308 }
309
310 TEST_CASE("rcFilterWalkableLowHeightSpans", "[recast, filtering]")
311 --
312 2.43.0
313