Skip to content

Commit

Permalink
Add From/Into array for Quaternion
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsided committed Jul 9, 2024
1 parent 2ce6de2 commit b2c3189
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/quaternion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ impl From<Quaternion> for (f32, f32, f32, f32) {
}
}

impl From<[f32; 4]> for Quaternion {
fn from(q: [f32; 4]) -> Quaternion {
Self::new(q[0], q[1], q[2], q[3])
}
}

impl From<Quaternion> for [f32; 4] {
fn from(q: Quaternion) -> [f32; 4] {
q.to_array()
}
}

impl Mul for Quaternion {
type Output = Self;

Expand Down Expand Up @@ -445,4 +457,24 @@ mod tests {

assert!((v1_r - v2).magnitude() < 1e-1);
}

#[test]
fn from_tuple() {
let quat: Quaternion = (1.0, 2.0, 3.0, 4.0).into();
let (a, b, c, d) = quat.into();
assert_eq!(a, 1.0);
assert_eq!(b, 2.0);
assert_eq!(c, 3.0);
assert_eq!(d, 4.0);
}

#[test]
fn from_array() {
let quat: Quaternion = [1.0, 2.0, 3.0, 4.0].into();
let quat: [f32; 4] = quat.into();
assert_eq!(quat[0], 1.0);
assert_eq!(quat[1], 2.0);
assert_eq!(quat[2], 3.0);
assert_eq!(quat[3], 4.0);
}
}

0 comments on commit b2c3189

Please sign in to comment.