From cc6d1b973002b4d366bc81ec6bf9e8240ad7b404 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Mon, 14 Dec 2020 15:51:43 +0100 Subject: Outsource the Shape trait, wquadtree, and shape types. --- src/utils.rs | 632 +++++------------------------------------------------------ 1 file changed, 53 insertions(+), 579 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 6557b74..4089631 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -7,7 +7,7 @@ use simba::simd::SimdValue; use std::ops::{Add, Mul}; use { - crate::simd::{SimdBool, SimdFloat}, + crate::math::{AngularInertia, SimdBool, SimdReal}, na::SimdPartialOrd, num::One, }; @@ -32,16 +32,6 @@ pub(crate) fn inv(val: f32) -> f32 { } } -/// Conditionally swaps each lanes of `a` with those of `b`. -/// -/// For each `i in [0..SIMD_WIDTH[`, if `do_swap.extract(i)` is `true` then -/// `a.extract(i)` is swapped with `b.extract(i)`. -pub fn simd_swap(do_swap: SimdBool, a: &mut SimdFloat, b: &mut SimdFloat) { - let _a = *a; - *a = b.select(do_swap, *a); - *b = _a.select(do_swap, *b); -} - /// Trait to copy the sign of each component of one scalar/vector/matrix to another. pub trait WSign: Sized { // See SIMD implementations of copy_sign there: https://stackoverflow.com/a/57872652 @@ -88,8 +78,8 @@ impl> WSign> for Vector3 { } } -impl WSign for SimdFloat { - fn copy_sign_to(self, to: SimdFloat) -> SimdFloat { +impl WSign for SimdReal { + fn copy_sign_to(self, to: SimdReal) -> SimdReal { to.simd_copysign(self) } } @@ -112,7 +102,7 @@ impl WComponent for f32 { } } -impl WComponent for SimdFloat { +impl WComponent for SimdReal { type Element = f32; fn min_component(self) -> Self::Element { @@ -328,22 +318,22 @@ impl WDot for f32 { } } -impl WCrossMatrix for Vector3 { - type CrossMat = Matrix3; +impl WCrossMatrix for Vector3 { + type CrossMat = Matrix3; #[inline] #[rustfmt::skip] fn gcross_matrix(self) -> Self::CrossMat { Matrix3::new( - SimdFloat::zero(), -self.z, self.y, - self.z, SimdFloat::zero(), -self.x, - -self.y, self.x, SimdFloat::zero(), + SimdReal::zero(), -self.z, self.y, + self.z, SimdReal::zero(), -self.x, + -self.y, self.x, SimdReal::zero(), ) } } -impl WCrossMatrix for Vector2 { - type CrossMat = Vector2; +impl WCrossMatrix for Vector2 { + type CrossMat = Vector2; #[inline] fn gcross_matrix(self) -> Self::CrossMat { @@ -351,24 +341,24 @@ impl WCrossMatrix for Vector2 { } } -impl WCross> for Vector3 { - type Result = Vector3; +impl WCross> for Vector3 { + type Result = Vector3; fn gcross(&self, rhs: Self) -> Self::Result { self.cross(&rhs) } } -impl WCross> for SimdFloat { - type Result = Vector2; +impl WCross> for SimdReal { + type Result = Vector2; - fn gcross(&self, rhs: Vector2) -> Self::Result { + fn gcross(&self, rhs: Vector2) -> Self::Result { Vector2::new(-rhs.y * *self, rhs.x * *self) } } -impl WCross> for Vector2 { - type Result = SimdFloat; +impl WCross> for Vector2 { + type Result = SimdReal; fn gcross(&self, rhs: Self) -> Self::Result { let yx = Vector2::new(rhs.y, rhs.x); @@ -377,26 +367,26 @@ impl WCross> for Vector2 { } } -impl WDot> for Vector3 { - type Result = SimdFloat; +impl WDot> for Vector3 { + type Result = SimdReal; - fn gdot(&self, rhs: Vector3) -> Self::Result { + fn gdot(&self, rhs: Vector3) -> Self::Result { self.x * rhs.x + self.y * rhs.y + self.z * rhs.z } } -impl WDot> for Vector2 { - type Result = SimdFloat; +impl WDot> for Vector2 { + type Result = SimdReal; - fn gdot(&self, rhs: Vector2) -> Self::Result { + fn gdot(&self, rhs: Vector2) -> Self::Result { self.x * rhs.x + self.y * rhs.y } } -impl WDot for SimdFloat { - type Result = SimdFloat; +impl WDot for SimdReal { + type Result = SimdReal; - fn gdot(&self, rhs: SimdFloat) -> Self::Result { + fn gdot(&self, rhs: SimdReal) -> Self::Result { *self * rhs } } @@ -446,26 +436,26 @@ impl WAngularInertia for f32 { } } -impl WAngularInertia for SimdFloat { - type AngVector = SimdFloat; - type LinVector = Vector2; - type AngMatrix = SimdFloat; +impl WAngularInertia for SimdReal { + type AngVector = SimdReal; + type LinVector = Vector2; + type AngMatrix = SimdReal; fn inverse(&self) -> Self { - let zero = ::zero(); + let zero = ::zero(); let is_zero = self.simd_eq(zero); - (::one() / *self).select(is_zero, zero) + (::one() / *self).select(is_zero, zero) } - fn transform_lin_vector(&self, pt: Vector2) -> Vector2 { + fn transform_lin_vector(&self, pt: Vector2) -> Vector2 { pt * *self } - fn transform_vector(&self, pt: SimdFloat) -> SimdFloat { + fn transform_vector(&self, pt: SimdReal) -> SimdReal { *self * pt } - fn squared(&self) -> SimdFloat { + fn squared(&self) -> SimdReal { *self * *self } @@ -478,325 +468,8 @@ impl WAngularInertia for SimdFloat { } } -/// A 2x2 symmetric-definite-positive matrix. -#[derive(Copy, Clone, Debug, PartialEq)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] -pub struct SdpMatrix2 { - /// The component at the first row and first column of this matrix. - pub m11: N, - /// The component at the first row and second column of this matrix. - pub m12: N, - /// The component at the second row and second column of this matrix. - pub m22: N, -} - -impl SdpMatrix2 { - /// A new SDP 2x2 matrix with the given components. - /// - /// Because the matrix is symmetric, only the lower off-diagonal component is required. - pub fn new(m11: N, m12: N, m22: N) -> Self { - Self { m11, m12, m22 } - } - - /// Build an `SdpMatrix2` structure from a plain matrix, assuming it is SDP. - /// - /// No check is performed to ensure `mat` is actually SDP. - pub fn from_sdp_matrix(mat: na::Matrix2) -> Self { - Self { - m11: mat.m11, - m12: mat.m12, - m22: mat.m22, - } - } - - /// Create a new SDP matrix filled with zeros. - pub fn zero() -> Self { - Self { - m11: N::zero(), - m12: N::zero(), - m22: N::zero(), - } - } - - /// Create a new SDP matrix with its diagonal filled with `val`, and its off-diagonal elements set to zero. - pub fn diagonal(val: N) -> Self { - Self { - m11: val, - m12: N::zero(), - m22: val, - } - } - - /// Adds `val` to the diagonal components of `self`. - pub fn add_diagonal(&mut self, elt: N) -> Self { - Self { - m11: self.m11 + elt, - m12: self.m12, - m22: self.m22 + elt, - } - } - - /// Compute the inverse of this SDP matrix without performing any inversibility check. - pub fn inverse_unchecked(&self) -> Self { - let determinant = self.m11 * self.m22 - self.m12 * self.m12; - let m11 = self.m22 / determinant; - let m12 = -self.m12 / determinant; - let m22 = self.m11 / determinant; - - Self { m11, m12, m22 } - } - - /// Convert this SDP matrix to a regular matrix representation. - pub fn into_matrix(self) -> Matrix2 { - Matrix2::new(self.m11, self.m12, self.m12, self.m22) - } -} - -impl Add> for SdpMatrix2 { - type Output = Self; - - fn add(self, rhs: SdpMatrix2) -> Self { - Self::new(self.m11 + rhs.m11, self.m12 + rhs.m12, self.m22 + rhs.m22) - } -} - -impl Mul> for SdpMatrix2 { - type Output = Vector2; - - fn mul(self, rhs: Vector2) -> Self::Output { - Vector2::new( - self.m11 * rhs.x + self.m12 * rhs.y, - self.m12 * rhs.x + self.m22 * rhs.y, - ) - } -} - -/// A 3x3 symmetric-definite-positive matrix. -#[derive(Copy, Clone, Debug, PartialEq)] -#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] -pub struct SdpMatrix3 { - /// The component at the first row and first column of this matrix. - pub m11: N, - /// The component at the first row and second column of this matrix. - pub m12: N, - /// The component at the first row and third column of this matrix. - pub m13: N, - /// The component at the second row and second column of this matrix. - pub m22: N, - /// The component at the second row and third column of this matrix. - pub m23: N, - /// The component at the third row and third column of this matrix. - pub m33: N, -} - -impl SdpMatrix3 { - /// A new SDP 3x3 matrix with the given components. - /// - /// Because the matrix is symmetric, only the lower off-diagonal components is required. - pub fn new(m11: N, m12: N, m13: N, m22: N, m23: N, m33: N) -> Self { - Self { - m11, - m12, - m13, - m22, - m23, - m33, - } - } - - /// Build an `SdpMatrix3` structure from a plain matrix, assuming it is SDP. - /// - /// No check is performed to ensure `mat` is actually SDP. - pub fn from_sdp_matrix(mat: na::Matrix3) -> Self { - Self { - m11: mat.m11, - m12: mat.m12, - m13: mat.m13, - m22: mat.m22, - m23: mat.m23, - m33: mat.m33, - } - } - - /// Create a new SDP matrix filled with zeros. - pub fn zero() -> Self { - Self { - m11: N::zero(), - m12: N::zero(), - m13: N::zero(), - m22: N::zero(), - m23: N::zero(), - m33: N::zero(), - } - } - - /// Create a new SDP matrix with its diagonal filled with `val`, and its off-diagonal elements set to zero. - pub fn diagonal(val: N) -> Self { - Self { - m11: val, - m12: N::zero(), - m13: N::zero(), - m22: val, - m23: N::zero(), - m33: val, - } - } - - /// Are all components of this matrix equal to zero? - pub fn is_zero(&self) -> bool { - self.m11.is_zero() - && self.m12.is_zero() - && self.m13.is_zero() - && self.m22.is_zero() - && self.m23.is_zero() - && self.m33.is_zero() - } - - /// Compute the inverse of this SDP matrix without performing any inversibility check. - pub fn inverse_unchecked(&self) -> Self { - let minor_m12_m23 = self.m22 * self.m33 - self.m23 * self.m23; - let minor_m11_m23 = self.m12 * self.m33 - self.m13 * self.m23; - let minor_m11_m22 = self.m12 * self.m23 - self.m13 * self.m22; - - let determinant = - self.m11 * minor_m12_m23 - self.m12 * minor_m11_m23 + self.m13 * minor_m11_m22; - let inv_det = N::one() / determinant; - - SdpMatrix3 { - m11: minor_m12_m23 * inv_det, - m12: -minor_m11_m23 * inv_det, - m13: minor_m11_m22 * inv_det, - m22: (self.m11 * self.m33 - self.m13 * self.m13) * inv_det, - m23: (self.m13 * self.m12 - self.m23 * self.m11) * inv_det, - m33: (self.m11 * self.m22 - self.m12 * self.m12) * inv_det, - } - } - - /// Compute the quadratic form `m.transpose() * self * m`. - pub fn quadform3x2(&self, m: &Matrix3x2) -> SdpMatrix2 { - let x0 = self.m11 * m.m11 + self.m12 * m.m21 + self.m13 * m.m31; - let y0 = self.m12 * m.m11 + self.m22 * m.m21 + self.m23 * m.m31; - let z0 = self.m13 * m.m11 + self.m23 * m.m21 + self.m33 * m.m31; - - let x1 = self.m11 * m.m12 + self.m12 * m.m22 + self.m13 * m.m32; - let y1 = self.m12 * m.m12 + self.m22 * m.m22 + self.m23 * m.m32; - let z1 = self.m13 * m.m12 + self.m23 * m.m22 + self.m33 * m.m32; - - let m11 = m.m11 * x0 + m.m21 * y0 + m.m31 * z0; - let m12 = m.m11 * x1 + m.m21 * y1 + m.m31 * z1; - let m22 = m.m12 * x1 + m.m22 * y1 + m.m32 * z1; - - SdpMatrix2 { m11, m12, m22 } - } - - /// Compute the quadratic form `m.transpose() * self * m`. - pub fn quadform(&self, m: &Matrix3) -> Self { - let x0 = self.m11 * m.m11 + self.m12 * m.m21 + self.m13 * m.m31; - let y0 = self.m12 * m.m11 + self.m22 * m.m21 + self.m23 * m.m31; - let z0 = self.m13 * m.m11 + self.m23 * m.m21 + self.m33 * m.m31; - - let x1 = self.m11 * m.m12 + self.m12 * m.m22 + self.m13 * m.m32; - let y1 = self.m12 * m.m12 + self.m22 * m.m22 + self.m23 * m.m32; - let z1 = self.m13 * m.m12 + self.m23 * m.m22 + self.m33 * m.m32; - - let x2 = self.m11 * m.m13 + self.m12 * m.m23 + self.m13 * m.m33; - let y2 = self.m12 * m.m13 + self.m22 * m.m23 + self.m23 * m.m33; - let z2 = self.m13 * m.m13 + self.m23 * m.m23 + self.m33 * m.m33; - - let m11 = m.m11 * x0 + m.m21 * y0 + m.m31 * z0; - let m12 = m.m11 * x1 + m.m21 * y1 + m.m31 * z1; - let m13 = m.m11 * x2 + m.m21 * y2 + m.m31 * z2; - - let m22 = m.m12 * x1 + m.m22 * y1 + m.m32 * z1; - let m23 = m.m12 * x2 + m.m22 * y2 + m.m32 * z2; - let m33 = m.m13 * x2 + m.m23 * y2 + m.m33 * z2; - - Self { - m11, - m12, - m13, - m22, - m23, - m33, - } - } - - /// Adds `elt` to the diagonal components of `self`. - pub fn add_diagonal(&self, elt: N) -> Self { - Self { - m11: self.m11 + elt, - m12: self.m12, - m13: self.m13, - m22: self.m22 + elt, - m23: self.m23, - m33: self.m33 + elt, - } - } -} - -impl> Add> for SdpMatrix3 { - type Output = SdpMatrix3; - - fn add(self, rhs: SdpMatrix3) -> Self::Output { - SdpMatrix3 { - m11: self.m11 + rhs.m11, - m12: self.m12 + rhs.m12, - m13: self.m13 + rhs.m13, - m22: self.m22 + rhs.m22, - m23: self.m23 + rhs.m23, - m33: self.m33 + rhs.m33, - } - } -} - -impl Mul> for SdpMatrix3 { - type Output = Vector3; - - fn mul(self, rhs: Vector3) -> Self::Output { - let x = self.m11 * rhs.x + self.m12 * rhs.y + self.m13 * rhs.z; - let y = self.m12 * rhs.x + self.m22 * rhs.y + self.m23 * rhs.z; - let z = self.m13 * rhs.x + self.m23 * rhs.y + self.m33 * rhs.z; - Vector3::new(x, y, z) - } -} - -impl Mul> for SdpMatrix3 { - type Output = Matrix3; - - fn mul(self, rhs: Matrix3) -> Self::Output { - let x0 = self.m11 * rhs.m11 + self.m12 * rhs.m21 + self.m13 * rhs.m31; - let y0 = self.m12 * rhs.m11 + self.m22 * rhs.m21 + self.m23 * rhs.m31; - let z0 = self.m13 * rhs.m11 + self.m23 * rhs.m21 + self.m33 * rhs.m31; - - let x1 = self.m11 * rhs.m12 + self.m12 * rhs.m22 + self.m13 * rhs.m32; - let y1 = self.m12 * rhs.m12 + self.m22 * rhs.m22 + self.m23 * rhs.m32; - let z1 = self.m13 * rhs.m12 + self.m23 * rhs.m22 + self.m33 * rhs.m32; - - let x2 = self.m11 * rhs.m13 + self.m12 * rhs.m23 + self.m13 * rhs.m33; - let y2 = self.m12 * rhs.m13 + self.m22 * rhs.m23 + self.m23 * rhs.m33; - let z2 = self.m13 * rhs.m13 + self.m23 * rhs.m23 + self.m33 * rhs.m33; - - Matrix3::new(x0, x1, x2, y0, y1, y2, z0, z1, z2) - } -} - -impl Mul> for SdpMatrix3 { - type Output = Matrix3x2; - - fn mul(self, rhs: Matrix3x2) -> Self::Output { - let x0 = self.m11 * rhs.m11 + self.m12 * rhs.m21 + self.m13 * rhs.m31; - let y0 = self.m12 * rhs.m11 + self.m22 * rhs.m21 + self.m23 * rhs.m31; - let z0 = self.m13 * rhs.m11 + self.m23 * rhs.m21 + self.m33 * rhs.m31; - - let x1 = self.m11 * rhs.m12 + self.m12 * rhs.m22 + self.m13 * rhs.m32; - let y1 = self.m12 * rhs.m12 + self.m22 * rhs.m22 + self.m23 * rhs.m32; - let z1 = self.m13 * rhs.m12 + self.m23 * rhs.m22 + self.m33 * rhs.m32; - - Matrix3x2::new(x0, x1, y0, y1, z0, z1) - } -} - -impl WAngularInertia for SdpMatrix3 { +#[cfg(feature = "dim3")] +impl WAngularInertia for AngularInertia { type AngVector = Vector3; type LinVector = Vector3; type AngMatrix = Matrix3; @@ -812,7 +485,7 @@ impl WAngularInertia for SdpMatrix3 { if determinant.is_zero() { Self::zero() } else { - SdpMatrix3 { + AngularInertia { m11: minor_m12_m23 / determinant, m12: -minor_m11_m23 / determinant, m13: minor_m11_m22 / determinant, @@ -824,7 +497,7 @@ impl WAngularInertia for SdpMatrix3 { } fn squared(&self) -> Self { - SdpMatrix3 { + AngularInertia { m11: self.m11 * self.m11 + self.m12 * self.m12 + self.m13 * self.m13, m12: self.m11 * self.m12 + self.m12 * self.m22 + self.m13 * self.m23, m13: self.m11 * self.m13 + self.m12 * self.m23 + self.m13 * self.m33, @@ -860,10 +533,11 @@ impl WAngularInertia for SdpMatrix3 { } } -impl WAngularInertia for SdpMatrix3 { - type AngVector = Vector3; - type LinVector = Vector3; - type AngMatrix = Matrix3; +#[cfg(feature = "dim3")] +impl WAngularInertia for AngularInertia { + type AngVector = Vector3; + type LinVector = Vector3; + type AngMatrix = Matrix3; fn inverse(&self) -> Self { let minor_m12_m23 = self.m22 * self.m33 - self.m23 * self.m23; @@ -873,11 +547,11 @@ impl WAngularInertia for SdpMatrix3 { let determinant = self.m11 * minor_m12_m23 - self.m12 * minor_m11_m23 + self.m13 * minor_m11_m22; - let zero = ::zero(); + let zero = ::zero(); let is_zero = determinant.simd_eq(zero); - let inv_det = (::one() / determinant).select(is_zero, zero); + let inv_det = (::one() / determinant).select(is_zero, zero); - SdpMatrix3 { + AngularInertia { m11: minor_m12_m23 * inv_det, m12: -minor_m11_m23 * inv_det, m13: minor_m11_m22 * inv_det, @@ -887,11 +561,11 @@ impl WAngularInertia for SdpMatrix3 { } } - fn transform_lin_vector(&self, v: Vector3) -> Vector3 { + fn transform_lin_vector(&self, v: Vector3) -> Vector3 { self.transform_vector(v) } - fn transform_vector(&self, v: Vector3) -> Vector3 { + fn transform_vector(&self, v: Vector3) -> Vector3 { let x = self.m11 * v.x + self.m12 * v.y + self.m13 * v.z; let y = self.m12 * v.x + self.m22 * v.y + self.m23 * v.z; let z = self.m13 * v.x + self.m23 * v.y + self.m33 * v.z; @@ -899,7 +573,7 @@ impl WAngularInertia for SdpMatrix3 { } fn squared(&self) -> Self { - SdpMatrix3 { + AngularInertia { m11: self.m11 * self.m11 + self.m12 * self.m12 + self.m13 * self.m13, m12: self.m11 * self.m12 + self.m12 * self.m22 + self.m13 * self.m23, m13: self.m11 * self.m13 + self.m12 * self.m23 + self.m13 * self.m33, @@ -910,7 +584,7 @@ impl WAngularInertia for SdpMatrix3 { } #[rustfmt::skip] - fn into_matrix(self) -> Matrix3 { + fn into_matrix(self) -> Matrix3 { Matrix3::new( self.m11, self.m12, self.m13, self.m12, self.m22, self.m23, @@ -919,7 +593,7 @@ impl WAngularInertia for SdpMatrix3 { } #[rustfmt::skip] - fn transform_matrix(&self, m: &Matrix3) -> Matrix3 { + fn transform_matrix(&self, m: &Matrix3) -> Matrix3 { let x0 = self.m11 * m.m11 + self.m12 * m.m21 + self.m13 * m.m31; let y0 = self.m12 * m.m11 + self.m22 * m.m21 + self.m23 * m.m31; let z0 = self.m13 * m.m11 + self.m23 * m.m21 + self.m33 * m.m31; @@ -940,206 +614,6 @@ impl WAngularInertia for SdpMatrix3 { } } -impl From<[SdpMatrix3; 4]> for SdpMatrix3 -where - T: From<[f32; 4]>, -{ - fn from(data: [SdpMatrix3; 4]) -> Self { - SdpMatrix3 { - m11: T::from([data[0].m11, data[1].m11, data[2].m11, data[3].m11]), - m12: T::from([data[0].m12, data[1].m12, data[2].m12, data[3].m12]), - m13: T::from([data[0].m13, data[1].m13, data[2].m13, data[3].m13]), - m22: T::from([data[0].m22, data[1].m22, data[2].m22, data[3].m22]), - m23: T::from([data[0].m23, data[1].m23, data[2].m23, data[3].m23]), - m33: T::from([data[0].m33, data[1].m33, data[2].m33, data[3].m33]), - } - } -} - -#[cfg(feature = "simd-nightly")] -impl From<[SdpMatrix3; 8]> for SdpMatrix3 { - fn from(data: [SdpMatrix3; 8]) -> Self { - SdpMatrix3 { - m11: simba::simd::f32x8::from([ - data[0].m11, - data[1].m11, - data[2].m11, - data[3].m11, - data[4].m11, - data[5].m11, - data[6].m11, - data[7].m11, - ]), - m12: simba::simd::f32x8::from([ - data[0].m12, - data[1].m12, - data[2].m12, - data[3].m12, - data[4].m12, - data[5].m12, - data[6].m12, - data[7].m12, - ]), - m13: simba::simd::f32x8::from([ - data[0].m13, - data[1].m13, - data[2].m13, - data[3].m13, - data[4].m13, - data[5].m13, - data[6].m13, - data[7].m13, - ]), - m22: simba::simd::f32x8::from([ - data[0].m22, - data[1].m22, - data[2].m22, - data[3].m22, - data[4].m22, - data[5].m22, - data[6].m22, - data[7].m22, - ]), - m23: simba::simd::f32x8::from([ - data[0].m23, - data[1].m23, - data[2].m23, - data[3].m23, - data[4].m23, - data[5].m23, - data[6].m23, - data[7].m23, - ]), - m33: simba::simd::f32x8::from([ - data[0].m33, - data[1].m33, - data[2].m33, - data[3].m33, - data[4].m33, - data[5].m33, - data[6].m33, - data[7].m33, - ]), - } - } -} - -#[cfg(feature = "simd-nightly")] -impl From<[SdpMatrix3; 16]> for SdpMatrix3 { - fn from(data: [SdpMatrix3; 16]) -> Self { - SdpMatrix3 { - m11: simba::simd::f32x16::from([ - data[0].m11, - data[1].m11, - data[2].m11, - data[3].m11, - data[4].m11, - data[5].m11, - data[6].m11, - data[7].m11, - data[8].m11, - data[9].m11, - data[10].m11, - data[11].m11, - data[12].m11, - data[13].m11, - data[14].m11, - data[15].m11, - ]), - m12: simba::simd::f32x16::from([ - data[0].m12, - data[1].m12, - data[2].m12, - data[3].m12, - data[4].m12, - data[5].m12, - data[6].m12, - data[7].m12, - data[8].m12, - data[9].m12, - data[10].m12, - data[11].m12, - data[12].m12, - data[13].m12, - data[14].m12, - data[15].m12, - ]), - m13: simba::simd::f32x16::from([ - data[0].m13, - data[1].m13, - data[2].m13, - data[3].m13, - data[4].m13, - data[5].m13, - data[6].m13, - data[7].m13, - data[8].m13, - data[9].m13, - data[10].m13, - data[11].m13, - data[12].m13, - data[13].m13, - data[14].m13, - data[15].m13, - ]), - m22: simba::simd::f32x16::from([ - data[0].m22, - data[1].m22, - data[2].m22, - data[3].m22, - data[4].m22, - data[5].m22, - data[6].m22, - data[7].m22, - data[8].m22, - data[9].m22, - data[10].m22, - data[11].m22, - data[12].m22, - data[13].m22, - data[14].m22, - data[15].m22, - ]), - m23: simba::simd::f32x16::from([ - data[0].m23, - data[1].m23, - data[2].m23, - data[3].m23, - data[4].m23, - data[5].m23, - data[6].m23, - data[7].m23, - data[8].m23, - data[9].m23, - data[10].m23, - data[11].m23, - data[12].m23, - data[13].m23, - data[14].m23, - data[15].m23, - ]), - m33: simba::simd::f32x16::from([ - data[0].m33, - data[1].m33, - data[2].m33, - data[3].m33, - data[4].m33, - data[5].m33, - data[6].m33, - data[7].m33, - data[8].m33, - data[9].m33, - data[10].m33, - data[11].m33, - data[12].m33, - data[13].m33, - data[14].m33, - data[15].m33, - ]), - } - } -} - // This is an RAII structure that enables flushing denormal numbers // to zero, and automatically reseting previous flags once it is dropped. #[derive(Clone, Debug, PartialEq, Eq)] -- cgit From e231bacec608fa5efd24f7a876572927dbd6c9c4 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Thu, 17 Dec 2020 10:24:36 +0100 Subject: Move all the contact manifold computations out of Rapier. --- src/utils.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 4089631..48414bc 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,6 +5,7 @@ use na::{Matrix2, Matrix3, Matrix3x2, Point2, Point3, Scalar, SimdRealField, Vec use num::Zero; use simba::simd::SimdValue; +use eagl::utils::SdpMatrix3; use std::ops::{Add, Mul}; use { crate::math::{AngularInertia, SimdBool, SimdReal}, @@ -468,8 +469,7 @@ impl WAngularInertia for SimdReal { } } -#[cfg(feature = "dim3")] -impl WAngularInertia for AngularInertia { +impl WAngularInertia for SdpMatrix3 { type AngVector = Vector3; type LinVector = Vector3; type AngMatrix = Matrix3; @@ -485,7 +485,7 @@ impl WAngularInertia for AngularInertia { if determinant.is_zero() { Self::zero() } else { - AngularInertia { + SdpMatrix3 { m11: minor_m12_m23 / determinant, m12: -minor_m11_m23 / determinant, m13: minor_m11_m22 / determinant, @@ -497,7 +497,7 @@ impl WAngularInertia for AngularInertia { } fn squared(&self) -> Self { - AngularInertia { + SdpMatrix3 { m11: self.m11 * self.m11 + self.m12 * self.m12 + self.m13 * self.m13, m12: self.m11 * self.m12 + self.m12 * self.m22 + self.m13 * self.m23, m13: self.m11 * self.m13 + self.m12 * self.m23 + self.m13 * self.m33, @@ -533,8 +533,7 @@ impl WAngularInertia for AngularInertia { } } -#[cfg(feature = "dim3")] -impl WAngularInertia for AngularInertia { +impl WAngularInertia for SdpMatrix3 { type AngVector = Vector3; type LinVector = Vector3; type AngMatrix = Matrix3; @@ -551,7 +550,7 @@ impl WAngularInertia for AngularInertia { let is_zero = determinant.simd_eq(zero); let inv_det = (::one() / determinant).select(is_zero, zero); - AngularInertia { + SdpMatrix3 { m11: minor_m12_m23 * inv_det, m12: -minor_m11_m23 * inv_det, m13: minor_m11_m22 * inv_det, @@ -573,7 +572,7 @@ impl WAngularInertia for AngularInertia { } fn squared(&self) -> Self { - AngularInertia { + SdpMatrix3 { m11: self.m11 * self.m11 + self.m12 * self.m12 + self.m13 * self.m13, m12: self.m11 * self.m12 + self.m12 * self.m22 + self.m13 * self.m23, m13: self.m11 * self.m13 + self.m12 * self.m23 + self.m13 * self.m33, -- cgit From 8fe2df126a279a435cc544b150aadf8f7b757868 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Thu, 17 Dec 2020 18:37:16 +0100 Subject: Remove some irrelevant code. --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 48414bc..ab7dc32 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,7 +5,7 @@ use na::{Matrix2, Matrix3, Matrix3x2, Point2, Point3, Scalar, SimdRealField, Vec use num::Zero; use simba::simd::SimdValue; -use eagl::utils::SdpMatrix3; +use cdl::utils::SdpMatrix3; use std::ops::{Add, Mul}; use { crate::math::{AngularInertia, SimdBool, SimdReal}, -- cgit From 7488cd02e89e82b3408e67411b2460d748c14f9f Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Fri, 18 Dec 2020 16:58:57 +0100 Subject: Remove unused constrants. --- src/utils.rs | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index ab7dc32..1f06ee5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -13,18 +13,6 @@ use { num::One, }; -// pub(crate) const SIN_10_DEGREES: f32 = 0.17364817766; -// pub(crate) const COS_10_DEGREES: f32 = 0.98480775301; -// pub(crate) const COS_45_DEGREES: f32 = 0.70710678118; -// pub(crate) const SIN_45_DEGREES: f32 = COS_45_DEGREES; -#[cfg(feature = "dim3")] -pub(crate) const COS_1_DEGREES: f32 = 0.99984769515; -pub(crate) const COS_5_DEGREES: f32 = 0.99619469809; -// #[cfg(feature = "dim2")] -pub(crate) const COS_FRAC_PI_8: f32 = 0.92387953251; -#[cfg(feature = "dim2")] -pub(crate) const SIN_FRAC_PI_8: f32 = 0.38268343236; - pub(crate) fn inv(val: f32) -> f32 { if val == 0.0 { 0.0 -- cgit From 967145a9492175be59e8db33299b1687d69d84e2 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Thu, 31 Dec 2020 11:16:03 +0100 Subject: Perform contact sorting in the narrow-phase directly. --- src/utils.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 1f06ee5..932c6e2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,17 +1,12 @@ //! Miscellaneous utilities. use crate::dynamics::RigidBodyHandle; -use na::{Matrix2, Matrix3, Matrix3x2, Point2, Point3, Scalar, SimdRealField, Vector2, Vector3}; +use na::{Matrix3, Point2, Point3, Scalar, SimdRealField, Vector2, Vector3}; use num::Zero; use simba::simd::SimdValue; use cdl::utils::SdpMatrix3; -use std::ops::{Add, Mul}; -use { - crate::math::{AngularInertia, SimdBool, SimdReal}, - na::SimdPartialOrd, - num::One, -}; +use {crate::math::SimdReal, na::SimdPartialOrd, num::One}; pub(crate) fn inv(val: f32) -> f32 { if val == 0.0 { -- cgit From aa61fe65e3ff0289ecab57b4053a3410cf6d4a87 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Mon, 4 Jan 2021 15:14:25 +0100 Subject: Add support of 64-bits reals. --- src/utils.rs | 93 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 44 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 932c6e2..3f76c8b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -6,9 +6,13 @@ use num::Zero; use simba::simd::SimdValue; use cdl::utils::SdpMatrix3; -use {crate::math::SimdReal, na::SimdPartialOrd, num::One}; +use { + crate::math::{Real, SimdReal}, + na::SimdPartialOrd, + num::One, +}; -pub(crate) fn inv(val: f32) -> f32 { +pub(crate) fn inv(val: Real) -> Real { if val == 0.0 { 0.0 } else { @@ -23,10 +27,11 @@ pub trait WSign: Sized { fn copy_sign_to(self, to: Rhs) -> Rhs; } -impl WSign for f32 { +impl WSign for Real { fn copy_sign_to(self, to: Self) -> Self { - let signbit: u32 = (-0.0f32).to_bits(); - f32::from_bits((signbit & self.to_bits()) | ((!signbit) & to.to_bits())) + const MINUS_ZERO: Real = -0.0; + let signbit = MINUS_ZERO.to_bits(); + Real::from_bits((signbit & self.to_bits()) | ((!signbit) & to.to_bits())) } } @@ -75,8 +80,8 @@ pub(crate) trait WComponent: Sized { fn max_component(self) -> Self::Element; } -impl WComponent for f32 { - type Element = f32; +impl WComponent for Real { + type Element = Real; fn min_component(self) -> Self::Element { self @@ -87,7 +92,7 @@ impl WComponent for f32 { } impl WComponent for SimdReal { - type Element = f32; + type Element = Real; fn min_component(self) -> Self::Element { self.simd_horizontal_min() @@ -221,8 +226,8 @@ pub(crate) trait WCrossMatrix: Sized { fn gcross_matrix(self) -> Self::CrossMat; } -impl WCrossMatrix for Vector3 { - type CrossMat = Matrix3; +impl WCrossMatrix for Vector3 { + type CrossMat = Matrix3; #[inline] #[rustfmt::skip] @@ -235,8 +240,8 @@ impl WCrossMatrix for Vector3 { } } -impl WCrossMatrix for Vector2 { - type CrossMat = Vector2; +impl WCrossMatrix for Vector2 { + type CrossMat = Vector2; #[inline] fn gcross_matrix(self) -> Self::CrossMat { @@ -249,26 +254,26 @@ pub(crate) trait WCross: Sized { fn gcross(&self, rhs: Rhs) -> Self::Result; } -impl WCross> for Vector3 { +impl WCross> for Vector3 { type Result = Self; - fn gcross(&self, rhs: Vector3) -> Self::Result { + fn gcross(&self, rhs: Vector3) -> Self::Result { self.cross(&rhs) } } -impl WCross> for Vector2 { - type Result = f32; +impl WCross> for Vector2 { + type Result = Real; - fn gcross(&self, rhs: Vector2) -> Self::Result { + fn gcross(&self, rhs: Vector2) -> Self::Result { self.x * rhs.y - self.y * rhs.x } } -impl WCross> for f32 { - type Result = Vector2; +impl WCross> for Real { + type Result = Vector2; - fn gcross(&self, rhs: Vector2) -> Self::Result { + fn gcross(&self, rhs: Vector2) -> Self::Result { Vector2::new(-rhs.y * *self, rhs.x * *self) } } @@ -278,26 +283,26 @@ pub(crate) trait WDot: Sized { fn gdot(&self, rhs: Rhs) -> Self::Result; } -impl WDot> for Vector3 { - type Result = f32; +impl WDot> for Vector3 { + type Result = Real; - fn gdot(&self, rhs: Vector3) -> Self::Result { + fn gdot(&self, rhs: Vector3) -> Self::Result { self.x * rhs.x + self.y * rhs.y + self.z * rhs.z } } -impl WDot> for Vector2 { - type Result = f32; +impl WDot> for Vector2 { + type Result = Real; - fn gdot(&self, rhs: Vector2) -> Self::Result { + fn gdot(&self, rhs: Vector2) -> Self::Result { self.x * rhs.x + self.y * rhs.y } } -impl WDot for f32 { - type Result = f32; +impl WDot for Real { + type Result = Real; - fn gdot(&self, rhs: f32) -> Self::Result { + fn gdot(&self, rhs: Real) -> Self::Result { *self * rhs } } @@ -387,10 +392,10 @@ pub(crate) trait WAngularInertia { fn into_matrix(self) -> Self::AngMatrix; } -impl WAngularInertia for f32 { - type AngVector = f32; - type LinVector = Vector2; - type AngMatrix = f32; +impl WAngularInertia for Real { + type AngVector = Real; + type LinVector = Vector2; + type AngMatrix = Real; fn inverse(&self) -> Self { if *self != 0.0 { @@ -400,14 +405,14 @@ impl WAngularInertia for f32 { } } - fn transform_lin_vector(&self, pt: Vector2) -> Vector2 { + fn transform_lin_vector(&self, pt: Vector2) -> Vector2 { *self * pt } - fn transform_vector(&self, pt: f32) -> f32 { + fn transform_vector(&self, pt: Real) -> Real { *self * pt } - fn squared(&self) -> f32 { + fn squared(&self) -> Real { *self * *self } @@ -452,10 +457,10 @@ impl WAngularInertia for SimdReal { } } -impl WAngularInertia for SdpMatrix3 { - type AngVector = Vector3; - type LinVector = Vector3; - type AngMatrix = Matrix3; +impl WAngularInertia for SdpMatrix3 { + type AngVector = Vector3; + type LinVector = Vector3; + type AngMatrix = Matrix3; fn inverse(&self) -> Self { let minor_m12_m23 = self.m22 * self.m33 - self.m23 * self.m23; @@ -490,11 +495,11 @@ impl WAngularInertia for SdpMatrix3 { } } - fn transform_lin_vector(&self, v: Vector3) -> Vector3 { + fn transform_lin_vector(&self, v: Vector3) -> Vector3 { self.transform_vector(v) } - fn transform_vector(&self, v: Vector3) -> Vector3 { + fn transform_vector(&self, v: Vector3) -> Vector3 { let x = self.m11 * v.x + self.m12 * v.y + self.m13 * v.z; let y = self.m12 * v.x + self.m22 * v.y + self.m23 * v.z; let z = self.m13 * v.x + self.m23 * v.y + self.m33 * v.z; @@ -502,7 +507,7 @@ impl WAngularInertia for SdpMatrix3 { } #[rustfmt::skip] - fn into_matrix(self) -> Matrix3 { + fn into_matrix(self) -> Matrix3 { Matrix3::new( self.m11, self.m12, self.m13, self.m12, self.m22, self.m23, @@ -511,7 +516,7 @@ impl WAngularInertia for SdpMatrix3 { } #[rustfmt::skip] - fn transform_matrix(&self, m: &Matrix3) -> Matrix3 { + fn transform_matrix(&self, m: &Matrix3) -> Matrix3 { *self * *m } } -- cgit From 0ade350b5f4b6e7c0c4116e1f4f2b30ab86b7e52 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Wed, 20 Jan 2021 16:33:42 +0100 Subject: Use newtypes for collider, rigid-body and joint handles. --- src/utils.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 3f76c8b..a6c4ef5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,5 @@ //! Miscellaneous utilities. -use crate::dynamics::RigidBodyHandle; use na::{Matrix3, Point2, Point3, Scalar, SimdRealField, Vector2, Vector3}; use num::Zero; use simba::simd::SimdValue; @@ -658,11 +657,8 @@ impl Drop for FlushToZeroDenormalsAreZeroFlags { } } -pub(crate) fn other_handle( - pair: (RigidBodyHandle, RigidBodyHandle), - handle: RigidBodyHandle, -) -> RigidBodyHandle { - if pair.0 == handle { +pub(crate) fn select_other(pair: (T, T), elt: T) -> T { + if pair.0 == elt { pair.1 } else { pair.0 -- cgit From 8f7220f03d3c23574b9ece09d81d32e862f1b5c6 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Sun, 24 Jan 2021 11:13:44 +0100 Subject: Rename cdl to parry. --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index a6c4ef5..e131b0a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,7 +4,7 @@ use na::{Matrix3, Point2, Point3, Scalar, SimdRealField, Vector2, Vector3}; use num::Zero; use simba::simd::SimdValue; -use cdl::utils::SdpMatrix3; +use parry::utils::SdpMatrix3; use { crate::math::{Real, SimdReal}, na::SimdPartialOrd, -- cgit