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 14:50:59 +02:00
parent 96d4d98811
commit adb3820305
5 changed files with 86 additions and 78 deletions

View File

@ -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,4 +1,4 @@
use super::glam::{DMat2, Mat2}; use super::glam::{DMat2, DQuat, DVec3, Mat2, Quat, Vec3};
use crate::{Complex, UnitComplex}; use crate::{Complex, UnitComplex};
impl From<UnitComplex<f32>> for Mat2 { impl From<UnitComplex<f32>> for Mat2 {

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;