Add XRRigidTransform::Inverse

This commit is contained in:
Manish Goregaokar 2019-03-26 00:08:19 -07:00
parent 6fda2f28a6
commit 4f128e47e5
3 changed files with 42 additions and 2 deletions

View file

@ -46,7 +46,8 @@ impl XRRigidTransform {
reflector_: Reflector::new(),
position: Dom::from_ref(position),
orientation: Dom::from_ref(orientation),
translate, rotate
translate,
rotate,
}
}
@ -98,6 +99,42 @@ impl XRRigidTransformMethods for XRRigidTransform {
fn Orientation(&self) -> DomRoot<DOMPointReadOnly> {
DomRoot::from_ref(&self.orientation)
}
// https://immersive-web.github.io/webxr/#dom-xrrigidtransform-inverse
fn Inverse(&self) -> DomRoot<XRRigidTransform> {
// An XRRigidTransform is a rotation and a translation,
// i.e. T * R
//
// Its inverse is (T * R)^-1
// = R^-1 * T^-1
// = R^-1 * T^-1 * (R * R^-1)
// = (R^-1 * T^-1 * R) * R^-1
// = T' * R^-1
// = T' * R'
//
// (R^-1 * T^-1 * R) is a translation matrix, and R^-1 is a
// rotation matrix, so we can use these in the new rigid transform
let r_1 = self.rotate.inverse();
let t_1 = self
.translate
.inverse()
.expect("translation matrices should be invertible");
let t_p = r_1
.to_transform()
.post_mul(&t_1)
.post_mul(&self.rotate.to_transform());
let global = self.global();
let position =
DOMPointReadOnly::new(&global, t_p.m41.into(), t_p.m42.into(), t_p.m43.into(), 1.);
let orientation = DOMPointReadOnly::new(
&global,
r_1.i.into(),
r_1.j.into(),
r_1.k.into(),
r_1.r.into(),
);
XRRigidTransform::new(global.as_window(), &position, &orientation)
}
}
impl XRRigidTransform {