sparkl2d_core/
lib.rs

1#![no_std]
2
3pub extern crate nalgebra as na;
4
5#[cfg(feature = "serde")]
6#[macro_use]
7extern crate serde;
8
9pub mod prelude {
10    pub use crate::dynamics::models::*;
11    pub use crate::dynamics::solver::*;
12    pub use crate::dynamics::*;
13    pub use crate::math::*;
14}
15
16pub mod math {
17    // pub use super::parry::math::*; // FIXME: make parry no-std friendly and import its math module instead.
18    pub use super::parry_math::math::*;
19    pub type Kernel = crate::dynamics::solver::QuadraticKernel;
20
21    #[derive(Copy, Clone, Debug, PartialEq)]
22    pub struct DecomposedTensor {
23        pub deviatoric_part: Matrix<Real>,
24        pub spherical_part: Real,
25    }
26
27    impl DecomposedTensor {
28        pub fn decompose(tensor: &Matrix<Real>) -> Self {
29            let spherical_part = tensor.trace() / (DIM as Real);
30            let mut deviatoric_part = *tensor;
31
32            for i in 0..DIM {
33                deviatoric_part[(i, i)] -= spherical_part;
34            }
35
36            Self {
37                deviatoric_part,
38                spherical_part,
39            }
40        }
41
42        pub fn zero() -> Self {
43            Self {
44                deviatoric_part: Matrix::zeros(),
45                spherical_part: 0.0,
46            }
47        }
48
49        pub fn recompose(&self) -> Matrix<Real> {
50            self.deviatoric_part + Matrix::identity() * self.spherical_part
51        }
52    }
53
54    impl core::ops::Add<DecomposedTensor> for DecomposedTensor {
55        type Output = DecomposedTensor;
56
57        #[inline]
58        fn add(self, rhs: DecomposedTensor) -> DecomposedTensor {
59            DecomposedTensor {
60                deviatoric_part: self.deviatoric_part + rhs.deviatoric_part,
61                spherical_part: self.spherical_part + rhs.spherical_part,
62            }
63        }
64    }
65
66    impl core::ops::AddAssign<DecomposedTensor> for DecomposedTensor {
67        #[inline]
68        fn add_assign(&mut self, rhs: DecomposedTensor) {
69            self.deviatoric_part += rhs.deviatoric_part;
70            self.spherical_part += rhs.spherical_part;
71        }
72    }
73
74    impl core::ops::Mul<Vector<Real>> for DecomposedTensor {
75        type Output = Vector<Real>;
76
77        #[inline]
78        fn mul(self, rhs: Vector<Real>) -> Self::Output {
79            self.deviatoric_part * rhs + self.spherical_part * rhs
80        }
81    }
82}
83
84mod parry_math {
85
86    mod real {
87        /// The scalar type used throughout this crate.
88        #[cfg(feature = "f64")]
89        pub type Real = f64;
90
91        /// The scalar type used throughout this crate.
92        #[cfg(feature = "f32")]
93        pub type Real = f32;
94    }
95
96    /// Compilation flags dependent aliases for mathematical types.
97    #[cfg(feature = "dim3")]
98    pub mod math {
99        pub use super::real::*;
100        use na::{
101            Isometry3, Matrix3, Point3, Translation3, UnitQuaternion, UnitVector3, Vector3,
102            Vector6, U3, U6,
103        };
104
105        /// The default tolerance used for geometric operations.
106        pub const DEFAULT_EPSILON: Real = Real::EPSILON;
107
108        /// The dimension of the space.
109        pub const DIM: usize = 3;
110
111        /// The dimension of the ambient space.
112        pub type Dim = U3;
113
114        /// The dimension of a spatial vector.
115        pub type SpatialDim = U6;
116
117        /// The dimension of the rotations.
118        pub type AngDim = U3;
119
120        /// The point type.
121        pub type Point<N> = Point3<N>;
122
123        /// The angular vector type.
124        pub type AngVector<N> = Vector3<N>;
125
126        /// The vector type.
127        pub type Vector<N> = Vector3<N>;
128
129        /// The unit vector type.
130        pub type UnitVector<N> = UnitVector3<N>;
131
132        /// The matrix type.
133        pub type Matrix<N> = Matrix3<N>;
134
135        /// The vector type with dimension `SpatialDim × 1`.
136        pub type SpatialVector<N> = Vector6<N>;
137
138        /// The orientation type.
139        pub type Orientation<N> = Vector3<N>;
140
141        /// The transformation matrix type.
142        pub type Isometry<N> = Isometry3<N>;
143
144        /// The rotation matrix type.
145        pub type Rotation<N> = UnitQuaternion<N>;
146
147        /// The translation type.
148        pub type Translation<N> = Translation3<N>;
149
150        /// The principal angular inertia of a rigid body.
151        pub type PrincipalAngularInertia<N> = Vector3<N>;
152
153        /// A matrix that represent the cross product with a given vector.
154        pub type CrossMatrix<N> = Matrix3<N>;
155
156        /// A vector with a dimension equal to the maximum number of degrees of freedom of a rigid body.
157        pub type SpacialVector<N> = Vector6<N>;
158    }
159
160    /// Compilation flags dependent aliases for mathematical types.
161    #[cfg(feature = "dim2")]
162    pub mod math {
163        pub use super::real::*;
164        use na::{
165            Isometry2, Matrix2, Point2, Translation2, UnitComplex, UnitVector2, Vector1, Vector2,
166            Vector3, U2,
167        };
168
169        /// The default tolerance used for geometric operations.
170        pub const DEFAULT_EPSILON: Real = Real::EPSILON;
171
172        /// The dimension of the space.
173        pub const DIM: usize = 2;
174
175        /// The dimension of the ambient space.
176        pub type Dim = U2;
177
178        /// The point type.
179        pub type Point<N> = Point2<N>;
180
181        /// The angular vector type.
182        pub type AngVector<N> = N;
183
184        /// The vector type.
185        pub type Vector<N> = Vector2<N>;
186
187        /// The unit vector type.
188        pub type UnitVector<N> = UnitVector2<N>;
189
190        /// The matrix type.
191        pub type Matrix<N> = Matrix2<N>;
192
193        /// The orientation type.
194        pub type Orientation<N> = Vector1<N>;
195
196        /// The transformation matrix type.
197        pub type Isometry<N> = Isometry2<N>;
198
199        /// The rotation matrix type.
200        pub type Rotation<N> = UnitComplex<N>;
201
202        /// The translation type.
203        pub type Translation<N> = Translation2<N>;
204
205        /// The angular inertia of a rigid body.
206        pub type AngularInertia<N> = N;
207
208        /// The principal angular inertia of a rigid body.
209        pub type PrincipalAngularInertia<N> = N;
210
211        /// A matrix that represent the cross product with a given vector.
212        pub type CrossMatrix<N> = Vector2<N>;
213
214        /// A vector with a dimension equal to the maximum number of degrees of freedom of a rigid body.
215        pub type SpacialVector<N> = Vector3<N>;
216    }
217}
218
219pub mod dynamics;
220pub mod utils;