Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: chrome/browser/android/vr_shell/ui_element.cc

Issue 2834543006: Hook up insecure content warnings for http webVR presentation. (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/android/vr_shell/ui_element.h"
6
7 #include <limits>
8
9 #include "base/logging.h"
10 #include "base/time/time.h"
11 #include "chrome/browser/android/vr_shell/animation.h"
12 #include "chrome/browser/android/vr_shell/easing.h"
13 #include "device/vr/vr_math.h"
14
15 namespace vr_shell {
16
17 namespace {
18
19 bool GetRayPlaneDistance(const gfx::Point3F& ray_origin,
20 const gfx::Vector3dF& ray_vector,
21 const gfx::Point3F& plane_origin,
22 const gfx::Vector3dF& plane_normal,
23 float* distance) {
24 float denom = gfx::DotProduct(ray_vector, plane_normal);
25 if (denom == 0) {
26 return false;
27 }
28 gfx::Vector3dF rel = ray_origin - plane_origin;
29 *distance = -gfx::DotProduct(plane_normal, rel) / denom;
30 return true;
31 }
32
33 } // namespace
34
35 Transform::Transform() {
36 MakeIdentity();
37 }
38
39 void Transform::MakeIdentity() {
40 vr::SetIdentityM(&to_world);
41 }
42
43 void Transform::Rotate(const vr::Quatf& quat) {
44 // TODO(klausw): use specialized rotation code? Constructing the matrix
45 // via axis-angle quaternion is inefficient.
46 vr::Mat4f forward;
47 vr::QuatToMatrix(quat, &forward);
48 vr::MatrixMul(forward, to_world, &to_world);
49 }
50
51 void Transform::Rotate(const vr::RotationAxisAngle& axis_angle) {
52 Rotate(vr::QuatFromAxisAngle(axis_angle));
53 }
54
55 void Transform::Translate(const gfx::Vector3dF& translation) {
56 vr::TranslateM(to_world, translation, &to_world);
57 }
58
59 void Transform::Scale(const gfx::Vector3dF& scale) {
60 vr::ScaleM(to_world, scale, &to_world);
61 }
62
63 const vr::Mat4f& WorldRectangle::TransformMatrix() const {
64 return transform_.to_world;
65 }
66
67 gfx::Point3F WorldRectangle::GetCenter() const {
68 const gfx::Point3F kOrigin(0.0f, 0.0f, 0.0f);
69 return kOrigin + vr::GetTranslation(transform_.to_world);
70 }
71
72 gfx::PointF WorldRectangle::GetUnitRectangleCoordinates(
73 const gfx::Point3F& world_point) {
74 // TODO(acondor): Simplify the math in this function.
75 const vr::Mat4f& transform = transform_.to_world;
76 gfx::Vector3dF origin =
77 vr::MatrixVectorMul(transform, gfx::Vector3dF(0, 0, 0));
78 gfx::Vector3dF x_axis =
79 vr::MatrixVectorMul(transform, gfx::Vector3dF(1, 0, 0));
80 gfx::Vector3dF y_axis =
81 vr::MatrixVectorMul(transform, gfx::Vector3dF(0, 1, 0));
82 x_axis.Subtract(origin);
83 y_axis.Subtract(origin);
84 gfx::Point3F point = world_point - origin;
85 gfx::Vector3dF v_point(point.x(), point.y(), point.z());
86
87 float x = gfx::DotProduct(v_point, x_axis) / gfx::DotProduct(x_axis, x_axis);
88 float y = gfx::DotProduct(v_point, y_axis) / gfx::DotProduct(y_axis, y_axis);
89 return gfx::PointF(x, y);
90 }
91
92 gfx::Vector3dF WorldRectangle::GetNormal() const {
93 const gfx::Vector3dF kNormalOrig = {0.0f, 0.0f, -1.0f};
94 return vr::MatrixVectorRotate(transform_.to_world, kNormalOrig);
95 }
96
97 bool WorldRectangle::GetRayDistance(const gfx::Point3F& ray_origin,
98 const gfx::Vector3dF& ray_vector,
99 float* distance) const {
100 return GetRayPlaneDistance(ray_origin, ray_vector, GetCenter(), GetNormal(),
101 distance);
102 }
103
104 UiElement::UiElement() = default;
105
106 UiElement::~UiElement() = default;
107
108 void UiElement::Animate(const base::TimeTicks& time) {
109 for (auto& it : animations) {
110 Animation& animation = *it;
111 if (time < animation.start)
112 continue;
113
114 // If |from| is not specified, start at the current values.
115 if (animation.from.size() == 0) {
116 switch (animation.property) {
117 case Animation::SIZE:
118 animation.from.push_back(size.x());
119 animation.from.push_back(size.y());
120 break;
121 case Animation::SCALE:
122 animation.from.push_back(scale.x());
123 animation.from.push_back(scale.y());
124 animation.from.push_back(scale.z());
125 break;
126 case Animation::ROTATION:
127 animation.from.push_back(rotation.x);
128 animation.from.push_back(rotation.y);
129 animation.from.push_back(rotation.z);
130 animation.from.push_back(rotation.angle);
131 break;
132 case Animation::TRANSLATION:
133 animation.from.push_back(translation.x());
134 animation.from.push_back(translation.y());
135 animation.from.push_back(translation.z());
136 break;
137 case Animation::OPACITY:
138 animation.from.push_back(opacity);
139 break;
140 }
141 }
142 CHECK_EQ(animation.from.size(), animation.to.size());
143
144 std::vector<float> values(animation.from.size());
145 for (std::size_t i = 0; i < animation.from.size(); ++i) {
146 if (animation.to[i] == animation.from[i] ||
147 time >= (animation.start + animation.duration)) {
148 values[i] = animation.to[i];
149 continue;
150 }
151 double value = animation.easing->CalculateValue(
152 (time - animation.start).InMillisecondsF() /
153 animation.duration.InMillisecondsF());
154 values[i] =
155 animation.from[i] + (value * (animation.to[i] - animation.from[i]));
156 }
157 switch (animation.property) {
158 case Animation::SIZE:
159 CHECK_EQ(animation.from.size(), 2u);
160 size.set_x(values[0]);
161 size.set_y(values[1]);
162 break;
163 case Animation::SCALE:
164 CHECK_EQ(animation.from.size(), 3u);
165 scale = {values[0], values[1], values[2]};
166 break;
167 case Animation::ROTATION:
168 CHECK_EQ(animation.from.size(), 4u);
169 rotation.x = values[0];
170 rotation.y = values[1];
171 rotation.z = values[2];
172 rotation.angle = values[3];
173 break;
174 case Animation::TRANSLATION:
175 CHECK_EQ(animation.from.size(), 3u);
176 translation = {values[0], values[1], values[2]};
177 break;
178 case Animation::OPACITY:
179 CHECK_EQ(animation.from.size(), 1u);
180 opacity = values[0];
181 break;
182 }
183 }
184 for (auto it = animations.begin(); it != animations.end();) {
185 const Animation& animation = **it;
186 if (time >= (animation.start + animation.duration)) {
187 it = animations.erase(it);
188 } else {
189 ++it;
190 }
191 }
192 }
193
194 bool UiElement::IsVisible() const {
195 return visible && computed_opacity > 0.0f;
196 }
197
198 bool UiElement::IsHitTestable() const {
199 return IsVisible() && hit_testable;
200 }
201
202 } // namespace vr_shell
OLDNEW
« no previous file with comments | « chrome/browser/android/vr_shell/ui_element.h ('k') | chrome/browser/android/vr_shell/ui_element_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698