Skip to main content

rapier3d/utils/
scalar_type.rs

1//! ScalarType trait for generic scalar types in Rapier.
2
3use crate::math::SimdReal;
4use crate::math::{AngularInertia, Matrix, Pose, Real, Rotation, Vector};
5#[cfg(feature = "dim3")]
6use crate::utils::SimdSelect;
7use crate::utils::{
8    AngularInertiaOps, ComponentMul, CrossProduct, CrossProductMatrix, DIM_MINUS_ONE, DotProduct,
9    MatrixColumn, OrthonormalBasis, PoseOps, RotationOps,
10};
11use core::fmt::Debug;
12use core::ops::{Add, AddAssign, DivAssign, Index, Mul, MulAssign, Neg, Sub, SubAssign};
13use na::SimdRealField;
14
15/// Trait for types that can be used as scalars in the generic code supporting both
16/// the scalar and AoSoA SIMD pattern.
17///
18/// This trait is mostly for internal use only. No other implementations of this trait
19/// is expected.
20pub trait ScalarType:
21    SimdRealField<Element = Real>
22    + Copy
23    + CrossProduct<Self::Vector, Result = Self::Vector>
24    + DotProduct<Self, Result = Self>
25{
26    /// The pose type (position + rotation) for this scalar.
27    type Pose: Copy + PoseOps<Self> + Mul<Self::Vector, Output = Self::Vector>;
28    /// The vector type for this scalar (2D).
29    #[cfg(feature = "dim2")]
30    type Vector: Copy
31        + Debug
32        + Default
33        + Neg<Output = Self::Vector>
34        + Add<Self::Vector, Output = Self::Vector>
35        + Sub<Self::Vector, Output = Self::Vector>
36        + AddAssign<Self::Vector>
37        + SubAssign<Self::Vector>
38        + MulAssign<Self>
39        + DivAssign<Self>
40        + Mul<Self, Output = Self::Vector>
41        + Index<usize, Output = Self>
42        + DotProduct<Self::Vector, Result = Self>
43        + OrthonormalBasis<Basis = [Self::Vector; DIM_MINUS_ONE]>
44        + CrossProduct<Self::Vector, Result = Self>
45        + CrossProductMatrix<CrossMat = Self::Vector, CrossMatTr = Self::Vector>
46        + ComponentMul;
47    /// The vector type for this scalar (3D).
48    #[cfg(feature = "dim3")]
49    type Vector: Copy
50        + Debug
51        + Default
52        + Neg<Output = Self::Vector>
53        + Add<Self::Vector, Output = Self::Vector>
54        + Sub<Self::Vector, Output = Self::Vector>
55        + AddAssign<Self::Vector>
56        + SubAssign<Self::Vector>
57        + MulAssign<Self>
58        + DivAssign<Self>
59        + Mul<Self, Output = Self::Vector>
60        + Index<usize, Output = Self>
61        + DotProduct<Self::Vector, Result = Self>
62        + OrthonormalBasis<Basis = [Self::Vector; DIM_MINUS_ONE]>
63        + CrossProduct<Self::Vector, Result = Self::Vector>
64        + CrossProductMatrix<CrossMat = Self::Matrix, CrossMatTr = Self::Matrix>
65        + SimdSelect<Self>
66        + ComponentMul
67        + Into<Self::AngVector>; // In 3D Vec and Ang are technically the same.
68    /// The angular vector type for this scalar (2D: scalar, 3D: vector).
69    #[cfg(feature = "dim2")]
70    type AngVector: Copy
71        + Debug
72        + Default
73        + Add<Self::AngVector, Output = Self::AngVector>
74        + Sub<Self::AngVector, Output = Self::AngVector>
75        + AddAssign<Self::AngVector>
76        + SubAssign<Self::AngVector>
77        + MulAssign<Self>
78        + DivAssign<Self>
79        + Mul<Self, Output = Self::AngVector>
80        + DotProduct<Self::AngVector, Result = Self>
81        + num::One
82        + From<Self>;
83    /// The angular vector type for this scalar (3D).
84    #[cfg(feature = "dim3")]
85    type AngVector: Copy
86        + Debug
87        + Default
88        + Add<Self::AngVector, Output = Self::AngVector>
89        + Sub<Self::AngVector, Output = Self::AngVector>
90        + AddAssign<Self::AngVector>
91        + SubAssign<Self::AngVector>
92        + MulAssign<Self>
93        + DivAssign<Self>
94        + Mul<Self, Output = Self::AngVector>
95        + DotProduct<Self::AngVector, Result = Self>;
96    /// The matrix type for this scalar.
97    type Matrix: Copy
98        + Debug
99        + MatrixColumn<Column = Self::Vector>
100        + MulAssign<Self>
101        + Mul<Self::Matrix, Output = Self::Matrix>;
102    /// The angular inertia type for this scalar.
103    type AngInertia: AngularInertiaOps<Self, AngVector = Self::AngVector>;
104    /// The rotation type for this scalar.
105    type Rotation: RotationOps<Self>;
106}
107
108impl ScalarType for Real {
109    type Pose = Pose;
110    type Vector = Vector;
111    #[cfg(feature = "dim2")]
112    type AngVector = Real;
113    #[cfg(feature = "dim3")]
114    type AngVector = Vector;
115    type Matrix = Matrix;
116    type AngInertia = AngularInertia;
117    type Rotation = Rotation;
118}
119
120#[cfg(feature = "dim3")]
121impl ScalarType for SimdReal {
122    type Pose = na::Isometry3<SimdReal>;
123    type Vector = na::Vector3<SimdReal>;
124    type AngVector = na::Vector3<SimdReal>;
125    type Matrix = na::Matrix3<SimdReal>;
126    type AngInertia = parry::utils::SdpMatrix3<SimdReal>;
127    type Rotation = na::UnitQuaternion<SimdReal>;
128}
129
130#[cfg(feature = "dim2")]
131impl ScalarType for SimdReal {
132    type Pose = na::Isometry2<SimdReal>;
133    type Vector = na::Vector2<SimdReal>;
134    type AngVector = SimdReal;
135    type Matrix = na::Matrix2<SimdReal>;
136    type AngInertia = SimdReal;
137    type Rotation = na::UnitComplex<SimdReal>;
138}