Skip to content
Permalink
Browse files

Move XRRigidTransform to using RigidTransform3D

  • Loading branch information...
Manishearth committed Apr 3, 2019
1 parent 67694dc commit feb3517ad5af7d62dc902031d6487ee593f37364
Showing with 23 additions and 38 deletions.
  1. +4 −2 components/script/dom/bindings/trace.rs
  2. +19 −36 components/script/dom/xrrigidtransform.rs
@@ -57,7 +57,8 @@ use devtools_traits::{CSSError, TimelineMarkerType, WorkerId};
use encoding_rs::{Decoder, Encoding};
use euclid::Length as EuclidLength;
use euclid::{
Point2D, Rect, Rotation3D, Transform2D, Transform3D, TypedScale, TypedSize2D, Vector2D,
Point2D, Rect, RigidTransform3D, Rotation3D, Transform2D, Transform3D, TypedScale, TypedSize2D,
Vector2D,
};
use html5ever::buffer_queue::BufferQueue;
use html5ever::{LocalName, Namespace, Prefix, QualName};
@@ -494,7 +495,8 @@ unsafe_no_jsmanaged_fields!(ResourceFetchTiming);
unsafe_no_jsmanaged_fields!(Timespec);
unsafe_no_jsmanaged_fields!(HTMLMediaElementFetchContext);
unsafe_no_jsmanaged_fields!(Rotation3D<f64>, Transform2D<f32>, Transform3D<f64>);
unsafe_no_jsmanaged_fields!(Point2D<f32>, Vector2D<f32>, Rect<Au>, Rect<f32>);
unsafe_no_jsmanaged_fields!(Point2D<f32>, Vector2D<f32>, Rect<Au>);
unsafe_no_jsmanaged_fields!(Rect<f32>, RigidTransform3D<f64>);

unsafe impl<'a> JSTraceable for &'a str {
#[inline]
@@ -13,25 +13,23 @@ 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 {
reflector_: Reflector,
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 {
fn new_inherited(
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()
}
}

0 comments on commit feb3517

Please sign in to comment.
You can’t perform that action at this time.