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

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

Issue 2730883003: Remove unnecessary UI element math. (Closed)
Patch Set: Add comments to non-obvious methods. Created 3 years, 9 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/vr_shell/ui_elements.h" 5 #include "chrome/browser/android/vr_shell/ui_elements.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "chrome/browser/android/vr_shell/animation.h" 10 #include "chrome/browser/android/vr_shell/animation.h"
11 #include "chrome/browser/android/vr_shell/easing.h" 11 #include "chrome/browser/android/vr_shell/easing.h"
12 12
13 namespace vr_shell { 13 namespace vr_shell {
14 14
15 namespace { 15 namespace {
16 16
17 float GetRayPlaneIntersection(gvr::Vec3f ray_origin, 17 bool GetRayPlaneDistance(const gvr::Vec3f& ray_origin,
18 gvr::Vec3f ray_vector, 18 const gvr::Vec3f& ray_vector,
19 gvr::Vec3f plane_origin, 19 const gvr::Vec3f& plane_origin,
20 gvr::Vec3f plane_normal) { 20 const gvr::Vec3f& plane_normal,
21 float* distance) {
21 float denom = vr_shell::VectorDot(ray_vector, plane_normal); 22 float denom = vr_shell::VectorDot(ray_vector, plane_normal);
22 if (denom == 0) { 23 if (denom == 0) {
23 // TODO(mthiesse): Line could be contained in the plane, do we care? 24 return false;
24 return -std::numeric_limits<float>::infinity();
25 } 25 }
26 gvr::Vec3f rel; 26 gvr::Vec3f rel;
27 rel.x = ray_origin.x - plane_origin.x; 27 rel.x = ray_origin.x - plane_origin.x;
28 rel.y = ray_origin.y - plane_origin.y; 28 rel.y = ray_origin.y - plane_origin.y;
29 rel.z = ray_origin.z - plane_origin.z; 29 rel.z = ray_origin.z - plane_origin.z;
30 30 *distance = -vr_shell::VectorDot(plane_normal, rel) / denom;
31 return -vr_shell::VectorDot(plane_normal, rel) / denom; 31 return true;
32 } 32 }
33 33
34 } // namespace 34 } // namespace
35 35
36 ReversibleTransform::ReversibleTransform() { 36 Transform::Transform() {
37 MakeIdentity(); 37 MakeIdentity();
38 } 38 }
39 39
40 void ReversibleTransform::MakeIdentity() { 40 void Transform::MakeIdentity() {
41 SetIdentityM(to_world); 41 SetIdentityM(to_world);
42 SetIdentityM(from_world);
43 orientation.qx = orientation.qy = orientation.qz = 0.0f;
44 orientation.qw = 1.0f;
45 } 42 }
46 43
47 void ReversibleTransform::Rotate(gvr::Quatf quat) { 44 void Transform::Rotate(gvr::Quatf quat) {
48 orientation = QuatMultiply(quat, orientation);
49
50 // TODO(klausw): use specialized rotation code? Constructing the matrix 45 // TODO(klausw): use specialized rotation code? Constructing the matrix
51 // via axis-angle quaternion is inefficient. 46 // via axis-angle quaternion is inefficient.
52 gvr::Mat4f forward = QuatToMatrix(quat); 47 gvr::Mat4f forward = QuatToMatrix(quat);
53 to_world = MatrixMul(forward, to_world); 48 to_world = MatrixMul(forward, to_world);
54 gvr::Mat4f reverse = MatrixTranspose(forward);
55 from_world = MatrixMul(from_world, reverse);
56 } 49 }
57 50
58 void ReversibleTransform::Rotate(float ax, float ay, float az, float rad) { 51 void Transform::Rotate(float ax, float ay, float az, float rad) {
59 // TODO(klausw): use specialized rotation code? Constructing the matrix
60 // via axis-angle quaternion is inefficient.
61 Rotate(QuatFromAxisAngle({ax, ay, az}, rad)); 52 Rotate(QuatFromAxisAngle({ax, ay, az}, rad));
62 } 53 }
63 54
64 void ReversibleTransform::Translate(float tx, float ty, float tz) { 55 void Transform::Translate(float tx, float ty, float tz) {
65 TranslateM(to_world, to_world, tx, ty, tz); 56 TranslateM(to_world, to_world, tx, ty, tz);
66 TranslateMRight(from_world, from_world, -tx, -ty, -tz);
67 } 57 }
68 58
69 void ReversibleTransform::Scale(float sx, float sy, float sz) { 59 void Transform::Scale(float sx, float sy, float sz) {
70 ScaleM(to_world, to_world, sx, sy, sz); 60 ScaleM(to_world, to_world, sx, sy, sz);
71 ScaleMRight(from_world, from_world, 1.0f / sx, 1.0f / sy, 1.0f / sz); 61 }
62
63 const gvr::Mat4f& WorldRectangle::TransformMatrix() const {
64 return transform_.to_world;
65 }
66
67 void WorldRectangle::SetTransform(const Transform& transform) {
68 transform_ = transform;
72 } 69 }
73 70
74 gvr::Vec3f WorldRectangle::GetCenter() const { 71 gvr::Vec3f WorldRectangle::GetCenter() const {
75 const gvr::Vec3f kOrigin = {0.0f, 0.0f, 0.0f}; 72 const gvr::Vec3f kOrigin = {0.0f, 0.0f, 0.0f};
76 return MatrixVectorMul(transform.to_world, kOrigin); 73 return MatrixVectorMul(transform_.to_world, kOrigin);
74 }
75
76 gvr::Vec2f WorldRectangle::GetUnitRectangleCoordinates(
77 const gvr::Vec3f& world_point) {
78 const gvr::Mat4f& transform = transform_.to_world;
79 gvr::Vec3f origin = MatrixVectorMul(transform, gvr::Vec3f({0, 0, 0}));
80 gvr::Vec3f xAxis = MatrixVectorMul(transform, gvr::Vec3f({1, 0, 0}));
81 gvr::Vec3f yAxis = MatrixVectorMul(transform, gvr::Vec3f({0, 1, 0}));
82 xAxis = VectorSubtract(xAxis, origin);
83 yAxis = VectorSubtract(yAxis, origin);
84 gvr::Vec3f point = VectorSubtract(world_point, origin);
85
86 float x = VectorDot(point, xAxis) / VectorDot(xAxis, xAxis);
87 float y = VectorDot(point, yAxis) / VectorDot(yAxis, yAxis);
88 return {x, y};
77 } 89 }
78 90
79 gvr::Vec3f WorldRectangle::GetNormal() const { 91 gvr::Vec3f WorldRectangle::GetNormal() const {
80 const gvr::Vec3f kNormalOrig = {0.0f, 0.0f, -1.0f}; 92 const gvr::Vec3f kNormalOrig = {0.0f, 0.0f, -1.0f};
81 return MatrixVectorRotate(transform.to_world, kNormalOrig); 93 return MatrixVectorRotate(transform_.to_world, kNormalOrig);
82 } 94 }
83 95
84 float WorldRectangle::GetRayDistance(gvr::Vec3f ray_origin, 96 bool WorldRectangle::GetRayDistance(const gvr::Vec3f& ray_origin,
85 gvr::Vec3f ray_vector) const { 97 const gvr::Vec3f& ray_vector,
86 return GetRayPlaneIntersection(ray_origin, ray_vector, GetCenter(), 98 float* distance) const {
87 GetNormal()); 99 return GetRayPlaneDistance(ray_origin, ray_vector, GetCenter(), GetNormal(),
100 distance);
88 } 101 }
89 102
90 ContentRectangle::ContentRectangle() = default; 103 ContentRectangle::ContentRectangle() = default;
91 104
92 ContentRectangle::~ContentRectangle() = default; 105 ContentRectangle::~ContentRectangle() = default;
93 106
94 void ContentRectangle::Animate(int64_t time) { 107 void ContentRectangle::Animate(int64_t time) {
95 for (auto& it : animations) { 108 for (auto& it : animations) {
96 Animation& animation = *it; 109 Animation& animation = *it;
97 if (time < animation.start) 110 if (time < animation.start)
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 208
196 bool ContentRectangle::IsVisible() const { 209 bool ContentRectangle::IsVisible() const {
197 return visible && computed_opacity > 0.0f; 210 return visible && computed_opacity > 0.0f;
198 } 211 }
199 212
200 bool ContentRectangle::IsHitTestable() const { 213 bool ContentRectangle::IsHitTestable() const {
201 return IsVisible() && hit_testable; 214 return IsVisible() && hit_testable;
202 } 215 }
203 216
204 } // namespace vr_shell 217 } // namespace vr_shell
OLDNEW
« no previous file with comments | « chrome/browser/android/vr_shell/ui_elements.h ('k') | chrome/browser/android/vr_shell/ui_scene.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698