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') 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/dynamics/solver/delta_vel.rs | 10 ++++---- .../joint_constraint/joint_velocity_constraint.rs | 11 ++++---- .../joint_velocity_constraint_builder.rs | 3 +-- src/dynamics/solver/velocity_constraint.rs | 9 +++---- src/dynamics/solver/velocity_constraint_element.rs | 17 +++++------- .../solver/velocity_ground_constraint_element.rs | 17 +++++------- src/utils.rs | 30 +++++++++------------- 7 files changed, 41 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/dynamics/solver/delta_vel.rs b/src/dynamics/solver/delta_vel.rs index 73f3c91..cfdb791 100644 --- a/src/dynamics/solver/delta_vel.rs +++ b/src/dynamics/solver/delta_vel.rs @@ -1,6 +1,6 @@ use crate::math::{AngVector, Vector, SPATIAL_DIM}; -use na::{DVectorSlice, DVectorSliceMut}; -use na::{Scalar, SimdRealField}; +use crate::utils::WReal; +use na::{DVectorSlice, DVectorSliceMut, Scalar}; use std::ops::{AddAssign, Sub}; #[derive(Copy, Clone, Debug, Default)] @@ -29,7 +29,7 @@ impl DeltaVel { } } -impl DeltaVel { +impl DeltaVel { pub fn zero() -> Self { Self { linear: na::zero(), @@ -38,14 +38,14 @@ impl DeltaVel { } } -impl AddAssign for DeltaVel { +impl AddAssign for DeltaVel { fn add_assign(&mut self, rhs: Self) { self.linear += rhs.linear; self.angular += rhs.angular; } } -impl Sub for DeltaVel { +impl Sub for DeltaVel { type Output = Self; fn sub(self, rhs: Self) -> Self { diff --git a/src/dynamics/solver/joint_constraint/joint_velocity_constraint.rs b/src/dynamics/solver/joint_constraint/joint_velocity_constraint.rs index a4ec313..d264313 100644 --- a/src/dynamics/solver/joint_constraint/joint_velocity_constraint.rs +++ b/src/dynamics/solver/joint_constraint/joint_velocity_constraint.rs @@ -3,7 +3,6 @@ use crate::dynamics::solver::DeltaVel; use crate::dynamics::{IntegrationParameters, JointData, JointGraphEdge, JointIndex}; use crate::math::{AngVector, AngularInertia, Isometry, Point, Real, Vector, DIM, SPATIAL_DIM}; use crate::utils::{WDot, WReal}; -use simba::simd::SimdRealField; #[cfg(feature = "simd-is-enabled")] use { @@ -12,7 +11,7 @@ use { }; #[derive(Copy, Clone, PartialEq, Debug)] -pub struct MotorParameters { +pub struct MotorParameters { pub stiffness: N, pub damping: N, pub gamma: N, @@ -22,7 +21,7 @@ pub struct MotorParameters { pub max_impulse: N, } -impl Default for MotorParameters { +impl Default for MotorParameters { fn default() -> Self { Self { stiffness: N::zero(), @@ -48,7 +47,7 @@ pub enum WritebackId { // the solver, to avoid fetching data from the rigid-body set // every time. #[derive(Copy, Clone)] -pub struct SolverBody { +pub struct SolverBody { pub linvel: Vector, pub angvel: AngVector, pub im: Vector, @@ -58,7 +57,7 @@ pub struct SolverBody { } #[derive(Debug, Copy, Clone)] -pub struct JointVelocityConstraint { +pub struct JointVelocityConstraint { pub mj_lambda1: [usize; LANES], pub mj_lambda2: [usize; LANES], @@ -339,7 +338,7 @@ impl JointVelocityConstraint { } #[derive(Debug, Copy, Clone)] -pub struct JointVelocityGroundConstraint { +pub struct JointVelocityGroundConstraint { pub mj_lambda2: [usize; LANES], pub joint_id: [JointIndex; LANES], diff --git a/src/dynamics/solver/joint_constraint/joint_velocity_constraint_builder.rs b/src/dynamics/solver/joint_constraint/joint_velocity_constraint_builder.rs index 55a112a..3aa00f2 100644 --- a/src/dynamics/solver/joint_constraint/joint_velocity_constraint_builder.rs +++ b/src/dynamics/solver/joint_constraint/joint_velocity_constraint_builder.rs @@ -7,10 +7,9 @@ use crate::dynamics::{IntegrationParameters, JointIndex}; use crate::math::{Isometry, Matrix, Point, Real, Rotation, Vector, ANG_DIM, DIM}; use crate::utils::{IndexMut2, WCrossMatrix, WDot, WQuat, WReal}; use na::SMatrix; -use simba::simd::SimdRealField; #[derive(Debug, Copy, Clone)] -pub struct JointVelocityConstraintBuilder { +pub struct JointVelocityConstraintBuilder { pub basis: Matrix, pub cmat1_basis: SMatrix, pub cmat2_basis: SMatrix, diff --git a/src/dynamics/solver/velocity_constraint.rs b/src/dynamics/solver/velocity_constraint.rs index bb00b66..5711518 100644 --- a/src/dynamics/solver/velocity_constraint.rs +++ b/src/dynamics/solver/velocity_constraint.rs @@ -5,7 +5,7 @@ use crate::dynamics::solver::{WVelocityConstraint, WVelocityGroundConstraint}; use crate::dynamics::{IntegrationParameters, RigidBodyIds, RigidBodyMassProps, RigidBodyVelocity}; use crate::geometry::{ContactManifold, ContactManifoldIndex}; use crate::math::{Real, Vector, DIM, MAX_MANIFOLD_POINTS}; -use crate::utils::{WAngularInertia, WBasis, WCross, WDot}; +use crate::utils::{WAngularInertia, WBasis, WCross, WDot, WReal}; use super::{DeltaVel, VelocityConstraintElement, VelocityConstraintNormalPart}; @@ -373,8 +373,7 @@ pub(crate) fn compute_tangent_contact_directions( linvel2: &Vector, ) -> [Vector; DIM - 1] where - N: na::SimdRealField + Copy, - N::Element: na::RealField + Copy, + N: WReal, Vector: WBasis, { use na::SimdValue; @@ -392,8 +391,8 @@ where tangent_relative_linvel.normalize_mut() }; - let threshold: N::Element = na::convert(1.0e-4); - let use_fallback = tangent_linvel_norm.simd_lt(N::splat(threshold)); + const THRESHOLD: Real = 1.0e-4; + let use_fallback = tangent_linvel_norm.simd_lt(N::splat(THRESHOLD)); let tangent_fallback = force_dir1.orthonormal_vector(); let tangent1 = tangent_fallback.select(use_fallback, tangent_relative_linvel); diff --git a/src/dynamics/solver/velocity_constraint_element.rs b/src/dynamics/solver/velocity_constraint_element.rs index 2d2221d..30afef5 100644 --- a/src/dynamics/solver/velocity_constraint_element.rs +++ b/src/dynamics/solver/velocity_constraint_element.rs @@ -1,10 +1,9 @@ use super::DeltaVel; use crate::math::{AngVector, Vector, DIM}; -use crate::utils::{WBasis, WDot}; -use na::SimdRealField; +use crate::utils::{WBasis, WDot, WReal}; #[derive(Copy, Clone, Debug)] -pub(crate) struct VelocityConstraintTangentPart { +pub(crate) struct VelocityConstraintTangentPart { pub gcross1: [AngVector; DIM - 1], pub gcross2: [AngVector; DIM - 1], pub rhs: [N; DIM - 1], @@ -18,7 +17,7 @@ pub(crate) struct VelocityConstraintTangentPart { pub r: [N; DIM], } -impl VelocityConstraintTangentPart { +impl VelocityConstraintTangentPart { fn zero() -> Self { Self { gcross1: [na::zero(); DIM - 1], @@ -43,7 +42,6 @@ impl VelocityConstraintTangentPart { mj_lambda2: &mut DeltaVel, ) where AngVector: WDot, Result = N>, - N::Element: SimdRealField + Copy, { #[cfg(feature = "dim2")] { @@ -107,7 +105,7 @@ impl VelocityConstraintTangentPart { } #[derive(Copy, Clone, Debug)] -pub(crate) struct VelocityConstraintNormalPart { +pub(crate) struct VelocityConstraintNormalPart { pub gcross1: AngVector, pub gcross2: AngVector, pub rhs: N, @@ -116,7 +114,7 @@ pub(crate) struct VelocityConstraintNormalPart { pub r: N, } -impl VelocityConstraintNormalPart { +impl VelocityConstraintNormalPart { fn zero() -> Self { Self { gcross1: na::zero(), @@ -157,12 +155,12 @@ impl VelocityConstraintNormalPart { } #[derive(Copy, Clone, Debug)] -pub(crate) struct VelocityConstraintElement { +pub(crate) struct VelocityConstraintElement { pub normal_part: VelocityConstraintNormalPart, pub tangent_part: VelocityConstraintTangentPart, } -impl VelocityConstraintElement { +impl VelocityConstraintElement { pub fn zero() -> Self { Self { normal_part: VelocityConstraintNormalPart::zero(), @@ -186,7 +184,6 @@ impl VelocityConstraintElement { ) where Vector: WBasis, AngVector: WDot, Result = N>, - N::Element: SimdRealField + Copy, { // Solve penetration. if solve_normal { diff --git a/src/dynamics/solver/velocity_ground_constraint_element.rs b/src/dynamics/solver/velocity_ground_constraint_element.rs index 8057030..06a727a 100644 --- a/src/dynamics/solver/velocity_ground_constraint_element.rs +++ b/src/dynamics/solver/velocity_ground_constraint_element.rs @@ -1,10 +1,9 @@ use super::DeltaVel; use crate::math::{AngVector, Vector, DIM}; -use crate::utils::{WBasis, WDot}; -use na::SimdRealField; +use crate::utils::{WBasis, WDot, WReal}; #[derive(Copy, Clone, Debug)] -pub(crate) struct VelocityGroundConstraintTangentPart { +pub(crate) struct VelocityGroundConstraintTangentPart { pub gcross2: [AngVector; DIM - 1], pub rhs: [N; DIM - 1], #[cfg(feature = "dim2")] @@ -17,7 +16,7 @@ pub(crate) struct VelocityGroundConstraintTangentPart { pub r: [N; DIM], } -impl VelocityGroundConstraintTangentPart { +impl VelocityGroundConstraintTangentPart { fn zero() -> Self { Self { gcross2: [na::zero(); DIM - 1], @@ -39,7 +38,6 @@ impl VelocityGroundConstraintTangentPart { mj_lambda2: &mut DeltaVel, ) where AngVector: WDot, Result = N>, - N::Element: SimdRealField + Copy, { #[cfg(feature = "dim2")] { @@ -89,7 +87,7 @@ impl VelocityGroundConstraintTangentPart { } #[derive(Copy, Clone, Debug)] -pub(crate) struct VelocityGroundConstraintNormalPart { +pub(crate) struct VelocityGroundConstraintNormalPart { pub gcross2: AngVector, pub rhs: N, pub rhs_wo_bias: N, @@ -97,7 +95,7 @@ pub(crate) struct VelocityGroundConstraintNormalPart { pub r: N, } -impl VelocityGroundConstraintNormalPart { +impl VelocityGroundConstraintNormalPart { fn zero() -> Self { Self { gcross2: na::zero(), @@ -129,12 +127,12 @@ impl VelocityGroundConstraintNormalPart { } #[derive(Copy, Clone, Debug)] -pub(crate) struct VelocityGroundConstraintElement { +pub(crate) struct VelocityGroundConstraintElement { pub normal_part: VelocityGroundConstraintNormalPart, pub tangent_part: VelocityGroundConstraintTangentPart, } -impl VelocityGroundConstraintElement { +impl VelocityGroundConstraintElement { pub fn zero() -> Self { Self { normal_part: VelocityGroundConstraintNormalPart::zero(), @@ -156,7 +154,6 @@ impl VelocityGroundConstraintElement { ) where Vector: WBasis, AngVector: WDot, Result = N>, - N::Element: SimdRealField + Copy, { // Solve penetration. if solve_normal { 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