Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple version of the optional glam conversion #900

Merged
merged 2 commits into from
Jun 1, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Replace unchecked glam conversion by TryFrom or checks
sebcrozet committed Jun 1, 2021

Unverified

This user has not yet uploaded their public signing key.
commit 47a4f52e8d07903eecfeb11b2d6dd0ccf8de58ab
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
197 changes: 100 additions & 97 deletions src/third_party/glam/common/glam_isometry.rs
Original file line number Diff line number Diff line change
@@ -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<Isometry2<f32>> for Mat3 {
fn from(iso: Isometry2<f32>) -> Mat3 {
@@ -38,153 +39,155 @@ 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::new(0.0, 0.0, 1.0), iso.rotation.angle());
let rot = Quat::from_axis_angle(Vec3::Z, 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());
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<f32> {
fn from((tra, rot): (Vec3, Quat)) -> Self {
Isometry3::from_parts(tra.into(), rot.into())
}
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<(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<(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<(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<(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.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.into(), rot.to_axis_angle().1)
}
}

impl From<(Vec2, f32)> for Isometry2<f32> {
fn from((tra, rot): (Vec2, f32)) -> Self {
Isometry2::new([tra.x, tra.y].into(), rot)
}
impl From<(Vec2, f32)> for Isometry2<f32> {
fn from((tra, rot): (Vec2, f32)) -> Self {
Isometry2::new(tra.into(), rot)
}
}

impl From<(DVec2, f64)> for Isometry2<f64> {
fn from((tra, rot): (DVec2, f64)) -> Self {
Isometry2::new([tra.x, tra.y].into(), rot)
}
impl From<(DVec2, f64)> for Isometry2<f64> {
fn from((tra, rot): (DVec2, f64)) -> Self {
Isometry2::new(tra.into(), rot)
}
}

impl From<Quat> for Isometry3<f32> {
fn from(rot: Quat) -> Self {
Isometry3::from_parts(crate::one(), rot.into())
}
impl From<Quat> for Isometry3<f32> {
fn from(rot: Quat) -> Self {
Isometry3::from_parts(crate::one(), rot.into())
}
}

impl From<DQuat> for Isometry3<f64> {
fn from(rot: DQuat) -> Self {
Isometry3::from_parts(crate::one(), rot.into())
}
impl From<DQuat> for Isometry3<f64> {
fn from(rot: DQuat) -> Self {
Isometry3::from_parts(crate::one(), rot.into())
}
}

impl From<Quat> for Isometry2<f32> {
fn from(rot: Quat) -> Self {
Isometry2::new(crate::zero(), rot.to_axis_angle().1)
}
impl From<Quat> for Isometry2<f32> {
fn from(rot: Quat) -> Self {
Isometry2::new(crate::zero(), rot.to_axis_angle().1)
}
}

impl From<DQuat> for Isometry2<f64> {
fn from(rot: DQuat) -> Self {
Isometry2::new(crate::zero(), rot.to_axis_angle().1)
}
impl From<DQuat> for Isometry2<f64> {
fn from(rot: DQuat) -> Self {
Isometry2::new(crate::zero(), rot.to_axis_angle().1)
}
}

impl From<Vec3> for Isometry3<f32> {
fn from(tra: Vec3) -> Self {
Isometry3::from_parts(tra.into(), crate::one())
}
impl From<Vec3> for Isometry3<f32> {
fn from(tra: Vec3) -> Self {
Isometry3::from_parts(tra.into(), crate::one())
}
}

impl From<DVec3> for Isometry3<f64> {
fn from(tra: DVec3) -> Self {
Isometry3::from_parts(tra.into(), crate::one())
}
impl From<DVec3> for Isometry3<f64> {
fn from(tra: DVec3) -> Self {
Isometry3::from_parts(tra.into(), crate::one())
}
}

impl From<Vec2> for Isometry2<f32> {
fn from(tra: Vec2) -> Self {
Isometry2::new(tra.into(), crate::one())
}
impl From<Vec2> for Isometry2<f32> {
fn from(tra: Vec2) -> Self {
Isometry2::new(tra.into(), crate::one())
}
}

impl From<DVec2> for Isometry2<f64> {
fn from(tra: DVec2) -> Self {
Isometry2::new(tra.into(), crate::one())
}
impl From<DVec2> for Isometry2<f64> {
fn from(tra: DVec2) -> Self {
Isometry2::new(tra.into(), crate::one())
}
}

impl From<Vec3> for Isometry2<f32> {
fn from(tra: Vec3) -> Self {
Isometry2::new([tra.x, tra.y].into(), crate::one())
}
impl From<Vec3> for Isometry2<f32> {
fn from(tra: Vec3) -> Self {
Isometry2::new([tra.x, tra.y].into(), crate::one())
}
}

impl From<DVec3> for Isometry2<f64> {
fn from(tra: DVec3) -> Self {
Isometry2::new([tra.x, tra.y].into(), crate::one())
}
impl From<DVec3> for Isometry2<f64> {
fn from(tra: DVec3) -> Self {
Isometry2::new([tra.x, tra.y].into(), crate::one())
}
}

impl TryFrom<Mat3> for Isometry2<f32> {
type Error = ();

impl From<Mat3> for Isometry2<f32> {
fn from(mat3: Mat3) -> Isometry2<f32> {
crate::convert_unchecked(Matrix3::from(mat3))
}
fn try_from(mat3: Mat3) -> Result<Isometry2<f32>, Self::Error> {
crate::try_convert(Matrix3::from(mat3)).ok_or(())
}
}

impl TryFrom<Mat4> for Isometry3<f32> {
type Error = ();

impl From<Mat4> for Isometry3<f32> {
fn from(mat4: Mat4) -> Isometry3<f32> {
crate::convert_unchecked(Matrix4::from(mat4))
}
fn try_from(mat4: Mat4) -> Result<Isometry3<f32>, Self::Error> {
crate::try_convert(Matrix4::from(mat4)).ok_or(())
}
}

impl From<DMat3> for Isometry2<f64> {
fn from(mat3: DMat3) -> Isometry2<f64> {
crate::convert_unchecked(Matrix3::from(mat3))
}
impl TryFrom<DMat3> for Isometry2<f64> {
type Error = ();

fn try_from(mat3: DMat3) -> Result<Isometry2<f64>, Self::Error> {
crate::try_convert(Matrix3::from(mat3)).ok_or(())
}
}

impl TryFrom<DMat4> for Isometry3<f64> {
type Error = ();

impl From<DMat4> for Isometry3<f64> {
fn from(mat4: DMat4) -> Isometry3<f64> {
crate::convert_unchecked(Matrix4::from(mat4))
}
fn try_from(mat4: DMat4) -> Result<Isometry3<f64>, Self::Error> {
crate::try_convert(Matrix4::from(mat4)).ok_or(())
}
}
24 changes: 9 additions & 15 deletions src/third_party/glam/common/glam_quaternion.rs
Original file line number Diff line number Diff line change
@@ -43,22 +43,16 @@ impl From<UnitQuaternion<f64>> for DQuat {
}
}

#[cfg(feature = "convert-glam-unchecked")]
mod unchecked {
use super::super::glam::{DQuat, Quat};
use crate::{Quaternion, UnitQuaternion};

impl From<Quat> for UnitQuaternion<f32> {
#[inline]
fn from(e: Quat) -> UnitQuaternion<f32> {
UnitQuaternion::new_unchecked(Quaternion::from(e))
}
impl From<Quat> for UnitQuaternion<f32> {
#[inline]
fn from(e: Quat) -> UnitQuaternion<f32> {
UnitQuaternion::new_normalize(Quaternion::from(e))
}
}

impl From<DQuat> for UnitQuaternion<f64> {
#[inline]
fn from(e: DQuat) -> UnitQuaternion<f64> {
UnitQuaternion::new_unchecked(Quaternion::from(e))
}
impl From<DQuat> for UnitQuaternion<f64> {
#[inline]
fn from(e: DQuat) -> UnitQuaternion<f64> {
UnitQuaternion::new_normalize(Quaternion::from(e))
}
}
46 changes: 20 additions & 26 deletions src/third_party/glam/common/glam_rotation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::glam::{DMat2, DQuat, Mat2, Quat};
use crate::{Rotation2, Rotation3, UnitQuaternion};
use crate::{Rotation2, Rotation3, UnitComplex, UnitQuaternion};

impl From<Rotation2<f32>> for Mat2 {
#[inline]
@@ -29,36 +29,30 @@ impl From<Rotation3<f64>> for DQuat {
}
}

#[cfg(feature = "convert-glam-unchecked")]
mod unchecked {
use super::super::glam::{DMat2, DQuat, Mat2, Quat};
use crate::{Rotation2, Rotation3, UnitQuaternion};

impl From<Mat2> for Rotation2<f32> {
#[inline]
fn from(e: Mat2) -> Rotation2<f32> {
Rotation2::from_matrix_unchecked(e.into())
}
impl From<Mat2> for Rotation2<f32> {
#[inline]
fn from(e: Mat2) -> Rotation2<f32> {
UnitComplex::from(e).to_rotation_matrix()
}
}

impl From<DMat2> for Rotation2<f64> {
#[inline]
fn from(e: DMat2) -> Rotation2<f64> {
Rotation2::from_matrix_unchecked(e.into())
}
impl From<DMat2> for Rotation2<f64> {
#[inline]
fn from(e: DMat2) -> Rotation2<f64> {
UnitComplex::from(e).to_rotation_matrix()
}
}

impl From<Quat> for Rotation3<f32> {
#[inline]
fn from(e: Quat) -> Rotation3<f32> {
Rotation3::from(UnitQuaternion::from(e))
}
impl From<Quat> for Rotation3<f32> {
#[inline]
fn from(e: Quat) -> Rotation3<f32> {
Rotation3::from(UnitQuaternion::from(e))
}
}

impl From<DQuat> for Rotation3<f64> {
#[inline]
fn from(e: DQuat) -> Rotation3<f64> {
Rotation3::from(UnitQuaternion::from(e))
}
impl From<DQuat> for Rotation3<f64> {
#[inline]
fn from(e: DQuat) -> Rotation3<f64> {
Rotation3::from(UnitQuaternion::from(e))
}
}
43 changes: 21 additions & 22 deletions src/third_party/glam/common/glam_similarity.rs
Original file line number Diff line number Diff line change
@@ -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<Similarity2<f32>> for Mat3 {
fn from(iso: Similarity2<f32>) -> Mat3 {
@@ -23,32 +24,30 @@ impl From<Similarity3<f64>> 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<Mat3> for Similarity2<f32> {
fn from(mat3: Mat3) -> Similarity2<f32> {
crate::convert_unchecked(Matrix3::from(mat3))
}
impl TryFrom<Mat3> for Similarity2<f32> {
type Error = ();
fn try_from(mat3: Mat3) -> Result<Similarity2<f32>, ()> {
crate::try_convert(Matrix3::from(mat3)).ok_or(())
}
}

impl From<Mat4> for Similarity3<f32> {
fn from(mat4: Mat4) -> Similarity3<f32> {
crate::convert_unchecked(Matrix4::from(mat4))
}
impl TryFrom<Mat4> for Similarity3<f32> {
type Error = ();
fn try_from(mat4: Mat4) -> Result<Similarity3<f32>, ()> {
crate::try_convert(Matrix4::from(mat4)).ok_or(())
}
}

impl From<DMat3> for Similarity2<f64> {
fn from(mat3: DMat3) -> Similarity2<f64> {
crate::convert_unchecked(Matrix3::from(mat3))
}
impl TryFrom<DMat3> for Similarity2<f64> {
type Error = ();
fn try_from(mat3: DMat3) -> Result<Similarity2<f64>, ()> {
crate::try_convert(Matrix3::from(mat3)).ok_or(())
}
}

impl From<DMat4> for Similarity3<f64> {
fn from(mat4: DMat4) -> Similarity3<f64> {
crate::convert_unchecked(Matrix4::from(mat4))
}
impl TryFrom<DMat4> for Similarity3<f64> {
type Error = ();
fn try_from(mat4: DMat4) -> Result<Similarity3<f64>, ()> {
crate::try_convert(Matrix4::from(mat4)).ok_or(())
}
}
26 changes: 10 additions & 16 deletions src/third_party/glam/common/glam_unit_complex.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::glam::{DMat2, Mat2};
use crate::UnitComplex;
use crate::{Complex, Rotation2, UnitComplex};

impl From<UnitComplex<f32>> for Mat2 {
#[inline]
@@ -15,22 +15,16 @@ impl From<UnitComplex<f64>> for DMat2 {
}
}

#[cfg(feature = "convert-glam-unchecked")]
mod unchecked {
use super::super::glam::{DMat2, Mat2};
use crate::{Rotation2, UnitComplex};

impl From<Mat2> for UnitComplex<f32> {
#[inline]
fn from(e: Mat2) -> UnitComplex<f32> {
Rotation2::from_matrix_unchecked(e.into()).into()
}
impl From<Mat2> for UnitComplex<f32> {
#[inline]
fn from(e: Mat2) -> UnitComplex<f32> {
UnitComplex::new_normalize(Complex::new(e.x_axis.x, e.x_axis.y))
}
}

impl From<DMat2> for UnitComplex<f64> {
#[inline]
fn from(e: DMat2) -> UnitComplex<f64> {
Rotation2::from_matrix_unchecked(e.into()).into()
}
impl From<DMat2> for UnitComplex<f64> {
#[inline]
fn from(e: DMat2) -> UnitComplex<f64> {
UnitComplex::new_normalize(Complex::new(e.x_axis.x, e.x_axis.y))
}
}