2019-03-25 18:21:41 +08:00
use na ::{ self , DefaultAllocator , RealField } ;
2018-09-21 01:54:12 +08:00
use num ::FromPrimitive ;
2019-04-27 01:38:03 +08:00
use core ::mem ;
2018-09-20 16:50:34 +08:00
2019-03-23 21:29:07 +08:00
use crate ::aliases ::{ TMat , TVec } ;
use crate ::traits ::{ Alloc , Dimension , Number } ;
2018-09-20 16:50:34 +08:00
2018-09-22 19:18:59 +08:00
/// For each matrix or vector component `x` if `x >= 0`; otherwise, it returns `-x`.
2018-10-04 21:49:01 +08:00
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let vec = glm::vec3(-1.0, 0.0, 2.0);
/// assert_eq!(glm::vec3(1.0, 0.0, 2.0), glm::abs(&vec));
///
/// let mat = glm::mat2(-0.0, 1.0, -3.0, 2.0);
/// assert_eq!(glm::mat2(0.0, 1.0, 3.0, 2.0), glm::abs(&mat));
/// ```
///
/// # See also:
///
/// * [`sign`](fn.sign.html)
2018-09-23 20:48:45 +08:00
pub fn abs < N : Number , R : Dimension , C : Dimension > ( x : & TMat < N , R , C > ) -> TMat < N , R , C >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , R , C > {
2018-10-22 04:11:41 +08:00
x . abs ( )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// For each matrix or vector component returns a value equal to the nearest integer that is greater than or equal to `x`.
2018-10-04 21:49:01 +08:00
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let vec = glm::vec3(-1.5, 0.5, 2.8);
/// assert_eq!(glm::vec3(-1.0, 1.0, 3.0), glm::ceil(&vec));
/// ```
///
/// # See also:
///
/// * [`ceil`](fn.ceil.html)
/// * [`floor`](fn.floor.html)
/// * [`fract`](fn.fract.html)
/// * [`round`](fn.round.html)
/// * [`trunc`](fn.trunc.html)
2019-03-25 18:21:41 +08:00
pub fn ceil < N : RealField , D : Dimension > ( x : & TVec < N , D > ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-09-21 01:54:12 +08:00
x . map ( | x | x . ceil ( ) )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns `min(max(x, min_val), max_val)`.
2018-10-04 21:49:01 +08:00
///
2018-10-06 22:22:46 +08:00
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// // Works with integers:
/// assert_eq!(3, glm::clamp_scalar(1, 3, 5));
/// assert_eq!(4, glm::clamp_scalar(4, 3, 5));
/// assert_eq!(5, glm::clamp_scalar(7, 3, 5));
///
/// // And it works with floats:
/// assert_eq!(3.25, glm::clamp_scalar(1.3, 3.25, 5.5));
/// assert_eq!(4.5, glm::clamp_scalar(4.5, 3.25, 5.5));
/// assert_eq!(5.5, glm::clamp_scalar(7.8, 3.25, 5.5));
/// ```
///
2018-10-04 21:49:01 +08:00
/// # See also:
///
/// * [`clamp`](fn.clamp.html)
/// * [`clamp_vec`](fn.clamp_vec.html)
2018-09-23 12:50:39 +08:00
pub fn clamp_scalar < N : Number > ( x : N , min_val : N , max_val : N ) -> N {
2018-09-22 19:18:59 +08:00
na ::clamp ( x , min_val , max_val )
2018-09-20 16:50:34 +08:00
}
2018-10-06 22:22:46 +08:00
/// Returns `min(max(x[i], min_val), max_val)` for each component in `x`
/// using the values `min_val and `max_val` as bounds.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// // Works with integers:
/// assert_eq!(glm::vec3(3, 4, 5),
/// glm::clamp(&glm::vec3(1, 4, 7), 3, 5));
///
/// // And it works with floats:
/// assert_eq!(glm::vec3(3.25, 4.5, 5.5),
/// glm::clamp(&glm::vec3(1.3, 4.5, 7.8), 3.25, 5.5));
/// ```
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`clamp_scalar`](fn.clamp_scalar.html)
/// * [`clamp_vec`](fn.clamp_vec.html)
2018-09-23 20:48:45 +08:00
pub fn clamp < N : Number , D : Dimension > ( x : & TVec < N , D > , min_val : N , max_val : N ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-09-22 19:18:59 +08:00
x . map ( | x | na ::clamp ( x , min_val , max_val ) )
2018-09-20 16:50:34 +08:00
}
2018-10-06 22:22:46 +08:00
/// Returns `min(max(x[i], min_val[i]), max_val[i])` for each component in `x`
/// using the components of `min_val` and `max_val` as bounds.
2018-10-04 21:49:01 +08:00
///
2018-10-07 12:31:41 +08:00
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let min_bounds = glm::vec2(1.0, 3.0);
/// let max_bounds = glm::vec2(5.0, 6.0);
/// assert_eq!(glm::vec2(1.0, 6.0),
/// glm::clamp_vec(&glm::vec2(0.0, 7.0),
/// &min_bounds,
/// &max_bounds));
/// assert_eq!(glm::vec2(2.0, 6.0),
/// glm::clamp_vec(&glm::vec2(2.0, 7.0),
/// &min_bounds,
/// &max_bounds));
/// assert_eq!(glm::vec2(1.0, 4.0),
/// glm::clamp_vec(&glm::vec2(0.0, 4.0),
/// &min_bounds,
/// &max_bounds));
/// ```
///
2018-10-04 21:49:01 +08:00
/// # See also:
///
/// * [`clamp_scalar`](fn.clamp_scalar.html)
/// * [`clamp`](fn.clamp.html)
2018-10-22 04:11:41 +08:00
pub fn clamp_vec < N : Number , D : Dimension > (
x : & TVec < N , D > ,
min_val : & TVec < N , D > ,
max_val : & TVec < N , D > ,
) -> TVec < N , D >
where
DefaultAllocator : Alloc < N , D > ,
{
2018-10-07 12:31:41 +08:00
x . zip_zip_map ( min_val , max_val , | a , min , max | na ::clamp ( a , min , max ) )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns a signed integer value representing the encoding of a floating-point value.
///
/// The floating-point value's bit-level representation is preserved.
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`float_bits_to_int_vec`](fn.float_bits_to_int_vec.html)
/// * [`float_bits_to_uint`](fn.float_bits_to_uint.html)
/// * [`float_bits_to_uint_vec`](fn.float_bits_to_uint_vec.html)
/// * [`int_bits_to_float`](fn.int_bits_to_float.html)
/// * [`int_bits_to_float_vec`](fn.int_bits_to_float_vec.html)
/// * [`uint_bits_to_float`](fn.uint_bits_to_float.html)
/// * [`uint_bits_to_float_scalar`](fn.uint_bits_to_float_scalar.html)
2018-09-21 01:54:12 +08:00
pub fn float_bits_to_int ( v : f32 ) -> i32 {
unsafe { mem ::transmute ( v ) }
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns a signed integer value representing the encoding of each component of `v`.
///
/// The floating point value's bit-level representation is preserved.
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`float_bits_to_int`](fn.float_bits_to_int.html)
/// * [`float_bits_to_uint`](fn.float_bits_to_uint.html)
/// * [`float_bits_to_uint_vec`](fn.float_bits_to_uint_vec.html)
/// * [`int_bits_to_float`](fn.int_bits_to_float.html)
/// * [`int_bits_to_float_vec`](fn.int_bits_to_float_vec.html)
/// * [`uint_bits_to_float`](fn.uint_bits_to_float.html)
/// * [`uint_bits_to_float_scalar`](fn.uint_bits_to_float_scalar.html)
2018-09-23 20:48:45 +08:00
pub fn float_bits_to_int_vec < D : Dimension > ( v : & TVec < f32 , D > ) -> TVec < i32 , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < f32 , D > {
2018-10-05 11:15:17 +08:00
v . map ( float_bits_to_int )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns an unsigned integer value representing the encoding of a floating-point value.
///
/// The floating-point value's bit-level representation is preserved.
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`float_bits_to_int`](fn.float_bits_to_int.html)
/// * [`float_bits_to_int_vec`](fn.float_bits_to_int_vec.html)
/// * [`float_bits_to_uint_vec`](fn.float_bits_to_uint_vec.html)
/// * [`int_bits_to_float`](fn.int_bits_to_float.html)
/// * [`int_bits_to_float_vec`](fn.int_bits_to_float_vec.html)
/// * [`uint_bits_to_float`](fn.uint_bits_to_float.html)
/// * [`uint_bits_to_float_scalar`](fn.uint_bits_to_float_scalar.html)
2018-09-21 01:54:12 +08:00
pub fn float_bits_to_uint ( v : f32 ) -> u32 {
unsafe { mem ::transmute ( v ) }
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns an unsigned integer value representing the encoding of each component of `v`.
///
/// The floating point value's bit-level representation is preserved.
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`float_bits_to_int`](fn.float_bits_to_int.html)
/// * [`float_bits_to_int_vec`](fn.float_bits_to_int_vec.html)
/// * [`float_bits_to_uint`](fn.float_bits_to_uint.html)
/// * [`int_bits_to_float`](fn.int_bits_to_float.html)
/// * [`int_bits_to_float_vec`](fn.int_bits_to_float_vec.html)
/// * [`uint_bits_to_float`](fn.uint_bits_to_float.html)
/// * [`uint_bits_to_float_scalar`](fn.uint_bits_to_float_scalar.html)
2018-09-23 20:48:45 +08:00
pub fn float_bits_to_uint_vec < D : Dimension > ( v : & TVec < f32 , D > ) -> TVec < u32 , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < f32 , D > {
2018-10-05 11:15:17 +08:00
v . map ( float_bits_to_uint )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns componentwise a value equal to the nearest integer that is less then or equal to `x`.
2018-10-04 21:49:01 +08:00
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let vec = glm::vec3(-1.5, 0.5, 2.8);
/// assert_eq!(glm::vec3(-2.0, 0.0, 2.0), glm::floor(&vec));
/// ```
///
/// # See also:
///
/// * [`ceil`](fn.ceil.html)
/// * [`fract`](fn.fract.html)
/// * [`round`](fn.round.html)
/// * [`trunc`](fn.trunc.html)
2019-03-25 18:21:41 +08:00
pub fn floor < N : RealField , D : Dimension > ( x : & TVec < N , D > ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-09-21 01:54:12 +08:00
x . map ( | x | x . floor ( ) )
2018-09-20 16:50:34 +08:00
}
2018-09-23 20:48:45 +08:00
//// FIXME: should be implemented for TVec/TMat?
2018-09-22 19:18:59 +08:00
//pub fn fma<N: Number>(a: N, b: N, c: N) -> N {
// // FIXME: use an actual FMA
// a * b + c
//}
2018-09-20 16:50:34 +08:00
2018-09-22 19:18:59 +08:00
/// Returns the fractional part of each component of `x`.
2018-10-04 21:49:01 +08:00
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let vec = glm::vec3(-1.5, 0.5, 2.25);
/// assert_eq!(glm::vec3(-0.5, 0.5, 0.25), glm::fract(&vec));
/// ```
///
/// # See also:
///
/// * [`ceil`](fn.ceil.html)
/// * [`floor`](fn.floor.html)
/// * [`round`](fn.round.html)
/// * [`trunc`](fn.trunc.html)
2019-03-25 18:21:41 +08:00
pub fn fract < N : RealField , D : Dimension > ( x : & TVec < N , D > ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-09-21 01:54:12 +08:00
x . map ( | x | x . fract ( ) )
2018-09-20 16:50:34 +08:00
}
2018-09-23 20:48:45 +08:00
//// FIXME: should be implemented for TVec/TMat?
2018-09-21 01:54:12 +08:00
///// Returns the (significant, exponent) of this float number.
2019-03-25 18:21:41 +08:00
//pub fn frexp<N: RealField>(x: N, exp: N) -> (N, N) {
2018-09-21 01:54:12 +08:00
// // FIXME: is there a better approach?
// let e = x.log2().ceil();
// (x * (-e).exp2(), e)
//}
2018-09-20 16:50:34 +08:00
2018-09-22 19:18:59 +08:00
/// Returns a floating-point value corresponding to a signed integer encoding of a floating-point value.
///
/// If an inf or NaN is passed in, it will not signal, and the resulting floating point value is unspecified. Otherwise, the bit-level representation is preserved.
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`float_bits_to_int`](fn.float_bits_to_int.html)
/// * [`float_bits_to_int_vec`](fn.float_bits_to_int_vec.html)
/// * [`float_bits_to_uint`](fn.float_bits_to_uint.html)
/// * [`float_bits_to_uint_vec`](fn.float_bits_to_uint_vec.html)
/// * [`int_bits_to_float_vec`](fn.int_bits_to_float_vec.html)
/// * [`uint_bits_to_float`](fn.uint_bits_to_float.html)
/// * [`uint_bits_to_float_scalar`](fn.uint_bits_to_float_scalar.html)
2018-09-21 01:54:12 +08:00
pub fn int_bits_to_float ( v : i32 ) -> f32 {
2018-10-05 13:28:32 +08:00
f32 ::from_bits ( v as u32 )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// For each components of `v`, returns a floating-point value corresponding to a signed integer encoding of a floating-point value.
///
/// If an inf or NaN is passed in, it will not signal, and the resulting floating point value is unspecified. Otherwise, the bit-level representation is preserved.
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`float_bits_to_int`](fn.float_bits_to_int.html)
/// * [`float_bits_to_int_vec`](fn.float_bits_to_int_vec.html)
/// * [`float_bits_to_uint`](fn.float_bits_to_uint.html)
/// * [`float_bits_to_uint_vec`](fn.float_bits_to_uint_vec.html)
/// * [`int_bits_to_float`](fn.int_bits_to_float.html)
/// * [`uint_bits_to_float`](fn.uint_bits_to_float.html)
/// * [`uint_bits_to_float_scalar`](fn.uint_bits_to_float_scalar.html)
2018-09-23 20:48:45 +08:00
pub fn int_bits_to_float_vec < D : Dimension > ( v : & TVec < i32 , D > ) -> TVec < f32 , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < f32 , D > {
2018-10-05 11:15:17 +08:00
v . map ( int_bits_to_float )
2018-09-20 16:50:34 +08:00
}
2019-12-06 06:54:17 +08:00
//pub fn isinf<N: Scalar + Clone, D: Dimension>(x: &TVec<N, D>) -> TVec<bool, D>
2018-09-21 01:54:12 +08:00
// where DefaultAllocator: Alloc<N, D> {
// unimplemented!()
//
//}
//
2019-12-06 06:54:17 +08:00
//pub fn isnan<N: Scalar + Clone, D: Dimension>(x: &TVec<N, D>) -> TVec<bool, D>
2018-09-21 01:54:12 +08:00
// where DefaultAllocator: Alloc<N, D> {
// unimplemented!()
//
//}
2018-09-20 16:50:34 +08:00
2018-09-21 01:54:12 +08:00
///// Returns the (significant, exponent) of this float number.
2019-03-25 18:21:41 +08:00
//pub fn ldexp<N: RealField>(x: N, exp: N) -> N {
2018-09-21 01:54:12 +08:00
// // FIXME: is there a better approach?
// x * (exp).exp2()
//}
2018-10-22 04:11:41 +08:00
/// Returns `x * (1.0 - a) + y * a`, i.e., the linear blend of the scalars x and y using the scalar value a.
///
/// The value for a is not restricted to the range `[0, 1]`.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// assert_eq!(glm::mix_scalar(2.0, 20.0, 0.1), 3.8);
/// ```
///
/// # See also:
///
/// * [`mix`](fn.mix.html)
/// * [`mix_vec`](fn.mix_vec.html)
pub fn mix_scalar < N : Number > ( x : N , y : N , a : N ) -> N {
x * ( N ::one ( ) - a ) + y * a
}
/// Returns `x * (1.0 - a) + y * a`, i.e., the linear blend of the vectors x and y using the scalar value a.
2018-09-22 19:18:59 +08:00
///
/// The value for a is not restricted to the range `[0, 1]`.
2018-10-22 04:11:41 +08:00
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let x = glm::vec3(1.0, 2.0, 3.0);
/// let y = glm::vec3(10.0, 20.0, 30.0);
/// assert_eq!(glm::mix(&x, &y, 0.1), glm::vec3(1.9, 3.8, 5.7));
/// ```
///
/// # See also:
///
/// * [`mix_scalar`](fn.mix_scalar.html)
/// * [`mix_vec`](fn.mix_vec.html)
pub fn mix < N : Number , D : Dimension > ( x : & TVec < N , D > , y : & TVec < N , D > , a : N ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-09-21 01:54:12 +08:00
x * ( N ::one ( ) - a ) + y * a
2018-09-20 16:50:34 +08:00
}
2018-10-22 04:11:41 +08:00
/// Returns `x * (1.0 - a) + y * a`, i.e., the component-wise linear blend of `x` and `y` using the components of
/// the vector `a` as coefficients.
///
/// The value for a is not restricted to the range `[0, 1]`.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let x = glm::vec3(1.0, 2.0, 3.0);
/// let y = glm::vec3(10.0, 20.0, 30.0);
/// let a = glm::vec3(0.1, 0.2, 0.3);
/// assert_eq!(glm::mix_vec(&x, &y, &a), glm::vec3(1.9, 5.6, 11.1));
/// ```
///
/// # See also:
///
/// * [`mix_scalar`](fn.mix_scalar.html)
/// * [`mix`](fn.mix.html)
pub fn mix_vec < N : Number , D : Dimension > (
x : & TVec < N , D > ,
y : & TVec < N , D > ,
a : & TVec < N , D > ,
) -> TVec < N , D >
where
DefaultAllocator : Alloc < N , D > ,
{
x . component_mul ( & ( TVec ::< N , D > ::repeat ( N ::one ( ) ) - a ) ) + y . component_mul ( & a )
}
/// Returns `x * (1.0 - a) + y * a`, i.e., the linear blend of the scalars x and y using the scalar value a.
///
/// The value for a is not restricted to the range `[0, 1]`.
/// This is an alias for `mix_scalar`.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// assert_eq!(glm::lerp_scalar(2.0, 20.0, 0.1), 3.8);
/// ```
///
/// # See also:
///
/// * [`lerp`](fn.lerp.html)
/// * [`lerp_vec`](fn.lerp_vec.html)
pub fn lerp_scalar < N : Number > ( x : N , y : N , a : N ) -> N {
mix_scalar ( x , y , a )
}
/// Returns `x * (1.0 - a) + y * a`, i.e., the linear blend of the vectors x and y using the scalar value a.
///
/// The value for a is not restricted to the range `[0, 1]`.
/// This is an alias for `mix`.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let x = glm::vec3(1.0, 2.0, 3.0);
/// let y = glm::vec3(10.0, 20.0, 30.0);
/// assert_eq!(glm::lerp(&x, &y, 0.1), glm::vec3(1.9, 3.8, 5.7));
/// ```
///
/// # See also:
///
/// * [`lerp_scalar`](fn.lerp_scalar.html)
/// * [`lerp_vec`](fn.lerp_vec.html)
pub fn lerp < N : Number , D : Dimension > ( x : & TVec < N , D > , y : & TVec < N , D > , a : N ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-10-22 04:11:41 +08:00
mix ( x , y , a )
}
/// Returns `x * (1.0 - a) + y * a`, i.e., the component-wise linear blend of `x` and `y` using the components of
/// the vector `a` as coefficients.
///
/// The value for a is not restricted to the range `[0, 1]`.
/// This is an alias for `mix_vec`.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let x = glm::vec3(1.0, 2.0, 3.0);
/// let y = glm::vec3(10.0, 20.0, 30.0);
/// let a = glm::vec3(0.1, 0.2, 0.3);
/// assert_eq!(glm::lerp_vec(&x, &y, &a), glm::vec3(1.9, 5.6, 11.1));
/// ```
///
/// # See also:
///
/// * [`lerp_scalar`](fn.lerp_scalar.html)
/// * [`lerp`](fn.lerp.html)
pub fn lerp_vec < N : Number , D : Dimension > (
x : & TVec < N , D > ,
y : & TVec < N , D > ,
a : & TVec < N , D > ,
) -> TVec < N , D >
where
DefaultAllocator : Alloc < N , D > ,
{
mix_vec ( x , y , a )
}
2018-09-22 19:21:02 +08:00
/// Component-wise modulus.
2018-09-22 19:18:59 +08:00
///
/// Returns `x - y * floor(x / y)` for each component in `x` using the corresponding component of `y`.
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`modf`](fn.modf.html)
2018-09-23 20:48:45 +08:00
pub fn modf_vec < N : Number , D : Dimension > ( x : & TVec < N , D > , y : & TVec < N , D > ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-09-21 01:54:12 +08:00
x . zip_map ( y , | x , y | x % y )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Modulus between two values.
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`modf_vec`](fn.modf_vec.html)
2018-09-21 01:54:12 +08:00
pub fn modf < N : Number > ( x : N , i : N ) -> N {
x % i
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:21:02 +08:00
/// Component-wise rounding.
2018-09-22 19:18:59 +08:00
///
/// Values equal to `0.5` are rounded away from `0.0`.
2018-10-04 21:49:01 +08:00
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let vec = glm::vec4(-1.5, 0.6, 1.5, -3.2);
/// assert_eq!(glm::vec4(-2.0, 1.0, 2.0, -3.0), glm::round(&vec));
/// ```
///
/// # See also:
///
/// * [`ceil`](fn.ceil.html)
/// * [`floor`](fn.floor.html)
/// * [`fract`](fn.fract.html)
/// * [`trunc`](fn.trunc.html)
2019-03-25 18:21:41 +08:00
pub fn round < N : RealField , D : Dimension > ( x : & TVec < N , D > ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-09-21 01:54:12 +08:00
x . map ( | x | x . round ( ) )
2018-09-20 16:50:34 +08:00
}
2019-12-06 06:54:17 +08:00
//pub fn roundEven<N: Scalar + Clone, D: Dimension>(x: &TVec<N, D>) -> TVec<N, D>
2018-09-21 01:54:12 +08:00
// where DefaultAllocator: Alloc<N, D> {
// unimplemented!()
//}
2018-09-20 16:50:34 +08:00
2018-10-05 11:54:52 +08:00
/// For each vector component `x`: 1 if `x > 0`, 0 if `x == 0`, or -1 if `x < 0`.
2018-10-05 03:04:37 +08:00
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
2018-10-06 22:20:57 +08:00
/// let vec = glm::vec4(-2.0, 0.0, -0.0, 2.0);
/// assert_eq!(glm::vec4(-1.0, 0.0, 0.0, 1.0), glm::sign(&vec));
2018-10-05 03:04:37 +08:00
/// ```
///
/// # See also:
///
/// * [`abs`](fn.abs.html)
///
2018-09-23 20:48:45 +08:00
pub fn sign < N : Number , D : Dimension > ( x : & TVec < N , D > ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-10-22 04:11:41 +08:00
x . map ( | x | if x . is_zero ( ) { N ::zero ( ) } else { x . signum ( ) } )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns 0.0 if `x <= edge0` and `1.0 if x >= edge1` and performs smooth Hermite interpolation between 0 and 1 when `edge0 < x < edge1`.
///
/// This is useful in cases where you would want a threshold function with a smooth transition.
/// This is equivalent to: `let result = clamp((x - edge0) / (edge1 - edge0), 0, 1); return t * t * (3 - 2 * t);` Results are undefined if `edge0 >= edge1`.
2018-09-21 01:54:12 +08:00
pub fn smoothstep < N : Number > ( 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 )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns 0.0 if `x < edge`, otherwise it returns 1.0.
2018-09-22 23:36:08 +08:00
pub fn step_scalar < N : Number > ( edge : N , x : N ) -> N {
2018-09-21 01:54:12 +08:00
if edge > x {
N ::zero ( )
} else {
N ::one ( )
}
2018-09-20 16:50:34 +08:00
}
2018-09-22 23:36:08 +08:00
2018-09-22 19:18:59 +08:00
/// Returns 0.0 if `x[i] < edge`, otherwise it returns 1.0.
2018-09-23 20:48:45 +08:00
pub fn step < N : Number , D : Dimension > ( edge : N , x : & TVec < N , D > ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-09-22 23:36:08 +08:00
x . map ( | x | step_scalar ( edge , x ) )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns 0.0 if `x[i] < edge[i]`, otherwise it returns 1.0.
2018-09-23 20:48:45 +08:00
pub fn step_vec < N : Number , D : Dimension > ( edge : & TVec < N , D > , x : & TVec < N , D > ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-10-05 11:15:17 +08:00
edge . zip_map ( x , step_scalar )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns a value equal to the nearest integer to `x` whose absolute value is not larger than the absolute value of `x`.
2018-10-04 21:49:01 +08:00
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let vec = glm::vec3(-1.5, 0.5, 2.8);
/// assert_eq!(glm::vec3(-1.0, 0.0, 2.0), glm::trunc(&vec));
/// ```
///
/// # See also:
///
/// * [`ceil`](fn.ceil.html)
/// * [`floor`](fn.floor.html)
/// * [`fract`](fn.fract.html)
/// * [`round`](fn.round.html)
2019-03-25 18:21:41 +08:00
pub fn trunc < N : RealField , D : Dimension > ( x : & TVec < N , D > ) -> TVec < N , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < N , D > {
2018-09-21 01:54:12 +08:00
x . map ( | x | x . trunc ( ) )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// Returns a floating-point value corresponding to a unsigned integer encoding of a floating-point value.
///
/// If an `inf` or `NaN` is passed in, it will not signal, and the resulting floating point value is unspecified. Otherwise, the bit-level representation is preserved.
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`float_bits_to_int`](fn.float_bits_to_int.html)
/// * [`float_bits_to_int_vec`](fn.float_bits_to_int_vec.html)
/// * [`float_bits_to_uint`](fn.float_bits_to_uint.html)
/// * [`float_bits_to_uint_vec`](fn.float_bits_to_uint_vec.html)
/// * [`int_bits_to_float`](fn.int_bits_to_float.html)
/// * [`int_bits_to_float_vec`](fn.int_bits_to_float_vec.html)
/// * [`uint_bits_to_float`](fn.uint_bits_to_float.html)
2018-09-22 23:36:08 +08:00
pub fn uint_bits_to_float_scalar ( v : u32 ) -> f32 {
2018-10-05 13:28:32 +08:00
f32 ::from_bits ( v )
2018-09-20 16:50:34 +08:00
}
2018-09-22 19:18:59 +08:00
/// For each component of `v`, returns a floating-point value corresponding to a unsigned integer encoding of a floating-point value.
///
/// If an inf or NaN is passed in, it will not signal, and the resulting floating point value is unspecified. Otherwise, the bit-level representation is preserved.
2018-10-04 21:49:01 +08:00
///
/// # See also:
///
/// * [`float_bits_to_int`](fn.float_bits_to_int.html)
/// * [`float_bits_to_int_vec`](fn.float_bits_to_int_vec.html)
/// * [`float_bits_to_uint`](fn.float_bits_to_uint.html)
/// * [`float_bits_to_uint_vec`](fn.float_bits_to_uint_vec.html)
/// * [`int_bits_to_float`](fn.int_bits_to_float.html)
/// * [`int_bits_to_float_vec`](fn.int_bits_to_float_vec.html)
/// * [`uint_bits_to_float_scalar`](fn.uint_bits_to_float_scalar.html)
2018-09-23 20:48:45 +08:00
pub fn uint_bits_to_float < D : Dimension > ( v : & TVec < u32 , D > ) -> TVec < f32 , D >
2018-10-22 13:00:10 +08:00
where DefaultAllocator : Alloc < f32 , D > {
2018-10-05 11:15:17 +08:00
v . map ( uint_bits_to_float_scalar )
2018-10-04 21:49:01 +08:00
}