• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 "ui/gfx/geometry/vector3d_f.h"
6 
7 #include <cmath>
8 
9 #include "base/strings/stringprintf.h"
10 #include "ui/gfx/geometry/angle_conversions.h"
11 
12 namespace {
13 const double kEpsilon = 1.0e-6;
14 }
15 
16 namespace gfx {
17 
ToString() const18 std::string Vector3dF::ToString() const {
19   return base::StringPrintf("[%f %f %f]", x_, y_, z_);
20 }
21 
IsZero() const22 bool Vector3dF::IsZero() const {
23   return x_ == 0 && y_ == 0 && z_ == 0;
24 }
25 
Add(const Vector3dF & other)26 void Vector3dF::Add(const Vector3dF& other) {
27   x_ += other.x_;
28   y_ += other.y_;
29   z_ += other.z_;
30 }
31 
Subtract(const Vector3dF & other)32 void Vector3dF::Subtract(const Vector3dF& other) {
33   x_ -= other.x_;
34   y_ -= other.y_;
35   z_ -= other.z_;
36 }
37 
LengthSquared() const38 double Vector3dF::LengthSquared() const {
39   return static_cast<double>(x_) * x_ + static_cast<double>(y_) * y_ +
40       static_cast<double>(z_) * z_;
41 }
42 
Length() const43 float Vector3dF::Length() const {
44   return static_cast<float>(std::sqrt(LengthSquared()));
45 }
46 
Scale(float x_scale,float y_scale,float z_scale)47 void Vector3dF::Scale(float x_scale, float y_scale, float z_scale) {
48   x_ *= x_scale;
49   y_ *= y_scale;
50   z_ *= z_scale;
51 }
52 
Cross(const Vector3dF & other)53 void Vector3dF::Cross(const Vector3dF& other) {
54   double dx = x_;
55   double dy = y_;
56   double dz = z_;
57   float x = static_cast<float>(dy * other.z() - dz * other.y());
58   float y = static_cast<float>(dz * other.x() - dx * other.z());
59   float z = static_cast<float>(dx * other.y() - dy * other.x());
60   x_ = x;
61   y_ = y;
62   z_ = z;
63 }
64 
GetNormalized(Vector3dF * out) const65 bool Vector3dF::GetNormalized(Vector3dF* out) const {
66   double length_squared = LengthSquared();
67   *out = *this;
68   if (length_squared < kEpsilon * kEpsilon)
69     return false;
70   out->Scale(1 / sqrt(length_squared));
71   return true;
72 }
73 
DotProduct(const Vector3dF & lhs,const Vector3dF & rhs)74 float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs) {
75   return lhs.x() * rhs.x() + lhs.y() * rhs.y() + lhs.z() * rhs.z();
76 }
77 
ScaleVector3d(const Vector3dF & v,float x_scale,float y_scale,float z_scale)78 Vector3dF ScaleVector3d(const Vector3dF& v,
79                         float x_scale,
80                         float y_scale,
81                         float z_scale) {
82   Vector3dF scaled_v(v);
83   scaled_v.Scale(x_scale, y_scale, z_scale);
84   return scaled_v;
85 }
86 
AngleBetweenVectorsInDegrees(const gfx::Vector3dF & base,const gfx::Vector3dF & other)87 float AngleBetweenVectorsInDegrees(const gfx::Vector3dF& base,
88                                    const gfx::Vector3dF& other) {
89   return gfx::RadToDeg(
90       std::acos(gfx::DotProduct(base, other) / base.Length() / other.Length()));
91 }
92 
ClockwiseAngleBetweenVectorsInDegrees(const gfx::Vector3dF & base,const gfx::Vector3dF & other,const gfx::Vector3dF & normal)93 float ClockwiseAngleBetweenVectorsInDegrees(const gfx::Vector3dF& base,
94                                             const gfx::Vector3dF& other,
95                                             const gfx::Vector3dF& normal) {
96   float angle = AngleBetweenVectorsInDegrees(base, other);
97   gfx::Vector3dF cross(base);
98   cross.Cross(other);
99 
100   // If the dot product of this cross product is normal, it means that the
101   // shortest angle between |base| and |other| was counterclockwise with respect
102   // to the surface represented by |normal| and this angle must be reversed.
103   if (gfx::DotProduct(cross, normal) > 0.0f)
104     angle = 360.0f - angle;
105   return angle;
106 }
107 
108 }  // namespace gfx
109