Skia
2D Graphics Library
SkPoint3.h
Go to the documentation of this file.
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 
17  SkPoint3 pt;
18  pt.set(x, y, z);
19  return pt;
20  }
21 
22  SkScalar x() const { return fX; }
23  SkScalar y() const { return fY; }
24  SkScalar z() const { return fZ; }
25 
26  void set(SkScalar x, SkScalar y, SkScalar z) { fX = x; fY = y; fZ = z; }
27 
28  friend bool operator==(const SkPoint3& a, const SkPoint3& b) {
29  return a.fX == b.fX && a.fY == b.fY && a.fZ == b.fZ;
30  }
31 
32  friend bool operator!=(const SkPoint3& a, const SkPoint3& b) {
33  return !(a == b);
34  }
35 
39 
42  SkScalar length() const { return SkPoint3::Length(fX, fY, fZ); }
43 
48  bool normalize();
49 
52  SkPoint3 makeScale(SkScalar scale) const {
53  SkPoint3 p;
54  p.set(scale * fX, scale * fY, scale * fZ);
55  return p;
56  }
57 
60  void scale(SkScalar value) {
61  fX *= value;
62  fY *= value;
63  fZ *= value;
64  }
65 
69  SkPoint3 operator-() const {
70  SkPoint3 neg;
71  neg.fX = -fX;
72  neg.fY = -fY;
73  neg.fZ = -fZ;
74  return neg;
75  }
76 
80  friend SkPoint3 operator-(const SkPoint3& a, const SkPoint3& b) {
81  return { a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ };
82  }
83 
86  friend SkPoint3 operator+(const SkPoint3& a, const SkPoint3& b) {
87  return { a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ };
88  }
89 
92  void operator+=(const SkPoint3& v) {
93  fX += v.fX;
94  fY += v.fY;
95  fZ += v.fZ;
96  }
97 
100  void operator-=(const SkPoint3& v) {
101  fX -= v.fX;
102  fY -= v.fY;
103  fZ -= v.fZ;
104  }
105 
107  return { t * p.fX, t * p.fY, t * p.fZ };
108  }
109 
114  bool isFinite() const {
115  SkScalar accum = 0;
116  accum *= fX;
117  accum *= fY;
118  accum *= fZ;
119 
120  // accum is either NaN or it is finite (zero).
121  SkASSERT(0 == accum || SkScalarIsNaN(accum));
122 
123  // value==value will be true iff value is not NaN
124  // TODO: is it faster to say !accum or accum==accum?
125  return !SkScalarIsNaN(accum);
126  }
127 
130  static SkScalar DotProduct(const SkPoint3& a, const SkPoint3& b) {
131  return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ;
132  }
133 
134  SkScalar dot(const SkPoint3& vec) const {
135  return DotProduct(*this, vec);
136  }
137 
140  static SkPoint3 CrossProduct(const SkPoint3& a, const SkPoint3& b) {
141  SkPoint3 result;
142  result.fX = a.fY*b.fZ - a.fZ*b.fY;
143  result.fY = a.fZ*b.fX - a.fX*b.fZ;
144  result.fZ = a.fX*b.fY - a.fY*b.fX;
145 
146  return result;
147  }
148 
149  SkPoint3 cross(const SkPoint3& vec) const {
150  return CrossProduct(*this, vec);
151  }
152 };
153 
156 
157 #endif
SkPoint3::operator-
friend SkPoint3 operator-(const SkPoint3 &a, const SkPoint3 &b)
Returns a new point whose coordinates are the difference between a and b (i.e., a - b)
Definition: SkPoint3.h:80
SkScalarIsNaN
static bool SkScalarIsNaN(SkScalar x)
Definition: SkScalar.h:67
SkVector3
SkPoint3 SkVector3
Definition: SkPoint3.h:154
SkPoint3
Definition: SkPoint3.h:13
SkPoint3::operator*
friend SkPoint3 operator*(SkScalar t, SkPoint3 p)
Definition: SkPoint3.h:106
SkPoint3::z
SkScalar z() const
Definition: SkPoint3.h:24
SkPoint3::scale
void scale(SkScalar value)
Scale the point's coordinates by scale.
Definition: SkPoint3.h:60
SkPoint3::fY
SkScalar fY
Definition: SkPoint3.h:14
SkPoint3::Length
static SkScalar Length(SkScalar x, SkScalar y, SkScalar z)
Returns the Euclidian distance from (0,0,0) to (x,y,z)
SkPoint3::operator!=
friend bool operator!=(const SkPoint3 &a, const SkPoint3 &b)
Definition: SkPoint3.h:32
SK_API
#define SK_API
Definition: SkTypes.h:181
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkPoint3::operator+=
void operator+=(const SkPoint3 &v)
Add v's coordinates to the point's.
Definition: SkPoint3.h:92
SkPoint3::set
void set(SkScalar x, SkScalar y, SkScalar z)
Definition: SkPoint3.h:26
SkPoint3::fZ
SkScalar fZ
Definition: SkPoint3.h:14
SkPoint3::isFinite
bool isFinite() const
Returns true if fX, fY, and fZ are measurable values.
Definition: SkPoint3.h:114
SkPoint3::CrossProduct
static SkPoint3 CrossProduct(const SkPoint3 &a, const SkPoint3 &b)
Returns the cross product of a and b, treating them as 3D vectors.
Definition: SkPoint3.h:140
SkPoint.h
SkPoint3::fX
SkScalar fX
Definition: SkPoint3.h:14
SkPoint3::x
SkScalar x() const
Definition: SkPoint3.h:22
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkPoint3::operator-
SkPoint3 operator-() const
Return a new point whose X, Y and Z coordinates are the negative of the original point's.
Definition: SkPoint3.h:69
SkPoint3::operator+
friend SkPoint3 operator+(const SkPoint3 &a, const SkPoint3 &b)
Returns a new point whose coordinates are the sum of a and b (a + b)
Definition: SkPoint3.h:86
SkPoint3::Make
static SkPoint3 Make(SkScalar x, SkScalar y, SkScalar z)
Definition: SkPoint3.h:16
SkPoint3::dot
SkScalar dot(const SkPoint3 &vec) const
Definition: SkPoint3.h:134
SkPoint3::makeScale
SkPoint3 makeScale(SkScalar scale) const
Return a new point whose X, Y and Z coordinates are scaled.
Definition: SkPoint3.h:52
SkColor3f
SkPoint3 SkColor3f
Definition: SkPoint3.h:155
SkPoint3::length
SkScalar length() const
Return the Euclidian distance from (0,0,0) to the point.
Definition: SkPoint3.h:42
SkPoint3::cross
SkPoint3 cross(const SkPoint3 &vec) const
Definition: SkPoint3.h:149
SkPoint3::DotProduct
static SkScalar DotProduct(const SkPoint3 &a, const SkPoint3 &b)
Returns the dot product of a and b, treating them as 3D vectors.
Definition: SkPoint3.h:130
SkPoint3::operator-=
void operator-=(const SkPoint3 &v)
Subtract v's coordinates from the point's.
Definition: SkPoint3.h:100
SkPoint3::operator==
friend bool operator==(const SkPoint3 &a, const SkPoint3 &b)
Definition: SkPoint3.h:28
SkPoint3::y
SkScalar y() const
Definition: SkPoint3.h:23
SkPoint3::normalize
bool normalize()
Set the point (vector) to be unit-length in the same direction as it already points.