From 4cc9a649fcd039a0ed415581049929b4f3310003 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Mon, 16 Oct 2023 00:28:11 -0400 Subject: [PATCH] More fixes for debug print + matrix equality --- citro3d/src/math/matrix.rs | 27 +++++++++++++++++---------- citro3d/src/math/ops.rs | 2 +- citro3d/src/math/projection.rs | 9 +++++---- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/citro3d/src/math/matrix.rs b/citro3d/src/math/matrix.rs index f863784..79faabb 100644 --- a/citro3d/src/math/matrix.rs +++ b/citro3d/src/math/matrix.rs @@ -38,25 +38,32 @@ mod private { pub(crate) fn as_mut(&mut self) -> *mut citro3d_sys::C3D_Mtx { &mut self.0 } + + /// Trim the matrix down to only the rows and columns we care about, + /// since the inner representation is always 4x4. + pub(crate) fn as_rows(&self) -> [[f32; N]; M] { + let rows = unsafe { self.0.r }.map(|row| -> [f32; N] { + // Rows are stored in WZYX order, so we slice from back to front. + // UNWRAP: N ≤ 4, so slicing to a smaller array should always work + unsafe { row.c[(4 - N)..].try_into() }.unwrap() + }); + + // UNWRAP: M ≤ 4, so slicing to a smaller array should always work + rows[..M].try_into().unwrap() + } } impl fmt::Debug for Matrix { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let rows = unsafe { self.0.r }.map(|row| { - // UNWRAP: N ≤ 4, so slicing to a smaller array should always work - let mut row: [f32; N] = unsafe { row.c[..N].try_into() }.unwrap(); + let inner = self.as_rows().map(|mut row| { // Rows are stored in WZYX order which is opposite of how most people - // probably expect, so we reverse each row in-place as well. + // probably expect, so reverse each row in-place for debug printing row.reverse(); row }); - // UNWRAP: M ≤ 4, so slicing to a smaller array should always work - let inner: [_; M] = rows[..M].try_into().unwrap(); - - f.debug_tuple(std::any::type_name::()) - .field(&inner) - .finish() + let type_name = std::any::type_name::().split("::").last().unwrap(); + f.debug_tuple(type_name).field(&inner).finish() } } } diff --git a/citro3d/src/math/ops.rs b/citro3d/src/math/ops.rs index e8c648a..8204ca4 100644 --- a/citro3d/src/math/ops.rs +++ b/citro3d/src/math/ops.rs @@ -157,7 +157,7 @@ impl Mul for &Matrix<4, 3> { impl, const M: usize, const N: usize> PartialEq for Matrix { fn eq(&self, other: &Rhs) -> bool { - unsafe { (*self.as_raw()).m == (*other.borrow().as_raw()).m } + self.as_rows() == other.borrow().as_rows() } } diff --git a/citro3d/src/math/projection.rs b/citro3d/src/math/projection.rs index 278a776..46313b9 100644 --- a/citro3d/src/math/projection.rs +++ b/citro3d/src/math/projection.rs @@ -73,10 +73,11 @@ impl Projection { /// far: 100.0, /// }; /// - /// let bottom: Matrix = + /// let bottom: Matrix4 = /// Projection::perspective(PI / 4.0, AspectRatio::BottomScreen, clip_planes).into(); /// - /// let top: Matrix = Projection::perspective(PI / 4.0, AspectRatio::TopScreen, clip_planes).into(); + /// let top: Matrix4 = + /// Projection::perspective(PI / 4.0, AspectRatio::TopScreen, clip_planes).into(); /// ``` #[doc(alias = "Mtx_Persp")] #[doc(alias = "Mtx_PerspTilt")] @@ -204,9 +205,9 @@ impl Projection { /// /// ``` /// # let _runner = test_runner::GdbRunner::default(); - /// # use citro3d::math::{Projection, ClipPlanes, Matrix}; + /// # use citro3d::math::{Projection, ClipPlanes, Matrix4}; /// # - /// let mtx: Matrix = Projection::orthographic( + /// let mtx: Matrix4 = Projection::orthographic( /// 0.0..240.0, /// 0.0..400.0, /// ClipPlanes {