script/dom/webxr/
xrjointspace.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use euclid::RigidTransform3D;
7use webxr_api::{BaseSpace, Frame, InputId, Joint, JointFrame, Space};
8
9use crate::dom::bindings::codegen::Bindings::XRHandBinding::XRHandJoint;
10use crate::dom::bindings::codegen::Bindings::XRJointSpaceBinding::XRJointSpaceMethods;
11use crate::dom::bindings::reflector::reflect_dom_object;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::globalscope::GlobalScope;
14use crate::dom::xrsession::{ApiPose, XRSession};
15use crate::dom::xrspace::XRSpace;
16use crate::script_runtime::CanGc;
17
18#[dom_struct]
19pub(crate) struct XRJointSpace {
20    xrspace: XRSpace,
21    #[ignore_malloc_size_of = "defined in rust-webxr"]
22    #[no_trace]
23    input: InputId,
24    #[ignore_malloc_size_of = "defined in rust-webxr"]
25    #[no_trace]
26    joint: Joint,
27    hand_joint: XRHandJoint,
28}
29
30impl XRJointSpace {
31    pub(crate) fn new_inherited(
32        session: &XRSession,
33        input: InputId,
34        joint: Joint,
35        hand_joint: XRHandJoint,
36    ) -> XRJointSpace {
37        XRJointSpace {
38            xrspace: XRSpace::new_inherited(session),
39            input,
40            joint,
41            hand_joint,
42        }
43    }
44
45    #[allow(unused)]
46    pub(crate) fn new(
47        global: &GlobalScope,
48        session: &XRSession,
49        input: InputId,
50        joint: Joint,
51        hand_joint: XRHandJoint,
52        can_gc: CanGc,
53    ) -> DomRoot<XRJointSpace> {
54        reflect_dom_object(
55            Box::new(Self::new_inherited(session, input, joint, hand_joint)),
56            global,
57            can_gc,
58        )
59    }
60
61    pub(crate) fn space(&self) -> Space {
62        let base = BaseSpace::Joint(self.input, self.joint);
63        let offset = RigidTransform3D::identity();
64        Space { base, offset }
65    }
66
67    pub(crate) fn frame<'a>(&self, frame: &'a Frame) -> Option<&'a JointFrame> {
68        frame
69            .inputs
70            .iter()
71            .find(|i| i.id == self.input)
72            .and_then(|i| i.hand.as_ref())
73            .and_then(|h| h.get(self.joint))
74    }
75
76    pub(crate) fn get_pose(&self, frame: &Frame) -> Option<ApiPose> {
77        self.frame(frame).map(|f| f.pose).map(|t| t.cast_unit())
78    }
79}
80
81impl XRJointSpaceMethods<crate::DomTypeHolder> for XRJointSpace {
82    /// <https://www.w3.org/TR/webxr-hand-input-1/#xrjointspace-jointname>
83    fn JointName(&self) -> XRHandJoint {
84        self.hand_joint
85    }
86}