use geo_nd::matrix;
use crate::{Mat4, Transformation};
#[derive(Debug)]
pub struct Bone {
pub transformation: Transformation,
pub(crate) ptb: Mat4,
pub(crate) mtb: Mat4,
pub matrix_index: usize,
}
impl Bone {
pub fn new(transformation: Transformation, matrix_index: usize) -> Self {
let ptb = [0.; 16];
let mtb = [0.; 16];
Self {
transformation,
matrix_index,
ptb,
mtb,
}
}
pub fn borrow_transformation(&self) -> &Transformation {
&self.transformation
}
pub fn set_transformation(mut self, transformation: Transformation) -> Self {
self.transformation = transformation;
self
}
pub fn derive_matrices(&mut self, is_root: bool, parent_mtb: &Mat4) -> &Mat4 {
self.ptb = self.transformation.mat4_inverse();
if is_root {
self.mtb = self.ptb;
} else {
self.mtb = matrix::multiply4(&self.ptb, parent_mtb);
}
&self.mtb
}
pub fn borrow_mtb(&self) -> &Mat4 {
&self.mtb
}
pub fn borrow_ptb(&self) -> &Mat4 {
&self.ptb
}
}
impl std::fmt::Display for Bone {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
f,
"Bone {} : {} : mtb={:?}",
self.matrix_index, self.transformation, self.mtb
)
}
}
impl indent_display::DefaultIndentedDisplay for Bone {}