Skip to content

Commit

Permalink
Add XRJointPose
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Apr 28, 2020
1 parent ad6da05 commit c30ad6c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/script/dom/mod.rs
Expand Up @@ -579,6 +579,7 @@ pub mod xrinputsource;
pub mod xrinputsourcearray;
pub mod xrinputsourceevent;
pub mod xrinputsourceschangeevent;
pub mod xrjointpose;
pub mod xrjointspace;
pub mod xrlayer;
pub mod xrmediabinding;
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/webidls/XRFrame.webidl
Expand Up @@ -10,5 +10,6 @@ interface XRFrame {

[Throws] XRViewerPose? getViewerPose(XRReferenceSpace referenceSpace);
[Throws] XRPose? getPose(XRSpace space, XRSpace relativeTo);
[Pref="dom.webxr.hands.enabled", Throws] XRJointPose? getJointPose(XRJointSpace space, XRSpace relativeTo);
sequence<XRHitTestResult> getHitTestResults(XRHitTestSource hitTestSource);
};
10 changes: 10 additions & 0 deletions components/script/dom/webidls/XRJointPose.webidl
@@ -0,0 +1,10 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

// https://github.com/immersive-web/webxr-hands-input/blob/master/explainer.md

[SecureContext, Exposed=Window, Pref="dom.webxr.hands.enabled"]
interface XRJointPose: XRPose {
readonly attribute float? radius;
};
34 changes: 34 additions & 0 deletions components/script/dom/xrframe.rs
Expand Up @@ -10,6 +10,8 @@ use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::globalscope::GlobalScope;
use crate::dom::xrhittestresult::XRHitTestResult;
use crate::dom::xrhittestsource::XRHitTestSource;
use crate::dom::xrjointpose::XRJointPose;
use crate::dom::xrjointspace::XRJointSpace;
use crate::dom::xrpose::XRPose;
use crate::dom::xrreferencespace::XRReferenceSpace;
use crate::dom::xrsession::{ApiPose, XRSession};
Expand Down Expand Up @@ -112,6 +114,38 @@ impl XRFrameMethods for XRFrame {
Ok(Some(XRPose::new(&self.global(), pose)))
}

/// https://immersive-web.github.io/webxr/#dom-xrframe-getpose
fn GetJointPose(
&self,
space: &XRJointSpace,
relative_to: &XRSpace,
) -> Result<Option<DomRoot<XRJointPose>>, Error> {
if self.session != space.upcast::<XRSpace>().session() ||
self.session != relative_to.session()
{
return Err(Error::InvalidState);
}
if !self.active.get() {
return Err(Error::InvalidState);
}
let joint_frame = if let Some(frame) = space.frame(&self.data) {
frame
} else {
return Ok(None);
};
let relative_to = if let Some(r) = self.get_pose(relative_to) {
r
} else {
return Ok(None);
};
let pose = relative_to.inverse().pre_transform(&joint_frame.pose);
Ok(Some(XRJointPose::new(
&self.global(),
pose.cast_unit(),
Some(joint_frame.radius),
)))
}

/// https://immersive-web.github.io/hit-test/#dom-xrframe-gethittestresults
fn GetHitTestResults(&self, source: &XRHitTestSource) -> Vec<DomRoot<XRHitTestResult>> {
self.data
Expand Down
48 changes: 48 additions & 0 deletions components/script/dom/xrjointpose.rs
@@ -0,0 +1,48 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::dom::bindings::codegen::Bindings::XRJointPoseBinding::XRJointPoseMethods;
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use crate::dom::xrpose::XRPose;
use crate::dom::xrrigidtransform::XRRigidTransform;
use crate::dom::xrsession::ApiRigidTransform;
use dom_struct::dom_struct;

#[dom_struct]
pub struct XRJointPose {
pose: XRPose,
radius: Option<f32>,
}

impl XRJointPose {
fn new_inherited(transform: &XRRigidTransform, radius: Option<f32>) -> XRJointPose {
XRJointPose {
pose: XRPose::new_inherited(transform),
radius,
}
}

#[allow(unsafe_code)]
pub fn new(
global: &GlobalScope,
pose: ApiRigidTransform,
radius: Option<f32>,
) -> DomRoot<XRJointPose> {
let transform = XRRigidTransform::new(global, pose);
reflect_dom_object(
Box::new(XRJointPose::new_inherited(&transform, radius)),
global,
)
}
}

impl XRJointPoseMethods for XRJointPose {
/// https://immersive-web.github.io/webxr/#dom-XRJointPose-views
fn GetRadius(&self) -> Option<Finite<f32>> {
self.radius.map(Finite::wrap)
}
}

0 comments on commit c30ad6c

Please sign in to comment.