1/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkPoint3_DEFINED
9#define SkPoint3_DEFINED
10
11#include "include/core/SkPoint.h"
12#include "include/core/SkScalar.h"
13
14struct SK_API SkPoint3 {
15 SkScalar fX, fY, fZ;
16
17 static SkPoint3 Make(SkScalar x, SkScalar y, SkScalar z) {
18 SkPoint3 pt;
19 pt.set(x, y, z);
20 return pt;
21 }
22
23 SkScalar x() const { return fX; }
24 SkScalar y() const { return fY; }
25 SkScalar z() const { return fZ; }
26
27 void set(SkScalar x, SkScalar y, SkScalar z) { fX = x; fY = y; fZ = z; }
28
29 friend bool operator==(const SkPoint3& a, const SkPoint3& b) {
30 return a.fX == b.fX && a.fY == b.fY && a.fZ == b.fZ;
31 }
32
33 friend bool operator!=(const SkPoint3& a, const SkPoint3& b) {
34 return !(a == b);
35 }
36
37 /** Returns the Euclidian distance from (0,0,0) to (x,y,z)
38 */
39 static SkScalar Length(SkScalar x, SkScalar y, SkScalar z);
40
41 /** Return the Euclidian distance from (0,0,0) to the point
42 */
43 SkScalar length() const { return SkPoint3::Length(x: fX, y: fY, z: fZ); }
44
45 /** Set the point (vector) to be unit-length in the same direction as it
46 already points. If the point has a degenerate length (i.e., nearly 0)
47 then set it to (0,0,0) and return false; otherwise return true.
48 */
49 bool normalize();
50
51 /** Return a new point whose X, Y and Z coordinates are scaled.
52 */
53 SkPoint3 makeScale(SkScalar scale) const {
54 SkPoint3 p;
55 p.set(x: scale * fX, y: scale * fY, z: scale * fZ);
56 return p;
57 }
58
59 /** Scale the point's coordinates by scale.
60 */
61 void scale(SkScalar value) {
62 fX *= value;
63 fY *= value;
64 fZ *= value;
65 }
66
67 /** Return a new point whose X, Y and Z coordinates are the negative of the
68 original point's
69 */
70 SkPoint3 operator-() const {
71 SkPoint3 neg;
72 neg.fX = -fX;
73 neg.fY = -fY;
74 neg.fZ = -fZ;
75 return neg;
76 }
77
78 /** Returns a new point whose coordinates are the difference between
79 a and b (i.e., a - b)
80 */
81 friend SkPoint3 operator-(const SkPoint3& a, const SkPoint3& b) {
82 return { .fX: a.fX - b.fX, .fY: a.fY - b.fY, .fZ: a.fZ - b.fZ };
83 }
84
85 /** Returns a new point whose coordinates are the sum of a and b (a + b)
86 */
87 friend SkPoint3 operator+(const SkPoint3& a, const SkPoint3& b) {
88 return { .fX: a.fX + b.fX, .fY: a.fY + b.fY, .fZ: a.fZ + b.fZ };
89 }
90
91 /** Add v's coordinates to the point's
92 */
93 void operator+=(const SkPoint3& v) {
94 fX += v.fX;
95 fY += v.fY;
96 fZ += v.fZ;
97 }
98
99 /** Subtract v's coordinates from the point's
100 */
101 void operator-=(const SkPoint3& v) {
102 fX -= v.fX;
103 fY -= v.fY;
104 fZ -= v.fZ;
105 }
106
107 friend SkPoint3 operator*(SkScalar t, SkPoint3 p) {
108 return { .fX: t * p.fX, .fY: t * p.fY, .fZ: t * p.fZ };
109 }
110
111 /** Returns true if fX, fY, and fZ are measurable values.
112
113 @return true for values other than infinities and NaN
114 */
115 bool isFinite() const {
116 SkScalar accum = 0;
117 accum *= fX;
118 accum *= fY;
119 accum *= fZ;
120
121 // accum is either NaN or it is finite (zero).
122 SkASSERT(0 == accum || SkScalarIsNaN(accum));
123
124 // value==value will be true iff value is not NaN
125 // TODO: is it faster to say !accum or accum==accum?
126 return !SkScalarIsNaN(x: accum);
127 }
128
129 /** Returns the dot product of a and b, treating them as 3D vectors
130 */
131 static SkScalar DotProduct(const SkPoint3& a, const SkPoint3& b) {
132 return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ;
133 }
134
135 SkScalar dot(const SkPoint3& vec) const {
136 return DotProduct(a: *this, b: vec);
137 }
138
139 /** Returns the cross product of a and b, treating them as 3D vectors
140 */
141 static SkPoint3 CrossProduct(const SkPoint3& a, const SkPoint3& b) {
142 SkPoint3 result;
143 result.fX = a.fY*b.fZ - a.fZ*b.fY;
144 result.fY = a.fZ*b.fX - a.fX*b.fZ;
145 result.fZ = a.fX*b.fY - a.fY*b.fX;
146
147 return result;
148 }
149
150 SkPoint3 cross(const SkPoint3& vec) const {
151 return CrossProduct(a: *this, b: vec);
152 }
153};
154
155typedef SkPoint3 SkVector3;
156typedef SkPoint3 SkColor3f;
157
158#endif
159

source code of flutter_engine/third_party/skia/include/core/SkPoint3.h