Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix edge case in Vector::scalar_projection_onto #810

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions crates/fj-math/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl<const D: usize> Vector<D> {

/// 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())
}
}
Expand Down Expand Up @@ -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
);
}
}