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 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* Copyright (C) 2006-2010 - Frictional Games
*
* This file is part of HPL1 Engine.
*/
#include "hpl1/engine/math/Math.h"
#include "hpl1/engine/system/low_level_system.h"
#include "hpl1/hpl1.h"
namespace hpl {
static char mpTempChar[1024];
//////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------
int cMath::RandRectl(int alMin, int alMax) {
return Hpl1::g_engine->getRandomNumber(alMax - alMin) + alMin;
}
//-----------------------------------------------------------------------
float cMath::RandRectf(float afMin, float afMax) {
float fRand = static_cast<float>(Hpl1::g_engine->getRandomNumber(UINT_MAX)) / static_cast<float>(UINT_MAX);
return afMin + fRand * (afMax - afMin);
}
//-----------------------------------------------------------------------
cVector2f cMath::RandRectVector2f(const cVector3f &avMin, const cVector3f &avMax) {
return cVector2f(RandRectf(avMin.x, avMax.x),
RandRectf(avMin.y, avMax.y));
}
//-----------------------------------------------------------------------
cVector3f cMath::RandRectVector3f(const cVector3f &avMin, const cVector3f &avMax) {
return cVector3f(RandRectf(avMin.x, avMax.x),
RandRectf(avMin.y, avMax.y),
RandRectf(avMin.z, avMax.z));
}
//-----------------------------------------------------------------------
cColor cMath::RandRectColor(const cColor &aMin, const cColor &aMax) {
return cColor(RandRectf(aMin.r, aMax.r),
RandRectf(aMin.g, aMax.g),
RandRectf(aMin.b, aMax.b),
RandRectf(aMin.a, aMax.a));
}
//-----------------------------------------------------------------------
void cMath::Randomize(int alSeed) {
#if 0
if (alSeed == -1) {
srand((unsigned)time(NULL));
} else {
srand(alSeed);
}
#endif
}
//-----------------------------------------------------------------------
float cMath::Dist2D(const cVector2f &avPosA, const cVector2f &avPosB) {
float fDx = avPosA.x - avPosB.x;
float fDy = avPosA.y - avPosB.y;
return sqrt(fDx * fDx + fDy * fDy);
}
//-----------------------------------------------------------------------
float cMath::Dist2D(const cVector3f &avPosA, const cVector3f &avPosB) {
float fDx = avPosA.x - avPosB.x;
float fDy = avPosA.y - avPosB.y;
return sqrt(fDx * fDx + fDy * fDy);
}
//-----------------------------------------------------------------------
float cMath::SqrDist2D(const cVector2f &avPosA, const cVector2f &avPosB) {
float fDx = avPosA.x - avPosB.x;
float fDy = avPosA.y - avPosB.y;
return fDx * fDx + fDy * fDy;
}
//-----------------------------------------------------------------------
float cMath::SqrDist2D(const cVector3f &avPosA, const cVector3f &avPosB) {
float fDx = avPosA.x - avPosB.x;
float fDy = avPosA.y - avPosB.y;
return fDx * fDx + fDy * fDy;
}
//-----------------------------------------------------------------------
bool cMath::BoxCollision(cRect2l aRect1, cRect2l aRect2) {
return (aRect1.x > aRect2.x + (aRect2.w - 1) || aRect2.x > aRect1.x + (aRect1.w - 1) ||
aRect1.y > aRect2.y + (aRect2.h - 1) || aRect2.y > aRect1.y + (aRect1.h - 1)) == false;
}
//-----------------------------------------------------------------------
bool cMath::BoxCollision(cRect2f aRect1, cRect2f aRect2) {
return (aRect1.x > aRect2.x + (aRect2.w) || aRect2.x > aRect1.x + (aRect1.w) ||
aRect1.y > aRect2.y + (aRect2.h) || aRect2.y > aRect1.y + (aRect1.h)) == false;
}
//-----------------------------------------------------------------------
bool cMath::PointBoxCollision(cVector2f avPoint, cRect2f aRect) {
if (avPoint.x < aRect.x || avPoint.x > aRect.x + aRect.w || avPoint.y < aRect.y || avPoint.y > aRect.y + aRect.h)
return false;
else
return true;
}
//-----------------------------------------------------------------------
bool cMath::BoxFit(cRect2l aRectSrc, cRect2l aRectDest) {
// check is size is smaller and doesn't overlap
if (aRectSrc.w > aRectDest.w || aRectSrc.h > aRectDest.h ||
aRectSrc.x + aRectSrc.w > aRectDest.x + aRectDest.w ||
aRectSrc.y + aRectSrc.h > aRectDest.y + aRectDest.h)
return false;
// check if x,y is in borders
if (aRectSrc.x < aRectDest.x || aRectSrc.y < aRectDest.y ||
aRectSrc.x > aRectDest.x + aRectDest.w || aRectSrc.y > aRectDest.y + aRectDest.h)
return false;
return true;
}
//-----------------------------------------------------------------------
bool cMath::BoxFit(cRect2f aRectSrc, cRect2f aRectDest) {
// check is size is smaller and doesn't overlap
if (aRectSrc.w > aRectDest.w || aRectSrc.h > aRectDest.h ||
aRectSrc.x + aRectSrc.w > aRectDest.x + aRectDest.w ||
aRectSrc.y + aRectSrc.h > aRectDest.y + aRectDest.h)
return false;
// check if x,y is in borders
if (aRectSrc.x < aRectDest.x || aRectSrc.y < aRectDest.y ||
aRectSrc.x > aRectDest.x + aRectDest.w || aRectSrc.y > aRectDest.y + aRectDest.h)
return false;
return true;
}
//-----------------------------------------------------------------------
cRect2f &cMath::ClipRect(cRect2f &aRectSrc, const cRect2f &aRectDest) {
if (aRectSrc.x < aRectDest.x) {
aRectSrc.w -= aRectDest.x - aRectSrc.x;
aRectSrc.x = aRectDest.x;
}
if (aRectSrc.y < aRectDest.y) {
aRectSrc.h -= aRectDest.y - aRectSrc.y;
aRectSrc.y = aRectDest.y;
}
if (aRectSrc.x + aRectSrc.w > aRectDest.x + aRectDest.w) {
aRectSrc.w -= (aRectSrc.x + aRectSrc.w) - (aRectDest.x + aRectDest.w);
}
if (aRectSrc.y + aRectSrc.h > aRectDest.y + aRectDest.h) {
aRectSrc.h -= (aRectSrc.y + aRectSrc.h) - (aRectDest.y + aRectDest.h);
}
return aRectSrc;
}
//-----------------------------------------------------------------------
bool cMath::CheckCollisionBV(cBoundingVolume &aBV1, cBoundingVolume &aBV2) {
//////////////////////////////////////////
// Check Sphere collision.
// Not needed I think.
/*float fRadiusSum = aBV1->GetRadius() + aBV2->GetRadius();
cVector3f vSepAxis = aBV1->GetWorldCenter() - aBV2->GetWorldCenter();
if(vSepAxis.SqrLength() > (fRadiusSum * fRadiusSum))
{
return eBVCollision_Outside;
}*/
///////////////////////////////////////////
// Check box collision
aBV1.UpdateSize();
aBV2.UpdateSize();
const cVector3f &vMin1 = aBV1.mvWorldMin;
const cVector3f &vMin2 = aBV2.mvWorldMin;
const cVector3f &vMax1 = aBV1.mvWorldMax;
const cVector3f &vMax2 = aBV2.mvWorldMax;
// Log("Min1: %s Max1: %s vs Min2: %s Max2: %s\n", vMin1.ToString().c_str(),vMax1.ToString().c_str(),
// vMin2.ToString().c_str(), vMax2.ToString().c_str());
if (vMax1.x < vMin2.x || vMax1.y < vMin2.y || vMax1.z < vMin2.z ||
vMax2.x < vMin1.x || vMax2.y < vMin1.y || vMax2.z < vMin1.z) {
return false;
}
return true;
}
//-----------------------------------------------------------------------
bool cMath::PointBVCollision(const cVector3f &avPoint, cBoundingVolume &aBV) {
cVector3f vMax = aBV.GetMax();
cVector3f vMin = aBV.GetMin();
if (avPoint.x > vMax.x || avPoint.y > vMax.y || avPoint.z > vMax.z ||
avPoint.x < vMin.x || avPoint.y < vMin.y || avPoint.z < vMin.z) {
return false;
}
return true;
}
//-----------------------------------------------------------------------
bool cMath::GetClipRectFromBV(cRect2l &aDestRect, cBoundingVolume &aBV,
const cMatrixf &a_mtxView, const cMatrixf &a_mtxProj,
float afNearClipPlane, const cVector2l &avScreenSize) {
cVector3f vMax = aBV.GetMax();
cVector3f vMin = aBV.GetMin();
cVector3f vCorners[8];
vCorners[0] = cVector3f(vMax.x, vMax.y, vMax.z);
vCorners[1] = cVector3f(vMax.x, vMax.y, vMin.z);
vCorners[2] = cVector3f(vMax.x, vMin.y, vMax.z);
vCorners[3] = cVector3f(vMax.x, vMin.y, vMin.z);
vCorners[4] = cVector3f(vMin.x, vMax.y, vMax.z);
vCorners[5] = cVector3f(vMin.x, vMax.y, vMin.z);
vCorners[6] = cVector3f(vMin.x, vMin.y, vMax.z);
vCorners[7] = cVector3f(vMin.x, vMin.y, vMin.z);
bool bVisible = true;
float fMaxZ = -afNearClipPlane;
for (int i = 0; i < 8; i++) {
vCorners[i] = MatrixMul(a_mtxView, vCorners[i]);
if (vCorners[i].z > fMaxZ) {
vCorners[i].z = 0;
bVisible = false; // we are inside the light
}
vCorners[i] = MatrixMulDivideW(a_mtxProj, vCorners[i]);
}
if (bVisible == false)
return false;
vMax = vCorners[0];
vMin = vCorners[0];
for (int i = 1; i < 8; i++) {
if (vCorners[i].x < vMin.x)
vMin.x = vCorners[i].x;
if (vCorners[i].x > vMax.x)
vMax.x = vCorners[i].x;
// Y
if (vCorners[i].y < vMin.y)
vMin.y = vCorners[i].y;
if (vCorners[i].y > vMax.y)
vMax.y = vCorners[i].y;
}
// Clip min and max
if (vMin.x < -1)
vMin.x = -1;
if (vMin.y < -1)
vMin.y = -1;
if (vMax.x > 1)
vMax.x = 1;
if (vMax.y > 1)
vMax.y = 1;
// Get the screen coordinates
cVector2f vHalfScreenSize = cVector2f((float)avScreenSize.x, (float)avScreenSize.y) * 0.5f;
cVector2l vTopLeft;
cVector2l vBottomRight;
vTopLeft.x = (int)(vHalfScreenSize.x + vMin.x * vHalfScreenSize.x);
vTopLeft.y = (int)(vHalfScreenSize.y + vMax.y * -vHalfScreenSize.y);
vBottomRight.x = (int)(vHalfScreenSize.x + vMax.x * vHalfScreenSize.x);
vBottomRight.y = (int)(vHalfScreenSize.y + vMin.y * -vHalfScreenSize.y);
// Clip the screen coordinates
if (vTopLeft.x < 0)
vTopLeft.x = 0;
if (vTopLeft.y < 0)
vTopLeft.y = 0;
if (vBottomRight.x > avScreenSize.x - 1)
vBottomRight.x = avScreenSize.x - 1;
if (vBottomRight.y > avScreenSize.y - 1)
vBottomRight.y = avScreenSize.y - 1;
aDestRect.x = vTopLeft.x;
aDestRect.y = vTopLeft.y;
aDestRect.w = (vBottomRight.x - vTopLeft.x) + 1;
aDestRect.h = (vBottomRight.y - vTopLeft.y) + 1;
return true;
}
//-----------------------------------------------------------------------
bool cMath::CheckSphereInPlanes(const cVector3f &avCenter, float afRadius,
const cPlanef *apPlanes, int alPlaneCount) {
for (int i = 0; i < alPlaneCount; i++) {
float fDist = cMath::PlaneToPointDist(apPlanes[i], avCenter);
if (fDist < -afRadius) {
return false;
}
}
return true;
}
//-----------------------------------------------------------------------
float cMath::GetFraction(float afVal) {
return afVal - floor(afVal);
}
//-----------------------------------------------------------------------
float cMath::Modulus(float afDividend, float afDivisor) {
float fNum = floor(ABS(afDividend / afDivisor));
float fRemain = ABS(afDividend) - ABS(afDivisor) * fNum;
return fRemain;
}
//-----------------------------------------------------------------------
float cMath::Wrap(float afX, float afMin, float afMax) {
// Quick check if value is okay. If so just return.
if (afX >= afMin && afX <= afMax)
return afX;
// Change setup so that min is 0
afMax = afMax - afMin;
float fOffSet = afMin;
afMin = 0;
afX = afX - fOffSet;
float fNumOfMax = floor(ABS(afX / afMax));
if (afX >= afMax)
afX = afX - fNumOfMax * afMax;
else if (afX < afMin)
afX = ((fNumOfMax + 1.0f) * afMax) + afX;
return afX + fOffSet;
}
//-----------------------------------------------------------------------
float cMath::Clamp(float afX, float afMin, float afMax) {
if (afX > afMax)
afX = afMax;
else if (afX < afMin)
afX = afMin;
return afX;
}
//-----------------------------------------------------------------------
float cMath::GetAngleDistanceRad(float afAngle1, float afAngle2) {
return GetAngleDistance(afAngle1, afAngle2, k2Pif);
}
float cMath::GetAngleDistanceDeg(float afAngle1, float afAngle2) {
return GetAngleDistance(afAngle1, afAngle2, 360.0f);
}
float cMath::GetAngleDistance(float afAngle1, float afAngle2, float afMaxAngle) {
afAngle1 = Wrap(afAngle1, 0, afMaxAngle);
afAngle2 = Wrap(afAngle2, 0, afMaxAngle);
if (afAngle1 == afAngle2) {
return 0;
} else {
float fDist1 = afAngle2 - afAngle1;
float fDist2 = afMaxAngle - ABS(fDist1);
if (fDist1 > 0)
fDist2 = -fDist2;
if (ABS(fDist1) < ABS(fDist2))
return fDist1;
else
return fDist2;
}
}
//-----------------------------------------------------------------------
float cMath::TurnAngle(float afAngle, float afFinalAngle, float afSpeed, float afMaxAngle) {
if (afAngle != afFinalAngle) {
float fAngleDist = afFinalAngle - afAngle;
if ((afFinalAngle > afAngle && fAngleDist < afMaxAngle) ||
(afFinalAngle < afAngle && fAngleDist < (-afMaxAngle)))
afAngle = afAngle + afSpeed;
else
afAngle = afAngle + (-afSpeed);
}
if (Abs(GetAngleDistance(afAngle, afFinalAngle, afMaxAngle * 2)) <= (afSpeed * 1.5))
afAngle = afFinalAngle;
return afAngle;
}
float cMath::TurnAngleRad(float afAngle, float afFinalAngle, float afSpeed) {
return TurnAngle(afAngle, afFinalAngle, afSpeed, kPif);
}
float cMath::TurnAngleDeg(float afAngle, float afFinalAngle, float afSpeed) {
return TurnAngle(afAngle, afFinalAngle, afSpeed, 180.0f);
}
//-----------------------------------------------------------------------
float cMath::GetAngleFromPoints2D(const cVector2f &avStartPos, const cVector2f &avGoalPos) {
float fDx;
float fDy;
float fAns = 0.0;
fDx = avGoalPos.x - avStartPos.x;
fDy = avGoalPos.y - avStartPos.y;
if (fDx == 0)
fDx = 0.00001f;
if (fDy == 0)
fDy = 0.00001f;
if (fDx >= 0 && fDy < 0) {
fAns = atan(fDx / (-fDy));
// Log("1\n");
} else if (fDx >= 0 && fDy >= 0) {
fAns = atan(fDy / fDx) + kPi2f;
// Log("2\n");
} else if (fDx < 0 && fDy >= 0) {
fAns = atan((-fDx) / fDy) + kPif;
// Log("3\n");
} else if (fDx < 0 && fDy < 0) {
fAns = atan((-fDy) / (-fDx)) + kPi2f + kPif;
// Log("4\n");
}
return fAns;
}
//-----------------------------------------------------------------------
cVector2f cMath::GetVectorFromAngle2D(float afAngle, float afLength) {
cVector2f vVec;
vVec.x = sin(afAngle) * afLength;
vVec.y = -cos(afAngle) * afLength;
return vVec;
}
//-----------------------------------------------------------------------
void cMath::GetAngleFromVector(const cVector2f &avVec, float *apAngle, float *apLength) {
*apLength = sqrt(avVec.x * avVec.x + avVec.y * avVec.y);
*apAngle = GetAngleFromPoints2D(0, avVec);
}
//-----------------------------------------------------------------------
cVector2f cMath::ProjectVector2D(const cVector2f &avSrcVec, const cVector2f &avDestVec) {
float fTemp = (avSrcVec.x * avDestVec.x + avSrcVec.y * avDestVec.y) /
(avDestVec.x * avDestVec.x + avDestVec.y * avDestVec.y);
return avDestVec * fTemp;
}
//-----------------------------------------------------------------------
float cMath::ToRad(float afAngle) {
return (afAngle / 360.0f) * k2Pif;
}
//-----------------------------------------------------------------------
float cMath::ToDeg(float afAngle) {
return (afAngle / k2Pif) * 360.0f;
}
//-----------------------------------------------------------------------
int cMath::Log2ToInt(int alX) {
switch (alX) {
case 1:
return 0;
case 2:
return 1;
case 4:
return 2;
case 8:
return 3;
case 16:
return 4;
case 32:
return 5;
case 64:
return 6;
case 128:
return 7;
case 256:
return 8;
case 512:
return 9;
default:
return (int)floor((log((float)alX) / log(2.0f)) + 0.5f);
}
// return (int)floor((log((float)aX) / log(2.0f)) + 0.5f);
}
//-----------------------------------------------------------------------
bool cMath::IsPow2(int alX) {
switch (alX) {
case 0:
return true;
case 1:
return true;
case 2:
return true;
case 4:
return true;
case 8:
return true;
case 16:
return true;
case 32:
return true;
case 64:
return true;
case 128:
return true;
case 256:
return true;
case 512:
return true;
case 1024:
return true;
case 2048:
return true;
case 4096:
return true;
case 8192:
return true;
default:
return false;
}
}
//-----------------------------------------------------------------------
float cMath::InterpolateFloat(float afA, float afB, float afT) {
return afA * (1 - afT) + afB * afT;
}
//-----------------------------------------------------------------------
cVector3f cMath::Vector3Cross(const cVector3f &avVecA, const cVector3f &avVecB) {
cVector3f vResult;
vResult.x = avVecA.y * avVecB.z - avVecA.z * avVecB.y;
vResult.y = avVecA.z * avVecB.x - avVecA.x * avVecB.z;
vResult.z = avVecA.x * avVecB.y - avVecA.y * avVecB.x;
return vResult;
}
//-----------------------------------------------------------------------
float cMath::Vector3Dot(const cVector3f &avVecA, const cVector3f &avVecB) {
return avVecA.x * avVecB.x + avVecA.y * avVecB.y + avVecA.z * avVecB.z;
}
//-----------------------------------------------------------------------
cVector3f cMath::ProjectVector3D(const cVector3f &avSrcVec, const cVector3f &avDestVec) {
float fTemp = (avSrcVec.x * avDestVec.x + avSrcVec.y * avDestVec.y + avSrcVec.z * avDestVec.z) /
(avDestVec.x * avDestVec.x + avDestVec.y * avDestVec.y + avDestVec.z * avDestVec.z);
return avDestVec * fTemp;
}
//-----------------------------------------------------------------------
float cMath::Vector3Angle(const cVector3f &avVecA, const cVector3f &avVecB) {
float fCos = Vector3Dot(avVecA, avVecB);
if (ABS(fCos - 1) <= kEpsilonf)
return 0;
return acos(fCos);
}
//-----------------------------------------------------------------------
cVector3f cMath::Vector3UnProject(const cVector3f &avVec, const cRect2f &aScreenRect,
cMatrixf a_mtxViewProj) {
cMatrixf mtxInvViewProj = MatrixInverse(a_mtxViewProj);
cVector3f vNormalized;
vNormalized.x = ((avVec.x - aScreenRect.x) * 2.0f / aScreenRect.w) - 1.0f;
vNormalized.y = -(((avVec.y - aScreenRect.y) * 2.0f / aScreenRect.h) - 1.0f);
vNormalized.z = 2.0f * avVec.z - 1.0f;
// Log("Normalized: %s\n",vNormalized.ToString().c_str());
// Object coordinates.
vNormalized = MatrixMulDivideW(mtxInvViewProj, vNormalized);
// vNormalized = MatrixMul(mtxInvViewProj,vNormalized);
// Log("Normalized After: %s\n",vNormalized.ToString().c_str());
return vNormalized * -1;
}
//-----------------------------------------------------------------------
// Helper function for GetAnglesFromPoints3D
static float GetAngleFromPoints2DSimple(const cVector3f &avStartPos, const cVector3f &avGoalPos) {
cVector3f vDelta = avGoalPos - avStartPos;
if (vDelta.x == 0)
vDelta.x = -kEpsilonf;
if (vDelta.y == 0)
vDelta.y = kEpsilonf;
if (vDelta.y >= 0 && vDelta.x <= 0) {
return -atan(vDelta.y / vDelta.x);
} else if (vDelta.y < 0 && vDelta.x <= 0) {
return -atan(vDelta.y / vDelta.x);
} else {
Error("Error in GetAngle 3D code! ARGHHH run in terror\n");
return -1000;
}
}
cVector3f cMath::GetAngleFromPoints3D(const cVector3f &avStartPos, const cVector3f &avGoalPos) {
cVector3f vAngle = cVector3f(0, 0, 0);
vAngle.y = -GetAngleFromPoints2D(cVector2f(avStartPos.x, avStartPos.z),
cVector2f(avGoalPos.x, avGoalPos.z));
// Log("Y Angle: %f\n",vAngle.y);
// vAngle.y = Wrap(vAngle.y,0.0f, k2Pif);
cMatrixf mtxRot = MatrixRotateY(-vAngle.y);
cVector3f vDelta = avGoalPos - avStartPos;
// Log("vDelta: %s\n",vDelta.ToString().c_str());
cVector3f vGoal = MatrixMul(mtxRot, vDelta);
// Log("vGoal: %s\n",vGoal.ToString().c_str());
vAngle.x = GetAngleFromPoints2DSimple(cVector3f(0, 0, 0), cVector2f(vGoal.z, vGoal.y));
// Log("X Angle: %f\n",vAngle.x);
vAngle.x = Wrap(vAngle.x, 0.0f, k2Pif);
return vAngle;
}
//-----------------------------------------------------------------------
float cMath::PlaneToPointDist(const cPlanef &aPlane, const cVector3f &avVec) {
return (aPlane.a * avVec.x) + (aPlane.b * avVec.y) + (aPlane.c * avVec.z) + aPlane.d;
}
//-----------------------------------------------------------------------
// TODO: This only works when there is no 0 in the planes.
void cMath::PlaneIntersectionLine(const cPlanef &aPA, const cPlanef &aPB,
cVector3f &avDir, cVector3f &avPoint) {
avDir = Vector3Cross(cVector3f(aPA.a, aPA.b, aPA.c), cVector3f(aPB.a, aPB.b, aPB.c));
// Set x to 0 so the calculation can be solved
avPoint.x = 0;
// find a value that sets b + b to 0.
float fVal = (-aPB.b) / aPA.b;
// Get z
avPoint.z = ((aPA.d * fVal) + aPB.d) / ((aPA.c * fVal) + aPB.c);
// Get y
avPoint.y = (aPA.d - (aPA.c * avPoint.z)) / aPA.b;
}
//-----------------------------------------------------------------------
static inline bool IntersectsPlanePair(const cPlanef &aPlane1, const cPlanef &aPlane2,
const cVector3f &avPoint1, const cVector3f &avPoint2) {
bool bPair[2][2];
// 1 to 1
if (cMath::PlaneToPointDist(aPlane1, avPoint1) >= 0)
bPair[0][0] = true;
else
bPair[0][0] = false;
// 1 to 2
if (cMath::PlaneToPointDist(aPlane1, avPoint2) >= 0)
bPair[0][1] = true;
else
bPair[0][1] = false;
// 2 to 1
if (cMath::PlaneToPointDist(aPlane2, avPoint1) >= 0)
bPair[1][0] = true;
else
bPair[1][0] = false;
// 2 to 2
if (cMath::PlaneToPointDist(aPlane2, avPoint2) >= 0)
bPair[1][1] = true;
else
bPair[1][1] = false;
/*Log("-------------\n");
Log("Pair 0 - 0: %d Pair 0 - 1: %d\n",bPair[0][0]?1:0,bPair[0][1]?1:0);
Log("Pair 1 - 0: %d Pair 1 - 1: %d\n",bPair[1][0]?1:0,bPair[1][1]?1:0);*/
// If either point is inside the plane pair we have an intersection.
if ((bPair[0][0] && bPair[1][0]) || (bPair[0][1] && bPair[1][1]))
return true;
// If they are on different sides of the plane, it is intersected as well.
if (((bPair[0][0] && !bPair[0][1]) && (!bPair[1][0] && bPair[1][1])) ||
((!bPair[0][0] && bPair[0][1]) && (bPair[1][0] && !bPair[1][1]))) {
return true;
}
return false;
}
bool cMath::CheckFrustumLineIntersection(const cPlanef *apPlanePairs, const cVector3f &avPoint1,
const cVector3f &avPoint2, int alPairNum) {
for (int i = 0; i < alPairNum; ++i) {
int lStart = i * 2;
if (IntersectsPlanePair(apPlanePairs[lStart], apPlanePairs[lStart + 1], avPoint1, avPoint2) == false) {
return false;
}
}
return true;
/*if( IntersectsPlanePair(apPlanePairs[0],apPlanePairs[1],avPoint1,avPoint2)
&&
IntersectsPlanePair(apPlanePairs[2],apPlanePairs[3],avPoint1,avPoint2)
&&
IntersectsPlanePair(apPlanePairs[4],apPlanePairs[5],avPoint1,avPoint2)
)
{
return true;
}
return false;
*/
}
//-----------------------------------------------------------------------
static const int kvQuadPairs[4][2] = {{0, 1}, {1, 2}, {2, 3}, {3, 0}};
bool cMath::CheckFrustumQuadMeshIntersection(const cPlanef *apPlanePairs, tVector3fVec *apPoints,
int alPairNum) {
int lPointNum = (int)apPoints->size();
for (int quad = 0; quad < lPointNum; quad += 4) {
cVector3f *pQuad = &(*apPoints)[quad];
// Check the pairs
for (int i = 0; i < 4; i++) {
if (CheckFrustumLineIntersection(apPlanePairs, pQuad[kvQuadPairs[i][0]],
pQuad[kvQuadPairs[i][1]], alPairNum)) {
return true;
}
}
}
return false;
}
//-----------------------------------------------------------------------
float cMath::QuaternionDot(const cQuaternion &aqA, const cQuaternion &aqB) {
return aqA.w * aqB.w + aqA.v.x * aqB.v.x + aqA.v.y * aqB.v.y + aqA.v.z * aqB.v.z;
}
//-----------------------------------------------------------------------
cQuaternion cMath::QuaternionSlerp(float afT, const cQuaternion &aqA, const cQuaternion &aqB,
bool abShortestPath) {
float fCos = QuaternionDot(aqA, aqB);
// If the rotations are the same, just return the first.
if (ABS(fCos - 1) <= kEpsilonf) {
return aqA;
}
float fAngle = acos(fCos);
float fSin = sin(fAngle);
float fInvSin = 1.0f / fSin;
float fCoeff0 = sin((1.0f - afT) * fAngle) * fInvSin;
float fCoeff1 = sin(afT * fAngle) * fInvSin;
// Do we need to invert rotation?
if (fCos < 0.0f && abShortestPath) {
fCoeff0 = -fCoeff0;
// taking the complement requires renormalisation
cQuaternion qT(aqA * fCoeff0 + aqB * fCoeff1);
qT.Normalise();
return qT;
} else {
return aqA * fCoeff0 + aqB * fCoeff1;
}
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixSlerp(float afT, const cMatrixf &a_mtxA, const cMatrixf &a_mtxB,
bool abShortestPath) {
cVector3f vPos = a_mtxA.GetTranslation() * (1 - afT) + a_mtxB.GetTranslation() * afT;
cQuaternion qA;
qA.FromRotationMatrix(a_mtxA);
cQuaternion qB;
qB.FromRotationMatrix(a_mtxB);
cQuaternion qFinal = cMath::QuaternionSlerp(afT, qA, qB, true);
cMatrixf mtxFinal = cMath::MatrixQuaternion(qFinal);
mtxFinal.SetTranslation(vPos);
return mtxFinal;
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixMul(const cMatrixf &a_mtxA, const cMatrixf &a_mtxB) {
cMatrixf mtxC;
mtxC.m[0][0] = a_mtxA.m[0][0] * a_mtxB.m[0][0] + a_mtxA.m[0][1] * a_mtxB.m[1][0] + a_mtxA.m[0][2] * a_mtxB.m[2][0] + a_mtxA.m[0][3] * a_mtxB.m[3][0];
mtxC.m[0][1] = a_mtxA.m[0][0] * a_mtxB.m[0][1] + a_mtxA.m[0][1] * a_mtxB.m[1][1] + a_mtxA.m[0][2] * a_mtxB.m[2][1] + a_mtxA.m[0][3] * a_mtxB.m[3][1];
mtxC.m[0][2] = a_mtxA.m[0][0] * a_mtxB.m[0][2] + a_mtxA.m[0][1] * a_mtxB.m[1][2] + a_mtxA.m[0][2] * a_mtxB.m[2][2] + a_mtxA.m[0][3] * a_mtxB.m[3][2];
mtxC.m[0][3] = a_mtxA.m[0][0] * a_mtxB.m[0][3] + a_mtxA.m[0][1] * a_mtxB.m[1][3] + a_mtxA.m[0][2] * a_mtxB.m[2][3] + a_mtxA.m[0][3] * a_mtxB.m[3][3];
mtxC.m[1][0] = a_mtxA.m[1][0] * a_mtxB.m[0][0] + a_mtxA.m[1][1] * a_mtxB.m[1][0] + a_mtxA.m[1][2] * a_mtxB.m[2][0] + a_mtxA.m[1][3] * a_mtxB.m[3][0];
mtxC.m[1][1] = a_mtxA.m[1][0] * a_mtxB.m[0][1] + a_mtxA.m[1][1] * a_mtxB.m[1][1] + a_mtxA.m[1][2] * a_mtxB.m[2][1] + a_mtxA.m[1][3] * a_mtxB.m[3][1];
mtxC.m[1][2] = a_mtxA.m[1][0] * a_mtxB.m[0][2] + a_mtxA.m[1][1] * a_mtxB.m[1][2] + a_mtxA.m[1][2] * a_mtxB.m[2][2] + a_mtxA.m[1][3] * a_mtxB.m[3][2];
mtxC.m[1][3] = a_mtxA.m[1][0] * a_mtxB.m[0][3] + a_mtxA.m[1][1] * a_mtxB.m[1][3] + a_mtxA.m[1][2] * a_mtxB.m[2][3] + a_mtxA.m[1][3] * a_mtxB.m[3][3];
mtxC.m[2][0] = a_mtxA.m[2][0] * a_mtxB.m[0][0] + a_mtxA.m[2][1] * a_mtxB.m[1][0] + a_mtxA.m[2][2] * a_mtxB.m[2][0] + a_mtxA.m[2][3] * a_mtxB.m[3][0];
mtxC.m[2][1] = a_mtxA.m[2][0] * a_mtxB.m[0][1] + a_mtxA.m[2][1] * a_mtxB.m[1][1] + a_mtxA.m[2][2] * a_mtxB.m[2][1] + a_mtxA.m[2][3] * a_mtxB.m[3][1];
mtxC.m[2][2] = a_mtxA.m[2][0] * a_mtxB.m[0][2] + a_mtxA.m[2][1] * a_mtxB.m[1][2] + a_mtxA.m[2][2] * a_mtxB.m[2][2] + a_mtxA.m[2][3] * a_mtxB.m[3][2];
mtxC.m[2][3] = a_mtxA.m[2][0] * a_mtxB.m[0][3] + a_mtxA.m[2][1] * a_mtxB.m[1][3] + a_mtxA.m[2][2] * a_mtxB.m[2][3] + a_mtxA.m[2][3] * a_mtxB.m[3][3];
mtxC.m[3][0] = a_mtxA.m[3][0] * a_mtxB.m[0][0] + a_mtxA.m[3][1] * a_mtxB.m[1][0] + a_mtxA.m[3][2] * a_mtxB.m[2][0] + a_mtxA.m[3][3] * a_mtxB.m[3][0];
mtxC.m[3][1] = a_mtxA.m[3][0] * a_mtxB.m[0][1] + a_mtxA.m[3][1] * a_mtxB.m[1][1] + a_mtxA.m[3][2] * a_mtxB.m[2][1] + a_mtxA.m[3][3] * a_mtxB.m[3][1];
mtxC.m[3][2] = a_mtxA.m[3][0] * a_mtxB.m[0][2] + a_mtxA.m[3][1] * a_mtxB.m[1][2] + a_mtxA.m[3][2] * a_mtxB.m[2][2] + a_mtxA.m[3][3] * a_mtxB.m[3][2];
mtxC.m[3][3] = a_mtxA.m[3][0] * a_mtxB.m[0][3] + a_mtxA.m[3][1] * a_mtxB.m[1][3] + a_mtxA.m[3][2] * a_mtxB.m[2][3] + a_mtxA.m[3][3] * a_mtxB.m[3][3];
return mtxC;
}
//-----------------------------------------------------------------------
cVector3f cMath::MatrixMul(const cMatrixf &a_mtxA, const cVector3f &avB) {
cVector3f vC;
vC.x = (a_mtxA.m[0][0] * avB.x + a_mtxA.m[0][1] * avB.y + a_mtxA.m[0][2] * avB.z + a_mtxA.m[0][3]);
vC.y = (a_mtxA.m[1][0] * avB.x + a_mtxA.m[1][1] * avB.y + a_mtxA.m[1][2] * avB.z + a_mtxA.m[1][3]);
vC.z = (a_mtxA.m[2][0] * avB.x + a_mtxA.m[2][1] * avB.y + a_mtxA.m[2][2] * avB.z + a_mtxA.m[2][3]);
return vC;
}
//-----------------------------------------------------------------------
cVector3f cMath::MatrixMulDivideW(const cMatrixf &a_mtxA, const cVector3f &avB) {
cVector3f vC;
float fInvW = 1.0f / (a_mtxA.m[3][0] * avB.x + a_mtxA.m[3][1] * avB.y + a_mtxA.m[3][2] * avB.z + a_mtxA.m[3][3]);
vC.x = (a_mtxA.m[0][0] * avB.x + a_mtxA.m[0][1] * avB.y + a_mtxA.m[0][2] * avB.z + a_mtxA.m[0][3]) * fInvW;
vC.y = (a_mtxA.m[1][0] * avB.x + a_mtxA.m[1][1] * avB.y + a_mtxA.m[1][2] * avB.z + a_mtxA.m[1][3]) * fInvW;
vC.z = (a_mtxA.m[2][0] * avB.x + a_mtxA.m[2][1] * avB.y + a_mtxA.m[2][2] * avB.z + a_mtxA.m[2][3]) * fInvW;
return vC;
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixMulScalar(const cMatrixf &a_mtxA, float afB) {
cMatrixf mtxC;
mtxC.m[0][0] = a_mtxA.m[0][0] * afB;
mtxC.m[0][1] = a_mtxA.m[0][1] * afB;
mtxC.m[0][2] = a_mtxA.m[0][2] * afB;
mtxC.m[0][3] = a_mtxA.m[0][3] * afB;
mtxC.m[1][0] = a_mtxA.m[1][0] * afB;
mtxC.m[1][1] = a_mtxA.m[1][1] * afB;
mtxC.m[1][2] = a_mtxA.m[1][2] * afB;
mtxC.m[1][3] = a_mtxA.m[1][3] * afB;
mtxC.m[2][0] = a_mtxA.m[2][0] * afB;
mtxC.m[2][1] = a_mtxA.m[2][1] * afB;
mtxC.m[2][2] = a_mtxA.m[2][2] * afB;
mtxC.m[2][3] = a_mtxA.m[2][3] * afB;
mtxC.m[3][0] = a_mtxA.m[3][0] * afB;
mtxC.m[3][1] = a_mtxA.m[3][1] * afB;
mtxC.m[3][2] = a_mtxA.m[3][2] * afB;
mtxC.m[3][3] = a_mtxA.m[3][3] * afB;
return mtxC;
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixRotate(cVector3f avRot, eEulerRotationOrder aOrder) {
cMatrixf mtxRot = cMatrixf::Identity;
switch (aOrder) {
case eEulerRotationOrder_XYZ:
mtxRot = MatrixMul(MatrixRotateX(avRot.x), mtxRot);
mtxRot = MatrixMul(MatrixRotateY(avRot.y), mtxRot);
mtxRot = MatrixMul(MatrixRotateZ(avRot.z), mtxRot);
break;
case eEulerRotationOrder_XZY:
mtxRot = MatrixMul(MatrixRotateX(avRot.x), mtxRot);
mtxRot = MatrixMul(MatrixRotateZ(avRot.z), mtxRot);
mtxRot = MatrixMul(MatrixRotateY(avRot.y), mtxRot);
break;
case eEulerRotationOrder_YXZ:
mtxRot = MatrixMul(MatrixRotateY(avRot.y), mtxRot);
mtxRot = MatrixMul(MatrixRotateX(avRot.x), mtxRot);
mtxRot = MatrixMul(MatrixRotateZ(avRot.z), mtxRot);
break;
case eEulerRotationOrder_YZX:
mtxRot = MatrixMul(MatrixRotateY(avRot.y), mtxRot);
mtxRot = MatrixMul(MatrixRotateZ(avRot.z), mtxRot);
mtxRot = MatrixMul(MatrixRotateX(avRot.x), mtxRot);
break;
case eEulerRotationOrder_ZXY:
mtxRot = MatrixMul(MatrixRotateZ(avRot.z), mtxRot);
mtxRot = MatrixMul(MatrixRotateX(avRot.x), mtxRot);
mtxRot = MatrixMul(MatrixRotateY(avRot.y), mtxRot);
break;
case eEulerRotationOrder_ZYX:
mtxRot = MatrixMul(MatrixRotateZ(avRot.z), mtxRot);
mtxRot = MatrixMul(MatrixRotateY(avRot.y), mtxRot);
mtxRot = MatrixMul(MatrixRotateX(avRot.x), mtxRot);
break;
case eEulerRotationOrder_LastEnum:
break;
}
return mtxRot;
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixRotateX(float afAngle) {
return cMatrixf(1, 0, 0, 0,
0, cos(afAngle), -sin(afAngle), 0,
0, sin(afAngle), cos(afAngle), 0,
0, 0, 0, 1);
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixRotateY(float afAngle) {
return cMatrixf(cos(afAngle), 0, sin(afAngle), 0,
0, 1, 0, 0,
-sin(afAngle), 0, cos(afAngle), 0,
0, 0, 0, 1);
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixRotateZ(float afAngle) {
return cMatrixf(cos(afAngle), -sin(afAngle), 0, 0,
sin(afAngle), cos(afAngle), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixQuaternion(const cQuaternion &aqRot) {
cMatrixf mtxOut;
// Set the non rotation part.
mtxOut.m[0][3] = 0;
mtxOut.m[1][3] = 0;
mtxOut.m[2][3] = 0;
mtxOut.m[3][0] = 0;
mtxOut.m[3][1] = 0;
mtxOut.m[3][2] = 0;
mtxOut.m[3][3] = 1;
aqRot.ToRotationMatrix(mtxOut);
return mtxOut;
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixScale(cVector3f avScale) {
return cMatrixf(avScale.x, 0, 0, 0,
0, avScale.y, 0, 0,
0, 0, avScale.z, 0,
0, 0, 0, 1);
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixTranslate(cVector3f avTrans) {
return cMatrixf(1, 0, 0, avTrans.x,
0, 1, 0, avTrans.y,
0, 0, 1, avTrans.z,
0, 0, 0, 1);
}
//-----------------------------------------------------------------------
float cMath::MatrixMinor(const cMatrixf &a_mtxA,
const size_t r0, const size_t r1, const size_t r2,
const size_t c0, const size_t c1, const size_t c2) {
return a_mtxA.m[r0][c0] * (a_mtxA.m[r1][c1] * a_mtxA.m[r2][c2] - a_mtxA.m[r2][c1] * a_mtxA.m[r1][c2]) -
a_mtxA.m[r0][c1] * (a_mtxA.m[r1][c0] * a_mtxA.m[r2][c2] - a_mtxA.m[r2][c0] * a_mtxA.m[r1][c2]) +
a_mtxA.m[r0][c2] * (a_mtxA.m[r1][c0] * a_mtxA.m[r2][c1] - a_mtxA.m[r2][c0] * a_mtxA.m[r1][c1]);
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixAdjoint(const cMatrixf &a_mtxA) {
return cMatrixf(MatrixMinor(a_mtxA, 1, 2, 3, 1, 2, 3),
-MatrixMinor(a_mtxA, 0, 2, 3, 1, 2, 3),
MatrixMinor(a_mtxA, 0, 1, 3, 1, 2, 3),
-MatrixMinor(a_mtxA, 0, 1, 2, 1, 2, 3),
-MatrixMinor(a_mtxA, 1, 2, 3, 0, 2, 3),
MatrixMinor(a_mtxA, 0, 2, 3, 0, 2, 3),
-MatrixMinor(a_mtxA, 0, 1, 3, 0, 2, 3),
MatrixMinor(a_mtxA, 0, 1, 2, 0, 2, 3),
MatrixMinor(a_mtxA, 1, 2, 3, 0, 1, 3),
-MatrixMinor(a_mtxA, 0, 2, 3, 0, 1, 3),
MatrixMinor(a_mtxA, 0, 1, 3, 0, 1, 3),
-MatrixMinor(a_mtxA, 0, 1, 2, 0, 1, 3),
-MatrixMinor(a_mtxA, 1, 2, 3, 0, 1, 2),
MatrixMinor(a_mtxA, 0, 2, 3, 0, 1, 2),
-MatrixMinor(a_mtxA, 0, 1, 3, 0, 1, 2),
MatrixMinor(a_mtxA, 0, 1, 2, 0, 1, 2));
}
//-----------------------------------------------------------------------
float cMath::MatrixDeterminant(const cMatrixf &a_mtxA) {
return a_mtxA.m[0][0] * MatrixMinor(a_mtxA, 1, 2, 3, 1, 2, 3) -
a_mtxA.m[0][1] * MatrixMinor(a_mtxA, 1, 2, 3, 0, 2, 3) +
a_mtxA.m[0][2] * MatrixMinor(a_mtxA, 1, 2, 3, 0, 1, 3) -
a_mtxA.m[0][3] * MatrixMinor(a_mtxA, 1, 2, 3, 0, 1, 2);
}
//-----------------------------------------------------------------------
cMatrixf cMath::MatrixInverse(const cMatrixf &a_mtxA) {
return MatrixMulScalar(MatrixAdjoint(a_mtxA), (1.0f / MatrixDeterminant(a_mtxA)));
}
//-----------------------------------------------------------------------
cVector3f cMath::MatrixToEulerAngles(const cMatrixf &a_mtxA, eEulerRotationOrder aOrder) {
cVector3f vAngles;
/*if (a_mtxA.m[2][0] > 0.998f) // singularity at north pole
{
vAngles.y = atan2(a_mtxA.m[0][2],a_mtxA.m[2][2]);
vAngles.z = kPif/2;
vAngles.x = 0;
Log("Special case 1\n");
}
if (a_mtxA.m[2][0] < -0.998f) // singularity at south pole
{
vAngles.y = atan2(a_mtxA.m[0][2],a_mtxA.m[2][2]);
vAngles.z = -kPif/2;
vAngles.x = 0;
Log("Special case 2\n");
}
else*/
{
vAngles.x = atan2(a_mtxA.m[2][1], a_mtxA.m[2][2]);
vAngles.y = -asin(a_mtxA.m[2][0]);
vAngles.z = atan2(a_mtxA.m[1][0], a_mtxA.m[0][0]);
}
return vAngles;
}
//-----------------------------------------------------------------------
const char *cMath::MatrixToChar(const cMatrixf &a_mtxA) {
snprintf(mpTempChar, 1024, "[%.3f, %.3f, %.3f, %.3f] [%.3f, %.3f, %.3f, %.3f] [%.3f, %.3f, %.3f, %.3f] [%.3f, %.3f, %.3f, %.3f]",
a_mtxA.m[0][0], a_mtxA.m[0][1], a_mtxA.m[0][2], a_mtxA.m[0][3],
a_mtxA.m[1][0], a_mtxA.m[1][1], a_mtxA.m[1][2], a_mtxA.m[1][3],
a_mtxA.m[2][0], a_mtxA.m[2][1], a_mtxA.m[2][2], a_mtxA.m[2][3],
a_mtxA.m[3][0], a_mtxA.m[3][1], a_mtxA.m[3][2], a_mtxA.m[3][3]);
return mpTempChar;
}
//-----------------------------------------------------------------------
static inline cVector3f GetVector3(const float *apVertexArray, int alIdx, int alStride) {
const float *apVec = &apVertexArray[alIdx * alStride];
return cVector3f(apVec[0], apVec[1], apVec[2]);
}
/*static inline void AddVector3(float *apArray, int alIdx, const cVector3f &avVec, int alStride) {
float *apVec = &apArray[alIdx * alStride];
apVec[0] += avVec.x;
apVec[1] += avVec.y;
apVec[2] += avVec.z;
}*/
static inline void SetVector4(const cVector3f &avVec, float afW, float *apArray, int alIdx) {
float *apVec = &apArray[alIdx * 4];
apVec[0] = avVec.x;
apVec[1] = avVec.y;
apVec[2] = avVec.z;
apVec[3] = afW;
}
static inline bool Vector3Equal(const float *apArrayA, int alIdxA, const float *apArrayB, int alIdxB,
int alStride) {
if (apArrayA[alIdxA * alStride + 0] == apArrayB[alIdxB * alStride + 0] &&
apArrayA[alIdxA * alStride + 1] == apArrayB[alIdxB * alStride + 1] &&
apArrayA[alIdxA * alStride + 2] == apArrayB[alIdxB * alStride + 2]) {
return true;
}
return false;
}
bool cMath::CreateTriTangentVectors(float *apDestArray,
const unsigned int *apIndexArray, int alIndexNum,
const float *apVertexArray, int alVtxStride,
const float *apTexArray,
const float *apNormalArray,
int alVertexNum) {
// Create two temp arrays and clear them.
tVector3fVec vTempTangents1;
tVector3fVec vTempTangents2;
// Log("Creating tangents:\n");
// Log("Num of indices: %d\n",alIndexNum);
// Log("Num of vertrices: %d\n",alVertexNum);
Hpl1::resizeAndFill(vTempTangents1, alVertexNum, cVector3f(0, 0, 0));
Hpl1::resizeAndFill(vTempTangents2, alVertexNum, cVector3f(0, 0, 0));
// Iterate through the triangles
for (int triIdx = 0; triIdx < alIndexNum; triIdx += 3) {
// Log("Triangle %d: ",triIdx/3);
// Get the indices of the triangle
int idx1 = apIndexArray[triIdx + 0];
int idx2 = apIndexArray[triIdx + 1];
int idx3 = apIndexArray[triIdx + 2];
// Log("1: '%d' 2: '%d' 3: '%d' ", idx1, idx2, idx3);
// Get the 3 points making up the triangle
cVector3f vPos1 = GetVector3(apVertexArray, idx1, alVtxStride);
cVector3f vPos2 = GetVector3(apVertexArray, idx2, alVtxStride);
cVector3f vPos3 = GetVector3(apVertexArray, idx3, alVtxStride);
// Get the 3 texture coords in the triangle.
cVector3f vTex1 = GetVector3(apTexArray, idx1, 3);
cVector3f vTex2 = GetVector3(apTexArray, idx2, 3);
cVector3f vTex3 = GetVector3(apTexArray, idx3, 3);
// Get the vectors between the positions.
cVector3f vPos1To2 = vPos2 - vPos1;
cVector3f vPos1To3 = vPos3 - vPos1;
// Get the vectors between the tex coords
cVector3f vTex1To2 = vTex2 - vTex1;
cVector3f vTex1To3 = vTex3 - vTex1;
// Get the direction of the S and T tangents
float fR = 1.0f / (vTex1To2.x * vTex1To3.y - vTex1To2.y * vTex1To3.x);
cVector3f vSDir((vTex1To3.y * vPos1To2.x - vTex1To2.y * vPos1To3.x) * fR,
(vTex1To3.y * vPos1To2.y - vTex1To2.y * vPos1To3.y) * fR,
(vTex1To3.y * vPos1To2.z - vTex1To2.y * vPos1To3.z) * fR);
cVector3f vTDir((vTex1To2.x * vPos1To3.x - vTex1To3.x * vPos1To2.x) * fR,
(vTex1To2.x * vPos1To3.y - vTex1To3.x * vPos1To2.y) * fR,
(vTex1To2.x * vPos1To3.z - vTex1To3.x * vPos1To2.z) * fR);
// Add the temp arrays with the values:
vTempTangents1[idx1] += vSDir;
vTempTangents1[idx2] += vSDir;
vTempTangents1[idx3] += vSDir;
vTempTangents2[idx1] += vTDir;
vTempTangents2[idx2] += vTDir;
vTempTangents2[idx3] += vTDir;
// Log("\n");
}
// Log("Looking for duplicates: \n");
// Go through the vertrices and find normal and vertex copies. Smooth the tangents on these
float fMaxCosAngle = -1.0f;
for (int i = 0; i < alVertexNum; i++) {
for (int j = i + 1; j < alVertexNum; j++) {
// Log("(%.1f, %.1f, %.1f)", apVertexArray[i+0],apVertexArray[i+1],apVertexArray[i+2]);
// Log(" vs ");
// Log("(%.1f, %.1f, %.1f)\n", apVertexArray[j+0],apVertexArray[j+1],apVertexArray[j+2]);
if (Vector3Equal(apVertexArray, i, apVertexArray, j, alVtxStride) &&
Vector3Equal(apNormalArray, i, apNormalArray, j, 3)) {
// Log("Found at %d and %d!\n", i, j);
cVector3f vAT1 = vTempTangents1[i];
cVector3f vAT2 = vTempTangents2[i];
cVector3f vBT1 = vTempTangents1[j];
cVector3f vBT2 = vTempTangents2[j];
if (Vector3Dot(vAT1, vBT1) >= fMaxCosAngle) {
vTempTangents1[j] += vAT1;
vTempTangents1[i] += vBT1;
}
if (Vector3Dot(vAT2, vBT2) >= fMaxCosAngle) {
vTempTangents2[j] += vAT2;
vTempTangents2[i] += vBT2;
}
}
}
}
// Iterate through the dest array and set tangent values
for (int vtxIdx = 0; vtxIdx < alVertexNum; vtxIdx++) {
cVector3f vNormal = GetVector3(apNormalArray, vtxIdx, 3);
cVector3f &vTempTan1 = vTempTangents1[vtxIdx];
cVector3f &vTempTan2 = vTempTangents2[vtxIdx];
// Gram-Schmidt orthogonalize
cVector3f vTan = vTempTan1 - (vNormal * cMath::Vector3Dot(vNormal, vTempTan1));
vTan.Normalise();
// Log("Add tangent %d: ",vtxIdx);
// Log(" %.1f, %.1f, %.1f ",vTan.x, vTan.y, vTan.z);
// Log("\n");
// Calculate if left or right handed.
float fW = (cMath::Vector3Dot(cMath::Vector3Cross(vNormal, vTempTan1), vTempTan2) < 0.0f) ? -1.0f : 1.0f;
SetVector4(vTan, fW, apDestArray, vtxIdx);
}
return true;
}
//-----------------------------------------------------------------------
bool cMath::CreateTriangleData(tTriangleDataVec &avTriangles,
const unsigned int *apIndexArray, int alIndexNum,
const float *apVertexArray, int alVtxStride, int alVertexNum) {
int lNumOfTri = alIndexNum / 3;
if ((int)avTriangles.size() < lNumOfTri)
avTriangles.resize(lNumOfTri);
// Log("Creating triangle data:\n");
for (int tri = 0, idx = 0; tri < lNumOfTri; tri++, idx += 3) {
// Log("checking: tri %d idx %d\n",tri, idx);
// Calculate normal
const float *pVtx0 = &apVertexArray[apIndexArray[idx] * alVtxStride];
const float *pVtx1 = &apVertexArray[apIndexArray[idx + 1] * alVtxStride];
const float *pVtx2 = &apVertexArray[apIndexArray[idx + 2] * alVtxStride];
cVector3f vEdge1(pVtx1[0] - pVtx0[0], pVtx1[1] - pVtx0[1], pVtx1[2] - pVtx0[2]);
cVector3f vEdge2(pVtx2[0] - pVtx0[0], pVtx2[1] - pVtx0[1], pVtx2[2] - pVtx0[2]);
avTriangles[tri].normal = Vector3Cross(vEdge2, vEdge1);
}
return true;
}
//-----------------------------------------------------------------------
/*static bool EdgePointEqual(const float *apVertexArray,
const cTriEdge &edge1, const cTriEdge &edge2, int alStride) {
if (Vector3Equal(apVertexArray, edge1.point1, apVertexArray, edge2.point1, alStride) &&
Vector3Equal(apVertexArray, edge1.point2, apVertexArray, edge2.point2, alStride)) {
return true;
}
if (Vector3Equal(apVertexArray, edge1.point1, apVertexArray, edge2.point2, alStride) &&
Vector3Equal(apVertexArray, edge1.point2, apVertexArray, edge2.point1, alStride)) {
return true;
}
return false;
}
/////////////////////////
static bool EdgeTriEqual(const cTriEdge &edge1, const cTriEdge &edge2) {
if (edge1.tri1 == edge2.tri1 && edge1.tri2 == edge2.tri2)
return true;
if (edge1.tri1 == edge1.tri1 && edge1.tri2 == edge2.tri1)
return true;
return false;
}
/////////////////////////
static bool EdgeEqual(const float *apVertexArray, const cTriEdge &edge1, const cTriEdge &edge2, int alStride) {
if (EdgePointEqual(apVertexArray, edge1, edge2, alStride) && EdgeTriEqual(edge1, edge2))
return true;
return false;
}*/
/////////////////////////
static const float *gpVertexArray;
static const unsigned int *gpIndexArray;
static int glVertexStride;
//////////////////////////////////////////////////////
class cVertexIndices {
public:
cVertexIndices(unsigned int alIdx) {
mlstIndices.push_back(alIdx);
}
tUIntList mlstIndices;
};
typedef Common::StableMap<cVector3f, cVertexIndices> tVtxIdxMap;
typedef tVtxIdxMap::iterator tVtxIdxMapIt;
//////////////////////////////////////////////////////
class cEdgeCompare {
public:
// The point1 must be > than point2 for this to work!
bool operator()(const cTriEdge &Edge1, const cTriEdge &Edge2) const {
cVector3f vPoint1_1 = GetVector3(gpVertexArray, Edge1.point1, glVertexStride);
cVector3f vPoint1_2 = GetVector3(gpVertexArray, Edge1.point2, glVertexStride);
cVector3f vPoint2_1 = GetVector3(gpVertexArray, Edge2.point1, glVertexStride);
cVector3f vPoint2_2 = GetVector3(gpVertexArray, Edge2.point2, glVertexStride);
// 1 - 1
if (vPoint1_1.x != vPoint2_1.x)
return vPoint1_1.x > vPoint2_1.x;
if (vPoint1_1.y != vPoint2_1.y)
return vPoint1_1.y > vPoint2_1.y;
if (vPoint1_1.z != vPoint2_1.z)
return vPoint1_1.z > vPoint2_1.z;
// 2 - 2
if (vPoint1_2.x != vPoint2_2.x)
return vPoint1_2.x > vPoint2_2.x;
if (vPoint1_2.y != vPoint2_2.y)
return vPoint1_2.y > vPoint2_2.y;
if (vPoint1_2.z != vPoint2_2.z)
return vPoint1_2.z > vPoint2_2.z;
return false;
}
};
typedef Hpl1::Std::set<cTriEdge, cEdgeCompare> tTriEdgeListMap;
typedef tTriEdgeListMap::iterator tTriEdgeListMapIt;
//////////////////////////////////////////////////////
static void CheckEdgeSwitch(cTriEdge *apEdge) {
cVector3f vPoint1 = GetVector3(gpVertexArray, apEdge->point1, glVertexStride);
cVector3f vPoint2 = GetVector3(gpVertexArray, apEdge->point2, glVertexStride);
if (vPoint1 < vPoint2) {
unsigned int lTemp = apEdge->point1;
apEdge->point1 = apEdge->point2;
apEdge->point2 = lTemp;
}
}
//////////////////////////////////////////////////////
void AddEdgeToMap(cTriEdge &aEdge, tTriEdgeListMap &aMap) {
tTriEdgeListMapIt it = aMap.find(aEdge);
// if edge is not added, create a new element.
if (it == aMap.end()) {
aEdge.tri2 = -1;
aMap.insert(aEdge);
}
// if added check if there already exist an edge with the triangles
else {
if (it->tri2 != -1)
return;
if (it->tri1 == aEdge.tri1)
return;
// Set the other triangle! voila our edge is done!
it->tri2 = aEdge.tri1;
}
}
//////////////////////////////////////////////////////
bool cMath::CreateEdges(tTriEdgeVec &avEdges,
const unsigned int *apIndexArray, int alIndexNum,
const float *apVertexArray, int alVtxStride, int alVertexNum,
bool *apIsDoubleSided) {
const bool bLog = false;
// Initial setup
*apIsDoubleSided = false;
tVtxIdxMap mapVtxIndices;
tTriEdgeListMap mapTriEdgeLists;
gpIndexArray = apIndexArray;
gpVertexArray = apVertexArray;
glVertexStride = alVtxStride;
////////////////////////////////////////////////
// Iterate indices and add all that reference the same vertex to
// the same element in a map
for (int idx = 0; idx < alIndexNum; idx++) {
cVector3f vVtx = GetVector3(apVertexArray, apIndexArray[idx], alVtxStride);
if (bLog)
Log("Checking idx: %d with vec: %s, ", idx, vVtx.ToString().c_str());
tVtxIdxMapIt it = mapVtxIndices.find(vVtx);
// If vertex already exist just add the index
if (it != mapVtxIndices.end()) {
if (bLog)
Log("Already added, appending.!\n");
it->second.mlstIndices.push_back(idx);
}
// if vertex is not added create new vertex and add.
else {
if (bLog)
Log("Adding as new!\n");
mapVtxIndices.insert(tVtxIdxMap::value_type(vVtx, cVertexIndices(idx)));
}
}
if (bLog) {
Log("FINAL VTX MAP:\n");
tVtxIdxMapIt VtxIt = mapVtxIndices.begin();
for (; VtxIt != mapVtxIndices.end(); ++VtxIt) {
const cVector3f &vVtx = VtxIt->first;
const cVertexIndices &Data = VtxIt->second;
Log("Vtx: %s Idx: ", vVtx.ToString().c_str());
for (tUIntList::const_iterator it = Data.mlstIndices.begin(); it != Data.mlstIndices.end(); ++it) {
Log("%d, ", *it);
}
Log("\n");
}
}
////////////////////////////////////////////////
// Iterate vertices and check what indices belong to it.
// create a edges from these.
// TODO: iterate the amp here instead.
tVtxIdxMapIt VtxIt = mapVtxIndices.begin();
for (; VtxIt != mapVtxIndices.end(); ++VtxIt) {
cVertexIndices &Data = VtxIt->second;
// Iterate the indices and create edges.
tUIntListIt it = Data.mlstIndices.begin();
for (; it != Data.mlstIndices.end(); ++it) {
int lTriIdx = ((*it) / 3) * 3;
unsigned int lVtx = apIndexArray[*it]; // The num of the vertex this index reference to.
// Create edges
cTriEdge edge1, edge2;
edge1.point1 = lVtx;
edge2.point1 = lVtx;
edge1.tri1 = lTriIdx / 3;
edge2.tri1 = lTriIdx / 3;
// Get the index the vertex has in the tri (0 -2)
int lIdxInTri = (*it) % 3;
// Log("Idx in tri: %d\n",lIdxInTri);
// Get the end points of the edge.
int lPoint1 = lIdxInTri + 1;
if (lPoint1 > 2)
lPoint1 = 0;
int lPoint2 = lIdxInTri - 1;
if (lPoint2 < 0)
lPoint2 = 2;
// Set the end points.
edge1.point2 = apIndexArray[lTriIdx + lPoint1];
edge2.point2 = apIndexArray[lTriIdx + lPoint2];
// Switch points if they 1 < 2
CheckEdgeSwitch(&edge1);
CheckEdgeSwitch(&edge2);
AddEdgeToMap(edge1, mapTriEdgeLists);
AddEdgeToMap(edge2, mapTriEdgeLists);
}
}
if (bLog) {
Log("FINAL EDGE MAP:\n");
tTriEdgeListMapIt EdgeIt = mapTriEdgeLists.begin();
for (; EdgeIt != mapTriEdgeLists.end(); ++EdgeIt) {
cTriEdge &Edge = const_cast<cTriEdge &>(*EdgeIt);
Log("P1: %d P2: %d Tri1: %d Tri2: %d\n", Edge.point1, Edge.point2, Edge.tri1, Edge.tri2);
}
}
////////////////////////////////////////////////
// Iterate edge map and check triangles it has, create edges from this.
avEdges.reserve(mapTriEdgeLists.size());
tTriEdgeListMapIt EdgeIt = mapTriEdgeLists.begin();
for (; EdgeIt != mapTriEdgeLists.end(); ++EdgeIt) {
cTriEdge &Edge = const_cast<cTriEdge &>(*EdgeIt);
const unsigned int *pTri1 = &apIndexArray[Edge.tri1 * 3];
if (Edge.tri2 == -1) {
Edge.invert_tri2 = true;
*apIsDoubleSided = true;
} else {
Edge.invert_tri2 = false;
}
// Get position of point1 in triangle
int lPoint1InTri = 0;
for (int i = 0; i < 3; i++) {
if (Vector3Equal(apVertexArray, pTri1[i], apVertexArray, Edge.point1, alVtxStride)) {
lPoint1InTri = i;
break;
}
}
// The next position in the triangle.
int lNextInTri = lPoint1InTri + 1;
if (lNextInTri >= 3)
lNextInTri = 0;
// Log("Point in: %d Next: %d\n",lPoint1InTri,lNextInTri);
// If next point is NOT point 2, then the edge
// must be switched.
if (Vector3Equal(apVertexArray, pTri1[lNextInTri], apVertexArray, Edge.point2, alVtxStride)) {
unsigned int lTemp = Edge.point1;
Edge.point1 = Edge.point2;
Edge.point2 = lTemp;
// if(bLog)Log("Switching points\n");
}
// Add the final edge.
avEdges.push_back(Edge);
}
return true;
}
/*bool cMath::CreateEdges(tTriEdgeVec &avEdges,
const unsigned int* apIndexArray,int alIndexNum,
const float* apVertexArray, int alVtxStride,int alVertexNum,
bool *apIsDoubleSided)
{
const bool bLog= false;
//Setup
*apIsDoubleSided = false;
////////////////////////////////////////////////////////////////////////
//For each vertex check what triangles reference it and make edges to the
//vertices in that triangle.
for(int vtx=0; vtx < alVertexNum; vtx++)
{
if(bLog)Log("Checking vtx: %d\n",vtx);
int lCount=0; //The number of times the vertex is referred to.
tUIntList mlstIndices;
for(int idx=0; idx < alIndexNum; idx++)
{
if(Vector3Equal(apVertexArray,vtx, apVertexArray, apIndexArray[idx],alVtxStride))
{
lCount++;
mlstIndices.push_back(idx);
}
}
if(lCount==0){
Warning("Found unreferenced vertex when building edges!\n");
}
else
{
///////////////////////////////////////////
//Create the edges
//Log("Vertex has %d references!\n",lCount);
tUIntListIt it = mlstIndices.begin();
for(; it != mlstIndices.end(); ++it)
{
//Get the triangle start index.
int lTriIdx = ((*it)/3)*3;
//if(bLog)Log("Tri index: %d!\n",lTriIdx);
//Create edges
cTriEdge edge1,edge2;
edge1.point1 = vtx;
edge2.point1 = vtx;
edge1.tri1 = lTriIdx/3;
edge2.tri1 = lTriIdx/3;
//Get the index the vertex has in the tri (0 -2)
int lIdxInTri = (*it) % 3;
//Log("Idx in tri: %d\n",lIdxInTri);
//Get the end points of the edge.
int lPoint1 = lIdxInTri+1;
if(lPoint1>2) lPoint1 =0;
int lPoint2 = lIdxInTri-1;
if(lPoint2<0) lPoint2 =2;
//if(bLog)Log("P1: %d P2: %d!\n",apIndexArray[lTriIdx +lPoint1],
// apIndexArray[lTriIdx +lPoint2]);
//Set the end points.
edge1.point2 = apIndexArray[lTriIdx + lPoint1];
edge2.point2 = apIndexArray[lTriIdx + lPoint2];
avEdges.push_back(edge1);
avEdges.push_back(edge2);
}
}
}
////////////////////////////////////////////////////////////////////////
//Check for triangles that share the same edge. Pair these.
if(bLog)Log("Looking for shared edges on triangles.\n");
tTriEdgeVec vTempEdges;
//Check for the last element as well incase it has no pair..
for(int e1 =0; e1 < (int)avEdges.size(); e1++)
{
bool bFound = false;
cTriEdge edge1 = avEdges[e1];
if(bLog)Log("Checking p(%d)-p(%d)|(%d)\n",
edge1.point1,edge1.point2,
edge1.tri1);
for(int e2 =e1+1; e2 < (int)avEdges.size(); e2++)
{
cTriEdge edge2 = avEdges[e2];
//if(bLog)Log("Checking %d to %d\n",e1,e2);
//Check if the edges has different triangles but the same points.
if(edge1.tri1 != edge2.tri1 && EdgePointEqual(apVertexArray,edge1,edge2,alVtxStride))
{
bFound = true;
if(bLog)Log("Found a pair: p(%d)-p(%d)|(%d)\n",edge2.point1,edge2.point2,
edge2.tri1);
edge1.tri2 = edge2.tri1;
edge1.invert_tri2 = false;
break;
}
}
//If no edge was found it is at a hole.
if(bFound == false)
{
edge1.tri2 = edge1.tri1;
edge1.invert_tri2 = true;
if(bLog) Log("No pair found, at a hole\n");
}
bFound = false;
//SLOW AS HELL THIS IS...
for(size_t i=0; i< vTempEdges.size(); i++)
{
if(EdgeEqual(apVertexArray,vTempEdges[i],edge1,alVtxStride)){
bFound = true;
break;
}
//if it is the last edge, don't add it if there is another edge with
//the same points and different triangle sides.
if(EdgePointEqual(apVertexArray,vTempEdges[i],edge1,alVtxStride) &&
vTempEdges[i].tri1 != vTempEdges[i].tri2)
{
bFound = true;
break;
}
}
//If not already added, add
if(bFound== false){
//If a face with inverted tri was added, the mesh is double sided.
if(edge1.invert_tri2){
*apIsDoubleSided = true;
}
vTempEdges.push_back(edge1);
if(bLog)Log("Added!\n");
}
if(bLog)Log("------------\n");
}
////////////////////////////////////////////////////////////////////////
//Clear the old list and add the new nice pairs.
avEdges.clear();
avEdges.reserve(vTempEdges.size());
//Log("FINAL EDGES:\n");
//Create the final edges. This means making sure so that
//edge point 1 is before point 2 in triangle 1 and 2 point 2 before
//1 in triangle 2.
for(size_t edge=0; edge< vTempEdges.size(); edge++)
{
cTriEdge Edge = vTempEdges[edge];
const unsigned int *pTri1 = &apIndexArray[Edge.tri1 * 3];
const unsigned int *pTri2 = &apIndexArray[Edge.tri2 * 3];
if(bLog)Log("Edge %d:\n",edge);
if(bLog)Log("Tri1: 0:(%.2f %.2f %.2f) 1:(%.2f %.2f %.2f) 2:(%.2f %.2f %.2f)\n",
apVertexArray[pTri1[0]*alVtxStride+0],apVertexArray[pTri1[0]*alVtxStride+1],apVertexArray[pTri1[0]*alVtxStride+2],
apVertexArray[pTri1[1]*alVtxStride+0],apVertexArray[pTri1[1]*alVtxStride+1],apVertexArray[pTri1[1]*alVtxStride+2],
apVertexArray[pTri1[2]*alVtxStride+0],apVertexArray[pTri1[2]*alVtxStride+1],apVertexArray[pTri1[2]*alVtxStride+2]);
if(bLog)Log("Tri2: 0:(%.2f %.2f %.2f) 1:(%.2f %.2f %.2f) 2:(%.2f %.2f %.2f)\n",
apVertexArray[pTri2[0]*alVtxStride+0],apVertexArray[pTri2[0]*alVtxStride+1],apVertexArray[pTri2[0]*alVtxStride+2],
apVertexArray[pTri2[1]*alVtxStride+0],apVertexArray[pTri2[1]*alVtxStride+1],apVertexArray[pTri2[1]*alVtxStride+2],
apVertexArray[pTri2[2]*alVtxStride+0],apVertexArray[pTri2[2]*alVtxStride+1],apVertexArray[pTri2[2]*alVtxStride+2]);
if(bLog)Log("Point1: (%.2f %.2f %.2f) Point2: (%.2f %.2f %.2f)\n",
apVertexArray[Edge.point1*alVtxStride+0],apVertexArray[Edge.point1*alVtxStride+1],apVertexArray[Edge.point1*alVtxStride+2],
apVertexArray[Edge.point2*alVtxStride+0],apVertexArray[Edge.point2*alVtxStride+1],apVertexArray[Edge.point2*alVtxStride+2]);
//Get position of point1 in triangle
int lPoint1InTri=0;
for(int i=0; i < 3; i++){
if(Vector3Equal(apVertexArray,pTri1[i],apVertexArray, Edge.point1,alVtxStride))
{
lPoint1InTri = i;
break;
}
}
//The next position in the triangle.
int lNextInTri = lPoint1InTri +1;
if(lNextInTri >=3 ) lNextInTri =0;
if(bLog)Log("Point in: %d Next: %d\n",lPoint1InTri,lNextInTri);
//If next point is NOT point 2, then the edge
//must be switched.
if(Vector3Equal(apVertexArray,pTri1[ lNextInTri],apVertexArray,Edge.point2,alVtxStride))
{
unsigned int lTemp = Edge.point1;
Edge.point1 = Edge.point2;
Edge.point2 = lTemp;
if(bLog)Log("Switching points\n");
}
if(bLog)Log("%d: p(%d)-p(%d)|(%d)-(%d)\n",edge,
Edge.point1,Edge.point2,
Edge.tri1, Edge.tri2);
//Log("-----------\n");
avEdges.push_back(Edge);
}
return true;
}*/
//-----------------------------------------------------------------------
} // namespace hpl
|