Browse Source

More fixes for debug print + matrix equality

pull/28/head
Ian Chamberlain 1 year ago
parent
commit
4cc9a649fc
No known key found for this signature in database
GPG Key ID: AE5484D09405AA60
  1. 27
      citro3d/src/math/matrix.rs
  2. 2
      citro3d/src/math/ops.rs
  3. 9
      citro3d/src/math/projection.rs

27
citro3d/src/math/matrix.rs

@ -38,25 +38,32 @@ mod private { @@ -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<const M: usize, const N: usize> fmt::Debug for Matrix<M, N> {
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::<Self>())
.field(&inner)
.finish()
let type_name = std::any::type_name::<Self>().split("::").last().unwrap();
f.debug_tuple(type_name).field(&inner).finish()
}
}
}

2
citro3d/src/math/ops.rs

@ -157,7 +157,7 @@ impl Mul<FVec3> for &Matrix<4, 3> { @@ -157,7 +157,7 @@ impl Mul<FVec3> for &Matrix<4, 3> {
impl<Rhs: Borrow<Self>, const M: usize, const N: usize> PartialEq<Rhs> for Matrix<M, N> {
fn eq(&self, other: &Rhs) -> bool {
unsafe { (*self.as_raw()).m == (*other.borrow().as_raw()).m }
self.as_rows() == other.borrow().as_rows()
}
}

9
citro3d/src/math/projection.rs

@ -73,10 +73,11 @@ impl Projection<Perspective> { @@ -73,10 +73,11 @@ impl Projection<Perspective> {
/// 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<Orthographic> { @@ -204,9 +205,9 @@ impl Projection<Orthographic> {
///
/// ```
/// # 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 {

Loading…
Cancel
Save