Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / geometry / vector3d_f.h
blob4e0eaa412492205b14e8ddb63a1d60c9cee4c15b
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.
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.
10 #ifndef UI_GFX_GEOMETRY_VECTOR3D_F_H_
11 #define UI_GFX_GEOMETRY_VECTOR3D_F_H_
13 #include <iosfwd>
14 #include <string>
16 #include "ui/gfx/geometry/vector2d_f.h"
17 #include "ui/gfx/gfx_export.h"
19 namespace gfx {
21 class GFX_EXPORT Vector3dF {
22 public:
23 Vector3dF();
24 Vector3dF(float x, float y, float z);
26 explicit Vector3dF(const Vector2dF& other);
28 float x() const { return x_; }
29 void set_x(float x) { x_ = x; }
31 float y() const { return y_; }
32 void set_y(float y) { y_ = y; }
34 float z() const { return z_; }
35 void set_z(float z) { z_ = z; }
37 // True if all components of the vector are 0.
38 bool IsZero() const;
40 // Add the components of the |other| vector to the current vector.
41 void Add(const Vector3dF& other);
42 // Subtract the components of the |other| vector from the current vector.
43 void Subtract(const Vector3dF& other);
45 void operator+=(const Vector3dF& other) { Add(other); }
46 void operator-=(const Vector3dF& other) { Subtract(other); }
48 void SetToMin(const Vector3dF& other) {
49 x_ = x_ <= other.x_ ? x_ : other.x_;
50 y_ = y_ <= other.y_ ? y_ : other.y_;
51 z_ = z_ <= other.z_ ? z_ : other.z_;
54 void SetToMax(const Vector3dF& other) {
55 x_ = x_ >= other.x_ ? x_ : other.x_;
56 y_ = y_ >= other.y_ ? y_ : other.y_;
57 z_ = z_ >= other.z_ ? z_ : other.z_;
60 // Gives the square of the diagonal length of the vector.
61 double LengthSquared() const;
62 // Gives the diagonal length of the vector.
63 float Length() const;
65 // Scale all components of the vector by |scale|.
66 void Scale(float scale) { Scale(scale, scale, scale); }
67 // Scale the each component of the vector by the given scale factors.
68 void Scale(float x_scale, float y_scale, float z_scale);
70 // Take the cross product of this vector with |other| and become the result.
71 void Cross(const Vector3dF& other);
73 std::string ToString() const;
75 private:
76 float x_;
77 float y_;
78 float z_;
81 inline bool operator==(const Vector3dF& lhs, const Vector3dF& rhs) {
82 return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
85 inline Vector3dF operator-(const Vector3dF& v) {
86 return Vector3dF(-v.x(), -v.y(), -v.z());
89 inline Vector3dF operator+(const Vector3dF& lhs, const Vector3dF& rhs) {
90 Vector3dF result = lhs;
91 result.Add(rhs);
92 return result;
95 inline Vector3dF operator-(const Vector3dF& lhs, const Vector3dF& rhs) {
96 Vector3dF result = lhs;
97 result.Add(-rhs);
98 return result;
101 // Return the cross product of two vectors.
102 inline Vector3dF CrossProduct(const Vector3dF& lhs, const Vector3dF& rhs) {
103 Vector3dF result = lhs;
104 result.Cross(rhs);
105 return result;
108 // Return the dot product of two vectors.
109 GFX_EXPORT float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs);
111 // Return a vector that is |v| scaled by the given scale factors along each
112 // axis.
113 GFX_EXPORT Vector3dF ScaleVector3d(const Vector3dF& v,
114 float x_scale,
115 float y_scale,
116 float z_scale);
118 // Return a vector that is |v| scaled by the given scale factor.
119 inline Vector3dF ScaleVector3d(const Vector3dF& v, float scale) {
120 return ScaleVector3d(v, scale, scale, scale);
123 // Returns the angle between |base| and |other| in degrees.
124 GFX_EXPORT float AngleBetweenVectorsInDegrees(const gfx::Vector3dF& base,
125 const gfx::Vector3dF& other);
127 // Returns the clockwise angle between |base| and |other| where |normal| is the
128 // normal of the virtual surface to measure clockwise according to.
129 GFX_EXPORT float ClockwiseAngleBetweenVectorsInDegrees(
130 const gfx::Vector3dF& base,
131 const gfx::Vector3dF& other,
132 const gfx::Vector3dF& normal);
134 // This is declared here for use in gtest-based unit tests but is defined in
135 // the gfx_test_support target. Depend on that to use this in your unit test.
136 // This should not be used in production code - call ToString() instead.
137 void PrintTo(const Vector3dF& vector, ::std::ostream* os);
139 } // namespace gfx
141 #endif // UI_GFX_GEOMETRY_VECTOR3D_F_H_