blob: f9b727b9983d9300001623b09b695655d82a785c [file] [log] [blame]
robertphillips3d32d762015-07-13 13:16:44 -07001/*
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 "SkScalar.h"
12
13struct SK_API SkPoint3 {
14 SkScalar fX, fY, fZ;
15
16#ifdef SK_LEGACY_SKPOINT3_CTORS
17 // TODO: remove these - they are needed for Chromium staging
18 SkPoint3() {}
19 SkPoint3(SkScalar x, SkScalar y, SkScalar z) : fX(x), fY(y), fZ(z) {}
20#endif
21
22 static SkPoint3 Make(SkScalar x, SkScalar y, SkScalar z) {
23 SkPoint3 pt;
24 pt.set(x, y, z);
25 return pt;
26 }
27
28 SkScalar x() const { return fX; }
29 SkScalar y() const { return fY; }
30 SkScalar z() const { return fZ; }
31
32 void set(SkScalar x, SkScalar y, SkScalar z) { fX = x; fY = y; fZ = z; }
33
34 friend bool operator==(const SkPoint3& a, const SkPoint3& b) {
35 return a.fX == b.fX && a.fY == b.fY && a.fZ == b.fZ;
36 }
37
38 friend bool operator!=(const SkPoint3& a, const SkPoint3& b) {
39 return !(a == b);
40 }
41
42 /** Returns the Euclidian distance from (0,0,0) to (x,y,z)
43 */
44 static SkScalar Length(SkScalar x, SkScalar y, SkScalar z);
45
46 /** Return the Euclidian distance from (0,0,0) to the point
47 */
48 SkScalar length() const { return SkPoint3::Length(fX, fY, fZ); }
49
50 /** Set the point (vector) to be unit-length in the same direction as it
51 already points. If the point has a degenerate length (i.e., nearly 0)
52 then set it to (0,0,0) and return false; otherwise return true.
53 */
54 bool normalize();
55
56 /** Return a new point whose X, Y and Z coordinates are scaled.
57 */
58 SkPoint3 makeScale(SkScalar scale) const {
59 SkPoint3 p;
60 p.set(scale * fX, scale * fY, scale * fZ);
61 return p;
62 }
63
64 /** Scale the point's coordinates by scale.
65 */
66 void scale(SkScalar value) {
67 fX *= value;
68 fY *= value;
69 fZ *= value;
70 }
71
72 /** Return a new point whose X, Y and Z coordinates are the negative of the
73 original point's
74 */
75 SkPoint3 operator-() const {
76 SkPoint3 neg;
77 neg.fX = -fX;
78 neg.fY = -fY;
79 neg.fZ = -fZ;
80 return neg;
81 }
82
83 /** Returns a new point whose coordinates are the difference between
84 a and b (i.e., 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 a new point whose coordinates are the sum of a and b (a + b)
93 */
94 friend SkPoint3 operator+(const SkPoint3& a, const SkPoint3& b) {
95 SkPoint3 v;
96 v.set(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ);
97 return v;
98 }
99
100 /** Returns the dot product of a and b, treating them as 3D vectors
101 */
102 static SkScalar DotProduct(const SkPoint3& a, const SkPoint3& b) {
103 return a.fX * b.fX + a.fY * b.fY + a.fZ * b.fZ;
104 }
105
106 SkScalar dot(const SkPoint3& vec) const {
107 return DotProduct(*this, vec);
108 }
109};
110
111typedef SkPoint3 SkVector3;
112
113#endif