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_
11 #include "ui/gfx/geometry/point_f.h"
12 #include "ui/gfx/geometry/vector3d_f.h"
13 #include "ui/gfx/gfx_export.h"
17 // A point has an x, y and z coordinate.
18 class GFX_EXPORT Point3F
{
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) {}
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
) {
50 // Offset the point by the given vector.
51 void operator+=(const Vector3dF
& v
) {
57 // Offset the point by the given vector's inverse.
58 void operator-=(const Vector3dF
& v
) {
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;
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
) {
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
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
,
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
);
126 #endif // UI_GFX_GEOMETRY_POINT3_F_H_