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
//! Square matrices.
use std::ops::*;
use crate::*;
macro_rules! mat2s {
($($n:ident => $m3t:ident, $v3t:ident, $vt:ident, $t:ident),+) => {
$(/// A 2x2 square matrix.
///
/// Useful for performing linear transformations (rotation, scaling) on 2d vectors.
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct $n {
pub cols: [$vt; 2],
}
derive_default_identity!($n);
impl $n {
#[inline]
pub const fn new(col1: $vt, col2: $vt) -> Self {
$n {
cols: [col1, col2],
}
}
#[inline]
pub fn identity() -> Self {
Self::new(
$vt::new($t::splat(1.0), $t::splat(0.0)),
$vt::new($t::splat(0.0), $t::splat(1.0)),
)
}
/// Turn this into a homogeneous 2d transformation matrix.
#[inline]
pub fn into_homogeneous(self) -> $m3t {
$m3t::new(
self.cols[0].into(),
self.cols[1].into(),
$v3t::new($t::splat(0.0), $t::splat(0.0), $t::splat(1.0))
)
}
#[inline]
pub fn transpose(&mut self) {
*self = self.transposed();
}
#[inline]
pub fn transposed(&self) -> Self {
let (x0, y0) = self.cols[0].into();
let (x1, y1) = self.cols[1].into();
Self::new(
$vt::new(x0, x1),
$vt::new(y0, y1),
)
}
#[inline]
pub fn determinant(&self) -> $t {
(self.cols[0].x * self.cols[1].y) - (self.cols[1].x * self.cols[0].y)
}
/// The adjugate of this matrix, i.e. the transpose of
/// the cofactor matrix.
///
/// This is equivalent to the inverse
/// but without dividing by the determinant of the matrix,
/// which can be useful in some contexts for better performance.
///
/// One such case is when this matrix is interpreted as a
/// a homogeneous transformation matrix, in which case uniform scaling will
/// not affect the resulting projected 3d version of transformed points or
/// vectors.
#[inline]
pub fn adjugate(&self) -> Self {
Self::new(
$vt::new(self.cols[1].y, -self.cols[0].y),
$vt::new(-self.cols[1].x, self.cols[0].x),
)
}
/// If this matrix is not currently invertable, this function will return
/// an invalid inverse. This status is not checked by the library.
#[inline]
pub fn inverse(&mut self) {
let n = self.inversed();
*self = n;
}
/// If this matrix is not currently invertable, this function will return
/// an invalid inverse. This status is not checked by the library.
#[inline]
pub fn inversed(&self) -> Self {
let det = self.determinant();
let inv_det = $t::splat(1.0) / det;
inv_det * self.adjugate()
}
/// Get the [`core::alloc::Layout`] of `Self`
#[inline]
pub fn layout() -> alloc::alloc::Layout {
alloc::alloc::Layout::from_size_align(std::mem::size_of::<Self>(), std::mem::align_of::<$vt>()).unwrap()
}
/// Interpret `self` as a statically-sized array of its base numeric type
#[inline]
pub fn as_array(&self) -> &[$t; 4] {
let ptr = self as *const $n as *const [$t; 4];
unsafe { &*ptr }
}
/// Interpret `self` as a statically-sized array of its base numeric type
#[inline]
pub fn as_mut_array(&mut self) -> &mut [$t; 4] {
let ptr = self as *mut $n as *mut [$t; 4];
unsafe { &mut *ptr }
}
/// Interpret `self` as a statically-sized array of its component (column) vector type
#[inline]
pub fn as_component_array(&self) -> &[$vt; 2] {
let ptr = self as *const $n as *const [$vt; 2];
unsafe { &*ptr }
}
/// Interpret `self` as a statically-sized array of its component (column) vector type
#[inline]
pub fn as_mut_component_array(&mut self) -> &mut [$vt; 2] {
let ptr = self as *mut $n as *mut [$vt; 2];
unsafe { &mut *ptr }
}
/// Interpret `self` as a slice of its base numeric type
#[inline]
pub fn as_slice(&self) -> &[$t] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts(self as *const $n as *const $t, 4)
}
}
/// Interpret `self` as a slice of its base numeric type
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [$t] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts_mut(self as *mut $n as *mut $t, 4)
}
}
/// Interpret `self` as a slice of its component (column) vector type
#[inline]
pub fn as_component_slice(&self) -> &[$vt] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts(self as *const $n as *const $vt, 2)
}
}
/// Interpret `self` as a slice of its component (column) vector type
#[inline]
pub fn as_mut_component_slice(&mut self) -> &mut [$vt] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts_mut(self as *mut $n as *mut $vt, 2)
}
}
/// Interpret `self` as a slice of bytes
#[inline]
pub fn as_byte_slice(&self) -> &[u8] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts(self as *const $n as *const u8, 4 * std::mem::size_of::<$t>())
}
}
/// Interpret `self` as a slice of bytes
#[inline]
pub fn as_mut_byte_slice(&mut self) -> &mut [u8] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts_mut(self as *mut $n as *mut u8, 4 * std::mem::size_of::<$t>())
}
}
/// Returns a constant unsafe pointer to the underlying data in the underlying type.
/// This function is safe because all types here are repr(C) and can be represented
/// as their underlying type.
///
/// # Safety
///
/// It is up to the caller to correctly use this pointer and its bounds.
#[inline]
pub const fn as_ptr(&self) -> *const $t {
self as *const $n as *const $t
}
/// Returns a mutable unsafe pointer to the underlying data in the underlying type.
/// This function is safe because all types here are repr(C) and can be represented
/// as their underlying type.
///
/// # Safety
///
/// It is up to the caller to correctly use this pointer and its bounds.
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut $t {
self as *mut $n as *mut $t
}
}
impl Mul for $n {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self {
let sa = self.cols[0];
let sb = self.cols[1];
let oa = rhs.cols[0];
let ob = rhs.cols[1];
Self::new(
$vt::new(
(sa.x * oa.x) + (sb.x * oa.y),
(sa.y * oa.x) + (sb.y * oa.y),
),
$vt::new(
(sa.x * ob.x) + (sb.x * ob.y),
(sa.y * ob.x) + (sb.y * ob.y),
),
)
}
}
impl Mul<$vt> for $n {
type Output = $vt;
#[inline]
fn mul(self, rhs: $vt) -> $vt {
let a = self.cols[0];
let b = self.cols[1];
$vt::new(
(a.x * rhs.x) + (b.x * rhs.y),
(a.y * rhs.x) + (b.y * rhs.y),
)
}
}
impl Mul<$t> for $n {
type Output = $n;
#[inline]
fn mul(self, rhs: $t) -> $n {
$n::new(
self.cols[0] * rhs,
self.cols[1] * rhs,
)
}
}
impl Mul<$n> for $t {
type Output = $n;
#[inline]
fn mul(self, rhs: $n) -> $n {
$n::new(
rhs.cols[0] * self,
rhs.cols[1] * self,
)
}
}
impl Add for $n {
type Output = Self;
#[inline]
fn add(self, rhs: $n) -> Self {
$n::new(
self.cols[0] + rhs.cols[0],
self.cols[1] + rhs.cols[1],
)
}
}
impl AddAssign for $n {
#[inline]
fn add_assign(&mut self, rhs: $n) {
*self = *self + rhs;
}
}
impl From<[$t; 4]> for $n {
#[inline]
fn from(comps: [$t; 4]) -> Self {
Self::new(
$vt::new(comps[0], comps[1]),
$vt::new(comps[2], comps[3])
)
}
}
impl From<[[$t; 2]; 2]> for $n {
#[inline]
fn from(comps: [[$t; 2]; 2]) -> Self {
Self::new(
$vt::new(comps[0][0], comps[0][1]),
$vt::new(comps[1][0], comps[1][1])
)
}
}
impl From<$n> for [[$t; 2]; 2] {
#[inline]
fn from(mat2: $n) -> Self {
[
[mat2.cols[0].x, mat2.cols[0].y],
[mat2.cols[1].x, mat2.cols[1].y],
]
}
}
impl From<&[$t; 4]> for $n {
#[inline]
fn from(comps: &[$t; 4]) -> Self {
Self::from(*comps)
}
}
impl Index<usize> for $n {
type Output = $vt;
fn index(&self, index: usize) -> &Self::Output {
&self.cols[index]
}
}
impl IndexMut<usize> for $n {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.cols[index]
}
}
)+
}
}
mat2s!(
Mat2 => Mat3, Vec3, Vec2, f32,
Mat2x4 => Mat3x4, Vec3x4, Vec2x4, f32x4,
Mat2x8 => Mat3x8, Vec3x8, Vec2x8, f32x8
);
#[cfg(feature = "f64")]
mat2s!(
DMat2 => DMat3, DVec3, DVec2, f64,
DMat2x2 => DMat3x2, DVec3x2, DVec2x2, f64x2,
DMat2x4 => DMat3x4, DVec3x4, DVec2x4, f64x4
);
macro_rules! mat3s {
($($n:ident => $rt:ident, $bt:ident, $m4t:ident, $v4t:ident, $v2t:ident, $vt:ident, $t:ident),+) => {
$(/// A 3x3 square matrix.
///
/// Useful for performing linear transformations (rotation, scaling) on 3d vectors,
/// or for performing arbitrary transformations (linear + translation, projection, etc)
/// on homogeneous 2d vectors
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct $n {
pub cols: [$vt; 3],
}
derive_default_identity!($n);
impl $n {
#[inline]
pub const fn new(col1: $vt, col2: $vt, col3: $vt) -> Self {
$n {
cols: [col1, col2, col3],
}
}
/// Assumes homogeneous 2d coordinates.
#[inline]
pub fn from_translation(trans: $v2t) -> Self {
Self::new(
$vt::new($t::splat(1.0), $t::splat(0.0), $t::splat(0.0)),
$vt::new($t::splat(0.0), $t::splat(1.0), $t::splat(0.0)),
$vt::new(trans.x, trans.y, $t::splat(1.0)))
}
/// Assumes homogeneous 2d coordinates.
#[inline]
pub fn from_scale_homogeneous(scale: $t) -> Self {
let zero = $t::splat(0.0);
Self::new(
$vt::new(scale, zero, zero),
$vt::new(zero, scale, zero),
$vt::new(zero, zero, $t::splat(1.0)),
)
}
/// Assumes homogeneous 2d coordinates.
#[inline]
pub fn from_nonuniform_scale_homogeneous(scale: $v2t) -> Self {
let zero = $t::splat(0.0);
Self::new(
$vt::new(scale.x, zero, zero),
$vt::new(zero, scale.y, zero),
$vt::new(zero, zero, $t::splat(1.0)),
)
}
/// Builds a homogeneous 2d rotation matrix (in the xy plane) from a given angle in radians.
#[inline]
pub fn from_rotation_homogeneous(angle: $t) -> Self {
let (s, c) = angle.sin_cos();
let zero = $t::splat(0.0);
Self::new(
$vt::new(c, s, zero),
$vt::new(-s, c, zero),
$vt::new(zero, zero, $t::splat(1.0)),
)
}
#[inline]
pub fn from_scale(scale: $t) -> Self {
let zero = $t::splat(0.0);
Self::new(
$vt::new(scale, zero, zero),
$vt::new(zero, scale, zero),
$vt::new(zero, zero, scale),
)
}
#[inline]
pub fn from_nonuniform_scale(scale: $vt) -> Self {
let zero = $t::splat(0.0);
Self::new(
$vt::new(scale.x, zero, zero),
$vt::new(zero, scale.y, zero),
$vt::new(zero, zero, scale.z),
)
}
#[inline]
pub fn identity() -> Self {
Self::new(
$vt::new($t::splat(1.0), $t::splat(0.0), $t::splat(0.0)),
$vt::new($t::splat(0.0), $t::splat(1.0), $t::splat(0.0)),
$vt::new($t::splat(0.0), $t::splat(0.0), $t::splat(1.0)))
}
/// Angles are applied in the order roll -> pitch -> yaw.
///
/// - Yaw is rotation inside the xz plane ("around the y axis")
/// - Pitch is rotation inside the yz plane ("around the x axis")
/// - Roll is rotation inside the xy plane ("around the z axis")
#[inline]
#[allow(unused_variables)]
pub fn from_euler_angles(roll: $t, pitch: $t, yaw: $t) -> Self {
let (sin_yaw, cos_yaw) = yaw.sin_cos();
let (sin_pitch, cos_pitch) = pitch.sin_cos();
let (sin_roll, cos_roll) = roll.sin_cos();
let sin_pitch_sin_roll = sin_pitch * sin_roll;
let sin_pitch_cos_roll = sin_pitch * cos_roll;
let m00 = cos_yaw * cos_roll + sin_pitch * sin_yaw* sin_roll;
let m10 = cos_pitch * sin_roll;
let m20 = -cos_roll * sin_yaw + cos_yaw * sin_pitch * sin_roll;
let m01 = cos_roll * sin_pitch * sin_yaw - cos_yaw * sin_roll;
let m11 = cos_pitch * cos_roll;
let m21 = cos_yaw * cos_roll * sin_pitch + sin_yaw * sin_roll;
let m02 = cos_pitch * sin_yaw;
let m12 = -sin_pitch;
let m22 = cos_pitch * cos_yaw;
// think transposed as arguments are columns
Self::new(
$vt::new(m00, m10, m20),
$vt::new(m01, m11, m21),
$vt::new(m02, m12, m22),
)
}
/// Create a new rotation matrix from a rotation "around the x axis". This is
/// here as a convenience function for users coming from other libraries; it is
/// more proper to think of this as a rotation *in the yz plane*.
#[inline]
pub fn from_rotation_x(angle: $t) -> Self {
let (sin, cos) = angle.sin_cos();
let zero = $t::splat(0.0);
let one = $t::splat(1.0);
// think transposed as arguments are columns
Self::new(
$vt::new(one, zero, zero),
$vt::new(zero, cos, sin),
$vt::new(zero, -sin, cos),
)
}
/// Create a new rotation matrix from a rotation "around the y axis". This is
/// here as a convenience function for users coming from other libraries; it is
/// more proper to think of this as a rotation *in the xz plane*.
#[inline]
pub fn from_rotation_y(angle: $t) -> Self {
let (sin, cos) = angle.sin_cos();
let zero = $t::splat(0.0);
let one = $t::splat(1.0);
// think transposed as arguments are columns
Self::new(
$vt::new(cos, zero, -sin),
$vt::new(zero, one, zero),
$vt::new(sin, zero, cos),
)
}
/// Create a new rotation matrix from a rotation "around the z axis". This is
/// here as a convenience function for users coming from other libraries; it is
/// more proper to think of this as a rotation *in the xy plane*.
#[inline]
pub fn from_rotation_z(angle: $t) -> Self {
let (sin, cos) = angle.sin_cos();
let zero = $t::splat(0.0);
let one = $t::splat(1.0);
// think transposed as arguments are columns
Self::new(
$vt::new(cos, sin, zero),
$vt::new(-sin, cos, zero),
$vt::new(zero, zero, one),
)
}
/// Create a new rotation matrix from a rotation around the given axis.
/// This is here as a convenience function for users coming from other libraries.
#[inline]
pub fn from_rotation_around(axis: $vt, angle: $t) -> Self {
let (sin, cos) = angle.sin_cos();
let mul = $t::splat(1.0) - cos;
let x_sin = axis.x * sin;
let y_sin = axis.y * sin;
let z_sin = axis.z * sin;
let xy_mul = axis.x * axis.y * mul;
let xz_mul = axis.x * axis.z * mul;
let yz_mul = axis.y * axis.z * mul;
let m00 = (axis.x * axis.x).mul_add(mul, cos);
let m10 = xy_mul + z_sin;
let m20 = xz_mul - y_sin;
let m01 = xy_mul - z_sin;
let m11 = (axis.y * axis.y).mul_add(mul, cos);
let m21 = yz_mul + x_sin;
let m02 = xz_mul + y_sin;
let m12 = yz_mul - x_sin;
let m22 = (axis.z * axis.z).mul_add(mul, cos);
// think transposed as arguments are columns
Self::new(
$vt::new(m00, m10, m20),
$vt::new(m01, m11, m21),
$vt::new(m02, m12, m22),
)
}
/// Construct a rotation matrix given a bivector which defines a plane and rotation orientation,
/// and a rotation angle.
///
/// `plane` must be normalized!
///
/// This is the equivalent of an axis-angle rotation.
#[inline]
pub fn from_angle_plane(angle: $t, plane: $bt) -> Self {
$rt::from_angle_plane(angle, plane).into_matrix()
}
#[inline]
pub fn into_homogeneous(self) -> $m4t {
let zero = $t::splat(0.0);
let one = $t::splat(1.0);
$m4t::new(
self.cols[0].into(),
self.cols[1].into(),
self.cols[2].into(),
$v4t::new(zero, zero, zero, one)
)
}
#[inline]
pub fn determinant(&self) -> $t {
self.cols[0].x.mul_add(
self.cols[1].y.mul_add(self.cols[2].z, -(self.cols[2].y * self.cols[1].z)),
-(self.cols[1].x.mul_add(
self.cols[0].y.mul_add(self.cols[2].z, -(self.cols[2].y * self.cols[0].z)),
-(self.cols[2].x * self.cols[0].y.mul_add(self.cols[1].z, -(self.cols[1].y * self.cols[0].z)))
))
)
}
/// The adjugate of this matrix, i.e. the transpose of
/// the cofactor matrix.
///
/// This is equivalent to the inverse
/// but without dividing by the determinant of the matrix,
/// which can be useful in some contexts for better performance.
///
/// One such case is when this matrix is interpreted as a
/// a homogeneous transformation matrix, in which case uniform scaling will
/// not affect the resulting projected 3d version of transformed points or
/// vectors.
#[inline]
pub fn adjugate(&self) -> Self {
let x = self.cols[1].cross(self.cols[2]);
let y = self.cols[2].cross(self.cols[0]);
let z = self.cols[0].cross(self.cols[1]);
Self::new(x, y, z).transposed()
}
/// If this matrix is not currently invertable, this function will return
/// an invalid inverse. This status is not checked by the library.
#[inline]
pub fn inverse(&mut self) {
*self = self.inversed();
}
/// If this matrix is not currently invertable, this function will return
/// an invalid inverse. This status is not checked by the library.
#[inline]
pub fn inversed(&self) -> Self {
let adjugate = self.adjugate();
let det = self.determinant();
let inv_det = $t::splat(1.0) / det;
inv_det * adjugate
}
#[inline]
pub fn transpose(&mut self) {
*self = self.transposed();
}
#[inline]
pub fn transposed(&self) -> Self {
let (x0, y0, z0) = self.cols[0].into();
let (x1, y1, z1) = self.cols[1].into();
let (x2, y2, z2) = self.cols[2].into();
Self::new(
$vt::new(x0, x1, x2),
$vt::new(y0, y1, y2),
$vt::new(z0, z1, z2),
)
}
/// Transform a Vec2 by self, interpreting it as a vector.
#[inline]
pub fn transform_vec2(&self, vec: $v2t) -> $v2t {
(*self * vec.into_homogeneous_vector()).truncated()
}
/// Transform a Vec2 by self, interpreting it as a point.
#[inline]
pub fn transform_point2(&self, point: $v2t) -> $v2t {
(*self * point.into_homogeneous_point()).normalized_homogeneous_point().truncated()
}
/// Get the [`core::alloc::Layout`] of `Self`
#[inline]
pub fn layout() -> alloc::alloc::Layout {
alloc::alloc::Layout::from_size_align(std::mem::size_of::<Self>(), std::mem::align_of::<$t>()).unwrap()
}
/// Interpret `self` as a statically sized array of the base numeric type.
#[inline]
pub fn as_array(&self) -> &[$t; 9] {
let ptr = self as *const $n as *const [$t; 9];
unsafe { &*ptr }
}
/// Interpret `self` as a statically sized array of the base numeric type.
#[inline]
pub fn as_mut_array(&mut self) -> &mut [$t; 9] {
let ptr = self as *mut $n as *mut [$t; 9];
unsafe { &mut *ptr }
}
/// Interpret `self` as a statically sized array of the component (column) vectors.
#[inline]
pub fn as_component_array(&self) -> &[$vt; 3] {
let ptr = self as *const $n as *const [$vt; 3];
unsafe { &*ptr }
}
/// Interpret `self` as a statically sized array of the component (column) vectors.
#[inline]
pub fn as_mut_component_array(&mut self) -> &mut [$vt; 3] {
let ptr = self as *mut $n as *mut [$vt; 3];
unsafe { &mut *ptr }
}
/// Interpret `self` as a slice of the base numeric type.
#[inline]
pub fn as_slice(&self) -> &[$t] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts(self as *const $n as *const $t, 9)
}
}
/// Interpret `self` as a slice of the component (column) vectors.
#[inline]
pub fn as_component_slice(&self) -> &[$vt] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts(self as *const $n as *const $vt, 3)
}
}
/// Interpret `self` as a slice of bytes.
#[inline]
pub fn as_byte_slice(&self) -> &[u8] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts(self as *const $n as *const u8, 9 * std::mem::size_of::<$t>())
}
}
/// Interpret `self` as a slice of the base numeric type.
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [$t] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts_mut(self as *mut $n as *mut $t, 9)
}
}
/// Interpret `self` as a slice of the component (column) vectors.
#[inline]
pub fn as_mut_component_slice(&mut self) -> &mut [$vt] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts_mut(self as *mut $n as *mut $vt, 3)
}
}
/// Interpret `self` as a slice of bytes.
#[inline]
pub fn as_mut_byte_slice(&mut self) -> &mut [u8] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts_mut(self as *mut $n as *mut u8, 9 * std::mem::size_of::<$t>())
}
}
/// Returns a constant unsafe pointer to the underlying data in the underlying type.
/// This function is safe because all types here are repr(C) and can be represented
/// as their underlying type.
///
/// # Safety
///
/// It is up to the caller to correctly use this pointer and its bounds.
#[inline]
pub const fn as_ptr(&self) -> *const $t {
self as *const $n as *const $t
}
/// Returns a mutable unsafe pointer to the underlying data in the underlying type.
/// This function is safe because all types here are repr(C) and can be represented
/// as their underlying type.
///
/// # Safety
///
/// It is up to the caller to correctly use this pointer and its bounds.
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut $t {
self as *mut $n as *mut $t
}
}
impl Mul for $n {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self {
let sa = self.cols[0];
let sb = self.cols[1];
let sc = self.cols[2];
let oa = rhs.cols[0];
let ob = rhs.cols[1];
let oc = rhs.cols[2];
Self::new(
$vt::new(
(sa.x * oa.x) + (sb.x * oa.y) + (sc.x * oa.z),
(sa.y * oa.x) + (sb.y * oa.y) + (sc.y * oa.z),
(sa.z * oa.x) + (sb.z * oa.y) + (sc.z * oa.z),
),
$vt::new(
(sa.x * ob.x) + (sb.x * ob.y) + (sc.x * ob.z),
(sa.y * ob.x) + (sb.y * ob.y) + (sc.y * ob.z),
(sa.z * ob.x) + (sb.z * ob.y) + (sc.z * ob.z),
),
$vt::new(
(sa.x * oc.x) + (sb.x * oc.y) + (sc.x * oc.z),
(sa.y * oc.x) + (sb.y * oc.y) + (sc.y * oc.z),
(sa.z * oc.x) + (sb.z * oc.y) + (sc.z * oc.z),
),
)
}
}
impl Mul<$vt> for $n {
type Output = $vt;
#[inline]
fn mul(self, rhs: $vt) -> $vt {
let a = self.cols[0];
let b = self.cols[1];
let c = self.cols[2];
$vt::new(
(a.x * rhs.x) + (b.x * rhs.y) + (c.x * rhs.z),
(a.y * rhs.x) + (b.y * rhs.y) + (c.y * rhs.z),
(a.z * rhs.x) + (b.z * rhs.y) + (c.z * rhs.z),
)
}
}
impl Mul<$t> for $n {
type Output = $n;
#[inline]
fn mul(self, rhs: $t) -> $n {
$n::new(
self.cols[0] * rhs,
self.cols[1] * rhs,
self.cols[2] * rhs,
)
}
}
impl Mul<$n> for $t {
type Output = $n;
#[inline]
fn mul(self, rhs: $n) -> $n {
$n::new(
rhs.cols[0] * self,
rhs.cols[1] * self,
rhs.cols[2] * self,
)
}
}
impl Add for $n {
type Output = Self;
#[inline]
fn add(self, rhs: $n) -> Self {
$n::new(
self.cols[0] + rhs.cols[0],
self.cols[1] + rhs.cols[1],
self.cols[2] + rhs.cols[2],
)
}
}
impl AddAssign for $n {
#[inline]
fn add_assign(&mut self, rhs: $n) {
*self = *self + rhs;
}
}
impl From<[$t; 9]> for $n {
#[inline]
fn from(comps: [$t; 9]) -> Self {
Self::new(
$vt::new(comps[0], comps[1], comps[2]),
$vt::new(comps[3], comps[4], comps[5]),
$vt::new(comps[6], comps[7], comps[8])
)
}
}
impl From<[[$t; 3]; 3]> for $n {
#[inline]
fn from(comps: [[$t; 3]; 3]) -> Self {
Self::new(
$vt::new(comps[0][0], comps[0][1], comps[0][2]),
$vt::new(comps[1][0], comps[1][1], comps[1][2]),
$vt::new(comps[2][0], comps[2][1], comps[2][2])
)
}
}
impl From<$n> for [[$t; 3]; 3] {
#[inline]
fn from(mat3: $n) -> Self {
[
[mat3.cols[0].x, mat3.cols[0].y, mat3.cols[0].z],
[mat3.cols[1].x, mat3.cols[1].y, mat3.cols[1].z],
[mat3.cols[2].x, mat3.cols[2].y, mat3.cols[2].z]
]
}
}
impl From<&[$t; 9]> for $n {
#[inline]
fn from(comps: &[$t; 9]) -> Self {
Self::from(*comps)
}
}
impl Index<usize> for $n {
type Output = $vt;
fn index(&self, index: usize) -> &Self::Output {
&self.cols[index]
}
}
impl IndexMut<usize> for $n {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.cols[index]
}
}
)+
}
}
mat3s!(
Mat3 => Rotor3, Bivec3, Mat4, Vec4, Vec2, Vec3, f32,
Mat3x4 => Rotor3x4, Bivec3x4, Mat4x4, Vec4x4, Vec2x4, Vec3x4, f32x4,
Mat3x8 => Rotor3x8, Bivec3x8, Mat4x8, Vec4x8, Vec2x8, Vec3x8, f32x8
);
#[cfg(feature = "f64")]
mat3s!(
DMat3 => DRotor3, DBivec3, DMat4, DVec4, DVec2, DVec3, f64,
DMat3x2 => DRotor3x2, DBivec3x2, DMat4x2, DVec4x2, DVec2x2, DVec3x2, f64x2,
DMat3x4 => DRotor3x4, DBivec3x4, DMat4x4, DVec4x4, DVec2x4, DVec3x4, f64x4
);
macro_rules! impl_mat3 {
($($mt:ident, $t:ident, $rt:ident, $bt:ident),+) => {
$(impl $mt {
/// If `self` is a rotation matrix, return a `Rotor3` representing the same rotation.
///
/// If `self` is not a rotation matrix, the returned value is a `Rotor3` with undefied
/// properties. The fact that `self` is a rotation matrix is not checked by the
/// library.
pub fn into_rotor3(self) -> $rt {
// Adapted from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
let w = ($t::splat(1.0) + self[0][0] + self[1][1] + self[2][2]).max($t::splat(0.0)).sqrt() * $t::splat(0.5);
let yz = {
let s = ($t::splat(1.0) + self[0][0] - self[1][1] - self[2][2]).max($t::splat(0.0)).sqrt() * $t::splat(0.5);
s.copysign(self[2][1] - self[1][2])
};
let xz = {
let s = ($t::splat(1.0) - self[0][0] + self[1][1] - self[2][2]).max($t::splat(0.0)).sqrt() * $t::splat(0.5);
s.copysign(self[2][0] - self[0][2])
};
let xy = {
let s = ($t::splat(1.0) - self[0][0] - self[1][1] + self[2][2]).max($t::splat(0.0)).sqrt() * $t::splat(0.5);
s.copysign(self[1][0] - self[0][1])
};
$rt::new(w, $bt::new(xy, xz, yz))
}
})+
}
}
impl_mat3!(Mat3, f32, Rotor3, Bivec3);
#[cfg(feature = "f64")]
impl_mat3!(DMat3, f64, DRotor3, DBivec3);
macro_rules! impl_mat3_wide {
($($mt:ident => $t:ident, $rt:ident, $bt:ident),+) => {
$(impl $mt {
/// If `self` is a rotation matrix, return a `Rotor3` representing the same rotation.
///
/// If `self` is not a rotation matrix, the returned value is a `Rotor3` with undefied
/// properties. The fact that `self` is a rotation matrix is not checked by the
/// library.
pub fn into_rotor3(self) -> $rt {
// Adapted from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
let w = ($t::splat(1.0) + self[0][0] + self[1][1] + self[2][2]).max($t::splat(0.0)).sqrt() * $t::splat(0.5);
let yz = {
let s = ($t::splat(1.0) + self[0][0] - self[1][1] - self[2][2]).max($t::splat(0.0)).sqrt() * $t::splat(0.5);
s.flip_signs(self[2][1] - self[1][2])
};
let xz = {
let s = ($t::splat(1.0) - self[0][0] + self[1][1] - self[2][2]).max($t::splat(0.0)).sqrt() * $t::splat(0.5);
s.flip_signs(self[2][0] - self[0][2])
};
let xy = {
let s = ($t::splat(1.0) - self[0][0] - self[1][1] + self[2][2]).max($t::splat(0.0)).sqrt() * $t::splat(0.5);
s.flip_signs(self[1][0] - self[0][1])
};
$rt::new(w, $bt::new(xy, xz, yz))
}
})+
}
}
impl_mat3_wide!(Mat3x4 => f32x4, Rotor3x4, Bivec3x4,
Mat3x8 => f32x8, Rotor3x8, Bivec3x8);
#[cfg(feature = "f64")]
impl_mat3_wide!(DMat3x2 => f64x2, DRotor3x2, DBivec3x2,
DMat3x4 => f64x4, DRotor3x4, DBivec3x4);
macro_rules! mat4s {
($($n:ident => $rt:ident, $bt:ident, $vt:ident, $v3t:ident, $m3t:ident, $i3t:ident, $t:ident),+) => {
$(/// A 4x4 square matrix.
///
/// Useful for performing linear transformations (rotation, scaling) on 4d vectors,
/// or for performing arbitrary transformations (linear + translation, projection, etc)
/// on homogeneous 3d vectors.
///
/// Note that most constructors assume that the matrix will be used as a homogeneous 3d
/// transformation matrix.
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
pub struct $n {
pub cols: [$vt; 4],
}
derive_default_identity!($n);
impl $n {
#[inline]
pub const fn new(col1: $vt, col2: $vt, col3: $vt, col4: $vt) -> Self {
$n {
cols: [col1, col2, col3, col4],
}
}
#[inline]
pub fn identity() -> Self {
Self::new(
$vt::new($t::splat(1.0), $t::splat(0.0), $t::splat(0.0), $t::splat(0.0)),
$vt::new($t::splat(0.0), $t::splat(1.0), $t::splat(0.0), $t::splat(0.0)),
$vt::new($t::splat(0.0), $t::splat(0.0), $t::splat(1.0), $t::splat(0.0)),
$vt::new($t::splat(0.0), $t::splat(0.0), $t::splat(0.0), $t::splat(1.0)))
}
/// Assumes homogeneous 3d coordinates.
#[inline]
pub fn from_translation(trans: $v3t) -> Self {
Self::new(
$vt::new($t::splat(1.0), $t::splat(0.0), $t::splat(0.0), $t::splat(0.0)),
$vt::new($t::splat(0.0), $t::splat(1.0), $t::splat(0.0), $t::splat(0.0)),
$vt::new($t::splat(0.0), $t::splat(0.0), $t::splat(1.0), $t::splat(0.0)),
$vt::new(trans.x, trans.y, trans.z, $t::splat(1.0)))
}
/// Assumes homogeneous 3d coordinates.
#[inline]
pub fn from_scale(scale: $t) -> Self {
let zero = $t::splat(0.0);
Self::new(
$vt::new(scale, zero, zero, zero),
$vt::new(zero, scale, zero, zero),
$vt::new(zero, zero, scale, zero),
$vt::new(zero, zero, zero, $t::splat(1.0)),
)
}
/// Assumes homogeneous 3d coordinates.
#[inline]
pub fn from_nonuniform_scale(scale: $v3t) -> Self {
let zero = $t::splat(0.0);
Self::new(
$vt::new(scale.x, zero, zero, zero),
$vt::new(zero, scale.y, zero, zero),
$vt::new(zero, zero, scale.z, zero),
$vt::new(zero, zero, zero, $t::splat(1.0)),
)
}
/// Full 4d diagonal matrix.
#[inline]
pub fn from_scale_4d(scale: $t) -> Self {
let zero = $t::splat(0.0);
Self::new(
$vt::new(scale, zero, zero, zero),
$vt::new(zero, scale, zero, zero),
$vt::new(zero, zero, scale, zero),
$vt::new(zero, zero, zero, scale),
)
}
/// Full 4d nonuniform scaling matrix.
#[inline]
pub fn from_nonuniform_scale_4d(scale: $vt) -> Self {
let zero = $t::splat(0.0);
Self::new(
$vt::new(scale.x, zero, zero, zero),
$vt::new(zero, scale.y, zero, zero),
$vt::new(zero, zero, scale.z, zero),
$vt::new(zero, zero, zero, scale.w),
)
}
/// Angles are applied in the order roll -> pitch -> yaw
///
/// - Roll is rotation inside the xy plane ("around the z axis")
/// - Pitch is rotation inside the yz plane ("around the x axis")
/// - Yaw is rotation inside the xz plane ("around the y axis")
///
/// Assumes homogeneous 3d coordinates.
///
/// **Important: This function assumes a right-handed, y-up coordinate space** where:
/// * +X axis points *right*
/// * +Y axis points *up*
/// * +Z axis points *towards the viewer* (i.e. out of the screen)
///
/// This means that you may see unexpected behavior when used with OpenGL or DirectX
/// as they use a different coordinate system. You should use the appropriate
/// projection matrix in ```projection``` module to fit your use case to remedy this.
#[inline]
pub fn from_euler_angles(roll: $t, pitch: $t, yaw: $t) -> Self {
let (sin_yaw, cos_yaw) = yaw.sin_cos();
let (sin_pitch, cos_pitch) = pitch.sin_cos();
let (sin_roll, cos_roll) = roll.sin_cos();
let zero = $t::splat(0.0);
let m00 = cos_yaw * cos_roll + sin_pitch * sin_yaw * sin_roll;
let m10 = cos_pitch * sin_roll;
let m20 = -cos_roll * sin_yaw + cos_yaw * sin_pitch * sin_roll;
let m01 = cos_roll * sin_pitch * sin_yaw - cos_yaw * sin_roll;
let m11 = cos_pitch * cos_roll;
let m21 = cos_yaw * cos_roll * sin_pitch + sin_yaw * sin_roll;
let m02 = cos_pitch * sin_yaw;
let m12 = -sin_pitch;
let m22 = cos_pitch * cos_yaw;
// think transposed as arguments are columns
Self::new(
$vt::new(m00, m10, m20, zero),
$vt::new(m01, m11, m21, zero),
$vt::new(m02, m12, m22, zero),
$vt::new(zero, zero, zero, $t::splat(1.0))
)
}
/// Create a new rotation matrix from a rotation "around the x axis". This is
/// here as a convenience function for users coming from other libraries; it is
/// more proper to think of this as a rotation *in the yz plane*.
///
/// Assumes homogeneous 3d coordinates.
#[inline]
pub fn from_rotation_x(angle: $t) -> Self {
let (sin, cos) = angle.sin_cos();
let zero = $t::splat(0.0);
let one = $t::splat(1.0);
// think transposed as arguments are columns
Self::new(
$vt::new(one, zero, zero, zero),
$vt::new(zero, cos, sin, zero),
$vt::new(zero, -sin, cos, zero),
$vt::new(zero, zero, zero, one),
)
}
/// Create a new rotation matrix from a rotation "around the y axis". This is
/// here as a convenience function for users coming from other libraries; it is
/// more proper to think of this as a rotation *in the xz plane*.
///
/// Assumes homogeneous 3d coordinates.
#[inline]
pub fn from_rotation_y(angle: $t) -> Self {
let (sin, cos) = angle.sin_cos();
let zero = $t::splat(0.0);
let one = $t::splat(1.0);
// think transposed as arguments are columns
Self::new(
$vt::new(cos, zero, -sin, zero),
$vt::new(zero, one, zero, zero),
$vt::new(sin, zero, cos, zero),
$vt::new(zero, zero, zero, one),
)
}
/// Create a new rotation matrix from a rotation "around the z axis". This is
/// here as a convenience function for users coming from other libraries; it is
/// more proper to think of this as a rotation *in the xy plane*.
///
/// Assumes homogeneous 3d coordinates.
#[inline]
pub fn from_rotation_z(angle: $t) -> Self {
let (sin, cos) = angle.sin_cos();
let zero = $t::splat(0.0);
let one = $t::splat(1.0);
// think transposed as arguments are columns
Self::new(
$vt::new(cos, sin, zero, zero),
$vt::new(-sin, cos, zero, zero),
$vt::new(zero, zero, one, zero),
$vt::new(zero, zero, zero, one),
)
}
/// Create a new rotation matrix from a rotation around the given axis.
/// The axis will be interpreted as a 3d vector.
/// This is here as a convenience function for users coming from other libraries.
///
/// Assumes homogeneous 3d coordinates.
#[inline]
pub fn from_rotation_around(axis: $vt, angle: $t) -> Self {
let (sin, cos) = angle.sin_cos();
let zero = $t::splat(0.0);
let one = $t::splat(1.0);
let mul = one - cos;
let x_sin = axis.x * sin;
let y_sin = axis.y * sin;
let z_sin = axis.z * sin;
let xy_mul = axis.x * axis.y * mul;
let xz_mul = axis.x * axis.z * mul;
let yz_mul = axis.y * axis.z * mul;
let m00 = (axis.x * axis.x).mul_add(mul, cos);
let m10 = xy_mul + z_sin;
let m20 = xz_mul - y_sin;
let m01 = xy_mul - z_sin;
let m11 = (axis.y * axis.y).mul_add(mul, cos);
let m21 = yz_mul + x_sin;
let m02 = xz_mul + y_sin;
let m12 = yz_mul - x_sin;
let m22 = (axis.z * axis.z).mul_add(mul, cos);
// think transposed as arguments are columns
Self::new(
$vt::new(m00, m10, m20, zero),
$vt::new(m01, m11, m21, zero),
$vt::new(m02, m12, m22, zero),
$vt::new(zero, zero, zero, one),
)
}
/// Construct a rotation matrix given a bivector which defines a plane and rotation orientation,
/// and a rotation angle.
///
/// `plane` must be normalized!
///
/// This is the equivalent of an axis-angle rotation.
///
/// Assumes homogeneous 3d coordinates.
#[inline]
pub fn from_angle_plane(angle: $t, plane: $bt) -> Self {
$rt::from_angle_plane(angle, plane).into_matrix().into_homogeneous()
}
/// Assumes homogeneous 3d coordinates.
pub fn translate(&mut self, translation: &$v3t) {
self[3].x += translation.x;
self[3].y += translation.y;
self[3].z += translation.z;
}
/// Assumes homogeneous 3d coordinates.
pub fn translated(&self, translation: &$v3t) -> Self {
let mut res = *self;
res.translate(translation);
res
}
/// Constructs a 'look-at' matrix from an eye position, a focus position to look towards,
/// and a vector that defines the 'up' direction.
///
/// This function assumes a right-handed, y-up coordinate space.
#[inline]
pub fn look_at(eye: $v3t, at: $v3t, up: $v3t) -> Self {
let f = (at - eye).normalized();
let r = f.cross(up).normalized();
let u = r.cross(f);
Self::new(
$vt::new(r.x, u.x, -f.x, $t::splat(0.0)),
$vt::new(r.y, u.y, -f.y, $t::splat(0.0)),
$vt::new(r.z, u.z, -f.z, $t::splat(0.0)),
$vt::new(-r.dot(eye), -u.dot(eye), f.dot(eye), $t::splat(1.0))
)
}
/// Constructs a 'look-at' matrix from an eye position, a focus position to look towards,
/// and a vector that defines the 'up' direction.
///
/// This function assumes a *left*-handed, y-up coordinate space.
#[inline]
pub fn look_at_lh(eye: $v3t, at: $v3t, up: $v3t) -> Self {
let f = (at - eye).normalized();
let r = f.cross(up).normalized();
let u = r.cross(f);
Self::new(
$vt::new(r.x, u.x, f.x, $t::splat(0.0)),
$vt::new(r.y, u.y, f.y, $t::splat(0.0)),
$vt::new(r.z, u.z, f.z, $t::splat(0.0)),
$vt::new(-r.dot(eye), -u.dot(eye), -f.dot(eye), $t::splat(1.0))
)
}
#[inline]
pub fn transpose(&mut self) {
*self = self.transposed();
}
#[inline]
pub fn transposed(&self) -> Self {
let (x0, y0, z0, w0) = self.cols[0].into();
let (x1, y1, z1, w1) = self.cols[1].into();
let (x2, y2, z2, w2) = self.cols[2].into();
let (x3, y3, z3, w3) = self.cols[3].into();
Self::new(
$vt::new(x0, x1, x2, x3),
$vt::new(y0, y1, y2, y3),
$vt::new(z0, z1, z2, z3),
$vt::new(w0, w1, w2, w3),
)
}
/// If this matrix is not currently invertable, this function will return
/// an invalid inverse. This status is not checked by the library.
#[inline]
pub fn inverse(&mut self) {
*self = self.inversed();
}
#[inline]
pub fn determinant(&self) -> $t {
let (m00, m01, m02, m03) = self.cols[0].into();
let (m10, m11, m12, m13) = self.cols[1].into();
let (m20, m21, m22, m23) = self.cols[2].into();
let (m30, m31, m32, m33) = self.cols[3].into();
let a2323 = (m22 * m33) - (m23 * m32);
let a1323 = (m21 * m33) - (m23 * m31);
let a1223 = (m21 * m32) - (m22 * m31);
let a0323 = (m20 * m33) - (m23 * m30);
let a0223 = (m20 * m32) - (m22 * m30);
let a0123 = (m20 * m31) - (m21 * m30);
m00 * (m11 * a2323 - m12 * a1323 + m13 * a1223)
- m01 * (m10 * a2323 - m12 * a0323 + m13 * a0223)
+ m02 * (m10 * a1323 - m11 * a0323 + m13 * a0123)
- m03 * (m10 * a1223 - m11 * a0223 + m12 * a0123)
}
/// The adjugate of this matrix, i.e. the transpose of
/// the cofactor matrix.
///
/// This is equivalent to the inverse
/// but without dividing by the determinant of the matrix,
/// which can be useful in some contexts for better performance.
///
/// One such case is when this matrix is interpreted as a
/// a homogeneous transformation matrix, in which case uniform scaling will
/// not affect the resulting projected 3d version of transformed points or
/// vectors.
#[inline]
pub fn adjugate(&self) -> Self {
let (m00, m01, m02, m03) = self.cols[0].into();
let (m10, m11, m12, m13) = self.cols[1].into();
let (m20, m21, m22, m23) = self.cols[2].into();
let (m30, m31, m32, m33) = self.cols[3].into();
let coef00 = (m22 * m33) - (m32 * m23);
let coef02 = (m12 * m33) - (m32 * m13);
let coef03 = (m12 * m23) - (m22 * m13);
let coef04 = (m21 * m33) - (m31 * m23);
let coef06 = (m11 * m33) - (m31 * m13);
let coef07 = (m11 * m23) - (m21 * m13);
let coef08 = (m21 * m32) - (m31 * m22);
let coef10 = (m11 * m32) - (m31 * m12);
let coef11 = (m11 * m22) - (m21 * m12);
let coef12 = (m20 * m33) - (m30 * m23);
let coef14 = (m10 * m33) - (m30 * m13);
let coef15 = (m10 * m23) - (m20 * m13);
let coef16 = (m20 * m32) - (m30 * m22);
let coef18 = (m10 * m32) - (m30 * m12);
let coef19 = (m10 * m22) - (m20 * m12);
let coef20 = (m20 * m31) - (m30 * m21);
let coef22 = (m10 * m31) - (m30 * m11);
let coef23 = (m10 * m21) - (m20 * m11);
let fac0 = $vt::new(coef00, coef00, coef02, coef03);
let fac1 = $vt::new(coef04, coef04, coef06, coef07);
let fac2 = $vt::new(coef08, coef08, coef10, coef11);
let fac3 = $vt::new(coef12, coef12, coef14, coef15);
let fac4 = $vt::new(coef16, coef16, coef18, coef19);
let fac5 = $vt::new(coef20, coef20, coef22, coef23);
let vec0 = $vt::new(m10, m00, m00, m00);
let vec1 = $vt::new(m11, m01, m01, m01);
let vec2 = $vt::new(m12, m02, m02, m02);
let vec3 = $vt::new(m13, m03, m03, m03);
let inv0 = (vec1 * fac0) - (vec2 * fac1) + (vec3 * fac2);
let inv1 = (vec0 * fac0) - (vec2 * fac3) + (vec3 * fac4);
let inv2 = (vec0 * fac1) - (vec1 * fac3) + (vec3 * fac5);
let inv3 = (vec0 * fac2) - (vec1 * fac4) + (vec2 * fac5);
let sign_a = $vt::new($t::splat(1.0), $t::splat(-1.0), $t::splat(1.0), $t::splat(-1.0));
let sign_b = $vt::new($t::splat(-1.0), $t::splat(1.0), $t::splat(-1.0), $t::splat(1.0));
Self {
cols: [
inv0 * sign_a,
inv1 * sign_b,
inv2 * sign_a,
inv3 * sign_b,
]
}
}
/// If this matrix is not currently invertable, this function will return
/// an invalid inverse. This status is not checked by the library.
#[inline]
pub fn inversed(&self) -> Self {
let adjugate = self.adjugate();
let row0 = $vt::new(
adjugate.cols[0].x,
adjugate.cols[1].x,
adjugate.cols[2].x,
adjugate.cols[3].x,
);
let dot0 = self.cols[0] * row0;
let dot1 = dot0.x + dot0.y + dot0.z + dot0.w;
let rcp_det = $t::splat(1.0) / dot1;
adjugate * rcp_det
}
/// Transform a Vec3 by self, interpreting it as a vector.
#[inline]
pub fn transform_vec3(&self, vec: $v3t) -> $v3t {
(*self * vec.into_homogeneous_vector()).truncated()
}
/// Transform a Vec3 by self, interpreting it as a point.
#[inline]
pub fn transform_point3(&self, point: $v3t) -> $v3t {
(*self * point.into_homogeneous_point()).normalized_homogeneous_point().truncated()
}
/// If `self` represents an affine transformation, return its translation components.
/// Otherwise, the returned value has undefined properties.
#[inline]
pub fn extract_translation(&self) -> $v3t {
self.cols[3].truncated()
}
/// If the 3x3 left upper block of `self` is a rotation, return the corresponding
/// rotor. Otherwise, the returned value is a `Rotor3` with undefined properties.
pub fn extract_rotation(&self) -> $rt {
self.truncate().into_rotor3()
}
/// If self represents an `Isometry3` (i.e. self is a product of the from `T * R` where
/// `T` is a translation and `R` a rotation), return the isometry
///
/// If `self` does not represent an isometry, the returned value has undefined
/// properties.
#[allow(clippy::wrong_self_convention)]
pub fn into_isometry(&self) -> $i3t {
$i3t::new(self.extract_translation(), self.extract_rotation())
}
/// Truncate `self` to a matrix consisting of the 3x3 left upper block.
/// If you need a rotation, consider using [`Self::extract_rotation()`] instead.
pub fn truncate(&self) -> $m3t {
$m3t::new(
self.cols[0].truncated(),
self.cols[1].truncated(),
self.cols[2].truncated(),
)
}
/// Get the [`core::alloc::Layout`] of `Self`
#[inline]
pub fn layout() -> alloc::alloc::Layout {
alloc::alloc::Layout::from_size_align(std::mem::size_of::<Self>(), std::mem::align_of::<$t>()).unwrap()
}
/// Interpret `self` as a statically sized array of the base numeric type.
#[inline]
pub fn as_array(&self) -> &[$t; 16] {
let ptr = self as *const $n as *const [$t; 16];
unsafe { &*ptr }
}
/// Interpret `self` as a statically sized array of the base numeric type.
#[inline]
pub fn as_mut_array(&mut self) -> &mut [$t; 16] {
let ptr = self as *mut $n as *mut [$t; 16];
unsafe { &mut *ptr }
}
/// Interpret `self` as a statically sized array of its component (column) vectors.
#[inline]
pub fn as_component_array(&self) -> &[$vt; 4] {
let ptr = self as *const $n as *const [$vt; 4];
unsafe { &*ptr }
}
/// Interpret `self` as a statically sized array of its component (column) vectors.
#[inline]
pub fn as_mut_component_array(&mut self) -> &mut [$vt; 4] {
let ptr = self as *mut $n as *mut [$vt; 4];
unsafe { &mut *ptr }
}
/// Interpret `self` as a slice of the base numeric type.
#[inline]
pub fn as_slice(&self) -> &[$t] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts(self as *const $n as *const $t, 16)
}
}
/// Interpret `self` as a slice of the base numeric type.
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [$t] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts_mut(self as *mut $n as *mut $t, 16)
}
}
/// Interpret `self` as a slice of the component (column) vectors
#[inline]
pub fn as_component_slice(&self) -> &[$vt] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts(self as *const $n as *const $vt, 4)
}
}
/// Interpret `self` as a slice of the component (column) vectors
#[inline]
pub fn as_mut_component_slice(&mut self) -> &mut [$vt] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts_mut(self as *mut $n as *mut $vt, 4)
}
}
/// Interpret `self` as a slice of bytes
#[inline]
pub fn as_byte_slice(&self) -> &[u8] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts(self as *const $n as *const u8, 16 * std::mem::size_of::<$t>())
}
}
/// Interpret `self` as a slice of bytes
#[inline]
pub fn as_mut_byte_slice(&mut self) -> &mut [u8] {
// This is safe because we are statically bounding our slices to the size of these
// vectors
unsafe {
std::slice::from_raw_parts_mut(self as *mut $n as *mut u8, 16 * std::mem::size_of::<$t>())
}
}
/// Returns a constant unsafe pointer to the underlying data in the underlying type.
/// This function is safe because all types here are repr(C) and can be represented
/// as their underlying type.
///
/// # Safety
///
/// It is up to the caller to correctly use this pointer and its bounds.
#[inline]
pub const fn as_ptr(&self) -> *const $t {
self as *const $n as *const $t
}
/// Returns a mutable unsafe pointer to the underlying data in the underlying type.
/// This function is safe because all types here are repr(C) and can be represented
/// as their underlying type.
///
/// # Safety
///
/// It is up to the caller to correctly use this pointer and its bounds.
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut $t {
self as *mut $n as *mut $t
}
}
impl Mul for $n {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self {
let sa = self.cols[0];
let sb = self.cols[1];
let sc = self.cols[2];
let sd = self.cols[3];
let oa = rhs.cols[0];
let ob = rhs.cols[1];
let oc = rhs.cols[2];
let od = rhs.cols[3];
Self::new(
$vt::new(
(sa.x * oa.x) + (sb.x * oa.y) + (sc.x * oa.z) + (sd.x * oa.w),
(sa.y * oa.x) + (sb.y * oa.y) + (sc.y * oa.z) + (sd.y * oa.w),
(sa.z * oa.x) + (sb.z * oa.y) + (sc.z * oa.z) + (sd.z * oa.w),
(sa.w * oa.x) + (sb.w * oa.y) + (sc.w * oa.z) + (sd.w * oa.w),
),
$vt::new(
(sa.x * ob.x) + (sb.x * ob.y) + (sc.x * ob.z) + (sd.x * ob.w),
(sa.y * ob.x) + (sb.y * ob.y) + (sc.y * ob.z) + (sd.y * ob.w),
(sa.z * ob.x) + (sb.z * ob.y) + (sc.z * ob.z) + (sd.z * ob.w),
(sa.w * ob.x) + (sb.w * ob.y) + (sc.w * ob.z) + (sd.w * ob.w),
),
$vt::new(
(sa.x * oc.x) + (sb.x * oc.y) + (sc.x * oc.z) + (sd.x * oc.w),
(sa.y * oc.x) + (sb.y * oc.y) + (sc.y * oc.z) + (sd.y * oc.w),
(sa.z * oc.x) + (sb.z * oc.y) + (sc.z * oc.z) + (sd.z * oc.w),
(sa.w * oc.x) + (sb.w * oc.y) + (sc.w * oc.z) + (sd.w * oc.w),
),
$vt::new(
(sa.x * od.x) + (sb.x * od.y) + (sc.x * od.z) + (sd.x * od.w),
(sa.y * od.x) + (sb.y * od.y) + (sc.y * od.z) + (sd.y * od.w),
(sa.z * od.x) + (sb.z * od.y) + (sc.z * od.z) + (sd.z * od.w),
(sa.w * od.x) + (sb.w * od.y) + (sc.w * od.z) + (sd.w * od.w),
),
)
}
}
impl Mul<$vt> for $n {
type Output = $vt;
#[inline]
fn mul(self, rhs: $vt) -> $vt {
let a = self.cols[0];
let b = self.cols[1];
let c = self.cols[2];
let d = self.cols[3];
$vt::new(
a.x * rhs.x + b.x * rhs.y + c.x * rhs.z + d.x * rhs.w,
a.y * rhs.x + b.y * rhs.y + c.y * rhs.z + d.y * rhs.w,
a.z * rhs.x + b.z * rhs.y + c.z * rhs.z + d.z * rhs.w,
a.w * rhs.x + b.w * rhs.y + c.w * rhs.z + d.w * rhs.w,
)
}
}
impl Mul<$t> for $n {
type Output = $n;
#[inline]
fn mul(self, rhs: $t) -> $n {
$n::new(
self.cols[0] * rhs,
self.cols[1] * rhs,
self.cols[2] * rhs,
self.cols[3] * rhs,
)
}
}
impl Mul<$n> for $t {
type Output = $n;
#[inline]
fn mul(self, rhs: $n) -> $n {
$n::new(
rhs.cols[0] * self,
rhs.cols[1] * self,
rhs.cols[2] * self,
rhs.cols[3] * self,
)
}
}
impl Add for $n {
type Output = Self;
#[inline]
fn add(self, rhs: $n) -> Self {
$n::new(
self.cols[0] + rhs.cols[0],
self.cols[1] + rhs.cols[1],
self.cols[2] + rhs.cols[2],
self.cols[3] + rhs.cols[3],
)
}
}
impl AddAssign for $n {
#[inline]
fn add_assign(&mut self, rhs: $n) {
*self = *self + rhs;
}
}
impl From<[$t; 16]> for $n {
#[inline]
fn from(comps: [$t; 16]) -> Self {
Self::new(
$vt::new(comps[0], comps[1], comps[2], comps[3]),
$vt::new(comps[4], comps[5], comps[6], comps[7]),
$vt::new(comps[8], comps[9], comps[10], comps[11]),
$vt::new(comps[12], comps[13], comps[14], comps[15]),
)
}
}
impl From<[[$t; 4]; 4]> for $n {
#[inline]
fn from(comps: [[$t; 4]; 4]) -> Self {
Self::new(
$vt::new(comps[0][0], comps[0][1], comps[0][2], comps[0][3]),
$vt::new(comps[1][0], comps[1][1], comps[1][2], comps[1][3]),
$vt::new(comps[2][0], comps[2][1], comps[2][2], comps[2][3]),
$vt::new(comps[3][0], comps[3][1], comps[3][2], comps[3][3])
)
}
}
impl From<$n> for [[$t; 4]; 4] {
#[inline]
fn from(mat4: $n) -> Self {
[
[mat4.cols[0].x, mat4.cols[0].y, mat4.cols[0].z, mat4.cols[0].w],
[mat4.cols[1].x, mat4.cols[1].y, mat4.cols[1].z, mat4.cols[1].w],
[mat4.cols[2].x, mat4.cols[2].y, mat4.cols[2].z, mat4.cols[2].w],
[mat4.cols[3].x, mat4.cols[3].y, mat4.cols[3].z, mat4.cols[3].w]
]
}
}
impl From<&[$t; 16]> for $n {
#[inline]
fn from(comps: &[$t; 16]) -> Self {
Self::from(*comps)
}
}
impl Index<usize> for $n {
type Output = $vt;
fn index(&self, index: usize) -> &Self::Output {
&self.cols[index]
}
}
impl IndexMut<usize> for $n {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.cols[index]
}
}
)+
}
}
mat4s!(
Mat4 => Rotor3, Bivec3, Vec4, Vec3, Mat3, Isometry3, f32,
Mat4x4 => Rotor3x4, Bivec3x4, Vec4x4, Vec3x4, Mat3x4, Isometry3x4, f32x4,
Mat4x8 => Rotor3x8, Bivec3x8, Vec4x8, Vec3x8, Mat3x8, Isometry3x8, f32x8
);
#[cfg(feature = "f64")]
mat4s!(
DMat4 => DRotor3, DBivec3, DVec4, DVec3, DMat3, DIsometry3, f64,
DMat4x2 => DRotor3x2, DBivec3x2, DVec4x2, DVec3x2, DMat3x2, DIsometry3x2, f64x2,
DMat4x4 => DRotor3x4, DBivec3x4, DVec4x4, DVec3x4, DMat3x4, DIsometry3x4, f64x4
);
#[cfg(test)]
mod test {
use super::*;
use crate::util::*;
/* TODO:
Re-enable these. The current way that Matrix3::into_rotor() works sometimes fails these
edge cases based on rounding error accumulated from the round trip due to the way it uses
use std::f32::consts::FRAC_PI_2;
use std::f32::consts::PI;
copysign()
#[test]
pub fn mat3_to_rotor_corner_cases(){
for i in 0..64 {
let alpha = {
match i % 4 {
0 => -FRAC_PI_2,
1 => 0.,
2 => FRAC_PI_2,
3 => PI,
_ => unreachable!()
}
};
let beta = {
match (i / 4) % 4 {
0 => -FRAC_PI_2,
1 => 0.,
2 => FRAC_PI_2,
3 => PI,
_ => unreachable!()
}
};
let gamma = {
match (i / 16) % 4 {
0 => -FRAC_PI_2,
1 => 0.,
2 => FRAC_PI_2,
3 => PI,
_ => unreachable!()
}
};
println!("roll {}, pitch {}, yaw {}", alpha, beta, gamma);
let rotor = Rotor3::from_euler_angles(alpha, beta, gamma);
let mat = rotor.into_matrix();
let rotor2 = mat.into_rotor3();
assert!(rotor.eq_eps(rotor2));
let xr = Vec3::unit_x().rotated_by(rotor);
let xr2 = Vec3::unit_x().rotated_by(rotor2);
assert!(xr.eq_eps(xr2));
let yr = Vec3::unit_y().rotated_by(rotor);
let yr2 = Vec3::unit_y().rotated_by(rotor2);
assert!(yr.eq_eps(yr2));
let zr = Vec3::unit_z().rotated_by(rotor);
let zr2 = Vec3::unit_z().rotated_by(rotor2);
assert!(zr.eq_eps(zr2));
}
}*/
#[test]
pub fn isometry_roundtrip() {
let a = Vec3::new(1.0, 2.0, -5.0).normalized();
let b = Vec3::new(1.0, 1.0, 1.0).normalized();
let c = Vec3::new(2.0, 3.0, -3.0).normalized();
let r_ab = Rotor3::from_rotation_between(a, b);
let iso = Isometry3::new(c, r_ab);
let iso_mat4 = iso.into_homogeneous_matrix();
let iso_ = iso_mat4.into_isometry();
assert!(iso_.translation.eq_eps(c));
assert!(iso_.rotation.eq_eps(r_ab));
}
#[test]
pub fn test_euler_angle_conversion() {
let roll = 0.4;
let yaw = 0.3;
let pitch = 0.2;
let mat1 = Mat3::from_euler_angles(roll, pitch, yaw);
let mat2 =
Mat3::from_rotation_y(yaw) * Mat3::from_rotation_x(pitch) * Mat3::from_rotation_z(roll);
assert_eq!(mat1[0], mat2[0]);
assert_eq!(mat1[1], mat2[1]);
assert_eq!(mat1[2], mat2[2]);
let mat3 = Mat4::from_euler_angles(roll, pitch, yaw);
let mat4 =
Mat4::from_rotation_y(yaw) * Mat4::from_rotation_x(pitch) * Mat4::from_rotation_z(roll);
assert_eq!(mat3[0], mat4[0]);
assert_eq!(mat3[1], mat4[1]);
assert_eq!(mat3[2], mat4[2]);
assert_eq!(mat3[3], mat4[3]);
}
}