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    #[no_trace]
22    input: InputId,
23    #[no_trace]
24    #[ignore_malloc_size_of = "Complicated"]
25    joint: Joint,
26    hand_joint: XRHandJoint,
27}
28
29impl XRJointSpace {
30    pub(crate) fn new_inherited(
31        session: &XRSession,
32        input: InputId,
33        joint: Joint,
34        hand_joint: XRHandJoint,
35    ) -> XRJointSpace {
36        XRJointSpace {
37            xrspace: XRSpace::new_inherited(session),
38            input,
39            joint,
40            hand_joint,
41        }
42    }
43
44    pub(crate) fn new(
45        global: &GlobalScope,
46        session: &XRSession,
47        input: InputId,
48        joint: Joint,
49        hand_joint: XRHandJoint,
50        can_gc: CanGc,
51    ) -> DomRoot<XRJointSpace> {
52        reflect_dom_object(
53            Box::new(Self::new_inherited(session, input, joint, hand_joint)),
54            global,
55            can_gc,
56        )
57    }
58
59    pub(crate) fn space(&self) -> Space {
60        let base = BaseSpace::Joint(self.input, self.joint);
61        let offset = RigidTransform3D::identity();
62        Space { base, offset }
63    }
64
65    pub(crate) fn frame<'a>(&self, frame: &'a Frame) -> Option<&'a JointFrame> {
66        frame
67            .inputs
68            .iter()
69            .find(|i| i.id == self.input)
70            .and_then(|i| i.hand.as_ref())
71            .and_then(|h| h.get(self.joint))
72    }
73
74    pub(crate) fn get_pose(&self, frame: &Frame) -> Option<ApiPose> {
75        self.frame(frame).map(|f| f.pose).map(|t| t.cast_unit())
76    }
77}
78
79impl XRJointSpaceMethods<crate::DomTypeHolder> for XRJointSpace {
80    /// <https://www.w3.org/TR/webxr-hand-input-1/#xrjointspace-jointname>
81    fn JointName(&self) -> XRHandJoint {
82        self.hand_joint
83    }
84}