Skip to content

Commit

Permalink
implement Default for vector and matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
rlane committed Sep 9, 2023
1 parent 123761d commit 0f1f9f9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/mat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ macro_rules! mat_impl {
}
}

impl<T> Default for $MatN<T> where T: Number {
fn default() -> Self {
Self::zero()
}
}

impl<T> Eq for $MatN<T> where T: Eq {}
impl<T> PartialEq for $MatN<T> where T: PartialEq {
fn eq(&self, other: &Self) -> bool {
Expand Down
6 changes: 6 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,12 @@ macro_rules! vec_impl {
}
}

impl<T> Default for $VecN<T> where T: Number {
fn default() -> Self {
Self::zero()
}
}

//
// ops
//
Expand Down
16 changes: 16 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,13 @@ fn perp_product() {
assert_eq!(pp, vec2f(-0.5, 0.5));
}

#[test]
fn default() {
let vz : Vec3f = Default::default();
let expected = vec3f(0.0, 0.0, 0.0);
assert_eq!(vz, expected);
}

#[test]
fn zero() {
let vz = Vec3f::zero();
Expand Down Expand Up @@ -914,6 +921,15 @@ fn matrix_get_rows_columns() {
assert_eq!(m34.get_column(2), vec3f(2.0, 6.0, 10.0));
}

#[test]
fn matrix_default() {
let m4 : Mat4<f32> = Default::default();
for i in 0..4 {
assert_eq!(m4.get_row(i), vec4f(0.0, 0.0, 0.0, 0.0));
assert_eq!(m4.get_column(i), vec4f(0.0, 0.0, 0.0, 0.0));
}
}

#[test]
fn matrix_zero() {
let m4 = Mat4f::zero();
Expand Down

0 comments on commit 0f1f9f9

Please sign in to comment.