Commit 837abe47 authored by Piotr Bialecki's avatar Piotr Bialecki Committed by Commit Bot

Make XRPlane return XRSpaces instead of handing out XRPoses

Currently, the pattern to obtain XRPoses in WebXR is to invoke
`XRFrame.getPose(space_to, space_from)` method - XRPlanes were
not following the pattern and returning XRPoses directly from
`XRPlane.getPose(space)`. This CL fixes that inconsistency -
planes will now return instances of XRSpace that can be used
to obtain the poses from a frame.

Change-Id: Ie656bcca50fe7118d5e2ffeb94e86e101b6e7f82
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1678440Reviewed-by: default avatarBill Orr <billorr@chromium.org>
Commit-Queue: Piotr Bialecki <bialpio@chromium.org>
Cr-Commit-Position: refs/heads/master@{#672724}
parent 3d18154f
......@@ -42,6 +42,8 @@ blink_modules_sources("xr") {
"xr_plane_detection_state.h",
"xr_plane_set.cc",
"xr_plane_set.h",
"xr_plane_space.cc",
"xr_plane_space.h",
"xr_pose.cc",
"xr_pose.h",
"xr_ray.cc",
......
......@@ -3,9 +3,11 @@
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/xr/xr_plane.h"
#include "third_party/blink/renderer/modules/xr/type_converters.h"
#include "third_party/blink/renderer/modules/xr/xr_pose.h"
#include "third_party/blink/renderer/modules/xr/xr_plane_space.h"
#include "third_party/blink/renderer/modules/xr/xr_reference_space.h"
#include "third_party/blink/renderer/modules/xr/xr_rigid_transform.h"
#include "third_party/blink/renderer/modules/xr/xr_session.h"
namespace blink {
......@@ -34,11 +36,16 @@ XRPlane::XRPlane(XRSession* session,
DVLOG(3) << __func__;
}
XRPose* XRPlane::getPose(XRReferenceSpace* reference_space) const {
std::unique_ptr<TransformationMatrix> viewer_pose =
reference_space->GetViewerPoseMatrix(pose_matrix_.get());
return MakeGarbageCollected<XRPose>(*viewer_pose,
session_->EmulatedPosition());
XRSpace* XRPlane::planeSpace() const {
if (!plane_space_) {
plane_space_ = MakeGarbageCollected<XRPlaneSpace>(session_, this);
}
return plane_space_;
}
TransformationMatrix XRPlane::poseMatrix() const {
return *pose_matrix_;
}
String XRPlane::orientation() const {
......@@ -82,6 +89,7 @@ void XRPlane::Update(const device::mojom::blink::XRPlaneDataPtr& plane_data,
void XRPlane::Trace(blink::Visitor* visitor) {
visitor->Trace(polygon_);
visitor->Trace(session_);
visitor->Trace(plane_space_);
ScriptWrappable::Trace(visitor);
}
......
......@@ -15,9 +15,8 @@
namespace blink {
class XRPose;
class XRSession;
class XRReferenceSpace;
class XRSpace;
class XRPlane : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO();
......@@ -34,8 +33,9 @@ class XRPlane : public ScriptWrappable {
const HeapVector<Member<DOMPointReadOnly>>& polygon,
double timestamp);
// Returns a pose expressed in passed in reference space.
XRPose* getPose(XRReferenceSpace* reference_space) const;
XRSpace* planeSpace() const;
TransformationMatrix poseMatrix() const;
String orientation() const;
HeapVector<Member<DOMPointReadOnly>> polygon() const;
......@@ -59,6 +59,9 @@ class XRPlane : public ScriptWrappable {
Member<XRSession> session_;
double last_changed_time_;
// Cached plane space - it will be created by `planeSpace()` if it's not set.
mutable Member<XRSpace> plane_space_;
};
} // namespace blink
......
......@@ -15,7 +15,7 @@ enum XRPlaneOrientation {
RuntimeEnabled=WebXRPlaneDetection
]
interface XRPlane {
XRPose getPose(XRReferenceSpace referenceSpace);
readonly attribute XRSpace planeSpace;
readonly attribute FrozenArray<DOMPointReadOnly> polygon;
readonly attribute XRPlaneOrientation? orientation;
readonly attribute DOMHighResTimeStamp lastChangedTime;
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/xr/xr_plane_space.h"
#include "third_party/blink/renderer/modules/xr/xr_plane.h"
#include "third_party/blink/renderer/platform/transforms/transformation_matrix.h"
namespace blink {
XRPlaneSpace::XRPlaneSpace(XRSession* session, const XRPlane* plane)
: XRSpace(session), plane_(plane) {}
std::unique_ptr<TransformationMatrix> XRPlaneSpace::GetTransformToMojoSpace() {
auto mojo_to_plane = plane_->poseMatrix();
if (!mojo_to_plane.IsInvertible()) {
return nullptr;
}
return std::make_unique<TransformationMatrix>(mojo_to_plane.Inverse());
}
void XRPlaneSpace::Trace(blink::Visitor* visitor) {
visitor->Trace(plane_);
XRSpace::Trace(visitor);
}
} // namespace blink
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_XR_XR_PLANE_SPACE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_XR_XR_PLANE_SPACE_H_
#include "third_party/blink/renderer/modules/xr/xr_space.h"
namespace blink {
class TransformationMatrix;
class XRPlane;
class XRSession;
class XRPlaneSpace : public XRSpace {
public:
explicit XRPlaneSpace(XRSession* session, const XRPlane* plane);
std::unique_ptr<TransformationMatrix> GetTransformToMojoSpace() override;
void Trace(blink::Visitor* visitor) override;
private:
Member<const XRPlane> plane_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_XR_XR_PLANE_SPACE_H_
......@@ -117,8 +117,8 @@ let calculateHitMatrix = function(ray_vector, plane_normal, point) {
}
// single plane hit test - doesn't take into account the plane's polygon
function hitTestPlane(ray, plane, frameOfReference) {
const plane_pose = plane.getPose(frameOfReference);
function hitTestPlane(frame, ray, plane, frameOfReference) {
const plane_pose = frame.getPose(plane.planeSpace, frameOfReference);
const plane_normal = transform_point_by_matrix(
plane_pose.transform.matrix, {x : 0, y : 1.0, z : 0, w : 0});
const plane_center = normalize_perspective(
......@@ -174,10 +174,12 @@ function hitTestPlane(ray, plane, frameOfReference) {
}
// multiple planes hit test
export function hitTest(ray, planes, frameOfReference) {
export function hitTest(frame, ray, frameOfReference) {
const planes = frame.worldInformation.detectedPlanes;
let hit_test_results = [];
planes.forEach(plane => {
let result = hitTestPlane(ray, plane, frameOfReference);
let result = hitTestPlane(frame, ray, plane, frameOfReference);
if(result) {
// throw away results with no intersection with plane
hit_test_results.push(result);
......
......@@ -347,7 +347,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}
}
function addPlaneToScene(plane, timestamp) {
function addPlaneToScene(frame, plane, timestamp) {
if(typeof XRPlane.counter == 'undefined') {
XRPlane.counter = 1;
XRPlane.colors = [
......@@ -389,7 +389,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
plane_node.addNode(planeFrameOfReference);
plane.scene_node = plane_node;
plane.scene_node.matrix = plane.getPose(xrRefSpace).transform.matrix;
plane.scene_node.matrix = frame.getPose(plane.planeSpace, xrRefSpace).transform.matrix;
plane.extended_polygon = extendPolygon(plane.polygon);
plane.extended_polygon_node = new PlaneNode({
......@@ -406,7 +406,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
// old plane that was updated in current frame
plane.scene_node.onPlaneChanged(plane.polygon);
plane.scene_node.matrix = plane.getPose(xrRefSpace).transform.matrix;
plane.scene_node.matrix = frame.getPose(plane.planeSpace, xrRefSpace).transform.matrix;
plane.extended_polygon = extendPolygon(plane.polygon);
plane.extended_polygon_node.onPlaneChanged(plane.extended_polygon);
}
......@@ -450,7 +450,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Process all currently detected planes.
detected_planes.forEach(plane => {
addPlaneToScene(plane, t);
addPlaneToScene(frame, plane, t);
});
// If requested, use the pose to cast a reticle into the scene using a
......@@ -469,7 +469,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
rayObject.matrix = ray.matrix.slice();
rayObject.visible = true;
const hitTestResults = hitTest(ray, frame.worldInformation.detectedPlanes, xrRefSpace);
const hitTestResults = hitTest(frame, ray, xrRefSpace);
const hitTestFiltered = filterHitTestResults(hitTestResults,
extendPlanesEnabled.checked,
false,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment