• 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 // Defines a simple float vector class.  This class is used to indicate a
6 // distance in two dimensions between two points. Subtracting two points should
7 // produce a vector, and adding a vector to a point produces the point at the
8 // vector's distance from the original point.
9 
10 #ifndef UI_GFX_GEOMETRY_VECTOR3D_F_H_
11 #define UI_GFX_GEOMETRY_VECTOR3D_F_H_
12 
13 #include <iosfwd>
14 #include <string>
15 
16 #include "ui/gfx/geometry/vector2d_f.h"
17 #include "ui/gfx/gfx_export.h"
18 
19 namespace gfx {
20 
21 class GFX_EXPORT Vector3dF {
22  public:
Vector3dF()23   constexpr Vector3dF() : x_(0), y_(0), z_(0) {}
Vector3dF(float x,float y,float z)24   constexpr Vector3dF(float x, float y, float z) : x_(x), y_(y), z_(z) {}
25 
Vector3dF(const Vector2dF & other)26   constexpr explicit Vector3dF(const Vector2dF& other)
27       : x_(other.x()), y_(other.y()), z_(0) {}
28 
x()29   constexpr float x() const { return x_; }
set_x(float x)30   void set_x(float x) { x_ = x; }
31 
y()32   constexpr float y() const { return y_; }
set_y(float y)33   void set_y(float y) { y_ = y; }
34 
z()35   constexpr float z() const { return z_; }
set_z(float z)36   void set_z(float z) { z_ = z; }
37 
38   // True if all components of the vector are 0.
39   bool IsZero() const;
40 
41   // Add the components of the |other| vector to the current vector.
42   void Add(const Vector3dF& other);
43   // Subtract the components of the |other| vector from the current vector.
44   void Subtract(const Vector3dF& other);
45 
46   void operator+=(const Vector3dF& other) { Add(other); }
47   void operator-=(const Vector3dF& other) { Subtract(other); }
48 
SetToMin(const Vector3dF & other)49   void SetToMin(const Vector3dF& other) {
50     x_ = x_ <= other.x_ ? x_ : other.x_;
51     y_ = y_ <= other.y_ ? y_ : other.y_;
52     z_ = z_ <= other.z_ ? z_ : other.z_;
53   }
54 
SetToMax(const Vector3dF & other)55   void SetToMax(const Vector3dF& other) {
56     x_ = x_ >= other.x_ ? x_ : other.x_;
57     y_ = y_ >= other.y_ ? y_ : other.y_;
58     z_ = z_ >= other.z_ ? z_ : other.z_;
59   }
60 
61   // Gives the square of the diagonal length of the vector.
62   double LengthSquared() const;
63   // Gives the diagonal length of the vector.
64   float Length() const;
65 
66   // Scale all components of the vector by |scale|.
Scale(float scale)67   void Scale(float scale) { Scale(scale, scale, scale); }
68   // Scale the each component of the vector by the given scale factors.
69   void Scale(float x_scale, float y_scale, float z_scale);
70 
71   // Take the cross product of this vector with |other| and become the result.
72   void Cross(const Vector3dF& other);
73 
74   // |out| is assigned a unit-length vector in the direction of |this| iff
75   // this function returns true. It can return false if |this| is too short.
76   bool GetNormalized(Vector3dF* out) const;
77 
78   std::string ToString() const;
79 
80  private:
81   float x_;
82   float y_;
83   float z_;
84 };
85 
86 inline bool operator==(const Vector3dF& lhs, const Vector3dF& rhs) {
87   return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
88 }
89 
90 inline Vector3dF operator-(const Vector3dF& v) {
91   return Vector3dF(-v.x(), -v.y(), -v.z());
92 }
93 
94 inline Vector3dF operator+(const Vector3dF& lhs, const Vector3dF& rhs) {
95   Vector3dF result = lhs;
96   result.Add(rhs);
97   return result;
98 }
99 
100 inline Vector3dF operator-(const Vector3dF& lhs, const Vector3dF& rhs) {
101   Vector3dF result = lhs;
102   result.Add(-rhs);
103   return result;
104 }
105 
106 // Return the cross product of two vectors.
CrossProduct(const Vector3dF & lhs,const Vector3dF & rhs)107 inline Vector3dF CrossProduct(const Vector3dF& lhs, const Vector3dF& rhs) {
108   Vector3dF result = lhs;
109   result.Cross(rhs);
110   return result;
111 }
112 
113 // Return the dot product of two vectors.
114 GFX_EXPORT float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs);
115 
116 // Return a vector that is |v| scaled by the given scale factors along each
117 // axis.
118 GFX_EXPORT Vector3dF ScaleVector3d(const Vector3dF& v,
119                                    float x_scale,
120                                    float y_scale,
121                                    float z_scale);
122 
123 // Return a vector that is |v| scaled by the components of |s|
ScaleVector3d(const Vector3dF & v,const Vector3dF & s)124 inline Vector3dF ScaleVector3d(const Vector3dF& v, const Vector3dF& s) {
125   return ScaleVector3d(v, s.x(), s.y(), s.z());
126 }
127 
128 // Return a vector that is |v| scaled by the given scale factor.
ScaleVector3d(const Vector3dF & v,float scale)129 inline Vector3dF ScaleVector3d(const Vector3dF& v, float scale) {
130   return ScaleVector3d(v, scale, scale, scale);
131 }
132 
133 // Returns the angle between |base| and |other| in degrees.
134 GFX_EXPORT float AngleBetweenVectorsInDegrees(const gfx::Vector3dF& base,
135                                               const gfx::Vector3dF& other);
136 
137 // Returns the clockwise angle between |base| and |other| where |normal| is the
138 // normal of the virtual surface to measure clockwise according to.
139 GFX_EXPORT float ClockwiseAngleBetweenVectorsInDegrees(
140     const gfx::Vector3dF& base,
141     const gfx::Vector3dF& other,
142     const gfx::Vector3dF& normal);
143 
144 // This is declared here for use in gtest-based unit tests but is defined in
145 // the //ui/gfx:test_support target. Depend on that to use this in your unit
146 // test. This should not be used in production code - call ToString() instead.
147 void PrintTo(const Vector3dF& vector, ::std::ostream* os);
148 
149 }  // namespace gfx
150 
151 #endif // UI_GFX_GEOMETRY_VECTOR3D_F_H_
152