Skip to content

Commit

Permalink
Fix Quaternion memory layout
Browse files Browse the repository at this point in the history
The Quaternion memory layout is now `[x, y, z, w]` (i.e. scalar at the
end). This is consistent with the `Into` trait implementation and also
consistent with other libraries (such as GLM) and the GLTF spec.

Fixes #49
  • Loading branch information
aloucks authored and kvark committed Dec 27, 2019
1 parent c7abd5d commit 8e33991
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/rotation.rs
Expand Up @@ -8,10 +8,10 @@ use vector::Vector3;
#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Eq, Ord)]
#[repr(C)]
pub struct Quaternion<T> {
/// Scalar part of a quaternion.
pub s: T,
/// Vector part of a quaternion.
pub v: Vector3<T>,
/// Scalar part of a quaternion.
pub s: T,
}

impl<T> From<[T; 4]> for Quaternion<T> {
Expand Down
18 changes: 18 additions & 0 deletions tests/tests.rs
Expand Up @@ -256,3 +256,21 @@ fn vector_from_slice_success() {
fn vector_from_slice_fail() {
let _ = Vector4::from_slice(&[0.0]);
}

#[test]
fn quaternion_layout() {
let q = Quaternion {
v: Vector3::from([0, 1, 2]),
s: 3,
};
let expected = [0, 1, 2, 3];

let a: [i32; 4] = q.into();
assert_eq!(a, expected);

let b: &[i32; 4] = q.as_ref();
assert_eq!(b, &expected);

let c: [i32; 4] = unsafe { core::mem::transmute(q) };
assert_eq!(c, expected);
}

0 comments on commit 8e33991

Please sign in to comment.