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
use std::ops::{Add, AddAssign, Mul, Neg};
use crate::algebra::{Force3, Velocity3};
use na::{self, Isometry3, Matrix3, Matrix6, RealField, U3};
#[derive(Clone, Copy, Debug)]
pub struct Inertia3<N: RealField + Copy> {
pub linear: N,
pub angular: Matrix3<N>,
}
impl<N: RealField + Copy> Inertia3<N> {
pub fn new(linear: N, angular: Matrix3<N>) -> Self {
Inertia3 { linear, angular }
}
pub fn new_with_angular_matrix(linear: N, angular: Matrix3<N>) -> Self {
Self::new(linear, angular)
}
pub fn mass(&self) -> N {
self.linear
}
pub fn inv_mass(&self) -> N {
if self.linear.is_zero() {
N::zero()
} else {
self.linear
}
}
pub fn zero() -> Self {
Inertia3::new(na::zero(), na::zero())
}
#[inline]
pub fn angular_matrix(&self) -> &Matrix3<N> {
&self.angular
}
pub fn to_matrix(&self) -> Matrix6<N> {
let mut res = Matrix6::zeros();
res.fixed_slice_mut::<3, 3>(3, 3).copy_from(&self.angular);
res.m11 = self.linear;
res.m22 = self.linear;
res.m33 = self.linear;
res
}
pub fn transformed(&self, i: &Isometry3<N>) -> Self {
let rot = i.rotation.to_rotation_matrix();
Inertia3::new(self.linear, rot * self.angular * rot.inverse())
}
#[cfg(not(feature = "improved_fixed_point_support"))]
pub fn inverse(&self) -> Self {
let inv_mass = if self.linear.is_zero() {
N::zero()
} else {
N::one() / self.linear
};
let inv_angular = self.angular.try_inverse().unwrap_or_else(na::zero);
Inertia3::new(inv_mass, inv_angular)
}
#[cfg(feature = "improved_fixed_point_support")]
pub fn inverse(&self) -> Self {
let inv_mass = if self.linear.is_zero() {
N::zero()
} else {
N::one() / self.linear
};
let diag = self.angular.diagonal();
let mut preconditioned = self.angular;
for i in 0..3 {
if !diag[i].is_zero() {
let mut row_i = preconditioned.row_mut(i);
row_i /= diag[i];
}
}
let mut inv_angular = preconditioned.try_inverse().unwrap_or_else(na::zero);
for i in 0..3 {
if !diag[i].is_zero() {
let mut col_i = inv_angular.column_mut(i);
col_i /= diag[i];
}
}
Inertia3::new(inv_mass, inv_angular)
}
}
impl<N: RealField + Copy> Neg for Inertia3<N> {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Self::new(-self.linear, -self.angular)
}
}
impl<N: RealField + Copy> Add<Inertia3<N>> for Inertia3<N> {
type Output = Inertia3<N>;
#[inline]
fn add(self, rhs: Inertia3<N>) -> Inertia3<N> {
Inertia3::new(self.linear + rhs.linear, self.angular + rhs.angular)
}
}
impl<N: RealField + Copy> AddAssign<Inertia3<N>> for Inertia3<N> {
#[inline]
fn add_assign(&mut self, rhs: Inertia3<N>) {
self.linear += rhs.linear;
self.angular += rhs.angular;
}
}
impl<N: RealField + Copy> Mul<Velocity3<N>> for Inertia3<N> {
type Output = Force3<N>;
#[inline]
fn mul(self, rhs: Velocity3<N>) -> Force3<N> {
Force3::new(rhs.linear * self.linear, self.angular * rhs.angular)
}
}
impl<N: RealField + Copy> Mul<Force3<N>> for Inertia3<N> {
type Output = Velocity3<N>;
#[inline]
fn mul(self, rhs: Force3<N>) -> Velocity3<N> {
Velocity3::new(rhs.linear * self.linear, self.angular * rhs.angular)
}
}