| 1 |
#include <stdio.h> |
| 2 |
#include <string.h> |
| 3 |
|
| 4 |
#include "catch2/catch_all.hpp" |
| 5 |
|
| 6 |
#include "Recast.h" |
| 7 |
|
| •8 |
TEST_CASE("rcSwap", "[recast]") |
| 9 |
{ |
| 10 |
SECTION("Swap two values") |
| 11 |
{ |
| 12 |
int one = 1; |
| 13 |
int two = 2; |
| 14 |
rcSwap(one, two); |
| 15 |
REQUIRE(one == 2); |
| 16 |
REQUIRE(two == 1); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| •20 |
TEST_CASE("rcMin", "[recast]") |
| 21 |
{ |
| 22 |
SECTION("Min returns the lowest value.") |
| 23 |
{ |
| 24 |
REQUIRE(rcMin(1, 2) == 1); |
| 25 |
REQUIRE(rcMin(2, 1) == 1); |
| 26 |
} |
| 27 |
|
| 28 |
SECTION("Min with equal args") |
| 29 |
{ |
| 30 |
REQUIRE(rcMin(1, 1) == 1); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| •34 |
TEST_CASE("rcMax", "[recast]") |
| 35 |
{ |
| 36 |
SECTION("Max returns the greatest value.") |
| 37 |
{ |
| 38 |
REQUIRE(rcMax(1, 2) == 2); |
| 39 |
REQUIRE(rcMax(2, 1) == 2); |
| 40 |
} |
| 41 |
|
| 42 |
SECTION("Max with equal args") |
| 43 |
{ |
| 44 |
REQUIRE(rcMax(1, 1) == 1); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| •48 |
TEST_CASE("rcAbs", "[recast]") |
| 49 |
{ |
| 50 |
SECTION("Abs returns the absolute value.") |
| 51 |
{ |
| 52 |
REQUIRE(rcAbs(-1) == 1); |
| 53 |
REQUIRE(rcAbs(1) == 1); |
| 54 |
REQUIRE(rcAbs(0) == 0); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| •58 |
TEST_CASE("rcSqr", "[recast]") |
| 59 |
{ |
| 60 |
SECTION("Sqr squares a number") |
| 61 |
{ |
| 62 |
REQUIRE(rcSqr(2) == 4); |
| 63 |
REQUIRE(rcSqr(-4) == 16); |
| 64 |
REQUIRE(rcSqr(0) == 0); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| •68 |
TEST_CASE("rcClamp", "[recast]") |
| 69 |
{ |
| 70 |
SECTION("Higher than range") |
| 71 |
{ |
| 72 |
REQUIRE(rcClamp(2, 0, 1) == 1); |
| 73 |
} |
| 74 |
|
| 75 |
SECTION("Within range") |
| 76 |
{ |
| 77 |
REQUIRE(rcClamp(1, 0, 2) == 1); |
| 78 |
} |
| 79 |
|
| 80 |
SECTION("Lower than range") |
| 81 |
{ |
| 82 |
REQUIRE(rcClamp(0, 1, 2) == 1); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| •86 |
TEST_CASE("rcSqrt", "[recast]") |
| 87 |
{ |
| 88 |
SECTION("Sqrt gets the sqrt of a number") |
| 89 |
{ |
| 90 |
REQUIRE(rcSqrt(4) == Catch::Approx(2)); |
| 91 |
REQUIRE(rcSqrt(81) == Catch::Approx(9)); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| •95 |
TEST_CASE("rcVcross", "[recast]") |
| 96 |
{ |
| 97 |
SECTION("Computes cross product") |
| 98 |
{ |
| 99 |
float v1[3] = {3, -3, 1}; |
| 100 |
float v2[3] = {4, 9, 2}; |
| 101 |
float result[3]; |
| 102 |
rcVcross(result, v1, v2); |
| 103 |
REQUIRE(result[0] == Catch::Approx(-15)); |
| 104 |
REQUIRE(result[1] == Catch::Approx(-2)); |
| 105 |
REQUIRE(result[2] == Catch::Approx(39)); |
| 106 |
} |
| 107 |
|
| 108 |
SECTION("Cross product with itself is zero") |
| 109 |
{ |
| 110 |
float v1[3] = {3, -3, 1}; |
| 111 |
float result[3]; |
| 112 |
rcVcross(result, v1, v1); |
| 113 |
REQUIRE(result[0] == Catch::Approx(0)); |
| 114 |
REQUIRE(result[1] == Catch::Approx(0)); |
| 115 |
REQUIRE(result[2] == Catch::Approx(0)); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| •119 |
TEST_CASE("rcVdot", "[recast]") |
| 120 |
{ |
| 121 |
SECTION("Dot normalized vector with itself") |
| 122 |
{ |
| 123 |
float v1[] = { 1, 0, 0 }; |
| 124 |
float result = rcVdot(v1, v1); |
| 125 |
REQUIRE(result == Catch::Approx(1)); |
| 126 |
} |
| 127 |
|
| 128 |
SECTION("Dot zero vector with anything is zero") |
| 129 |
{ |
| 130 |
float v1[] = { 1, 2, 3 }; |
| 131 |
float v2[] = { 0, 0, 0 }; |
| 132 |
|
| 133 |
float result = rcVdot(v1, v2); |
| 134 |
REQUIRE(result == Catch::Approx(0)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| •138 |
TEST_CASE("rcVmad", "[recast]") |
| 139 |
{ |
| 140 |
SECTION("scaled add two vectors") |
| 141 |
{ |
| 142 |
float v1[3] = {1, 2, 3}; |
| 143 |
float v2[3] = {0, 2, 4}; |
| 144 |
float result[3]; |
| 145 |
rcVmad(result, v1, v2, 2); |
| 146 |
REQUIRE(result[0] == Catch::Approx(1)); |
| 147 |
REQUIRE(result[1] == Catch::Approx(6)); |
| 148 |
REQUIRE(result[2] == Catch::Approx(11)); |
| 149 |
} |
| 150 |
|
| 151 |
SECTION("second vector is scaled, first is not") |
| 152 |
{ |
| 153 |
float v1[3] = {1, 2, 3}; |
| 154 |
float v2[3] = {5, 6, 7}; |
| 155 |
float result[3]; |
| 156 |
rcVmad(result, v1, v2, 0); |
| 157 |
REQUIRE(result[0] == Catch::Approx(1)); |
| 158 |
REQUIRE(result[1] == Catch::Approx(2)); |
| 159 |
REQUIRE(result[2] == Catch::Approx(3)); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| •163 |
TEST_CASE("rcVadd", "[recast]") |
| 164 |
{ |
| 165 |
SECTION("add two vectors") |
| 166 |
{ |
| 167 |
float v1[3] = {1, 2, 3}; |
| 168 |
float v2[3] = {5, 6, 7}; |
| 169 |
float result[3]; |
| 170 |
rcVadd(result, v1, v2); |
| 171 |
REQUIRE(result[0] == Catch::Approx(6)); |
| 172 |
REQUIRE(result[1] == Catch::Approx(8)); |
| 173 |
REQUIRE(result[2] == Catch::Approx(10)); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| •177 |
TEST_CASE("rcVsub", "[recast]") |
| 178 |
{ |
| 179 |
SECTION("subtract two vectors") |
| 180 |
{ |
| 181 |
float v1[3] = {5, 4, 3}; |
| 182 |
float v2[3] = {1, 2, 3}; |
| 183 |
float result[3]; |
| 184 |
rcVsub(result, v1, v2); |
| 185 |
REQUIRE(result[0] == Catch::Approx(4)); |
| 186 |
REQUIRE(result[1] == Catch::Approx(2)); |
| 187 |
REQUIRE(result[2] == Catch::Approx(0)); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| •191 |
TEST_CASE("rcVmin", "[recast]") |
| 192 |
{ |
| 193 |
SECTION("selects the min component from the vectors") |
| 194 |
{ |
| 195 |
float v1[3] = {5, 4, 0}; |
| 196 |
float v2[3] = {1, 2, 9}; |
| 197 |
rcVmin(v1, v2); |
| 198 |
REQUIRE(v1[0] == Catch::Approx(1)); |
| 199 |
REQUIRE(v1[1] == Catch::Approx(2)); |
| 200 |
REQUIRE(v1[2] == Catch::Approx(0)); |
| 201 |
} |
| 202 |
|
| 203 |
SECTION("v1 is min") |
| 204 |
{ |
| 205 |
float v1[3] = {1, 2, 3}; |
| 206 |
float v2[3] = {4, 5, 6}; |
| 207 |
rcVmin(v1, v2); |
| 208 |
REQUIRE(v1[0] == Catch::Approx(1)); |
| 209 |
REQUIRE(v1[1] == Catch::Approx(2)); |
| 210 |
REQUIRE(v1[2] == Catch::Approx(3)); |
| 211 |
} |
| 212 |
|
| 213 |
SECTION("v2 is min") |
| 214 |
{ |
| 215 |
float v1[3] = {4, 5, 6}; |
| 216 |
float v2[3] = {1, 2, 3}; |
| 217 |
rcVmin(v1, v2); |
| 218 |
REQUIRE(v1[0] == Catch::Approx(1)); |
| 219 |
REQUIRE(v1[1] == Catch::Approx(2)); |
| 220 |
REQUIRE(v1[2] == Catch::Approx(3)); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| •224 |
TEST_CASE("rcVmax", "[recast]") |
| 225 |
{ |
| 226 |
SECTION("selects the max component from the vectors") |
| 227 |
{ |
| 228 |
float v1[3] = {5, 4, 0}; |
| 229 |
float v2[3] = {1, 2, 9}; |
| 230 |
rcVmax(v1, v2); |
| 231 |
REQUIRE(v1[0] == Catch::Approx(5)); |
| 232 |
REQUIRE(v1[1] == Catch::Approx(4)); |
| 233 |
REQUIRE(v1[2] == Catch::Approx(9)); |
| 234 |
} |
| 235 |
|
| 236 |
SECTION("v2 is max") |
| 237 |
{ |
| 238 |
float v1[3] = {1, 2, 3}; |
| 239 |
float v2[3] = {4, 5, 6}; |
| 240 |
rcVmax(v1, v2); |
| 241 |
REQUIRE(v1[0] == Catch::Approx(4)); |
| 242 |
REQUIRE(v1[1] == Catch::Approx(5)); |
| 243 |
REQUIRE(v1[2] == Catch::Approx(6)); |
| 244 |
} |
| 245 |
|
| 246 |
SECTION("v1 is max") |
| 247 |
{ |
| 248 |
float v1[3] = {4, 5, 6}; |
| 249 |
float v2[3] = {1, 2, 3}; |
| 250 |
rcVmax(v1, v2); |
| 251 |
REQUIRE(v1[0] == Catch::Approx(4)); |
| 252 |
REQUIRE(v1[1] == Catch::Approx(5)); |
| 253 |
REQUIRE(v1[2] == Catch::Approx(6)); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| •257 |
TEST_CASE("rcVcopy", "[recast]") |
| 258 |
{ |
| 259 |
SECTION("copies a vector into another vector") |
| 260 |
{ |
| 261 |
float v1[3] = {5, 4, 0}; |
| 262 |
float result[3] = {1, 2, 9}; |
| 263 |
rcVcopy(result, v1); |
| 264 |
REQUIRE(result[0] == Catch::Approx(5)); |
| 265 |
REQUIRE(result[1] == Catch::Approx(4)); |
| 266 |
REQUIRE(result[2] == Catch::Approx(0)); |
| 267 |
REQUIRE(v1[0] == Catch::Approx(5)); |
| 268 |
REQUIRE(v1[1] == Catch::Approx(4)); |
| 269 |
REQUIRE(v1[2] == Catch::Approx(0)); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| •273 |
TEST_CASE("rcVdist", "[recast]") |
| 274 |
{ |
| 275 |
SECTION("distance between two vectors") |
| 276 |
{ |
| 277 |
float v1[3] = {3, 1, 3}; |
| 278 |
float v2[3] = {1, 3, 1}; |
| 279 |
float result = rcVdist(v1, v2); |
| 280 |
|
| 281 |
REQUIRE(result == Catch::Approx(3.4641f)); |
| 282 |
} |
| 283 |
|
| 284 |
SECTION("Distance from zero is magnitude") |
| 285 |
{ |
| 286 |
float v1[3] = {3, 1, 3}; |
| 287 |
float v2[3] = {0, 0, 0}; |
| 288 |
float distance = rcVdist(v1, v2); |
| 289 |
float magnitude = rcSqrt(rcSqr(v1[0]) + rcSqr(v1[1]) + rcSqr(v1[2])); |
| 290 |
REQUIRE(distance == Catch::Approx(magnitude)); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| •294 |
TEST_CASE("rcVdistSqr", "[recast]") |
| 295 |
{ |
| 296 |
SECTION("squared distance between two vectors") |
| 297 |
{ |
| 298 |
float v1[3] = {3, 1, 3}; |
| 299 |
float v2[3] = {1, 3, 1}; |
| 300 |
float result = rcVdistSqr(v1, v2); |
| 301 |
|
| 302 |
REQUIRE(result == Catch::Approx(12)); |
| 303 |
} |
| 304 |
|
| 305 |
SECTION("squared distance from zero is squared magnitude") |
| 306 |
{ |
| 307 |
float v1[3] = {3, 1, 3}; |
| 308 |
float v2[3] = {0, 0, 0}; |
| 309 |
float distance = rcVdistSqr(v1, v2); |
| 310 |
float magnitude = rcSqr(v1[0]) + rcSqr(v1[1]) + rcSqr(v1[2]); |
| 311 |
REQUIRE(distance == Catch::Approx(magnitude)); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| •315 |
TEST_CASE("rcVnormalize", "[recast]") |
| 316 |
{ |
| 317 |
SECTION("normalizing reduces magnitude to 1") |
| 318 |
{ |
| 319 |
float v[3] = {3, 3, 3}; |
| 320 |
rcVnormalize(v); |
| 321 |
REQUIRE(v[0] == Catch::Approx(rcSqrt(1.0f / 3.0f))); |
| 322 |
REQUIRE(v[1] == Catch::Approx(rcSqrt(1.0f / 3.0f))); |
| 323 |
REQUIRE(v[2] == Catch::Approx(rcSqrt(1.0f / 3.0f))); |
| 324 |
float magnitude = rcSqrt(rcSqr(v[0]) + rcSqr(v[1]) + rcSqr(v[2])); |
| 325 |
REQUIRE(magnitude == Catch::Approx(1)); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| •329 |
TEST_CASE("rcCalcBounds", "[recast]") |
| 330 |
{ |
| 331 |
SECTION("bounds of one vector") |
| 332 |
{ |
| 333 |
float verts[] = {1, 2, 3}; |
| 334 |
float bmin[3]; |
| 335 |
float bmax[3]; |
| 336 |
rcCalcBounds(verts, 1, bmin, bmax); |
| 337 |
|
| 338 |
REQUIRE(bmin[0] == Catch::Approx(verts[0])); |
| 339 |
REQUIRE(bmin[1] == Catch::Approx(verts[1])); |
| 340 |
REQUIRE(bmin[2] == Catch::Approx(verts[2])); |
| 341 |
|
| 342 |
REQUIRE(bmax[0] == Catch::Approx(verts[0])); |
| 343 |
REQUIRE(bmax[1] == Catch::Approx(verts[1])); |
| 344 |
REQUIRE(bmax[2] == Catch::Approx(verts[2])); |
| 345 |
} |
| 346 |
|
| 347 |
SECTION("bounds of more than one vector") |
| 348 |
{ |
| 349 |
float verts[] = { |
| 350 |
1, 2, 3, |
| 351 |
0, 2, 5 |
| 352 |
}; |
| 353 |
float bmin[3]; |
| 354 |
float bmax[3]; |
| 355 |
rcCalcBounds(verts, 2, bmin, bmax); |
| 356 |
|
| 357 |
REQUIRE(bmin[0] == Catch::Approx(0)); |
| 358 |
REQUIRE(bmin[1] == Catch::Approx(2)); |
| 359 |
REQUIRE(bmin[2] == Catch::Approx(3)); |
| 360 |
|
| 361 |
REQUIRE(bmax[0] == Catch::Approx(1)); |
| 362 |
REQUIRE(bmax[1] == Catch::Approx(2)); |
| 363 |
REQUIRE(bmax[2] == Catch::Approx(5)); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| •367 |
TEST_CASE("rcCalcGridSize", "[recast]") |
| 368 |
{ |
| 369 |
SECTION("computes the size of an x & z axis grid") |
| 370 |
{ |
| 371 |
float verts[] = { |
| 372 |
1, 2, 3, |
| 373 |
0, 2, 6 |
| 374 |
}; |
| 375 |
float bmin[3]; |
| 376 |
float bmax[3]; |
| 377 |
rcCalcBounds(verts, 2, bmin, bmax); |
| 378 |
|
| 379 |
float cellSize = 1.5f; |
| 380 |
|
| 381 |
int width; |
| 382 |
int height; |
| 383 |
|
| 384 |
rcCalcGridSize(bmin, bmax, cellSize, &width, &height); |
| 385 |
|
| 386 |
REQUIRE(width == 1); |
| 387 |
REQUIRE(height == 2); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| •391 |
TEST_CASE("rcCreateHeightfield", "[recast]") |
| 392 |
{ |
| 393 |
SECTION("create a heightfield") |
| 394 |
{ |
| 395 |
float verts[] = { |
| 396 |
1, 2, 3, |
| 397 |
0, 2, 6 |
| 398 |
}; |
| 399 |
float bmin[3]; |
| 400 |
float bmax[3]; |
| 401 |
rcCalcBounds(verts, 2, bmin, bmax); |
| 402 |
|
| 403 |
float cellSize = 1.5f; |
| 404 |
float cellHeight = 2; |
| 405 |
|
| 406 |
int width; |
| 407 |
int height; |
| 408 |
|
| 409 |
rcCalcGridSize(bmin, bmax, cellSize, &width, &height); |
| 410 |
|
| 411 |
rcHeightfield heightfield; |
| 412 |
|
| 413 |
bool result = rcCreateHeightfield(0, heightfield, width, height, bmin, bmax, cellSize, cellHeight); |
| 414 |
|
| 415 |
REQUIRE(result); |
| 416 |
|
| 417 |
REQUIRE(heightfield.width == width); |
| 418 |
REQUIRE(heightfield.height == height); |
| 419 |
|
| 420 |
REQUIRE(heightfield.bmin[0] == Catch::Approx(bmin[0])); |
| 421 |
REQUIRE(heightfield.bmin[1] == Catch::Approx(bmin[1])); |
| 422 |
REQUIRE(heightfield.bmin[2] == Catch::Approx(bmin[2])); |
| 423 |
|
| 424 |
REQUIRE(heightfield.bmax[0] == Catch::Approx(bmax[0])); |
| 425 |
REQUIRE(heightfield.bmax[1] == Catch::Approx(bmax[1])); |
| 426 |
REQUIRE(heightfield.bmax[2] == Catch::Approx(bmax[2])); |
| 427 |
|
| 428 |
REQUIRE(heightfield.cs == Catch::Approx(cellSize)); |
| 429 |
REQUIRE(heightfield.ch == Catch::Approx(cellHeight)); |
| 430 |
|
| 431 |
REQUIRE(heightfield.spans != 0); |
| 432 |
REQUIRE(heightfield.pools == 0); |
| 433 |
REQUIRE(heightfield.freelist == 0); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| •437 |
TEST_CASE("rcMarkWalkableTriangles", "[recast]") |
| 438 |
{ |
| 439 |
rcContext* ctx = 0; |
| 440 |
float walkableSlopeAngle = 45; |
| 441 |
float verts[] = { |
| 442 |
0, 0, 0, |
| 443 |
1, 0, 0, |
| 444 |
0, 0, -1 |
| 445 |
}; |
| 446 |
int nv = 3; |
| 447 |
int walkable_tri[] = { 0, 1, 2 }; |
| 448 |
int unwalkable_tri[] = { 0, 2, 1 }; |
| 449 |
int nt = 1; |
| 450 |
unsigned char areas[] = { RC_NULL_AREA }; |
| 451 |
|
| 452 |
SECTION("One walkable triangle") |
| 453 |
{ |
| 454 |
rcMarkWalkableTriangles(ctx, walkableSlopeAngle, verts, nv, walkable_tri, nt, areas); |
| 455 |
REQUIRE(areas[0] == RC_WALKABLE_AREA); |
| 456 |
} |
| 457 |
|
| 458 |
SECTION("One non-walkable triangle") |
| 459 |
{ |
| 460 |
rcMarkWalkableTriangles(ctx, walkableSlopeAngle, verts, nv, unwalkable_tri, nt, areas); |
| 461 |
REQUIRE(areas[0] == RC_NULL_AREA); |
| 462 |
} |
| 463 |
|
| 464 |
SECTION("Non-walkable triangle area id's are not modified") |
| 465 |
{ |
| 466 |
areas[0] = 42; |
| 467 |
rcMarkWalkableTriangles(ctx, walkableSlopeAngle, verts, nv, unwalkable_tri, nt, areas); |
| 468 |
REQUIRE(areas[0] == 42); |
| 469 |
} |
| 470 |
|
| 471 |
SECTION("Slopes equal to the max slope are considered unwalkable.") |
| 472 |
{ |
| 473 |
walkableSlopeAngle = 0; |
| 474 |
rcMarkWalkableTriangles(ctx, walkableSlopeAngle, verts, nv, walkable_tri, nt, areas); |
| 475 |
REQUIRE(areas[0] == RC_NULL_AREA); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| •479 |
TEST_CASE("rcClearUnwalkableTriangles", "[recast]") |
| 480 |
{ |
| 481 |
rcContext* ctx = 0; |
| 482 |
float walkableSlopeAngle = 45; |
| 483 |
float verts[] = { |
| 484 |
0, 0, 0, |
| 485 |
1, 0, 0, |
| 486 |
0, 0, -1 |
| 487 |
}; |
| 488 |
int nv = 3; |
| 489 |
int walkable_tri[] = { 0, 1, 2 }; |
| 490 |
int unwalkable_tri[] = { 0, 2, 1 }; |
| 491 |
int nt = 1; |
| 492 |
unsigned char areas[] = { 42 }; |
| 493 |
|
| 494 |
SECTION("Sets area ID of unwalkable triangle to RC_NULL_AREA") |
| 495 |
{ |
| 496 |
rcClearUnwalkableTriangles(ctx, walkableSlopeAngle, verts, nv, unwalkable_tri, nt, areas); |
| 497 |
REQUIRE(areas[0] == RC_NULL_AREA); |
| 498 |
} |
| 499 |
|
| 500 |
SECTION("Does not modify walkable triangle aread ID's") |
| 501 |
{ |
| 502 |
rcClearUnwalkableTriangles(ctx, walkableSlopeAngle, verts, nv, walkable_tri, nt, areas); |
| 503 |
REQUIRE(areas[0] == 42); |
| 504 |
} |
| 505 |
|
| 506 |
SECTION("Slopes equal to the max slope are considered unwalkable.") |
| 507 |
{ |
| 508 |
walkableSlopeAngle = 0; |
| 509 |
rcClearUnwalkableTriangles(ctx, walkableSlopeAngle, verts, nv, walkable_tri, nt, areas); |
| 510 |
REQUIRE(areas[0] == RC_NULL_AREA); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| •514 |
TEST_CASE("rcAddSpan", "[recast]") |
| 515 |
{ |
| 516 |
rcContext ctx(false); |
| 517 |
|
| 518 |
float verts[] = { |
| 519 |
1, 2, 3, |
| 520 |
0, 2, 6 |
| 521 |
}; |
| 522 |
float bmin[3]; |
| 523 |
float bmax[3]; |
| 524 |
rcCalcBounds(verts, 2, bmin, bmax); |
| 525 |
|
| 526 |
float cellSize = 1.5f; |
| 527 |
float cellHeight = 2; |
| 528 |
|
| 529 |
int width; |
| 530 |
int height; |
| 531 |
|
| 532 |
rcCalcGridSize(bmin, bmax, cellSize, &width, &height); |
| 533 |
|
| 534 |
rcHeightfield hf; |
| 535 |
REQUIRE(rcCreateHeightfield(&ctx, hf, width, height, bmin, bmax, cellSize, cellHeight)); |
| 536 |
|
| 537 |
int x = 0; |
| 538 |
int y = 0; |
| 539 |
unsigned short smin = 0; |
| 540 |
unsigned short smax = 1; |
| 541 |
unsigned char area = 42; |
| 542 |
int flagMergeThr = 1; |
| 543 |
|
| 544 |
SECTION("Add a span to an empty heightfield.") |
| 545 |
{ |
| 546 |
bool result = rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr); |
| 547 |
REQUIRE(result); |
| 548 |
REQUIRE(hf.spans[0] != 0); |
| 549 |
REQUIRE(hf.spans[0]->smin == smin); |
| 550 |
REQUIRE(hf.spans[0]->smax == smax); |
| 551 |
REQUIRE(hf.spans[0]->area == area); |
| 552 |
} |
| 553 |
|
| 554 |
SECTION("Add a span that gets merged with an existing span.") |
| 555 |
{ |
| 556 |
bool result = rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr); |
| 557 |
REQUIRE(result); |
| 558 |
REQUIRE(hf.spans[0] != 0); |
| 559 |
REQUIRE(hf.spans[0]->smin == smin); |
| 560 |
REQUIRE(hf.spans[0]->smax == smax); |
| 561 |
REQUIRE(hf.spans[0]->area == area); |
| 562 |
|
| 563 |
smin = 1; |
| 564 |
smax = 2; |
| 565 |
result = rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr); |
| 566 |
REQUIRE(result); |
| 567 |
REQUIRE(hf.spans[0] != 0); |
| 568 |
REQUIRE(hf.spans[0]->smin == 0); |
| 569 |
REQUIRE(hf.spans[0]->smax == 2); |
| 570 |
REQUIRE(hf.spans[0]->area == area); |
| 571 |
} |
| 572 |
|
| 573 |
SECTION("Add a span that merges with two spans above and below.") |
| 574 |
{ |
| 575 |
smin = 0; |
| 576 |
smax = 1; |
| 577 |
REQUIRE(rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr)); |
| 578 |
REQUIRE(hf.spans[0] != 0); |
| 579 |
REQUIRE(hf.spans[0]->smin == smin); |
| 580 |
REQUIRE(hf.spans[0]->smax == smax); |
| 581 |
REQUIRE(hf.spans[0]->area == area); |
| 582 |
REQUIRE(hf.spans[0]->next == 0); |
| 583 |
|
| 584 |
smin = 2; |
| 585 |
smax = 3; |
| 586 |
REQUIRE(rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr)); |
| 587 |
REQUIRE(hf.spans[0]->next != 0); |
| 588 |
REQUIRE(hf.spans[0]->next->smin == smin); |
| 589 |
REQUIRE(hf.spans[0]->next->smax == smax); |
| 590 |
REQUIRE(hf.spans[0]->next->area == area); |
| 591 |
|
| 592 |
smin = 1; |
| 593 |
smax = 2; |
| 594 |
REQUIRE(rcAddSpan(&ctx, hf, x, y, smin, smax, area, flagMergeThr)); |
| 595 |
REQUIRE(hf.spans[0] != 0); |
| 596 |
REQUIRE(hf.spans[0]->smin == 0); |
| 597 |
REQUIRE(hf.spans[0]->smax == 3); |
| 598 |
REQUIRE(hf.spans[0]->area == area); |
| 599 |
REQUIRE(hf.spans[0]->next == 0); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| •603 |
TEST_CASE("rcRasterizeTriangle", "[recast]") |
| 604 |
{ |
| 605 |
rcContext ctx; |
| 606 |
float verts[] = { |
| 607 |
0, 0, 0, |
| 608 |
1, 0, 0, |
| 609 |
0, 0, -1 |
| 610 |
}; |
| 611 |
float bmin[3]; |
| 612 |
float bmax[3]; |
| 613 |
rcCalcBounds(verts, 3, bmin, bmax); |
| 614 |
|
| 615 |
float cellSize = .5f; |
| 616 |
float cellHeight = .5f; |
| 617 |
|
| 618 |
int width; |
| 619 |
int height; |
| 620 |
|
| 621 |
rcCalcGridSize(bmin, bmax, cellSize, &width, &height); |
| 622 |
|
| 623 |
rcHeightfield solid; |
| 624 |
REQUIRE(rcCreateHeightfield(&ctx, solid, width, height, bmin, bmax, cellSize, cellHeight)); |
| 625 |
|
| 626 |
unsigned char area = 42; |
| 627 |
int flagMergeThr = 1; |
| 628 |
|
| 629 |
SECTION("Rasterize a triangle") |
| 630 |
{ |
| 631 |
REQUIRE(rcRasterizeTriangle(&ctx, &verts[0], &verts[3], &verts[6], area, solid, flagMergeThr)); |
| 632 |
|
| 633 |
REQUIRE(solid.spans[0 + 0 * width]); |
| 634 |
REQUIRE(!solid.spans[1 + 0 * width]); |
| 635 |
REQUIRE(solid.spans[0 + 1 * width]); |
| 636 |
REQUIRE(solid.spans[1 + 1 * width]); |
| 637 |
|
| 638 |
REQUIRE(solid.spans[0 + 0 * width]->smin == 0); |
| 639 |
REQUIRE(solid.spans[0 + 0 * width]->smax == 1); |
| 640 |
REQUIRE(solid.spans[0 + 0 * width]->area == area); |
| 641 |
REQUIRE(!solid.spans[0 + 0 * width]->next); |
| 642 |
|
| 643 |
REQUIRE(solid.spans[0 + 1 * width]->smin == 0); |
| 644 |
REQUIRE(solid.spans[0 + 1 * width]->smax == 1); |
| 645 |
REQUIRE(solid.spans[0 + 1 * width]->area == area); |
| 646 |
REQUIRE(!solid.spans[0 + 1 * width]->next); |
| 647 |
|
| 648 |
REQUIRE(solid.spans[1 + 1 * width]->smin == 0); |
| 649 |
REQUIRE(solid.spans[1 + 1 * width]->smax == 1); |
| 650 |
REQUIRE(solid.spans[1 + 1 * width]->area == area); |
| 651 |
REQUIRE(!solid.spans[1 + 1 * width]->next); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| •655 |
TEST_CASE("rcRasterizeTriangle overlapping bb but non-overlapping triangle", "[recast]") |
| 656 |
{ |
| 657 |
// This is a minimal repro case for the issue fixed in PR #476 (https://github.com/recastnavigation/recastnavigation/pull/476) |
| 658 |
rcContext ctx; |
| 659 |
|
| 660 |
// create a heightfield |
| 661 |
float cellSize = 1; |
| 662 |
float cellHeight = 1; |
| 663 |
int width = 10; |
| 664 |
int height = 10; |
| 665 |
float bmin[] = { 0, 0, 0 }; |
| 666 |
float bmax[] = { 10, 10, 10 }; |
| 667 |
rcHeightfield heightfield; |
| 668 |
REQUIRE(rcCreateHeightfield(&ctx, heightfield, width, height, bmin, bmax, cellSize, cellHeight)); |
| 669 |
|
| 670 |
// rasterize a triangle outside of the heightfield. |
| 671 |
unsigned char area = 42; |
| 672 |
int flagMergeThr = 1; |
| 673 |
float verts[] = |
| 674 |
{ |
| 675 |
-10.0, 5.5, -10.0, |
| 676 |
-10.0, 5.5, 3, |
| 677 |
3.0, 5.5, -10.0 |
| 678 |
}; |
| 679 |
REQUIRE(rcRasterizeTriangle(&ctx, &verts[0], &verts[3], &verts[6], area, heightfield, flagMergeThr)); |
| 680 |
|
| 681 |
// ensure that no spans were created |
| 682 |
for (int x = 0; x < width; ++x) |
| 683 |
{ |
| 684 |
for (int z = 0; z < height; ++z) |
| 685 |
{ |
| 686 |
rcSpan* span = heightfield.spans[x + z * heightfield.width]; |
| 687 |
REQUIRE(span == NULL); |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
|
| •692 |
TEST_CASE("rcRasterizeTriangle smaller than half a voxel size in x", "[recast]") |
| 693 |
{ |
| 694 |
SECTION("Skinny triangle along x axis") |
| 695 |
{ |
| 696 |
rcContext ctx; |
| 697 |
float verts[] = { |
| 698 |
5, 0, 0.005f, |
| 699 |
5, 0, -0.005f, |
| 700 |
-5, 0, 0.005f, |
| 701 |
|
| 702 |
-5, 0, 0.005f, |
| 703 |
5, 0, -0.005f, |
| 704 |
-5, 0, -0.005f, |
| 705 |
}; |
| 706 |
float bmin[3]; |
| 707 |
float bmax[3]; |
| 708 |
rcCalcBounds(verts, 3, bmin, bmax); |
| 709 |
|
| 710 |
float cellSize = 1; |
| 711 |
float cellHeight = 1; |
| 712 |
|
| 713 |
int width; |
| 714 |
int height; |
| 715 |
|
| 716 |
rcCalcGridSize(bmin, bmax, cellSize, &width, &height); |
| 717 |
|
| 718 |
rcHeightfield solid; |
| 719 |
REQUIRE(rcCreateHeightfield(&ctx, solid, width, height, bmin, bmax, cellSize, cellHeight)); |
| 720 |
|
| 721 |
unsigned char areas[] = {42, 42}; |
| 722 |
int flagMergeThr = 1; |
| 723 |
REQUIRE(rcRasterizeTriangles(&ctx, verts, areas, 2, solid, flagMergeThr)); |
| 724 |
} |
| 725 |
|
| 726 |
SECTION("Skinny triangle along z axis") |
| 727 |
{ |
| 728 |
rcContext ctx; |
| 729 |
float verts[] = { |
| 730 |
0.005f, 0, 5, |
| 731 |
-0.005f, 0, 5, |
| 732 |
0.005f, 0, -5, |
| 733 |
|
| 734 |
0.005f, 0, -5, |
| 735 |
-0.005f, 0, 5, |
| 736 |
-0.005f, 0, -5 |
| 737 |
}; |
| 738 |
float bmin[3]; |
| 739 |
float bmax[3]; |
| 740 |
rcCalcBounds(verts, 3, bmin, bmax); |
| 741 |
|
| 742 |
float cellSize = 1; |
| 743 |
float cellHeight = 1; |
| 744 |
|
| 745 |
int width; |
| 746 |
int height; |
| 747 |
|
| 748 |
rcCalcGridSize(bmin, bmax, cellSize, &width, &height); |
| 749 |
|
| 750 |
rcHeightfield solid; |
| 751 |
REQUIRE(rcCreateHeightfield(&ctx, solid, width, height, bmin, bmax, cellSize, cellHeight)); |
| 752 |
|
| 753 |
unsigned char areas[] = {42, 42}; |
| 754 |
int flagMergeThr = 1; |
| 755 |
REQUIRE(rcRasterizeTriangles(&ctx, verts, areas, 2, solid, flagMergeThr)); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| •759 |
TEST_CASE("rcRasterizeTriangles", "[recast]") |
| 760 |
{ |
| 761 |
rcContext ctx; |
| 762 |
float verts[] = { |
| 763 |
0, 0, 0, |
| 764 |
1, 0, 0, |
| 765 |
0, 0, -1, |
| 766 |
0, 0, 1 |
| 767 |
}; |
| 768 |
int tris[] = { |
| 769 |
0, 1, 2, |
| 770 |
0, 3, 1 |
| 771 |
}; |
| 772 |
unsigned char areas[] = { |
| 773 |
1, |
| 774 |
2 |
| 775 |
}; |
| 776 |
float bmin[3]; |
| 777 |
float bmax[3]; |
| 778 |
rcCalcBounds(verts, 4, bmin, bmax); |
| 779 |
|
| 780 |
float cellSize = .5f; |
| 781 |
float cellHeight = .5f; |
| 782 |
|
| 783 |
int width; |
| 784 |
int height; |
| 785 |
|
| 786 |
rcCalcGridSize(bmin, bmax, cellSize, &width, &height); |
| 787 |
|
| 788 |
rcHeightfield solid; |
| 789 |
REQUIRE(rcCreateHeightfield(&ctx, solid, width, height, bmin, bmax, cellSize, cellHeight)); |
| 790 |
|
| 791 |
int flagMergeThr = 1; |
| 792 |
|
| 793 |
SECTION("Rasterize some triangles") |
| 794 |
{ |
| 795 |
REQUIRE(rcRasterizeTriangles(&ctx, verts, 4, tris, areas, 2, solid, flagMergeThr)); |
| 796 |
|
| 797 |
REQUIRE(solid.spans[0 + 0 * width]); |
| 798 |
REQUIRE(solid.spans[0 + 1 * width]); |
| 799 |
REQUIRE(solid.spans[0 + 2 * width]); |
| 800 |
REQUIRE(solid.spans[0 + 3 * width]); |
| 801 |
REQUIRE(!solid.spans[1 + 0 * width]); |
| 802 |
REQUIRE(solid.spans[1 + 1 * width]); |
| 803 |
REQUIRE(solid.spans[1 + 2 * width]); |
| 804 |
REQUIRE(!solid.spans[1 + 3 * width]); |
| 805 |
|
| 806 |
REQUIRE(solid.spans[0 + 0 * width]->smin == 0); |
| 807 |
REQUIRE(solid.spans[0 + 0 * width]->smax == 1); |
| 808 |
REQUIRE(solid.spans[0 + 0 * width]->area == 1); |
| 809 |
REQUIRE(!solid.spans[0 + 0 * width]->next); |
| 810 |
|
| 811 |
REQUIRE(solid.spans[0 + 1 * width]->smin == 0); |
| 812 |
REQUIRE(solid.spans[0 + 1 * width]->smax == 1); |
| 813 |
REQUIRE(solid.spans[0 + 1 * width]->area == 1); |
| 814 |
REQUIRE(!solid.spans[0 + 1 * width]->next); |
| 815 |
|
| 816 |
REQUIRE(solid.spans[0 + 2 * width]->smin == 0); |
| 817 |
REQUIRE(solid.spans[0 + 2 * width]->smax == 1); |
| 818 |
REQUIRE(solid.spans[0 + 2 * width]->area == 2); |
| 819 |
REQUIRE(!solid.spans[0 + 2 * width]->next); |
| 820 |
|
| 821 |
REQUIRE(solid.spans[0 + 3 * width]->smin == 0); |
| 822 |
REQUIRE(solid.spans[0 + 3 * width]->smax == 1); |
| 823 |
REQUIRE(solid.spans[0 + 3 * width]->area == 2); |
| 824 |
REQUIRE(!solid.spans[0 + 3 * width]->next); |
| 825 |
|
| 826 |
REQUIRE(solid.spans[1 + 1 * width]->smin == 0); |
| 827 |
REQUIRE(solid.spans[1 + 1 * width]->smax == 1); |
| 828 |
REQUIRE(solid.spans[1 + 1 * width]->area == 1); |
| 829 |
REQUIRE(!solid.spans[1 + 1 * width]->next); |
| 830 |
|
| 831 |
REQUIRE(solid.spans[1 + 2 * width]->smin == 0); |
| 832 |
REQUIRE(solid.spans[1 + 2 * width]->smax == 1); |
| 833 |
REQUIRE(solid.spans[1 + 2 * width]->area == 2); |
| 834 |
REQUIRE(!solid.spans[1 + 2 * width]->next); |
| 835 |
} |
| 836 |
|
| 837 |
SECTION("Unsigned short overload") |
| 838 |
{ |
| 839 |
unsigned short utris[] = { |
| 840 |
0, 1, 2, |
| 841 |
0, 3, 1 |
| 842 |
}; |
| 843 |
REQUIRE(rcRasterizeTriangles(&ctx, verts, 4, utris, areas, 2, solid, flagMergeThr)); |
| 844 |
|
| 845 |
REQUIRE(solid.spans[0 + 0 * width]); |
| 846 |
REQUIRE(solid.spans[0 + 1 * width]); |
| 847 |
REQUIRE(solid.spans[0 + 2 * width]); |
| 848 |
REQUIRE(solid.spans[0 + 3 * width]); |
| 849 |
REQUIRE(!solid.spans[1 + 0 * width]); |
| 850 |
REQUIRE(solid.spans[1 + 1 * width]); |
| 851 |
REQUIRE(solid.spans[1 + 2 * width]); |
| 852 |
REQUIRE(!solid.spans[1 + 3 * width]); |
| 853 |
|
| 854 |
REQUIRE(solid.spans[0 + 0 * width]->smin == 0); |
| 855 |
REQUIRE(solid.spans[0 + 0 * width]->smax == 1); |
| 856 |
REQUIRE(solid.spans[0 + 0 * width]->area == 1); |
| 857 |
REQUIRE(!solid.spans[0 + 0 * width]->next); |
| 858 |
|
| 859 |
REQUIRE(solid.spans[0 + 1 * width]->smin == 0); |
| 860 |
REQUIRE(solid.spans[0 + 1 * width]->smax == 1); |
| 861 |
REQUIRE(solid.spans[0 + 1 * width]->area == 1); |
| 862 |
REQUIRE(!solid.spans[0 + 1 * width]->next); |
| 863 |
|
| 864 |
REQUIRE(solid.spans[0 + 2 * width]->smin == 0); |
| 865 |
REQUIRE(solid.spans[0 + 2 * width]->smax == 1); |
| 866 |
REQUIRE(solid.spans[0 + 2 * width]->area == 2); |
| 867 |
REQUIRE(!solid.spans[0 + 2 * width]->next); |
| 868 |
|
| 869 |
REQUIRE(solid.spans[0 + 3 * width]->smin == 0); |
| 870 |
REQUIRE(solid.spans[0 + 3 * width]->smax == 1); |
| 871 |
REQUIRE(solid.spans[0 + 3 * width]->area == 2); |
| 872 |
REQUIRE(!solid.spans[0 + 3 * width]->next); |
| 873 |
|
| 874 |
REQUIRE(solid.spans[1 + 1 * width]->smin == 0); |
| 875 |
REQUIRE(solid.spans[1 + 1 * width]->smax == 1); |
| 876 |
REQUIRE(solid.spans[1 + 1 * width]->area == 1); |
| 877 |
REQUIRE(!solid.spans[1 + 1 * width]->next); |
| 878 |
|
| 879 |
REQUIRE(solid.spans[1 + 2 * width]->smin == 0); |
| 880 |
REQUIRE(solid.spans[1 + 2 * width]->smax == 1); |
| 881 |
REQUIRE(solid.spans[1 + 2 * width]->area == 2); |
| 882 |
REQUIRE(!solid.spans[1 + 2 * width]->next); |
| 883 |
} |
| 884 |
|
| 885 |
SECTION("Triangle list overload") |
| 886 |
{ |
| 887 |
float vertsList[] = { |
| 888 |
0, 0, 0, |
| 889 |
1, 0, 0, |
| 890 |
0, 0, -1, |
| 891 |
0, 0, 0, |
| 892 |
0, 0, 1, |
| 893 |
1, 0, 0, |
| 894 |
}; |
| 895 |
|
| 896 |
REQUIRE(rcRasterizeTriangles(&ctx, vertsList, areas, 2, solid, flagMergeThr)); |
| 897 |
|
| 898 |
REQUIRE(solid.spans[0 + 0 * width]); |
| 899 |
REQUIRE(solid.spans[0 + 1 * width]); |
| 900 |
REQUIRE(solid.spans[0 + 2 * width]); |
| 901 |
REQUIRE(solid.spans[0 + 3 * width]); |
| 902 |
REQUIRE(!solid.spans[1 + 0 * width]); |
| 903 |
REQUIRE(solid.spans[1 + 1 * width]); |
| 904 |
REQUIRE(solid.spans[1 + 2 * width]); |
| 905 |
REQUIRE(!solid.spans[1 + 3 * width]); |
| 906 |
|
| 907 |
REQUIRE(solid.spans[0 + 0 * width]->smin == 0); |
| 908 |
REQUIRE(solid.spans[0 + 0 * width]->smax == 1); |
| 909 |
REQUIRE(solid.spans[0 + 0 * width]->area == 1); |
| 910 |
REQUIRE(!solid.spans[0 + 0 * width]->next); |
| 911 |
|
| 912 |
REQUIRE(solid.spans[0 + 1 * width]->smin == 0); |
| 913 |
REQUIRE(solid.spans[0 + 1 * width]->smax == 1); |
| 914 |
REQUIRE(solid.spans[0 + 1 * width]->area == 1); |
| 915 |
REQUIRE(!solid.spans[0 + 1 * width]->next); |
| 916 |
|
| 917 |
REQUIRE(solid.spans[0 + 2 * width]->smin == 0); |
| 918 |
REQUIRE(solid.spans[0 + 2 * width]->smax == 1); |
| 919 |
REQUIRE(solid.spans[0 + 2 * width]->area == 2); |
| 920 |
REQUIRE(!solid.spans[0 + 2 * width]->next); |
| 921 |
|
| 922 |
REQUIRE(solid.spans[0 + 3 * width]->smin == 0); |
| 923 |
REQUIRE(solid.spans[0 + 3 * width]->smax == 1); |
| 924 |
REQUIRE(solid.spans[0 + 3 * width]->area == 2); |
| 925 |
REQUIRE(!solid.spans[0 + 3 * width]->next); |
| 926 |
|
| 927 |
REQUIRE(solid.spans[1 + 1 * width]->smin == 0); |
| 928 |
REQUIRE(solid.spans[1 + 1 * width]->smax == 1); |
| 929 |
REQUIRE(solid.spans[1 + 1 * width]->area == 1); |
| 930 |
REQUIRE(!solid.spans[1 + 1 * width]->next); |
| 931 |
|
| 932 |
REQUIRE(solid.spans[1 + 2 * width]->smin == 0); |
| 933 |
REQUIRE(solid.spans[1 + 2 * width]->smax == 1); |
| 934 |
REQUIRE(solid.spans[1 + 2 * width]->area == 2); |
| 935 |
REQUIRE(!solid.spans[1 + 2 * width]->next); |
| 936 |
} |
| 937 |
} |