From 51b3d2d4773574793427e24f1b69a92a95392dae Mon Sep 17 00:00:00 2001 From: Owen Sanchez Date: Thu, 13 Jul 2017 12:24:30 -0700 Subject: [PATCH] Add cast method to Quaternion --- src/quaternion.rs | 9 ++++++++- tests/quaternion.rs | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/quaternion.rs b/src/quaternion.rs index 599756d..b3edec5 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -18,7 +18,7 @@ use std::mem; use std::ops::*; use rand::{Rand, Rng}; -use num_traits::cast; +use num_traits::{NumCast, cast}; use structure::*; @@ -229,6 +229,13 @@ impl MetricSpace for Quaternion { } } +impl Quaternion { + /// Component-wise casting to another type. + pub fn cast(&self) -> Quaternion { + Quaternion::from_sv(NumCast::from(self.s).unwrap(), self.v.cast()) + } +} + #[cfg(not(feature = "simd"))] impl InnerSpace for Quaternion { #[inline] diff --git a/tests/quaternion.rs b/tests/quaternion.rs index 89bd531..315c5f3 100644 --- a/tests/quaternion.rs +++ b/tests/quaternion.rs @@ -331,3 +331,13 @@ mod rotate_between_vectors { assert_ulps_eq!(Quaternion::between_vectors(a, b), expected); } } + +mod cast { + use cgmath::*; + + #[test] + fn test_cast() { + assert_ulps_eq!(Quaternion::new(0.9f64, 1.5, 2.4, 7.6).cast(), + Quaternion::new(0.9f32, 1.5, 2.4, 7.6)); + } +}