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