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_
15 #include "ui/base/ui_export.h"
16 #include "ui/gfx/vector2d_f.h"
20 class UI_EXPORT 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.
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 ClampToMax(const Vector3dF
& max
) {
48 x_
= x_
<= max
.x_
? x_
: max
.x_
;
49 y_
= y_
<= max
.y_
? y_
: max
.y_
;
50 z_
= z_
<= max
.z_
? z_
: max
.z_
;
53 void ClampToMin(const Vector3dF
& min
) {
54 x_
= x_
>= min
.x_
? x_
: min
.x_
;
55 y_
= y_
>= min
.y_
? y_
: min
.y_
;
56 z_
= z_
>= min
.z_
? z_
: min
.z_
;
59 // Gives the square of the diagonal length of the vector.
60 double LengthSquared() const;
61 // Gives the diagonal length of the vector.
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;
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
;
94 inline Vector3dF
operator-(const Vector3dF
& lhs
, const Vector3dF
& rhs
) {
95 Vector3dF result
= lhs
;
100 // Return the cross product of two vectors.
101 inline Vector3dF
CrossProduct(const Vector3dF
& lhs
, const Vector3dF
& rhs
) {
102 Vector3dF result
= lhs
;
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
112 UI_EXPORT Vector3dF
ScaleVector3d(const Vector3dF
& v
,
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
);
124 #endif // UI_GFX_VECTOR3D_F_H_