Allow specified tolerance for NearlyEq checks

This commit is contained in:
Danny McGee
2023-10-18 00:59:37 -04:00
parent b674ee3c3a
commit cfb15613b1
2 changed files with 24 additions and 8 deletions
+1 -1
View File
@@ -161,7 +161,7 @@ impl From<Vec3> for SphericalCoords {
}
let pitch = f32::asin(-y / radius);
let heading = if (pitch - 90_f32.to_radians()).abs() < 1e-3_f32 {
let heading = if pitch.nearly_eq_within(90_f32.to_radians(), 1e-3) {
0.
} else {
f32::atan2(x, z)
+23 -7
View File
@@ -1,17 +1,33 @@
use bevy::prelude::Vec3;
pub trait NearlyEq<Rhs> {
fn nearly_eq(self, rhs: Rhs) -> bool;
pub trait NearlyEq<Rhs, Tolerance = Rhs> {
const DEFAULT_TOLERANCE: Tolerance;
#[inline]
fn nearly_eq(self, rhs: Rhs) -> bool
where
Self: Sized,
{
self.nearly_eq_within(rhs, Self::DEFAULT_TOLERANCE)
}
fn nearly_eq_within(self, rhs: Rhs, tolerance: Tolerance) -> bool;
}
impl NearlyEq<f32> for f32 {
fn nearly_eq(self, rhs: f32) -> bool {
(self - rhs).abs() < Self::EPSILON
const DEFAULT_TOLERANCE: f32 = f32::EPSILON;
fn nearly_eq_within(self, rhs: f32, tolerance: f32) -> bool {
(self - rhs).abs() < tolerance
}
}
impl NearlyEq<Vec3> for Vec3 {
fn nearly_eq(self, rhs: Vec3) -> bool {
self.x.nearly_eq(rhs.x) && self.y.nearly_eq(rhs.y) && self.z.nearly_eq(rhs.z)
impl NearlyEq<Vec3, f32> for Vec3 {
const DEFAULT_TOLERANCE: f32 = <f32 as NearlyEq<f32>>::DEFAULT_TOLERANCE;
fn nearly_eq_within(self, rhs: Vec3, tolerance: f32) -> bool {
self.x.nearly_eq_within(rhs.x, tolerance)
&& self.y.nearly_eq_within(rhs.y, tolerance)
&& self.z.nearly_eq_within(rhs.z, tolerance)
}
}