Merge pull request #1103 from dimforge/additional-glam-conversion

glam: add conversion from Vec2/3/4 to UnitVector2/3/4 + remove ambigous conversions
This commit is contained in:
Sébastien Crozet 2022-04-28 15:11:51 +02:00 committed by GitHub
commit b621177a74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 81 deletions

View File

@ -37,7 +37,6 @@ cuda = [ "cust_core", "simba/cuda" ]
# Conversion # Conversion
convert-mint = [ "mint" ] convert-mint = [ "mint" ]
convert-bytemuck = [ "bytemuck" ] convert-bytemuck = [ "bytemuck" ]
convert-glam013 = [ "glam013" ]
convert-glam014 = [ "glam014" ] convert-glam014 = [ "glam014" ]
convert-glam015 = [ "glam015" ] convert-glam015 = [ "glam015" ]
convert-glam016 = [ "glam016" ] convert-glam016 = [ "glam016" ]
@ -88,7 +87,6 @@ pest_derive = { version = "2", optional = true }
bytemuck = { version = "1.5", optional = true } bytemuck = { version = "1.5", optional = true }
matrixcompare-core = { version = "0.1", optional = true } matrixcompare-core = { version = "0.1", optional = true }
proptest = { version = "1", optional = true, default-features = false, features = ["std"] } 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 } glam014 = { package = "glam", version = "0.14", optional = true }
glam015 = { package = "glam", version = "0.15", optional = true } glam015 = { package = "glam", version = "0.15", optional = true }
glam016 = { package = "glam", version = "0.16", optional = true } glam016 = { package = "glam", version = "0.16", optional = true }

View File

@ -26,7 +26,6 @@ cuda = [ "nalgebra/cuda" ]
# Conversion # Conversion
convert-mint = [ "nalgebra/mint" ] convert-mint = [ "nalgebra/mint" ]
convert-bytemuck = [ "nalgebra/bytemuck" ] convert-bytemuck = [ "nalgebra/bytemuck" ]
convert-glam013 = [ "nalgebra/glam013" ]
convert-glam014 = [ "nalgebra/glam014" ] convert-glam014 = [ "nalgebra/glam014" ]
convert-glam015 = [ "nalgebra/glam015" ] convert-glam015 = [ "nalgebra/glam015" ]
convert-glam016 = [ "nalgebra/glam016" ] convert-glam016 = [ "nalgebra/glam016" ]

View File

