mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Move XRRigidTransform to using RigidTransform3D
This commit is contained in:
parent
67694dc90d
commit
feb3517ad5
2 changed files with 23 additions and 38 deletions
|
@ -13,7 +13,7 @@ use crate::dom::bindings::root::{Dom, DomRoot};
|
|||
use crate::dom::dompointreadonly::DOMPointReadOnly;
|
||||
use crate::dom::window::Window;
|
||||
use dom_struct::dom_struct;
|
||||
use euclid::{Rotation3D, Transform3D};
|
||||
use euclid::{RigidTransform3D, Rotation3D, Transform3D, Vector3D};
|
||||
|
||||
#[dom_struct]
|
||||
pub struct XRRigidTransform {
|
||||
|
@ -21,9 +21,7 @@ pub struct XRRigidTransform {
|
|||
position: Dom<DOMPointReadOnly>,
|
||||
orientation: Dom<DOMPointReadOnly>,
|
||||
#[ignore_malloc_size_of = "defined in euclid"]
|
||||
translate: Transform3D<f64>,
|
||||
#[ignore_malloc_size_of = "defined in euclid"]
|
||||
rotate: Rotation3D<f64>,
|
||||
transform: RigidTransform3D<f64>,
|
||||
}
|
||||
|
||||
impl XRRigidTransform {
|
||||
|
@ -31,7 +29,7 @@ impl XRRigidTransform {
|
|||
position: &DOMPointReadOnly,
|
||||
orientation: &DOMPointReadOnly,
|
||||
) -> XRRigidTransform {
|
||||
let translate = Transform3D::create_translation(
|
||||
let translate = Vector3D::new(
|
||||
position.X() as f64,
|
||||
position.Y() as f64,
|
||||
position.Z() as f64,
|
||||
|
@ -42,12 +40,12 @@ impl XRRigidTransform {
|
|||
orientation.Z() as f64,
|
||||
orientation.W() as f64,
|
||||
);
|
||||
let transform = RigidTransform3D::new(rotate, translate);
|
||||
XRRigidTransform {
|
||||
reflector_: Reflector::new(),
|
||||
position: Dom::from_ref(position),
|
||||
orientation: Dom::from_ref(orientation),
|
||||
translate,
|
||||
rotate,
|
||||
transform,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,37 +99,22 @@ impl XRRigidTransformMethods for XRRigidTransform {
|
|||
}
|
||||
// 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 inverse = self.transform.inverse();
|
||||
|
||||
let position = DOMPointReadOnly::new(
|
||||
&global,
|
||||
inverse.translation.x.into(),
|
||||
inverse.translation.y.into(),
|
||||
inverse.translation.z.into(),
|
||||
1.,
|
||||
);
|
||||
let orientation = DOMPointReadOnly::new(
|
||||
&global,
|
||||
r_1.i.into(),
|
||||
r_1.j.into(),
|
||||
r_1.k.into(),
|
||||
r_1.r.into(),
|
||||
inverse.rotation.i.into(),
|
||||
inverse.rotation.j.into(),
|
||||
inverse.rotation.k.into(),
|
||||
inverse.rotation.r.into(),
|
||||
);
|
||||
XRRigidTransform::new(global.as_window(), &position, &orientation)
|
||||
}
|
||||
|
@ -141,6 +124,6 @@ impl XRRigidTransform {
|
|||
pub fn matrix(&self) -> Transform3D<f64> {
|
||||
// Spec says the orientation applies first,
|
||||
// so post-multiply (?)
|
||||
self.translate.post_mul(&self.rotate.to_transform())
|
||||
self.transform.to_transform()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue