1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/XRBoundedReferenceSpace.h"
8 #include "mozilla/dom/XRRigidTransform.h"
9 #include "mozilla/dom/DOMPoint.h"
10 #include "VRDisplayClient.h"
12 namespace mozilla::dom
{
14 XRBoundedReferenceSpace::XRBoundedReferenceSpace(nsIGlobalObject
* aParent
,
16 XRNativeOrigin
* aNativeOrigin
)
17 : XRReferenceSpace(aParent
, aSession
, aNativeOrigin
,
18 XRReferenceSpaceType::Bounded_floor
) {}
20 JSObject
* XRBoundedReferenceSpace::WrapObject(
21 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
22 return XRBoundedReferenceSpace_Binding::Wrap(aCx
, this, aGivenProto
);
25 void XRBoundedReferenceSpace::GetBoundsGeometry(
26 nsTArray
<RefPtr
<DOMPointReadOnly
>>& result
) {
28 mSession
->GetDisplayClient()->GetDisplayInfo().GetStageSize();
29 if (size
.width
== 0 || size
.height
== 0) {
33 // https://immersive-web.github.io/webxr/#dom-xrboundedreferencespace-boundsgeometry
34 // bounds geometry must be premultiplied by the inverse of the origin offset.
35 gfx::PointDouble3D offset
= mNativeOrigin
->GetPosition();
37 const auto addPoint
= [&](const double x
, const double z
) {
38 RefPtr
<DOMPointReadOnly
> obj
= new DOMPointReadOnly(
39 GetParentObject(), x
- offset
.x
, 0.0f
, z
- offset
.z
, 1.0f
);
40 result
.EmplaceBack(obj
);
43 addPoint(-size
.width
* 0.5f
, size
.height
* 0.5f
);
44 addPoint(size
.width
* 0.5f
, size
.height
* 0.5f
);
45 addPoint(size
.width
* 0.5f
, -size
.height
* 0.5f
);
46 addPoint(-size
.width
* 0.5f
, -size
.height
* 0.5f
);
48 // TODO (Bug 1611526): Support WebXR bounded reference spaces
51 already_AddRefed
<XRReferenceSpace
>
52 XRBoundedReferenceSpace::GetOffsetReferenceSpace(
53 const XRRigidTransform
& aOriginOffset
) {
54 RefPtr
<XRBoundedReferenceSpace
> offsetReferenceSpace
=
55 new XRBoundedReferenceSpace(GetParentObject(), mSession
, mNativeOrigin
);
57 // https://immersive-web.github.io/webxr/#multiply-transforms
58 // An XRRigidTransform is essentially a rotation followed by a translation
59 gfx::QuaternionDouble otherOrientation
= aOriginOffset
.RawOrientation();
60 // The resulting rotation is the two combined
61 offsetReferenceSpace
->mOriginOffsetOrientation
=
62 mOriginOffsetOrientation
* otherOrientation
;
63 // We first apply the rotation of aOriginOffset to
64 // mOriginOffsetPosition offset, then translate by the offset of
66 offsetReferenceSpace
->mOriginOffsetPosition
=
67 otherOrientation
.RotatePoint(mOriginOffsetPosition
) +
68 aOriginOffset
.RawPosition();
70 return offsetReferenceSpace
.forget();
73 } // namespace mozilla::dom