diff --git a/nalgebra-glm/Cargo.toml b/nalgebra-glm/Cargo.toml index 7c4aacb2..456dabce 100644 --- a/nalgebra-glm/Cargo.toml +++ b/nalgebra-glm/Cargo.toml @@ -15,7 +15,7 @@ edition = "2018" [features] default = [ "std" ] -std = [ "nalgebra/std", "alga/std" ] +std = [ "nalgebra/std", "simba/std" ] stdweb = [ "nalgebra/stdweb" ] arbitrary = [ "nalgebra/arbitrary" ] serde-serialize = [ "nalgebra/serde-serialize" ] @@ -24,5 +24,5 @@ abomonation-serialize = [ "nalgebra/abomonation-serialize" ] [dependencies] num-traits = { version = "0.2", default-features = false } approx = { version = "0.3", default-features = false } -alga = { version = "0.9", default-features = false } +simba = { version = "0.1", default-features = false } nalgebra = { path = "..", version = "0.20", default-features = false } diff --git a/nalgebra-glm/src/ext/mod.rs b/nalgebra-glm/src/ext/mod.rs index 52aa9dc3..1923e806 100644 --- a/nalgebra-glm/src/ext/mod.rs +++ b/nalgebra-glm/src/ext/mod.rs @@ -1,19 +1,13 @@ //! (Reexported) Additional features not specified by GLSL specification pub use self::matrix_clip_space::{ - ortho, ortho_lh, ortho_lh_no, ortho_lh_zo, ortho_no, ortho_rh, ortho_rh_no, ortho_rh_zo, - ortho_zo, - - perspective, perspective_lh, perspective_lh_no, perspective_lh_zo, perspective_no, - perspective_rh, perspective_rh_no, perspective_rh_zo, perspective_zo, - - perspective_fov, perspective_fov_lh,perspective_fov_lh_no, perspective_fov_lh_zo, + infinite_perspective_rh_no, infinite_perspective_rh_zo, ortho, ortho_lh, ortho_lh_no, + ortho_lh_zo, ortho_no, ortho_rh, ortho_rh_no, ortho_rh_zo, ortho_zo, perspective, + perspective_fov, perspective_fov_lh, perspective_fov_lh_no, perspective_fov_lh_zo, perspective_fov_no, perspective_fov_rh, perspective_fov_rh_no, perspective_fov_rh_zo, - perspective_fov_zo, - - infinite_perspective_rh_no, infinite_perspective_rh_zo, - - reversed_perspective_rh_zo, reversed_infinite_perspective_rh_zo, + perspective_fov_zo, perspective_lh, perspective_lh_no, perspective_lh_zo, perspective_no, + perspective_rh, perspective_rh_no, perspective_rh_zo, perspective_zo, + reversed_infinite_perspective_rh_zo, reversed_perspective_rh_zo, }; pub use self::matrix_projection::{ pick_matrix, project, project_no, project_zo, unproject, unproject_no, unproject_zo, @@ -35,7 +29,9 @@ pub use self::quaternion_relational::{ }; pub use self::quaternion_transform::{quat_exp, quat_log, quat_pow, quat_rotate}; pub use self::quaternion_trigonometric::{quat_angle, quat_angle_axis, quat_axis}; -pub use self::scalar_common::{max3_scalar, max4_scalar, min3_scalar, min4_scalar}; +pub use self::scalar_common::{ + max2_scalar, max3_scalar, max4_scalar, min2_scalar, min3_scalar, min4_scalar, +}; pub use self::scalar_constants::{epsilon, pi}; pub use self::vector_common::{max, max2, max3, max4, min, min2, min3, min4}; pub use self::vector_relational::{equal_eps, equal_eps_vec, not_equal_eps, not_equal_eps_vec}; diff --git a/nalgebra-glm/src/ext/scalar_common.rs b/nalgebra-glm/src/ext/scalar_common.rs index 11104ce2..b3bd9c13 100644 --- a/nalgebra-glm/src/ext/scalar_common.rs +++ b/nalgebra-glm/src/ext/scalar_common.rs @@ -1,7 +1,51 @@ -use na; - use crate::traits::Number; +/// Returns the maximum among two values. +/// +/// # Examples: +/// +/// ``` +/// # use nalgebra_glm as glm; +/// assert_eq!(2.0, glm::max2_scalar(1.0, 2.0)); +/// assert_eq!(1, glm::max2_scalar(0, 1)); +/// ``` +/// +/// # See also: +/// +/// * [`max4_scalar`](fn.max4_scalar.html) +/// * [`min3_scalar`](fn.min3_scalar.html) +/// * [`min4_scalar`](fn.min4_scalar.html) +pub fn max2_scalar(a: N, b: N) -> N { + if a >= b { + a + } else { + b + } +} + +/// Returns the maximum among two values. +/// +/// # Examples: +/// +/// ``` +/// # use nalgebra_glm as glm; +/// assert_eq!(1.0, glm::min2_scalar(1.0, 2.0)); +/// assert_eq!(0, glm::min2_scalar(0, 1)); +/// ``` +/// +/// # See also: +/// +/// * [`max4_scalar`](fn.max4_scalar.html) +/// * [`min3_scalar`](fn.min3_scalar.html) +/// * [`min4_scalar`](fn.min4_scalar.html) +pub fn min2_scalar(a: N, b: N) -> N { + if a <= b { + a + } else { + b + } +} + /// Returns the maximum among three values. /// /// # Examples: @@ -18,7 +62,7 @@ use crate::traits::Number; /// * [`min3_scalar`](fn.min3_scalar.html) /// * [`min4_scalar`](fn.min4_scalar.html) pub fn max3_scalar(a: N, b: N, c: N) -> N { - na::sup(&na::sup(&a, &b), &c) + max2_scalar(max2_scalar(a, b), c) } /// Returns the maximum among four values. @@ -37,7 +81,7 @@ pub fn max3_scalar(a: N, b: N, c: N) -> N { /// * [`min3_scalar`](fn.min3_scalar.html) /// * [`min4_scalar`](fn.min4_scalar.html) pub fn max4_scalar(a: N, b: N, c: N, d: N) -> N { - na::sup(&na::sup(&a, &b), &na::sup(&c, &d)) + max2_scalar(max2_scalar(a, b), max2_scalar(c, d)) } /// Returns the minimum among three values. @@ -56,7 +100,7 @@ pub fn max4_scalar(a: N, b: N, c: N, d: N) -> N { /// * [`max4_scalar`](fn.max4_scalar.html) /// * [`min4_scalar`](fn.min4_scalar.html) pub fn min3_scalar(a: N, b: N, c: N) -> N { - na::inf(&na::inf(&a, &b), &c) + min2_scalar(min2_scalar(a, b), c) } /// Returns the minimum among four values. @@ -75,5 +119,5 @@ pub fn min3_scalar(a: N, b: N, c: N) -> N { /// * [`max4_scalar`](fn.max4_scalar.html) /// * [`min3_scalar`](fn.min3_scalar.html) pub fn min4_scalar(a: N, b: N, c: N, d: N) -> N { - na::inf(&na::inf(&a, &b), &na::inf(&c, &d)) + min2_scalar(min2_scalar(a, b), min2_scalar(c, d)) } diff --git a/nalgebra-glm/src/ext/vector_common.rs b/nalgebra-glm/src/ext/vector_common.rs index a6418ef8..ff892f1e 100644 --- a/nalgebra-glm/src/ext/vector_common.rs +++ b/nalgebra-glm/src/ext/vector_common.rs @@ -17,8 +17,10 @@ use crate::traits::{Alloc, Dimension, Number}; /// * [`min3`](fn.min3.html) /// * [`min4`](fn.min4.html) pub fn max(a: &TVec, b: N) -> TVec -where DefaultAllocator: Alloc { - a.map(|a| na::sup(&a, &b)) +where + DefaultAllocator: Alloc, +{ + a.map(|a| crate::max2_scalar(a, b)) } /// Component-wise maximum between two vectors. @@ -35,8 +37,10 @@ where DefaultAllocator: Alloc { /// * [`min3`](fn.min3.html) /// * [`min4`](fn.min4.html) pub fn max2(a: &TVec, b: &TVec) -> TVec -where DefaultAllocator: Alloc { - na::sup(a, b) +where + DefaultAllocator: Alloc, +{ + a.zip_map(b, |a, b| crate::max2_scalar(a, b)) } /// Component-wise maximum between three vectors. @@ -53,7 +57,9 @@ where DefaultAllocator: Alloc { /// * [`min3`](fn.min3.html) /// * [`min4`](fn.min4.html) pub fn max3(a: &TVec, b: &TVec, c: &TVec) -> TVec -where DefaultAllocator: Alloc { +where + DefaultAllocator: Alloc, +{ max2(&max2(a, b), c) } @@ -96,8 +102,10 @@ where /// * [`min3`](fn.min3.html) /// * [`min4`](fn.min4.html) pub fn min(x: &TVec, y: N) -> TVec -where DefaultAllocator: Alloc { - x.map(|x| na::inf(&x, &y)) +where + DefaultAllocator: Alloc, +{ + x.map(|x| crate::min2_scalar(x, y)) } /// Component-wise minimum between two vectors. @@ -114,8 +122,10 @@ where DefaultAllocator: Alloc { /// * [`min3`](fn.min3.html) /// * [`min4`](fn.min4.html) pub fn min2(x: &TVec, y: &TVec) -> TVec -where DefaultAllocator: Alloc { - na::inf(x, y) +where + DefaultAllocator: Alloc, +{ + x.zip_map(y, |a, b| crate::min2_scalar(a, b)) } /// Component-wise minimum between three vectors. @@ -132,7 +142,9 @@ where DefaultAllocator: Alloc { /// * [`min2`](fn.min2.html) /// * [`min4`](fn.min4.html) pub fn min3(a: &TVec, b: &TVec, c: &TVec) -> TVec -where DefaultAllocator: Alloc { +where + DefaultAllocator: Alloc, +{ min2(&min2(a, b), c) } diff --git a/nalgebra-glm/src/gtx/component_wise.rs b/nalgebra-glm/src/gtx/component_wise.rs index 7c4af7a0..cfb851f4 100644 --- a/nalgebra-glm/src/gtx/component_wise.rs +++ b/nalgebra-glm/src/gtx/component_wise.rs @@ -22,7 +22,9 @@ use crate::traits::{Alloc, Dimension, Number}; /// * [`comp_min`](fn.comp_min.html) /// * [`comp_mul`](fn.comp_mul.html) pub fn comp_add(m: &TMat) -> N -where DefaultAllocator: Alloc { +where + DefaultAllocator: Alloc, +{ m.iter().fold(N::zero(), |x, y| x + *y) } @@ -49,8 +51,11 @@ where DefaultAllocator: Alloc { /// * [`max3`](fn.max3.html) /// * [`max4`](fn.max4.html) pub fn comp_max(m: &TMat) -> N -where DefaultAllocator: Alloc { - m.iter().fold(N::min_value(), |x, y| na::sup(&x, y)) +where + DefaultAllocator: Alloc, +{ + m.iter() + .fold(N::min_value(), |x, y| crate::max2_scalar(x, *y)) } /// The minimum of every component of the given matrix or vector. @@ -76,8 +81,11 @@ where DefaultAllocator: Alloc { /// * [`min3`](fn.min3.html) /// * [`min4`](fn.min4.html) pub fn comp_min(m: &TMat) -> N -where DefaultAllocator: Alloc { - m.iter().fold(N::max_value(), |x, y| na::inf(&x, y)) +where + DefaultAllocator: Alloc, +{ + m.iter() + .fold(N::max_value(), |x, y| crate::min2_scalar(x, *y)) } /// The product of every component of the given matrix or vector. @@ -99,7 +107,9 @@ where DefaultAllocator: Alloc { /// * [`comp_max`](fn.comp_max.html) /// * [`comp_min`](fn.comp_min.html) pub fn comp_mul(m: &TMat) -> N -where DefaultAllocator: Alloc { +where + DefaultAllocator: Alloc, +{ m.iter().fold(N::one(), |x, y| x * *y) } diff --git a/nalgebra-glm/src/lib.rs b/nalgebra-glm/src/lib.rs index c8f266a4..e9c64160 100644 --- a/nalgebra-glm/src/lib.rs +++ b/nalgebra-glm/src/lib.rs @@ -116,10 +116,10 @@ extern crate num_traits as num; #[macro_use] extern crate approx; -extern crate alga; extern crate nalgebra as na; pub use crate::aliases::*; +pub use crate::traits::{Alloc, Dimension, Number}; pub use common::{ abs, ceil, clamp, clamp_scalar, clamp_vec, float_bits_to_int, float_bits_to_int_vec, float_bits_to_uint, float_bits_to_uint_vec, floor, fract, int_bits_to_float, @@ -133,7 +133,6 @@ pub use geometric::{ cross, distance, dot, faceforward, length, magnitude, normalize, reflect_vec, refract_vec, }; pub use matrix::{determinant, inverse, matrix_comp_mult, outer_product, transpose}; -pub use crate::traits::{Alloc, Dimension, Number}; pub use trigonometric::{ acos, acosh, asin, asinh, atan, atan2, atanh, cos, cosh, degrees, radians, sin, sinh, tan, tanh, }; @@ -143,20 +142,20 @@ pub use vector_relational::{ pub use ext::{ epsilon, equal_columns, equal_columns_eps, equal_columns_eps_vec, equal_eps, equal_eps_vec, - identity, look_at, look_at_lh, look_at_rh, max, max2, max3, max3_scalar, max4, max4_scalar, - min, min2, min3, min3_scalar, min4, min4_scalar, not_equal_columns, not_equal_columns_eps, - not_equal_columns_eps_vec, not_equal_eps, not_equal_eps_vec, ortho, perspective, perspective_fov, - perspective_fov_lh,perspective_fov_lh_no, perspective_fov_lh_zo, perspective_fov_no, - perspective_fov_rh, perspective_fov_rh_no, perspective_fov_rh_zo, perspective_fov_zo, - perspective_lh, perspective_lh_no, perspective_lh_zo, perspective_no, perspective_rh, - perspective_rh_no, perspective_rh_zo, perspective_zo, ortho_lh, ortho_lh_no, ortho_lh_zo, - ortho_no, ortho_rh, ortho_rh_no, ortho_rh_zo, ortho_zo, pi, pick_matrix, project, project_no, - project_zo, quat_angle, quat_angle_axis, quat_axis, quat_conjugate, quat_cross, quat_dot, - quat_equal, quat_equal_eps, quat_exp, quat_inverse, quat_length, quat_lerp, quat_log, + identity, infinite_perspective_rh_no, infinite_perspective_rh_zo, look_at, look_at_lh, + look_at_rh, max, max2, max2_scalar, max3, max3_scalar, max4, max4_scalar, min, min2, + min2_scalar, min3, min3_scalar, min4, min4_scalar, not_equal_columns, not_equal_columns_eps, + not_equal_columns_eps_vec, not_equal_eps, not_equal_eps_vec, ortho, ortho_lh, ortho_lh_no, + ortho_lh_zo, ortho_no, ortho_rh, ortho_rh_no, ortho_rh_zo, ortho_zo, perspective, + perspective_fov, perspective_fov_lh, perspective_fov_lh_no, perspective_fov_lh_zo, + perspective_fov_no, perspective_fov_rh, perspective_fov_rh_no, perspective_fov_rh_zo, + perspective_fov_zo, perspective_lh, perspective_lh_no, perspective_lh_zo, perspective_no, + perspective_rh, perspective_rh_no, perspective_rh_zo, perspective_zo, pi, pick_matrix, project, + project_no, project_zo, quat_angle, quat_angle_axis, quat_axis, quat_conjugate, quat_cross, + quat_dot, quat_equal, quat_equal_eps, quat_exp, quat_inverse, quat_length, quat_lerp, quat_log, quat_magnitude, quat_normalize, quat_not_equal, quat_not_equal_eps, quat_pow, quat_rotate, - quat_slerp, rotate, rotate_x, rotate_y, rotate_z, scale, translate, unproject, unproject_no, - unproject_zo, infinite_perspective_rh_no, infinite_perspective_rh_zo, - reversed_perspective_rh_zo, reversed_infinite_perspective_rh_zo, + quat_slerp, reversed_infinite_perspective_rh_zo, reversed_perspective_rh_zo, rotate, rotate_x, + rotate_y, rotate_z, scale, translate, unproject, unproject_no, unproject_zo, }; pub use gtc::{ affine_inverse, column, e, euler, four_over_pi, golden_ratio, half_pi, inverse_transpose, diff --git a/nalgebra-glm/src/traits.rs b/nalgebra-glm/src/traits.rs index 1783ee5d..0857484e 100644 --- a/nalgebra-glm/src/traits.rs +++ b/nalgebra-glm/src/traits.rs @@ -3,7 +3,8 @@ use num::{Bounded, FromPrimitive, Signed}; use na::allocator::Allocator; use na::{DimMin, DimName, Scalar, U1}; -use simba::scalar::{Lattice, Ring}; +use simba::scalar::{ClosedAdd, ClosedMul, ClosedSub}; +use std::cmp::PartialOrd; /// A type-level number representing a vector, matrix row, or matrix column, dimension. pub trait Dimension: DimName + DimMin {} @@ -11,15 +12,26 @@ impl> Dimension for D {} /// A number that can either be an integer or a float. pub trait Number: - Scalar + Copy + Ring + Lattice + AbsDiffEq + Signed + FromPrimitive + Bounded + Scalar + + Copy + + PartialOrd + + ClosedAdd + + ClosedSub + + ClosedMul + + AbsDiffEq + + Signed + + FromPrimitive + + Bounded { } impl< T: Scalar + Copy - + Ring - + Lattice + + PartialOrd + + ClosedAdd + + ClosedSub + + ClosedMul + AbsDiffEq + Signed + FromPrimitive