Debian Codegraph @

Tests/Recast/Tests_RecastFilter.cpp

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 #include <stdio.h>
2 #include <string.h>
3 #include <vector>
4
5 #include "catch2/catch_all.hpp"
6
7 #include "Recast.h"
8 #include "RecastAlloc.h"
9
10 TEST_CASE("rcFilterLowHangingWalkableObstacles", "[recast, filtering]")
11 {
12 rcContext context;
13 int walkableHeight = 5;
14
15 rcHeightfield heightfield;
16 heightfield.width = 1;
17 heightfield.height = 1;
18 heightfield.bmin[0] = 0;
19 heightfield.bmin[1] = 0;
20 heightfield.bmin[2] = 0;
21 heightfield.bmax[0] = 1;
22 heightfield.bmax[1] = 1;
23 heightfield.bmax[2] = 1;
24 heightfield.cs = 1;
25 heightfield.ch = 1;
26 heightfield.spans = (rcSpan**)rcAlloc(heightfield.width * heightfield.height * sizeof(rcSpan*), RC_ALLOC_PERM);
27 heightfield.pools = NULL;
28 heightfield.freelist = NULL;
29
30 SECTION("Span with no spans above it is unchanged")
31 {
32 rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
33 span->area = 1;
34 span->next = NULL;
35 span->smin = 0;
36 span->smax = 1;
37 heightfield.spans[0] = span;
38
39 rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
40
41 REQUIRE(heightfield.spans[0]->area == 1);
42
43 rcFree(span);
44 }
45
46 SECTION("Span with span above that is higher than walkableHeight is unchanged")
47 {
48 // Put the second span just above the first one.
49 rcSpan* secondSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
50 secondSpan->area = 1;
51 secondSpan->next = NULL;
52 secondSpan->smin = 1 + walkableHeight;
53 secondSpan->smax = secondSpan->smin + 1;
54
55 rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
56 span->area = 1;
57 span->next = secondSpan;
58 span->smin = 0;
59 span->smax = 1;
60
61 heightfield.spans[0] = span;
62
63 rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
64
65 // Check that nothing has changed.
66 REQUIRE(heightfield.spans[0]->area == 1);
67 REQUIRE(heightfield.spans[0]->next->area == 1);
68
69 // Check again but with a more clearance
70 secondSpan->smin += 10;
71 secondSpan->smax += 10;
72
73 rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
74
75 // Check that nothing has changed.
76 REQUIRE(heightfield.spans[0]->area == 1);
77 REQUIRE(heightfield.spans[0]->next->area == 1);
78
79 rcFree(span);
80 rcFree(secondSpan);
81 }
82
83 SECTION("Marks low obstacles walkable if they're below the walkableClimb")
84 {
85 // Put the second span just above the first one.
86 rcSpan* secondSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
87 secondSpan->area = RC_NULL_AREA;
88 secondSpan->next = NULL;
89 secondSpan->smin = 1 + (walkableHeight - 1);
90 secondSpan->smax = secondSpan->smin + 1;
91
92 rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
93 span->area = 1;
94 span->next = secondSpan;
95 span->smin = 0;
96 span->smax = 1;
97
98 heightfield.spans[0] = span;
99
100 rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
101
102 // Check that the second span was changed to walkable.
103 REQUIRE(heightfield.spans[0]->area == 1);
104 REQUIRE(heightfield.spans[0]->next->area == 1);
105
106 rcFree(span);
107 rcFree(secondSpan);
108 }
109
110 SECTION("Low obstacle that overlaps the walkableClimb distance is not changed")
111 {
112 // Put the second span just above the first one.
113 rcSpan* secondSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
114 secondSpan->area = RC_NULL_AREA;
115 secondSpan->next = NULL;
116 secondSpan->smin = 2 + (walkableHeight - 1);
117 secondSpan->smax = secondSpan->smin + 1;
118
119 rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
120 span->area = 1;
121 span->next = secondSpan;
122 span->smin = 0;
123 span->smax = 1;
124
125 heightfield.spans[0] = span;
126
127 rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
128
129 // Check that the second span was changed to walkable.
130 REQUIRE(heightfield.spans[0]->area == 1);
131 REQUIRE(heightfield.spans[0]->next->area == RC_NULL_AREA);
132
133 rcFree(span);
134 rcFree(secondSpan);
135 }
136
137 SECTION("Only the first of multiple, low obstacles are marked walkable")
138 {
139 rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
140 span->area = 1;
141 span->next = NULL;
142 span->smin = 0;
143 span->smax = 1;
144 heightfield.spans[0] = span;
145
146 rcSpan* previousSpan = span;
147 for (int i = 0; i < 9; ++i)
148 {
149 rcSpan* nextSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
150 nextSpan->area = RC_NULL_AREA;
151 nextSpan->next = NULL;
152 nextSpan->smin = previousSpan->smax + (walkableHeight - 1);
153 nextSpan->smax = nextSpan->smin + 1;
154 previousSpan->next = nextSpan;
155 previousSpan = nextSpan;
156 }
157
158 rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
159
160 rcSpan* currentSpan = heightfield.spans[0];
161 for (int i = 0; i < 10; ++i)
162 {
163 REQUIRE(currentSpan != NULL);
164 // only the first and second spans should be marked as walkabl
165 REQUIRE(currentSpan->area == (i <= 1 ? 1 : RC_NULL_AREA));
166 currentSpan = currentSpan->next;
167 }
168
169 std::vector<rcSpan*> toFree;
170 span = heightfield.spans[0];
171 for (int i = 0; i < 10; ++i)
172 {
173 toFree.push_back(span);
174 span = span->next;
175 }
176
177 for (int i = 0; i < 10; ++i)
178 {
179 rcFree(toFree[i]);
180 }
181 }
182 }
183
184 TEST_CASE("rcFilterLedgeSpans", "[recast, filtering]")
185 {
186 rcContext context;
187 int walkableClimb = 5;
188 int walkableHeight = 10;
189
190 rcHeightfield heightfield;
191 heightfield.width = 10;
192 heightfield.height = 10;
193 heightfield.bmin[0] = 0;
194 heightfield.bmin[1] = 0;
195 heightfield.bmin[2] = 0;
196 heightfield.bmax[0] = 10;
197 heightfield.bmax[1] = 1;
198 heightfield.bmax[2] = 10;
199 heightfield.cs = 1;
200 heightfield.ch = 1;
201 heightfield.spans = (rcSpan**)rcAlloc(heightfield.width * heightfield.height * sizeof(rcSpan*), RC_ALLOC_PERM);
202 heightfield.pools = NULL;
203 heightfield.freelist = NULL;
204
205 SECTION("Edge spans are marked unwalkable")
206 {
207 // Create a flat plane.
208 for (int x = 0; x < heightfield.width; ++x)
209 {
210 for (int z = 0; z < heightfield.height; ++z)
211 {
212 rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
213 span->area = 1;
214 span->next = NULL;
215 span->smin = 0;
216 span->smax = 1;
217 heightfield.spans[x + z * heightfield.width] = span;
218 }
219 }
220
221 rcFilterLedgeSpans(&context, walkableHeight, walkableClimb, heightfield);
222
223 for (int x = 0; x < heightfield.width; ++x)
224 {
225 for (int z = 0; z < heightfield.height; ++z)
226 {
227 rcSpan* span = heightfield.spans[x + z * heightfield.width];
228 REQUIRE(span != NULL);
229
230 if (x == 0 || z == 0 || x == 9 || z == 9)
231 {
232 REQUIRE(span->area == RC_NULL_AREA);
233 }
234 else
235 {
236 REQUIRE(span->area == 1);
237 }
238
239 REQUIRE(span->next == NULL);
240 REQUIRE(span->smin == 0);
241 REQUIRE(span->smax == 1);
242 }
243 }
244
245 // Free all the heightfield spans
246 for (int x = 0; x < heightfield.width; ++x)
247 {
248 for (int z = 0; z < heightfield.height; ++z)
249 {
250 rcFree(heightfield.spans[x + z * heightfield.width]);
251 }
252 }
253 }
254
255 SECTION("Edge spans are marked unwalkable")
256 {
257 // Create a flat plane.
258 for (int x = 0; x < heightfield.width; ++x)
259 {
260 for (int z = 0; z < heightfield.height; ++z)
261 {
262 rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
263 span->area = 1;
264 span->next = NULL;
265 span->smin = 0;
266 span->smax = 1;
267 heightfield.spans[x + z * heightfield.width] = span;
268 }
269 }
270
271 rcFilterLedgeSpans(&context, walkableHeight, walkableClimb, heightfield);
272
273 for (int x = 0; x < heightfield.width; ++x)
274 {
275 for (int z = 0; z < heightfield.height; ++z)
276 {
277 rcSpan* span = heightfield.spans[x + z * heightfield.width];
278 REQUIRE(span != NULL);
279
280 if (x == 0 || z == 0 || x == 9 || z == 9)
281 {
282 REQUIRE(span->area == RC_NULL_AREA);
283 }
284 else
285 {
286 REQUIRE(span->area == 1);
287 }
288
289 REQUIRE(span->next == NULL);
290 REQUIRE(span->smin == 0);
291 REQUIRE(span->smax == 1);
292 }
293 }
294
295 // Free all the heightfield spans
296 for (int x = 0; x < heightfield.width; ++x)
297 {
298 for (int z = 0; z < heightfield.height; ++z)
299 {
300 rcFree(heightfield.spans[x + z * heightfield.width]);
301 }
302 }
303 }
304 }
305
306 TEST_CASE("rcFilterWalkableLowHeightSpans", "[recast, filtering]")
307 {
308 rcContext context;
309 int walkableHeight = 5;
310
311 rcHeightfield heightfield;
312 heightfield.width = 1;
313 heightfield.height = 1;
314 heightfield.bmin[0] = 0;
315 heightfield.bmin[1] = 0;
316 heightfield.bmin[2] = 0;
317 heightfield.bmax[0] = 1;
318 heightfield.bmax[1] = 1;
319 heightfield.bmax[2] = 1;
320 heightfield.cs = 1;
321 heightfield.ch = 1;
322 heightfield.spans = (rcSpan**)rcAlloc(heightfield.width * heightfield.height * sizeof(rcSpan*), RC_ALLOC_PERM);
323 heightfield.pools = NULL;
324 heightfield.freelist = NULL;
325
326 SECTION("span nothing above is unchanged")
327 {
328 rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
329 span->area = 1;
330 span->next = NULL;
331 span->smin = 0;
332 span->smax = 1;
333 heightfield.spans[0] = span;
334
335 rcFilterWalkableLowHeightSpans(&context, walkableHeight, heightfield);
336
337 REQUIRE(heightfield.spans[0]->area == 1);
338
339 rcFree(span);
340 }
341
342 SECTION("span with lots of room above is unchanged")
343 {
344 rcSpan* overheadSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
345 overheadSpan->area = RC_NULL_AREA;
346 overheadSpan->next = NULL;
347 overheadSpan->smin = 10;
348 overheadSpan->smax = 11;
349
350 rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
351 span->area = 1;
352 span->next = overheadSpan;
353 span->smin = 0;
354 span->smax = 1;
355 heightfield.spans[0] = span;
356
357 rcFilterWalkableLowHeightSpans(&context, walkableHeight, heightfield);
358
359 REQUIRE(heightfield.spans[0]->area == 1);
360 REQUIRE(heightfield.spans[0]->next->area == RC_NULL_AREA);
361
362 rcFree(overheadSpan);
363 rcFree(span);
364 }
365
366 SECTION("Span with low hanging obstacle is marked as unwalkable")
367 {
368 rcSpan* overheadSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
369 overheadSpan->area = RC_NULL_AREA;
370 overheadSpan->next = NULL;
371 overheadSpan->smin = 3;
372 overheadSpan->smax = 4;
373
374 rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
375 span->area = 1;
376 span->next = overheadSpan;
377 span->smin = 0;
378 span->smax = 1;
379 heightfield.spans[0] = span;
380
381 rcFilterWalkableLowHeightSpans(&context, walkableHeight, heightfield);
382
383 REQUIRE(heightfield.spans[0]->area == RC_NULL_AREA);
384 REQUIRE(heightfield.spans[0]->next->area == RC_NULL_AREA);
385
386 rcFree(overheadSpan);
387 rcFree(span);
388 }
389 }