| OLD | NEW |
| (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_elements.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 ContentRectangle::ContentRectangle() = default; | |
| 105 | |
| 106 ContentRectangle::~ContentRectangle() = default; | |
| 107 | |
| 108 void ContentRectangle::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::COPYRECT: | |
| 118 animation.from.push_back(copy_rect.x()); | |
| 119 animation.from.push_back(copy_rect.y()); | |
| 120 animation.from.push_back(copy_rect.width()); | |
| 121 animation.from.push_back(copy_rect.height()); | |
| 122 break; | |
| 123 case Animation::SIZE: | |
| 124 animation.from.push_back(size.x()); | |
| 125 animation.from.push_back(size.y()); | |
| 126 break; | |
| 127 case Animation::SCALE: | |
| 128 animation.from.push_back(scale.x()); | |
| 129 animation.from.push_back(scale.y()); | |
| 130 animation.from.push_back(scale.z()); | |
| 131 break; | |
| 132 case Animation::ROTATION: | |
| 133 animation.from.push_back(rotation.x); | |
| 134 animation.from.push_back(rotation.y); | |
| 135 animation.from.push_back(rotation.z); | |
| 136 animation.from.push_back(rotation.angle); | |
| 137 break; | |
| 138 case Animation::TRANSLATION: | |
| 139 animation.from.push_back(translation.x()); | |
| 140 animation.from.push_back(translation.y()); | |
| 141 animation.from.push_back(translation.z()); | |
| 142 break; | |
| 143 case Animation::OPACITY: | |
| 144 animation.from.push_back(opacity); | |
| 145 break; | |
| 146 } | |
| 147 } | |
| 148 CHECK_EQ(animation.from.size(), animation.to.size()); | |
| 149 | |
| 150 std::vector<float> values(animation.from.size()); | |
| 151 for (std::size_t i = 0; i < animation.from.size(); ++i) { | |
| 152 if (animation.to[i] == animation.from[i] || | |
| 153 time >= (animation.start + animation.duration)) { | |
| 154 values[i] = animation.to[i]; | |
| 155 continue; | |
| 156 } | |
| 157 double value = animation.easing->CalculateValue( | |
| 158 (time - animation.start).InMillisecondsF() / | |
| 159 animation.duration.InMillisecondsF()); | |
| 160 values[i] = | |
| 161 animation.from[i] + (value * (animation.to[i] - animation.from[i])); | |
| 162 } | |
| 163 switch (animation.property) { | |
| 164 case Animation::COPYRECT: | |
| 165 CHECK_EQ(animation.from.size(), 4u); | |
| 166 copy_rect.SetRect(values[0], values[1], values[2], values[3]); | |
| 167 break; | |
| 168 case Animation::SIZE: | |
| 169 CHECK_EQ(animation.from.size(), 2u); | |
| 170 size.set_x(values[0]); | |
| 171 size.set_y(values[1]); | |
| 172 break; | |
| 173 case Animation::SCALE: | |
| 174 CHECK_EQ(animation.from.size(), 3u); | |
| 175 scale = {values[0], values[1], values[2]}; | |
| 176 break; | |
| 177 case Animation::ROTATION: | |
| 178 CHECK_EQ(animation.from.size(), 4u); | |
| 179 rotation.x = values[0]; | |
| 180 rotation.y = values[1]; | |
| 181 rotation.z = values[2]; | |
| 182 rotation.angle = values[3]; | |
| 183 break; | |
| 184 case Animation::TRANSLATION: | |
| 185 CHECK_EQ(animation.from.size(), 3u); | |
| 186 translation = {values[0], values[1], values[2]}; | |
| 187 break; | |
| 188 case Animation::OPACITY: | |
| 189 CHECK_EQ(animation.from.size(), 1u); | |
| 190 opacity = values[0]; | |
| 191 break; | |
| 192 } | |
| 193 } | |
| 194 for (auto it = animations.begin(); it != animations.end();) { | |
| 195 const Animation& animation = **it; | |
| 196 if (time >= (animation.start + animation.duration)) { | |
| 197 it = animations.erase(it); | |
| 198 } else { | |
| 199 ++it; | |
| 200 } | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 bool ContentRectangle::IsVisible() const { | |
| 205 return visible && computed_opacity > 0.0f; | |
| 206 } | |
| 207 | |
| 208 bool ContentRectangle::IsHitTestable() const { | |
| 209 return IsVisible() && hit_testable; | |
| 210 } | |
| 211 | |
| 212 } // namespace vr_shell | |
| OLD | NEW |