| 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 <cmath> |
| 8 #include <vector> |
| 9 |
| 10 namespace vr_shell { |
| 11 |
| 12 namespace { |
| 13 |
| 14 float GetRayPlaneIntersection(gvr::Vec3f ray_origin, |
| 15 gvr::Vec3f ray_vector, |
| 16 gvr::Vec3f plane_origin, |
| 17 gvr::Vec3f plane_normal) { |
| 18 float denom = vr_shell::VectorDot(ray_vector, plane_normal); |
| 19 if (denom == 0) { |
| 20 // TODO(mthiesse): Line could be contained in the plane, do we care? |
| 21 return -std::numeric_limits<float>::infinity(); |
| 22 } |
| 23 gvr::Vec3f rel; |
| 24 rel.x = ray_origin.x - plane_origin.x; |
| 25 rel.y = ray_origin.y - plane_origin.y; |
| 26 rel.z = ray_origin.z - plane_origin.z; |
| 27 |
| 28 return -vr_shell::VectorDot(plane_normal, rel) / denom; |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 ReversibleTransform::ReversibleTransform() { MakeIdentity(); } |
| 34 |
| 35 void ReversibleTransform::MakeIdentity() { |
| 36 SetIdentityM(to_world); |
| 37 SetIdentityM(from_world); |
| 38 orientation.qx = orientation.qy = orientation.qz = 0.0f; |
| 39 orientation.qw = 1.0f; |
| 40 } |
| 41 |
| 42 void ReversibleTransform::Rotate(gvr::Quatf quat) { |
| 43 orientation = QuatMultiply(quat, orientation); |
| 44 |
| 45 // TODO(klausw): use specialized rotation code? Constructing the matrix |
| 46 // via axis-angle quaternion is inefficient. |
| 47 gvr::Mat4f forward = QuatToMatrix(quat); |
| 48 to_world = MatrixMul(forward, to_world); |
| 49 gvr::Mat4f reverse = MatrixTranspose(forward); |
| 50 from_world = MatrixMul(from_world, reverse); |
| 51 } |
| 52 |
| 53 void ReversibleTransform::Rotate(float ax, float ay, float az, float rad) { |
| 54 // TODO(klausw): use specialized rotation code? Constructing the matrix |
| 55 // via axis-angle quaternion is inefficient. |
| 56 Rotate(QuatFromAxisAngle(ax, ay, az, rad)); |
| 57 } |
| 58 |
| 59 void ReversibleTransform::Translate(float tx, float ty, float tz) { |
| 60 TranslateM(to_world, to_world, tx, ty, tz); |
| 61 TranslateMRight(from_world, from_world, -tx, -ty, -tz); |
| 62 } |
| 63 |
| 64 void ReversibleTransform::Scale(float sx, float sy, float sz) { |
| 65 ScaleM(to_world, to_world, sx, sy, sz); |
| 66 ScaleMRight(from_world, from_world, 1.0f / sx, 1.0f / sy, 1.0f / sz); |
| 67 } |
| 68 |
| 69 gvr::Vec3f WorldRectangle::GetCenter() const { |
| 70 const gvr::Vec3f kOrigin = {0.0f, 0.0f, 0.0f}; |
| 71 return MatrixVectorMul(transform.to_world, kOrigin); |
| 72 } |
| 73 |
| 74 gvr::Vec3f WorldRectangle::GetNormal() const { |
| 75 const gvr::Vec3f kNormalOrig = {0.0f, 0.0f, -1.0f}; |
| 76 return MatrixVectorRotate(transform.to_world, kNormalOrig); |
| 77 } |
| 78 |
| 79 float WorldRectangle::GetRayDistance(gvr::Vec3f ray_origin, |
| 80 gvr::Vec3f ray_vector) const { |
| 81 return GetRayPlaneIntersection(ray_origin, ray_vector, GetCenter(), |
| 82 GetNormal()); |
| 83 } |
| 84 |
| 85 ContentRectangle::ContentRectangle() = default; |
| 86 |
| 87 ContentRectangle::~ContentRectangle() = default; |
| 88 |
| 89 } // namespace vr_shell |
| 90 |
| OLD | NEW |