| •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 = RC_NULL_AREA; |
| 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 == RC_NULL_AREA); |
| 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 == RC_NULL_AREA); |
| 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 |
|
| 256 |
TEST_CASE("rcFilterWalkableLowHeightSpans", "[recast, filtering]") |
| 257 |
{ |
| 258 |
rcContext context; |
| 259 |
int walkableHeight = 5; |
| 260 |
|
| 261 |
rcHeightfield heightfield; |
| 262 |
heightfield.width = 1; |
| 263 |
heightfield.height = 1; |
| 264 |
heightfield.bmin[0] = 0; |
| 265 |
heightfield.bmin[1] = 0; |
| 266 |
heightfield.bmin[2] = 0; |
| 267 |
heightfield.bmax[0] = 1; |
| 268 |
heightfield.bmax[1] = 1; |
| 269 |
heightfield.bmax[2] = 1; |
| 270 |
heightfield.cs = 1; |
| 271 |
heightfield.ch = 1; |
| 272 |
heightfield.spans = (rcSpan**)rcAlloc(heightfield.width * heightfield.height * sizeof(rcSpan*), RC_ALLOC_PERM); |
| 273 |
heightfield.pools = NULL; |
| 274 |
heightfield.freelist = NULL; |
| 275 |
|
| 276 |
SECTION("span nothing above is unchanged") |
| 277 |
{ |
| 278 |
rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM); |
| 279 |
span->area = 1; |
| 280 |
span->next = NULL; |
| 281 |
span->smin = 0; |
| 282 |
span->smax = 1; |
| 283 |
heightfield.spans[0] = span; |
| 284 |
|
| 285 |
rcFilterWalkableLowHeightSpans(&context, walkableHeight, heightfield); |
| 286 |
|
| 287 |
REQUIRE(heightfield.spans[0]->area == 1); |
| 288 |
|
| 289 |
rcFree(span); |
| 290 |
} |
| 291 |
|
| 292 |
SECTION("span with lots of room above is unchanged") |
| 293 |
{ |
| 294 |
rcSpan* overheadSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM); |
| 295 |
overheadSpan->area = RC_NULL_AREA; |
| 296 |
overheadSpan->next = NULL; |
| 297 |
overheadSpan->smin = 10; |
| 298 |
overheadSpan->smax = 11; |
| 299 |
|
| 300 |
rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM); |
| 301 |
span->area = 1; |
| 302 |
span->next = overheadSpan; |
| 303 |
span->smin = 0; |
| 304 |
span->smax = 1; |
| 305 |
heightfield.spans[0] = span; |
| 306 |
|
| 307 |
rcFilterWalkableLowHeightSpans(&context, walkableHeight, heightfield); |
| 308 |
|
| 309 |
REQUIRE(heightfield.spans[0]->area == 1); |
| 310 |
REQUIRE(heightfield.spans[0]->next->area == RC_NULL_AREA); |
| 311 |
|
| 312 |
rcFree(overheadSpan); |
| 313 |
rcFree(span); |
| 314 |
} |
| 315 |
|
| 316 |
SECTION("Span with low hanging obstacle is marked as unwalkable") |
| 317 |
{ |
| 318 |
rcSpan* overheadSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM); |
| 319 |
overheadSpan->area = RC_NULL_AREA; |
| 320 |
overheadSpan->next = NULL; |
| 321 |
overheadSpan->smin = 3; |
| 322 |
overheadSpan->smax = 4; |
| 323 |
|
| 324 |
rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM); |
| 325 |
span->area = 1; |
| 326 |
span->next = overheadSpan; |
| 327 |
span->smin = 0; |
| 328 |
span->smax = 1; |
| 329 |
heightfield.spans[0] = span; |
| 330 |
|
| 331 |
rcFilterWalkableLowHeightSpans(&context, walkableHeight, heightfield); |
| 332 |
|
| 333 |
REQUIRE(heightfield.spans[0]->area == RC_NULL_AREA); |
| 334 |
REQUIRE(heightfield.spans[0]->next->area == RC_NULL_AREA); |
| 335 |
|
| 336 |
rcFree(overheadSpan); |
| 337 |
rcFree(span); |
| 338 |
} |
| 339 |
} |