diff options
author | Anna Maria <offenwanger@chromium.org> | 2017-12-07 10:09:49 +0900 |
---|---|---|
committer | Qijiang Fan <fqj@google.com> | 2020-06-05 07:59:10 +0900 |
commit | 4a0dbda51660ed914c7bc34856c0102cbf0978e7 (patch) | |
tree | 1b72d3dacb7ab606366d2bbb782423612e6da1c2 | |
parent | 2bfbd285a4943890c5dd0e0504dde5fc80a2bc25 (diff) | |
download | libchrome-4a0dbda51660ed914c7bc34856c0102cbf0978e7.tar.gz |
Add vector Length function to Quaternion
Bug: 695937
Change-Id: I9355c99f508f61a3d72156c9bca6cbcb4ed18324
Reviewed-on: https://chromium-review.googlesource.com/812445
Reviewed-by: Ian Vollick <vollick@chromium.org>
Commit-Queue: Anna Offenwanger <offenwanger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522289}
CrOS-Libchrome-Original-Commit: c9199d52c916658bec4674a41da856a675eb4945
-rw-r--r-- | ui/gfx/geometry/quaternion.cc | 6 | ||||
-rw-r--r-- | ui/gfx/geometry/quaternion.h | 2 | ||||
-rw-r--r-- | ui/gfx/geometry/quaternion_unittest.cc | 13 |
3 files changed, 20 insertions, 1 deletions
diff --git a/ui/gfx/geometry/quaternion.cc b/ui/gfx/geometry/quaternion.cc index 9da8f47111..ef7fae06c7 100644 --- a/ui/gfx/geometry/quaternion.cc +++ b/ui/gfx/geometry/quaternion.cc @@ -80,8 +80,12 @@ Quaternion Quaternion::Lerp(const Quaternion& q, double t) const { return (((1.0 - t) * *this) + (t * q)).Normalized(); } +double Quaternion::Length() const { + return x_ * x_ + y_ * y_ + z_ * z_ + w_ * w_; +} + Quaternion Quaternion::Normalized() const { - double length = x_ * x_ + y_ * y_ + z_ * z_ + w_ * w_; + double length = Length(); if (length < kEpsilon) return *this; return *this / sqrt(length); diff --git a/ui/gfx/geometry/quaternion.h b/ui/gfx/geometry/quaternion.h index f4e124ce52..7f65b796e6 100644 --- a/ui/gfx/geometry/quaternion.h +++ b/ui/gfx/geometry/quaternion.h @@ -59,6 +59,8 @@ class GFX_EXPORT Quaternion { // and values outside that range will extrapolate beyond in either direction. Quaternion Lerp(const Quaternion& q, double t) const; + double Length() const; + Quaternion Normalized() const; std::string ToString() const; diff --git a/ui/gfx/geometry/quaternion_unittest.cc b/ui/gfx/geometry/quaternion_unittest.cc index 38e192e1d0..5c8fa9b8ae 100644 --- a/ui/gfx/geometry/quaternion_unittest.cc +++ b/ui/gfx/geometry/quaternion_unittest.cc @@ -99,6 +99,19 @@ TEST(QuatTest, Scaling) { } } +TEST(QuatTest, Normalization) { + Quaternion q(1, -1, 1, -1); + EXPECT_NEAR(q.Length(), 4, kEpsilon); + + q = q.Normalized(); + + EXPECT_NEAR(q.Length(), 1, kEpsilon); + EXPECT_NEAR(q.x(), 0.5, kEpsilon); + EXPECT_NEAR(q.y(), -0.5, kEpsilon); + EXPECT_NEAR(q.z(), 0.5, kEpsilon); + EXPECT_NEAR(q.w(), -0.5, kEpsilon); +} + TEST(QuatTest, Lerp) { for (size_t i = 1; i < 100; ++i) { Quaternion a(0, 0, 0, 0); |