From 711999b9ef2c98334aa92cb32c704c33be6b6b97 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 13 Jul 2022 14:46:44 +0200 Subject: [PATCH] Fix edge case in `Vector::scalar_projection_onto` --- crates/fj-math/src/vector.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/fj-math/src/vector.rs b/crates/fj-math/src/vector.rs index c9a1282e5..2b99f4055 100644 --- a/crates/fj-math/src/vector.rs +++ b/crates/fj-math/src/vector.rs @@ -102,6 +102,10 @@ impl Vector { /// Compute the scalar project of this vector onto another pub fn scalar_projection_onto(&self, other: &Self) -> Scalar { + if other.magnitude() == Scalar::ZERO { + return Scalar::ZERO; + } + self.dot(&other.normalize()) } } @@ -366,5 +370,12 @@ mod tests { assert_eq!(v.scalar_projection_onto(&x), Scalar::from(1.)); assert_eq!(v.scalar_projection_onto(&y), Scalar::from(2.)); assert_eq!(v.scalar_projection_onto(&z), Scalar::from(3.)); + + // Zero-length vectors should be handled as well. + assert_eq!( + Vector::unit_x() + .scalar_projection_onto(&Vector::from([0., 0., 0.])), + Scalar::ZERO + ); } }