Merge pull request #455 from derekdreery/is_finite
Add is_finite method to vectors and matrices.
This commit is contained in:
commit
f052bf0c9c
5 changed files with 67 additions and 0 deletions
|
@ -106,6 +106,11 @@ impl<S: BaseFloat> Matrix2<S> {
|
||||||
|
|
||||||
Matrix2::new(c, s, -s, c)
|
Matrix2::new(c, s, -s, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Are all entries in the matrix finite.
|
||||||
|
pub fn is_finite(&self) -> bool {
|
||||||
|
self.x.is_finite() && self.y.is_finite()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: BaseFloat> Matrix3<S> {
|
impl<S: BaseFloat> Matrix3<S> {
|
||||||
|
@ -205,6 +210,11 @@ impl<S: BaseFloat> Matrix3<S> {
|
||||||
_1subc * axis.z * axis.z + c,
|
_1subc * axis.z * axis.z + c,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Are all entries in the matrix finite.
|
||||||
|
pub fn is_finite(&self) -> bool {
|
||||||
|
self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: BaseFloat> Matrix4<S> {
|
impl<S: BaseFloat> Matrix4<S> {
|
||||||
|
@ -357,6 +367,11 @@ impl<S: BaseFloat> Matrix4<S> {
|
||||||
S::zero(), S::zero(), S::zero(), S::one(),
|
S::zero(), S::zero(), S::zero(), S::one(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Are all entries in the matrix finite.
|
||||||
|
pub fn is_finite(&self) -> bool {
|
||||||
|
self.w.is_finite() && self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: BaseFloat> Zero for Matrix2<S> {
|
impl<S: BaseFloat> Zero for Matrix2<S> {
|
||||||
|
|
|
@ -118,6 +118,10 @@ macro_rules! impl_point {
|
||||||
fn product(self) -> S where S: Mul<Output = S> {
|
fn product(self) -> S where S: Mul<Output = S> {
|
||||||
fold_array!(mul, { $(self.$field),+ })
|
fold_array!(mul, { $(self.$field),+ })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_finite(&self) -> bool where S: BaseFloat {
|
||||||
|
$(self.$field.is_finite())&&+
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: NumCast + Copy> $PointN<S> {
|
impl<S: NumCast + Copy> $PointN<S> {
|
||||||
|
|
|
@ -173,6 +173,10 @@ impl<S: BaseFloat> Quaternion<S> {
|
||||||
(self * scale1 + other * scale2) * Rad::sin(theta).recip()
|
(self * scale1 + other * scale2) * Rad::sin(theta).recip()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_finite(&self) -> bool {
|
||||||
|
self.s.is_finite() && self.v.is_finite()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: BaseFloat> Zero for Quaternion<S> {
|
impl<S: BaseFloat> Zero for Quaternion<S> {
|
||||||
|
|
|
@ -87,6 +87,11 @@ where
|
||||||
fn product(self) -> Self::Element
|
fn product(self) -> Self::Element
|
||||||
where
|
where
|
||||||
Self::Element: Mul<Output = <Self as Array>::Element>;
|
Self::Element: Mul<Output = <Self as Array>::Element>;
|
||||||
|
|
||||||
|
/// Whether all elements of the array are finite
|
||||||
|
fn is_finite(&self) -> bool
|
||||||
|
where
|
||||||
|
Self::Element: BaseFloat;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Element-wise arithmetic operations. These are supplied for pragmatic
|
/// Element-wise arithmetic operations. These are supplied for pragmatic
|
||||||
|
|
|
@ -163,6 +163,10 @@ macro_rules! impl_vector {
|
||||||
fn product(self) -> S where S: Mul<Output = S> {
|
fn product(self) -> S where S: Mul<Output = S> {
|
||||||
fold_array!(mul, { $(self.$field),+ })
|
fold_array!(mul, { $(self.$field),+ })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_finite(&self) -> bool where S: BaseFloat {
|
||||||
|
$(self.$field.is_finite())&&+
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: BaseNum> Zero for $VectorN<S> {
|
impl<S: BaseNum> Zero for $VectorN<S> {
|
||||||
|
@ -353,6 +357,13 @@ macro_rules! impl_vector_default {
|
||||||
$VectorN::new($($field),+)
|
$VectorN::new($($field),+)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<S: BaseFloat> $VectorN<S> {
|
||||||
|
/// True if all entries in the vector are finite
|
||||||
|
pub fn is_finite(&self) -> bool {
|
||||||
|
$(self.$field.is_finite())&&+
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<S: NumCast + Copy> $VectorN<S> {
|
impl<S: NumCast + Copy> $VectorN<S> {
|
||||||
/// Component-wise casting to another type.
|
/// Component-wise casting to another type.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -398,6 +409,10 @@ macro_rules! impl_vector_default {
|
||||||
fn product(self) -> S where S: Mul<Output = S> {
|
fn product(self) -> S where S: Mul<Output = S> {
|
||||||
fold_array!(mul, { $(self.$field),+ })
|
fold_array!(mul, { $(self.$field),+ })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_finite(&self) -> bool where S: BaseFloat {
|
||||||
|
$(self.$field.is_finite())&&+
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: BaseNum> Zero for $VectorN<S> {
|
impl<S: BaseNum> Zero for $VectorN<S> {
|
||||||
|
@ -1323,6 +1338,14 @@ mod tests {
|
||||||
assert_eq!(v, &VECTOR2);
|
assert_eq!(v, &VECTOR2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_is_finite() {
|
||||||
|
use num_traits::Float;
|
||||||
|
assert!(!Vector2::from([Float::nan(), 1.0]).is_finite());
|
||||||
|
assert!(!Vector2::from([1.0, Float::infinity()]).is_finite());
|
||||||
|
assert!(Vector2::from([-1.0, 1.0]).is_finite());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod vector3 {
|
mod vector3 {
|
||||||
|
@ -1428,6 +1451,14 @@ mod tests {
|
||||||
assert_eq!(v, &VECTOR3);
|
assert_eq!(v, &VECTOR3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_is_finite() {
|
||||||
|
use num_traits::Float;
|
||||||
|
assert!(!Vector3::from([Float::nan(), 1.0, 1.0]).is_finite());
|
||||||
|
assert!(!Vector3::from([1.0, 1.0, Float::infinity()]).is_finite());
|
||||||
|
assert!(Vector3::from([-1.0, 1.0, 1.0]).is_finite());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod vector4 {
|
mod vector4 {
|
||||||
|
@ -1539,5 +1570,13 @@ mod tests {
|
||||||
assert_eq!(v, &VECTOR4);
|
assert_eq!(v, &VECTOR4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_is_finite() {
|
||||||
|
use num_traits::Float;
|
||||||
|
assert!(!Vector4::from([0.0, Float::nan(), 1.0, 1.0]).is_finite());
|
||||||
|
assert!(!Vector4::from([1.0, 1.0, Float::neg_infinity(), 0.0]).is_finite());
|
||||||
|
assert!(Vector4::from([-1.0, 0.0, 1.0, 1.0]).is_finite());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue