rapier3d/dynamics/joint/multibody_joint/
multibody_link.rsuse std::ops::{Deref, DerefMut};
use crate::dynamics::{MultibodyJoint, RigidBodyHandle};
use crate::math::{Isometry, Real, Vector};
use crate::prelude::RigidBodyVelocity;
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug)]
pub struct MultibodyLink {
pub(crate) internal_id: usize,
pub(crate) assembly_id: usize,
pub(crate) parent_internal_id: usize,
pub(crate) rigid_body: RigidBodyHandle,
pub joint: MultibodyJoint,
pub(crate) local_to_world: Isometry<Real>,
pub(crate) local_to_parent: Isometry<Real>,
pub(crate) shift02: Vector<Real>,
pub(crate) shift23: Vector<Real>,
pub(crate) joint_velocity: RigidBodyVelocity,
}
impl MultibodyLink {
pub fn new(
rigid_body: RigidBodyHandle,
internal_id: usize,
assembly_id: usize,
parent_internal_id: usize,
joint: MultibodyJoint,
local_to_world: Isometry<Real>,
local_to_parent: Isometry<Real>,
) -> Self {
let joint_velocity = RigidBodyVelocity::zero();
MultibodyLink {
internal_id,
assembly_id,
parent_internal_id,
joint,
local_to_world,
local_to_parent,
shift02: na::zero(),
shift23: na::zero(),
joint_velocity,
rigid_body,
}
}
pub fn joint(&self) -> &MultibodyJoint {
&self.joint
}
pub fn rigid_body_handle(&self) -> RigidBodyHandle {
self.rigid_body
}
#[inline]
pub fn is_root(&self) -> bool {
self.internal_id == 0
}
#[inline]
pub fn link_id(&self) -> usize {
self.internal_id
}
#[inline]
pub fn parent_id(&self) -> Option<usize> {
if self.internal_id != 0 {
Some(self.parent_internal_id)
} else {
None
}
}
#[inline]
pub fn local_to_world(&self) -> &Isometry<Real> {
&self.local_to_world
}
#[inline]
pub fn local_to_parent(&self) -> &Isometry<Real> {
&self.local_to_parent
}
}
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub(crate) struct MultibodyLinkVec(pub Vec<MultibodyLink>);
impl MultibodyLinkVec {
#[inline]
pub fn get_mut_with_parent(&mut self, i: usize) -> (&mut MultibodyLink, &MultibodyLink) {
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)
}
}
}
impl Deref for MultibodyLinkVec {
type Target = Vec<MultibodyLink>;
#[inline]
fn deref(&self) -> &Vec<MultibodyLink> {
let MultibodyLinkVec(ref me) = *self;
me
}
}
impl DerefMut for MultibodyLinkVec {
#[inline]
fn deref_mut(&mut self) -> &mut Vec<MultibodyLink> {
let MultibodyLinkVec(ref mut me) = *self;
me
}
}