vector3d_f.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 <string>
14
15#include "ui/gfx/geometry/vector2d_f.h"
16#include "ui/gfx/gfx_export.h"
17
18namespace gfx {
19
20class GFX_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  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_;
51  }
52
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_;
57  }
58
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;
63
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);
68
69  // Take the cross product of this vector with |other| and become the result.
70  void Cross(const Vector3dF& other);
71
72  std::string ToString() const;
73
74 private:
75  float x_;
76  float y_;
77  float z_;
78};
79
80inline bool operator==(const Vector3dF& lhs, const Vector3dF& rhs) {
81  return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
82}
83
84inline Vector3dF operator-(const Vector3dF& v) {
85  return Vector3dF(-v.x(), -v.y(), -v.z());
86}
87
88inline Vector3dF operator+(const Vector3dF& lhs, const Vector3dF& rhs) {
89  Vector3dF result = lhs;
90  result.Add(rhs);
91  return result;
92}
93
94inline Vector3dF operator-(const Vector3dF& lhs, const Vector3dF& rhs) {
95  Vector3dF result = lhs;
96  result.Add(-rhs);
97  return result;
98}
99
100// Return the cross product of two vectors.
101inline Vector3dF CrossProduct(const Vector3dF& lhs, const Vector3dF& rhs) {
102  Vector3dF result = lhs;
103  result.Cross(rhs);
104  return result;
105}
106
107// Return the dot product of two vectors.
108GFX_EXPORT float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs);
109
110// Return a vector that is |v| scaled by the given scale factors along each
111// axis.
112GFX_EXPORT Vector3dF ScaleVector3d(const Vector3dF& v,
113                                   float x_scale,
114                                   float y_scale,
115                                   float z_scale);
116
117// Return a vector that is |v| scaled by the given scale factor.
118inline Vector3dF ScaleVector3d(const Vector3dF& v, float scale) {
119  return ScaleVector3d(v, scale, scale, scale);
120}
121
122}  // namespace gfx
123
124#endif // UI_GFX_GEOMETRY_VECTOR3D_F_H_
125