From e7edad5ebbb7b14ac581b5fe01933a4c8e3db960 Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Thu, 20 Sep 2018 14:23:31 +0200 Subject: [PATCH] Start actually implementing the GLM functions. --- nalgebra-glm/Cargo.toml | 1 + nalgebra-glm/src/common.rs | 52 ++++---- nalgebra-glm/src/exponential.rs | 33 ++--- nalgebra-glm/src/ext_matrix_relationnal.rs | 52 +++++--- nalgebra-glm/src/ext_matrix_transform.rs | 33 ++--- nalgebra-glm/src/ext_quaternion_common.rs | 34 +++--- nalgebra-glm/src/ext_quaternion_geometric.rs | 8 +- nalgebra-glm/src/ext_quaternion_relational.rs | 24 +++- nalgebra-glm/src/ext_quaternion_transform.rs | 14 ++- .../src/ext_quaternion_trigonometric.rs | 14 ++- nalgebra-glm/src/ext_scalar_constants.rs | 11 +- nalgebra-glm/src/ext_vector_common.rs | 56 +++------ nalgebra-glm/src/ext_vector_relational.rs | 20 ++-- nalgebra-glm/src/geometric.rs | 40 +++++-- nalgebra-glm/src/gtc_bitfield.rs | 14 +-- nalgebra-glm/src/gtc_constants.rs | 113 +++++++++--------- nalgebra-glm/src/gtc_epsilon.rs | 21 ++-- nalgebra-glm/src/gtc_integer.rs | 8 +- nalgebra-glm/src/gtc_matrix_access.rs | 24 ++-- nalgebra-glm/src/gtc_matrix_inverse.rs | 13 +- nalgebra-glm/src/gtc_packing.rs | 16 +-- nalgebra-glm/src/gtc_quaternion.rs | 71 ++++++----- nalgebra-glm/src/gtc_round.rs | 22 ++-- nalgebra-glm/src/gtc_type_ptr.rs | 91 +++++++------- nalgebra-glm/src/integer.rs | 24 ++-- nalgebra-glm/src/lib.rs | 5 + nalgebra-glm/src/matrix.rs | 21 ++-- nalgebra-glm/src/traits.rs | 24 ++-- nalgebra-glm/src/trigonometric.rs | 42 ++++--- nalgebra-glm/src/vector_relational.rs | 22 ++-- 30 files changed, 510 insertions(+), 413 deletions(-) diff --git a/nalgebra-glm/Cargo.toml b/nalgebra-glm/Cargo.toml index 1c8d0a4c..07ba7478 100644 --- a/nalgebra-glm/Cargo.toml +++ b/nalgebra-glm/Cargo.toml @@ -5,5 +5,6 @@ authors = ["sebcrozet "] [dependencies] num-traits = { version = "0.2", default-features = false } +approx = { version = "0.3", default-features = false } alga = "0.7" nalgebra = { path = ".." } \ No newline at end of file diff --git a/nalgebra-glm/src/common.rs b/nalgebra-glm/src/common.rs index 79fcd9ab..35ed1d58 100644 --- a/nalgebra-glm/src/common.rs +++ b/nalgebra-glm/src/common.rs @@ -1,7 +1,7 @@ -use na::{Scalar, DimName, DefaultAllocator}; +use na::{Scalar, DefaultAllocator}; use aliases::Vec; -use traits::Alloc; +use traits::{Dimension, Alloc}; pub fn abs(x: T) -> T { @@ -9,13 +9,13 @@ pub fn abs(x: T) -> T { } -pub fn abs2(x: &Vec) -> Vec +pub fn abs2(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn ceil(x: &Vec) -> Vec +pub fn ceil(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -26,13 +26,13 @@ pub fn clamp(x: T, minVal: T, maxVal: T) -> T { } -pub fn clamp2(x: &Vec,minVal: N, maxVal: N) -> Vec +pub fn clamp2(x: &Vec,minVal: N, maxVal: N) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn clamp3(x: &Vec, minVal: &Vec, maxVal: &Vec) -> Vec +pub fn clamp3(x: &Vec, minVal: &Vec, maxVal: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -43,7 +43,7 @@ pub fn floatBitsToInt(v: f32) -> i32 { } -pub fn floatBitsToInt2(v: Vec) -> Vec +pub fn floatBitsToInt2(v: Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -54,12 +54,12 @@ pub fn floatBitsToUint(v: f32) -> u32 { } -pub fn floatBitsToUint2(v: &Vec) -> Vec +pub fn floatBitsToUint2(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn floor(x: &Vec) -> Vec +pub fn floor(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -74,7 +74,7 @@ pub fn fract(x: T) -> T { } -pub fn fract2(x: &Vec) -> Vec +pub fn fract2(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -85,24 +85,24 @@ pub fn frexp(x: T, exp: I) -> T { } -pub fn intBitsToFloat(v: i32) -> f32 { +pub fn intBitsToFloat(v: i32) -> f32 { unimplemented!() } -pub fn intBitsToFloat2(v: &Vec) -> Vec +pub fn intBitsToFloat2(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn isinf(x: &Vec) -> Vec +pub fn isinf(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn isnan(x: &Vec) -> Vec +pub fn isnan(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -118,12 +118,12 @@ pub fn max(x: T, y: T) -> T { } -pub fn max2(x: &Vec,y: N) -> Vec +pub fn max2(x: &Vec,y: N) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn max3(x: &Vec, y: &Vec) -> Vec +pub fn max3(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -133,12 +133,12 @@ pub fn min(x: T, y: T) -> T { } -pub fn min2(x: &Vec,y: N) -> Vec +pub fn min2(x: &Vec,y: N) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn min3(x: &Vec, y: &Vec) -> Vec +pub fn min3(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -148,7 +148,7 @@ pub fn mix(x: T, y: T, a: T) -> T { } -pub fn mod_(x: &Vec, y: &Vec) -> Vec +pub fn mod_(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -158,17 +158,17 @@ pub fn modf(x: T, i: &T) -> T { } -pub fn round(x: &Vec) -> Vec +pub fn round(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn roundEven(x: &Vec) -> Vec +pub fn roundEven(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn sign(x: &Vec) -> Vec +pub fn sign(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -184,17 +184,17 @@ pub fn step(edge: T, x: T) -> T { } -pub fn step2(edge: N, x: &Vec) -> Vec +pub fn step2(edge: N, x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn step3(edge: &Vec, x: &Vec) -> Vec +pub fn step3(edge: &Vec, x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn trunc(x: &Vec) -> Vec +pub fn trunc(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() @@ -203,7 +203,7 @@ pub fn uintBitsToFloat(v: u32) -> f32 { unimplemented!() } -pub fn uintBitsToFloat2(v: &Vec) -> Vec +pub fn uintBitsToFloat2(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } \ No newline at end of file diff --git a/nalgebra-glm/src/exponential.rs b/nalgebra-glm/src/exponential.rs index 296ee533..08fe8f32 100644 --- a/nalgebra-glm/src/exponential.rs +++ b/nalgebra-glm/src/exponential.rs @@ -1,39 +1,40 @@ -use na::{Real, DefaultAllocator, DimName}; +use na::{Real, DefaultAllocator}; use aliases::Vec; -use traits::Alloc; +use traits::{Alloc, Dimension}; -pub fn exp(v: &Vec) -> Vec +pub fn exp(v: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + v.map(|x| x.exp()) } -pub fn exp2(v: &Vec) -> Vec +pub fn exp2(v: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + v.map(|x| x.exp2()) } -pub fn inversesqrt(v: &Vec) -> Vec +pub fn inversesqrt(v: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + v.map(|x| N::one() / x.sqrt()) + } -pub fn log(v: &Vec) -> Vec +pub fn log(v: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + v.map(|x| x.ln()) } -pub fn log2(v: &Vec) -> Vec +pub fn log2(v: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + v.map(|x| x.log2()) } -pub fn pow(base: &Vec, exponent: &Vec) +pub fn pow(base: &Vec, exponent: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + base.zip_map(exponent, |b, e| b.powf(e)) } -pub fn sqrt(v: &Vec) -> Vec +pub fn sqrt(v: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + v.map(|x| x.sqrt()) } diff --git a/nalgebra-glm/src/ext_matrix_relationnal.rs b/nalgebra-glm/src/ext_matrix_relationnal.rs index 06256e85..21ab54ec 100644 --- a/nalgebra-glm/src/ext_matrix_relationnal.rs +++ b/nalgebra-glm/src/ext_matrix_relationnal.rs @@ -1,35 +1,59 @@ -use na::{Scalar, DimName, DefaultAllocator}; +use na::DefaultAllocator; use aliases::{Vec, Mat}; -use traits::Alloc; +use traits::{Alloc, Number, Dimension}; -pub fn equal(x: &Mat, y: &Mat) -> Vec +pub fn equal(x: &Mat, y: &Mat) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + let mut res = Vec::<_, C>::repeat(false); + + for i in 0..C::dim() { + res[i] = x.column(i) == y.column(i) + } + + res } -pub fn equal_eps(x: &Mat, y: &Mat,epsilon: N) -> Vec +pub fn equal_eps(x: &Mat, y: &Mat, epsilon: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + equal_eps_vec(x, y, &Vec::<_, C>::repeat(epsilon)) } -pub fn equal_eps_vec(x: &Mat, y: &Mat, epsilon: &Vec) -> Vec +pub fn equal_eps_vec(x: &Mat, y: &Mat, epsilon: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + let mut res = Vec::<_, C>::repeat(false); + + for i in 0..C::dim() { + res[i] = (x.column(i) - y.column(i)).abs() < Vec::<_, R>::repeat(epsilon[i]) + } + + res } -pub fn not_equal(x: &Mat, y: &Mat) -> Vec +pub fn not_equal(x: &Mat, y: &Mat) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + let mut res = Vec::<_, C>::repeat(false); + + for i in 0..C::dim() { + res[i] = x.column(i) != y.column(i) + } + + res } -pub fn not_equal_eps(x: &Mat, y: &Mat,epsilon: N) -> Vec +pub fn not_equal_eps(x: &Mat, y: &Mat, epsilon: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + not_equal_eps_vec(x, y, &Vec::<_, C>::repeat(epsilon)) } -pub fn not_equal_eps_vec(x: &Mat, y: &Mat, epsilon: &Vec) -> Vec +pub fn not_equal_eps_vec(x: &Mat, y: &Mat, epsilon: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + let mut res = Vec::<_, C>::repeat(false); + + for i in 0..C::dim() { + res[i] = (x.column(i) - y.column(i)).abs() >= Vec::<_, R>::repeat(epsilon[i]) + } + + res } diff --git a/nalgebra-glm/src/ext_matrix_transform.rs b/nalgebra-glm/src/ext_matrix_transform.rs index 1e55e0c6..a9d210a5 100644 --- a/nalgebra-glm/src/ext_matrix_transform.rs +++ b/nalgebra-glm/src/ext_matrix_transform.rs @@ -1,31 +1,34 @@ -use na::{Scalar, U3, U4}; +use na::{DefaultAllocator, Scalar, Real, U3, U4, Unit, Rotation3, Point3}; +use traits::{Dimension, Number, Alloc}; use aliases::{Mat, Vec}; -pub fn identity() -> T { - unimplemented!() +pub fn identity() -> Mat + where DefaultAllocator: Alloc { + Mat::::identity() } -pub fn lookAt(eye: &Vec, center: &Vec, up: &Vec) -> Mat { - unimplemented!() +/// Same as `look_at_rh` +pub fn look_at(eye: &Vec, center: &Vec, up: &Vec) -> Mat { + look_at_rh(eye, center, up) } -pub fn lookAtLH(eye: &Vec, center: &Vec, up: &Vec) -> Mat { - unimplemented!() +pub fn look_at_lh(eye: &Vec, center: &Vec, up: &Vec) -> Mat { + Mat::look_at_lh(&Point3::from_coordinates(*eye), &Point3::from_coordinates(*center), up) } -pub fn lookAtRH(eye: &Vec, center: &Vec, up: &Vec) -> Mat { - unimplemented!() +pub fn look_at_rh(eye: &Vec, center: &Vec, up: &Vec) -> Mat { + Mat::look_at_rh(&Point3::from_coordinates(*eye), &Point3::from_coordinates(*center), up) } -pub fn rotate(m: &Mat,angle: N, axis: &Vec) -> Mat { - unimplemented!() +pub fn rotate(m: &Mat, angle: N, axis: &Vec) -> Mat { + m * Rotation3::from_axis_angle(&Unit::new_normalize(*axis), angle).to_homogeneous() } -pub fn scale(m: &Mat, v: &Vec) -> Mat { - unimplemented!() +pub fn scale(m: &Mat, v: &Vec) -> Mat { + m.append_nonuniform_scaling(v) } -pub fn translate(m: &Mat, v: &Vec) -> Mat { - unimplemented!() +pub fn translate(m: &Mat, v: &Vec) -> Mat { + m.append_translation(v) } diff --git a/nalgebra-glm/src/ext_quaternion_common.rs b/nalgebra-glm/src/ext_quaternion_common.rs index 42e045bf..05d33b2b 100644 --- a/nalgebra-glm/src/ext_quaternion_common.rs +++ b/nalgebra-glm/src/ext_quaternion_common.rs @@ -1,31 +1,31 @@ -use na::{Real, U4}; +use na::{self, Real, Unit, U4}; use aliases::{Vec, Qua}; pub fn conjugate(q: &Qua) -> Qua { - unimplemented!() + q.conjugate() } pub fn inverse(q: &Qua) -> Qua { - unimplemented!() + q.try_inverse().unwrap_or(na::zero()) } -pub fn isinf(x: &Qua) -> Vec { - unimplemented!() +//pub fn isinf(x: &Qua) -> Vec { +// x.coords.map(|e| e.is_inf()) +//} + +//pub fn isnan(x: &Qua) -> Vec { +// x.coords.map(|e| e.is_nan()) +//} + +pub fn lerp(x: &Qua, y: &Qua, a: N) -> Qua { + x.lerp(y, a) } -pub fn isnan(x: &Qua) -> Vec { - unimplemented!() +pub fn mix(x: &Qua, y: &Qua, a: N) -> Qua { + x * (N::one() - a) + y * a } -pub fn lerp(x: &Qua, y: &Qua,a: N) -> Qua { - unimplemented!() -} - -pub fn mix(x: &Qua, y: &Qua,a: N) -> Qua { - unimplemented!() -} - -pub fn slerp(x: &Qua, y: &Qua,a: N) -> Qua { - unimplemented!() +pub fn slerp(x: &Qua, y: &Qua, a: N) -> Qua { + Unit::new_normalize(*x).slerp(&Unit::new_normalize(*y), a).unwrap() } diff --git a/nalgebra-glm/src/ext_quaternion_geometric.rs b/nalgebra-glm/src/ext_quaternion_geometric.rs index e9fd7d09..7b231101 100644 --- a/nalgebra-glm/src/ext_quaternion_geometric.rs +++ b/nalgebra-glm/src/ext_quaternion_geometric.rs @@ -3,14 +3,14 @@ use na::Real; use aliases::Qua; pub fn cross(q1: &Qua, q2: &Qua) -> Qua { - unimplemented!() + q1 * q2 } pub fn dot(x: &Qua, y: &Qua) -> N { - unimplemented!() + x.dot(y) } pub fn length(q: &Qua) -> N { - unimplemented!() + q.norm() } pub fn normalize(q: &Qua) -> Qua { - unimplemented!() + q.normalize() } \ No newline at end of file diff --git a/nalgebra-glm/src/ext_quaternion_relational.rs b/nalgebra-glm/src/ext_quaternion_relational.rs index fe5a32b0..ab37d297 100644 --- a/nalgebra-glm/src/ext_quaternion_relational.rs +++ b/nalgebra-glm/src/ext_quaternion_relational.rs @@ -1,4 +1,20 @@ -pub fn vec< 4, bool, Q > equal(x: &Qua, y: &Qua) -pub fn vec< 4, bool, Q > equal(x: &Qua, y: &Qua,epsilon: N) -pub fn vec< 4, bool, Q > notEqual(x: &Qua, y: &Qua) -pub fn vec< 4, bool, Q > notEqual(x: &Qua, y: &Qua,epsilon: N) \ No newline at end of file +use na::{Real, U4}; + + +use aliases::{Qua, Vec}; + +pub fn equal(x: &Qua, y: &Qua) -> Vec { + ::equal(&x.coords, &y.coords) +} + +pub fn equal_eps(x: &Qua, y: &Qua, epsilon: N) -> Vec { + ::equal_eps(&x.coords, &y.coords, epsilon) +} + +pub fn not_equal(x: &Qua, y: &Qua) -> Vec { + ::not_equal(&x.coords, &y.coords) +} + +pub fn not_equal_eps(x: &Qua, y: &Qua, epsilon: N) -> Vec { + ::not_equal_eps(&x.coords, &y.coords, epsilon) +} diff --git a/nalgebra-glm/src/ext_quaternion_transform.rs b/nalgebra-glm/src/ext_quaternion_transform.rs index c82dc841..08f7b393 100644 --- a/nalgebra-glm/src/ext_quaternion_transform.rs +++ b/nalgebra-glm/src/ext_quaternion_transform.rs @@ -1,19 +1,23 @@ -use na::{Real, U3}; +use na::{Real, U3, UnitQuaternion, Unit}; use aliases::{Vec, Qua}; pub fn exp(q: &Qua) -> Qua { - unimplemented!() + q.exp() } + pub fn log(q: &Qua) -> Qua { - unimplemented!() + q.ln() } + pub fn pow(q: &Qua, y: N) -> Qua { - unimplemented!() + q.powf(y) } + pub fn rotate(q: &Qua, angle: N, axis: &Vec) -> Qua { - unimplemented!() + q * UnitQuaternion::from_axis_angle(&Unit::new_normalize(*axis), angle).unwrap() } + pub fn sqrt(q: &Qua) -> Qua { unimplemented!() } \ No newline at end of file diff --git a/nalgebra-glm/src/ext_quaternion_trigonometric.rs b/nalgebra-glm/src/ext_quaternion_trigonometric.rs index 77f20b74..3f0e6b61 100644 --- a/nalgebra-glm/src/ext_quaternion_trigonometric.rs +++ b/nalgebra-glm/src/ext_quaternion_trigonometric.rs @@ -1,13 +1,19 @@ -use na::{Real, U3}; +use na::{Real, U3, Unit, UnitQuaternion, Vector3}; use aliases::{Vec, Qua}; pub fn angle(x: &Qua) -> N { - unimplemented!() + UnitQuaternion::from_quaternion(*x).angle() } + pub fn angleAxis(angle: N, axis: &Vec) -> Qua { - unimplemented!() + UnitQuaternion::from_axis_angle(&Unit::new_normalize(*axis), angle).unwrap() } + pub fn axis(x: &Qua) -> Vec { - unimplemented!() + if let Some(a) = UnitQuaternion::from_quaternion(*x).axis() { + a.unwrap() + } else { + Vector3::zeros() + } } \ No newline at end of file diff --git a/nalgebra-glm/src/ext_scalar_constants.rs b/nalgebra-glm/src/ext_scalar_constants.rs index 525946f7..d34c58bf 100644 --- a/nalgebra-glm/src/ext_scalar_constants.rs +++ b/nalgebra-glm/src/ext_scalar_constants.rs @@ -1,7 +1,10 @@ -pub fn epsilon() -> T { - unimplemented!() +use approx::AbsDiffEq; +use na::Real; + +pub fn epsilon>() -> N { + N::default_epsilon() } -pub fn pi() -> T { - unimplemented!() +pub fn pi() -> N { + N::pi() } diff --git a/nalgebra-glm/src/ext_vector_common.rs b/nalgebra-glm/src/ext_vector_common.rs index 02aa420e..59033378 100644 --- a/nalgebra-glm/src/ext_vector_common.rs +++ b/nalgebra-glm/src/ext_vector_common.rs @@ -1,64 +1,44 @@ -use na::{Scalar, DimName, DefaultAllocator}; +use na::{self, DefaultAllocator}; -use traits::Alloc; +use traits::{Alloc, Number, Dimension}; use aliases::Vec; -pub fn fmax(a: &Vec,b: N) -> Vec +pub fn max(a: &Vec, b: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + a.map(|a| na::sup(&a, &b)) } -pub fn fmax2(a: &Vec, b: &Vec) -> Vec +pub fn max2(a: &Vec, b: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + na::sup(a, b) } -pub fn fmax3(a: &Vec, b: &Vec, c: &Vec) -> Vec +pub fn max3(a: &Vec, b: &Vec, c: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + max2(&max2(a, b), c) } -pub fn fmax4(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> Vec +pub fn max4(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + max2(&max2(a, b), &max2(c, d)) } -pub fn fmin(x: &Vec,y: N) -> Vec +pub fn min(x: &Vec,y: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + x.map(|x| na::inf(&x, &y)) } -pub fn fmin2(x: &Vec, y: &Vec) -> Vec +pub fn min2(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + na::inf(x, y) } -pub fn fmin3(a: &Vec, b: &Vec, c: &Vec) -> Vec +pub fn min3(a: &Vec, b: &Vec, c: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + min2(&min2(a, b), c) } -pub fn fmin4(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> Vec +pub fn min4(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() -} - -pub fn max3(x: &Vec, y: &Vec, z: &Vec) -> Vec - where DefaultAllocator: Alloc { - unimplemented!() -} - -pub fn max4(x: &Vec, y: &Vec, z: &Vec, w: &Vec) -> Vec - where DefaultAllocator: Alloc { - unimplemented!() -} - -pub fn min3(a: &Vec, b: &Vec, c: &Vec) -> Vec - where DefaultAllocator: Alloc { - unimplemented!() -} - -pub fn min4(a: &Vec, b: &Vec, c: &Vec, d: &Vec) -> Vec - where DefaultAllocator: Alloc { - unimplemented!() + min2(&min2(a, b), &min2(c, d)) } diff --git a/nalgebra-glm/src/ext_vector_relational.rs b/nalgebra-glm/src/ext_vector_relational.rs index a8e02196..ba505cb7 100644 --- a/nalgebra-glm/src/ext_vector_relational.rs +++ b/nalgebra-glm/src/ext_vector_relational.rs @@ -1,24 +1,24 @@ -use na::{Scalar, DimName, DefaultAllocator}; +use na::{DefaultAllocator}; -use traits::Alloc; +use traits::{Alloc, Number, Dimension}; use aliases::Vec; -pub fn equal(x: &Vec, y: &Vec, epsilon: N) -> Vec +pub fn equal_eps(x: &Vec, y: &Vec, epsilon: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + x.zip_map(y, |x, y| abs_diff_eq!(x, y)) } -pub fn equal2(x: &Vec, y: &Vec, epsilon: &Vec) -> Vec +pub fn equal_eps_vec(x: &Vec, y: &Vec, epsilon: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + x.zip_zip_map(y, epsilon, |x, y, eps| abs_diff_eq!(x, y, epsilon = eps)) } -pub fn notEqual(x: &Vec, y: &Vec, epsilon: N) -> Vec +pub fn not_equal_eps(x: &Vec, y: &Vec, epsilon: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + x.zip_map(y, |x, y| abs_diff_ne!(x, y)) } -pub fn notEqual2(x: &Vec, y: &Vec, epsilon: &Vec) -> Vec +pub fn not_equal_eps_vec(x: &Vec, y: &Vec, epsilon: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + x.zip_zip_map(y, epsilon, |x, y, eps| abs_diff_ne!(x, y, epsilon = eps)) } diff --git a/nalgebra-glm/src/geometric.rs b/nalgebra-glm/src/geometric.rs index dfea9c03..75050bd5 100644 --- a/nalgebra-glm/src/geometric.rs +++ b/nalgebra-glm/src/geometric.rs @@ -1,43 +1,57 @@ -use na::{Scalar, Real, DimName, U3, DefaultAllocator}; +use na::{self, Scalar, Real, U3, DefaultAllocator}; -use traits::{Number, Alloc}; +use traits::{Number, Alloc, Dimension}; use aliases::Vec; -pub fn cross(x: &Vec, y: &Vec) -> Vec { +pub fn cross(x: &Vec, y: &Vec) -> Vec { x.cross(y) } -pub fn distance(p0: &Vec, p1: &Vec) -> N +pub fn distance(p0: &Vec, p1: &Vec) -> N where DefaultAllocator: Alloc { (p1 - p0).norm() } -pub fn dot(x: &Vec, y: &Vec) -> N +pub fn dot(x: &Vec, y: &Vec) -> N where DefaultAllocator: Alloc { x.dot(y) } -pub fn faceforward(N: &Vec, I: &Vec, Nref: &Vec) -> Vec +pub fn faceforward(n: &Vec, i: &Vec, nref: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + if nref.dot(i) < N::zero() { + n.clone() + } else { + -n.clone() + } } -pub fn length(x: &Vec) -> N +pub fn length(x: &Vec) -> N where DefaultAllocator: Alloc { x.norm() } -pub fn normalize(x: &Vec) -> Vec +pub fn normalize(x: &Vec) -> Vec where DefaultAllocator: Alloc { x.normalize() } -pub fn reflect(I: &Vec, N: &Vec) -> Vec +pub fn reflect(i: &Vec, n: &Vec) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + let _2 = N::one() + N::one(); + i - n * (n.dot(i) * _2) } -pub fn refract(I: &Vec, N: &Vec, eta: N) -> Vec +pub fn refract(i: &Vec, n: &Vec, eta: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + + let ni = n.dot(i); + let k = N::one() - eta * eta * (N::one() - ni * ni); + + if k < N::zero() { + Vec::<_, D>::zeros() + } + else { + i * eta - n * (eta * dot(n, i) + k.sqrt()) + } } \ No newline at end of file diff --git a/nalgebra-glm/src/gtc_bitfield.rs b/nalgebra-glm/src/gtc_bitfield.rs index 5789934c..e04768ef 100644 --- a/nalgebra-glm/src/gtc_bitfield.rs +++ b/nalgebra-glm/src/gtc_bitfield.rs @@ -1,6 +1,6 @@ -use na::{Scalar, DimName, DefaultAllocator}; +use na::{Scalar, DefaultAllocator}; -use traits::Alloc; +use traits::{Alloc, Dimension}; use aliases::*; pub fn bitfieldDeinterleave(x: u16) -> U8Vec2 { @@ -19,7 +19,7 @@ pub fn bitfieldFillOne(Value: IU, FirstBit: i32, BitCount: i32) -> IU { unimplemented!() } -pub fn bitfieldFillOne2(Value: &Vec, FirstBit: i32, BitCount: i32) -> Vec +pub fn bitfieldFillOne2(Value: &Vec, FirstBit: i32, BitCount: i32) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -28,7 +28,7 @@ pub fn bitfieldFillZero(Value: IU, FirstBit: i32, BitCount: i32) -> IU { unimplemented!() } -pub fn bitfieldFillZero2(Value: &Vec, FirstBit: i32, BitCount: i32) -> Vec +pub fn bitfieldFillZero2(Value: &Vec, FirstBit: i32, BitCount: i32) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -113,7 +113,7 @@ pub fn bitfieldRotateLeft(In: IU, Shift: i32) -> IU { unimplemented!() } -pub fn bitfieldRotateLeft2(In: &Vec, Shift: i32) -> Vec +pub fn bitfieldRotateLeft2(In: &Vec, Shift: i32) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -122,7 +122,7 @@ pub fn bitfieldRotateRight(In: IU, Shift: i32) -> IU { unimplemented!() } -pub fn bitfieldRotateRight2(In: &Vec, Shift: i32) -> Vec +pub fn bitfieldRotateRight2(In: &Vec, Shift: i32) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -131,7 +131,7 @@ pub fn mask(Bits: IU) -> IU { unimplemented!() } -pub fn mask2(v: &Vec) -> Vec +pub fn mask2(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } diff --git a/nalgebra-glm/src/gtc_constants.rs b/nalgebra-glm/src/gtc_constants.rs index 3bb93258..ab9ebb2c 100644 --- a/nalgebra-glm/src/gtc_constants.rs +++ b/nalgebra-glm/src/gtc_constants.rs @@ -1,108 +1,111 @@ -pub fn e() -> T { - unimplemented!() +use na::{self, Real}; +use traits::Number; + +pub fn e() -> N { + N::e() } -pub fn euler() -> T { - unimplemented!() +pub fn euler() -> N { + N::e() } -pub fn four_over_pi() -> T { - unimplemented!() +pub fn four_over_pi() -> N { + na::convert::<_, N>(4.0) / N::pi() } -pub fn golden_ratio() -> T { - unimplemented!() +pub fn golden_ratio() -> N { + (N::one() + root_five()) / na::convert(2.0) } -pub fn half_pi() -> T { - unimplemented!() +pub fn half_pi() -> N { + N::frac_pi_2() } -pub fn ln_ln_two() -> T { - unimplemented!() +pub fn ln_ln_two() -> N { + N::ln_2().ln() } -pub fn ln_ten() -> T { - unimplemented!() +pub fn ln_ten() -> N { + N::ln_10() } -pub fn ln_two() -> T { - unimplemented!() +pub fn ln_two() -> N { + N::ln_2() } -pub fn one() -> T { - unimplemented!() +pub fn one() -> N { + N::one() } -pub fn one_over_pi() -> T { - unimplemented!() +pub fn one_over_pi() -> N { + N::frac_1_pi() } -pub fn one_over_root_two() -> T { - unimplemented!() +pub fn one_over_root_two() -> N { + N::one() / root_two() } -pub fn one_over_two_pi() -> T { - unimplemented!() +pub fn one_over_two_pi() -> N { + N::frac_1_pi() * na::convert(0.5) } -pub fn quarter_pi() -> T { - unimplemented!() +pub fn quarter_pi() -> N { + N::frac_pi_4() } -pub fn root_five() -> T { - unimplemented!() +pub fn root_five() -> N { + na::convert::<_, N>(5.0).sqrt() } -pub fn root_half_pi() -> T { - unimplemented!() +pub fn root_half_pi() -> N { + (N::pi() / na::convert(2.0)).sqrt() } -pub fn root_ln_four() -> T { - unimplemented!() +pub fn root_ln_four() -> N { + na::convert::<_, N>(4.0).sqrt() } -pub fn root_pi() -> T { - unimplemented!() +pub fn root_pi() -> N { + N::pi().sqrt() } -pub fn root_three() -> T { - unimplemented!() +pub fn root_three() -> N { + na::convert::<_, N>(3.0).sqrt() } -pub fn root_two() -> T { - unimplemented!() +pub fn root_two() -> N { + // FIXME: there should be a ::sqrt_2() on the Real trait. + na::convert::<_, N>(2.0).sqrt() } -pub fn root_two_pi() -> T { - unimplemented!() +pub fn root_two_pi() -> N { + N::two_pi().sqrt() } -pub fn third() -> T { - unimplemented!() +pub fn third() -> N { + na::convert(1.0 / 2.0) } -pub fn three_over_two_pi() -> T { - unimplemented!() +pub fn three_over_two_pi() -> N { + na::convert::<_, N>(3.0) / N::two_pi() } -pub fn two_over_pi() -> T { - unimplemented!() +pub fn two_over_pi() -> N { + N::frac_2_pi() } -pub fn two_over_root_pi() -> T { - unimplemented!() +pub fn two_over_root_pi() -> N { + N::frac_2_pi() } -pub fn two_pi() -> T { - unimplemented!() +pub fn two_pi() -> N { + N::two_pi() } -pub fn two_thirds() -> T { - unimplemented!() +pub fn two_thirds() -> N { + na::convert(2.0 / 3.0) } -pub fn zero() -> T { - unimplemented!() - +pub fn zero() -> N { + N::zero() } \ No newline at end of file diff --git a/nalgebra-glm/src/gtc_epsilon.rs b/nalgebra-glm/src/gtc_epsilon.rs index 13c21a9f..5b5b4c2a 100644 --- a/nalgebra-glm/src/gtc_epsilon.rs +++ b/nalgebra-glm/src/gtc_epsilon.rs @@ -1,22 +1,23 @@ -use na::{DimName, Scalar, DefaultAllocator}; +use approx::AbsDiffEq; +use na::DefaultAllocator; -use traits::Alloc; +use traits::{Alloc, Number, Dimension}; use aliases::Vec; -pub fn epsilonEqual(x: &Vec, y: &Vec, epsilon: N) -> Vec +pub fn epsilonEqual(x: &Vec, y: &Vec, epsilon: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + x.zip_map(y, |x, y| abs_diff_eq!(x, y, epsilon = epsilon)) } -pub fn epsilonEqual2(x: T, y: T, epsilon: T) -> bool { - unimplemented!() +pub fn epsilonEqual2>(x: N, y: N, epsilon: N) -> bool { + abs_diff_eq!(x, y, epsilon = epsilon) } -pub fn epsilonNotEqual(x: &Vec, y: &Vec, epsilon: N) -> Vec +pub fn epsilonNotEqual(x: &Vec, y: &Vec, epsilon: N) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + x.zip_map(y, |x, y| abs_diff_ne!(x, y, epsilon = epsilon)) } -pub fn epsilonNotEqual2(x: T, y: T, epsilon: T) -> bool { - unimplemented!() +pub fn epsilonNotEqual2>(x: N, y: N, epsilon: N) -> bool { + abs_diff_ne!(x, y, epsilon = epsilon) } diff --git a/nalgebra-glm/src/gtc_integer.rs b/nalgebra-glm/src/gtc_integer.rs index 6ebb4c33..0531f01a 100644 --- a/nalgebra-glm/src/gtc_integer.rs +++ b/nalgebra-glm/src/gtc_integer.rs @@ -1,9 +1,9 @@ -use na::{DimName, Scalar, DefaultAllocator}; +use na::{Scalar, DefaultAllocator}; -use traits::Alloc; +use traits::{Alloc, Dimension}; use aliases::Vec; -pub fn iround(x: &Vec) -> Vec +pub fn iround(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -12,7 +12,7 @@ pub fn log2(x: I) -> I { unimplemented!() } -pub fn uround(x: &Vec) -> Vec +pub fn uround(x: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } \ No newline at end of file diff --git a/nalgebra-glm/src/gtc_matrix_access.rs b/nalgebra-glm/src/gtc_matrix_access.rs index 640a7979..f8280e96 100644 --- a/nalgebra-glm/src/gtc_matrix_access.rs +++ b/nalgebra-glm/src/gtc_matrix_access.rs @@ -1,24 +1,28 @@ -use na::{Scalar, DimName, DefaultAllocator}; +use na::{Scalar, DefaultAllocator}; -use traits::Alloc; +use traits::{Alloc, Dimension}; use aliases::{Vec, Mat}; -pub fn column(m: &Mat, index: usize) -> Vec +pub fn column(m: &Mat, index: usize) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + m.column(index).into_owned() } -pub fn column2(m: &Mat, index: usize, x: &Vec) -> Mat +pub fn set_column(m: &Mat, index: usize, x: &Vec) -> Mat where DefaultAllocator: Alloc { - unimplemented!() + let mut res = m.clone(); + res.set_column(index, x); + res } -pub fn row(m: &Mat, index: usize) -> Vec +pub fn row(m: &Mat, index: usize) -> Vec where DefaultAllocator: Alloc { - unimplemented!() + m.row(index).into_owned().transpose() } -pub fn row2(m: &Mat, index: usize, x: &Vec) -> Mat +pub fn set_row(m: &Mat, index: usize, x: &Vec) -> Mat where DefaultAllocator: Alloc { - unimplemented!() + let mut res = m.clone(); + res.set_row(index, &x.transpose()); + res } diff --git a/nalgebra-glm/src/gtc_matrix_inverse.rs b/nalgebra-glm/src/gtc_matrix_inverse.rs index 2d06dfbf..c36210ea 100644 --- a/nalgebra-glm/src/gtc_matrix_inverse.rs +++ b/nalgebra-glm/src/gtc_matrix_inverse.rs @@ -1,14 +1,15 @@ -use na::{Scalar, DimName, DefaultAllocator}; +use na::{Real, DefaultAllocator, Transform, TAffine}; -use traits::Alloc; +use traits::{Alloc, Dimension}; use aliases::Mat; -pub fn affineInverse(m: &Mat) -> Mat +pub fn affine_inverse(m: Mat) -> Mat where DefaultAllocator: Alloc { - unimplemented!() + // FIXME: this should be optimized. + m.try_inverse().unwrap_or(Mat::<_, D, D>::zeros()) } -pub fn inverseTranspose(m: &Mat) -> Mat +pub fn inverse_transpose(m: Mat) -> Mat where DefaultAllocator: Alloc { - unimplemented!() + m.try_inverse().unwrap_or(Mat::<_, D, D>::zeros()).transpose() } \ No newline at end of file diff --git a/nalgebra-glm/src/gtc_packing.rs b/nalgebra-glm/src/gtc_packing.rs index 394324a7..e0ab84a0 100644 --- a/nalgebra-glm/src/gtc_packing.rs +++ b/nalgebra-glm/src/gtc_packing.rs @@ -1,6 +1,6 @@ -use na::{Scalar, Real, DimName, DefaultAllocator, U3, U4}; +use na::{Scalar, Real, DefaultAllocator, U3, U4}; -use traits::Alloc; +use traits::{Alloc, Dimension}; use aliases::*; @@ -12,7 +12,7 @@ pub fn packF3x9_E1x5(v: &Vec3) -> i32 { unimplemented!() } -pub fn packHalf(v: &Vec) -> Vec +pub fn packHalf(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -53,7 +53,7 @@ pub fn packRGBM(rgb: &Vec) -> Vec { unimplemented!() } -pub fn packSnorm(v: Vec) -> Vec +pub fn packSnorm(v: Vec) -> Vec where DefaultAllocator: Alloc + Alloc { unimplemented!() } @@ -102,7 +102,7 @@ pub fn packUint4x8(v: &U8Vec4) -> i32 { unimplemented!() } -pub fn packUnorm(v: &Vec) -> Vec +pub fn packUnorm(v: &Vec) -> Vec where DefaultAllocator: Alloc + Alloc { unimplemented!() } @@ -155,7 +155,7 @@ pub fn unpackF3x9_E1x5(p: i32) -> Vec3 { unimplemented!() } -pub fn unpackHalf(p: Vec) -> Vec +pub fn unpackHalf(p: Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -196,7 +196,7 @@ pub fn unpackRGBM(rgbm: &Vec) -> Vec { unimplemented!() } -pub fn unpackSnorm(v: &Vec) -> Vec +pub fn unpackSnorm(v: &Vec) -> Vec where DefaultAllocator: Alloc + Alloc { unimplemented!() } @@ -245,7 +245,7 @@ pub fn unpackUint4x8(p: i32) -> U8Vec4 { unimplemented!() } -pub fn unpackUnorm(v: &Vec) -> Vec +pub fn unpackUnorm(v: &Vec) -> Vec where DefaultAllocator: Alloc + Alloc { unimplemented!() } diff --git a/nalgebra-glm/src/gtc_quaternion.rs b/nalgebra-glm/src/gtc_quaternion.rs index 286cdf4f..7adf0e14 100644 --- a/nalgebra-glm/src/gtc_quaternion.rs +++ b/nalgebra-glm/src/gtc_quaternion.rs @@ -1,64 +1,73 @@ -use na::{Real, U3, U4}; +use na::{self, Real, U3, U4, UnitQuaternion, Vector3, Rotation3}; use aliases::{Qua, Vec, Mat}; -pub fn eulerAngles(x: &Qua) -> Vec { - unimplemented!() +/// Euler angles of the quaternion as (pitch, yaw, roll). +pub fn euler_angles(x: &Qua) -> Vec { + let q = UnitQuaternion::new_unchecked(*x); + let a = q.to_euler_angles(); + Vector3::new(a.2, a.1, a.0) } -pub fn greaterThan(x: &Qua, y: &Qua) -> Vec { - unimplemented!() +pub fn greater_than(x: &Qua, y: &Qua) -> Vec { + ::greater_than(&x.coords, &y.coords) } -pub fn greaterThanEqual(x: &Qua, y: &Qua) -> Vec { - unimplemented!() +pub fn greater_than_equal(x: &Qua, y: &Qua) -> Vec { + ::greater_than_equal(&x.coords, &y.coords) } -pub fn lessThan(x: &Qua, y: &Qua) -> Vec { - unimplemented!() +pub fn less_than(x: &Qua, y: &Qua) -> Vec { + ::less_than(&x.coords, &y.coords) } -pub fn lessThanEqual(x: &Qua, y: &Qua) -> Vec { - unimplemented!() +pub fn less_than_equal(x: &Qua, y: &Qua) -> Vec { + ::less_than_equal(&x.coords, &y.coords) } -pub fn mat3_cast(x: &Qua) -> Mat { - unimplemented!() +pub fn mat3_cast(x: Qua) -> Mat { + let q = UnitQuaternion::new_unchecked(x); + q.to_rotation_matrix().unwrap() } -pub fn mat4_cast(x: &Qua) -> Mat { - unimplemented!() +pub fn mat4_cast(x: Qua) -> Mat { + let q = UnitQuaternion::new_unchecked(x); + q.to_homogeneous() } -pub fn pitch(x: &Qua) -> N { - unimplemented!() +pub fn quat_cast(x: Mat) -> Qua { + let rot = Rotation3::from_matrix_unchecked(x); + UnitQuaternion::from_rotation_matrix(&rot).unwrap() } -pub fn quat_cast(x: &Mat) -> Qua { - unimplemented!() +pub fn quat_cast2(x: Mat) -> Qua { + quat_cast(x.fixed_slice::(0, 0).into_owned()) } -pub fn quat_cast2(x: &Mat) -> Qua { - unimplemented!() +pub fn quat_look_at(direction: &Vec, up: &Vec) -> Qua { + quat_look_at_rh(direction, up) } -pub fn quatLookAt(direction: &Vec, up: &Vec) -> Qua { - unimplemented!() +pub fn quat_look_at_lh(direction: &Vec, up: &Vec) -> Qua { + UnitQuaternion::look_at_lh(direction, up).unwrap() } -pub fn quatLookAtLH(direction: &Vec, up: &Vec) -> Qua { - unimplemented!() -} - -pub fn quatLookAtRH(direction: &Vec, up: &Vec) -> Qua { - unimplemented!() +pub fn quat_look_at_rh(direction: &Vec, up: &Vec) -> Qua { + UnitQuaternion::look_at_rh(direction, up).unwrap() } pub fn roll(x: &Qua) -> N { - unimplemented!() + // FIXME: optimize this. + euler_angles(x).z } pub fn yaw(x: &Qua) -> N { - unimplemented!() + // FIXME: optimize this. + euler_angles(x).y +} + +pub fn pitch(x: &Qua) -> N { + // FIXME: optimize this. + euler_angles(x).x } diff --git a/nalgebra-glm/src/gtc_round.rs b/nalgebra-glm/src/gtc_round.rs index 94443b7a..8e08afda 100644 --- a/nalgebra-glm/src/gtc_round.rs +++ b/nalgebra-glm/src/gtc_round.rs @@ -1,6 +1,6 @@ -use na::{Scalar, Real, DimName, U3, DefaultAllocator}; +use na::{Scalar, Real, U3, DefaultAllocator}; -use traits::{Number, Alloc}; +use traits::{Number, Alloc, Dimension}; use aliases::Vec; @@ -8,7 +8,7 @@ pub fn ceilMultiple(v: T, Multiple: T) -> T { unimplemented!() } -pub fn ceilMultiple2(v: &Vec, Multiple: &Vec) -> Vec +pub fn ceilMultiple2(v: &Vec, Multiple: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -17,7 +17,7 @@ pub fn ceilPowerOfTwo(v: IU) -> IU { unimplemented!() } -pub fn ceilPowerOfTwo2(v: &Vec) -> Vec +pub fn ceilPowerOfTwo2(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -26,7 +26,7 @@ pub fn floorMultiple(v: T, Multiple: T) -> T { unimplemented!() } -pub fn floorMultiple2(v: &Vec, Multiple: &Vec) -> Vec +pub fn floorMultiple2(v: &Vec, Multiple: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -35,7 +35,7 @@ pub fn floorPowerOfTwo(v: IU) -> IU { unimplemented!() } -pub fn floorPowerOfTwo2(v: &Vec) -> Vec +pub fn floorPowerOfTwo2(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -44,12 +44,12 @@ pub fn isMultiple(v: IU, Multiple: IU) -> bool { unimplemented!() } -pub fn isMultiple2(v: &Vec,Multiple: N) -> Vec +pub fn isMultiple2(v: &Vec,Multiple: N) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn isMultiple3(v: &Vec, Multiple: &Vec) -> Vec +pub fn isMultiple3(v: &Vec, Multiple: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -58,7 +58,7 @@ pub fn isPowerOfTwo2(v: IU) -> bool { unimplemented!() } -pub fn isPowerOfTwo(v: &Vec) -> Vec +pub fn isPowerOfTwo(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -67,7 +67,7 @@ pub fn roundMultiple(v: T, Multiple: T) -> T { unimplemented!() } -pub fn roundMultiple2(v: &Vec, Multiple: &Vec) -> Vec +pub fn roundMultiple2(v: &Vec, Multiple: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -76,7 +76,7 @@ pub fn roundPowerOfTwo(v: IU) -> IU { unimplemented!() } -pub fn roundPowerOfTwo2(v: &Vec) -> Vec +pub fn roundPowerOfTwo2(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } diff --git a/nalgebra-glm/src/gtc_type_ptr.rs b/nalgebra-glm/src/gtc_type_ptr.rs index fb54b878..49bb63d2 100644 --- a/nalgebra-glm/src/gtc_type_ptr.rs +++ b/nalgebra-glm/src/gtc_type_ptr.rs @@ -1,137 +1,146 @@ -use na::{Scalar, Real, U1, U2, U3, U4}; +use na::{Scalar, Real, U1, U2, U3, U4, DefaultAllocator, + Quaternion, Matrix2, Matrix3, Matrix4, Vector1, Vector2, Vector3, Vector4, + Matrix2x3, Matrix2x4, Matrix3x2, Matrix3x4, Matrix4x2, Matrix4x3}; +use traits::{Number, Alloc, Dimension}; use aliases::{Qua, Vec, Mat}; pub fn make_mat2(ptr: &[N]) -> Mat { - unimplemented!() + Matrix2::from_column_slice(ptr) } pub fn make_mat2x2(ptr: &[N]) -> Mat { - unimplemented!() + Matrix2::from_column_slice(ptr) } pub fn make_mat2x3(ptr: &[N]) -> Mat { - unimplemented!() + Matrix2x3::from_column_slice(ptr) } pub fn make_mat2x4(ptr: &[N]) -> Mat { - unimplemented!() + Matrix2x4::from_column_slice(ptr) } pub fn make_mat3(ptr: &[N]) -> Mat { - unimplemented!() + Matrix3::from_column_slice(ptr) } pub fn make_mat3x2(ptr: &[N]) -> Mat { - unimplemented!() + Matrix3x2::from_column_slice(ptr) } pub fn make_mat3x3(ptr: &[N]) -> Mat { - unimplemented!() + Matrix3::from_column_slice(ptr) } pub fn make_mat3x4(ptr: &[N]) -> Mat { - unimplemented!() + Matrix3x4::from_column_slice(ptr) } pub fn make_mat4(ptr: &[N]) -> Mat { - unimplemented!() + Matrix4::from_column_slice(ptr) } pub fn make_mat4x2(ptr: &[N]) -> Mat { - unimplemented!() + Matrix4x2::from_column_slice(ptr) } pub fn make_mat4x3(ptr: &[N]) -> Mat { - unimplemented!() + Matrix4x3::from_column_slice(ptr) } pub fn make_mat4x4(ptr: &[N]) -> Mat { - unimplemented!() + Matrix4::from_column_slice(ptr) } pub fn make_quat(ptr: &[N]) -> Qua { - unimplemented!() + Quaternion::from_vector(Vector4::from_column_slice(ptr)) } pub fn make_vec1(v: &Vec) -> Vec { - unimplemented!() + *v } pub fn make_vec1_2(v: &Vec) -> Vec { - unimplemented!() + Vector1::new(v.x) } pub fn make_vec1_3(v: &Vec) -> Vec { - unimplemented!() + Vector1::new(v.x) } pub fn make_vec1_4(v: &Vec) -> Vec { - unimplemented!() + Vector1::new(v.x) } -pub fn make_vec2_1(v: &Vec) -> Vec { - unimplemented!() +pub fn make_vec2_1(v: &Vec) -> Vec { + Vector2::new(v.x, N::zero()) } pub fn make_vec2_2(v: &Vec) -> Vec { - unimplemented!() + *v } pub fn make_vec2_3(v: &Vec) -> Vec { - unimplemented!() + Vector2::new(v.x, v.y) } pub fn make_vec2_4(v: &Vec) -> Vec { - unimplemented!() + Vector2::new(v.x, v.y) } pub fn make_vec2(ptr: &[N]) -> Vec { - unimplemented!() + Vector2::from_column_slice(ptr) } -pub fn make_vec3_1(v: &Vec) -> Vec { - unimplemented!() +pub fn make_vec3_1(v: &Vec) -> Vec { + Vector3::new(v.x, N::zero(), N::zero()) } -pub fn make_vec3_2(v: &Vec) -> Vec { - unimplemented!() +pub fn make_vec3_2(v: &Vec) -> Vec { + Vector3::new(v.x, v.y, N::zero()) } pub fn make_vec3_3(v: &Vec) -> Vec { - unimplemented!() + *v } pub fn make_vec3_4(v: &Vec) -> Vec { - unimplemented!() + Vector3::new(v.x, v.y, v.z) } pub fn make_vec3(ptr: &[N]) -> Vec { - unimplemented!() + Vector3::from_column_slice(ptr) } -pub fn make_vec4_1(v: &Vec) -> Vec { - unimplemented!() +pub fn make_vec4_1(v: &Vec) -> Vec { + Vector4::new(v.x, N::zero(), N::zero(), N::zero()) } -pub fn make_vec4_2(v: &Vec) -> Vec { - unimplemented!() +pub fn make_vec4_2(v: &Vec) -> Vec { + Vector4::new(v.x, v.y, N::zero(), N::zero()) } -pub fn make_vec4_3(v: &Vec) -> Vec { - unimplemented!() +pub fn make_vec4_3(v: &Vec) -> Vec { + Vector4::new(v.x, v.y, v.z, N::zero()) } pub fn make_vec4_4(v: &Vec) -> Vec { - unimplemented!() + *v } pub fn make_vec4(ptr: &[N]) -> Vec { - unimplemented!() + Vector4::from_column_slice(ptr) } -pub fn value_ptr(x: &T) -> &V { - unimplemented!() +pub fn value_ptr(x: &Mat) -> &[N] + where DefaultAllocator: Alloc { + x.as_slice() +} + +pub fn value_ptr_mut(x: &mut Mat) -> &mut [N] + where DefaultAllocator: Alloc { + x.as_mut_slice() } diff --git a/nalgebra-glm/src/integer.rs b/nalgebra-glm/src/integer.rs index 0e96e0d7..0a17c08e 100644 --- a/nalgebra-glm/src/integer.rs +++ b/nalgebra-glm/src/integer.rs @@ -1,28 +1,28 @@ -use na::{Scalar, Real, DimName, U3, DefaultAllocator}; +use na::{Scalar, Real, U3, DefaultAllocator}; -use traits::{Number, Alloc}; +use traits::{Number, Alloc, Dimension}; use aliases::Vec; pub fn bitCount(v: T) -> i32 { unimplemented!() } -pub fn bitCount2(v: &Vec) -> Vec +pub fn bitCount2(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn bitfieldExtract(Value: &Vec, Offset: i32, Bits: i32) -> Vec +pub fn bitfieldExtract(Value: &Vec, Offset: i32, Bits: i32) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn bitfieldInsert(Base: &Vec, Insert: &Vec, Offset: i32, Bits: i32) -> Vec +pub fn bitfieldInsert(Base: &Vec, Insert: &Vec, Offset: i32, Bits: i32) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn bitfieldReverse(v: &Vec) -> Vec +pub fn bitfieldReverse(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -31,7 +31,7 @@ pub fn findLSB(x: IU) -> u32 { unimplemented!() } -pub fn findLSB2(v: &Vec) -> Vec +pub fn findLSB2(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } @@ -40,27 +40,27 @@ pub fn findMSB(x: IU) -> i32 { unimplemented!() } -pub fn findMSB2(v: &Vec) -> Vec +pub fn findMSB2(v: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn imulExtended(x: &Vec, y: &Vec, msb: &Vec, lsb: &Vec) +pub fn imulExtended(x: &Vec, y: &Vec, msb: &Vec, lsb: &Vec) where DefaultAllocator: Alloc { unimplemented!() } -pub fn uaddCarry(x: &Vec, y: &Vec, carry: &Vec) -> Vec +pub fn uaddCarry(x: &Vec, y: &Vec, carry: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } -pub fn umulExtended(x: &Vec, y: &Vec, msb: &Vec, lsb: &Vec) +pub fn umulExtended(x: &Vec, y: &Vec, msb: &Vec, lsb: &Vec) where DefaultAllocator: Alloc { unimplemented!() } -pub fn usubBorrow(x: &Vec, y: &Vec, borrow: &Vec) -> Vec +pub fn usubBorrow(x: &Vec, y: &Vec, borrow: &Vec) -> Vec where DefaultAllocator: Alloc { unimplemented!() } diff --git a/nalgebra-glm/src/lib.rs b/nalgebra-glm/src/lib.rs index 869ae333..6b1688ba 100644 --- a/nalgebra-glm/src/lib.rs +++ b/nalgebra-glm/src/lib.rs @@ -1,4 +1,6 @@ extern crate num_traits as num; +#[macro_use] +extern crate approx; extern crate alga; extern crate nalgebra as na; @@ -11,6 +13,8 @@ pub use trigonometric::*; pub use vector_relational::*; pub use exponential::*; +pub use ext_vector_relational::*; + mod aliases; pub mod constructors; mod common; @@ -30,6 +34,7 @@ pub mod ext_quaternion_common; pub mod ext_quaternion_geometric; pub mod ext_quaternion_transform; pub mod ext_quaternion_trigonometric; +pub mod ext_quaternion_relational; pub mod ext_scalar_common; pub mod ext_scalar_constants; pub mod ext_vector_common; diff --git a/nalgebra-glm/src/matrix.rs b/nalgebra-glm/src/matrix.rs index 2fb9176f..99b9e565 100644 --- a/nalgebra-glm/src/matrix.rs +++ b/nalgebra-glm/src/matrix.rs @@ -1,32 +1,31 @@ use num::Num; -use traits::{Alloc, Number}; -use na::{Scalar, Real, DimName, DefaultAllocator, U1}; -use na::allocator::Allocator; +use na::{Scalar, Real, DefaultAllocator, U1}; +use traits::{Alloc, Dimension, Number}; use aliases::{Mat, Vec}; -//pub fn determinant(m: &Mat) -> N -// where DefaultAllocator: Allocator { -// m.determinant() -//} +pub fn determinant(m: &Mat) -> N + where DefaultAllocator: Alloc { + m.determinant() +} -pub fn inverse(m: &Mat) -> Mat +pub fn inverse(m: &Mat) -> Mat where DefaultAllocator: Alloc { m.clone().try_inverse().unwrap_or(Mat::::zeros()) } -pub fn matrix_comp_mult(x: &Mat, y: &Mat) -> Mat +pub fn matrix_comp_mult(x: &Mat, y: &Mat) -> Mat where DefaultAllocator: Alloc { x.component_mul(y) } -pub fn outer_product(c: &Vec, r: &Vec) -> Mat +pub fn outer_product(c: &Vec, r: &Vec) -> Mat where DefaultAllocator: Alloc { c * r.transpose() } -pub fn transpose(x: &Mat) -> Mat +pub fn transpose(x: &Mat) -> Mat where DefaultAllocator: Alloc { x.transpose() } \ No newline at end of file diff --git a/nalgebra-glm/src/traits.rs b/nalgebra-glm/src/traits.rs index 0106ec64..7b1f40d8 100644 --- a/nalgebra-glm/src/traits.rs +++ b/nalgebra-glm/src/traits.rs @@ -1,18 +1,23 @@ use std::cmp::{PartialOrd, PartialEq}; +use num::Signed; +use approx::AbsDiffEq; -use alga::general::Ring; -use na::{Scalar, DimName, U1}; +use alga::general::{Ring, Lattice}; +use na::{Scalar, DimName, DimMin, U1}; use na::allocator::Allocator; +pub trait Dimension: DimName + DimMin {} +impl> Dimension for D {} -pub trait Number: Scalar + Ring + PartialOrd + PartialEq { + +pub trait Number: Scalar + Ring + Lattice + AbsDiffEq + Signed { } -impl Number for T { +impl + Signed> Number for T { } #[doc(hidden)] -pub trait Alloc: +pub trait Alloc: Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + @@ -21,11 +26,12 @@ Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + -Allocator + Allocator +Allocator + Allocator + +Allocator<(usize, usize), R> + Allocator<(usize, usize), C> { } -impl +impl Alloc for T where T: Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + @@ -35,5 +41,7 @@ where T: Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + Allocator + - Allocator + Allocator { + Allocator + Allocator + + Allocator<(usize, usize), R> + Allocator<(usize, usize), C> +{ } \ No newline at end of file diff --git a/nalgebra-glm/src/trigonometric.rs b/nalgebra-glm/src/trigonometric.rs index 56c08d63..f590ce1a 100644 --- a/nalgebra-glm/src/trigonometric.rs +++ b/nalgebra-glm/src/trigonometric.rs @@ -1,73 +1,79 @@ -use na::{self, Real, DimName, DefaultAllocator}; +use na::{self, Real, DefaultAllocator}; use aliases::Vec; -use traits::Alloc; +use traits::{Alloc, Dimension}; -pub fn acos(x: &Vec) -> Vec +pub fn acos(x: &Vec) -> Vec where DefaultAllocator: Alloc { x.map(|e| e.acos()) } -pub fn acosh(x: &Vec) -> Vec + +pub fn acosh(x: &Vec) -> Vec where DefaultAllocator: Alloc { x.map(|e| e.acosh()) } -pub fn asin(x: &Vec) -> Vec + +pub fn asin(x: &Vec) -> Vec where DefaultAllocator: Alloc { x.map(|e| e.asin()) } -pub fn asinh(x: &Vec) -> Vec + +pub fn asinh(x: &Vec) -> Vec where DefaultAllocator: Alloc { x.map(|e| e.asinh()) } -pub fn atan2(y: &Vec, x: &Vec) -> Vec + +pub fn atan2(y: &Vec, x: &Vec) -> Vec where DefaultAllocator: Alloc { y.zip_map(x, |y, x| y.atan2(x)) } -pub fn atan(y_over_x: &Vec) -> Vec + +pub fn atan(y_over_x: &Vec) -> Vec where DefaultAllocator: Alloc { y_over_x.map(|e| e.atan()) } -pub fn atanh(x: &Vec) -> Vec + +pub fn atanh(x: &Vec) -> Vec where DefaultAllocator: Alloc { x.map(|e| e.atanh()) } -pub fn cos(angle: &Vec) -> Vec +pub fn cos(angle: &Vec) -> Vec where DefaultAllocator: Alloc { angle.map(|e| e.cos()) } -pub fn cosh(angle: &Vec) -> Vec +pub fn cosh(angle: &Vec) -> Vec where DefaultAllocator: Alloc { angle.map(|e| e.cosh()) } -pub fn degrees(radians: &Vec) -> Vec +pub fn degrees(radians: &Vec) -> Vec where DefaultAllocator: Alloc { radians.map(|e| e * na::convert(180.0) / N::pi()) } -pub fn radians(degrees: &Vec) -> Vec +pub fn radians(degrees: &Vec) -> Vec where DefaultAllocator: Alloc { degrees.map(|e| e * N::pi() / na::convert(180.0)) } -pub fn sin(angle: &Vec) -> Vec +pub fn sin(angle: &Vec) -> Vec where DefaultAllocator: Alloc { angle.map(|e| e.sin()) } -pub fn sinh(angle: &Vec) -> Vec +pub fn sinh(angle: &Vec) -> Vec where DefaultAllocator: Alloc { angle.map(|e| e.sinh()) } -pub fn tan(angle: &Vec) -> Vec +pub fn tan(angle: &Vec) -> Vec where DefaultAllocator: Alloc { angle.map(|e| e.tan()) } -pub fn tanh(angle: &Vec) -> Vec +pub fn tanh(angle: &Vec) -> Vec where DefaultAllocator: Alloc { angle.map(|e| e.tanh()) -} \ No newline at end of file +} diff --git a/nalgebra-glm/src/vector_relational.rs b/nalgebra-glm/src/vector_relational.rs index 28c979ae..ae1a2a2e 100644 --- a/nalgebra-glm/src/vector_relational.rs +++ b/nalgebra-glm/src/vector_relational.rs @@ -1,50 +1,50 @@ -use na::{self, Real, DimName, DefaultAllocator}; +use na::{self, Real, DefaultAllocator}; use aliases::Vec; -use traits::{Number, Alloc}; +use traits::{Number, Alloc, Dimension}; -pub fn all(v: &Vec) -> bool +pub fn all(v: &Vec) -> bool where DefaultAllocator: Alloc { v.iter().all(|x| *x) } -pub fn any(v: &Vec) -> bool +pub fn any(v: &Vec) -> bool where DefaultAllocator: Alloc { v.iter().any(|x| *x) } -pub fn equal(x: &Vec, y: &Vec) -> Vec +pub fn equal(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { x.zip_map(y, |x, y| x == y) } -pub fn greaterThan(x: &Vec, y: &Vec) -> Vec +pub fn greater_than(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { x.zip_map(y, |x, y| x > y) } -pub fn greaterThanEqual(x: &Vec, y: &Vec) -> Vec +pub fn greater_than_equal(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { x.zip_map(y, |x, y| x >= y) } -pub fn lessThan(x: &Vec, y: &Vec) -> Vec +pub fn less_than(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { x.zip_map(y, |x, y| x < y) } -pub fn lessThanEqual(x: &Vec, y: &Vec) -> Vec +pub fn less_than_equal(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { x.zip_map(y, |x, y| x <= y) } -pub fn not(v: &Vec) -> Vec +pub fn not(v: &Vec) -> Vec where DefaultAllocator: Alloc { v.map(|x| !x) } -pub fn notEqual(x: &Vec, y: &Vec) -> Vec +pub fn not_equal(x: &Vec, y: &Vec) -> Vec where DefaultAllocator: Alloc { x.zip_map(y, |x, y| x != y) }