parry3d_f64/utils/spade.rs
1use crate::math::Real;
2
3/// Ensures the given coordinate doesn’t go out of the bounds of spade’s acceptable values.
4///
5/// Returns 0.0 if the coordinate is smaller than `spade::MIN_ALLOWED_VALUE`.
6/// Returns `spade::MAX_ALLOWED_VALUE` the coordinate is larger than `spade::MAX_ALLOWED_VALUE`.
7pub fn sanitize_spade_coord(coord: Real) -> Real {
8 let abs = coord.abs();
9
10 #[allow(clippy::unnecessary_cast)]
11 if abs as f64 <= spade::MIN_ALLOWED_VALUE {
12 return 0.0;
13 }
14
15 #[cfg(feature = "f64")]
16 if abs > spade::MAX_ALLOWED_VALUE {
17 // This cannot happen in f32 since the max is 3.40282347E+38.
18 return spade::MAX_ALLOWED_VALUE * coord.signum();
19 }
20
21 coord
22}
23
24/// Ensures the coordinates of the given point don’t go out of the bounds of spade’s acceptable values.
25pub fn sanitize_spade_point(point: spade::Point2<Real>) -> spade::Point2<Real> {
26 spade::Point2::new(sanitize_spade_coord(point.x), sanitize_spade_coord(point.y))
27}