blob: 40688d80728c9f95fd441d5a2197bf971f121c62 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
2 * Copyright 2012 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#ifndef SkPathOpsPoint_DEFINED
8#define SkPathOpsPoint_DEFINED
9
10#include "SkPathOpsTypes.h"
11#include "SkPoint.h"
12
caryclark@google.coma5e55922013-05-07 18:51:31 +000013inline bool AlmostEqualUlps(const SkPoint& pt1, const SkPoint& pt2) {
14 return AlmostEqualUlps(pt1.fX, pt2.fX) && AlmostEqualUlps(pt1.fY, pt2.fY);
15}
16
caryclark@google.com07393ca2013-04-08 11:47:37 +000017struct SkDVector {
18 double fX, fY;
19
20 friend SkDPoint operator+(const SkDPoint& a, const SkDVector& b);
21
22 void operator+=(const SkDVector& v) {
23 fX += v.fX;
24 fY += v.fY;
25 }
26
27 void operator-=(const SkDVector& v) {
28 fX -= v.fX;
29 fY -= v.fY;
30 }
31
32 void operator/=(const double s) {
33 fX /= s;
34 fY /= s;
35 }
36
37 void operator*=(const double s) {
38 fX *= s;
39 fY *= s;
40 }
41
42 SkVector asSkVector() const {
43 SkVector v = {SkDoubleToScalar(fX), SkDoubleToScalar(fY)};
44 return v;
45 }
46
47 double cross(const SkDVector& a) const {
48 return fX * a.fY - fY * a.fX;
49 }
50
51 double dot(const SkDVector& a) const {
52 return fX * a.fX + fY * a.fY;
53 }
54
55 double length() const {
56 return sqrt(lengthSquared());
57 }
58
59 double lengthSquared() const {
60 return fX * fX + fY * fY;
61 }
62};
63
64struct SkDPoint {
65 double fX;
66 double fY;
67
68 void set(const SkPoint& pt) {
69 fX = pt.fX;
70 fY = pt.fY;
71 }
72
73 friend SkDVector operator-(const SkDPoint& a, const SkDPoint& b);
74
75 friend bool operator==(const SkDPoint& a, const SkDPoint& b) {
76 return a.fX == b.fX && a.fY == b.fY;
77 }
78
79 friend bool operator!=(const SkDPoint& a, const SkDPoint& b) {
80 return a.fX != b.fX || a.fY != b.fY;
81 }
82
83 void operator=(const SkPoint& pt) {
84 fX = pt.fX;
85 fY = pt.fY;
86 }
87
88
89 void operator+=(const SkDVector& v) {
90 fX += v.fX;
91 fY += v.fY;
92 }
93
94 void operator-=(const SkDVector& v) {
95 fX -= v.fX;
96 fY -= v.fY;
97 }
98
99 // note: this can not be implemented with
100 // return approximately_equal(a.fY, fY) && approximately_equal(a.fX, fX);
101 // because that will not take the magnitude of the values
102 bool approximatelyEqual(const SkDPoint& a) const {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000103 if (approximately_equal(fX, a.fX) && approximately_equal(fY, a.fY)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000104 return true;
105 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000106 if (!RoughlyEqualUlps(fX, a.fX) || !RoughlyEqualUlps(fY, a.fY)) {
107 return false;
108 }
109 double dist = distance(a); // OPTIMIZATION: can we compare against distSq instead ?
110 double tiniest = SkTMin(SkTMin(SkTMin(fX, a.fX), fY), a.fY);
111 double largest = SkTMax(SkTMax(SkTMax(fX, a.fX), fY), a.fY);
112 largest = SkTMax(largest, -tiniest);
113 return AlmostBequalUlps(largest, largest + dist); // is the dist within ULPS tolerance?
caryclark@google.com07393ca2013-04-08 11:47:37 +0000114 }
115
116 bool approximatelyEqual(const SkPoint& a) const {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000117 SkDPoint dA;
118 dA.set(a);
119 return approximatelyEqual(dA);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000120 }
121
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000122 static bool ApproximatelyEqual(const SkPoint& a, const SkPoint& b) {
123 if (approximately_equal(a.fX, b.fX) && approximately_equal(a.fY, b.fY)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000124 return true;
125 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000126 if (!RoughlyEqualUlps(a.fX, b.fX) || !RoughlyEqualUlps(a.fY, b.fY)) {
127 return false;
128 }
129 SkDPoint dA, dB;
130 dA.set(a);
131 dB.set(b);
132 double dist = dA.distance(dB); // OPTIMIZATION: can we compare against distSq instead ?
133 float tiniest = SkTMin(SkTMin(SkTMin(a.fX, b.fX), a.fY), b.fY);
134 float largest = SkTMax(SkTMax(SkTMax(a.fX, b.fX), a.fY), b.fY);
135 largest = SkTMax(largest, -tiniest);
136 return AlmostBequalUlps((double) largest, largest + dist); // is dist within ULPS tolerance?
caryclark@google.com07393ca2013-04-08 11:47:37 +0000137 }
138
139 bool approximatelyZero() const {
140 return approximately_zero(fX) && approximately_zero(fY);
141 }
142
143 SkPoint asSkPoint() const {
144 SkPoint pt = {SkDoubleToScalar(fX), SkDoubleToScalar(fY)};
145 return pt;
146 }
147
148 double distance(const SkDPoint& a) const {
149 SkDVector temp = *this - a;
150 return temp.length();
151 }
152
153 double distanceSquared(const SkDPoint& a) const {
154 SkDVector temp = *this - a;
155 return temp.lengthSquared();
156 }
157
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000158 static SkDPoint Mid(const SkDPoint& a, const SkDPoint& b) {
159 SkDPoint result;
160 result.fX = (a.fX + b.fX) / 2;
161 result.fY = (a.fY + b.fY) / 2;
162 return result;
163 }
164
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000165 bool moreRoughlyEqual(const SkDPoint& a) const {
166 if (roughly_equal(fX, a.fX) && roughly_equal(fY, a.fY)) {
167 return true;
168 }
169 double dist = distance(a); // OPTIMIZATION: can we compare against distSq instead ?
170 double tiniest = SkTMin(SkTMin(SkTMin(fX, a.fX), fY), a.fY);
171 double largest = SkTMax(SkTMax(SkTMax(fX, a.fX), fY), a.fY);
172 largest = SkTMax(largest, -tiniest);
173 return RoughlyEqualUlps(largest, largest + dist); // is the dist within ULPS tolerance?
caryclark@google.com07393ca2013-04-08 11:47:37 +0000174 }
175
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000176 bool roughlyEqual(const SkDPoint& a) const {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000177 return roughly_equal(a.fY, fY) && roughly_equal(a.fX, fX);
178 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000179
180 #ifdef SK_DEBUG
181 void dump() {
182 SkDebugf("{");
183 DebugDumpDouble(fX);
184 SkDebugf(", ");
185 DebugDumpDouble(fY);
186 SkDebugf("}");
187 }
188
189 static void DumpSkPoint(const SkPoint& pt) {
190 SkDebugf("{");
191 DebugDumpFloat(pt.fX);
192 SkDebugf(", ");
193 DebugDumpFloat(pt.fY);
194 SkDebugf("}");
195 }
196 #endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000197};
198
199#endif