diff --git a/nalgebra-glm/README b/nalgebra-glm/README index d76c97df..4d04221a 100644 --- a/nalgebra-glm/README +++ b/nalgebra-glm/README @@ -3,4 +3,7 @@ * not_ -> not * equal overload with epsilon -> equal_epsilon * ortho(l, r, b, t) -> infinite_ortho -* tweaked_infinite_perspective(fovy, aspect, near, ep) -> tweaked_infinite_perspective_ep \ No newline at end of file +* tweaked_infinite_perspective(fovy, aspect, near, ep) -> tweaked_infinite_perspective_ep +* Function overload: the vec version is suffixed by _vec +* Function overload: the methods taking an epsilon as suffixed by _eps +* L1Norm and L2Norm between two vectors have been renamed: l1_distance, l2_distance \ No newline at end of file diff --git a/nalgebra-glm/src/common.rs b/nalgebra-glm/src/common.rs index 35ed1d58..88deed62 100644 --- a/nalgebra-glm/src/common.rs +++ b/nalgebra-glm/src/common.rs @@ -1,209 +1,202 @@ -use na::{Scalar, DefaultAllocator}; +use std::mem; +use num::FromPrimitive; +use na::{self, Scalar, Real, DefaultAllocator}; -use aliases::Vec; -use traits::{Dimension, Alloc}; +use aliases::{Vec, Mat}; +use traits::{Number, Dimension, Alloc}; -pub fn abs(x: T) -> T { - unimplemented!() - +pub fn abs(x: &Mat) -> Mat + where DefaultAllocator: Alloc { + x.abs() } -pub fn abs2(x: &Vec) -> Vec +pub fn ceil(x: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + x.map(|x| x.ceil()) } -pub fn ceil(x: &Vec) -> Vec +pub fn clamp(x: N, minVal: N, maxVal: N) -> N { + na::clamp(x, minVal, maxVal) +} + +pub fn clamp2(x: &Vec,minVal: N, maxVal: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + x.map(|x| na::clamp(x, minVal, maxVal)) } -pub fn clamp(x: T, minVal: T, maxVal: T) -> T { - unimplemented!() - -} - -pub fn clamp2(x: &Vec,minVal: N, maxVal: N) -> Vec +pub fn clamp3(x: &Vec, minVal: &Vec, maxVal: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + na::clamp(x.clone(), minVal.clone(), maxVal.clone()) } -pub fn clamp3(x: &Vec, minVal: &Vec, maxVal: &Vec) -> Vec - where DefaultAllocator: Alloc { - unimplemented!() - +pub fn float_bits_to_int(v: f32) -> i32 { + unsafe { mem::transmute(v) } } -pub fn floatBitsToInt(v: f32) -> i32 { - unimplemented!() - -} - -pub fn floatBitsToInt2(v: Vec) -> Vec +pub fn float_bits_to_int_vec(v: Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + v.map(|v| float_bits_to_int(v)) } -pub fn floatBitsToUint(v: f32) -> u32 { - unimplemented!() - +pub fn float_bits_to_uint(v: f32) -> u32 { + unsafe { mem::transmute(v) } } -pub fn floatBitsToUint2(v: &Vec) -> Vec +pub fn float_bits_to_uint_vec(v: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + v.map(|v| float_bits_to_uint(v)) } -pub fn floor(x: &Vec) -> Vec + +pub fn floor(x: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - -} -pub fn fma(a: T, b: T, c: T) -> T { - unimplemented!() - + x.map(|x| x.floor()) } -pub fn fract(x: T) -> T { - unimplemented!() - +// FIXME: should be implemented for Vec/Mat? +pub fn fma(a: N, b: N, c: N) -> N { + // FIXME: use an actual FMA + a * b + c } -pub fn fract2(x: &Vec) -> Vec +pub fn fract(x: N) -> N { + x.fract() +} + +pub fn fract2(x: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + x.map(|x| x.fract()) +} + +//// FIXME: should be implemented for Vec/Mat? +///// Returns the (significant, exponent) of this float number. +//pub fn frexp(x: N, exp: N) -> (N, N) { +// // FIXME: is there a better approach? +// let e = x.log2().ceil(); +// (x * (-e).exp2(), e) +//} + +pub fn int_bits_to_float(v: i32) -> f32 { + unsafe { mem::transmute(v) } } -pub fn frexp(x: T, exp: I) -> T { - unimplemented!() - -} - -pub fn intBitsToFloat(v: i32) -> f32 { - unimplemented!() - -} - -pub fn intBitsToFloat2(v: &Vec) -> Vec +pub fn int_bits_to_float_vec(v: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + v.map(|v| int_bits_to_float(v)) } -pub fn isinf(x: &Vec) -> Vec +//pub fn isinf(x: &Vec) -> Vec +// where DefaultAllocator: Alloc { +// unimplemented!() +// +//} +// +//pub fn isnan(x: &Vec) -> Vec +// where DefaultAllocator: Alloc { +// unimplemented!() +// +//} + +///// Returns the (significant, exponent) of this float number. +//pub fn ldexp(x: N, exp: N) -> N { +// // FIXME: is there a better approach? +// x * (exp).exp2() +//} + +pub fn max(x: N, y: N) -> N { + na::sup(&x, &y) +} + +pub fn max2(x: &Vec, y: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + x.map(|x| na::sup(&x, &y)) } -pub fn isnan(x: &Vec) -> Vec +pub fn max3(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + na::sup(x, y) } -pub fn ldexp(x: T, exp: I) -> T { - unimplemented!() - +pub fn min(x: N, y: N) -> N { + na::inf(&x, &y) } -pub fn max(x: T, y: T) -> T { - unimplemented!() - -} - -pub fn max2(x: &Vec,y: N) -> Vec +pub fn min2(x: &Vec,y: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + x.map(|x| na::inf(&x, &y)) } -pub fn max3(x: &Vec, y: &Vec) -> Vec + +pub fn min3(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - -} -pub fn min(x: T, y: T) -> T { - unimplemented!() - + na::inf(x, y) } -pub fn min2(x: &Vec,y: N) -> Vec +pub fn mix(x: N, y: N, a: N) -> N { + x * (N::one() - a) + y * a +} + +pub fn mod_(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + x.zip_map(y, |x, y| x % y) } -pub fn min3(x: &Vec, y: &Vec) -> Vec + +pub fn modf(x: N, i: N) -> N { + x % i +} + +pub fn round(x: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - -} -pub fn mix(x: T, y: T, a: T) -> T { - unimplemented!() + x.map(|x| x.round()) } -pub fn mod_(x: &Vec, y: &Vec) -> Vec +//pub fn roundEven(x: &Vec) -> Vec +// where DefaultAllocator: Alloc { +// unimplemented!() +//} + +pub fn sign(x: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - -} -pub fn modf(x: T, i: &T) -> T { - unimplemented!() - + x.map(|x| x.signum()) } -pub fn round(x: &Vec) -> Vec +pub fn smoothstep(edge0: N, edge1: N, x: N) -> N { + let _3: N = FromPrimitive::from_f64(3.0).unwrap(); + let _2: N = FromPrimitive::from_f64(2.0).unwrap(); + let t = na::clamp((x - edge0) / (edge1 - edge0), N::zero(), N::one()); + t * t * (_3 - t * _2) +} + +pub fn step(edge: N, x: N) -> N { + if edge > x { + N::zero() + } else { + N::one() + } +} + +pub fn step2(edge: N, x: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + x.map(|x| step(edge, x)) } -pub fn roundEven(x: &Vec) -> Vec + +pub fn step3(edge: &Vec, x: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() - + edge.zip_map(x, |edge, x| step(edge, x)) } -pub fn sign(x: &Vec) -> Vec + +pub fn trunc(x: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + x.map(|x| x.trunc()) +} + +pub fn uint_bits_to_float(v: u32) -> f32 { + unsafe { mem::transmute(v) } } -pub fn smoothstep(edge0: T, edge1: T, x: T) -> T { - unimplemented!() - -} - -pub fn step(edge: T, x: T) -> T { - unimplemented!() - -} - -pub fn step2(edge: N, x: &Vec) -> Vec - where DefaultAllocator: Alloc { - unimplemented!() - -} -pub fn step3(edge: &Vec, x: &Vec) -> Vec - where DefaultAllocator: Alloc { - unimplemented!() - -} -pub fn trunc(x: &Vec) -> Vec - where DefaultAllocator: Alloc { - unimplemented!() - -} -pub fn uintBitsToFloat(v: u32) -> f32 { - unimplemented!() - -} -pub fn uintBitsToFloat2(v: &Vec) -> Vec +pub fn uint_bits_to_float_vec(v: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + v.map(|v| uint_bits_to_float(v)) } \ No newline at end of file diff --git a/nalgebra-glm/src/ext_matrix_projection.rs b/nalgebra-glm/src/ext_matrix_projection.rs index 6aa40f57..5d2a942a 100644 --- a/nalgebra-glm/src/ext_matrix_projection.rs +++ b/nalgebra-glm/src/ext_matrix_projection.rs @@ -1,32 +1,67 @@ -use na::{Real, U2, U3, U4}; +use na::{self, Real, U2, U3, U4, Vector3, Vector4, Matrix4}; use aliases::{Mat, Vec}; -pub fn pickMatrix(center: &Vec, delta: &Vec, viewport: &Vec) -> Mat { - unimplemented!() +pub fn pick_matrix(center: &Vec, delta: &Vec, viewport: &Vec) -> Mat { + let shift = Vector3::new( + (viewport.z - (center.x - viewport.x) * na::convert(2.0)) / delta.x, + (viewport.w - (center.y - viewport.y) * na::convert(2.0)) / delta.y, + N::zero() + ); + + let result = Matrix4::new_translation(&shift); + result.prepend_nonuniform_scaling(&Vector3::new(viewport.z / delta.x, viewport.w / delta.y, N::one())) } pub fn project(obj: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { - unimplemented!() + project_no(obj, model, proj, viewport) } -pub fn projectNO(obj: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { - unimplemented!() +pub fn project_no(obj: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { + let proj = project_zo(obj, model, proj, viewport); + Vector3::new(proj.x, proj.y, proj.z * na::convert(0.5) + na::convert(0.5)) } -pub fn projectZO(obj: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { - unimplemented!() +pub fn project_zo(obj: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { + let mut normalized = proj * model * Vector4::new(obj.x, obj.y, obj.z, N::one()); + let scale = N::one() / normalized.w; + + Vector3::new( + viewport.x + (viewport.z * (normalized.x * scale + N::one()) * na::convert(0.5)), + viewport.y + (viewport.w * (normalized.y * scale + N::one()) * na::convert(0.5)), + normalized.z * scale, + ) } -pub fn unProject(win: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { - unimplemented!() +pub fn unproject(win: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { + unproject_no(win, model, proj, viewport) } -pub fn unProjectNO(win: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { - unimplemented!() +pub fn unproject_no(win: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { + let _2: N = na::convert(2.0); + let transform = (proj * model).try_inverse().unwrap_or(Matrix4::zeros()); + let pt = Vector4::new( + _2 * (win.x - viewport.x) / viewport.z - N::one(), + _2 * (win.y - viewport.y) / viewport.w - N::one(), + _2 * win.z - N::one(), + N::one(), + ); + + let result = transform * pt; + result.fixed_rows::(0) / result.w } -pub fn unProjectZO(win: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { - unimplemented!() +pub fn unproject_zo(win: &Vec, model: &Mat, proj: &Mat, viewport: Vec) -> Vec { + let _2: N = na::convert(2.0); + let transform = (proj * model).try_inverse().unwrap_or(Matrix4::zeros()); + let pt = Vector4::new( + _2 * (win.x - viewport.x) / viewport.z - N::one(), + _2 * (win.y - viewport.y) / viewport.w - N::one(), + win.z, + N::one(), + ); + + let result = transform * pt; + result.fixed_rows::(0) / result.w } \ No newline at end of file diff --git a/nalgebra-glm/src/ext_matrix_transform.rs b/nalgebra-glm/src/ext_matrix_transform.rs index a9d210a5..6dff5ccc 100644 --- a/nalgebra-glm/src/ext_matrix_transform.rs +++ b/nalgebra-glm/src/ext_matrix_transform.rs @@ -26,9 +26,9 @@ pub fn rotate(m: &Mat, angle: N, axis: &Vec) -> Mat(m: &Mat, v: &Vec) -> Mat { - m.append_nonuniform_scaling(v) + m.prepend_nonuniform_scaling(v) } pub fn translate(m: &Mat, v: &Vec) -> Mat { - m.append_translation(v) + m.prepend_translation(v) } diff --git a/nalgebra-glm/src/ext_scalar_common.rs b/nalgebra-glm/src/ext_scalar_common.rs index b73637e6..17119d7e 100644 --- a/nalgebra-glm/src/ext_scalar_common.rs +++ b/nalgebra-glm/src/ext_scalar_common.rs @@ -1,41 +1,19 @@ -use na::Scalar; +use na; -pub fn fmax(a: N, b: N) -> N { - unimplemented!() +use traits::Number; + +pub fn max3(a: N, b: N, c: N) -> N { + na::sup(&na::sup(&a, &b), &c) } -pub fn fmax3(a: N, b: N, C: N) -> N { - unimplemented!() +pub fn max4(a: N, b: N, c: N, d: N) -> N { + na::sup(&na::sup(&a, &b), &na::sup(&c, &d)) } -pub fn fmax4(a: N, b: N, C: N, D: N) -> N { - unimplemented!() +pub fn min3(a: N, b: N, c: N) -> N { + na::inf(&na::inf(&a, &b), &c) } -pub fn fmin(a: N, b: N) -> N { - unimplemented!() -} - -pub fn fmin3(a: N, b: N, c: N) -> N { - unimplemented!() -} - -pub fn fmin4(a: N, b: N, c: N, d: N) -> N { - unimplemented!() -} - -pub fn max3(a: N, b: N, c: N) -> N { - unimplemented!() -} - -pub fn max4(a: N, b: N, c: N, d: N) -> N { - unimplemented!() -} - -pub fn min3(a: N, b: N, c: N) -> N { - unimplemented!() -} - -pub fn min4(a: N, b: N, c: N, d: N) -> N { - unimplemented!() -} +pub fn min4(a: N, b: N, c: N, d: N) -> N { + na::inf(&na::inf(&a, &b), &na::inf(&c, &d)) +} \ No newline at end of file diff --git a/nalgebra-glm/src/gtc_integer.rs b/nalgebra-glm/src/gtc_integer.rs index 0531f01a..383316fd 100644 --- a/nalgebra-glm/src/gtc_integer.rs +++ b/nalgebra-glm/src/gtc_integer.rs @@ -1,18 +1,18 @@ -use na::{Scalar, DefaultAllocator}; +//use na::{Scalar, DefaultAllocator}; +// +//use traits::{Alloc, Dimension}; +//use aliases::Vec; -use traits::{Alloc, Dimension}; -use aliases::Vec; - -pub fn iround(x: &Vec) -> Vec - where DefaultAllocator: Alloc { - unimplemented!() -} - -pub fn log2(x: I) -> I { - unimplemented!() -} - -pub fn uround(x: &Vec) -> Vec - where DefaultAllocator: Alloc { - unimplemented!() -} \ No newline at end of file +//pub fn iround(x: &Vec) -> Vec +// where DefaultAllocator: Alloc { +// x.map(|x| x.round()) +//} +// +//pub fn log2(x: I) -> I { +// unimplemented!() +//} +// +//pub fn uround(x: &Vec) -> Vec +// where DefaultAllocator: Alloc { +// unimplemented!() +//} \ No newline at end of file diff --git a/nalgebra-glm/src/gtx_component_wise.rs b/nalgebra-glm/src/gtx_component_wise.rs new file mode 100644 index 00000000..9d8a74b1 --- /dev/null +++ b/nalgebra-glm/src/gtx_component_wise.rs @@ -0,0 +1,28 @@ +use na::{self, Scalar, Real, DefaultAllocator}; + +use traits::{Number, Alloc, Dimension}; +use aliases::Mat; + + +pub fn comp_add(m: &Mat) -> N + where DefaultAllocator: Alloc { + m.iter().fold(N::zero(), |x, y| x + *y) +} + +pub fn comp_max(m: &Mat) -> N + where DefaultAllocator: Alloc { + m.iter().fold(N::min_value(), |x, y| na::sup(&x, y)) +} + +pub fn comp_min(m: &Mat) -> N + where DefaultAllocator: Alloc { + m.iter().fold(N::max_value(), |x, y| na::inf(&x, y)) +} + +pub fn comp_mul(m: &Mat) -> N + where DefaultAllocator: Alloc { + m.iter().fold(N::one(), |x, y| x * *y) +} + +//pub fn vec< L, floatType, Q > compNormalize (vec< L, T, Q > const &v) +//pub fn vec< L, T, Q > compScale (vec< L, floatType, Q > const &v) \ No newline at end of file diff --git a/nalgebra-glm/src/gtx_euler_angles.rs b/nalgebra-glm/src/gtx_euler_angles.rs new file mode 100644 index 00000000..f42a941a --- /dev/null +++ b/nalgebra-glm/src/gtx_euler_angles.rs @@ -0,0 +1,41 @@ +pub fn mat< 4, 4, T, defaultp > derivedEulerAngleX (T const &angleX, T const &angularVelocityX) +pub fn mat< 4, 4, T, defaultp > derivedEulerAngleY (T const &angleY, T const &angularVelocityY) +pub fn mat< 4, 4, T, defaultp > derivedEulerAngleZ (T const &angleZ, T const &angularVelocityZ) +pub fn mat< 4, 4, T, defaultp > eulerAngleX (T const &angleX) +pub fn mat< 4, 4, T, defaultp > eulerAngleXY (T const &angleX, T const &angleY) +pub fn mat< 4, 4, T, defaultp > eulerAngleXYX (T const &t1, T const &t2, T const &t3) +pub fn mat< 4, 4, T, defaultp > eulerAngleXYZ (T const &t1, T const &t2, T const &t3) +pub fn mat< 4, 4, T, defaultp > eulerAngleXZ (T const &angleX, T const &angleZ) +pub fn mat< 4, 4, T, defaultp > eulerAngleXZX (T const &t1, T const &t2, T const &t3) +pub fn mat< 4, 4, T, defaultp > eulerAngleXZY (T const &t1, T const &t2, T const &t3) +pub fn mat< 4, 4, T, defaultp > eulerAngleY (T const &angleY) +pub fn mat< 4, 4, T, defaultp > eulerAngleYX (T const &angleY, T const &angleX) +pub fn mat< 4, 4, T, defaultp > eulerAngleYXY (T const &t1, T const &t2, T const &t3) +pub fn mat< 4, 4, T, defaultp > eulerAngleYXZ (T const &yaw, T const &pitch, T const &roll) +pub fn mat< 4, 4, T, defaultp > eulerAngleYZ (T const &angleY, T const &angleZ) +pub fn mat< 4, 4, T, defaultp > eulerAngleYZX (T const &t1, T const &t2, T const &t3) +pub fn mat< 4, 4, T, defaultp > eulerAngleYZY (T const &t1, T const &t2, T const &t3) +pub fn mat< 4, 4, T, defaultp > eulerAngleZ (T const &angleZ) +pub fn mat< 4, 4, T, defaultp > eulerAngleZX (T const &angle, T const &angleX) +pub fn mat< 4, 4, T, defaultp > eulerAngleZXY (T const &t1, T const &t2, T const &t3) +pub fn mat< 4, 4, T, defaultp > eulerAngleZXZ (T const &t1, T const &t2, T const &t3) +pub fn mat< 4, 4, T, defaultp > eulerAngleZY (T const &angleZ, T const &angleY) +pub fn mat< 4, 4, T, defaultp > eulerAngleZYX (T const &t1, T const &t2, T const &t3) +pub fn mat< 4, 4, T, defaultp > eulerAngleZYZ (T const &t1, T const &t2, T const &t3) +pub fn void extractEulerAngleXYX (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleXYZ (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleXZX (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleXZY (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleYXY (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleYXZ (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleYZX (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleYZY (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleZXY (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleZXZ (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleZYX (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn void extractEulerAngleZYZ (mat< 4, 4, T, defaultp > const &M, T &t1, T &t2, T &t3) +pub fn mat< 2, 2, T, defaultp > orientate2 (T const &angle) +pub fn mat< 3, 3, T, defaultp > orientate3 (T const &angle) +pub fn mat< 3, 3, T, Q > orientate3 (vec< 3, T, Q > const &angles) +pub fn mat< 4, 4, T, Q > orientate4 (vec< 3, T, Q > const &angles) +pub fn mat< 4, 4, T, defaultp > yawPitchRoll (T const &yaw, T const &pitch, T const &roll) \ No newline at end of file diff --git a/nalgebra-glm/src/gtx_exterior_product.rs b/nalgebra-glm/src/gtx_exterior_product.rs new file mode 100644 index 00000000..2d1f9993 --- /dev/null +++ b/nalgebra-glm/src/gtx_exterior_product.rs @@ -0,0 +1,8 @@ +use na::U2; + +use traits::Number; +use aliases::Vec; + +pub fn cross(v: &Vec, u: &Vec) -> N { + v.perp(u) +} \ No newline at end of file diff --git a/nalgebra-glm/src/gtx_handed_coordinate_space.rs b/nalgebra-glm/src/gtx_handed_coordinate_space.rs new file mode 100644 index 00000000..9b5d9634 --- /dev/null +++ b/nalgebra-glm/src/gtx_handed_coordinate_space.rs @@ -0,0 +1,12 @@ +use na::U3; + +use traits::Number; +use aliases::Vec; + +pub fn left_handed(a: &Vec, b: &Vec, c: &Vec) -> bool { + a.cross(b).dot(c) < N::zero() +} + +pub fn right_handed(a: &Vec, b: &Vec, c: &Vec) -> bool { + a.cross(b).dot(c) > N::zero() +} diff --git a/nalgebra-glm/src/gtx_matrix_cross_product.rs b/nalgebra-glm/src/gtx_matrix_cross_product.rs new file mode 100644 index 00000000..e247c2ab --- /dev/null +++ b/nalgebra-glm/src/gtx_matrix_cross_product.rs @@ -0,0 +1,19 @@ +use na::{self, Real, U3, U4, Matrix4}; + +use aliases::{Vec, Mat}; + +pub fn matrix_cross3(x: &Vec) -> Mat { + x.cross_matrix() +} + +pub fn matrix_cross4(x: &Vec) -> Mat { + let m = x.cross_matrix(); + + // FIXME: use a dedicated constructor from Matrix3 to Matrix4. + Matrix4::new( + m.m11, m.m12, m.m13, N::zero(), + m.m21, m.m22, m.m23, N::zero(), + m.m31, m.m32, m.m33, N::zero(), + N::zero(), N::zero(), N::zero(), N::one(), + ) +} diff --git a/nalgebra-glm/src/gtx_matrix_operation.rs b/nalgebra-glm/src/gtx_matrix_operation.rs new file mode 100644 index 00000000..1cd8d300 --- /dev/null +++ b/nalgebra-glm/src/gtx_matrix_operation.rs @@ -0,0 +1,42 @@ +use na::{Matrix2, Matrix2x3, Matrix2x4, Matrix3, Matrix3x2, Matrix3x4, Matrix4, Matrix4x2, Matrix4x3, + U2, U3, U4}; + +use traits::Number; +use aliases::{Vec, Mat}; + + +pub fn diagonal2x2(v: &Vec) -> Mat { + Matrix2::from_diagonal(v) +} + +pub fn diagonal2x3(v: &Vec) -> Mat { + Matrix2x3::from_partial_diagonal(v.as_slice()) +} + +pub fn diagonal2x4(v: &Vec) -> Mat { + Matrix2x4::from_partial_diagonal(v.as_slice()) +} + +pub fn diagonal3x2(v: &Vec) -> Mat { + Matrix3x2::from_partial_diagonal(v.as_slice()) +} + +pub fn diagonal3x3(v: &Vec) -> Mat { + Matrix3::from_diagonal(v) +} + +pub fn diagonal3x4(v: &Vec) -> Mat { + Matrix3x4::from_partial_diagonal(v.as_slice()) +} + +pub fn diagonal4x2(v: &Vec) -> Mat { + Matrix4x2::from_partial_diagonal(v.as_slice()) +} + +pub fn diagonal4x3(v: &Vec) -> Mat { + Matrix4x3::from_partial_diagonal(v.as_slice()) +} + +pub fn diagonal4x4(v: &Vec) -> Mat { + Matrix4::from_diagonal(v) +} diff --git a/nalgebra-glm/src/gtx_norm.rs b/nalgebra-glm/src/gtx_norm.rs new file mode 100644 index 00000000..dddbf743 --- /dev/null +++ b/nalgebra-glm/src/gtx_norm.rs @@ -0,0 +1,44 @@ +use na::{Real, DefaultAllocator}; + +use traits::{Alloc, Dimension}; +use aliases::Vec; + +pub fn distance2(p0: &Vec, p1: &Vec) -> N + where DefaultAllocator: Alloc { + (p1 - p0).norm_squared() +} + +pub fn l1_distance(x: &Vec, y: &Vec) -> N + where DefaultAllocator: Alloc { + l1_norm(&(x - y)) +} + +pub fn l1_norm(v: &Vec) -> N + where DefaultAllocator: Alloc { + ::comp_add(&v.abs()) +} + +pub fn l2_distance(x: &Vec, y: &Vec) -> N + where DefaultAllocator: Alloc { + l2_norm(&(y - x)) +} + +pub fn l2_norm(x: &Vec) -> N + where DefaultAllocator: Alloc { + x.norm() +} + +pub fn length2(x: &Vec) -> N + where DefaultAllocator: Alloc { + x.norm_squared() +} + +//pub fn lxNorm(x: &Vec, y: &Vec, unsigned int Depth) -> N +// where DefaultAllocator: Alloc { +// unimplemented!() +//} +// +//pub fn lxNorm(x: &Vec, unsigned int Depth) -> N +// where DefaultAllocator: Alloc { +// unimplemented!() +//} diff --git a/nalgebra-glm/src/gtx_normal.rs b/nalgebra-glm/src/gtx_normal.rs new file mode 100644 index 00000000..aa32c875 --- /dev/null +++ b/nalgebra-glm/src/gtx_normal.rs @@ -0,0 +1,7 @@ +use na::{Real, U3}; + +use aliases::Vec; + +pub fn triangleNormal(p1: &Vec, p2: &Vec, p3: &Vec) -> Vec { + (p2 - p1).cross(&(p3 - p1)).normalize() +} \ No newline at end of file diff --git a/nalgebra-glm/src/gtx_normalize_dot.rs b/nalgebra-glm/src/gtx_normalize_dot.rs new file mode 100644 index 00000000..e51331d5 --- /dev/null +++ b/nalgebra-glm/src/gtx_normalize_dot.rs @@ -0,0 +1,17 @@ +use na::{Real, DefaultAllocator}; + +use traits::{Dimension, Alloc}; +use aliases::Vec; + + +pub fn fastNormalizeDot(x: &Vec, y: &Vec) -> N + where DefaultAllocator: Alloc { + // XXX: improve those. + x.normalize().dot(&y.normalize()) +} + +pub fn normalizeDot(x: &Vec, y: &Vec) -> N + where DefaultAllocator: Alloc { + // XXX: improve those. + x.normalize().dot(&y.normalize()) +} \ No newline at end of file diff --git a/nalgebra-glm/src/gtx_quaternion.rs b/nalgebra-glm/src/gtx_quaternion.rs new file mode 100644 index 00000000..6c7e7c99 --- /dev/null +++ b/nalgebra-glm/src/gtx_quaternion.rs @@ -0,0 +1,16 @@ +pub fn vec< 3, T, Q > cross (qua< T, Q > const &q, vec< 3, T, Q > const &v) +pub fn vec< 3, T, Q > cross (vec< 3, T, Q > const &v, qua< T, Q > const &q) +pub fn T extractRealComponent (qua< T, Q > const &q) +pub fn qua< T, Q > fastMix (qua< T, Q > const &x, qua< T, Q > const &y, T const &a) +pub fn qua< T, Q > intermediate (qua< T, Q > const &prev, qua< T, Q > const &curr, qua< T, Q > const &next) +pub fn T length2 (qua< T, Q > const &q) +pub fn qua< T, Q > quat_identity () +pub fn vec< 3, T, Q > rotate (qua< T, Q > const &q, vec< 3, T, Q > const &v) +pub fn vec< 4, T, Q > rotate (qua< T, Q > const &q, vec< 4, T, Q > const &v) +pub fn qua< T, Q > rotation (vec< 3, T, Q > const &orig, vec< 3, T, Q > const &dest) +pub fn qua< T, Q > shortMix (qua< T, Q > const &x, qua< T, Q > const &y, T const &a) +pub fn qua< T, Q > squad (qua< T, Q > const &q1, qua< T, Q > const &q2, qua< T, Q > const &s1, qua< T, Q > const &s2, T const &h) +pub fn mat< 3, 3, T, Q > toMat3 (qua< T, Q > const &x) +pub fn mat< 4, 4, T, Q > toMat4 (qua< T, Q > const &x) +pub fn qua< T, Q > toQuat (mat< 3, 3, T, Q > const &x) +pub fn qua< T, Q > toQuat (mat< 4, 4, T, Q > const &x) diff --git a/nalgebra-glm/src/gtx_rotate_normalized_axis.rs b/nalgebra-glm/src/gtx_rotate_normalized_axis.rs new file mode 100644 index 00000000..ab30ca15 --- /dev/null +++ b/nalgebra-glm/src/gtx_rotate_normalized_axis.rs @@ -0,0 +1,11 @@ +use na::{Real, Rotation3, UnitQuaternion, Unit, U3, U4}; + +use aliases::{Vec, Mat, Qua}; + +pub fn rotateNormalizedAxis(m: &Mat, angle: N, axis: &Vec) -> Mat { + m * Rotation3::from_axis_angle(&Unit::new_unchecked(*axis), angle).to_homogeneous() +} + +pub fn rotateNormalizedAxis2(q: &Qua, angle: N, axis: &Vec) -> Qua { + q * UnitQuaternion::from_axis_angle(&Unit::new_unchecked(*axis), angle).unwrap() +} \ No newline at end of file diff --git a/nalgebra-glm/src/gtx_rotate_vector.rs b/nalgebra-glm/src/gtx_rotate_vector.rs new file mode 100644 index 00000000..2202ba00 --- /dev/null +++ b/nalgebra-glm/src/gtx_rotate_vector.rs @@ -0,0 +1,45 @@ +use na::{Real, U2, U3, U4}; + +pub fn orientation(Normal: &Vec, Up: &Vec) -> Mat { + unimplemented!() +} + +pub fn rotate2(v: &Vec, angle: N) -> Vec { + unimplemented!() +} + +pub fn rotate(v: &Vec, angle: N, normal: &Vec) -> Vec { + unimplemented!() +} + +pub fn rotate4(v: &Vec, angle: N, normal: &Vec) -> Vec { + unimplemented!() +} + +pub fn rotateX(v: &Vec, angle: N) -> Vec { + unimplemented!() +} + +pub fn rotateX4(v: &Vec, angle: N) -> Vec { + unimplemented!() +} + +pub fn rotateY(v: &Vec, angle: N) -> Vec { + unimplemented!() +} + +pub fn rotateY4(v: &Vec, angle: N) -> Vec { + unimplemented!() +} + +pub fn rotateZ(v: &Vec, angle: N) -> Vec { + unimplemented!() +} + +pub fn rotateZ4(v: &Vec, angle: N) -> Vec { + unimplemented!() +} + +pub fn slerp(x: &Vec, y: &Vec, a: N) -> Vec { + unimplemented!() +} diff --git a/nalgebra-glm/src/gtx_transform.rs b/nalgebra-glm/src/gtx_transform.rs new file mode 100644 index 00000000..698c68e6 --- /dev/null +++ b/nalgebra-glm/src/gtx_transform.rs @@ -0,0 +1,17 @@ +use na::{Real, Unit, Rotation3, Matrix4, U3, U4}; + +use traits::Number; +use aliases::{Vec, Mat}; + + +pub fn rotate(angle: N, v: &Vec) -> Mat { + Rotation3::from_axis_angle(&Unit::new_normalize(*v), angle).to_homogeneous() +} + +pub fn scale(v: &Vec) -> Mat { + Matrix4::new_nonuniform_scaling(v) +} + +pub fn translate(v: &Vec) -> Mat { + Matrix4::new_translation(v) +} diff --git a/nalgebra-glm/src/gtx_transform2.rs b/nalgebra-glm/src/gtx_transform2.rs new file mode 100644 index 00000000..0b1789a3 --- /dev/null +++ b/nalgebra-glm/src/gtx_transform2.rs @@ -0,0 +1,9 @@ +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 diff --git a/nalgebra-glm/src/gtx_transform2d.rs b/nalgebra-glm/src/gtx_transform2d.rs new file mode 100644 index 00000000..4330564a --- /dev/null +++ b/nalgebra-glm/src/gtx_transform2d.rs @@ -0,0 +1,5 @@ +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 diff --git a/nalgebra-glm/src/gtx_vector_angle.rs b/nalgebra-glm/src/gtx_vector_angle.rs new file mode 100644 index 00000000..da0d5a44 --- /dev/null +++ b/nalgebra-glm/src/gtx_vector_angle.rs @@ -0,0 +1,3 @@ +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 diff --git a/nalgebra-glm/src/gtx_vector_query.rs b/nalgebra-glm/src/gtx_vector_query.rs new file mode 100644 index 00000000..ef5a7e21 --- /dev/null +++ b/nalgebra-glm/src/gtx_vector_query.rs @@ -0,0 +1,37 @@ +use na::{Real, DefaultAllocator, U2, U3}; + +use traits::{Number, Dimension, Alloc}; +use aliases::Vec; + +pub fn are_collinear(v0: &Vec, v1: &Vec, epsilon: N) -> bool { + is_null(&v0.cross(v1), epsilon) +} + +pub fn are_collinear2(v0: &Vec, v1: &Vec, epsilon: N) -> bool { + abs_diff_eq!(v0.perp(v1), N::zero(), epsilon = epsilon) +} + +pub fn are_orthogonal(v0: &Vec, v1: &Vec, epsilon: N) -> bool + where DefaultAllocator: Alloc { + abs_diff_eq!(v0.dot(v1), N::zero(), epsilon = epsilon) +} + +pub fn are_orthonormal(v0: &Vec, v1: &Vec, epsilon: N) -> bool + where DefaultAllocator: Alloc { + unimplemented!() +} + +pub fn is_comp_null(v: &Vec, epsilon: N) -> Vec + where DefaultAllocator: Alloc { + v.map(|x| abs_diff_eq!(x, N::zero(), epsilon = epsilon)) +} + +pub fn is_normalized(v: &Vec, epsilon: N) -> bool + where DefaultAllocator: Alloc { + abs_diff_eq!(v.norm_squared(), N::one(), epsilon = epsilon * epsilon) +} + +pub fn is_null(v: &Vec, epsilon: N) -> bool + where DefaultAllocator: Alloc { + abs_diff_eq!(*v, Vec::::zeros(), epsilon = epsilon) +} diff --git a/nalgebra-glm/src/lib.rs b/nalgebra-glm/src/lib.rs index 6b1688ba..2ee8b708 100644 --- a/nalgebra-glm/src/lib.rs +++ b/nalgebra-glm/src/lib.rs @@ -15,6 +15,8 @@ pub use exponential::*; pub use ext_vector_relational::*; +pub use gtx_component_wise::*; + mod aliases; pub mod constructors; mod common; @@ -39,15 +41,33 @@ pub mod ext_scalar_common; pub mod ext_scalar_constants; pub mod ext_vector_common; pub mod ext_vector_relational; -pub mod gtc_bitfield; +//pub mod gtc_bitfield; pub mod gtc_constants; pub mod gtc_epsilon; -pub mod gtc_integer; +//pub mod gtc_integer; pub mod gtc_matrix_access; pub mod gtc_matrix_inverse; -pub mod gtc_packing; +//pub mod gtc_packing; pub mod gtc_quaternion; -pub mod gtc_reciprocal; -pub mod gtc_round; +//pub mod gtc_reciprocal; +//pub mod gtc_round; pub mod gtc_type_ptr; -pub mod gtc_ulp; \ No newline at end of file +//pub mod gtc_ulp; + +pub mod gtx_component_wise; +//pub mod gtx_euler_angles; +pub mod gtx_exterior_product; +pub mod gtx_handed_coordinate_space; +pub mod gtx_matrix_cross_product; +pub mod gtx_matrix_operation; +pub mod gtx_norm; +pub mod gtx_normal; +pub mod gtx_normalize_dot; +//pub mod gtx_quaternion; +pub mod gtx_rotate_normalized_axis; +//pub mod gtx_rotate_vector; +pub mod gtx_transform; +//pub mod gtx_transform2; +//pub mod gtx_transform2d; +//pub mod gtx_vector_angle; +pub mod gtx_vector_query; \ No newline at end of file diff --git a/nalgebra-glm/src/traits.rs b/nalgebra-glm/src/traits.rs index 7b1f40d8..53a16813 100644 --- a/nalgebra-glm/src/traits.rs +++ b/nalgebra-glm/src/traits.rs @@ -1,5 +1,5 @@ use std::cmp::{PartialOrd, PartialEq}; -use num::Signed; +use num::{Signed, FromPrimitive, Bounded}; use approx::AbsDiffEq; use alga::general::{Ring, Lattice}; @@ -10,10 +10,10 @@ pub trait Dimension: DimName + DimMin {} impl> Dimension for D {} -pub trait Number: Scalar + Ring + Lattice + AbsDiffEq + Signed { +pub trait Number: Scalar + Ring + Lattice + AbsDiffEq + Signed + FromPrimitive + Bounded { } -impl + Signed> Number for T { +impl + Signed + FromPrimitive + Bounded> Number for T { } #[doc(hidden)]