use crate::{DAffine3, DMat4, DPoint3, DVec3, UnitDQuat, UnitDVec3};
use auto_ops_det::impl_op_ex;
use core::ops;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct DIsometry3 {
pub rotation: UnitDQuat,
pub translation: DVec3,
}
impl DIsometry3 {
pub const IDENTITY: Self = Self {
rotation: UnitDQuat::IDENTITY,
translation: DVec3::ZERO,
};
#[inline]
pub fn from_array_unchecked(m: &[f64; 7]) -> Self {
Self {
rotation: UnitDQuat::from_slice_unchecked(&m[0..4]),
translation: DVec3::from_slice(&m[4..7]),
}
}
#[inline]
pub fn to_array(&self) -> [f64; 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: &[f64]) -> Self {
Self {
rotation: UnitDQuat::from_slice_unchecked(&slice[0..4]),
translation: DVec3::from_slice(&slice[4..7]),
}
}
#[inline]
pub fn write_to_slice(self, slice: &mut [f64]) {
self.rotation.write_to_slice(&mut slice[0..4]);
self.translation.write_to_slice(&mut slice[4..7]);
}
#[inline]
pub fn from_quat(rotation: UnitDQuat) -> Self {
Self {
rotation,
translation: DVec3::ZERO,
}
}
#[inline]
pub fn from_axis_angle(axis: UnitDVec3, angle: f64) -> Self {
Self {
rotation: UnitDQuat::from_axis_angle(axis, angle),
translation: DVec3::ZERO,
}
}
#[inline]
pub fn from_rotation_x(angle: f64) -> Self {
Self {
rotation: UnitDQuat::from_rotation_x(angle),
translation: DVec3::ZERO,
}
}
#[inline]
pub fn from_rotation_y(angle: f64) -> Self {
Self {
rotation: UnitDQuat::from_rotation_y(angle),
translation: DVec3::ZERO,
}
}
#[inline]
pub fn from_rotation_z(angle: f64) -> Self {
Self {
rotation: UnitDQuat::from_rotation_z(angle),
translation: DVec3::ZERO,
}
}
#[inline]
pub fn from_translation(translation: DVec3) -> Self {
#[allow(clippy::useless_conversion)]
Self {
rotation: UnitDQuat::IDENTITY,
translation: translation.into(),
}
}
#[inline]
pub fn from_rotation_translation(rotation: UnitDQuat, translation: DVec3) -> Self {
#[allow(clippy::useless_conversion)]
Self {
rotation,
translation: translation.into(),
}
}
#[inline]
pub fn to_rotation_translation(&self) -> (UnitDQuat, DVec3) {
(self.rotation, self.translation)
}
#[inline]
pub fn transform_point3(&self, rhs: DPoint3) -> DPoint3 {
DPoint3(self.rotation.mul_vec3(rhs.0) + self.translation)
}
pub fn inverse_transform_point3(&self, rhs: DPoint3) -> DPoint3 {
DPoint3(self.rotation.inverse() * (rhs.0 - self.translation))
}
#[inline]
pub fn transform_vector3(&self, rhs: DVec3) -> DVec3 {
self.rotation.mul_vec3(rhs)
}
#[inline]
pub fn is_finite(&self) -> bool {
self.translation.is_finite()
}
#[inline]
pub fn is_nan(&self) -> bool {
self.translation.is_nan()
}
#[inline]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> bool {
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 DIsometry3 {
#[inline]
fn default() -> Self {
Self::IDENTITY
}
}
impl PartialEq for DIsometry3 {
#[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 DIsometry3 {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt.debug_struct(stringify!(DIsometry3))
.field("UnitDQuat", &self.rotation)
.field("translation", &self.translation)
.finish()
}
}
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Display for DIsometry3 {
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 DIsometry3 {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::IDENTITY, |a, &b| a * b)
}
}
impl_op_ex!(*|a: &DIsometry3, b: &DIsometry3| -> DIsometry3 {
DIsometry3 {
rotation: a.rotation * b.rotation,
translation: a.rotation * b.translation + a.translation,
}
});
impl From<DIsometry3> for DAffine3 {
#[inline]
fn from(iso: DIsometry3) -> DAffine3 {
DAffine3::from_scale_rotation_translation(DVec3::ONE, iso.rotation, iso.translation)
}
}
impl From<&DIsometry3> for DAffine3 {
#[inline]
fn from(iso: &DIsometry3) -> DAffine3 {
DAffine3::from_scale_rotation_translation(DVec3::ONE, iso.rotation, iso.translation)
}
}
impl From<DIsometry3> for DMat4 {
#[inline]
fn from(iso: DIsometry3) -> DMat4 {
DMat4::from(DAffine3::from(iso))
}
}
impl From<&DIsometry3> for DMat4 {
#[inline]
fn from(iso: &DIsometry3) -> DMat4 {
DMat4::from(DAffine3::from(iso))
}
}
impl_op_ex!(*|a: &DIsometry3, b: &DAffine3| -> DAffine3 { DAffine3::from(a) * b });
impl_op_ex!(*|a: &DAffine3, b: &DIsometry3| -> DAffine3 { a * DAffine3::from(b) });
impl_op_ex!(*|a: &DIsometry3, b: &DVec3| -> DVec3 { a.rotation * b });
impl_op_ex!(*|a: &DIsometry3, b: &UnitDVec3| -> UnitDVec3 { a.rotation * b });
impl_op_ex!(*|a: &DIsometry3, b: &DPoint3| -> DPoint3 { a.transform_point3(*b) });
impl_op_ex!(*|a: &DIsometry3, b: &DMat4| -> DMat4 { DMat4::from(a) * b });
impl_op_ex!(*|a: &DMat4, b: &DIsometry3| -> DMat4 { a * DMat4::from(b) });