Add more conversion for glam types

Add Isometry3 <-> (Vec3, Quat)
Add Isometry2 <-> (Vec3, Quat)
Add Translation2/3/4 <-> Vec2/3/4
This commit is contained in:
Crozet Sébastien 2021-04-27 13:21:46 +02:00
parent 65b94ccb91
commit d59d438189
3 changed files with 132 additions and 2 deletions

View File

@ -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<Isometry2<f32>> for Mat3 {
fn from(iso: Isometry2<f32>) -> Mat3 {
@ -23,10 +23,74 @@ impl From<Isometry3<f64>> for DMat4 {
}
}
impl From<Isometry3<f32>> for (Vec3, Quat) {
fn from(iso: Isometry3<f32>) -> (Vec3, Quat) {
(iso.translation.into(), iso.rotation.into())
}
}
impl From<Isometry3<f64>> for (DVec3, DQuat) {
fn from(iso: Isometry3<f64>) -> (DVec3, DQuat) {
(iso.translation.into(), iso.rotation.into())
}
}
impl From<Isometry2<f32>> for (Vec3, Quat) {
fn from(iso: Isometry2<f32>) -> (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<Isometry2<f64>> for (DVec3, DQuat) {
fn from(iso: Isometry2<f64>) -> (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<f32> {
fn from((tra, rot): (Vec3, Quat)) -> Self {
Isometry3::from_parts(tra.into(), rot.into())
}
}
impl From<(DVec3, DQuat)> for Isometry3<f64> {
fn from((tra, rot): (DVec3, DQuat)) -> Self {
Isometry3::from_parts(tra.into(), rot.into())
}
}
impl From<(Vec3, Quat)> for Isometry2<f32> {
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<f64> {
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<f32> {
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<f64> {
fn from((tra, rot): (DVec2, DQuat)) -> Self {
Isometry2::new([tra.x, tra.y].into(), rot.to_axis_angle().1)
}
}
impl From<Mat3> for Isometry2<f32> {
fn from(mat3: Mat3) -> Isometry2<f32> {

View File

@ -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<Translation2<$N>> 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<Translation3<$N>> 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<Translation4<$N>> 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<Vec3A> for Translation3<f32> {
#[inline]
fn from(e: Vec3A) -> Translation3<f32> {
(*e.as_ref()).into()
}
}
impl From<Translation3<f32>> for Vec3A {
#[inline]
fn from(e: Translation3<f32>) -> Vec3A {
e.vector.into()
}
}

View File

@ -4,4 +4,5 @@ mod glam_point;
mod glam_quaternion;
mod glam_rotation;
mod glam_similarity;
mod glam_translation;
mod glam_unit_complex;