@ -1,5 +1,5 @@
use super::glam::{DMat3, DMat4, DQuat, DVec2, DVec3, Mat3, Mat4, Quat, Vec2, Vec3}; use super::glam::{DMat3, DMat4, DQuat, DVec2, DVec3, Mat3, Mat4, Quat, Vec2, Vec3};
use crate::{Isometry2, Isometry3, Matrix3, Matrix4, Translation3, UnitQuaternion, Vector2}; use crate::{Isometry2, Isometry3, Matrix3, Matrix4, Translation3, UnitQuaternion};
use std::convert::TryFrom; use std::convert::TryFrom;
impl From<Isometry2<f32>> for Mat3 { impl From<Isometry2<f32>> for Mat3 {
@ -36,18 +36,18 @@ impl From<Isometry3<f64>> for (DVec3, DQuat) {
} }
} }
impl From<Isometry2<f32>> for (Vec3, Quat) { impl From<Isometry2<f32>> for (Vec2, f32) {
fn from(iso: Isometry2<f32>) -> (Vec3, Quat) { fn from(iso: Isometry2<f32>) -> (Vec2, f32) {
let tra = Vec3::new(iso.translation.x, iso.translation.y, 0.0); let tra = Vec2::new(iso.translation.x, iso.translation.y);
let rot = Quat::from_axis_angle(Vec3::Z, iso.rotation.angle()); let rot = iso.rotation.angle();
(tra, rot) (tra, rot)
} }
} }
impl From<Isometry2<f64>> for (DVec3, DQuat) { impl From<Isometry2<f64>> for (DVec2, f64) {
fn from(iso: Isometry2<f64>) -> (DVec3, DQuat) { fn from(iso: Isometry2<f64>) -> (DVec2, f64) {
let tra = DVec3::new(iso.translation.x, iso.translation.y, 0.0); let tra = DVec2::new(iso.translation.x, iso.translation.y);
let rot = DQuat::from_axis_angle(DVec3::Z, iso.rotation.angle()); let rot = iso.rotation.angle();
(tra, rot) (tra, rot)
} }
} }
@ -64,30 +64,6 @@ impl From<(DVec3, DQuat)> for Isometry3<f64> {
} }
} }
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.into(), rot.to_axis_angle().1)
}
}
impl From<(DVec2, DQuat)> for Isometry2<f64> {
fn from((tra, rot): (DVec2, DQuat)) -> Self {
Isometry2::new(tra.into(), rot.to_axis_angle().1)
}
}
impl From<(Vec2, f32)> for Isometry2<f32> { impl From<(Vec2, f32)> for Isometry2<f32> {
fn from((tra, rot): (Vec2, f32)) -> Self { fn from((tra, rot): (Vec2, f32)) -> Self {
Isometry2::new(tra.into(), rot) Isometry2::new(tra.into(), rot)
@ -112,18 +88,6 @@ impl From<DQuat> for Isometry3<f64> {
} }
} }
impl From<Quat> for Isometry2<f32> {
fn from(rot: Quat) -> Self {
Isometry2::new(Vector2::zeros(), rot.to_axis_angle().1)
}
}
impl From<DQuat> for Isometry2<f64> {
fn from(rot: DQuat) -> Self {
Isometry2::new(Vector2::zeros(), rot.to_axis_angle().1)
}
}
impl From<Vec3> for Isometry3<f32> { impl From<Vec3> for Isometry3<f32> {
fn from(tra: Vec3) -> Self { fn from(tra: Vec3) -> Self {
Isometry3::from_parts(tra.into(), UnitQuaternion::identity()) Isometry3::from_parts(tra.into(), UnitQuaternion::identity())
@ -148,18 +112,6 @@ impl From<DVec2> for Isometry2<f64> {
} }
} }
impl From<Vec3> for Isometry2<f32> {
fn from(tra: Vec3) -> Self {
Isometry2::new([tra.x, tra.y].into(), 0.0)
}
}
impl From<DVec3> for Isometry2<f64> {
fn from(tra: DVec3) -> Self {
Isometry2::new([tra.x, tra.y].into(), 0.0)
}
}
impl TryFrom<Mat3> for Isometry2<f32> { impl TryFrom<Mat3> for Isometry2<f32> {
type Error = (); type Error = ();

View File

@ -3,7 +3,11 @@ use super::glam::{
Mat4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4, Mat4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4,
}; };
use crate::storage::RawStorage; use crate::storage::RawStorage;
use crate::{Matrix, Matrix2, Matrix3, Matrix4, Vector, Vector2, Vector3, Vector4, U2, U3, U4}; use crate::{
Matrix, Matrix2, Matrix3, Matrix4, Unit, UnitVector2, UnitVector3, UnitVector4, Vector,
Vector2, Vector3, Vector4, U2, U3, U4,
};
use std::convert::TryFrom;
macro_rules! impl_vec_conversion( macro_rules! impl_vec_conversion(
($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => { ($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => {
@ -66,6 +70,63 @@ impl_vec_conversion!(i32, IVec2, IVec3, IVec4);
impl_vec_conversion!(u32, UVec2, UVec3, UVec4); impl_vec_conversion!(u32, UVec2, UVec3, UVec4);
impl_vec_conversion!(bool, BVec2, BVec3, BVec4); impl_vec_conversion!(bool, BVec2, BVec3, BVec4);
const ERR: &'static str = "Normalization failed.";
macro_rules! impl_unit_vec_conversion(
($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => {
impl TryFrom<$Vec2> for UnitVector2<$N> {
type Error = &'static str;
#[inline]
fn try_from(e: $Vec2) -> Result<Self, Self::Error> {
Unit::try_new(e.into(), 0.0).ok_or(ERR)
}
}
impl From<UnitVector2<$N>> for $Vec2
{
#[inline]
fn from(e: UnitVector2<$N>) -> $Vec2 {
e.into_inner().into()
}
}
impl TryFrom<$Vec3> for UnitVector3<$N> {
type Error = &'static str;
#[inline]
fn try_from(e: $Vec3) -> Result<Self, Self::Error> {
Unit::try_new(e.into(), 0.0).ok_or(ERR)
}
}
impl From<UnitVector3<$N>> for $Vec3
{
#[inline]
fn from(e: UnitVector3<$N>) -> $Vec3 {
e.into_inner().into()
}
}
impl TryFrom<$Vec4> for UnitVector4<$N> {
type Error = &'static str;
#[inline]
fn try_from(e: $Vec4) -> Result<Self, Self::Error> {
Unit::try_new(e.into(), 0.0).ok_or(ERR)
}
}
impl From<UnitVector4<$N>> for $Vec4
{
#[inline]
fn from(e: UnitVector4<$N>) -> $Vec4 {
e.into_inner().into()
}
}
}
);
impl_unit_vec_conversion!(f32, Vec2, Vec3, Vec4);
impl_unit_vec_conversion!(f64, DVec2, DVec3, DVec4);
impl From<Vec3A> for Vector3<f32> { impl From<Vec3A> for Vector3<f32> {
#[inline] #[inline]
fn from(e: Vec3A) -> Vector3<f32> { fn from(e: Vec3A) -> Vector3<f32> {
@ -83,6 +144,21 @@ where
} }
} }
impl TryFrom<Vec3A> for UnitVector3<f32> {
type Error = &'static str;
#[inline]
fn try_from(e: Vec3A) -> Result<Self, Self::Error> {
Unit::try_new(e.into(), 0.0).ok_or(ERR)
}
}
impl From<UnitVector3<f32>> for Vec3A {
#[inline]
fn from(e: UnitVector3<f32>) -> Vec3A {
e.into_inner().into()
}
}
impl From<Mat2> for Matrix2<f32> { impl From<Mat2> for Matrix2<f32> {
#[inline] #[inline]
fn from(e: Mat2) -> Matrix2<f32> { fn from(e: Mat2) -> Matrix2<f32> {

View File

@ -1,5 +1,3 @@
#[cfg(feature = "glam013")]
mod v013;
#[cfg(feature = "glam014")] #[cfg(feature = "glam014")]
mod v014; mod v014;
#[cfg(feature = "glam015")] #[cfg(feature = "glam015")]

View File

@ -1,18 +0,0 @@
#[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;