Skip to content

Commit

Permalink
Merge pull request #1103 from dimforge/additional-glam-conversion
Browse files Browse the repository at this point in the history
glam: add conversion from Vec2/3/4 to UnitVector2/3/4 + remove ambigous conversions
  • Loading branch information
sebcrozet committed Apr 28, 2022
2 parents 96d4d98 + c9bf6aa commit b621177
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 81 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Expand Up @@ -37,7 +37,6 @@ cuda = [ "cust_core", "simba/cuda" ]
# Conversion
convert-mint = [ "mint" ]
convert-bytemuck = [ "bytemuck" ]
convert-glam013 = [ "glam013" ]
convert-glam014 = [ "glam014" ]
convert-glam015 = [ "glam015" ]
convert-glam016 = [ "glam016" ]
Expand Down Expand Up @@ -88,7 +87,6 @@ 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 }
glam016 = { package = "glam", version = "0.16", optional = true }
Expand Down
1 change: 0 additions & 1 deletion nalgebra-glm/Cargo.toml
Expand Up @@ -26,7 +26,6 @@ cuda = [ "nalgebra/cuda" ]
# Conversion
convert-mint = [ "nalgebra/mint" ]
convert-bytemuck = [ "nalgebra/bytemuck" ]
convert-glam013 = [ "nalgebra/glam013" ]
convert-glam014 = [ "nalgebra/glam014" ]
convert-glam015 = [ "nalgebra/glam015" ]
convert-glam016 = [ "nalgebra/glam016" ]
Expand Down
66 changes: 9 additions & 57 deletions src/third_party/glam/common/glam_isometry.rs
@@ -1,5 +1,5 @@
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;

impl From<Isometry2<f32>> for Mat3 {
Expand Down Expand Up @@ -36,18 +36,18 @@ impl From<Isometry3<f64>> for (DVec3, DQuat) {
}
}

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::Z, iso.rotation.angle());
impl From<Isometry2<f32>> for (Vec2, f32) {
fn from(iso: Isometry2<f32>) -> (Vec2, f32) {
let tra = Vec2::new(iso.translation.x, iso.translation.y);
let rot = 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::Z, iso.rotation.angle());
impl From<Isometry2<f64>> for (DVec2, f64) {
fn from(iso: Isometry2<f64>) -> (DVec2, f64) {
let tra = DVec2::new(iso.translation.x, iso.translation.y);
let rot = iso.rotation.angle();
(tra, rot)
}
}
Expand All @@ -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> {
fn from((tra, rot): (Vec2, f32)) -> Self {
Isometry2::new(tra.into(), rot)
Expand All @@ -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> {
fn from(tra: Vec3) -> Self {
Isometry3::from_parts(tra.into(), UnitQuaternion::identity())
Expand All @@ -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> {
type Error = ();

Expand Down
78 changes: 77 additions & 1 deletion src/third_party/glam/common/glam_matrix.rs
Expand Up @@ -3,7 +3,11 @@ use super::glam::{
Mat4, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4,
};
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(
($N: ty, $Vec2: ty, $Vec3: ty, $Vec4: ty) => {
Expand Down Expand Up @@ -66,6 +70,63 @@ impl_vec_conversion!(i32, IVec2, IVec3, IVec4);
impl_vec_conversion!(u32, UVec2, UVec3, UVec4);
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> {
#[inline]
fn from(e: Vec3A) -> Vector3<f32> {
Expand All @@ -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> {
#[inline]
fn from(e: Mat2) -> Matrix2<f32> {
Expand Down
2 changes: 0 additions & 2 deletions src/third_party/glam/mod.rs
@@ -1,5 +1,3 @@
#[cfg(feature = "glam013")]
mod v013;
#[cfg(feature = "glam014")]
mod v014;
#[cfg(feature = "glam015")]
Expand Down
18 changes: 0 additions & 18 deletions src/third_party/glam/v013/mod.rs

This file was deleted.

0 comments on commit b621177

Please sign in to comment.