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