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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use std::ops::{Deref, DerefMut};
use na::{DVectorSlice, DVectorSliceMut, Real};
use joint::Joint;
use math::{Force, Inertia, Isometry, Point, Vector, Velocity};
use object::{BodyHandle, Multibody};
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq)]
pub struct MultibodyLinkId {
pub(crate) internal_id: usize,
}
impl MultibodyLinkId {
pub(crate) fn new(internal_id: usize) -> Self {
MultibodyLinkId {
internal_id: internal_id,
}
}
pub fn ground() -> Self {
MultibodyLinkId {
internal_id: usize::max_value(),
}
}
pub fn is_root(&self) -> bool {
self.internal_id == 0
}
pub fn is_ground(&self) -> bool {
self.internal_id == usize::max_value()
}
}
pub(crate) struct MultibodyLink<N: Real> {
pub handle: BodyHandle,
pub assembly_id: usize,
pub impulse_id: usize,
pub is_leaf: bool,
pub parent: MultibodyLinkId,
pub dof: Box<Joint<N>>,
pub parent_shift: Vector<N>,
pub body_shift: Vector<N>,
pub parent_to_world: Isometry<N>,
pub local_to_world: Isometry<N>,
pub local_to_parent: Isometry<N>,
pub velocity_dot_wrt_joint: Velocity<N>,
pub velocity_wrt_joint: Velocity<N>,
pub velocity: Velocity<N>,
pub external_forces: Force<N>,
pub inertia: Inertia<N>,
pub com: Point<N>,
pub local_inertia: Inertia<N>,
pub local_com: Point<N>,
}
impl<N: Real> MultibodyLink<N> {
pub fn new(
handle: BodyHandle,
assembly_id: usize,
impulse_id: usize,
parent: MultibodyLinkId,
dof: Box<Joint<N>>,
parent_shift: Vector<N>,
body_shift: Vector<N>,
parent_to_world: Isometry<N>,
local_to_world: Isometry<N>,
local_to_parent: Isometry<N>,
local_inertia: Inertia<N>,
local_com: Point<N>,
) -> Self {
let is_leaf = true;
let velocity = Velocity::zero();
let velocity_dot_wrt_joint = Velocity::zero();
let velocity_wrt_joint = Velocity::zero();
let external_forces = Force::zero();
let inertia = local_inertia.transformed(&local_to_world);
let com = local_to_world * local_com;
MultibodyLink {
handle,
assembly_id,
impulse_id,
is_leaf,
parent,
dof,
parent_shift,
body_shift,
parent_to_world,
local_to_world,
local_to_parent,
velocity_dot_wrt_joint,
velocity_wrt_joint,
velocity,
external_forces,
local_inertia,
local_com,
inertia,
com,
}
}
pub fn center_of_mass(&self) -> Point<N> {
self.com
}
}
pub struct MultibodyLinkMut<'a, N: Real> {
id: MultibodyLinkId,
multibody: &'a mut Multibody<N>,
}
impl<'a, N: Real> MultibodyLinkMut<'a, N> {
pub fn new(id: MultibodyLinkId, multibody: &'a mut Multibody<N>) -> Self {
MultibodyLinkMut { id, multibody }
}
#[inline]
pub fn handle(&self) -> BodyHandle {
self.multibody.rbs()[self.id.internal_id].handle
}
pub fn as_ref<'b>(&'b self) -> MultibodyLinkRef<'b, N> {
MultibodyLinkRef::new(self.id, &*self.multibody)
}
#[inline]
pub(crate) fn id(&self) -> MultibodyLinkId {
self.id
}
#[inline]
pub fn joint_mut(&mut self) -> &mut Joint<N> {
&mut *self.multibody.rbs_mut()[self.id.internal_id].dof
}
#[inline]
pub fn apply_force(&mut self, force: &Force<N>) {
let rb = &mut self.multibody.rbs_mut()[self.id.internal_id];
rb.external_forces.linear += force.linear;
rb.external_forces.angular += force.angular;
}
#[inline]
pub fn joint_velocity_mut(&mut self) -> DVectorSliceMut<N> {
let ndofs = self.multibody.rbs()[self.id.internal_id].dof.ndofs();
let id = self.multibody.rbs()[self.id.internal_id].assembly_id;
let vels = self.multibody.generalized_velocity_slice_mut();
DVectorSliceMut::from_slice(&mut vels[id..id + ndofs], ndofs)
}
}
pub struct MultibodyLinkRef<'a, N: Real> {
id: MultibodyLinkId,
multibody: &'a Multibody<N>,
link: &'a MultibodyLink<N>,
}
impl<'a, N: Real> MultibodyLinkRef<'a, N> {
#[inline]
pub fn new(id: MultibodyLinkId, multibody: &'a Multibody<N>) -> Self {
let link = &multibody.rbs()[id.internal_id];
MultibodyLinkRef {
id,
multibody,
link,
}
}
#[inline]
pub fn handle(&self) -> BodyHandle {
self.link.handle
}
#[inline]
pub fn joint(&self) -> &Joint<N> {
&*self.link.dof
}
#[inline]
pub fn center_of_mass(&self) -> Point<N> {
self.link.center_of_mass()
}
#[inline]
pub fn velocity(&self) -> &Velocity<N> {
&self.link.velocity
}
#[inline]
pub fn joint_velocity(&self) -> DVectorSlice<N> {
let vels = self.multibody.generalized_velocity_slice();
let ndofs = self.link.dof.ndofs();
DVectorSlice::from_slice(
&vels[self.link.assembly_id..self.link.assembly_id + ndofs],
ndofs,
)
}
#[inline]
pub fn id(&self) -> MultibodyLinkId {
self.id
}
#[inline]
pub fn assembly_id(&self) -> usize {
self.link.assembly_id
}
#[inline]
pub fn impulse_id(&self) -> usize {
self.link.impulse_id
}
#[inline]
pub fn multibody(&self) -> &Multibody<N> {
self.multibody
}
pub fn inv_mass_mul_unit_joint_force(&self, dof_id: usize, force: N, out: &mut [N]) {
self.multibody
.inv_mass_mul_unit_joint_force(self.id, dof_id, force, out)
}
pub fn inv_mass_mul_joint_force(&self, force: DVectorSlice<N>, out: &mut [N]) {
self.multibody.inv_mass_mul_joint_force(self.id, force, out)
}
#[inline]
pub fn position(&self) -> Isometry<N> {
self.link.local_to_world
}
#[inline]
pub fn body_jacobian_mul_force(&self, force: &Force<N>, out: &mut [N]) {
self.multibody.body_jacobian_mul_force(self.id, force, out)
}
#[inline]
pub fn inv_mass_mul_generalized_forces(&self, out: &mut [N]) {
self.multibody.inv_mass_mul_generalized_forces(out)
}
#[inline]
pub fn inv_mass_mul_force(&self, force: &Force<N>, out: &mut [N]) {
self.multibody.inv_mass_mul_force(self.id, force, out)
}
#[inline]
pub fn local_inertia(&self) -> &Inertia<N> {
&self.link.local_inertia
}
#[inline]
pub fn inertia(&self) -> &Inertia<N> {
&self.link.inertia
}
}
pub(crate) struct MultibodyLinkVec<N: Real>(pub Vec<MultibodyLink<N>>);
impl<N: Real> MultibodyLinkVec<N> {
#[inline]
#[inline]
pub fn get_mut_with_parent(&mut self, i: usize) -> (&mut MultibodyLink<N>, &MultibodyLink<N>) {
let parent_id = self[i].parent.internal_id;
assert!(
parent_id != i,
"Internal error: circular rigid body dependency."
);
assert!(parent_id < self.len(), "Invalid parent index.");
unsafe {
let rb = &mut *(self.get_unchecked_mut(i) as *mut _);
let parent_rb = &*(self.get_unchecked(parent_id) as *const _);
(rb, parent_rb)
}
}
#[inline]
pub fn unwrap(self) -> Vec<MultibodyLink<N>> {
self.0
}
}
impl<N: Real> Deref for MultibodyLinkVec<N> {
type Target = Vec<MultibodyLink<N>>;
#[inline]
fn deref(&self) -> &Vec<MultibodyLink<N>> {
let MultibodyLinkVec(ref me) = *self;
me
}
}
impl<N: Real> DerefMut for MultibodyLinkVec<N> {
#[inline]
fn deref_mut(&mut self) -> &mut Vec<MultibodyLink<N>> {
let MultibodyLinkVec(ref mut me) = *self;
me
}
}