From 19a00885d69dba87d9745edec1722513909e282a Mon Sep 17 00:00:00 2001 From: Sébastien Crozet Date: Mon, 21 Feb 2022 21:16:38 +0100 Subject: Use a threshold for utils::inv and simd_inv --- src/utils.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 9aa4bf2..07f0626 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -19,16 +19,19 @@ pub trait WReal: SimdRealField + Copy {} impl WReal for Real {} impl WReal for SimdReal {} +const INV_EPSILON: Real = 1.0e-20; + pub(crate) fn inv(val: Real) -> Real { - if val == 0.0 { + if val >= -INV_EPSILON && val <= INV_EPSILON { 0.0 } else { 1.0 / val } } -pub(crate) fn simd_inv(val: N) -> N { - N::zero().select(val.simd_eq(N::zero()), N::one() / val) +pub(crate) fn simd_inv(val: N) -> N { + let eps = N::splat(INV_EPSILON); + N::zero().select(val.simd_gt(-eps) & val.simd_lt(eps), N::one() / val) } /// Trait to copy the sign of each component of one scalar/vector/matrix to another. -- cgit From 15b165893c718a5606622b05b36f8041a429e30c Mon Sep 17 00:00:00 2001 From: Sébastien Crozet Date: Mon, 21 Feb 2022 21:19:30 +0100 Subject: Use WReal instead of SimdRealField everywhere --- src/utils.rs | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 07f0626..c9edf16 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -126,7 +126,7 @@ pub trait WBasis: Sized { fn orthonormal_vector(self) -> Self; } -impl WBasis for Vector2 { +impl WBasis for Vector2 { type Basis = [Vector2; 1]; fn orthonormal_basis(self) -> [Vector2; 1] { [Vector2::new(-self.y, self.x)] @@ -136,7 +136,7 @@ impl WBasis for Vector2 { } } -impl> WBasis for Vector3 { +impl> WBasis for Vector3 { type Basis = [Vector3; 2]; // Robust and branchless implementation from Pixar: // https://graphics.pixar.com/library/OrthonormalB/paper.pdf @@ -254,7 +254,7 @@ pub(crate) trait WCrossMatrix: Sized { fn gcross_matrix_tr(self) -> Self::CrossMatTr; } -impl WCrossMatrix for Vector3 { +impl WCrossMatrix for Vector3 { type CrossMat = Matrix3; type CrossMatTr = Matrix3; @@ -279,7 +279,7 @@ impl WCrossMatrix for Vector3 { } } -impl WCrossMatrix for Vector2 { +impl WCrossMatrix for Vector2 { type CrossMat = RowVector2; type CrossMatTr = Vector2; @@ -356,7 +356,7 @@ pub(crate) trait WDot: Sized { fn gdot(&self, rhs: Rhs) -> Self::Result; } -impl WDot> for Vector3 { +impl WDot> for Vector3 { type Result = N; fn gdot(&self, rhs: Vector3) -> Self::Result { @@ -364,7 +364,7 @@ impl WDot> for Vector3 { } } -impl WDot> for Vector2 { +impl WDot> for Vector2 { type Result = N; fn gdot(&self, rhs: Vector2) -> Self::Result { @@ -372,7 +372,7 @@ impl WDot> for Vector2 { } } -impl WDot> for N { +impl WDot> for N { type Result = N; fn gdot(&self, rhs: Vector1) -> Self::Result { @@ -388,7 +388,7 @@ impl WDot for N { } } -impl WDot for Vector1 { +impl WDot for Vector1 { type Result = N; fn gdot(&self, rhs: N) -> Self::Result { @@ -428,26 +428,20 @@ pub trait WQuat { fn diff_conj1_2(&self, rhs: &Self) -> Self::Result; } -impl WQuat for UnitComplex -where - ::Element: SimdRealField, -{ +impl WQuat for UnitComplex { type Result = Matrix1; fn diff_conj1_2(&self, rhs: &Self) -> Self::Result { - let two: N = na::convert(2.0); + let two: N = N::splat(2.0); Matrix1::new((self.im * rhs.im + self.re * rhs.re) * two) } } -impl WQuat for UnitQuaternion -where - ::Element: SimdRealField, -{ +impl WQuat for UnitQuaternion { type Result = Matrix3; fn diff_conj1_2(&self, rhs: &Self) -> Self::Result { - let half: N = na::convert(0.5); + let half = N::splat(0.5); let v1 = self.imag(); let v2 = rhs.imag(); let w1 = self.w; -- cgit