• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 struct SK_API SkPoint3 {
14     SkScalar fX, fY, fZ;
15 
MakeSkPoint316     static SkPoint3 Make(SkScalar x, SkScalar y, SkScalar z) {
17         SkPoint3 pt;
18         pt.set(x, y, z);
19         return pt;
20     }
21 
dumpSkPoint322     void dump(std::string &desc, int depth) const
23     {
24         std::string split(depth, '\t');
25         desc += split + "\n SkPoint3:{ \n";
26         desc += split + "\t fX: " + std::to_string(fX) + "\n";
27         desc += split + "\t fY: " + std::to_string(fY) + "\n";
28         desc += split + "\t fZ: " + std::to_string(fZ) + "\n";
29         desc += split + "}\n";
30     }
31 
xSkPoint332     SkScalar x() const { return fX; }
ySkPoint333     SkScalar y() const { return fY; }
zSkPoint334     SkScalar z() const { return fZ; }
35 
setSkPoint336     void set(SkScalar x, SkScalar y, SkScalar z) { fX = x; fY = y; fZ = z; }
37 
38     friend bool operator==(const SkPoint3& a, const SkPoint3& b) {
39         return a.fX == b.fX && a.fY == b.fY && a.fZ == b.fZ;
40     }
41 
42     friend bool operator!=(const SkPoint3& a, const SkPoint3& b) {
43         return !(a == b);
44     }
45 
46     /** Returns the Euclidian distance from (0,0,0) to (x,y,z)
47     */
48     static SkScalar Length(SkScalar x, SkScalar y, SkScalar z);
49 
50     /** Return the Euclidian distance from (0,0,0) to the point
51     */
lengthSkPoint352     SkScalar length() const { return SkPoint3::Length(fX, fY, fZ); }
53 
54     /** Set the point (vector) to be unit-length in the same direction as it
55         already points.  If the point has a degenerate length (i.e., nearly 0)
56         then set it to (0,0,0) and return false; otherwise return true.
57     */
58     bool normalize();
59 
60     /** Return a new point whose X, Y and Z coordinates are scaled.
61     */
makeScaleSkPoint362     SkPoint3 makeScale(SkScalar scale) const {
63         SkPoint3 p;
64         p.set(scale * fX, scale * fY, scale * fZ);
65         return p;
66     }
67 
68     /** Scale the point's coordinates by scale.
69     */
scaleSkPoint370     void scale(SkScalar value) {
71         fX *= value;
72         fY *= value;
73         fZ *= value;
74     }
75 
76     /** Return a new point whose X, Y and Z coordinates are the negative of the
77         original point's
78     */
79     SkPoint3 operator-() const {
80         SkPoint3 neg;
81         neg.fX = -fX;
82         neg.fY = -fY;
83         neg.fZ = -fZ;
84         return neg;
85     }
86 
87     /** Returns a new point whose coordinates are the difference between
88         a and b (i.e., a - b)
89     */
90     friend SkPoint3 operator-(const SkPoint3& a, const SkPoint3& b) {
91         SkPoint3 v;
92         v.set(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ);
93         return v;
94     }
95 
96     /** Returns a new point whose coordinates are the sum of a and b (a + b)
97     */
98     friend SkPoint3 operator+(const SkPoint3& a, const SkPoint3& b) {
99         SkPoint3 v;
100         v.set(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ);
101         return v;
102     }
103 
104     /** Add v's coordinates to the point's
105     */
106     void operator+=(const SkPoint3& v) {
107         fX += v.fX;
108         fY += v.fY;
109         fZ += v.fZ;
110     }
111 
112     /** Subtract v's coordinates from the point's
113     */
114     void operator-=(const SkPoint3& v) {
115         fX -= v.fX;
116         fY -= v.fY;
117         fZ -= v.fZ;
118     }
119 
120     /** Returns true if fX, fY, and fZ are measurable values.
121 
122      @return  true for values other than infinities and NaN
123      */
isFiniteSkPoint3124     bool isFinite() const {
125         SkScalar accum = 0;
126         accum *= fX;
127         accum *= fY;
128         accum *= fZ;
129 
130         // accum is either NaN or it is finite (zero).
131         SkASSERT(0 == accum || SkScalarIsNaN(accum));
132 
133         // value==value will be true iff value is not NaN
134         // TODO: is it faster to say !accum or accum==accum?
135         return !SkScalarIsNaN(accum);
136     }
137 
138     /** Returns the dot product of a and b, treating them as 3D vectors
139     */
DotProductSkPoint3140     static SkScalar DotProduct(const SkPoint3& a, const SkPoint3& b) {
141         return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ;
142     }
143 
dotSkPoint3144     SkScalar dot(const SkPoint3& vec) const {
145         return DotProduct(*this, vec);
146     }
147 
148     /** Returns the cross product of a and b, treating them as 3D vectors
149     */
CrossProductSkPoint3150     static SkPoint3 CrossProduct(const SkPoint3& a, const SkPoint3& b) {
151         SkPoint3 result;
152         result.fX = a.fY*b.fZ - a.fZ*b.fY;
153         result.fY = a.fZ*b.fX - a.fX*b.fZ;
154         result.fZ = a.fX*b.fY - a.fY*b.fX;
155 
156         return result;
157     }
158 
crossSkPoint3159     SkPoint3 cross(const SkPoint3& vec) const {
160         return CrossProduct(*this, vec);
161     }
162 };
163 
164 typedef SkPoint3 SkVector3;
165 typedef SkPoint3 SkColor3f;
166 
167 #endif
168