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 <iosfwd>
14#include <string>
15
16#include "ui/gfx/geometry/vector2d_f.h"
17#include "ui/gfx/gfx_export.h"
18
19namespace gfx {
20
21class GFX_EXPORT Vector3dF {
22 public:
23  Vector3dF();
24  Vector3dF(float x, float y, float z);
25
26  explicit Vector3dF(const Vector2dF& other);
27
28  float x() const { return x_; }
29  void set_x(float x) { x_ = x; }
30
31  float y() const { return y_; }
32  void set_y(float y) { y_ = y; }
33
34  float z() const { return z_; }
35  void set_z(float z) { z_ = z; }
36
37  // True if all components of the vector are 0.
38  bool IsZero() const;
39
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);
44
45  void operator+=(const Vector3dF& other) { Add(other); }
46  void operator-=(const Vector3dF& other) { Subtract(other); }
47
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_;
52  }
53
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_;
58  }
59
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;
64
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);
69
70  // Take the cross product of this vector with |other| and become the result.
71  void Cross(const Vector3dF& other);
72
73  std::string ToString() const;
74
75 private:
76  float x_;
77  float y_;
78  float z_;
79};
80
81inline bool operator==(const Vector3dF& lhs, const Vector3dF& rhs) {
82  return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
83}
84
85inline Vector3dF operator-(const Vector3dF& v) {
86  return Vector3dF(-v.x(), -v.y(), -v.z());
87}
88
89inline Vector3dF operator+(const Vector3dF& lhs, const Vector3dF& rhs) {
90  Vector3dF result = lhs;
91  result.Add(rhs);
92  return result;
93}
94
95inline Vector3dF operator-(const Vector3dF& lhs, const Vector3dF& rhs) {
96  Vector3dF result = lhs;
97  result.Add(-rhs);
98  return result;
99}
100
101// Return the cross product of two vectors.
102inline Vector3dF CrossProduct(const Vector3dF& lhs, const Vector3dF& rhs) {
103  Vector3dF result = lhs;
104  result.Cross(rhs);
105  return result;
106}
107
108// Return the dot product of two vectors.
109GFX_EXPORT float DotProduct(const Vector3dF& lhs, const Vector3dF& rhs);
110
111// Return a vector that is |v| scaled by the given scale factors along each
112// axis.
113GFX_EXPORT Vector3dF ScaleVector3d(const Vector3dF& v,
114                                   float x_scale,
115                                   float y_scale,
116                                   float z_scale);
117
118// Return a vector that is |v| scaled by the given scale factor.
119inline Vector3dF ScaleVector3d(const Vector3dF& v, float scale) {
120  return ScaleVector3d(v, scale, scale, scale);
121}
122
123// This is declared here for use in gtest-based unit tests but is defined in
124// the gfx_test_support target. Depend on that to use this in your unit test.
125// This should not be used in production code - call ToString() instead.
126void PrintTo(const Vector3dF& vector, ::std::ostream* os);
127
128}  // namespace gfx
129
130#endif // UI_GFX_GEOMETRY_VECTOR3D_F_H_
131