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