/usr/include/libwildmagic/Wm5Matrix3.inl is in libwildmagic-dev 5.13-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 | // Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.2 (2012/07/29)
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>::Matrix3 (bool makeZero)
{
if (makeZero)
{
MakeZero();
}
else
{
MakeIdentity();
}
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>::Matrix3 (const Matrix3& mat)
{
mEntry[0] = mat.mEntry[0];
mEntry[1] = mat.mEntry[1];
mEntry[2] = mat.mEntry[2];
mEntry[3] = mat.mEntry[3];
mEntry[4] = mat.mEntry[4];
mEntry[5] = mat.mEntry[5];
mEntry[6] = mat.mEntry[6];
mEntry[7] = mat.mEntry[7];
mEntry[8] = mat.mEntry[8];
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>::Matrix3 (Real m00, Real m01, Real m02, Real m10, Real m11,
Real m12, Real m20, Real m21, Real m22)
{
mEntry[0] = m00;
mEntry[1] = m01;
mEntry[2] = m02;
mEntry[3] = m10;
mEntry[4] = m11;
mEntry[5] = m12;
mEntry[6] = m20;
mEntry[7] = m21;
mEntry[8] = m22;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>::Matrix3 (const Real entry[9], bool rowMajor)
{
if (rowMajor)
{
mEntry[0] = entry[0];
mEntry[1] = entry[1];
mEntry[2] = entry[2];
mEntry[3] = entry[3];
mEntry[4] = entry[4];
mEntry[5] = entry[5];
mEntry[6] = entry[6];
mEntry[7] = entry[7];
mEntry[8] = entry[8];
}
else
{
mEntry[0] = entry[0];
mEntry[1] = entry[3];
mEntry[2] = entry[6];
mEntry[3] = entry[1];
mEntry[4] = entry[4];
mEntry[5] = entry[7];
mEntry[6] = entry[2];
mEntry[7] = entry[5];
mEntry[8] = entry[8];
}
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>::Matrix3 (const Vector3<Real>& u, const Vector3<Real>& v,
const Vector3<Real>& w, bool columns)
{
if (columns)
{
mEntry[0] = u[0];
mEntry[1] = v[0];
mEntry[2] = w[0];
mEntry[3] = u[1];
mEntry[4] = v[1];
mEntry[5] = w[1];
mEntry[6] = u[2];
mEntry[7] = v[2];
mEntry[8] = w[2];
}
else
{
mEntry[0] = u[0];
mEntry[1] = u[1];
mEntry[2] = u[2];
mEntry[3] = v[0];
mEntry[4] = v[1];
mEntry[5] = v[2];
mEntry[6] = w[0];
mEntry[7] = w[1];
mEntry[8] = w[2];
}
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>::Matrix3 (const Vector3<Real>* v, bool columns)
{
if (columns)
{
mEntry[0] = v[0][0];
mEntry[1] = v[1][0];
mEntry[2] = v[2][0];
mEntry[3] = v[0][1];
mEntry[4] = v[1][1];
mEntry[5] = v[2][1];
mEntry[6] = v[0][2];
mEntry[7] = v[1][2];
mEntry[8] = v[2][2];
}
else
{
mEntry[0] = v[0][0];
mEntry[1] = v[0][1];
mEntry[2] = v[0][2];
mEntry[3] = v[1][0];
mEntry[4] = v[1][1];
mEntry[5] = v[1][2];
mEntry[6] = v[2][0];
mEntry[7] = v[2][1];
mEntry[8] = v[2][2];
}
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>::Matrix3 (Real m00, Real m11, Real m22)
{
MakeDiagonal(m00, m11, m22);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>::Matrix3 (const Vector3<Real>& axis, Real angle)
{
MakeRotation(axis, angle);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>::Matrix3 (const Vector3<Real>& u, const Vector3<Real>& v)
{
MakeTensorProduct(u, v);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::operator= (const Matrix3& mat)
{
mEntry[0] = mat.mEntry[0];
mEntry[1] = mat.mEntry[1];
mEntry[2] = mat.mEntry[2];
mEntry[3] = mat.mEntry[3];
mEntry[4] = mat.mEntry[4];
mEntry[5] = mat.mEntry[5];
mEntry[6] = mat.mEntry[6];
mEntry[7] = mat.mEntry[7];
mEntry[8] = mat.mEntry[8];
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::MakeZero ()
{
mEntry[0] = (Real)0;
mEntry[1] = (Real)0;
mEntry[2] = (Real)0;
mEntry[3] = (Real)0;
mEntry[4] = (Real)0;
mEntry[5] = (Real)0;
mEntry[6] = (Real)0;
mEntry[7] = (Real)0;
mEntry[8] = (Real)0;
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::MakeIdentity ()
{
mEntry[0] = (Real)1;
mEntry[1] = (Real)0;
mEntry[2] = (Real)0;
mEntry[3] = (Real)0;
mEntry[4] = (Real)1;
mEntry[5] = (Real)0;
mEntry[6] = (Real)0;
mEntry[7] = (Real)0;
mEntry[8] = (Real)1;
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::MakeDiagonal (Real m00, Real m11, Real m22)
{
mEntry[0] = m00;
mEntry[1] = (Real)0;
mEntry[2] = (Real)0;
mEntry[3] = (Real)0;
mEntry[4] = m11;
mEntry[5] = (Real)0;
mEntry[6] = (Real)0;
mEntry[7] = (Real)0;
mEntry[8] = m22;
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::MakeRotation (const Vector3<Real>& axis,
Real angle)
{
Real cs = Math<Real>::Cos(angle);
Real sn = Math<Real>::Sin(angle);
Real oneMinusCos = ((Real)1) - cs;
Real x2 = axis[0]*axis[0];
Real y2 = axis[1]*axis[1];
Real z2 = axis[2]*axis[2];
Real xym = axis[0]*axis[1]*oneMinusCos;
Real xzm = axis[0]*axis[2]*oneMinusCos;
Real yzm = axis[1]*axis[2]*oneMinusCos;
Real xSin = axis[0]*sn;
Real ySin = axis[1]*sn;
Real zSin = axis[2]*sn;
mEntry[0] = x2*oneMinusCos + cs;
mEntry[1] = xym - zSin;
mEntry[2] = xzm + ySin;
mEntry[3] = xym + zSin;
mEntry[4] = y2*oneMinusCos + cs;
mEntry[5] = yzm - xSin;
mEntry[6] = xzm - ySin;
mEntry[7] = yzm + xSin;
mEntry[8] = z2*oneMinusCos + cs;
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::MakeTensorProduct (const Vector3<Real>& u,
const Vector3<Real>& v)
{
mEntry[0] = u[0]*v[0];
mEntry[1] = u[0]*v[1];
mEntry[2] = u[0]*v[2];
mEntry[3] = u[1]*v[0];
mEntry[4] = u[1]*v[1];
mEntry[5] = u[1]*v[2];
mEntry[6] = u[2]*v[0];
mEntry[7] = u[2]*v[1];
mEntry[8] = u[2]*v[2];
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::operator+ (const Matrix3& mat) const
{
return Matrix3<Real>
(
mEntry[0] + mat.mEntry[0],
mEntry[1] + mat.mEntry[1],
mEntry[2] + mat.mEntry[2],
mEntry[3] + mat.mEntry[3],
mEntry[4] + mat.mEntry[4],
mEntry[5] + mat.mEntry[5],
mEntry[6] + mat.mEntry[6],
mEntry[7] + mat.mEntry[7],
mEntry[8] + mat.mEntry[8]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::operator- (const Matrix3& mat) const
{
return Matrix3<Real>
(
mEntry[0] - mat.mEntry[0],
mEntry[1] - mat.mEntry[1],
mEntry[2] - mat.mEntry[2],
mEntry[3] - mat.mEntry[3],
mEntry[4] - mat.mEntry[4],
mEntry[5] - mat.mEntry[5],
mEntry[6] - mat.mEntry[6],
mEntry[7] - mat.mEntry[7],
mEntry[8] - mat.mEntry[8]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::operator* (Real scalar) const
{
return Matrix3<Real>
(
scalar*mEntry[0],
scalar*mEntry[1],
scalar*mEntry[2],
scalar*mEntry[3],
scalar*mEntry[4],
scalar*mEntry[5],
scalar*mEntry[6],
scalar*mEntry[7],
scalar*mEntry[8]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::operator/ (Real scalar) const
{
if (scalar != (Real)0)
{
Real invScalar = ((Real)1)/scalar;
return Matrix3<Real>
(
invScalar*mEntry[0],
invScalar*mEntry[1],
invScalar*mEntry[2],
invScalar*mEntry[3],
invScalar*mEntry[4],
invScalar*mEntry[5],
invScalar*mEntry[6],
invScalar*mEntry[7],
invScalar*mEntry[8]
);
}
else
{
return Matrix3<Real>
(
Math<Real>::MAX_REAL,
Math<Real>::MAX_REAL,
Math<Real>::MAX_REAL,
Math<Real>::MAX_REAL,
Math<Real>::MAX_REAL,
Math<Real>::MAX_REAL,
Math<Real>::MAX_REAL,
Math<Real>::MAX_REAL,
Math<Real>::MAX_REAL
);
}
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::operator- () const
{
return Matrix3<Real>
(
-mEntry[0],
-mEntry[1],
-mEntry[2],
-mEntry[3],
-mEntry[4],
-mEntry[5],
-mEntry[6],
-mEntry[7],
-mEntry[8]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::operator+= (const Matrix3& mat)
{
mEntry[0] += mat.mEntry[0];
mEntry[1] += mat.mEntry[1];
mEntry[2] += mat.mEntry[2];
mEntry[3] += mat.mEntry[3];
mEntry[4] += mat.mEntry[4];
mEntry[5] += mat.mEntry[5];
mEntry[6] += mat.mEntry[6];
mEntry[7] += mat.mEntry[7];
mEntry[8] += mat.mEntry[8];
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::operator-= (const Matrix3& mat)
{
mEntry[0] -= mat.mEntry[0];
mEntry[1] -= mat.mEntry[1];
mEntry[2] -= mat.mEntry[2];
mEntry[3] -= mat.mEntry[3];
mEntry[4] -= mat.mEntry[4];
mEntry[5] -= mat.mEntry[5];
mEntry[6] -= mat.mEntry[6];
mEntry[7] -= mat.mEntry[7];
mEntry[8] -= mat.mEntry[8];
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::operator*= (Real scalar)
{
mEntry[0] *= scalar;
mEntry[1] *= scalar;
mEntry[2] *= scalar;
mEntry[3] *= scalar;
mEntry[4] *= scalar;
mEntry[5] *= scalar;
mEntry[6] *= scalar;
mEntry[7] *= scalar;
mEntry[8] *= scalar;
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::operator/= (Real scalar)
{
if (scalar != (Real)0)
{
Real invScalar = ((Real)1)/scalar;
mEntry[0] *= invScalar;
mEntry[1] *= invScalar;
mEntry[2] *= invScalar;
mEntry[3] *= invScalar;
mEntry[4] *= invScalar;
mEntry[5] *= invScalar;
mEntry[6] *= invScalar;
mEntry[7] *= invScalar;
mEntry[8] *= invScalar;
}
else
{
mEntry[0] = Math<Real>::MAX_REAL;
mEntry[1] = Math<Real>::MAX_REAL;
mEntry[2] = Math<Real>::MAX_REAL;
mEntry[3] = Math<Real>::MAX_REAL;
mEntry[4] = Math<Real>::MAX_REAL;
mEntry[5] = Math<Real>::MAX_REAL;
mEntry[6] = Math<Real>::MAX_REAL;
mEntry[7] = Math<Real>::MAX_REAL;
mEntry[8] = Math<Real>::MAX_REAL;
}
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
Vector3<Real> Matrix3<Real>::operator* (const Vector3<Real>& vec) const
{
return Vector3<Real>
(
mEntry[0]*vec[0] + mEntry[1]*vec[1] + mEntry[2]*vec[2],
mEntry[3]*vec[0] + mEntry[4]*vec[1] + mEntry[5]*vec[2],
mEntry[6]*vec[0] + mEntry[7]*vec[1] + mEntry[8]*vec[2]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Real Matrix3<Real>::QForm (const Vector3<Real>& u, const Vector3<Real>& v)
const
{
return u.Dot((*this)*v);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::Transpose () const
{
return Matrix3<Real>
(
mEntry[0],
mEntry[3],
mEntry[6],
mEntry[1],
mEntry[4],
mEntry[7],
mEntry[2],
mEntry[5],
mEntry[8]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::operator* (const Matrix3& mat) const
{
// A*B
return Matrix3<Real>
(
mEntry[0]*mat.mEntry[0] +
mEntry[1]*mat.mEntry[3] +
mEntry[2]*mat.mEntry[6],
mEntry[0]*mat.mEntry[1] +
mEntry[1]*mat.mEntry[4] +
mEntry[2]*mat.mEntry[7],
mEntry[0]*mat.mEntry[2] +
mEntry[1]*mat.mEntry[5] +
mEntry[2]*mat.mEntry[8],
mEntry[3]*mat.mEntry[0] +
mEntry[4]*mat.mEntry[3] +
mEntry[5]*mat.mEntry[6],
mEntry[3]*mat.mEntry[1] +
mEntry[4]*mat.mEntry[4] +
mEntry[5]*mat.mEntry[7],
mEntry[3]*mat.mEntry[2] +
mEntry[4]*mat.mEntry[5] +
mEntry[5]*mat.mEntry[8],
mEntry[6]*mat.mEntry[0] +
mEntry[7]*mat.mEntry[3] +
mEntry[8]*mat.mEntry[6],
mEntry[6]*mat.mEntry[1] +
mEntry[7]*mat.mEntry[4] +
mEntry[8]*mat.mEntry[7],
mEntry[6]*mat.mEntry[2] +
mEntry[7]*mat.mEntry[5] +
mEntry[8]*mat.mEntry[8]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::TransposeTimes (const Matrix3& mat) const
{
// A^T*B
return Matrix3<Real>
(
mEntry[0]*mat.mEntry[0] +
mEntry[3]*mat.mEntry[3] +
mEntry[6]*mat.mEntry[6],
mEntry[0]*mat.mEntry[1] +
mEntry[3]*mat.mEntry[4] +
mEntry[6]*mat.mEntry[7],
mEntry[0]*mat.mEntry[2] +
mEntry[3]*mat.mEntry[5] +
mEntry[6]*mat.mEntry[8],
mEntry[1]*mat.mEntry[0] +
mEntry[4]*mat.mEntry[3] +
mEntry[7]*mat.mEntry[6],
mEntry[1]*mat.mEntry[1] +
mEntry[4]*mat.mEntry[4] +
mEntry[7]*mat.mEntry[7],
mEntry[1]*mat.mEntry[2] +
mEntry[4]*mat.mEntry[5] +
mEntry[7]*mat.mEntry[8],
mEntry[2]*mat.mEntry[0] +
mEntry[5]*mat.mEntry[3] +
mEntry[8]*mat.mEntry[6],
mEntry[2]*mat.mEntry[1] +
mEntry[5]*mat.mEntry[4] +
mEntry[8]*mat.mEntry[7],
mEntry[2]*mat.mEntry[2] +
mEntry[5]*mat.mEntry[5] +
mEntry[8]*mat.mEntry[8]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::TimesTranspose (const Matrix3& mat) const
{
// A*B^T
return Matrix3<Real>
(
mEntry[0]*mat.mEntry[0] +
mEntry[1]*mat.mEntry[1] +
mEntry[2]*mat.mEntry[2],
mEntry[0]*mat.mEntry[3] +
mEntry[1]*mat.mEntry[4] +
mEntry[2]*mat.mEntry[5],
mEntry[0]*mat.mEntry[6] +
mEntry[1]*mat.mEntry[7] +
mEntry[2]*mat.mEntry[8],
mEntry[3]*mat.mEntry[0] +
mEntry[4]*mat.mEntry[1] +
mEntry[5]*mat.mEntry[2],
mEntry[3]*mat.mEntry[3] +
mEntry[4]*mat.mEntry[4] +
mEntry[5]*mat.mEntry[5],
mEntry[3]*mat.mEntry[6] +
mEntry[4]*mat.mEntry[7] +
mEntry[5]*mat.mEntry[8],
mEntry[6]*mat.mEntry[0] +
mEntry[7]*mat.mEntry[1] +
mEntry[8]*mat.mEntry[2],
mEntry[6]*mat.mEntry[3] +
mEntry[7]*mat.mEntry[4] +
mEntry[8]*mat.mEntry[5],
mEntry[6]*mat.mEntry[6] +
mEntry[7]*mat.mEntry[7] +
mEntry[8]*mat.mEntry[8]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::TransposeTimesTranspose (const Matrix3& mat)
const
{
// A^T*B^T
return Matrix3<Real>
(
mEntry[0]*mat.mEntry[0] +
mEntry[3]*mat.mEntry[1] +
mEntry[6]*mat.mEntry[2],
mEntry[0]*mat.mEntry[3] +
mEntry[3]*mat.mEntry[4] +
mEntry[6]*mat.mEntry[5],
mEntry[0]*mat.mEntry[6] +
mEntry[3]*mat.mEntry[7] +
mEntry[6]*mat.mEntry[8],
mEntry[1]*mat.mEntry[0] +
mEntry[4]*mat.mEntry[1] +
mEntry[7]*mat.mEntry[2],
mEntry[1]*mat.mEntry[3] +
mEntry[4]*mat.mEntry[4] +
mEntry[7]*mat.mEntry[5],
mEntry[1]*mat.mEntry[6] +
mEntry[4]*mat.mEntry[7] +
mEntry[7]*mat.mEntry[8],
mEntry[2]*mat.mEntry[0] +
mEntry[5]*mat.mEntry[1] +
mEntry[8]*mat.mEntry[2],
mEntry[2]*mat.mEntry[3] +
mEntry[5]*mat.mEntry[4] +
mEntry[8]*mat.mEntry[5],
mEntry[2]*mat.mEntry[6] +
mEntry[5]*mat.mEntry[7] +
mEntry[8]*mat.mEntry[8]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::TimesDiagonal (const Vector3<Real>& diag) const
{
return Matrix3<Real>
(
mEntry[0]*diag[0],
mEntry[1]*diag[1],
mEntry[2]*diag[2],
mEntry[3]*diag[0],
mEntry[4]*diag[1],
mEntry[5]*diag[2],
mEntry[6]*diag[0],
mEntry[7]*diag[1],
mEntry[8]*diag[2]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::DiagonalTimes (const Vector3<Real>& diag) const
{
return Matrix3<Real>
(
diag[0]*mEntry[0],
diag[0]*mEntry[1],
diag[0]*mEntry[2],
diag[1]*mEntry[3],
diag[1]*mEntry[4],
diag[1]*mEntry[5],
diag[2]*mEntry[6],
diag[2]*mEntry[7],
diag[2]*mEntry[8]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::Inverse (const Real epsilon) const
{
// Invert a 3x3 using cofactors. This is faster than using a generic
// Gaussian elimination because of the loop overhead of such a method.
Matrix3 inverse;
// Compute the adjoint.
inverse.mEntry[0] = mEntry[4]*mEntry[8] - mEntry[5]*mEntry[7];
inverse.mEntry[1] = mEntry[2]*mEntry[7] - mEntry[1]*mEntry[8];
inverse.mEntry[2] = mEntry[1]*mEntry[5] - mEntry[2]*mEntry[4];
inverse.mEntry[3] = mEntry[5]*mEntry[6] - mEntry[3]*mEntry[8];
inverse.mEntry[4] = mEntry[0]*mEntry[8] - mEntry[2]*mEntry[6];
inverse.mEntry[5] = mEntry[2]*mEntry[3] - mEntry[0]*mEntry[5];
inverse.mEntry[6] = mEntry[3]*mEntry[7] - mEntry[4]*mEntry[6];
inverse.mEntry[7] = mEntry[1]*mEntry[6] - mEntry[0]*mEntry[7];
inverse.mEntry[8] = mEntry[0]*mEntry[4] - mEntry[1]*mEntry[3];
Real det = mEntry[0]*inverse.mEntry[0] + mEntry[1]*inverse.mEntry[3] +
mEntry[2]*inverse.mEntry[6];
if (Math<Real>::FAbs(det) > epsilon)
{
Real invDet = ((Real)1)/det;
inverse.mEntry[0] *= invDet;
inverse.mEntry[1] *= invDet;
inverse.mEntry[2] *= invDet;
inverse.mEntry[3] *= invDet;
inverse.mEntry[4] *= invDet;
inverse.mEntry[5] *= invDet;
inverse.mEntry[6] *= invDet;
inverse.mEntry[7] *= invDet;
inverse.mEntry[8] *= invDet;
return inverse;
}
return ZERO;
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real> Matrix3<Real>::Adjoint () const
{
return Matrix3<Real>
(
mEntry[4]*mEntry[8] - mEntry[5]*mEntry[7],
mEntry[2]*mEntry[7] - mEntry[1]*mEntry[8],
mEntry[1]*mEntry[5] - mEntry[2]*mEntry[4],
mEntry[5]*mEntry[6] - mEntry[3]*mEntry[8],
mEntry[0]*mEntry[8] - mEntry[2]*mEntry[6],
mEntry[2]*mEntry[3] - mEntry[0]*mEntry[5],
mEntry[3]*mEntry[7] - mEntry[4]*mEntry[6],
mEntry[1]*mEntry[6] - mEntry[0]*mEntry[7],
mEntry[0]*mEntry[4] - mEntry[1]*mEntry[3]
);
}
//----------------------------------------------------------------------------
template <typename Real>
Real Matrix3<Real>::Determinant () const
{
Real co00 = mEntry[4]*mEntry[8] - mEntry[5]*mEntry[7];
Real co10 = mEntry[5]*mEntry[6] - mEntry[3]*mEntry[8];
Real co20 = mEntry[3]*mEntry[7] - mEntry[4]*mEntry[6];
Real det = mEntry[0]*co00 + mEntry[1]*co10 + mEntry[2]*co20;
return det;
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::ExtractAxisAngle (Vector3<Real>& axis, Real& angle) const
{
// Let (x,y,z) be the unit-length axis and let A be an angle of rotation.
// The rotation matrix is R = I + sin(A)*P + (1-cos(A))*P^2 where
// I is the identity and
//
// +- -+
// P = | 0 -z +y |
// | +z 0 -x |
// | -y +x 0 |
// +- -+
//
// If A > 0, R represents a counterclockwise rotation about the axis in
// the sense of looking from the tip of the axis vector towards the
// origin. Some algebra will show that
//
// cos(A) = (trace(R)-1)/2 and R - R^t = 2*sin(A)*P
//
// In the event that A = pi, R-R^t = 0 which prevents us from extracting
// the axis through P. Instead note that R = I+2*P^2 when A = pi, so
// P^2 = (R-I)/2. The diagonal entries of P^2 are x^2-1, y^2-1, and
// z^2-1. We can solve these for axis (x,y,z). Because the angle is pi,
// it does not matter which sign you choose on the square roots.
Real trace = mEntry[0] + mEntry[4] + mEntry[8];
Real cs = ((Real)0.5)*(trace - (Real)1);
angle = Math<Real>::ACos(cs); // in [0,PI]
if (angle > (Real)0)
{
if (angle < Math<Real>::PI)
{
axis[0] = mEntry[7] - mEntry[5];
axis[1] = mEntry[2] - mEntry[6];
axis[2] = mEntry[3] - mEntry[1];
axis.Normalize();
}
else
{
// angle is PI
Real halfInverse;
if (mEntry[0] >= mEntry[4])
{
// r00 >= r11
if (mEntry[0] >= mEntry[8])
{
// r00 is maximum diagonal term
axis[0] = ((Real)0.5)*Math<Real>::Sqrt((Real)1
+ mEntry[0] - mEntry[4] - mEntry[8]);
halfInverse = ((Real)0.5)/axis[0];
axis[1] = halfInverse*mEntry[1];
axis[2] = halfInverse*mEntry[2];
}
else
{
// r22 is maximum diagonal term
axis[2] = ((Real)0.5)*Math<Real>::Sqrt((Real)1
+ mEntry[8] - mEntry[0] - mEntry[4]);
halfInverse = ((Real)0.5)/axis[2];
axis[0] = halfInverse*mEntry[2];
axis[1] = halfInverse*mEntry[5];
}
}
else
{
// r11 > r00
if (mEntry[4] >= mEntry[8])
{
// r11 is maximum diagonal term
axis[1] = ((Real)0.5)*Math<Real>::Sqrt((Real)1
+ mEntry[4] - mEntry[0] - mEntry[8]);
halfInverse = ((Real)0.5)/axis[1];
axis[0] = halfInverse*mEntry[1];
axis[2] = halfInverse*mEntry[5];
}
else
{
// r22 is maximum diagonal term
axis[2] = ((Real)0.5)*Math<Real>::Sqrt((Real)1
+ mEntry[8] - mEntry[0] - mEntry[4]);
halfInverse = ((Real)0.5)/axis[2];
axis[0] = halfInverse*mEntry[2];
axis[1] = halfInverse*mEntry[5];
}
}
}
}
else
{
// The angle is 0 and the matrix is the identity. Any axis will
// work, so just use the x-axis.
axis[0] = (Real)1;
axis[1] = (Real)0;
axis[2] = (Real)0;
}
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::Orthonormalize ()
{
// Algorithm uses Gram-Schmidt orthogonalization. If 'this' matrix is
// M = [m0|m1|m2], then orthonormal output matrix is Q = [q0|q1|q2],
//
// q0 = m0/|m0|
// q1 = (m1-(q0*m1)q0)/|m1-(q0*m1)q0|
// q2 = (m2-(q0*m2)q0-(q1*m2)q1)/|m2-(q0*m2)q0-(q1*m2)q1|
//
// where |V| indicates length of vector V and A*B indicates dot
// product of vectors A and B.
// Compute q0.
Real invLength = Math<Real>::InvSqrt(mEntry[0]*mEntry[0] +
mEntry[3]*mEntry[3] + mEntry[6]*mEntry[6]);
mEntry[0] *= invLength;
mEntry[3] *= invLength;
mEntry[6] *= invLength;
// Compute q1.
Real dot0 = mEntry[0]*mEntry[1] + mEntry[3]*mEntry[4] +
mEntry[6]*mEntry[7];
mEntry[1] -= dot0*mEntry[0];
mEntry[4] -= dot0*mEntry[3];
mEntry[7] -= dot0*mEntry[6];
invLength = Math<Real>::InvSqrt(mEntry[1]*mEntry[1] +
mEntry[4]*mEntry[4] + mEntry[7]*mEntry[7]);
mEntry[1] *= invLength;
mEntry[4] *= invLength;
mEntry[7] *= invLength;
// compute q2
Real dot1 = mEntry[1]*mEntry[2] + mEntry[4]*mEntry[5] +
mEntry[7]*mEntry[8];
dot0 = mEntry[0]*mEntry[2] + mEntry[3]*mEntry[5] +
mEntry[6]*mEntry[8];
mEntry[2] -= dot0*mEntry[0] + dot1*mEntry[1];
mEntry[5] -= dot0*mEntry[3] + dot1*mEntry[4];
mEntry[8] -= dot0*mEntry[6] + dot1*mEntry[7];
invLength = Math<Real>::InvSqrt(mEntry[2]*mEntry[2] +
mEntry[5]*mEntry[5] + mEntry[8]*mEntry[8]);
mEntry[2] *= invLength;
mEntry[5] *= invLength;
mEntry[8] *= invLength;
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::EigenDecomposition (Matrix3& rot, Matrix3& diag) const
{
// Factor M = R*D*R^T. The columns of R are the eigenvectors. The
// diagonal entries of D are the corresponding eigenvalues.
Real diagonal[3], subdiagonal[2];
rot = *this;
bool reflection = rot.Tridiagonalize(diagonal, subdiagonal);
bool converged = rot.QLAlgorithm(diagonal, subdiagonal);
assertion(converged, "QLAlgorithm failed to converge\n");
WM5_UNUSED(converged);
// Sort the eigenvalues in increasing order, d0 <= d1 <= d2. This is an
// insertion sort.
int i;
Real save;
if (diagonal[1] < diagonal[0])
{
// Swap d0 and d1.
save = diagonal[0];
diagonal[0] = diagonal[1];
diagonal[1] = save;
// Swap V0 and V1.
for (i = 0; i < 3; ++i)
{
save = rot[i][0];
rot[i][0] = rot[i][1];
rot[i][1] = save;
}
reflection = !reflection;
}
if (diagonal[2] < diagonal[1])
{
// Swap d1 and d2.
save = diagonal[1];
diagonal[1] = diagonal[2];
diagonal[2] = save;
// Swap V1 and V2.
for (i = 0; i < 3; ++i)
{
save = rot[i][1];
rot[i][1] = rot[i][2];
rot[i][2] = save;
}
reflection = !reflection;
}
if (diagonal[1] < diagonal[0])
{
// Swap d0 and d1.
save = diagonal[0];
diagonal[0] = diagonal[1];
diagonal[1] = save;
// Swap V0 and V1.
for (i = 0; i < 3; ++i)
{
save = rot[i][0];
rot[i][0] = rot[i][1];
rot[i][1] = save;
}
reflection = !reflection;
}
diag.MakeDiagonal(diagonal[0], diagonal[1], diagonal[2]);
if (reflection)
{
// The orthogonal transformation that diagonalizes M is a reflection.
// Make the eigenvectors a right-handed system by changing sign on
// the last column.
rot[0][2] = -rot[0][2];
rot[1][2] = -rot[1][2];
rot[2][2] = -rot[2][2];
}
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerXYZ (Real xAngle, Real yAngle, Real zAngle)
{
Real cs, sn;
cs = Math<Real>::Cos(xAngle);
sn = Math<Real>::Sin(xAngle);
Matrix3 xMat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
cs = Math<Real>::Cos(yAngle);
sn = Math<Real>::Sin(yAngle);
Matrix3 yMat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
cs = Math<Real>::Cos(zAngle);
sn = Math<Real>::Sin(zAngle);
Matrix3 zMat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
*this = xMat*(yMat*zMat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerXZY (Real xAngle, Real zAngle, Real yAngle)
{
Real cs, sn;
cs = Math<Real>::Cos(xAngle);
sn = Math<Real>::Sin(xAngle);
Matrix3 xMat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
cs = Math<Real>::Cos(zAngle);
sn = Math<Real>::Sin(zAngle);
Matrix3 zMat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
cs = Math<Real>::Cos(yAngle);
sn = Math<Real>::Sin(yAngle);
Matrix3 yMat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
*this = xMat*(zMat*yMat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerYXZ (Real yAngle, Real xAngle, Real zAngle)
{
Real cs, sn;
cs = Math<Real>::Cos(yAngle);
sn = Math<Real>::Sin(yAngle);
Matrix3 yMat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
cs = Math<Real>::Cos(xAngle);
sn = Math<Real>::Sin(xAngle);
Matrix3 xMat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
cs = Math<Real>::Cos(zAngle);
sn = Math<Real>::Sin(zAngle);
Matrix3 zMat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
*this = yMat*(xMat*zMat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerYZX (Real yAngle, Real zAngle, Real xAngle)
{
Real cs, sn;
cs = Math<Real>::Cos(yAngle);
sn = Math<Real>::Sin(yAngle);
Matrix3 yMat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
cs = Math<Real>::Cos(zAngle);
sn = Math<Real>::Sin(zAngle);
Matrix3 zMat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
cs = Math<Real>::Cos(xAngle);
sn = Math<Real>::Sin(xAngle);
Matrix3 xMat(
(Real)1,(Real)0,(Real)0,
(Real)0,cs,-sn,
(Real)0,sn,cs);
*this = yMat*(zMat*xMat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerZXY (Real zAngle, Real xAngle, Real yAngle)
{
Real cs, sn;
cs = Math<Real>::Cos(zAngle);
sn = Math<Real>::Sin(zAngle);
Matrix3 zMat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
cs = Math<Real>::Cos(xAngle);
sn = Math<Real>::Sin(xAngle);
Matrix3 xMat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
cs = Math<Real>::Cos(yAngle);
sn = Math<Real>::Sin(yAngle);
Matrix3 yMat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
*this = zMat*(xMat*yMat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerZYX (Real zAngle, Real yAngle, Real xAngle)
{
Real cs, sn;
cs = Math<Real>::Cos(zAngle);
sn = Math<Real>::Sin(zAngle);
Matrix3 zMat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
cs = Math<Real>::Cos(yAngle);
sn = Math<Real>::Sin(yAngle);
Matrix3 yMat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
cs = Math<Real>::Cos(xAngle);
sn = Math<Real>::Sin(xAngle);
Matrix3 xMat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
*this = zMat*(yMat*xMat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerXYX (Real x0Angle, Real yAngle, Real x1Angle)
{
Real cs, sn;
cs = Math<Real>::Cos(x0Angle);
sn = Math<Real>::Sin(x0Angle);
Matrix3 x0Mat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
cs = Math<Real>::Cos(yAngle);
sn = Math<Real>::Sin(yAngle);
Matrix3 yMat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
cs = Math<Real>::Cos(x1Angle);
sn = Math<Real>::Sin(x1Angle);
Matrix3 x1Mat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
*this = x0Mat*(yMat*x1Mat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerXZX (Real x0Angle, Real zAngle, Real x1Angle)
{
Real cs, sn;
cs = Math<Real>::Cos(x0Angle);
sn = Math<Real>::Sin(x0Angle);
Matrix3 x0Mat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
cs = Math<Real>::Cos(zAngle);
sn = Math<Real>::Sin(zAngle);
Matrix3 zMat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
cs = Math<Real>::Cos(x1Angle);
sn = Math<Real>::Sin(x1Angle);
Matrix3 x1Mat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
*this = x0Mat*(zMat*x1Mat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerYXY (Real y0Angle, Real xAngle, Real y1Angle)
{
Real cs, sn;
cs = Math<Real>::Cos(y0Angle);
sn = Math<Real>::Sin(y0Angle);
Matrix3 y0Mat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
cs = Math<Real>::Cos(xAngle);
sn = Math<Real>::Sin(xAngle);
Matrix3 xMat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
cs = Math<Real>::Cos(y1Angle);
sn = Math<Real>::Sin(y1Angle);
Matrix3 y1Mat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
*this = y0Mat*(xMat*y1Mat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerYZY (Real y0Angle, Real zAngle, Real y1Angle)
{
Real cs, sn;
cs = Math<Real>::Cos(y0Angle);
sn = Math<Real>::Sin(y0Angle);
Matrix3 y0Mat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
cs = Math<Real>::Cos(zAngle);
sn = Math<Real>::Sin(zAngle);
Matrix3 zMat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
cs = Math<Real>::Cos(y1Angle);
sn = Math<Real>::Sin(y1Angle);
Matrix3 y1Mat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
*this = y0Mat*(zMat*y1Mat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerZXZ (Real z0Angle, Real xAngle, Real z1Angle)
{
Real cs, sn;
cs = Math<Real>::Cos(z0Angle);
sn = Math<Real>::Sin(z0Angle);
Matrix3 z0Mat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
cs = Math<Real>::Cos(xAngle);
sn = Math<Real>::Sin(xAngle);
Matrix3 xMat(
(Real)1, (Real)0, (Real)0,
(Real)0, cs, -sn,
(Real)0, sn, cs);
cs = Math<Real>::Cos(z1Angle);
sn = Math<Real>::Sin(z1Angle);
Matrix3 z1Mat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
*this = z0Mat*(xMat*z1Mat);
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::MakeEulerZYZ (Real z0Angle, Real yAngle, Real z1Angle)
{
Real cs, sn;
cs = Math<Real>::Cos(z0Angle);
sn = Math<Real>::Sin(z0Angle);
Matrix3 z0Mat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
cs = Math<Real>::Cos(yAngle);
sn = Math<Real>::Sin(yAngle);
Matrix3 yMat(
cs, (Real)0, sn,
(Real)0, (Real)1, (Real)0,
-sn, (Real)0, cs);
cs = Math<Real>::Cos(z1Angle);
sn = Math<Real>::Sin(z1Angle);
Matrix3 z1Mat(
cs, -sn, (Real)0,
sn, cs, (Real)0,
(Real)0, (Real)0, (Real)1);
*this = z0Mat*(yMat*z1Mat);
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerXYZ (
Real& xAngle, Real& yAngle, Real& zAngle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cy*cz -cy*sz sy |
// | r10 r11 r12 | = | cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx |
// | r20 r21 r22 | | -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy |
// +- -+ +- -+
if (mEntry[2] < (Real)1)
{
if (mEntry[2] > -(Real)1)
{
// y_angle = asin(r02)
// x_angle = atan2(-r12,r22)
// z_angle = atan2(-r01,r00)
yAngle = (Real)asin((double)mEntry[2]);
xAngle = Math<Real>::ATan2(-mEntry[5], mEntry[8]);
zAngle = Math<Real>::ATan2(-mEntry[1], mEntry[0]);
return EA_UNIQUE;
}
else
{
// y_angle = -pi/2
// z_angle - x_angle = atan2(r10,r11)
// WARNING. The solution is not unique. Choosing z_angle = 0.
yAngle = -Math<Real>::HALF_PI;
xAngle = -Math<Real>::ATan2(mEntry[3], mEntry[4]);
zAngle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// y_angle = +pi/2
// z_angle + x_angle = atan2(r10,r11)
// WARNING. The solutions is not unique. Choosing z_angle = 0.
yAngle = Math<Real>::HALF_PI;
xAngle = Math<Real>::ATan2(mEntry[3], mEntry[4]);
zAngle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerXZY (
Real& xAngle, Real& zAngle, Real& yAngle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cy*cz -sz cz*sy |
// | r10 r11 r12 | = | sx*sy+cx*cy*sz cx*cz -cy*sx+cx*sy*sz |
// | r20 r21 r22 | | -cx*sy+cy*sx*sz cz*sx cx*cy+sx*sy*sz |
// +- -+ +- -+
if (mEntry[1] < (Real)1)
{
if (mEntry[1] > -(Real)1)
{
// z_angle = asin(-r01)
// x_angle = atan2(r21,r11)
// y_angle = atan2(r02,r00)
zAngle = (Real)asin(-(double)mEntry[1]);
xAngle = Math<Real>::ATan2(mEntry[7], mEntry[4]);
yAngle = Math<Real>::ATan2(mEntry[2], mEntry[0]);
return EA_UNIQUE;
}
else
{
// z_angle = +pi/2
// y_angle - x_angle = atan2(-r20,r22)
// WARNING. The solution is not unique. Choosing y_angle = 0.
zAngle = Math<Real>::HALF_PI;
xAngle = -Math<Real>::ATan2(-mEntry[6] ,mEntry[8]);
yAngle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// z_angle = -pi/2
// y_angle + x_angle = atan2(-r20,r22)
// WARNING. The solution is not unique. Choosing y_angle = 0.
zAngle = -Math<Real>::HALF_PI;
xAngle = Math<Real>::ATan2(-mEntry[6], mEntry[8]);
yAngle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerYXZ (
Real& yAngle, Real& xAngle, Real& zAngle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cy*cz+sx*sy*sz cz*sx*sy-cy*sz cx*sy |
// | r10 r11 r12 | = | cx*sz cx*cz -sx |
// | r20 r21 r22 | | -cz*sy+cy*sx*sz cy*cz*sx+sy*sz cx*cy |
// +- -+ +- -+
if (mEntry[5] < (Real)1)
{
if (mEntry[5] > -(Real)1)
{
// x_angle = asin(-r12)
// y_angle = atan2(r02,r22)
// z_angle = atan2(r10,r11)
xAngle = (Real)asin(-(double)mEntry[5]);
yAngle = Math<Real>::ATan2(mEntry[2], mEntry[8]);
zAngle = Math<Real>::ATan2(mEntry[3], mEntry[4]);
return EA_UNIQUE;
}
else
{
// x_angle = +pi/2
// z_angle - y_angle = atan2(-r01,r00)
// WARNING. The solution is not unique. Choosing z_angle = 0.
xAngle = Math<Real>::HALF_PI;
yAngle = -Math<Real>::ATan2(-mEntry[1], mEntry[0]);
zAngle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// x_angle = -pi/2
// z_angle + y_angle = atan2(-r01,r00)
// WARNING. The solution is not unique. Choosing z_angle = 0.
xAngle = -Math<Real>::HALF_PI;
yAngle = Math<Real>::ATan2(-mEntry[1], mEntry[0]);
zAngle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerYZX (
Real& yAngle, Real& zAngle, Real& xAngle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cy*cz sx*sy-cx*cy*sz cx*sy+cy*sx*sz |
// | r10 r11 r12 | = | sz cx*cz -cz*sx |
// | r20 r21 r22 | | -cz*sy cy*sx+cx*sy*sz cx*cy-sx*sy*sz |
// +- -+ +- -+
if (mEntry[3] < (Real)1)
{
if (mEntry[3] > -(Real)1)
{
// z_angle = asin(r10)
// y_angle = atan2(-r20,r00)
// x_angle = atan2(-r12,r11)
zAngle = (Real)asin((double)mEntry[3]);
yAngle = Math<Real>::ATan2(-mEntry[6], mEntry[0]);
xAngle = Math<Real>::ATan2(-mEntry[5], mEntry[4]);
return EA_UNIQUE;
}
else
{
// z_angle = -pi/2
// x_angle - y_angle = atan2(r21,r22)
// WARNING. The solution is not unique. Choosing x_angle = 0.
zAngle = -Math<Real>::HALF_PI;
yAngle = -Math<Real>::ATan2(mEntry[7], mEntry[8]);
xAngle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// z_angle = +pi/2
// x_angle + y_angle = atan2(r21,r22)
// WARNING. The solution is not unique. Choosing x_angle = 0.
zAngle = Math<Real>::HALF_PI;
yAngle = Math<Real>::ATan2(mEntry[7], mEntry[8]);
xAngle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerZXY (
Real& zAngle, Real& xAngle, Real& yAngle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cy*cz-sx*sy*sz -cx*sz cz*sy+cy*sx*sz |
// | r10 r11 r12 | = | cz*sx*sy+cy*sz cx*cz -cy*cz*sx+sy*sz |
// | r20 r21 r22 | | -cx*sy sx cx*cy |
// +- -+ +- -+
if (mEntry[7] < (Real)1)
{
if (mEntry[7] > -(Real)1)
{
// x_angle = asin(r21)
// z_angle = atan2(-r01,r11)
// y_angle = atan2(-r20,r22)
xAngle = (Real)asin((double)mEntry[7]);
zAngle = Math<Real>::ATan2(-mEntry[1], mEntry[4]);
yAngle = Math<Real>::ATan2(-mEntry[6], mEntry[8]);
return EA_UNIQUE;
}
else
{
// x_angle = -pi/2
// y_angle - z_angle = atan2(r02,r00)
// WARNING. The solution is not unique. Choosing y_angle = 0.
xAngle = -Math<Real>::HALF_PI;
zAngle = -Math<Real>::ATan2(mEntry[2], mEntry[0]);
yAngle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// x_angle = +pi/2
// y_angle + z_angle = atan2(r02,r00)
// WARNING. The solution is not unique. Choosing y_angle = 0.
xAngle = Math<Real>::HALF_PI;
zAngle = Math<Real>::ATan2(mEntry[2], mEntry[0]);
yAngle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerZYX (
Real& zAngle, Real& yAngle, Real& xAngle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cy*cz cz*sx*sy-cx*sz cx*cz*sy+sx*sz |
// | r10 r11 r12 | = | cy*sz cx*cz+sx*sy*sz -cz*sx+cx*sy*sz |
// | r20 r21 r22 | | -sy cy*sx cx*cy |
// +- -+ +- -+
if (mEntry[6] < (Real)1)
{
if (mEntry[6] > -(Real)1)
{
// y_angle = asin(-r20)
// z_angle = atan2(r10,r00)
// x_angle = atan2(r21,r22)
yAngle = (Real)asin(-(double)mEntry[6]);
zAngle = Math<Real>::ATan2(mEntry[3], mEntry[0]);
xAngle = Math<Real>::ATan2(mEntry[7], mEntry[8]);
return EA_UNIQUE;
}
else
{
// y_angle = +pi/2
// x_angle - z_angle = atan2(r01,r02)
// WARNING. The solution is not unique. Choosing x_angle = 0.
yAngle = Math<Real>::HALF_PI;
zAngle = -Math<Real>::ATan2(mEntry[1], mEntry[2]);
xAngle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// y_angle = -pi/2
// x_angle + z_angle = atan2(-r01,-r02)
// WARNING. The solution is not unique. Choosing x_angle = 0;
yAngle = -Math<Real>::HALF_PI;
zAngle = Math<Real>::ATan2(-mEntry[1], -mEntry[2]);
xAngle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerXYX (
Real& x0Angle, Real& yAngle, Real& x1Angle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cy sy*sx1 sy*cx1 |
// | r10 r11 r12 | = | sy*sx0 cx0*cx1-cy*sx0*sx1 -cy*cx1*sx0-cx0*sx1 |
// | r20 r21 r22 | | -sy*cx0 cx1*sx0+cy*cx0*sx1 cy*cx0*cx1-sx0*sx1 |
// +- -+ +- -+
if (mEntry[0] < (Real)1)
{
if (mEntry[0] > -(Real)1)
{
// y_angle = acos(r00)
// x0_angle = atan2(r10,-r20)
// x1_angle = atan2(r01,r02)
yAngle = (Real)acos((double)mEntry[0]);
x0Angle = Math<Real>::ATan2(mEntry[3], -mEntry[6]);
x1Angle = Math<Real>::ATan2(mEntry[1], mEntry[2]);
return EA_UNIQUE;
}
else
{
// Not a unique solution: x1_angle - x0_angle = atan2(-r12,r11)
yAngle = Math<Real>::PI;
x0Angle = -Math<Real>::ATan2(-mEntry[5], mEntry[4]);
x1Angle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// Not a unique solution: x1_angle + x0_angle = atan2(-r12,r11)
yAngle = (Real)0;
x0Angle = Math<Real>::ATan2(-mEntry[5], mEntry[4]);
x1Angle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerXZX (
Real& x0Angle, Real& zAngle, Real& x1Angle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cz -sz*cx1 sz*sx1 |
// | r10 r11 r12 | = | sz*cx0 cz*cx0*cx1-sx0*sx1 -cx1*sx0-cz*cx0*sx1 |
// | r20 r21 r22 | | sz*sx0 cz*cx1*sx0+cx0*sx1 cx0*cx1-cz*sx0*sx1 |
// +- -+ +- -+
if (mEntry[0] < (Real)1)
{
if (mEntry[0] > -(Real)1)
{
// z_angle = acos(r00)
// x0_angle = atan2(r20,r10)
// x1_angle = atan2(r02,-r01)
zAngle = (Real)acos((double)mEntry[0]);
x0Angle = Math<Real>::ATan2(mEntry[6], mEntry[3]);
x1Angle = Math<Real>::ATan2(mEntry[2], -mEntry[1]);
return EA_UNIQUE;
}
else
{
// Not a unique solution: x1_angle - x0_angle = atan2(r21,r22)
zAngle = Math<Real>::PI;
x0Angle = -Math<Real>::ATan2(mEntry[7], mEntry[8]);
x1Angle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// Not a unique solution: x1_angle + x0_angle = atan2(r21,r22)
zAngle = (Real)0;
x0Angle = Math<Real>::ATan2(mEntry[7], mEntry[8]);
x1Angle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerYXY (
Real& y0Angle, Real& xAngle, Real& y1Angle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cy0*cy1-cx*sy0*sy1 sx*sy0 cx*cy1*sy0+cy0*sy1 |
// | r10 r11 r12 | = | sx*sy1 cx -sx*cy1 |
// | r20 r21 r22 | | -cy1*sy0-cx*cy0*sy1 sx*cy0 cx*cy0*cy1-sy0*sy1 |
// +- -+ +- -+
if (mEntry[4] < (Real)1)
{
if (mEntry[4] > -(Real)1)
{
// x_angle = acos(r11)
// y0_angle = atan2(r01,r21)
// y1_angle = atan2(r10,-r12)
xAngle = (Real)acos((double)mEntry[4]);
y0Angle = Math<Real>::ATan2(mEntry[1], mEntry[7]);
y1Angle = Math<Real>::ATan2(mEntry[3], -mEntry[5]);
return EA_UNIQUE;
}
else
{
// Not a unique solution: y1_angle - y0_angle = atan2(r02,r00)
xAngle = Math<Real>::PI;
y0Angle = -Math<Real>::ATan2(mEntry[2], mEntry[0]);
y1Angle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// Not a unique solution: y1_angle + y0_angle = atan2(r02,r00)
xAngle = (Real)0;
y0Angle = Math<Real>::ATan2(mEntry[2], mEntry[0]);
y1Angle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerYZY (
Real& y0Angle, Real& zAngle, Real& y1Angle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cz*cy0*cy1-sy0*sy1 -sz*cy0 cy1*sy0+cz*cy0*sy1 |
// | r10 r11 r12 | = | sz*cy1 cz sz*sy1 |
// | r20 r21 r22 | | -cz*cy1*sy0-cy0*sy1 sz*sy0 cy0*cy1-cz*sy0*sy1 |
// +- -+ +- -+
if (mEntry[4] < (Real)1)
{
if (mEntry[4] > -(Real)1)
{
// z_angle = acos(r11)
// y0_angle = atan2(r21,-r01)
// y1_angle = atan2(r12,r10)
zAngle = (Real)acos((double)mEntry[4]);
y0Angle = Math<Real>::ATan2(mEntry[7], -mEntry[1]);
y1Angle = Math<Real>::ATan2(mEntry[5], mEntry[3]);
return EA_UNIQUE;
}
else
{
// Not a unique solution: y1_angle - y0_angle = atan2(-r20,r22)
zAngle = Math<Real>::PI;
y0Angle = -Math<Real>::ATan2(-mEntry[6], mEntry[8]);
y1Angle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// Not a unique solution: y1_angle + y0_angle = atan2(-r20,r22)
zAngle = (Real)0;
y0Angle = Math<Real>::ATan2(-mEntry[6], mEntry[8]);
y1Angle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerZXZ (
Real& z0Angle, Real& xAngle, Real& z1Angle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cz0*cz1-cx*sz0*sz1 -cx*cz1*sz0-cz0*sz1 sx*sz0 |
// | r10 r11 r12 | = | cz1*sz0+cx*cz0*sz1 cx*cz0*cz1-sz0*sz1 -sz*cz0 |
// | r20 r21 r22 | | sx*sz1 sx*cz1 cx |
// +- -+ +- -+
if (mEntry[8] < (Real)1)
{
if (mEntry[8] > -(Real)1)
{
// x_angle = acos(r22)
// z0_angle = atan2(r02,-r12)
// z1_angle = atan2(r20,r21)
xAngle = (Real)acos((double)mEntry[8]);
z0Angle = Math<Real>::ATan2(mEntry[2], -mEntry[5]);
z1Angle = Math<Real>::ATan2(mEntry[6], mEntry[7]);
return EA_UNIQUE;
}
else
{
// Not a unique solution: z1_angle - z0_angle = atan2(-r01,r00)
xAngle = Math<Real>::PI;
z0Angle = -Math<Real>::ATan2(-mEntry[1], mEntry[0]);
z1Angle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else
{
// Not a unique solution: z1_angle + z0_angle = atan2(-r01,r00)
xAngle = (Real)0;
z0Angle = Math<Real>::ATan2(-mEntry[1], mEntry[0]);
z1Angle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
typename Matrix3<Real>::EulerResult Matrix3<Real>::ExtractEulerZYZ (
Real& z0Angle, Real& yAngle, Real& z1Angle) const
{
// +- -+ +- -+
// | r00 r01 r02 | | cy*cz0*cz1-sz0*sz1 -cz1*sz0-cy*cz0*sz1 sy*cz0 |
// | r10 r11 r12 | = | cy*cz1*sz0+cz0*sz1 cz0*cz1-cy*sz0*sz1 sy*sz0 |
// | r20 r21 r22 | | -sy*cz1 sy*sz1 cy |
// +- -+ +- -+
if (mEntry[8] < (Real)1)
{
if (mEntry[8] > -(Real)1)
{
// y_angle = acos(r22)
// z0_angle = atan2(r12,r02)
// z1_angle = atan2(r21,-r20)
yAngle = (Real)acos((double)mEntry[8]);
z0Angle = Math<Real>::ATan2(mEntry[5], mEntry[2]);
z1Angle = Math<Real>::ATan2(mEntry[7], -mEntry[6]);
return EA_UNIQUE;
}
else // r22 = -1
{
// Not a unique solution: z1_angle - z0_angle = atan2(r10,r11)
yAngle = Math<Real>::PI;
z0Angle = -Math<Real>::ATan2(mEntry[3], mEntry[4]);
z1Angle = (Real)0;
return EA_NOT_UNIQUE_DIF;
}
}
else // r22 = +1
{
// Not a unique solution: z1_angle + z0_angle = atan2(r10,r11)
yAngle = (Real)0;
z0Angle = Math<Real>::ATan2(mEntry[3], mEntry[4]);
z1Angle = (Real)0;
return EA_NOT_UNIQUE_SUM;
}
}
//----------------------------------------------------------------------------
template <typename Real>
Matrix3<Real>& Matrix3<Real>::Slerp (Real t, const Matrix3& rot0,
const Matrix3& rot1)
{
Vector3<Real> axis;
Real angle;
Matrix3 prod = rot0.TransposeTimes(rot1);
prod.ExtractAxisAngle(axis, angle);
MakeRotation(axis, t*angle);
*this = rot0*(*this);
return *this;
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::SingularValueDecomposition (Matrix3& left,
Matrix3& diag, Matrix3& rightTranspose) const
{
// TODO. Replace by a call to EigenDecomposition and a QR factorization
// that is specialized for 3x3. The QDUDecomposition appears to assume
// the input matrix is invertible, but a general QR factorization has to
// deal with non-full rank.
GMatrix<Real> M(3, 3);
memcpy(M.GetElements(), mEntry, 9*sizeof(Real));
GMatrix<Real> tmpL(3, 3), tmpD(3, 3), tmpRTranspose(3, 3);
Wm5::SingularValueDecomposition<Real>(M, tmpL, tmpD, tmpRTranspose);
memcpy(left.mEntry, tmpL.GetElements(), 9*sizeof(Real));
memcpy(diag.mEntry, tmpD.GetElements(), 9*sizeof(Real));
memcpy(rightTranspose.mEntry, tmpRTranspose.GetElements(), 9*sizeof(Real));
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::PolarDecomposition (Matrix3& qMat, Matrix3& sMat)
{
// Decompose M = L*D*R^T.
Matrix3 left, diag, rightTranspose;
SingularValueDecomposition(left, diag, rightTranspose);
// Compute Q = L*R^T.
qMat = left*rightTranspose;
// Compute S = R*D*R^T.
sMat = rightTranspose.TransposeTimes(diag*rightTranspose);
// Numerical round-off errors can cause S not to be symmetric in the
// sense that S[i][j] and S[j][i] are slightly different for i != j.
// Correct this by averaging S = (S + S^T)/2.
sMat[0][1] = ((Real)0.5)*(sMat[0][1] + sMat[1][0]);
sMat[1][0] = sMat[0][1];
sMat[0][2] = ((Real)0.5)*(sMat[0][2] + sMat[2][0]);
sMat[2][0] = sMat[0][2];
sMat[1][2] = ((Real)0.5)*(sMat[1][2] + sMat[2][1]);
sMat[2][1] = sMat[1][2];
}
//----------------------------------------------------------------------------
template <typename Real>
void Matrix3<Real>::QDUDecomposition (Matrix3& qMat, Matrix3& diag,
Matrix3& uMat) const
{
// Factor M = QR = QDU where Q is orthogonal (rotation), D is diagonal
// (scaling), and U is upper triangular with ones on its diagonal
// (shear). Algorithm uses Gram-Schmidt orthogonalization (the QR
// algorithm).
//
// If M = [ m0 | m1 | m2 ] and Q = [ q0 | q1 | q2 ], then
//
// q0 = m0/|m0|
// q1 = (m1-(q0*m1)q0)/|m1-(q0*m1)q0|
// q2 = (m2-(q0*m2)q0-(q1*m2)q1)/|m2-(q0*m2)q0-(q1*m2)q1|
//
// where |V| indicates length of vector V and A*B indicates dot
// product of vectors A and B. The matrix R has entries
//
// r00 = q0*m0 r01 = q0*m1 r02 = q0*m2
// r10 = 0 r11 = q1*m1 r12 = q1*m2
// r20 = 0 r21 = 0 r22 = q2*m2
//
// so D = diag(r00,r11,r22) and U has entries u01 = r01/r00,
// u02 = r02/r00, and u12 = r12/r11.
// Build orthogonal matrix Q.
Real invLength = Math<Real>::InvSqrt(mEntry[0]*mEntry[0] +
mEntry[3]*mEntry[3] + mEntry[6]*mEntry[6]);
qMat[0][0] = mEntry[0]*invLength;
qMat[1][0] = mEntry[3]*invLength;
qMat[2][0] = mEntry[6]*invLength;
Real fDot = qMat[0][0]*mEntry[1] + qMat[1][0]*mEntry[4] +
qMat[2][0]*mEntry[7];
qMat[0][1] = mEntry[1]-fDot*qMat[0][0];
qMat[1][1] = mEntry[4]-fDot*qMat[1][0];
qMat[2][1] = mEntry[7]-fDot*qMat[2][0];
invLength = Math<Real>::InvSqrt(qMat[0][1]*qMat[0][1] +
qMat[1][1]*qMat[1][1] + qMat[2][1]*qMat[2][1]);
qMat[0][1] *= invLength;
qMat[1][1] *= invLength;
qMat[2][1] *= invLength;
fDot = qMat[0][0]*mEntry[2] + qMat[1][0]*mEntry[5] +
qMat[2][0]*mEntry[8];
qMat[0][2] = mEntry[2]-fDot*qMat[0][0];
qMat[1][2] = mEntry[5]-fDot*qMat[1][0];
qMat[2][2] = mEntry[8]-fDot*qMat[2][0];
fDot = qMat[0][1]*mEntry[2] + qMat[1][1]*mEntry[5] +
qMat[2][1]*mEntry[8];
qMat[0][2] -= fDot*qMat[0][1];
qMat[1][2] -= fDot*qMat[1][1];
qMat[2][2] -= fDot*qMat[2][1];
invLength = Math<Real>::InvSqrt(qMat[0][2]*qMat[0][2] +
qMat[1][2]*qMat[1][2] + qMat[2][2]*qMat[2][2]);
qMat[0][2] *= invLength;
qMat[1][2] *= invLength;
qMat[2][2] *= invLength;
// Guarantee that orthogonal matrix has determinant 1 (no reflections).
Real det =
qMat[0][0]*qMat[1][1]*qMat[2][2] + qMat[0][1]*qMat[1][2]*qMat[2][0] +
qMat[0][2]*qMat[1][0]*qMat[2][1] - qMat[0][2]*qMat[1][1]*qMat[2][0] -
qMat[0][1]*qMat[1][0]*qMat[2][2] - qMat[0][0]*qMat[1][2]*qMat[2][1];
if (det < (Real)0)
{
for (int row = 0; row < 3; ++row)
{
for (int col = 0; col < 3; ++col)
{
qMat[row][col] = -qMat[row][col];
}
}
}
// Build "right" matrix R.
Matrix3 right;
right[0][0] = qMat[0][0]*mEntry[0] + qMat[1][0]*mEntry[3] +
qMat[2][0]*mEntry[6];
right[0][1] = qMat[0][0]*mEntry[1] + qMat[1][0]*mEntry[4] +
qMat[2][0]*mEntry[7];
right[1][1] = qMat[0][1]*mEntry[1] + qMat[1][1]*mEntry[4] +
qMat[2][1]*mEntry[7];
right[0][2] = qMat[0][0]*mEntry[2] + qMat[1][0]*mEntry[5] +
qMat[2][0]*mEntry[8];
right[1][2] = qMat[0][1]*mEntry[2] + qMat[1][1]*mEntry[5] +
qMat[2][1]*mEntry[8];
right[2][2] = qMat[0][2]*mEntry[2] + qMat[1][2]*mEntry[5] +
qMat[2][2]*mEntry[8];
// The scaling component.
diag.MakeDiagonal(right[0][0], right[1][1], right[2][2]);
// the shear component
Real invD00 = ((Real)1)/diag[0][0];
uMat[0][0] = (Real)1;
uMat[0][1] = right[0][1]*invD00;
uMat[0][2] = right[0][2]*invD00;
uMat[1][0] = (Real)0;
uMat[1][1] = (Real)1;
uMat[1][2] = right[1][2]/diag[1][1];
uMat[2][0] = (Real)0;
uMat[2][1] = (Real)0;
uMat[2][2] = (Real)1;
}
//----------------------------------------------------------------------------
template <typename Real>
bool Matrix3<Real>::Tridiagonalize (Real diagonal[3], Real subdiagonal[2])
{
// Householder reduction T = Q^t M Q
// Input:
// mat, symmetric 3x3 matrix M
// Output:
// mat, orthogonal matrix Q (a reflection)
// diag, diagonal entries of T
// subd, subdiagonal entries of T (T is symmetric)
Real m00 = mEntry[0];
Real m01 = mEntry[1];
Real m02 = mEntry[2];
Real m11 = mEntry[4];
Real m12 = mEntry[5];
Real m22 = mEntry[8];
diagonal[0] = m00;
if (Math<Real>::FAbs(m02) >= Math<Real>::ZERO_TOLERANCE)
{
subdiagonal[0] = Math<Real>::Sqrt(m01*m01 + m02*m02);
Real invLength = ((Real)1)/subdiagonal[0];
m01 *= invLength;
m02 *= invLength;
Real tmp = ((Real)2)*m01*m12 + m02*(m22 - m11);
diagonal[1] = m11 + m02*tmp;
diagonal[2] = m22 - m02*tmp;
subdiagonal[1] = m12 - m01*tmp;
mEntry[0] = (Real)1;
mEntry[1] = (Real)0;
mEntry[2] = (Real)0;
mEntry[3] = (Real)0;
mEntry[4] = m01;
mEntry[5] = m02;
mEntry[6] = (Real)0;
mEntry[7] = m02;
mEntry[8] = -m01;
return true;
}
else
{
diagonal[1] = m11;
diagonal[2] = m22;
subdiagonal[0] = m01;
subdiagonal[1] = m12;
mEntry[0] = (Real)1;
mEntry[1] = (Real)0;
mEntry[2] = (Real)0;
mEntry[3] = (Real)0;
mEntry[4] = (Real)1;
mEntry[5] = (Real)0;
mEntry[6] = (Real)0;
mEntry[7] = (Real)0;
mEntry[8] = (Real)1;
return false;
}
}
//----------------------------------------------------------------------------
template <typename Real>
bool Matrix3<Real>::QLAlgorithm (Real diagonal[3], Real subdiagonal[2])
{
// This is an implementation of the symmetric QR algorithm from the book
// "Matrix Computations" by Gene H. Golub and Charles F. Van Loan,
// second edition. The algorithm is 8.2.3. The implementation has a
// slight variation to actually make it a QL algorithm, and it traps the
// case when either of the subdiagonal terms s0 or s1 is zero and reduces
// the 2-by-2 subblock directly.
const int imax = 32;
for (int i = 0; i < imax; ++i)
{
Real sum, diff, discr, eigVal0, eigVal1, cs, sn, tmp;
int row;
sum = Math<Real>::FAbs(diagonal[0]) + Math<Real>::FAbs(diagonal[1]);
if (Math<Real>::FAbs(subdiagonal[0]) + sum == sum)
{
// The matrix is effectively
// +- -+
// M = | d0 0 0 |
// | 0 d1 s1 |
// | 0 s1 d2 |
// +- -+
// Test whether M is diagonal (within numerical round-off).
sum = Math<Real>::FAbs(diagonal[1]) +
Math<Real>::FAbs(diagonal[2]);
if (Math<Real>::FAbs(subdiagonal[1]) + sum == sum)
{
return true;
}
// Compute the eigenvalues as roots of a quadratic equation.
sum = diagonal[1] + diagonal[2];
diff = diagonal[1] - diagonal[2];
discr = Math<Real>::Sqrt(diff*diff +
((Real)4)*subdiagonal[1]*subdiagonal[1]);
eigVal0 = ((Real)0.5)*(sum - discr);
eigVal1 = ((Real)0.5)*(sum + discr);
// Compute the Givens rotation.
if (diff >= (Real)0)
{
cs = subdiagonal[1];
sn = diagonal[1] - eigVal0;
}
else
{
cs = diagonal[2] - eigVal0;
sn = subdiagonal[1];
}
tmp = Math<Real>::InvSqrt(cs*cs + sn*sn);
cs *= tmp;
sn *= tmp;
// Postmultiply the current orthogonal matrix with the Givens
// rotation.
for (row = 0; row < 3; ++row)
{
tmp = mEntry[2+3*row];
mEntry[2+3*row] = sn*mEntry[1+3*row] + cs*tmp;
mEntry[1+3*row] = cs*mEntry[1+3*row] - sn*tmp;
}
// Update the tridiagonal matrix.
diagonal[1] = eigVal0;
diagonal[2] = eigVal1;
subdiagonal[0] = (Real)0;
subdiagonal[1] = (Real)0;
return true;
}
sum = Math<Real>::FAbs(diagonal[1]) + Math<Real>::FAbs(diagonal[2]);
if (Math<Real>::FAbs(subdiagonal[1]) + sum == sum)
{
// The matrix is effectively
// +- -+
// M = | d0 s0 0 |
// | s0 d1 0 |
// | 0 0 d2 |
// +- -+
// Test whether M is diagonal (within numerical round-off).
sum = Math<Real>::FAbs(diagonal[0]) +
Math<Real>::FAbs(diagonal[1]);
if (Math<Real>::FAbs(subdiagonal[0]) + sum == sum)
{
return true;
}
// Compute the eigenvalues as roots of a quadratic equation.
sum = diagonal[0] + diagonal[1];
diff = diagonal[0] - diagonal[1];
discr = Math<Real>::Sqrt(diff*diff +
((Real)4.0)*subdiagonal[0]*subdiagonal[0]);
eigVal0 = ((Real)0.5)*(sum - discr);
eigVal1 = ((Real)0.5)*(sum + discr);
// Compute the Givens rotation.
if (diff >= (Real)0)
{
cs = subdiagonal[0];
sn = diagonal[0] - eigVal0;
}
else
{
cs = diagonal[1] - eigVal0;
sn = subdiagonal[0];
}
tmp = Math<Real>::InvSqrt(cs*cs + sn*sn);
cs *= tmp;
sn *= tmp;
// Postmultiply the current orthogonal matrix with the Givens
// rotation.
for (row = 0; row < 3; ++row)
{
tmp = mEntry[1+3*row];
mEntry[1+3*row] = sn*mEntry[0+3*row] + cs*tmp;
mEntry[0+3*row] = cs*mEntry[0+3*row] - sn*tmp;
}
// Update the tridiagonal matrix.
diagonal[0] = eigVal0;
diagonal[1] = eigVal1;
subdiagonal[0] = (Real)0;
subdiagonal[1] = (Real)0;
return true;
}
// The matrix is
// +- -+
// M = | d0 s0 0 |
// | s0 d1 s1 |
// | 0 s1 d2 |
// +- -+
// Set up the parameters for the first pass of the QL step. The
// value for A is the difference between diagonal term D[2] and the
// implicit shift suggested by Wilkinson.
Real ratio = (diagonal[1] - diagonal[0])/(((Real)2)*subdiagonal[0]);
Real root = Math<Real>::Sqrt((Real)1 + ratio*ratio);
Real b = subdiagonal[1];
Real a = diagonal[2] - diagonal[0];
if (ratio >= (Real)0)
{
a += subdiagonal[0]/(ratio + root);
}
else
{
a += subdiagonal[0]/(ratio - root);
}
// Compute the Givens rotation for the first pass.
if (Math<Real>::FAbs(b) >= Math<Real>::FAbs(a))
{
ratio = a/b;
sn = Math<Real>::InvSqrt((Real)1 + ratio*ratio);
cs = ratio*sn;
}
else
{
ratio = b/a;
cs = Math<Real>::InvSqrt((Real)1 + ratio*ratio);
sn = ratio*cs;
}
// Postmultiply the current orthogonal matrix with the Givens
// rotation.
for (row = 0; row < 3; ++row)
{
tmp = mEntry[2+3*row];
mEntry[2+3*row] = sn*mEntry[1+3*row] + cs*tmp;
mEntry[1+3*row] = cs*mEntry[1+3*row] - sn*tmp;
}
// Set up the parameters for the second pass of the QL step. The
// values tmp0 and tmp1 are required to fully update the tridiagonal
// matrix at the end of the second pass.
Real tmp0 = (diagonal[1] - diagonal[2])*sn +
((Real)2)*subdiagonal[1]*cs;
Real tmp1 = cs*subdiagonal[0];
b = sn*subdiagonal[0];
a = cs*tmp0 - subdiagonal[1];
tmp0 *= sn;
// Compute the Givens rotation for the second pass. The subdiagonal
// term S[1] in the tridiagonal matrix is updated at this time.
if (Math<Real>::FAbs(b) >= Math<Real>::FAbs(a))
{
ratio = a/b;
root = Math<Real>::Sqrt((Real)1 + ratio*ratio);
subdiagonal[1] = b*root;
sn = ((Real)1)/root;
cs = ratio*sn;
}
else
{
ratio = b/a;
root = Math<Real>::Sqrt((Real)1 + ratio*ratio);
subdiagonal[1] = a*root;
cs = ((Real)1)/root;
sn = ratio*cs;
}
// Postmultiply the current orthogonal matrix with the Givens
// rotation.
for (row = 0; row < 3; ++row)
{
tmp = mEntry[1+3*row];
mEntry[1+3*row] = sn*mEntry[0+3*row] + cs*tmp;
mEntry[0+3*row] = cs*mEntry[0+3*row] - sn*tmp;
}
// Complete the update of the tridiagonal matrix.
Real tmp2 = diagonal[1] - tmp0;
diagonal[2] += tmp0;
tmp0 = (diagonal[0] - tmp2)*sn + ((Real)2)*tmp1*cs;
subdiagonal[0] = cs*tmp0 - tmp1;
tmp0 *= sn;
diagonal[1] = tmp2 + tmp0;
diagonal[0] -= tmp0;
}
return false;
}
//----------------------------------------------------------------------------
template <typename Real>
inline Matrix3<Real> operator* (Real scalar, const Matrix3<Real>& mat)
{
return mat*scalar;
}
//----------------------------------------------------------------------------
template <typename Real>
inline Vector3<Real> operator* (const Vector3<Real>& vec,
const Matrix3<Real>& mat)
{
return Vector3<Real>
(
vec[0]*mat[0][0] + vec[1]*mat[1][0] + vec[2]*mat[2][0],
vec[0]*mat[0][1] + vec[1]*mat[1][1] + vec[2]*mat[2][1],
vec[0]*mat[0][2] + vec[1]*mat[1][2] + vec[2]*mat[2][2]
);
}
//----------------------------------------------------------------------------
|