1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#![no_std]

pub extern crate nalgebra as na;

#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;

pub mod prelude {
    pub use crate::dynamics::models::*;
    pub use crate::dynamics::solver::*;
    pub use crate::dynamics::*;
    pub use crate::math::*;
}

pub mod math {
    // pub use super::parry::math::*; // FIXME: make parry no-std friendly and import its math module instead.
    pub use super::parry_math::math::*;
    pub type Kernel = crate::dynamics::solver::QuadraticKernel;

    #[derive(Copy, Clone, Debug, PartialEq)]
    pub struct DecomposedTensor {
        pub deviatoric_part: Matrix<Real>,
        pub spherical_part: Real,
    }

    impl DecomposedTensor {
        pub fn decompose(tensor: &Matrix<Real>) -> Self {
            let spherical_part = tensor.trace() / (DIM as Real);
            let mut deviatoric_part = *tensor;

            for i in 0..DIM {
                deviatoric_part[(i, i)] -= spherical_part;
            }

            Self {
                deviatoric_part,
                spherical_part,
            }
        }

        pub fn zero() -> Self {
            Self {
                deviatoric_part: Matrix::zeros(),
                spherical_part: 0.0,
            }
        }

        pub fn recompose(&self) -> Matrix<Real> {
            self.deviatoric_part + Matrix::identity() * self.spherical_part
        }
    }

    impl core::ops::Add<DecomposedTensor> for DecomposedTensor {
        type Output = DecomposedTensor;

        #[inline]
        fn add(self, rhs: DecomposedTensor) -> DecomposedTensor {
            DecomposedTensor {
                deviatoric_part: self.deviatoric_part + rhs.deviatoric_part,
                spherical_part: self.spherical_part + rhs.spherical_part,
            }
        }
    }

    impl core::ops::AddAssign<DecomposedTensor> for DecomposedTensor {
        #[inline]
        fn add_assign(&mut self, rhs: DecomposedTensor) {
            self.deviatoric_part += rhs.deviatoric_part;
            self.spherical_part += rhs.spherical_part;
        }
    }

    impl core::ops::Mul<Vector<Real>> for DecomposedTensor {
        type Output = Vector<Real>;

        #[inline]
        fn mul(self, rhs: Vector<Real>) -> Self::Output {
            self.deviatoric_part * rhs + self.spherical_part * rhs
        }
    }
}

mod parry_math {

    mod real {
        /// The scalar type used throughout this crate.
        #[cfg(feature = "f64")]
        pub type Real = f64;

        /// The scalar type used throughout this crate.
        #[cfg(feature = "f32")]
        pub type Real = f32;
    }

    /// Compilation flags dependent aliases for mathematical types.
    #[cfg(feature = "dim3")]
    pub mod math {
        pub use super::real::*;
        use na::{
            Isometry3, Matrix3, Point3, Translation3, UnitQuaternion, UnitVector3, Vector3,
            Vector6, U3, U6,
        };

        /// The default tolerance used for geometric operations.
        pub const DEFAULT_EPSILON: Real = Real::EPSILON;

        /// The dimension of the space.
        pub const DIM: usize = 3;

        /// The dimension of the ambient space.
        pub type Dim = U3;

        /// The dimension of a spatial vector.
        pub type SpatialDim = U6;

        /// The dimension of the rotations.
        pub type AngDim = U3;

        /// The point type.
        pub type Point<N> = Point3<N>;

        /// The angular vector type.
        pub type AngVector<N> = Vector3<N>;

        /// The vector type.
        pub type Vector<N> = Vector3<N>;

        /// The unit vector type.
        pub type UnitVector<N> = UnitVector3<N>;

        /// The matrix type.
        pub type Matrix<N> = Matrix3<N>;

        /// The vector type with dimension `SpatialDim × 1`.
        pub type SpatialVector<N> = Vector6<N>;

        /// The orientation type.
        pub type Orientation<N> = Vector3<N>;

        /// The transformation matrix type.
        pub type Isometry<N> = Isometry3<N>;

        /// The rotation matrix type.
        pub type Rotation<N> = UnitQuaternion<N>;

        /// The translation type.
        pub type Translation<N> = Translation3<N>;

        /// The principal angular inertia of a rigid body.
        pub type PrincipalAngularInertia<N> = Vector3<N>;

        /// A matrix that represent the cross product with a given vector.
        pub type CrossMatrix<N> = Matrix3<N>;

        /// A vector with a dimension equal to the maximum number of degrees of freedom of a rigid body.
        pub type SpacialVector<N> = Vector6<N>;
    }

    /// Compilation flags dependent aliases for mathematical types.
    #[cfg(feature = "dim2")]
    pub mod math {
        pub use super::real::*;
        use na::{
            Isometry2, Matrix2, Point2, Translation2, UnitComplex, UnitVector2, Vector1, Vector2,
            Vector3, U2,
        };

        /// The default tolerance used for geometric operations.
        pub const DEFAULT_EPSILON: Real = Real::EPSILON;

        /// The dimension of the space.
        pub const DIM: usize = 2;

        /// The dimension of the ambient space.
        pub type Dim = U2;

        /// The point type.
        pub type Point<N> = Point2<N>;

        /// The angular vector type.
        pub type AngVector<N> = N;

        /// The vector type.
        pub type Vector<N> = Vector2<N>;

        /// The unit vector type.
        pub type UnitVector<N> = UnitVector2<N>;

        /// The matrix type.
        pub type Matrix<N> = Matrix2<N>;

        /// The orientation type.
        pub type Orientation<N> = Vector1<N>;

        /// The transformation matrix type.
        pub type Isometry<N> = Isometry2<N>;

        /// The rotation matrix type.
        pub type Rotation<N> = UnitComplex<N>;

        /// The translation type.
        pub type Translation<N> = Translation2<N>;

        /// The angular inertia of a rigid body.
        pub type AngularInertia<N> = N;

        /// The principal angular inertia of a rigid body.
        pub type PrincipalAngularInertia<N> = N;

        /// A matrix that represent the cross product with a given vector.
        pub type CrossMatrix<N> = Vector2<N>;

        /// A vector with a dimension equal to the maximum number of degrees of freedom of a rigid body.
        pub type SpacialVector<N> = Vector3<N>;
    }
}

pub mod dynamics;
pub mod utils;