From d59d4381890e87d99899c8f6af06c8e53a71e805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Tue, 27 Apr 2021 13:21:46 +0200 Subject: [PATCH] Add more conversion for glam types Add Isometry3 <-> (Vec3, Quat) Add Isometry2 <-> (Vec3, Quat) Add Translation2/3/4 <-> Vec2/3/4 --- src/third_party/glam/glam_isometry.rs | 68 +++++++++++++++++++++++- src/third_party/glam/glam_translation.rs | 65 ++++++++++++++++++++++ src/third_party/glam/mod.rs | 1 + 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/third_party/glam/glam_translation.rs diff --git a/src/third_party/glam/glam_isometry.rs b/src/third_party/glam/glam_isometry.rs index 52c36e37..34a321e5 100644 --- a/src/third_party/glam/glam_isometry.rs +++ b/src/third_party/glam/glam_isometry.rs @@ -1,5 +1,5 @@ use crate::{Isometry2, Isometry3}; -use glam::{DMat3, DMat4, Mat3, Mat4}; +use glam::{DMat3, DMat4, DQuat, DVec3, Mat3, Mat4, Quat, Vec3}; impl From> for Mat3 { fn from(iso: Isometry2) -> Mat3 { @@ -23,10 +23,74 @@ impl From> for DMat4 { } } +impl From> for (Vec3, Quat) { + fn from(iso: Isometry3) -> (Vec3, Quat) { + (iso.translation.into(), iso.rotation.into()) + } +} + +impl From> for (DVec3, DQuat) { + fn from(iso: Isometry3) -> (DVec3, DQuat) { + (iso.translation.into(), iso.rotation.into()) + } +} + +impl From> for (Vec3, Quat) { + fn from(iso: Isometry2) -> (Vec3, Quat) { + let tra = Vec3::new(iso.translation.x, iso.translation.y, 0.0); + let rot = Quat::from_axis_angle(Vec3::new(0.0, 0.0, 1.0), iso.rotation.angle()); + (tra, rot) + } +} + +impl From> for (DVec3, DQuat) { + fn from(iso: Isometry2) -> (DVec3, DQuat) { + let tra = DVec3::new(iso.translation.x, iso.translation.y, 0.0); + let rot = DQuat::from_axis_angle(DVec3::new(0.0, 0.0, 1.0), iso.rotation.angle()); + (tra, rot) + } +} + #[cfg(feature = "convert-glam-unchecked")] mod unchecked { use crate::{Isometry2, Isometry3, Matrix3, Matrix4}; - use glam::{DMat3, DMat4, Mat3, Mat4}; + use glam::{DMat3, DMat4, DQuat, DVec2, DVec3, Mat3, Mat4, Quat, Vec2, Vec3}; + + impl From<(Vec3, Quat)> for Isometry3 { + fn from((tra, rot): (Vec3, Quat)) -> Self { + Isometry3::from_parts(tra.into(), rot.into()) + } + } + + impl From<(DVec3, DQuat)> for Isometry3 { + fn from((tra, rot): (DVec3, DQuat)) -> Self { + Isometry3::from_parts(tra.into(), rot.into()) + } + } + + impl From<(Vec3, Quat)> for Isometry2 { + fn from((tra, rot): (Vec3, Quat)) -> Self { + Isometry2::new([tra.x, tra.y].into(), rot.to_axis_angle().1) + } + } + + impl From<(DVec3, DQuat)> for Isometry2 { + fn from((tra, rot): (DVec3, DQuat)) -> Self { + Isometry2::new([tra.x, tra.y].into(), rot.to_axis_angle().1) + } + } + + impl From<(Vec2, Quat)> for Isometry2 { + fn from((tra, rot): (Vec2, Quat)) -> Self { + Isometry2::new([tra.x, tra.y].into(), rot.to_axis_angle().1) + } + } + + impl From<(DVec2, DQuat)> for Isometry2 { + fn from((tra, rot): (DVec2, DQuat)) -> Self { + Isometry2::new([tra.x, tra.y].into(), rot.to_axis_angle().1) + } + } impl From for Isometry2 { fn from(mat3: Mat3) -> Isometry2 { diff --git a/src/third_party/glam/glam_translation.rs b/src/third_party/glam/glam_translation.rs new file mode 100644 index 00000000..f92d3275 --- /dev/null +++ b/src/third_party/glam/glam_translation.rs @@ -0,0 +1,65 @@ +use crate::{Translation2, Translation3, Translation4}; +use glam::{DVec2, DVec3, DVec4, Vec2, Vec3, Vec3A, Vec4}; + +macro_rules! impl_translation_conversion( + ($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => { + impl From<$Vec2> for Translation2<$N> { + #[inline] + fn from(e: $Vec2) -> Translation2<$N> { + (*e.as_ref()).into() + } + } + + impl From> for $Vec2 { + #[inline] + fn from(e: Translation2<$N>) -> $Vec2 { + e.vector.into() + } + } + + impl From<$Vec3> for Translation3<$N> { + #[inline] + fn from(e: $Vec3) -> Translation3<$N> { + (*e.as_ref()).into() + } + } + + impl From> for $Vec3 { + #[inline] + fn from(e: Translation3<$N>) -> $Vec3 { + e.vector.into() + } + } + + impl From<$Vec4> for Translation4<$N> { + #[inline] + fn from(e: $Vec4) -> Translation4<$N> { + (*e.as_ref()).into() + } + } + + impl From> for $Vec4 { + #[inline] + fn from(e: Translation4<$N>) -> $Vec4 { + e.vector.into() + } + } + } +); + +impl_translation_conversion!(f32, Vec2, Vec3, Vec4); +impl_translation_conversion!(f64, DVec2, DVec3, DVec4); + +impl From for Translation3 { + #[inline] + fn from(e: Vec3A) -> Translation3 { + (*e.as_ref()).into() + } +} + +impl From> for Vec3A { + #[inline] + fn from(e: Translation3) -> Vec3A { + e.vector.into() + } +} diff --git a/src/third_party/glam/mod.rs b/src/third_party/glam/mod.rs index d4f8b643..20925923 100644 --- a/src/third_party/glam/mod.rs +++ b/src/third_party/glam/mod.rs @@ -4,4 +4,5 @@ mod glam_point; mod glam_quaternion; mod glam_rotation; mod glam_similarity; +mod glam_translation; mod glam_unit_complex;