OLD | NEW |
(Empty) | |
| 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_VECTOR3D_F_H_ |
| 11 #define UI_GFX_VECTOR3D_F_H_ |
| 12 |
| 13 #include <string> |
| 14 |
| 15 #include "ui/base/ui_export.h" |
| 16 #include "ui/gfx/vector2d_f.h" |
| 17 |
| 18 namespace gfx { |
| 19 |
| 20 class UI_EXPORT Vector3dF { |
| 21 public: |
| 22 Vector3dF(); |
| 23 Vector3dF(float x, float y, float z); |
| 24 |
| 25 explicit Vector3dF(const Vector2dF& other); |
| 26 |
| 27 float x() const { return x_; } |
| 28 void set_x(float x) { x_ = x; } |
| 29 |
| 30 float y() const { return y_; } |
| 31 void set_y(float y) { y_ = y; } |
| 32 |
| 33 float z() const { return z_; } |
| 34 void set_z(float z) { z_ = z; } |
| 35 |
| 36 // True if all components of the vector are 0. |
| 37 bool IsZero() const; |
| 38 |
| 39 // Add the components of the |other| vector to the current vector. |
| 40 void Add(const Vector3dF& other); |
| 41 // Subtract the components of the |other| vector from the current vector. |
| 42 void Subtract(const Vector3dF& other); |
| 43 |
| 44 void operator+=(const Vector3dF& other) { Add(other); } |
| 45 void operator-=(const Vector3dF& other) { Subtract(other); } |
| 46 |
| 47 // Gives the square of the diagonal length of the vector. |
| 48 double LengthSquared() const; |
| 49 // Gives the diagonal length of the vector. |
| 50 float Length() const; |
| 51 |
| 52 // Scale all components of the vector by |scale|. |
| 53 void Scale(float scale) { Scale(scale, scale, scale); } |
| 54 // Scale the each component of the vector by the given scale factors. |
| 55 void Scale(float x_scale, float y_scale, float z_scale); |
| 56 |
| 57 // Take the cross product of this vector with |other| and become the result. |
| 58 void Cross(const Vector3dF& other); |
| 59 |
| 60 std::string ToString() const; |
| 61 |
| 62 private: |
| 63 float x_; |
| 64 float y_; |
| 65 float z_; |
| 66 }; |
| 67 |
| 68 inline bool operator==(const Vector3dF& lhs, const Vector3dF& rhs) { |
| 69 return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z(); |
| 70 } |
| 71 |
| 72 inline Vector3dF operator-(const Vector3dF& v) { |
| 73 return Vector3dF(-v.x(), -v.y(), -v.z()); |
| 74 } |
| 75 |
| 76 inline Vector3dF operator+(const Vector3dF& lhs, const Vector3dF& rhs) { |
| 77 Vector3dF result = lhs; |
| 78 result.Add(rhs); |
| 79 return result; |
| 80 } |
| 81 |
| 82 inline Vector3dF operator-(const Vector3dF& lhs, const Vector3dF& rhs) { |
| 83 Vector3dF result = lhs; |
| 84 result.Add(-rhs); |
| 85 return result; |
| 86 } |
| 87 |
| 88 // Return the cross product of two vectors. |
| 89 inline Vector3dF CrossProduct(const Vector3dF& lhs, const Vector3dF& rhs) { |
| 90 Vector3dF result = lhs; |
| 91 result.Cross(rhs); |
| 92 return result; |
| 93 } |
| 94 |
| 95 // Return the dot product of two vectors. |
| 96 UI_EXPORT float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs); |
| 97 |
| 98 |
| 99 } // namespace gfx |
| 100 |
| 101 #endif // UI_GFX_VECTOR3D_F_H_ |
OLD | NEW |