Skip to main content

rapier2d/utils/
scalar_type.rs

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