From 808a17fc22b27ed1278b05fc15563bf8ea91757d Mon Sep 17 00:00:00 2001 From: Mark Hintz Date: Thu, 28 Apr 2016 01:03:33 +0200 Subject: [PATCH] implement from_angle{x, y, z} and from_axis_angle for Matrix4 All are duplicates of the same functions for Matrix3, with extra parameters added in to make up the full 4x4 --- src/matrix.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/matrix.rs b/src/matrix.rs index d405847..a3fad3f 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -264,6 +264,62 @@ impl Matrix4 { s.z.clone(), u.z.clone(), -f.z.clone(), S::zero(), -eye.dot(s), -eye.dot(u), eye.dot(f), S::one()) } + + /// Create a homogeneous transformation matrix from a rotation around the `x` axis (pitch). + pub fn from_angle_x(theta: Rad) -> Matrix4 { + // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations + let (s, c) = Rad::sin_cos(theta); + Matrix4::new(S::one(), S::zero(), S::zero(), S::zero(), + S::zero(), c, s, S::zero(), + S::zero(), -s, c, S::zero(), + S::zero(), S::zero(), S::zero(), S::one()) + } + + /// Create a homogeneous transformation matrix from a rotation around the `y` axis (yaw). + pub fn from_angle_y(theta: Rad) -> Matrix4 { + // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations + let (s, c) = Rad::sin_cos(theta); + Matrix4::new(c, S::zero(), -s, S::zero(), + S::zero(), S::one(), S::zero(), S::zero(), + s, S::zero(), c, S::zero(), + S::zero(), S::zero(), S::zero(), S::one()) + } + + /// Create a homogeneous transformation matrix from a rotation around the `z` axis (roll). + pub fn from_angle_z(theta: Rad) -> Matrix4 { + // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations + let (s, c) = Rad::sin_cos(theta); + Matrix4::new( c, s, S::zero(), S::zero(), + -s, c, S::zero(), S::zero(), + S::zero(), S::zero(), S::one(), S::zero(), + S::zero(), S::zero(), S::zero(), S::one()) + } + + /// Create a homogeneous transformation matrix from an angle around an arbitrary axis. + pub fn from_axis_angle(axis: Vector3, angle: Rad) -> Matrix4 { + let (s, c) = Rad::sin_cos(angle); + let _1subc = S::one() - c; + + Matrix4::new(_1subc * axis.x * axis.x + c, + _1subc * axis.x * axis.y + s * axis.z, + _1subc * axis.x * axis.z - s * axis.y, + S::zero(), + + _1subc * axis.x * axis.y - s * axis.z, + _1subc * axis.y * axis.y + c, + _1subc * axis.y * axis.z + s * axis.x, + S::zero(), + + _1subc * axis.x * axis.z + s * axis.y, + _1subc * axis.y * axis.z - s * axis.x, + _1subc * axis.z * axis.z + c, + S::zero(), + + S::zero(), + S::zero(), + S::zero(), + S::one()) + } } impl Zero for Matrix2 {