Skip to content

Commit

Permalink
Use explicit methods to convert between arrays and matrices.
Browse files Browse the repository at this point in the history
Removed `From` trait implementations converting between 1D and 2D arrays
and matrix types to avoid any potential confusion about data being
loaded and stored in column major order. New function names hopefully
make it clear that column major ordering is being used.

Fixes #21.
  • Loading branch information
bitshifter committed Oct 8, 2019
1 parent 6736a13 commit 4896c0f
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 209 deletions.
71 changes: 38 additions & 33 deletions src/f32/mat2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::ops::{Add, Mul, Sub};

#[inline]
pub fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 {
Mat2::new(x_axis, y_axis)
Mat2::from_cols(x_axis, y_axis)
}

/// A 2x2 column major matrix.
Expand All @@ -35,11 +35,47 @@ impl Mat2 {
Self(Vec4::new(1.0, 0.0, 0.0, 1.0))
}

#[deprecated(since = "0.7.2", note = "please use `Mat4::from_cols` instead")]
#[inline]
pub fn new(x_axis: Vec2, y_axis: Vec2) -> Self {
Self::from_cols(x_axis, y_axis)
}

/// Creates a new `Mat2` from four column vectors.
#[inline]
pub fn from_cols(x_axis: Vec2, y_axis: Vec2) -> Self {
Self(Vec4::new(x_axis.x(), x_axis.y(), y_axis.x(), y_axis.y()))
}

/// Creates a new `Mat2` from a `[f32; 4]` stored in column major order.
/// If your data is stored in row major you will need to `transpose` the resulting `Mat2`.
#[inline]
pub fn from_cols_array(m: &[f32; 4]) -> Self {
Mat2(Vec4::new(m[0], m[1], m[2], m[3]))
}

/// Creates a new `[f32; 4]` storing data in column major order.
/// If you require data in row major order `transpose` the `Mat2` first.
#[inline]
pub fn into_cols_array(&self) -> [f32; 4] {
self.0.into()
}

/// Creates a new `Mat2` from a `[[f32; 2]; 2]` stored in column major order.
/// If your data is in row major order you will need to `transpose` the resulting `Mat2`.
#[inline]
pub fn from_cols_array_2d(m: &[[f32; 2]; 2]) -> Self {
Mat2(Vec4::new(m[0][0], m[0][1], m[1][0], m[1][1]))
}

/// Creates a new `[[f32; 2]; 2]` storing data in column major order.
/// If you require data in row major order `transpose` the `Mat2` first.
#[inline]
pub fn into_cols_array_2d(&self) -> [[f32; 2]; 2] {
let (x0, y0, x1, y1) = self.0.into();
[[x0, y0], [x1, y1]]
}

