From 98cf1a8d17dfa319105b70ef20af2433d00b6953 Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Thu, 20 Sep 2018 22:12:26 +0200 Subject: [PATCH] Implement more gtx functions. --- nalgebra-glm/src/gtx_rotate_vector.rs | 30 +++--- nalgebra-glm/src/gtx_transform2.rs | 136 ++++++++++++++++++++++++-- nalgebra-glm/src/gtx_transform2d.rs | 45 ++++++++- nalgebra-glm/src/gtx_vector_angle.rs | 21 +++- 4 files changed, 203 insertions(+), 29 deletions(-) diff --git a/nalgebra-glm/src/gtx_rotate_vector.rs b/nalgebra-glm/src/gtx_rotate_vector.rs index 2202ba00..666190f7 100644 --- a/nalgebra-glm/src/gtx_rotate_vector.rs +++ b/nalgebra-glm/src/gtx_rotate_vector.rs @@ -1,43 +1,49 @@ -use na::{Real, U2, U3, U4}; +use na::{Real, U2, U3, U4, Rotation3, Vector3, Unit, UnitComplex}; -pub fn orientation(Normal: &Vec, Up: &Vec) -> Mat { - unimplemented!() +use aliases::{Vec, Mat}; + +pub fn orientation(normal: &Vec, up: &Vec) -> Mat { + if let Some(r) = Rotation3::rotation_between(normal, up) { + r.to_homogeneous() + } else { + Mat::::identity() + } } pub fn rotate2(v: &Vec, angle: N) -> Vec { - unimplemented!() + UnitComplex::new(angle) * v } pub fn rotate(v: &Vec, angle: N, normal: &Vec) -> Vec { - unimplemented!() + Rotation3::from_axis_angle(&Unit::new_normalize(*normal), angle) * v } pub fn rotate4(v: &Vec, angle: N, normal: &Vec) -> Vec { - unimplemented!() + Rotation3::from_axis_angle(&Unit::new_normalize(*normal), angle).to_homogeneous() * v } pub fn rotateX(v: &Vec, angle: N) -> Vec { - unimplemented!() + Rotation3::from_axis_angle(&Vector3::x_axis(), angle) * v } pub fn rotateX4(v: &Vec, angle: N) -> Vec { - unimplemented!() + Rotation3::from_axis_angle(&Vector3::x_axis(), angle).to_homogeneous() * v } pub fn rotateY(v: &Vec, angle: N) -> Vec { - unimplemented!() + Rotation3::from_axis_angle(&Vector3::y_axis(), angle) * v } pub fn rotateY4(v: &Vec, angle: N) -> Vec { - unimplemented!() + Rotation3::from_axis_angle(&Vector3::y_axis(), angle).to_homogeneous() * v } pub fn rotateZ(v: &Vec, angle: N) -> Vec { - unimplemented!() + Rotation3::from_axis_angle(&Vector3::z_axis(), angle) * v } pub fn rotateZ4(v: &Vec, angle: N) -> Vec { - unimplemented!() + Rotation3::from_axis_angle(&Vector3::z_axis(), angle).to_homogeneous() * v } pub fn slerp(x: &Vec, y: &Vec, a: N) -> Vec { diff --git a/nalgebra-glm/src/gtx_transform2.rs b/nalgebra-glm/src/gtx_transform2.rs index 0b1789a3..3d3477cd 100644 --- a/nalgebra-glm/src/gtx_transform2.rs +++ b/nalgebra-glm/src/gtx_transform2.rs @@ -1,9 +1,127 @@ -pub fn mat< 3, 3, T, Q > proj2D (mat< 3, 3, T, Q > const &m, vec< 3, T, Q > const &normal) -pub fn mat< 4, 4, T, Q > proj3D (mat< 4, 4, T, Q > const &m, vec< 3, T, Q > const &normal) -pub fn mat< 4, 4, T, Q > scaleBias (T scale, T bias) -pub fn mat< 4, 4, T, Q > scaleBias (mat< 4, 4, T, Q > const &m, T scale, T bias) -pub fn mat< 3, 3, T, Q > shearX2D (mat< 3, 3, T, Q > const &m, T y) -pub fn mat< 4, 4, T, Q > shearX3D (mat< 4, 4, T, Q > const &m, T y, T z) -pub fn mat< 3, 3, T, Q > shearY2D (mat< 3, 3, T, Q > const &m, T x) -pub fn mat< 4, 4, T, Q > shearY3D (mat< 4, 4, T, Q > const &m, T x, T z) -pub fn mat< 4, 4, T, Q > shearZ3D (mat< 4, 4, T, Q > const &m, T x, T y) \ No newline at end of file +use na::{self, U2, U3, U4, Matrix3, Matrix4}; + +use traits::Number; +use aliases::{Mat, Vec}; + +pub fn proj2D(m: &Mat, normal: &Vec) -> Mat { + let mut res = Matrix3::identity(); + + { + let mut part = res.fixed_slice_mut::(0, 0); + part -= normal * normal.transpose(); + } + + res +} + +pub fn proj3D(m: &Mat, normal: &Vec) -> Mat { + let mut res = Matrix4::identity(); + + { + let mut part = res.fixed_slice_mut::(0, 0); + part -= normal * normal.transpose(); + } + + res +} + +pub fn reflect2D(m: &Mat, normal: &Vec) -> Mat { + let mut res = Matrix3::identity(); + + { + let mut part = res.fixed_slice_mut::(0, 0); + part -= (normal * N::from_f64(2.0).unwrap()) * normal.transpose(); + } + + res +} + +pub fn reflect3D(m: &Mat, normal: &Vec) -> Mat { + let mut res = Matrix4::identity(); + + { + let mut part = res.fixed_slice_mut::(0, 0); + part -= (normal * N::from_f64(2.0).unwrap()) * normal.transpose(); + } + + res +} + +pub fn scaleBias(scale: N, bias: N) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + + Matrix4::new( + scale, _0, _0, bias, + _0, scale, _0, bias, + _0, _0, scale, bias, + _0, _0, _0, _1, + ) +} + +pub fn scaleBias2(m: &Mat, scale: N, bias: N) -> Mat { + m * scaleBias(scale, bias) +} + +pub fn shearX2D(m: &Mat, y: N) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + + let shear = Matrix3::new( + _1, y, _0, + _0, _1, _0, + _0, _0, _1 + ); + m * shear +} + +pub fn shearX3D(m: &Mat, y: N, z: N) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + let shear = Matrix4::new( + _1, _0, _0, _0, + y, _1, _0, _0, + z, _0, _1, _0, + _0, _0, _0, _1, + ); + + m * shear +} + +pub fn shearY2D(m: &Mat, x: N) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + + let shear = Matrix3::new( + _1, _0, _0, + x, _1, _0, + _0, _0, _1 + ); + m * shear +} + +pub fn shearY3D(m: &Mat, x: N, z: N) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + let shear = Matrix4::new( + _1, x, _0, _0, + _0, _1, _0, _0, + _0, z, _1, _0, + _0, _0, _0, _1, + ); + + m * shear +} + +pub fn shearZ3D(m: &Mat, x: N, y: N) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + let shear = Matrix4::new( + _1, _0, x, _0, + _0, _1, y, _0, + _0, _0, _1, _0, + _0, _0, _0, _1, + ); + + m * shear +} diff --git a/nalgebra-glm/src/gtx_transform2d.rs b/nalgebra-glm/src/gtx_transform2d.rs index 4330564a..b04a8275 100644 --- a/nalgebra-glm/src/gtx_transform2d.rs +++ b/nalgebra-glm/src/gtx_transform2d.rs @@ -1,5 +1,40 @@ -pub fn mat< 3, 3, T, Q > rotate (mat< 3, 3, T, Q > const &m, T angle) -pub fn mat< 3, 3, T, Q > scale (mat< 3, 3, T, Q > const &m, vec< 2, T, Q > const &v) -pub fn mat< 3, 3, T, Q > shearX (mat< 3, 3, T, Q > const &m, T y) -pub fn mat< 3, 3, T, Q > shearY (mat< 3, 3, T, Q > const &m, T x) -pub fn mat< 3, 3, T, Q > translate (mat< 3, 3, T, Q > const &m, vec< 2, T, Q > const &v) \ No newline at end of file +use na::{Real, U2, U3, UnitComplex, Matrix3}; + +use traits::Number; +use aliases::{Mat, Vec}; + +pub fn rotate(m: &Mat, angle: N) -> Mat { + m * UnitComplex::new(angle).to_homogeneous() +} + +pub fn scale(m: &Mat, v: &Vec) -> Mat { + m.prepend_nonuniform_scaling(v) +} + +pub fn shearX(m: &Mat, y: N) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + + let shear = Matrix3::new( + _1, y, _0, + _0, _1, _0, + _0, _0, _1 + ); + m * shear +} + +pub fn shearY(m: &Mat, x: N) -> Mat { + let _0 = N::zero(); + let _1 = N::one(); + + let shear = Matrix3::new( + _1, _0, _0, + x, _1, _0, + _0, _0, _1 + ); + m * shear +} + +pub fn translate(m: &Mat, v: &Vec) -> Mat { + m.prepend_translation(v) +} diff --git a/nalgebra-glm/src/gtx_vector_angle.rs b/nalgebra-glm/src/gtx_vector_angle.rs index da0d5a44..74e7ca38 100644 --- a/nalgebra-glm/src/gtx_vector_angle.rs +++ b/nalgebra-glm/src/gtx_vector_angle.rs @@ -1,3 +1,18 @@ -pub fn T angle (vec< L, T, Q > const &x, vec< L, T, Q > const &y) -pub fn T orientedAngle (vec< 2, T, Q > const &x, vec< 2, T, Q > const &y) -pub fn T orientedAngle (vec< 3, T, Q > const &x, vec< 3, T, Q > const &y, vec< 3, T, Q > const &ref) \ No newline at end of file +use na::{self, DefaultAllocator, Real, U2, U3}; + +use traits::{Dimension, Alloc}; +use aliases::Vec; + + +pub fn angle(x: &Vec, y: &Vec) -> N + where DefaultAllocator: Alloc { + x.angle(y) +} + +pub fn oriented_angle(x: &Vec, y: &Vec) -> N { + unimplemented!() +} + +pub fn oriented_angle_ref(x: &Vec, y: &Vec, refv: &Vec) -> N { + unimplemented!() +}