1 /*
2 Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <wildfox@kde.org>
3 2004, 2005 Rob Buis <buis@kde.org>
4 2005 Eric Seidel <eric@webkit.org>
5 2010 Zoltan Herczeg <zherczeg@webkit.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 aint with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21 */
22
23 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_FLOAT_POINT_3D_H_
24 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_FLOAT_POINT_3D_H_
25
26 #include "third_party/blink/renderer/platform/geometry/float_point.h"
27 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
28 #include "third_party/blink/renderer/platform/wtf/forward.h"
29 #include "third_party/skia/include/core/SkPoint3.h"
30
31 namespace gfx {
32 class Point3F;
33 }
34
35 namespace blink {
36
37 class PLATFORM_EXPORT FloatPoint3D {
38 DISALLOW_NEW();
39
40 public:
FloatPoint3D()41 constexpr FloatPoint3D() : x_(0), y_(0), z_(0) {}
42
FloatPoint3D(float x,float y,float z)43 constexpr FloatPoint3D(float x, float y, float z) : x_(x), y_(y), z_(z) {}
44
FloatPoint3D(const FloatPoint & p)45 constexpr FloatPoint3D(const FloatPoint& p) : x_(p.X()), y_(p.Y()), z_(0) {}
46
FloatPoint3D(const FloatPoint3D & p)47 constexpr FloatPoint3D(const FloatPoint3D& p)
48 : x_(p.X()), y_(p.Y()), z_(p.Z()) {}
49
50 FloatPoint3D(const gfx::Point3F&);
51
X()52 constexpr float X() const { return x_; }
SetX(float x)53 void SetX(float x) { x_ = x; }
54
Y()55 constexpr float Y() const { return y_; }
SetY(float y)56 void SetY(float y) { y_ = y; }
57
Z()58 constexpr float Z() const { return z_; }
SetZ(float z)59 void SetZ(float z) { z_ = z; }
Set(float x,float y,float z)60 void Set(float x, float y, float z) {
61 x_ = x;
62 y_ = y;
63 z_ = z;
64 }
Move(float dx,float dy,float dz)65 void Move(float dx, float dy, float dz) {
66 x_ += dx;
67 y_ += dy;
68 z_ += dz;
69 }
Scale(float sx,float sy,float sz)70 void Scale(float sx, float sy, float sz) {
71 x_ *= sx;
72 y_ *= sy;
73 z_ *= sz;
74 }
75
IsZero()76 constexpr bool IsZero() const { return !x_ && !y_ && !z_; }
77
78 void Normalize();
79
Dot(const FloatPoint3D & a)80 float Dot(const FloatPoint3D& a) const {
81 return x_ * a.X() + y_ * a.Y() + z_ * a.Z();
82 }
83
84 // Compute the angle (in radians) between this and y. If either vector is the
85 // zero vector, return an angle of 0.
86 float AngleBetween(const FloatPoint3D& y) const;
87
88 // Sets this FloatPoint3D to the cross product of the passed two.
89 // It is safe for "this" to be the same as either or both of the
90 // arguments.
Cross(const FloatPoint3D & a,const FloatPoint3D & b)91 void Cross(const FloatPoint3D& a, const FloatPoint3D& b) {
92 float x = a.Y() * b.Z() - a.Z() * b.Y();
93 float y = a.Z() * b.X() - a.X() * b.Z();
94 float z = a.X() * b.Y() - a.Y() * b.X();
95 x_ = x;
96 y_ = y;
97 z_ = z;
98 }
99
100 // Convenience function returning "this cross point" as a
101 // stack-allocated result.
Cross(const FloatPoint3D & point)102 FloatPoint3D Cross(const FloatPoint3D& point) const {
103 FloatPoint3D result;
104 result.Cross(*this, point);
105 return result;
106 }
107
LengthSquared()108 float LengthSquared() const { return this->Dot(*this); }
length()109 float length() const { return sqrtf(LengthSquared()); }
110
111 float DistanceTo(const FloatPoint3D& a) const;
112
SkPoint3()113 operator SkPoint3() const { return SkPoint3::Make(x_, y_, z_); }
Point3F()114 operator gfx::Point3F() const { return gfx::Point3F(x_, y_, z_); }
115
116 String ToString() const;
117
118 private:
119 float x_;
120 float y_;
121 float z_;
122 };
123
124 inline FloatPoint3D& operator+=(FloatPoint3D& a, const FloatPoint3D& b) {
125 a.Move(b.X(), b.Y(), b.Z());
126 return a;
127 }
128
129 inline FloatPoint3D& operator-=(FloatPoint3D& a, const FloatPoint3D& b) {
130 a.Move(-b.X(), -b.Y(), -b.Z());
131 return a;
132 }
133
134 constexpr FloatPoint3D operator+(const FloatPoint3D& a, const FloatPoint3D& b) {
135 return FloatPoint3D(a.X() + b.X(), a.Y() + b.Y(), a.Z() + b.Z());
136 }
137
138 constexpr FloatPoint3D operator-(const FloatPoint3D& a, const FloatPoint3D& b) {
139 return FloatPoint3D(a.X() - b.X(), a.Y() - b.Y(), a.Z() - b.Z());
140 }
141
142 constexpr bool operator==(const FloatPoint3D& a, const FloatPoint3D& b) {
143 return a.X() == b.X() && a.Y() == b.Y() && a.Z() == b.Z();
144 }
145
146 constexpr bool operator!=(const FloatPoint3D& a, const FloatPoint3D& b) {
147 return !(a == b);
148 }
149
150 inline float operator*(const FloatPoint3D& a, const FloatPoint3D& b) {
151 // dot product
152 return a.Dot(b);
153 }
154
155 inline FloatPoint3D operator*(float k, const FloatPoint3D& v) {
156 return FloatPoint3D(k * v.X(), k * v.Y(), k * v.Z());
157 }
158
159 inline FloatPoint3D operator*(const FloatPoint3D& v, float k) {
160 return FloatPoint3D(k * v.X(), k * v.Y(), k * v.Z());
161 }
162
DistanceTo(const FloatPoint3D & a)163 inline float FloatPoint3D::DistanceTo(const FloatPoint3D& a) const {
164 return (*this - a).length();
165 }
166
167 PLATFORM_EXPORT std::ostream& operator<<(std::ostream&, const FloatPoint3D&);
168 WTF::TextStream& operator<<(WTF::TextStream&, const FloatPoint3D&);
169
170 } // namespace blink
171
172 #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_FLOAT_POINT_3D_H_
173