Nalgebra 3D matrix transform_vector()

⚓ Rust    📅 2025-08-27    👤 surdeus    👁️ 4      

surdeus

The AI gave me this test:

#[test]
fn matrix_translation() {
    let mut m = Matrix3d::identity();
    m.set_position(Vector3d(1.0, 2.0, 3.0, 0.0));

    assert_eq!(m.position(), Vector3d(1.0, 2.0, 3.0, 0.0));

    let v = Vector3d(0.0, 0.0, 0.0, 0.0);
    let t = m.transform_vector(v);
    assert_eq!(t, Vector3d::new(1.0, 2.0, 3.0, 0.0));
}

It's panicking at the assert_eq!(t, Vector3d::new(1.0, 2.0, 3.0, 0.0)); line:

thread 'geom::matrix3d::tests::matrix_translation' (10124) panicked at crates\whack\src\geom\matrix3d.rs:828:9:
assertion `left == right` failed
  left: (0, 0, 0)
 right: (1, 2, 3)

Here are the relevant parts of Matrix3d:

// row-major order; mostly always built by nalgebra
pub struct Matrix3d(pub [f64; 16]);

impl Matrix3d {
    pub fn identity() -> Self {
        Self([
            1.0, 0.0, 0.0, 0.0,
            0.0, 1.0, 0.0, 0.0,
            0.0, 0.0, 1.0, 0.0,
            0.0, 0.0, 0.0, 1.0,
        ])
    }

    pub fn position(&self) -> Vector3d {
        Vector3d(self.0[12], self.0[13], self.0[14], 0.)
    }

    pub fn set_position(&mut self, pos: Vector3d) {
        self.0[12] = pos.0;
        self.0[13] = pos.1;
        self.0[14] = pos.2;
    }

    pub fn transform_vector(&self, v: Vector3d) -> Vector3d {
        Vector3d::from(self.to_na().transform_vector(&v.to_na_3()))
    }

    /// Returns the `nagelbra` version of this matrix.
    pub fn to_na(&self) -> nalgebra::Matrix4<f64> {
        nalgebra::Matrix4::from_row_slice(&self.0)
    }
}

Is it correct or wrong? Or is nalgebra not aware this is a 3D matrix (4x4)?

From Adobe documentation:

image

Note Vector3d has an optional component w, to comply mostly with the Adobe Display List.

1 post - 1 participant

Read full topic

🏷️ Rust_feed