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