/// Create a 2x2 matrix containing scale and rotation (in radians).
#[inline]
pub fn from_scale_angle(scale: Vec2, angle: f32) -> Self {
Expand Down Expand Up @@ -128,7 +164,7 @@ impl Mat2 {
pub fn mul_mat2(&self, rhs: &Self) -> Self {
// TODO: SSE2
let (x0, y0, x1, y1) = rhs.0.into();
Mat2::new(
Mat2::from_cols(
self.mul_vec2(Vec2::new(x0, y0)),
self.mul_vec2(Vec2::new(x1, y1)),
)
Expand Down Expand Up @@ -220,34 +256,3 @@ impl Mul<f32> for Mat2 {
self.mul_scalar(rhs)
}
}

impl From<[[f32; 2]; 2]> for Mat2 {
#[inline]
fn from(m: [[f32; 2]; 2]) -> Self {
Mat2(Vec4::new(m[0][0], m[0][1], m[1][0], m[1][1]))
}
}

impl From<Mat2> for [[f32; 2]; 2] {
#[inline]
fn from(m: Mat2) -> Self {
let (x0, y0, x1, y1) = m.0.into();
[[x0, y0], [x1, y1]]
}
}

impl From<[f32; 4]> for Mat2 {
#[inline]
/// Load from array in column major order.
fn from(m: [f32; 4]) -> Self {
Mat2(m.into())
}
}

impl From<Mat2> for [f32; 4] {
#[inline]
/// Store to array in column major order.
fn from(m: Mat2) -> Self {
m.0.into()
}
}
91 changes: 48 additions & 43 deletions src/f32/mat3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,62 @@ impl Mat3 {
}
}

#[deprecated(since = "0.7.2", note = "please use `Mat3::from_cols` instead")]
#[inline]
pub fn new(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
Self::from_cols(x_axis, y_axis, z_axis)
}

/// Creates a new `Mat3` from three column vectors.
#[inline]
pub fn from_cols(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
Self {
x_axis,
y_axis,
z_axis,
}
}

/// Create a 3x3 matrix that can scale, rotate and translate a 2D vector.
/// Creates a new `Mat3` from a `[f32; 9]` stored in column major order.
/// If your data is stored in row major you will need to `transpose` the resulting `Mat3`.
#[inline]
pub fn from_cols_array(m: &[f32; 9]) -> Self {
Mat3 {
x_axis: Vec3::new(m[0], m[1], m[2]),
y_axis: Vec3::new(m[3], m[4], m[5]),
z_axis: Vec3::new(m[6], m[7], m[8]),
}
}

/// Creates a new `[f32; 9]` storing data in column major order.
/// If you require data in row major order `transpose` the `Mat3` first.
#[inline]
pub fn into_cols_array(&self) -> [f32; 9] {
let (m00, m01, m02) = self.x_axis.into();
let (m10, m11, m12) = self.y_axis.into();
let (m20, m21, m22) = self.z_axis.into();
[m00, m01, m02, m10, m11, m12, m20, m21, m22]
}

/// Creates a new `Mat3` from a `[[f32; 3]; 3]` stored in column major order.
/// If your data is in row major order you will need to `transpose` the resulting `Mat3`.
#[inline]
pub fn from_cols_array_2d(m: &[[f32; 3]; 3]) -> Self {
Mat3 {
x_axis: m[0].into(),
y_axis: m[1].into(),
z_axis: m[2].into(),
}
}

/// Creates a new `[[f32; 3]; 3]` storing data in column major order.
/// If you require data in row major order `transpose` the `Mat3` first.
#[inline]
pub fn into_cols_array_2d(&self) -> [[f32; 3]; 3] {
[self.x_axis.into(), self.y_axis.into(), self.z_axis.into()]
}

/// Creates a new `Mat3` that can scale, rotate and translate a 2D vector.
/// `angle` is in radians.
#[inline]
pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self {
Expand Down Expand Up @@ -235,7 +281,7 @@ impl Mat3 {
glam_assert!(det.cmpne(Vec3::zero()).all());
let inv_det = det.reciprocal();
// TODO: Work out if it's possible to get rid of the transpose
Mat3::new(tmp0 * inv_det, tmp1 * inv_det, tmp2 * inv_det).transpose()
Mat3::from_cols(tmp0 * inv_det, tmp1 * inv_det, tmp2 * inv_det).transpose()
}

#[inline]
Expand Down Expand Up @@ -352,44 +398,3 @@ impl Mul<f32> for Mat3 {
self.mul_scalar(rhs)
}
}

impl From<[[f32; 3]; 3]> for Mat3 {
#[inline]
fn from(m: [[f32; 3]; 3]) -> Self {
Mat3 {
x_axis: m[0].into(),
y_axis: m[1].into(),
z_axis: m[2].into(),
}
}
}

impl From<Mat3> for [[f32; 3]; 3] {
#[inline]
fn from(m: Mat3) -> Self {
[m.x_axis.into(), m.y_axis.into(), m.z_axis.into()]
}
}

impl From<[f32; 9]> for Mat3 {
#[inline]
/// Load from array in column major order.
fn from(m: [f32; 9]) -> Self {
Mat3 {
x_axis: Vec3::new(m[0], m[1], m[2]),
y_axis: Vec3::new(m[3], m[4], m[5]),
z_axis: Vec3::new(m[6], m[7], m[8]),
}
}
}

impl From<Mat3> for [f32; 9] {
#[inline]
/// Store to array in column major order.
fn from(m: Mat3) -> Self {
let (m00, m01, m02) = m.x_axis.into();
let (m10, m11, m12) = m.y_axis.into();
let (m20, m21, m22) = m.z_axis.into();
[m00, m01, m02, m10, m11, m12, m20, m21, m22]
}
}
Loading

0 comments on commit 4896c0f

Please sign in to comment.