Adding inverse transformation of a vector.

This commit is contained in:
Simon Rönnberg 2017-09-28 22:32:23 +02:00
parent 3543ac0274
commit 8343e40b48
2 changed files with 26 additions and 0 deletions

View file

@ -37,6 +37,11 @@ pub trait Transform<P: EuclideanSpace>: Sized {
/// Transform a vector using this transform. /// Transform a vector using this transform.
fn transform_vector(&self, vec: P::Diff) -> P::Diff; fn transform_vector(&self, vec: P::Diff) -> P::Diff;
/// Inverse transform a vector using this transform
fn inverse_transform_vector(&self, vec: P::Diff) -> Option<P::Diff> {
self.inverse_transform().and_then(|inverse| Some(inverse.transform_vector(vec)))
}
/// Transform a point using this transform. /// Transform a point using this transform.
fn transform_point(&self, point: P) -> P; fn transform_point(&self, point: P) -> P;
@ -93,6 +98,15 @@ impl<P: EuclideanSpace, R: Rotation<P>> Transform<P> for Decomposed<P::Diff, R>
self.rot.rotate_vector(vec * self.scale) self.rot.rotate_vector(vec * self.scale)
} }
#[inline]
fn inverse_transform_vector(&self, vec: P::Diff) -> Option<P::Diff> {
if ulps_eq!(self.scale, &P::Scalar::zero()) {
None
} else {
Some(self.rot.invert().rotate_vector(vec / self.scale))
}
}
#[inline] #[inline]
fn transform_point(&self, point: P) -> P { fn transform_point(&self, point: P) -> P {
self.rot.rotate_point(point * self.scale) + self.disp self.rot.rotate_point(point * self.scale) + self.disp

View file

@ -35,6 +35,18 @@ fn test_invert() {
assert_ulps_eq!(&v, &ti.transform_vector(vt)); assert_ulps_eq!(&v, &ti.transform_vector(vt));
} }
#[test]
fn test_inverse_vector() {
let v = Vector3::new(1.0f64, 2.0, 3.0);
let t = Decomposed {
scale: 1.5f64,
rot: Quaternion::new(0.5f64, 0.5, 0.5, 0.5),
disp: Vector3::new(6.0f64, -7.0, 8.0),
};
let vt = t.inverse_transform_vector(v).expect("Expected successful inversion");
assert_ulps_eq!(v, t.transform_vector(vt));
}
#[test] #[test]
fn test_look_at() { fn test_look_at() {
let eye = Point3::new(0.0f64, 0.0, -5.0); let eye = Point3::new(0.0f64, 0.0, -5.0);