nalgebra/nalgebra-glm/src/vector_relational.rs

213 lines
5.5 KiB
Rust
Raw Normal View History

2019-03-23 21:29:07 +08:00
use crate::aliases::TVec;
2021-04-11 17:00:38 +08:00
use crate::traits::Number;
/// Checks that all the vector components are `true`.
2018-10-03 19:28:07 +08:00
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let vec = glm::vec2(true, false);
/// assert_eq!(false, glm::all(&vec));
///
/// let vec = glm::vec2(true, true);
/// assert_eq!(true, glm::all(&vec));
/// ```
///
2018-10-03 19:28:07 +08:00
/// # See also:
///
/// * [`any`](fn.any.html)
/// * [`not`](fn.not.html)
2021-04-11 17:00:38 +08:00
pub fn all<const D: usize>(v: &TVec<bool, D>) -> bool {
v.iter().all(|x| *x)
}
/// Checks that at least one of the vector components is `true`.
2018-10-03 19:28:07 +08:00
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let vec = glm::vec2(true, false);
/// assert_eq!(true, glm::any(&vec));
///
/// let vec = glm::vec2(true, true);
/// assert_eq!(true, glm::any(&vec));
///
/// let vec = glm::vec2(false, false);
/// assert_eq!(false, glm::any(&vec));
/// ```
///
2018-10-03 19:28:07 +08:00
/// # See also:
///
/// * [`all`](fn.all.html)
/// * [`not`](fn.not.html)
2021-04-11 17:00:38 +08:00
pub fn any<const D: usize>(v: &TVec<bool, D>) -> bool {
v.iter().any(|x| *x)
}
2018-09-22 19:21:02 +08:00
/// Component-wise equality comparison.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// assert_eq!(glm::vec2(true, false),
/// glm::equal(&glm::vec2(1.0, 3.0),
/// &glm::vec2(1.0, 2.0)));
/// ```
///
2018-10-07 21:47:59 +08:00
/// # See also:
///
/// * [`greater_than`](fn.greater_than.html)
/// * [`greater_than_equal`](fn.greater_than_equal.html)
/// * [`less_than`](fn.less_than.html)
/// * [`less_than_equal`](fn.less_than_equal.html)
/// * [`not`](fn.not.html)
/// * [`not_equal`](fn.not_equal.html)
2021-04-11 17:00:38 +08:00
pub fn equal<T: Number, const D: usize>(x: &TVec<T, D>, y: &TVec<T, D>) -> TVec<bool, D> {
x.zip_map(y, |x, y| x == y)
}
2018-09-22 19:21:02 +08:00
/// Component-wise `>` comparison.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// assert_eq!(glm::vec2(false, true),
/// glm::greater_than(&glm::vec2(1.0, 3.0),
/// &glm::vec2(1.0, 2.0)));
/// ```
///
2018-10-07 21:47:59 +08:00
/// # See also:
///
/// * [`equal`](fn.equal.html)
/// * [`greater_than_equal`](fn.greater_than_equal.html)
/// * [`less_than`](fn.less_than.html)
/// * [`less_than_equal`](fn.less_than_equal.html)
/// * [`not`](fn.not.html)
/// * [`not_equal`](fn.not_equal.html)
2021-04-11 17:00:38 +08:00
pub fn greater_than<T: Number, const D: usize>(x: &TVec<T, D>, y: &TVec<T, D>) -> TVec<bool, D> {
x.zip_map(y, |x, y| x > y)
}
2018-09-22 19:21:02 +08:00
/// Component-wise `>=` comparison.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// assert_eq!(glm::vec3(true, true, false),
/// glm::greater_than_equal(&glm::vec3(1.0, 3.0, 4.0),
/// &glm::vec3(1.0, 2.0, 5.0)));
/// ```
///
2018-10-07 21:47:59 +08:00
/// # See also:
///
/// * [`equal`](fn.equal.html)
/// * [`greater_than`](fn.greater_than.html)
/// * [`less_than`](fn.less_than.html)
/// * [`less_than_equal`](fn.less_than_equal.html)
/// * [`not`](fn.not.html)
/// * [`not_equal`](fn.not_equal.html)
2021-04-11 17:00:38 +08:00
pub fn greater_than_equal<T: Number, const D: usize>(
x: &TVec<T, D>,
y: &TVec<T, D>,
) -> TVec<bool, D> {
x.zip_map(y, |x, y| x >= y)
}
2018-09-22 19:21:02 +08:00
/// Component-wise `<` comparison.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// assert_eq!(glm::vec3(false, false, true),
/// glm::less_than(&glm::vec3(1.0, 3.0, 4.0),
/// &glm::vec3(1.0, 2.0, 5.0)));
/// ```
///
2018-10-07 21:47:59 +08:00
/// # See also:
///
/// * [`equal`](fn.equal.html)
/// * [`greater_than`](fn.greater_than.html)
/// * [`greater_than_equal`](fn.greater_than_equal.html)
/// * [`less_than_equal`](fn.less_than_equal.html)
/// * [`not`](fn.not.html)
/// * [`not_equal`](fn.not_equal.html)
2021-04-11 17:00:38 +08:00
pub fn less_than<T: Number, const D: usize>(x: &TVec<T, D>, y: &TVec<T, D>) -> TVec<bool, D> {
x.zip_map(y, |x, y| x < y)
}
2018-09-22 19:21:02 +08:00
/// Component-wise `>=` comparison.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// assert_eq!(glm::vec3(true, false, true),
/// glm::less_than_equal(&glm::vec3(1.0, 3.0, 4.0),
/// &glm::vec3(1.0, 2.0, 5.0)));
/// ```
///
2018-10-07 21:47:59 +08:00
/// # See also:
///
/// * [`equal`](fn.equal.html)
/// * [`greater_than`](fn.greater_than.html)
/// * [`greater_than_equal`](fn.greater_than_equal.html)
/// * [`less_than`](fn.less_than.html)
/// * [`not`](fn.not.html)
/// * [`not_equal`](fn.not_equal.html)
2021-04-11 17:00:38 +08:00
pub fn less_than_equal<T: Number, const D: usize>(x: &TVec<T, D>, y: &TVec<T, D>) -> TVec<bool, D> {
x.zip_map(y, |x, y| x <= y)
}
2018-09-22 19:21:02 +08:00
/// Component-wise not `!`.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// let vec = glm::vec2(true, false);
/// assert_eq!(glm::vec2(false, true), glm::not(&vec));
/// ```
///
2018-10-07 21:47:59 +08:00
/// # See also:
///
/// * [`all`](fn.all.html)
/// * [`any`](fn.any.html)
/// * [`equal`](fn.equal.html)
/// * [`greater_than`](fn.greater_than.html)
/// * [`greater_than_equal`](fn.greater_than_equal.html)
/// * [`less_than`](fn.less_than.html)
/// * [`less_than_equal`](fn.less_than_equal.html)
/// * [`not_equal`](fn.not_equal.html)
2021-04-11 17:00:38 +08:00
pub fn not<const D: usize>(v: &TVec<bool, D>) -> TVec<bool, D> {
v.map(|x| !x)
}
2018-09-22 19:21:02 +08:00
/// Component-wise not-equality `!=`.
///
/// # Examples:
///
/// ```
/// # use nalgebra_glm as glm;
/// assert_eq!(glm::vec2(false, true),
/// glm::not_equal(&glm::vec2(1.0, 3.0),
/// &glm::vec2(1.0, 2.0)));
/// ```
///
2018-10-07 21:47:59 +08:00
/// # See also:
///
/// * [`equal`](fn.equal.html)
/// * [`greater_than`](fn.greater_than.html)
/// * [`greater_than_equal`](fn.greater_than_equal.html)
/// * [`less_than`](fn.less_than.html)
/// * [`less_than_equal`](fn.less_than_equal.html)
/// * [`not`](fn.not.html)
2021-04-11 17:00:38 +08:00
pub fn not_equal<T: Number, const D: usize>(x: &TVec<T, D>, y: &TVec<T, D>) -> TVec<bool, D> {
x.zip_map(y, |x, y| x != y)
}