1 // Copyright (c) 2011 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 #ifndef UI_GFX_GEOMETRY_POINT3_F_H_
6 #define UI_GFX_GEOMETRY_POINT3_F_H_
7
8 #include <iosfwd>
9 #include <string>
10
11 #include "ui/gfx/geometry/point_f.h"
12 #include "ui/gfx/geometry/vector3d_f.h"
13 #include "ui/gfx/gfx_export.h"
14
15 namespace gfx {
16
17 // A point has an x, y and z coordinate.
18 class GFX_EXPORT Point3F {
19 public:
Point3F()20 constexpr Point3F() : x_(0), y_(0), z_(0) {}
Point3F(float x,float y,float z)21 constexpr Point3F(float x, float y, float z) : x_(x), y_(y), z_(z) {}
22
Point3F(const PointF & point)23 constexpr explicit Point3F(const PointF& point)
24 : x_(point.x()), y_(point.y()), z_(0) {}
25
Scale(float scale)26 void Scale(float scale) {
27 Scale(scale, scale, scale);
28 }
29
Scale(float x_scale,float y_scale,float z_scale)30 void Scale(float x_scale, float y_scale, float z_scale) {
31 SetPoint(x() * x_scale, y() * y_scale, z() * z_scale);
32 }
33
x()34 constexpr float x() const { return x_; }
y()35 constexpr float y() const { return y_; }
z()36 constexpr float z() const { return z_; }
37
set_x(float x)38 void set_x(float x) { x_ = x; }
set_y(float y)39 void set_y(float y) { y_ = y; }
set_z(float z)40 void set_z(float z) { z_ = z; }
41
SetPoint(float x,float y,float z)42 void SetPoint(float x, float y, float z) {
43 x_ = x;
44 y_ = y;
45 z_ = z;
46 }
47
48 // Offset the point by the given vector.
49 void operator+=(const Vector3dF& v) {
50 x_ += v.x();
51 y_ += v.y();
52 z_ += v.z();
53 }
54
55 // Offset the point by the given vector's inverse.
56 void operator-=(const Vector3dF& v) {
57 x_ -= v.x();
58 y_ -= v.y();
59 z_ -= v.z();
60 }
61
62 // Returns the squared euclidean distance between two points.
SquaredDistanceTo(const Point3F & other)63 float SquaredDistanceTo(const Point3F& other) const {
64 float dx = x_ - other.x_;
65 float dy = y_ - other.y_;
66 float dz = z_ - other.z_;
67 return dx * dx + dy * dy + dz * dz;
68 }
69
AsPointF()70 PointF AsPointF() const { return PointF(x_, y_); }
71
72 // Returns a string representation of 3d point.
73 std::string ToString() const;
74
75 private:
76 float x_;
77 float y_;
78 float z_;
79
80 // copy/assign are allowed.
81 };
82
83 inline bool operator==(const Point3F& lhs, const Point3F& rhs) {
84 return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
85 }
86
87 inline bool operator!=(const Point3F& lhs, const Point3F& rhs) {
88 return !(lhs == rhs);
89 }
90
91 // Add a vector to a point, producing a new point offset by the vector.
92 GFX_EXPORT Point3F operator+(const Point3F& lhs, const Vector3dF& rhs);
93
94 // Subtract a vector from a point, producing a new point offset by the vector's
95 // inverse.
96 GFX_EXPORT Point3F operator-(const Point3F& lhs, const Vector3dF& rhs);
97
98 // Subtract one point from another, producing a vector that represents the
99 // distances between the two points along each axis.
100 GFX_EXPORT Vector3dF operator-(const Point3F& lhs, const Point3F& rhs);
101
PointAtOffsetFromOrigin(const Vector3dF & offset)102 inline Point3F PointAtOffsetFromOrigin(const Vector3dF& offset) {
103 return Point3F(offset.x(), offset.y(), offset.z());
104 }
105
ScalePoint(const Point3F & p,float x_scale,float y_scale,float z_scale)106 inline Point3F ScalePoint(const Point3F& p,
107 float x_scale,
108 float y_scale,
109 float z_scale) {
110 return Point3F(p.x() * x_scale, p.y() * y_scale, p.z() * z_scale);
111 }
112
ScalePoint(const Point3F & p,const Vector3dF & v)113 inline Point3F ScalePoint(const Point3F& p, const Vector3dF& v) {
114 return Point3F(p.x() * v.x(), p.y() * v.y(), p.z() * v.z());
115 }
116
ScalePoint(const Point3F & p,float scale)117 inline Point3F ScalePoint(const Point3F& p, float scale) {
118 return ScalePoint(p, scale, scale, scale);
119 }
120
121 // This is declared here for use in gtest-based unit tests but is defined in
122 // the //ui/gfx:test_support target. Depend on that to use this in your unit
123 // test. This should not be used in production code - call ToString() instead.
124 void PrintTo(const Point3F& point, ::std::ostream* os);
125
126 } // namespace gfx
127
128 #endif // UI_GFX_GEOMETRY_POINT3_F_H_
129