Skip to content

Commit

Permalink
Created explicit from/to conversion functions.
Browse files Browse the repository at this point in the history
Still not sure about naming.
  • Loading branch information
bitshifter committed Oct 7, 2019
1 parent 5b69980 commit 6736a13
Showing 1 changed file with 62 additions and 25 deletions.
87 changes: 62 additions & 25 deletions src/f32/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Mat4 {
}
}

/// Create a new homogenous `Mat4` containing a rotation around a normalized rotation axis of
/// Create a new `Mat4` containing a rotation around a normalized rotation axis of
/// angle (in radians).
#[inline]
pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
Expand All @@ -159,15 +159,15 @@ impl Mat4 {
}
}

/// Create a new homogenous `Mat4` containing a rotation around the given euler angles
/// Create a new `Mat4` containing a rotation around the given euler angles
/// (in radians).
#[inline]
pub fn from_rotation_ypr(yaw: f32, pitch: f32, roll: f32) -> Self {
let quat = Quat::from_rotation_ypr(yaw, pitch, roll);
Self::from_quat(quat)
}

/// Create a new homogenous `Mat4` containing a rotation around the x axis of angle
/// Create a new `Mat4` containing a rotation around the x axis of angle
/// (in radians).
#[inline]
pub fn from_rotation_x(angle: f32) -> Self {
Expand All @@ -180,7 +180,7 @@ impl Mat4 {
}
}

/// Create a new homogenous `Mat4` containing a rotation around the y axis of angle
/// Create a new `Mat4` containing a rotation around the y axis of angle
/// (in radians).
#[inline]
pub fn from_rotation_y(angle: f32) -> Self {
Expand All @@ -193,7 +193,7 @@ impl Mat4 {
}
}

/// Create a new homogenous `Mat4` containing a rotation around the z axis of angle
/// Create a new `Mat4` containing a rotation around the z axis of angle
/// (in radians).
#[inline]
pub fn from_rotation_z(angle: f32) -> Self {
Expand All @@ -218,6 +218,43 @@ impl Mat4 {
}
}

#[inline]
pub fn from_array_as_cols(m: [f32; 16]) -> Self {
Mat4 {
x_axis: Vec4::new(m[0], m[1], m[2], m[3]),
y_axis: Vec4::new(m[4], m[5], m[6], m[7]),
z_axis: Vec4::new(m[8], m[9], m[10], m[11]),
w_axis: Vec4::new(m[12], m[13], m[14], m[15]),
}
}

#[inline]
pub fn to_array_as_cols(&self) -> [f32; 16] {
*self.as_ref()
}

/// Create a new `Mat4` from a 4x4 array of f32.
/// Array rows are treated as columns.
#[inline]
pub fn from_arrays_as_cols(m: [[f32; 4]; 4]) -> Self {
Mat4 {
x_axis: m[0].into(),
y_axis: m[1].into(),
z_axis: m[2].into(),
w_axis: m[3].into(),
}
}

#[inline]
pub fn to_arrays_as_cols(&self) -> [[f32; 4]; 4] {
[
self.x_axis.into(),
self.y_axis.into(),
self.z_axis.into(),
self.w_axis.into(),
]
}

#[inline]
pub fn set_x_axis(&mut self, x: Vec4) {
self.x_axis = x;
Expand Down Expand Up @@ -606,45 +643,45 @@ impl Mul<f32> for Mat4 {
}
}

#[deprecated(
since = "0.7.2",
note = "please use `Mat4::from_arrays_as_cols` instead"
)]
impl From<[[f32; 4]; 4]> for Mat4 {
#[inline]
fn from(m: [[f32; 4]; 4]) -> Self {
Mat4 {
x_axis: m[0].into(),
y_axis: m[1].into(),
z_axis: m[2].into(),
w_axis: m[3].into(),
}
Mat4::from_arrays_as_cols(m)
}
}

#[deprecated(
since = "0.7.2",
note = "please use `Mat4::to_arrays_as_cols` instead"
)]
impl From<Mat4> for [[f32; 4]; 4] {
#[inline]
fn from(m: Mat4) -> Self {
[
m.x_axis.into(),
m.y_axis.into(),
m.z_axis.into(),
m.w_axis.into(),
]
m.to_arrays_as_cols()
}
}

#[deprecated(
since = "0.7.2",
note = "please use `Mat4::from_array_as_cols` instead"
)]
impl From<[f32; 16]> for Mat4 {
#[inline]
fn from(m: [f32; 16]) -> Self {
Mat4 {
x_axis: Vec4::new(m[0], m[1], m[2], m[3]),
y_axis: Vec4::new(m[4], m[5], m[6], m[7]),
z_axis: Vec4::new(m[8], m[9], m[10], m[11]),
w_axis: Vec4::new(m[12], m[13], m[14], m[15]),
}
Mat4::from_array_as_cols(m)
}
}

#[deprecated(
since = "0.7.2",
note = "please use `Mat4::to_array_as_cols` instead"
)]
impl From<Mat4> for [f32; 16] {
#[inline]
fn from(m: Mat4) -> Self {
*m.as_ref()
m.to_array_as_cols()
}
}

0 comments on commit 6736a13

Please sign in to comment.