Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / geometry / point3_f.h
blob262e3555d31849f0e41b6a4569fddde8f1e6880c
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.
5 #ifndef UI_GFX_GEOMETRY_POINT3_F_H_
6 #define UI_GFX_GEOMETRY_POINT3_F_H_
8 #include <iosfwd>
9 #include <string>
11 #include "ui/gfx/geometry/point_f.h"
12 #include "ui/gfx/geometry/vector3d_f.h"
13 #include "ui/gfx/gfx_export.h"
15 namespace gfx {
17 // A point has an x, y and z coordinate.
18 class GFX_EXPORT Point3F {
19 public:
20 Point3F() : x_(0), y_(0), z_(0) {}
22 Point3F(float x, float y, float z) : x_(x), y_(y), z_(z) {}
24 explicit Point3F(const PointF& point) : x_(point.x()), y_(point.y()), z_(0) {}
26 ~Point3F() {}
28 void Scale(float scale) {
29 Scale(scale, scale, scale);
32 void Scale(float x_scale, float y_scale, float z_scale) {
33 SetPoint(x() * x_scale, y() * y_scale, z() * z_scale);
36 float x() const { return x_; }
37 float y() const { return y_; }
38 float z() const { return z_; }
40 void set_x(float x) { x_ = x; }
41 void set_y(float y) { y_ = y; }
42 void set_z(float z) { z_ = z; }
44 void SetPoint(float x, float y, float z) {
45 x_ = x;
46 y_ = y;
47 z_ = z;
50 // Offset the point by the given vector.
51 void operator+=(const Vector3dF& v) {
52 x_ += v.x();
53 y_ += v.y();
54 z_ += v.z();
57 // Offset the point by the given vector's inverse.
58 void operator-=(const Vector3dF& v) {
59 x_ -= v.x();
60 y_ -= v.y();
61 z_ -= v.z();
64 // Returns the squared euclidean distance between two points.
65 float SquaredDistanceTo(const Point3F& other) const {
66 float dx = x_ - other.x_;
67 float dy = y_ - other.y_;
68 float dz = z_ - other.z_;
69 return dx * dx + dy * dy + dz * dz;
72 PointF AsPointF() const { return PointF(x_, y_); }
74 // Returns a string representation of 3d point.
75 std::string ToString() const;
77 private:
78 float x_;
79 float y_;
80 float z_;
82 // copy/assign are allowed.
85 inline bool operator==(const Point3F& lhs, const Point3F& rhs) {
86 return lhs.x() == rhs.x() && lhs.y() == rhs.y() && lhs.z() == rhs.z();
89 inline bool operator!=(const Point3F& lhs, const Point3F& rhs) {
90 return !(lhs == rhs);
93 // Add a vector to a point, producing a new point offset by the vector.
94 GFX_EXPORT Point3F operator+(const Point3F& lhs, const Vector3dF& rhs);
96 // Subtract a vector from a point, producing a new point offset by the vector's
97 // inverse.
98 GFX_EXPORT Point3F operator-(const Point3F& lhs, const Vector3dF& rhs);
100 // Subtract one point from another, producing a vector that represents the
101 // distances between the two points along each axis.
102 GFX_EXPORT Vector3dF operator-(const Point3F& lhs, const Point3F& rhs);
104 inline Point3F PointAtOffsetFromOrigin(const Vector3dF& offset) {
105 return Point3F(offset.x(), offset.y(), offset.z());
108 inline Point3F ScalePoint(const Point3F& p,
109 float x_scale,
110 float y_scale,
111 float z_scale) {
112 return Point3F(p.x() * x_scale, p.y() * y_scale, p.z() * z_scale);
115 inline Point3F ScalePoint(const Point3F& p, float scale) {
116 return ScalePoint(p, scale, scale, scale);
119 // This is declared here for use in gtest-based unit tests but is defined in
120 // the gfx_test_support target. Depend on that to use this in your unit test.
121 // This should not be used in production code - call ToString() instead.
122 void PrintTo(const Point3F& point, ::std::ostream* os);
124 } // namespace gfx
126 #endif // UI_GFX_GEOMETRY_POINT3_F_H_