From 4af979c55b37bb757616bc2db59f01feeecfb0e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Tue, 1 Jun 2021 10:27:32 +0200 Subject: [PATCH 1/2] Support multiple version of the optional glam conversion --- Cargo.toml | 13 +++- .../glam/{ => common}/glam_isometry.rs | 76 ++++++++++++++++++- .../glam/{ => common}/glam_matrix.rs | 6 +- .../glam/{ => common}/glam_point.rs | 4 +- .../glam/{ => common}/glam_quaternion.rs | 4 +- .../glam/{ => common}/glam_rotation.rs | 4 +- .../glam/{ => common}/glam_similarity.rs | 4 +- .../glam/{ => common}/glam_translation.rs | 2 +- .../glam/{ => common}/glam_unit_complex.rs | 4 +- src/third_party/glam/mod.rs | 14 ++-- src/third_party/glam/v013/mod.rs | 18 +++++ src/third_party/glam/v014/mod.rs | 18 +++++ src/third_party/glam/v015/mod.rs | 18 +++++ src/third_party/mod.rs | 1 - 14 files changed, 158 insertions(+), 28 deletions(-) rename src/third_party/glam/{ => common}/glam_isometry.rs (61%) rename src/third_party/glam/{ => common}/glam_matrix.rs (99%) rename src/third_party/glam/{ => common}/glam_point.rs (98%) rename src/third_party/glam/{ => common}/glam_quaternion.rs (95%) rename src/third_party/glam/{ => common}/glam_rotation.rs (93%) rename src/third_party/glam/{ => common}/glam_similarity.rs (93%) rename src/third_party/glam/{ => common}/glam_translation.rs (96%) rename src/third_party/glam/{ => common}/glam_unit_complex.rs (91%) create mode 100644 src/third_party/glam/v013/mod.rs create mode 100644 src/third_party/glam/v014/mod.rs create mode 100644 src/third_party/glam/v015/mod.rs diff --git a/Cargo.toml b/Cargo.toml index f1d65128..f879ae1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,9 +36,13 @@ macros = [ "nalgebra-macros" ] # Conversion convert-mint = [ "mint" ] -convert-glam = [ "glam" ] -convert-glam-unchecked = [ "convert-glam" ] # Enable edgy conversions like Mat4 -> Isometry3 convert-bytemuck = [ "bytemuck" ] +convert-glam013 = [ "glam013" ] +convert-glam013-unchecked = [ "convert-glam013" ] # Enable edgy conversions like Mat4 -> Isometry3 +convert-glam014 = [ "glam014" ] +convert-glam014-unchecked = [ "convert-glam014" ] # Enable edgy conversions like Mat4 -> Isometry3 +convert-glam015 = [ "glam015" ] +convert-glam015-unchecked = [ "convert-glam015" ] # Enable edgy conversions like Mat4 -> Isometry3 # Serialization ## To use serde in a #[no-std] environment, enable the @@ -78,13 +82,16 @@ serde = { version = "1.0", default-features = false, features = [ "deri abomonation = { version = "0.7", optional = true } rkyv = { version = "~0.6.4", default-features = false, features = ["const_generics"], optional = true } mint = { version = "0.5", optional = true } -glam = { version = "0.13", optional = true } quickcheck = { version = "1", optional = true } pest = { version = "2", optional = true } pest_derive = { version = "2", optional = true } bytemuck = { version = "1.5", optional = true } matrixcompare-core = { version = "0.1", optional = true } proptest = { version = "1", optional = true, default-features = false, features = ["std"] } +glam013 = { package = "glam", version = "0.13", optional = true } +glam014 = { package = "glam", version = "0.14", optional = true } +glam015 = { package = "glam", version = "0.15", optional = true } + [dev-dependencies] serde_json = "1.0" diff --git a/src/third_party/glam/glam_isometry.rs b/src/third_party/glam/common/glam_isometry.rs similarity index 61% rename from src/third_party/glam/glam_isometry.rs rename to src/third_party/glam/common/glam_isometry.rs index 34a321e5..3d2cbaba 100644 --- a/src/third_party/glam/glam_isometry.rs +++ b/src/third_party/glam/common/glam_isometry.rs @@ -1,5 +1,5 @@ +use super::glam::{DMat3, DMat4, DQuat, DVec3, Mat3, Mat4, Quat, Vec3}; use crate::{Isometry2, Isometry3}; -use glam::{DMat3, DMat4, DQuat, DVec3, Mat3, Mat4, Quat, Vec3}; impl From> for Mat3 { fn from(iso: Isometry2) -> Mat3 { @@ -53,8 +53,8 @@ impl From> for (DVec3, DQuat) { #[cfg(feature = "convert-glam-unchecked")] mod unchecked { + use super::super::glam::{DMat3, DMat4, DQuat, DVec2, DVec3, Mat3, Mat4, Quat, Vec2, Vec3}; use crate::{Isometry2, Isometry3, Matrix3, Matrix4}; - 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 { @@ -92,6 +92,78 @@ mod unchecked { } } + impl From<(Vec2, f32)> for Isometry2 { + fn from((tra, rot): (Vec2, f32)) -> Self { + Isometry2::new([tra.x, tra.y].into(), rot) + } + } + + impl From<(DVec2, f64)> for Isometry2 { + fn from((tra, rot): (DVec2, f64)) -> Self { + Isometry2::new([tra.x, tra.y].into(), rot) + } + } + + impl From for Isometry3 { + fn from(rot: Quat) -> Self { + Isometry3::from_parts(crate::one(), rot.into()) + } + } + + impl From for Isometry3 { + fn from(rot: DQuat) -> Self { + Isometry3::from_parts(crate::one(), rot.into()) + } + } + + impl From for Isometry2 { + fn from(rot: Quat) -> Self { + Isometry2::new(crate::zero(), rot.to_axis_angle().1) + } + } + + impl From for Isometry2 { + fn from(rot: DQuat) -> Self { + Isometry2::new(crate::zero(), rot.to_axis_angle().1) + } + } + + impl From for Isometry3 { + fn from(tra: Vec3) -> Self { + Isometry3::from_parts(tra.into(), crate::one()) + } + } + + impl From for Isometry3 { + fn from(tra: DVec3) -> Self { + Isometry3::from_parts(tra.into(), crate::one()) + } + } + + impl From for Isometry2 { + fn from(tra: Vec2) -> Self { + Isometry2::new(tra.into(), crate::one()) + } + } + + impl From for Isometry2 { + fn from(tra: DVec2) -> Self { + Isometry2::new(tra.into(), crate::one()) + } + } + + impl From for Isometry2 { + fn from(tra: Vec3) -> Self { + Isometry2::new([tra.x, tra.y].into(), crate::one()) + } + } + + impl From for Isometry2 { + fn from(tra: DVec3) -> Self { + Isometry2::new([tra.x, tra.y].into(), crate::one()) + } + } + impl From for Isometry2 { fn from(mat3: Mat3) -> Isometry2 { crate::convert_unchecked(Matrix3::from(mat3)) diff --git a/src/third_party/glam/glam_matrix.rs b/src/third_party/glam/common/glam_matrix.rs similarity index 99% rename from src/third_party/glam/glam_matrix.rs rename to src/third_party/glam/common/glam_matrix.rs index bbb2bd17..77b68b5e 100644 --- a/src/third_party/glam/glam_matrix.rs +++ b/src/third_party/glam/common/glam_matrix.rs @@ -1,9 +1,9 @@ -use crate::storage::Storage; -use crate::{Matrix, Matrix2, Matrix3, Matrix4, Vector, Vector2, Vector3, Vector4, U2, U3, U4}; -use glam::{ +use super::glam::{ BVec2, BVec3, BVec4, DMat2, DMat3, DMat4, DVec2, DVec3, DVec4, IVec2, IVec3, IVec4, Mat2, Mat3, Mat4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4, }; +use crate::storage::Storage; +use crate::{Matrix, Matrix2, Matrix3, Matrix4, Vector, Vector2, Vector3, Vector4, U2, U3, U4}; macro_rules! impl_vec_conversion( ($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => { diff --git a/src/third_party/glam/glam_point.rs b/src/third_party/glam/common/glam_point.rs similarity index 98% rename from src/third_party/glam/glam_point.rs rename to src/third_party/glam/common/glam_point.rs index 823dc806..b15a6c6d 100644 --- a/src/third_party/glam/glam_point.rs +++ b/src/third_party/glam/common/glam_point.rs @@ -1,8 +1,8 @@ -use crate::{Point2, Point3, Point4}; -use glam::{ +use super::glam::{ BVec2, BVec3, BVec4, DVec2, DVec3, DVec4, IVec2, IVec3, IVec4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4, }; +use crate::{Point2, Point3, Point4}; macro_rules! impl_point_conversion( ($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => { diff --git a/src/third_party/glam/glam_quaternion.rs b/src/third_party/glam/common/glam_quaternion.rs similarity index 95% rename from src/third_party/glam/glam_quaternion.rs rename to src/third_party/glam/common/glam_quaternion.rs index e57b7714..a622bab7 100644 --- a/src/third_party/glam/glam_quaternion.rs +++ b/src/third_party/glam/common/glam_quaternion.rs @@ -1,5 +1,5 @@ +use super::glam::{DQuat, Quat}; use crate::{Quaternion, UnitQuaternion}; -use glam::{DQuat, Quat}; impl From for Quaternion { #[inline] @@ -45,8 +45,8 @@ impl From> for DQuat { #[cfg(feature = "convert-glam-unchecked")] mod unchecked { + use super::super::glam::{DQuat, Quat}; use crate::{Quaternion, UnitQuaternion}; - use glam::{DQuat, Quat}; impl From for UnitQuaternion { #[inline] diff --git a/src/third_party/glam/glam_rotation.rs b/src/third_party/glam/common/glam_rotation.rs similarity index 93% rename from src/third_party/glam/glam_rotation.rs rename to src/third_party/glam/common/glam_rotation.rs index 2db9f50e..6ae8d809 100644 --- a/src/third_party/glam/glam_rotation.rs +++ b/src/third_party/glam/common/glam_rotation.rs @@ -1,5 +1,5 @@ +use super::glam::{DMat2, DQuat, Mat2, Quat}; use crate::{Rotation2, Rotation3, UnitQuaternion}; -use glam::{DMat2, DQuat, Mat2, Quat}; impl From> for Mat2 { #[inline] @@ -31,8 +31,8 @@ impl From> for DQuat { #[cfg(feature = "convert-glam-unchecked")] mod unchecked { + use super::super::glam::{DMat2, DQuat, Mat2, Quat}; use crate::{Rotation2, Rotation3, UnitQuaternion}; - use glam::{DMat2, DQuat, Mat2, Quat}; impl From for Rotation2 { #[inline] diff --git a/src/third_party/glam/glam_similarity.rs b/src/third_party/glam/common/glam_similarity.rs similarity index 93% rename from src/third_party/glam/glam_similarity.rs rename to src/third_party/glam/common/glam_similarity.rs index 92107074..12295502 100644 --- a/src/third_party/glam/glam_similarity.rs +++ b/src/third_party/glam/common/glam_similarity.rs @@ -1,5 +1,5 @@ +use super::glam::{DMat3, DMat4, Mat3, Mat4}; use crate::{Similarity2, Similarity3}; -use glam::{DMat3, DMat4, Mat3, Mat4}; impl From> for Mat3 { fn from(iso: Similarity2) -> Mat3 { @@ -25,8 +25,8 @@ impl From> for DMat4 { #[cfg(feature = "convert-glam-unchecked")] mod unchecked { + use super::super::glam::{DMat3, DMat4, Mat3, Mat4}; use crate::{Matrix3, Matrix4, Similarity2, Similarity3}; - use glam::{DMat3, DMat4, Mat3, Mat4}; impl From for Similarity2 { fn from(mat3: Mat3) -> Similarity2 { diff --git a/src/third_party/glam/glam_translation.rs b/src/third_party/glam/common/glam_translation.rs similarity index 96% rename from src/third_party/glam/glam_translation.rs rename to src/third_party/glam/common/glam_translation.rs index f92d3275..6dbbf602 100644 --- a/src/third_party/glam/glam_translation.rs +++ b/src/third_party/glam/common/glam_translation.rs @@ -1,5 +1,5 @@ +use super::glam::{DVec2, DVec3, DVec4, Vec2, Vec3, Vec3A, Vec4}; 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) => { diff --git a/src/third_party/glam/glam_unit_complex.rs b/src/third_party/glam/common/glam_unit_complex.rs similarity index 91% rename from src/third_party/glam/glam_unit_complex.rs rename to src/third_party/glam/common/glam_unit_complex.rs index c5d9cc6a..ac5e405c 100644 --- a/src/third_party/glam/glam_unit_complex.rs +++ b/src/third_party/glam/common/glam_unit_complex.rs @@ -1,5 +1,5 @@ +use super::glam::{DMat2, Mat2}; use crate::UnitComplex; -use glam::{DMat2, Mat2}; impl From> for Mat2 { #[inline] @@ -17,8 +17,8 @@ impl From> for DMat2 { #[cfg(feature = "convert-glam-unchecked")] mod unchecked { + use super::super::glam::{DMat2, Mat2}; use crate::{Rotation2, UnitComplex}; - use glam::{DMat2, Mat2}; impl From for UnitComplex { #[inline] diff --git a/src/third_party/glam/mod.rs b/src/third_party/glam/mod.rs index 20925923..a09e37ca 100644 --- a/src/third_party/glam/mod.rs +++ b/src/third_party/glam/mod.rs @@ -1,8 +1,6 @@ -mod glam_isometry; -mod glam_matrix; -mod glam_point; -mod glam_quaternion; -mod glam_rotation; -mod glam_similarity; -mod glam_translation; -mod glam_unit_complex; +#[cfg(feature = "glam013")] +mod v013; +#[cfg(feature = "glam014")] +mod v014; +#[cfg(feature = "glam015")] +mod v015; diff --git a/src/third_party/glam/v013/mod.rs b/src/third_party/glam/v013/mod.rs new file mode 100644 index 00000000..4787fb21 --- /dev/null +++ b/src/third_party/glam/v013/mod.rs @@ -0,0 +1,18 @@ +#[path = "../common/glam_isometry.rs"] +mod glam_isometry; +#[path = "../common/glam_matrix.rs"] +mod glam_matrix; +#[path = "../common/glam_point.rs"] +mod glam_point; +#[path = "../common/glam_quaternion.rs"] +mod glam_quaternion; +#[path = "../common/glam_rotation.rs"] +mod glam_rotation; +#[path = "../common/glam_similarity.rs"] +mod glam_similarity; +#[path = "../common/glam_translation.rs"] +mod glam_translation; +#[path = "../common/glam_unit_complex.rs"] +mod glam_unit_complex; + +pub(self) use glam013 as glam; diff --git a/src/third_party/glam/v014/mod.rs b/src/third_party/glam/v014/mod.rs new file mode 100644 index 00000000..3f9b8286 --- /dev/null +++ b/src/third_party/glam/v014/mod.rs @@ -0,0 +1,18 @@ +#[path = "../common/glam_isometry.rs"] +mod glam_isometry; +#[path = "../common/glam_matrix.rs"] +mod glam_matrix; +#[path = "../common/glam_point.rs"] +mod glam_point; +#[path = "../common/glam_quaternion.rs"] +mod glam_quaternion; +#[path = "../common/glam_rotation.rs"] +mod glam_rotation; +#[path = "../common/glam_similarity.rs"] +mod glam_similarity; +#[path = "../common/glam_translation.rs"] +mod glam_translation; +#[path = "../common/glam_unit_complex.rs"] +mod glam_unit_complex; + +pub(self) use glam014 as glam; diff --git a/src/third_party/glam/v015/mod.rs b/src/third_party/glam/v015/mod.rs new file mode 100644 index 00000000..83b79848 --- /dev/null +++ b/src/third_party/glam/v015/mod.rs @@ -0,0 +1,18 @@ +#[path = "../common/glam_isometry.rs"] +mod glam_isometry; +#[path = "../common/glam_matrix.rs"] +mod glam_matrix; +#[path = "../common/glam_point.rs"] +mod glam_point; +#[path = "../common/glam_quaternion.rs"] +mod glam_quaternion; +#[path = "../common/glam_rotation.rs"] +mod glam_rotation; +#[path = "../common/glam_similarity.rs"] +mod glam_similarity; +#[path = "../common/glam_translation.rs"] +mod glam_translation; +#[path = "../common/glam_unit_complex.rs"] +mod glam_unit_complex; + +pub(self) use glam015 as glam; diff --git a/src/third_party/mod.rs b/src/third_party/mod.rs index ce0fcaad..218c6879 100644 --- a/src/third_party/mod.rs +++ b/src/third_party/mod.rs @@ -1,6 +1,5 @@ #[cfg(feature = "alga")] mod alga; -#[cfg(feature = "glam")] mod glam; #[cfg(feature = "mint")] mod mint; From 47a4f52e8d07903eecfeb11b2d6dd0ccf8de58ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Tue, 1 Jun 2021 10:48:24 +0200 Subject: [PATCH 2/2] Replace unchecked glam conversion by TryFrom or checks --- Cargo.toml | 3 - src/third_party/glam/common/glam_isometry.rs | 281 +++++++++--------- .../glam/common/glam_quaternion.rs | 28 +- src/third_party/glam/common/glam_rotation.rs | 58 ++-- .../glam/common/glam_similarity.rs | 55 ++-- .../glam/common/glam_unit_complex.rs | 30 +- 6 files changed, 218 insertions(+), 237 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f879ae1f..012b9c80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,11 +38,8 @@ macros = [ "nalgebra-macros" ] convert-mint = [ "mint" ] convert-bytemuck = [ "bytemuck" ] convert-glam013 = [ "glam013" ] -convert-glam013-unchecked = [ "convert-glam013" ] # Enable edgy conversions like Mat4 -> Isometry3 convert-glam014 = [ "glam014" ] -convert-glam014-unchecked = [ "convert-glam014" ] # Enable edgy conversions like Mat4 -> Isometry3 convert-glam015 = [ "glam015" ] -convert-glam015-unchecked = [ "convert-glam015" ] # Enable edgy conversions like Mat4 -> Isometry3 # Serialization ## To use serde in a #[no-std] environment, enable the diff --git a/src/third_party/glam/common/glam_isometry.rs b/src/third_party/glam/common/glam_isometry.rs index 3d2cbaba..6bd970d2 100644 --- a/src/third_party/glam/common/glam_isometry.rs +++ b/src/third_party/glam/common/glam_isometry.rs @@ -1,5 +1,6 @@ -use super::glam::{DMat3, DMat4, DQuat, DVec3, Mat3, Mat4, Quat, Vec3}; -use crate::{Isometry2, Isometry3}; +use super::glam::{DMat3, DMat4, DQuat, DVec2, DVec3, Mat3, Mat4, Quat, Vec2, Vec3}; +use crate::{Isometry2, Isometry3, Matrix3, Matrix4}; +use std::convert::TryFrom; impl From> for Mat3 { fn from(iso: Isometry2) -> Mat3 { @@ -38,7 +39,7 @@ impl From> for (DVec3, DQuat) { 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()); + let rot = Quat::from_axis_angle(Vec3::Z, iso.rotation.angle()); (tra, rot) } } @@ -46,145 +47,147 @@ impl From> for (Vec3, Quat) { 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()); + let rot = DQuat::from_axis_angle(DVec3::Z, iso.rotation.angle()); (tra, rot) } } -#[cfg(feature = "convert-glam-unchecked")] -mod unchecked { - use super::super::glam::{DMat3, DMat4, DQuat, DVec2, DVec3, Mat3, Mat4, Quat, Vec2, Vec3}; - use crate::{Isometry2, Isometry3, Matrix3, Matrix4}; - - 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<(Vec2, f32)> for Isometry2 { - fn from((tra, rot): (Vec2, f32)) -> Self { - Isometry2::new([tra.x, tra.y].into(), rot) - } - } - - impl From<(DVec2, f64)> for Isometry2 { - fn from((tra, rot): (DVec2, f64)) -> Self { - Isometry2::new([tra.x, tra.y].into(), rot) - } - } - - impl From for Isometry3 { - fn from(rot: Quat) -> Self { - Isometry3::from_parts(crate::one(), rot.into()) - } - } - - impl From for Isometry3 { - fn from(rot: DQuat) -> Self { - Isometry3::from_parts(crate::one(), rot.into()) - } - } - - impl From for Isometry2 { - fn from(rot: Quat) -> Self { - Isometry2::new(crate::zero(), rot.to_axis_angle().1) - } - } - - impl From for Isometry2 { - fn from(rot: DQuat) -> Self { - Isometry2::new(crate::zero(), rot.to_axis_angle().1) - } - } - - impl From for Isometry3 { - fn from(tra: Vec3) -> Self { - Isometry3::from_parts(tra.into(), crate::one()) - } - } - - impl From for Isometry3 { - fn from(tra: DVec3) -> Self { - Isometry3::from_parts(tra.into(), crate::one()) - } - } - - impl From for Isometry2 { - fn from(tra: Vec2) -> Self { - Isometry2::new(tra.into(), crate::one()) - } - } - - impl From for Isometry2 { - fn from(tra: DVec2) -> Self { - Isometry2::new(tra.into(), crate::one()) - } - } - - impl From for Isometry2 { - fn from(tra: Vec3) -> Self { - Isometry2::new([tra.x, tra.y].into(), crate::one()) - } - } - - impl From for Isometry2 { - fn from(tra: DVec3) -> Self { - Isometry2::new([tra.x, tra.y].into(), crate::one()) - } - } - - impl From for Isometry2 { - fn from(mat3: Mat3) -> Isometry2 { - crate::convert_unchecked(Matrix3::from(mat3)) - } - } - - impl From for Isometry3 { - fn from(mat4: Mat4) -> Isometry3 { - crate::convert_unchecked(Matrix4::from(mat4)) - } - } - - impl From for Isometry2 { - fn from(mat3: DMat3) -> Isometry2 { - crate::convert_unchecked(Matrix3::from(mat3)) - } - } - - impl From for Isometry3 { - fn from(mat4: DMat4) -> Isometry3 { - crate::convert_unchecked(Matrix4::from(mat4)) - } +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.into(), rot.to_axis_angle().1) + } +} + +impl From<(DVec2, DQuat)> for Isometry2 { + fn from((tra, rot): (DVec2, DQuat)) -> Self { + Isometry2::new(tra.into(), rot.to_axis_angle().1) + } +} + +impl From<(Vec2, f32)> for Isometry2 { + fn from((tra, rot): (Vec2, f32)) -> Self { + Isometry2::new(tra.into(), rot) + } +} + +impl From<(DVec2, f64)> for Isometry2 { + fn from((tra, rot): (DVec2, f64)) -> Self { + Isometry2::new(tra.into(), rot) + } +} + +impl From for Isometry3 { + fn from(rot: Quat) -> Self { + Isometry3::from_parts(crate::one(), rot.into()) + } +} + +impl From for Isometry3 { + fn from(rot: DQuat) -> Self { + Isometry3::from_parts(crate::one(), rot.into()) + } +} + +impl From for Isometry2 { + fn from(rot: Quat) -> Self { + Isometry2::new(crate::zero(), rot.to_axis_angle().1) + } +} + +impl From for Isometry2 { + fn from(rot: DQuat) -> Self { + Isometry2::new(crate::zero(), rot.to_axis_angle().1) + } +} + +impl From for Isometry3 { + fn from(tra: Vec3) -> Self { + Isometry3::from_parts(tra.into(), crate::one()) + } +} + +impl From for Isometry3 { + fn from(tra: DVec3) -> Self { + Isometry3::from_parts(tra.into(), crate::one()) + } +} + +impl From for Isometry2 { + fn from(tra: Vec2) -> Self { + Isometry2::new(tra.into(), crate::one()) + } +} + +impl From for Isometry2 { + fn from(tra: DVec2) -> Self { + Isometry2::new(tra.into(), crate::one()) + } +} + +impl From for Isometry2 { + fn from(tra: Vec3) -> Self { + Isometry2::new([tra.x, tra.y].into(), crate::one()) + } +} + +impl From for Isometry2 { + fn from(tra: DVec3) -> Self { + Isometry2::new([tra.x, tra.y].into(), crate::one()) + } +} + +impl TryFrom for Isometry2 { + type Error = (); + + fn try_from(mat3: Mat3) -> Result, Self::Error> { + crate::try_convert(Matrix3::from(mat3)).ok_or(()) + } +} + +impl TryFrom for Isometry3 { + type Error = (); + + fn try_from(mat4: Mat4) -> Result, Self::Error> { + crate::try_convert(Matrix4::from(mat4)).ok_or(()) + } +} + +impl TryFrom for Isometry2 { + type Error = (); + + fn try_from(mat3: DMat3) -> Result, Self::Error> { + crate::try_convert(Matrix3::from(mat3)).ok_or(()) + } +} + +impl TryFrom for Isometry3 { + type Error = (); + + fn try_from(mat4: DMat4) -> Result, Self::Error> { + crate::try_convert(Matrix4::from(mat4)).ok_or(()) } } diff --git a/src/third_party/glam/common/glam_quaternion.rs b/src/third_party/glam/common/glam_quaternion.rs index a622bab7..e0432ead 100644 --- a/src/third_party/glam/common/glam_quaternion.rs +++ b/src/third_party/glam/common/glam_quaternion.rs @@ -43,22 +43,16 @@ impl From> for DQuat { } } -#[cfg(feature = "convert-glam-unchecked")] -mod unchecked { - use super::super::glam::{DQuat, Quat}; - use crate::{Quaternion, UnitQuaternion}; - - impl From for UnitQuaternion { - #[inline] - fn from(e: Quat) -> UnitQuaternion { - UnitQuaternion::new_unchecked(Quaternion::from(e)) - } - } - - impl From for UnitQuaternion { - #[inline] - fn from(e: DQuat) -> UnitQuaternion { - UnitQuaternion::new_unchecked(Quaternion::from(e)) - } +impl From for UnitQuaternion { + #[inline] + fn from(e: Quat) -> UnitQuaternion { + UnitQuaternion::new_normalize(Quaternion::from(e)) + } +} + +impl From for UnitQuaternion { + #[inline] + fn from(e: DQuat) -> UnitQuaternion { + UnitQuaternion::new_normalize(Quaternion::from(e)) } } diff --git a/src/third_party/glam/common/glam_rotation.rs b/src/third_party/glam/common/glam_rotation.rs index 6ae8d809..1e1fe8e1 100644 --- a/src/third_party/glam/common/glam_rotation.rs +++ b/src/third_party/glam/common/glam_rotation.rs @@ -1,5 +1,5 @@ use super::glam::{DMat2, DQuat, Mat2, Quat}; -use crate::{Rotation2, Rotation3, UnitQuaternion}; +use crate::{Rotation2, Rotation3, UnitComplex, UnitQuaternion}; impl From> for Mat2 { #[inline] @@ -29,36 +29,30 @@ impl From> for DQuat { } } -#[cfg(feature = "convert-glam-unchecked")] -mod unchecked { - use super::super::glam::{DMat2, DQuat, Mat2, Quat}; - use crate::{Rotation2, Rotation3, UnitQuaternion}; - - impl From for Rotation2 { - #[inline] - fn from(e: Mat2) -> Rotation2 { - Rotation2::from_matrix_unchecked(e.into()) - } - } - - impl From for Rotation2 { - #[inline] - fn from(e: DMat2) -> Rotation2 { - Rotation2::from_matrix_unchecked(e.into()) - } - } - - impl From for Rotation3 { - #[inline] - fn from(e: Quat) -> Rotation3 { - Rotation3::from(UnitQuaternion::from(e)) - } - } - - impl From for Rotation3 { - #[inline] - fn from(e: DQuat) -> Rotation3 { - Rotation3::from(UnitQuaternion::from(e)) - } +impl From for Rotation2 { + #[inline] + fn from(e: Mat2) -> Rotation2 { + UnitComplex::from(e).to_rotation_matrix() + } +} + +impl From for Rotation2 { + #[inline] + fn from(e: DMat2) -> Rotation2 { + UnitComplex::from(e).to_rotation_matrix() + } +} + +impl From for Rotation3 { + #[inline] + fn from(e: Quat) -> Rotation3 { + Rotation3::from(UnitQuaternion::from(e)) + } +} + +impl From for Rotation3 { + #[inline] + fn from(e: DQuat) -> Rotation3 { + Rotation3::from(UnitQuaternion::from(e)) } } diff --git a/src/third_party/glam/common/glam_similarity.rs b/src/third_party/glam/common/glam_similarity.rs index 12295502..f6679f08 100644 --- a/src/third_party/glam/common/glam_similarity.rs +++ b/src/third_party/glam/common/glam_similarity.rs @@ -1,5 +1,6 @@ use super::glam::{DMat3, DMat4, Mat3, Mat4}; -use crate::{Similarity2, Similarity3}; +use crate::{Matrix3, Matrix4, Similarity2, Similarity3}; +use std::convert::TryFrom; impl From> for Mat3 { fn from(iso: Similarity2) -> Mat3 { @@ -23,32 +24,30 @@ impl From> for DMat4 { } } -#[cfg(feature = "convert-glam-unchecked")] -mod unchecked { - use super::super::glam::{DMat3, DMat4, Mat3, Mat4}; - use crate::{Matrix3, Matrix4, Similarity2, Similarity3}; - - impl From for Similarity2 { - fn from(mat3: Mat3) -> Similarity2 { - crate::convert_unchecked(Matrix3::from(mat3)) - } - } - - impl From for Similarity3 { - fn from(mat4: Mat4) -> Similarity3 { - crate::convert_unchecked(Matrix4::from(mat4)) - } - } - - impl From for Similarity2 { - fn from(mat3: DMat3) -> Similarity2 { - crate::convert_unchecked(Matrix3::from(mat3)) - } - } - - impl From for Similarity3 { - fn from(mat4: DMat4) -> Similarity3 { - crate::convert_unchecked(Matrix4::from(mat4)) - } +impl TryFrom for Similarity2 { + type Error = (); + fn try_from(mat3: Mat3) -> Result, ()> { + crate::try_convert(Matrix3::from(mat3)).ok_or(()) + } +} + +impl TryFrom for Similarity3 { + type Error = (); + fn try_from(mat4: Mat4) -> Result, ()> { + crate::try_convert(Matrix4::from(mat4)).ok_or(()) + } +} + +impl TryFrom for Similarity2 { + type Error = (); + fn try_from(mat3: DMat3) -> Result, ()> { + crate::try_convert(Matrix3::from(mat3)).ok_or(()) + } +} + +impl TryFrom for Similarity3 { + type Error = (); + fn try_from(mat4: DMat4) -> Result, ()> { + crate::try_convert(Matrix4::from(mat4)).ok_or(()) } } diff --git a/src/third_party/glam/common/glam_unit_complex.rs b/src/third_party/glam/common/glam_unit_complex.rs index ac5e405c..67b6a040 100644 --- a/src/third_party/glam/common/glam_unit_complex.rs +++ b/src/third_party/glam/common/glam_unit_complex.rs @@ -1,5 +1,5 @@ use super::glam::{DMat2, Mat2}; -use crate::UnitComplex; +use crate::{Complex, Rotation2, UnitComplex}; impl From> for Mat2 { #[inline] @@ -15,22 +15,16 @@ impl From> for DMat2 { } } -#[cfg(feature = "convert-glam-unchecked")] -mod unchecked { - use super::super::glam::{DMat2, Mat2}; - use crate::{Rotation2, UnitComplex}; - - impl From for UnitComplex { - #[inline] - fn from(e: Mat2) -> UnitComplex { - Rotation2::from_matrix_unchecked(e.into()).into() - } - } - - impl From for UnitComplex { - #[inline] - fn from(e: DMat2) -> UnitComplex { - Rotation2::from_matrix_unchecked(e.into()).into() - } +impl From for UnitComplex { + #[inline] + fn from(e: Mat2) -> UnitComplex { + UnitComplex::new_normalize(Complex::new(e.x_axis.x, e.x_axis.y)) + } +} + +impl From for UnitComplex { + #[inline] + fn from(e: DMat2) -> UnitComplex { + UnitComplex::new_normalize(Complex::new(e.x_axis.x, e.x_axis.y)) } }