Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "SkColor.h" | |
|
reed1
2015/07/10 15:46:29
why do we include this?
robertphillips
2015/07/10 16:54:24
Done.
| |
| 12 #include "SkScalar.h" | |
| 13 | |
| 14 struct 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(fX, fY, 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 /** Scale the point's coordinates by scale, writing the answer into dst. | |
| 52 It is legal for dst == this. | |
| 53 */ | |
| 54 void scale(SkScalar scale, SkPoint3* dst) const { | |
|
reed1
2015/07/10 15:46:29
as an API pattern, I think we should consider retu
robertphillips
2015/07/10 16:54:24
Done. What do you think about propagating this bac
| |
| 55 SkASSERT(dst); | |
| 56 dst->set(SkScalarMul(fX, scale), SkScalarMul(fY, scale), SkScalarMul(fZ, scale)); | |
|
reed1
2015/07/10 15:46:29
SkScalarMul is not needed!
robertphillips
2015/07/10 16:54:24
Done.
| |
| 57 } | |
| 58 | |
| 59 /** Scale the point's coordinates by scale, writing the answer back into | |
| 60 the point. | |
| 61 */ | |
| 62 void scale(SkScalar value) { this->scale(value, this); } | |
| 63 | |
| 64 /** Return a new point whose X, Y and Z coordinates are the negative of the | |
| 65 original point's | |
| 66 */ | |
| 67 SkPoint3 operator-() const { | |
| 68 SkPoint3 neg; | |
| 69 neg.fX = -fX; | |
| 70 neg.fY = -fY; | |
| 71 neg.fZ = -fZ; | |
| 72 return neg; | |
| 73 } | |
| 74 | |
| 75 /** Returns a new point whose coordinates are the difference between | |
| 76 a and b (i.e., a - b) | |
| 77 */ | |
| 78 friend SkPoint3 operator-(const SkPoint3& a, const SkPoint3& b) { | |
| 79 SkPoint3 v; | |
| 80 v.set(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ); | |
| 81 return v; | |
| 82 } | |
| 83 | |
| 84 /** Returns a new point whose coordinates are the sum of a and b (a + b) | |
| 85 */ | |
| 86 friend SkPoint3 operator+(const SkPoint3& a, const SkPoint3& b) { | |
| 87 SkPoint3 v; | |
| 88 v.set(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ); | |
| 89 return v; | |
| 90 } | |
| 91 | |
| 92 /** Returns the dot product of a and b, treating them as 3D vectors | |
| 93 */ | |
| 94 static SkScalar DotProduct(const SkPoint3& a, const SkPoint3& b) { | |
| 95 return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ; | |
| 96 } | |
| 97 | |
| 98 SkScalar dot(const SkPoint3& vec) const { | |
| 99 return DotProduct(*this, vec); | |
| 100 } | |
| 101 }; | |
| 102 | |
| 103 typedef SkPoint3 SkVector3; | |
| 104 | |
| 105 #endif | |
| OLD | NEW |