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
//! Square matrices. use std::ops::*; use crate::*; use wide::f32x4; macro_rules! mat2s { ($($n:ident, $vt:ident => $t:ident),+) => { /// A 2x2 square matrix. /// /// Useful for performing linear transformations (rotation, scaling) on 2d vectors. $(#[derive(Clone, Copy, Debug)] #[repr(C)] pub struct $n { pub cols: [$vt; 2], } impl $n { #[inline] pub fn new(col1: $vt, col2: $vt) -> Self { $n { cols: [col1, col2], } } #[inline] pub fn layout() -> alloc::alloc::Layout { alloc::alloc::Layout::from_size_align(std::mem::size_of::<Self>(), std::mem::align_of::<$vt>()).unwrap() } #[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(std::mem::transmute(self), 4) } } #[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(std::mem::transmute(self), 2) } } #[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(std::mem::transmute(self), 4 * std::mem::size_of::<$t>()) } } #[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(std::mem::transmute(self), 4) } } #[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(std::mem::transmute(self), 2) } } #[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(std::mem::transmute(self), 4 * std::mem::size_of::<$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.x * ob.x + sb.x * ob.y, ), $vt::new( sa.y * oa.x + sb.y * oa.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 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; 4]> for $n { #[inline] fn from(comps: &[$t; 4]) -> Self { Self::from(*comps) } } )+ } } mat2s!(Mat2, Vec2 => f32 , Wat2, Wec2 => f32x4); macro_rules! mat3s { ($($n:ident => $rt:ident, $bt:ident, $m4t:ident, $v4t: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)] #[repr(C)] pub struct $n { pub cols: [$vt; 3], } impl $n { #[inline] pub fn new(col1: $vt, col2: $vt, col3: $vt) -> Self { $n { cols: [col1, col2, col3], } } #[inline] pub fn from_scale(scale: $t) -> Self { let zero = $t::from(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::from(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::from(1.0), $t::from(0.0), $t::from(0.0)), $vt::new($t::from(0.0), $t::from(1.0), $t::from(0.0)), $vt::new($t::from(0.0), $t::from(0.0), $t::from(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") /// /// **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 rotor = $rt::from_euler_angles(roll, pitch, yaw); rotor.into_matrix() } /// Create a new rotation matrix from a rotation "about 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*. /// /// **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_rotation_x(angle: $t) -> Self { // TODO: Easy optimization target. Self::from_euler_angles($t::from(0.0), angle, $t::from(0.0)) } /// Create a new rotation matrix from a rotation "about 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*. /// /// **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_rotation_y(angle: $t) -> Self { // TODO: Easy optimization target. Self::from_euler_angles($t::from(0.0), $t::from(0.0), angle) } /// Create a new rotation matrix from a rotation "about 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*. /// /// **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_rotation_z(angle: $t) -> Self { // TODO: Easy optimization target. Self::from_euler_angles(angle, $t::from(0.0), $t::from(0.0)) } /// 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::from(0.0); let one = $t::from(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 layout() -> alloc::alloc::Layout { alloc::alloc::Layout::from_size_align(std::mem::size_of::<Self>(), std::mem::align_of::<$t>()).unwrap() } #[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(std::mem::transmute(self), 9) } } #[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(std::mem::transmute(self), 3) } } #[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(std::mem::transmute(self), 9 * std::mem::size_of::<$t>()) } } #[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(std::mem::transmute(self), 9) } } #[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(std::mem::transmute(self), 3) } } #[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(std::mem::transmute(self), 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 fn as_ptr(&self) -> *const $t { unsafe { std::mem::transmute(self) } } /// 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(&self) -> *mut $t { unsafe { std::mem::transmute(self) } } } 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 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; 9]> for $n { #[inline] fn from(comps: &[$t; 9]) -> Self { Self::from(*comps) } } )+ } } mat3s!(Mat3 => Rotor3, Bivec3, Mat4, Vec4, Vec3, f32, Wat3 => WRotor3, WBivec3, Wat4, Wec4, Wec3, f32x4); macro_rules! mat4s { ($($n:ident => $rt:ident, $bt:ident, $vt:ident, $v3t: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)] #[repr(C)] pub struct $n { pub cols: [$vt; 4], } impl $n { #[inline] pub 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::from(1.0), $t::from(0.0), $t::from(0.0), $t::from(0.0)), $vt::new($t::from(0.0), $t::from(1.0), $t::from(0.0), $t::from(0.0)), $vt::new($t::from(0.0), $t::from(0.0), $t::from(1.0), $t::from(0.0)), $vt::new($t::from(0.0), $t::from(0.0), $t::from(0.0), $t::from(1.0))) } /// Assumes homogeneous 3d coordinates. #[inline] pub fn from_translation(trans: $v3t) -> Self { Self::new( $vt::new($t::from(1.0), $t::from(0.0), $t::from(0.0), $t::from(0.0)), $vt::new($t::from(0.0), $t::from(1.0), $t::from(0.0), $t::from(0.0)), $vt::new($t::from(0.0), $t::from(0.0), $t::from(1.0), $t::from(0.0)), $vt::new(trans.x, trans.y, trans.z, $t::from(1.0))) } /// Assumes homogeneous 3d coordinates. #[inline] pub fn from_scale(scale: $t) -> Self { let zero = $t::from(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::from(1.0)), ) } /// Assumes homogeneous 3d coordinates. #[inline] pub fn from_nonuniform_scale(scale: $vt) -> Self { let zero = $t::from(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::from(1.0)), ) } /// 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 rotor = $rt::from_euler_angles(roll, pitch, yaw); rotor.into_matrix().into_homogeneous() } /// Create a new rotation matrix from a rotation "about 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. /// /// **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_rotation_x(angle: $t) -> Self { // TODO: Easy optimization target. Self::from_euler_angles($t::from(0.0), angle, $t::from(0.0)) } /// Create a new rotation matrix from a rotation "about 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. /// /// **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_rotation_y(angle: $t) -> Self { Self::from_euler_angles($t::from(0.0), $t::from(0.0), angle) } /// Create a new rotation matrix from a rotation "about 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. /// /// **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_rotation_z(angle: $t) -> Self { // TODO: Easy optimization target. Self::from_euler_angles(angle, $t::from(0.0), $t::from(0.0)) } /// 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() } /// 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::from(0.0)), $vt::new(r.y, u.y, -f.y, $t::from(0.0)), $vt::new(r.z, u.z, -f.z, $t::from(0.0)), $vt::new(-r.dot(eye), -u.dot(eye), f.dot(eye), $t::from(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::from(0.0)), $vt::new(r.y, u.y, f.y, $t::from(0.0)), $vt::new(r.z, u.z, f.z, $t::from(0.0)), $vt::new(-r.dot(eye), -u.dot(eye), -f.dot(eye), $t::from(1.0)) ) } #[inline] pub fn layout() -> alloc::alloc::Layout { alloc::alloc::Layout::from_size_align(std::mem::size_of::<Self>(), std::mem::align_of::<$t>()).unwrap() } #[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(std::mem::transmute(self), 16) } } #[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(std::mem::transmute(self), 4) } } #[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(std::mem::transmute(self), 16 * std::mem::size_of::<$t>()) } } #[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(std::mem::transmute(self), 16) } } #[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(std::mem::transmute(self), 4) } } #[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(std::mem::transmute(self), 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 fn as_ptr(&self) -> *const $t { unsafe { std::mem::transmute(self) } } /// 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(&self) -> *mut $t { unsafe { std::mem::transmute(self) } } } 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.z * rhs.w, ) } } 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; 16]> for $n { #[inline] fn from(comps: &[$t; 16]) -> Self { Self::from(*comps) } } )+ } } mat4s!(Mat4 => Rotor3, Bivec3, Vec4, Vec3, f32, Wat4 => WRotor3, WBivec3, Wec4, Wec3, f32x4);