Chromium Code Reviews| Index: include/effects/SkPoint3.h |
| diff --git a/include/effects/SkPoint3.h b/include/effects/SkPoint3.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..814e3e3a3d0834f9cbebf5b04b970abb65924f05 |
| --- /dev/null |
| +++ b/include/effects/SkPoint3.h |
| @@ -0,0 +1,105 @@ |
| +/* |
| + * Copyright 2015 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#ifndef SkPoint3_DEFINED |
| +#define SkPoint3_DEFINED |
| + |
| +#include "SkColor.h" |
|
reed1
2015/07/10 15:46:29
why do we include this?
robertphillips
2015/07/10 16:54:24
Done.
|
| +#include "SkScalar.h" |
| + |
| +struct SK_API SkPoint3 { |
| + SkScalar fX, fY, fZ; |
| + |
| + static SkPoint3 Make(SkScalar x, SkScalar y, SkScalar z) { |
| + SkPoint3 pt; |
| + pt.set(x, y, z); |
| + return pt; |
| + } |
| + |
| + SkScalar x() const { return fX; } |
| + SkScalar y() const { return fY; } |
| + SkScalar z() const { return fZ; } |
| + |
| + void set(SkScalar x, SkScalar y, SkScalar z) { fX = x; fY = y; fZ = z; } |
| + |
| + friend bool operator==(const SkPoint3& a, const SkPoint3& b) { |
| + return a.fX == b.fX && a.fY == b.fY && a.fZ == b.fZ; |
| + } |
| + |
| + friend bool operator!=(const SkPoint3& a, const SkPoint3& b) { |
| + return !(a == b); |
| + } |
| + |
| + /** Returns the Euclidian distance from (0,0,0) to (x,y,z) |
| + */ |
| + static SkScalar Length(SkScalar x, SkScalar y, SkScalar z); |
| + |
| + /** Return the Euclidian distance from (0,0,0) to the point |
| + */ |
| + SkScalar length() const { return SkPoint3::Length(fX, fY, fZ); } |
| + |
| + /** Set the point (vector) to be unit-length in the same direction as it |
| + already points. If the point has a degenerate length (i.e., nearly 0) |
| + then set it to (0,0,0) and return false; otherwise return true. |
| + */ |
| + bool normalize(); |
| + |
| + /** Scale the point's coordinates by scale, writing the answer into dst. |
| + It is legal for dst == this. |
| + */ |
| + 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
|
| + SkASSERT(dst); |
| + 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.
|
| + } |
| + |
| + /** Scale the point's coordinates by scale, writing the answer back into |
| + the point. |
| + */ |
| + void scale(SkScalar value) { this->scale(value, this); } |
| + |
| + /** Return a new point whose X, Y and Z coordinates are the negative of the |
| + original point's |
| + */ |
| + SkPoint3 operator-() const { |
| + SkPoint3 neg; |
| + neg.fX = -fX; |
| + neg.fY = -fY; |
| + neg.fZ = -fZ; |
| + return neg; |
| + } |
| + |
| + /** Returns a new point whose coordinates are the difference between |
| + a and b (i.e., a - b) |
| + */ |
| + friend SkPoint3 operator-(const SkPoint3& a, const SkPoint3& b) { |
| + SkPoint3 v; |
| + v.set(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ); |
| + return v; |
| + } |
| + |
| + /** Returns a new point whose coordinates are the sum of a and b (a + b) |
| + */ |
| + friend SkPoint3 operator+(const SkPoint3& a, const SkPoint3& b) { |
| + SkPoint3 v; |
| + v.set(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ); |
| + return v; |
| + } |
| + |
| + /** Returns the dot product of a and b, treating them as 3D vectors |
| + */ |
| + static SkScalar DotProduct(const SkPoint3& a, const SkPoint3& b) { |
| + return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ; |
| + } |
| + |
| + SkScalar dot(const SkPoint3& vec) const { |
| + return DotProduct(*this, vec); |
| + } |
| +}; |
| + |
| +typedef SkPoint3 SkVector3; |
| + |
| +#endif |