use crate::{
nums::{bool32x4, f32x4},
Affine3x4, Isometry3, Mat4x4, Point3x4, UnitQuatx4, UnitVec3x4, Vec3x4,
};
use auto_ops_det::impl_op_ex;
use core::ops;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct Isometry3x4 {
pub rotation: UnitQuatx4,
pub translation: Vec3x4,
}
impl Isometry3x4 {
pub const IDENTITY: Self = Self {
rotation: UnitQuatx4::IDENTITY,
translation: Vec3x4::ZERO,
};
#[inline]
pub fn lane_select(cond: bool32x4, if_true: Self, if_false: Self) -> Self {
Self {
rotation: UnitQuatx4::lane_select(cond, if_true.rotation, if_false.rotation),
translation: Vec3x4::lane_select(cond, if_true.translation, if_false.translation),
}
}
#[inline]
pub fn splat_soa(m: Isometry3) -> Self {
Self {
rotation: UnitQuatx4::splat_soa(m.rotation),
translation: Vec3x4::splat_soa(m.translation),
}
}
#[inline]
pub fn extract_lane(&self, i: usize) -> Isometry3 {
Isometry3 {
rotation: self.rotation.extract_lane(i),
translation: self.translation.extract_lane(i),
}
}
#[inline]
pub fn replace_lane(&mut self, i: usize, m: Isometry3) {
self.rotation.replace_lane(i, m.rotation);
self.translation.replace_lane(i, m.translation);
}
#[inline]
pub fn split_soa(self) -> [Isometry3; 4] {
[
self.extract_lane(0),
self.extract_lane(1),
self.extract_lane(2),
self.extract_lane(3),
]
}
#[inline]
pub fn compose_soa(a: &[Isometry3; 4]) -> Isometry3x4 {
let mut v = Self::default();
v.replace_lane(0, a[0]);
v.replace_lane(1, a[1]);
v.replace_lane(2, a[2]);
v.replace_lane(3, a[3]);
v
}
#[inline]
pub fn from_array_unchecked(m: &[f32x4; 7]) -> Self {
Self {
rotation: UnitQuatx4::from_slice_unchecked(&m[0..4]),
translation: Vec3x4::from_slice(&m[4..7]),
}
}
#[inline]
pub fn to_array(&self) -> [f32x4; 7] {
[
self.rotation.x,
self.rotation.y,
self.rotation.z,
self.rotation.w,
self.translation.x,
self.translation.y,
self.translation.z,
]
}
#[inline]
pub fn from_slice_unchecked(slice: &[f32x4]) -> Self {
Self {
rotation: UnitQuatx4::from_slice_unchecked(&slice[0..4]),
translation: Vec3x4::from_slice(&slice[4..7]),
}
}
#[inline]
pub fn write_to_slice(self, slice: &mut [f32x4]) {
self.rotation.write_to_slice(&mut slice[0..4]);
self.translation.write_to_slice(&mut slice[4..7]);
}
#[inline]
pub fn from_quat(rotation: UnitQuatx4) -> Self {
Self {
rotation,
translation: Vec3x4::ZERO,
}
}
#[inline]
pub fn from_axis_angle(axis: UnitVec3x4, angle: f32x4) -> Self {
Self {
rotation: UnitQuatx4::from_axis_angle(axis, angle),
translation: Vec3x4::ZERO,
}
}
#[inline]
pub fn from_rotation_x(angle: f32x4) -> Self {
Self {
rotation: UnitQuatx4::from_rotation_x(angle),
translation: Vec3x4::ZERO,
}
}
#[inline]
pub fn from_rotation_y(angle: f32x4) -> Self {
Self {
rotation: UnitQuatx4::from_rotation_y(angle),
translation: Vec3x4::ZERO,
}
}
#[inline]
pub fn from_rotation_z(angle: f32x4) -> Self {
Self {
rotation: UnitQuatx4::from_rotation_z(angle),
translation: Vec3x4::ZERO,
}
}
#[inline]
pub fn from_translation(translation: Vec3x4) -> Self {
#[allow(clippy::useless_conversion)]
Self {
rotation: UnitQuatx4::IDENTITY,
translation: translation.into(),
}
}
#[inline]
pub fn from_rotation_translation(rotation: UnitQuatx4, translation: Vec3x4) -> Self {
#[allow(clippy::useless_conversion)]
Self {
rotation,
translation: translation.into(),
}
}
#[inline]
pub fn to_rotation_translation(&self) -> (UnitQuatx4, Vec3x4) {
(self.rotation, self.translation)
}
#[inline]
pub fn transform_point3(&self, rhs: Point3x4) -> Point3x4 {
Point3x4(self.rotation.mul_vec3(rhs.0) + self.translation)
}
pub fn inverse_transform_point3(&self, rhs: Point3x4) -> Point3x4 {
Point3x4(self.rotation.inverse() * (rhs.0 - self.translation))
}
#[inline]
pub fn transform_vector3(&self, rhs: Vec3x4) -> Vec3x4 {
self.rotation.mul_vec3(rhs)
}
#[inline]
pub fn is_finite(&self) -> bool32x4 {
self.translation.is_finite()
}
#[inline]
pub fn is_nan(&self) -> bool32x4 {
self.translation.is_nan()
}
#[inline]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32x4) -> bool32x4 {
self.rotation.abs_diff_eq(rhs.rotation, max_abs_diff)
& self.translation.abs_diff_eq(rhs.translation, max_abs_diff)
}
#[must_use]
#[inline]
pub fn inverse(&self) -> Self {
let rotation = self.rotation.inverse();
let translation = -(rotation * self.translation);
Self {
rotation,
translation,
}
}
}
impl Default for Isometry3x4 {
#[inline]
fn default() -> Self {
Self::IDENTITY
}
}
impl PartialEq for Isometry3x4 {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
self.rotation.eq(&rhs.rotation) && self.translation.eq(&rhs.translation)
}
}
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Debug for Isometry3x4 {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt.debug_struct(stringify!(Isometry3x4))
.field("UnitQuatx4", &self.rotation)
.field("translation", &self.translation)
.finish()
}
}
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Display for Isometry3x4 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "[{}, {}]", self.rotation, self.translation)
}
}
impl<'a> core::iter::Product<&'a Self> for Isometry3x4 {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::IDENTITY, |a, &b| a * b)
}
}
impl_op_ex!(*|a: &Isometry3x4, b: &Isometry3x4| -> Isometry3x4 {
Isometry3x4 {
rotation: a.rotation * b.rotation,
translation: a.rotation * b.translation + a.translation,
}
});
impl From<Isometry3x4> for Affine3x4 {
#[inline]
fn from(iso: Isometry3x4) -> Affine3x4 {
Affine3x4::from_scale_rotation_translation(Vec3x4::ONE, iso.rotation, iso.translation)
}
}
impl From<&Isometry3x4> for Affine3x4 {
#[inline]
fn from(iso: &Isometry3x4) -> Affine3x4 {
Affine3x4::from_scale_rotation_translation(Vec3x4::ONE, iso.rotation, iso.translation)
}
}
impl From<Isometry3x4> for Mat4x4 {
#[inline]
fn from(iso: Isometry3x4) -> Mat4x4 {
Mat4x4::from(Affine3x4::from(iso))
}
}
impl From<&Isometry3x4> for Mat4x4 {
#[inline]
fn from(iso: &Isometry3x4) -> Mat4x4 {
Mat4x4::from(Affine3x4::from(iso))
}
}
impl_op_ex!(*|a: &Isometry3x4, b: &Affine3x4| -> Affine3x4 { Affine3x4::from(a) * b });
impl_op_ex!(*|a: &Affine3x4, b: &Isometry3x4| -> Affine3x4 { a * Affine3x4::from(b) });
impl_op_ex!(*|a: &Isometry3x4, b: &Vec3x4| -> Vec3x4 { a.rotation * b });
impl_op_ex!(*|a: &Isometry3x4, b: &UnitVec3x4| -> UnitVec3x4 { a.rotation * b });
impl_op_ex!(*|a: &Isometry3x4, b: &Point3x4| -> Point3x4 { a.transform_point3(*b) });
impl_op_ex!(*|a: &Isometry3x4, b: &Mat4x4| -> Mat4x4 { Mat4x4::from(a) * b });
impl_op_ex!(*|a: &Mat4x4, b: &Isometry3x4| -> Mat4x4 { a * Mat4x4::from(b) });