summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/xr/type_converters.cc
blob: bfea7162f7e65e093fff2beb778338f0eba6f62d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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/type_converters.h"

#include "third_party/blink/renderer/platform/geometry/float_point_3d.h"

namespace mojo {

base::Optional<blink::XRPlane::Orientation>
TypeConverter<base::Optional<blink::XRPlane::Orientation>,
              device::mojom::blink::XRPlaneOrientation>::
    Convert(const device::mojom::blink::XRPlaneOrientation& orientation) {
  switch (orientation) {
    case device::mojom::blink::XRPlaneOrientation::UNKNOWN:
      return base::nullopt;
    case device::mojom::blink::XRPlaneOrientation::HORIZONTAL:
      return blink::XRPlane::Orientation::kHorizontal;
    case device::mojom::blink::XRPlaneOrientation::VERTICAL:
      return blink::XRPlane::Orientation::kVertical;
  }
}

blink::TransformationMatrix
TypeConverter<blink::TransformationMatrix, device::mojom::blink::VRPosePtr>::
    Convert(const device::mojom::blink::VRPosePtr& pose) {
  DCHECK(pose);

  blink::TransformationMatrix result;
  blink::TransformationMatrix::DecomposedType decomp = {};

  decomp.perspective_w = 1;
  decomp.scale_x = 1;
  decomp.scale_y = 1;
  decomp.scale_z = 1;

  if (pose->orientation) {
    // TODO(https://crbug.com/929841): Remove negation once the bug is fixed.
    gfx::Quaternion quat = pose->orientation->inverse();
    decomp.quaternion_x = quat.x();
    decomp.quaternion_y = quat.y();
    decomp.quaternion_z = quat.z();
    decomp.quaternion_w = quat.w();
  } else {
    decomp.quaternion_w = 1.0;
  }

  if (pose->position) {
    decomp.translate_x = pose->position->x();
    decomp.translate_y = pose->position->y();
    decomp.translate_z = pose->position->z();
  }

  result.Recompose(decomp);

  return result;
}

blink::TransformationMatrix
TypeConverter<blink::TransformationMatrix, device::mojom::blink::PosePtr>::
    Convert(const device::mojom::blink::PosePtr& pose) {
  DCHECK(pose);

  blink::TransformationMatrix result;
  blink::TransformationMatrix::DecomposedType decomp = {};

  decomp.perspective_w = 1;
  decomp.scale_x = 1;
  decomp.scale_y = 1;
  decomp.scale_z = 1;

  // TODO(https://crbug.com/929841): Remove negation once the bug is fixed.
  gfx::Quaternion quat = pose->orientation.inverse();
  decomp.quaternion_x = quat.x();
  decomp.quaternion_y = quat.y();
  decomp.quaternion_z = quat.z();
  decomp.quaternion_w = quat.w();

  decomp.translate_x = pose->position.x();
  decomp.translate_y = pose->position.y();
  decomp.translate_z = pose->position.z();

  result.Recompose(decomp);

  return result;
}

blink::HeapVector<blink::Member<blink::DOMPointReadOnly>>
TypeConverter<blink::HeapVector<blink::Member<blink::DOMPointReadOnly>>,
              Vector<device::mojom::blink::XRPlanePointDataPtr>>::
    Convert(const Vector<device::mojom::blink::XRPlanePointDataPtr>& vertices) {
  blink::HeapVector<blink::Member<blink::DOMPointReadOnly>> result;

  for (const auto& vertex_data : vertices) {
    result.push_back(blink::DOMPointReadOnly::Create(vertex_data->x, 0.0,
                                                     vertex_data->z, 1.0));
  }

  return result;
}

}  // namespace mojo