Add cast method to Quaternion

This commit is contained in:
Owen Sanchez 2017-07-13 12:24:30 -07:00
parent 17d8a223f7
commit 51b3d2d477
2 changed files with 18 additions and 1 deletions

View file

@ -18,7 +18,7 @@ use std::mem;
use std::ops::*; use std::ops::*;
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num_traits::cast; use num_traits::{NumCast, cast};
use structure::*; use structure::*;
@ -229,6 +229,13 @@ impl<S: BaseFloat> MetricSpace for Quaternion<S> {
} }
} }
impl<S: NumCast + Copy> Quaternion<S> {
/// Component-wise casting to another type.
pub fn cast<T: BaseFloat>(&self) -> Quaternion<T> {
Quaternion::from_sv(NumCast::from(self.s).unwrap(), self.v.cast())
}
}
#[cfg(not(feature = "simd"))] #[cfg(not(feature = "simd"))]
impl<S: BaseFloat> InnerSpace for Quaternion<S> { impl<S: BaseFloat> InnerSpace for Quaternion<S> {
#[inline] #[inline]

View file

@ -331,3 +331,13 @@ mod rotate_between_vectors {
assert_ulps_eq!(Quaternion::between_vectors(a, b), expected); 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));
}
}