Don't crash when SimpleCache index is corrupt.
[chromium-blink-merge.git] / ui / gfx / vector3d_f.h
blob17ad332f3aab68a66b887f4e093c0c3f25547e7f
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_VECTOR3D_F_H_
11 #define UI_GFX_VECTOR3D_F_H_
13 #include <string>
15 #include "ui/base/ui_export.h"
16 #include "ui/gfx/vector2d_f.h"
18 namespace gfx {
20 class UI_EXPORT Vector3dF {
21 public:
22 Vector3dF();
23 Vector3dF(float x, float y, float z);
25 explicit Vector3dF(const Vector2dF& other);
27 float x() const { return x_; }
28 void set_x(float x) { x_ = x; }
30 float y() const { return y_; }
31 void set_y(float y) { y_ = y; }
33 float z() const { return z_; }
34 void set_z(float z) { z_ = z; }
36 // True if all components of the vector are 0.
37 bool IsZero() const;
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);
44 void operator+=(const Vector3dF& other) { Add(other); }
45 void operator-=(const Vector3dF& other) { Subtract(other); }
47 void SetToMin(const Vector3dF& other) {
48 x_ = x_ <= other.x_ ? x_ : other.x_;
49 y_ = y_ <= other.y_ ? y_ : other.y_;
50 z_ = z_ <= other.z_ ? z_ : other.z_;
53 void SetToMax(const Vector3dF& other) {
54 x_ = x_ >= other.x_ ? x_ : other.x_;
55 y_ = y_ >= other.y_ ? y_ : other.y_;
56 z_ = z_ >= other.z_ ? z_ : other.z_;
59 // Gives the square of the diagonal length of the vector.
60 double LengthSquared() const;
61 // Gives the diagonal length of the vector.
62 float Length() const;
64 // Scale all components of the vector by |scale|.
65 void Scale(float scale) { Scale(scale, scale, scale); }
66 // Scale the each component of the vector by the given scale factors.
67 void Scale(float x_scale, float y_scale, float z_scale);
69 // Take the cross product of this vector with |other| and become the result.
70 void Cross(const Vector3dF& other);
72 std::string ToString() const;
74 private:
75 float x_;
76 float y_;
77 float z_;
80 inline bool operator==(const Vector3dF& lhs, const Vector3dF& rhs) {
81 return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
84 inline Vector3dF operator-(const Vector3dF& v) {
85 return Vector3dF(-v.x(), -v.y(), -v.z());
88 inline Vector3dF operator+(const Vector3dF& lhs, const Vector3dF& rhs) {
89 Vector3dF result = lhs;
90 result.Add(rhs);
91 return result;
94 inline Vector3dF operator-(const Vector3dF& lhs, const Vector3dF& rhs) {
95 Vector3dF result = lhs;
96 result.Add(-rhs);
97 return result;
100 // Return the cross product of two vectors.
101 inline Vector3dF CrossProduct(const Vector3dF& lhs, const Vector3dF& rhs) {
102 Vector3dF result = lhs;
103 result.Cross(rhs);
104 return result;
107 // Return the dot product of two vectors.
108 UI_EXPORT float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs);
110 // Return a vector that is |v| scaled by the given scale factors along each
111 // axis.
112 UI_EXPORT Vector3dF ScaleVector3d(const Vector3dF& v,
113 float x_scale,
114 float y_scale,
115 float z_scale);
117 // Return a vector that is |v| scaled by the given scale factor.
118 inline Vector3dF ScaleVector3d(const Vector3dF& v, float scale) {
119 return ScaleVector3d(v, scale, scale, scale);
122 } // namespace gfx
124 #endif // UI_GFX_VECTOR3D_F_H_