OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #ifndef SkPathOpsPoint_DEFINED | 7 #ifndef SkPathOpsPoint_DEFINED |
8 #define SkPathOpsPoint_DEFINED | 8 #define SkPathOpsPoint_DEFINED |
9 | 9 |
10 #include "SkPathOpsTypes.h" | 10 #include "SkPathOpsTypes.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 93 |
94 void operator-=(const SkDVector& v) { | 94 void operator-=(const SkDVector& v) { |
95 fX -= v.fX; | 95 fX -= v.fX; |
96 fY -= v.fY; | 96 fY -= v.fY; |
97 } | 97 } |
98 | 98 |
99 // note: this can not be implemented with | 99 // note: this can not be implemented with |
100 // return approximately_equal(a.fY, fY) && approximately_equal(a.fX, fX); | 100 // return approximately_equal(a.fY, fY) && approximately_equal(a.fX, fX); |
101 // because that will not take the magnitude of the values | 101 // because that will not take the magnitude of the values |
102 bool approximatelyEqual(const SkDPoint& a) const { | 102 bool approximatelyEqual(const SkDPoint& a) const { |
103 double denom = SkTMax(fabs(fX), SkTMax(fabs(fY), | 103 if (approximately_equal(fX, a.fX) && approximately_equal(fY, a.fY)) { |
104 SkTMax(fabs(a.fX), fabs(a.fY)))); | |
105 if (precisely_zero(denom)) { | |
106 return true; | 104 return true; |
107 } | 105 } |
108 double inv = 1 / denom; | 106 if (!RoughlyEqualUlps(fX, a.fX) || !RoughlyEqualUlps(fY, a.fY)) { |
109 return approximately_equal(fX * inv, a.fX * inv) | 107 return false; |
110 && approximately_equal(fY * inv, a.fY * inv); | 108 } |
| 109 double dist = distance(a); // OPTIMIZATION: can we compare against dist
Sq 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? |
111 } | 114 } |
112 | 115 |
113 bool approximatelyEqual(const SkPoint& a) const { | 116 bool approximatelyEqual(const SkPoint& a) const { |
114 return AlmostEqualUlps(SkDoubleToScalar(fX), a.fX) | 117 SkDPoint dA; |
115 && AlmostEqualUlps(SkDoubleToScalar(fY), a.fY); | 118 dA.set(a); |
| 119 return approximatelyEqual(dA); |
116 } | 120 } |
117 | 121 |
118 bool approximatelyEqualHalf(const SkDPoint& a) const { | 122 static bool ApproximatelyEqual(const SkPoint& a, const SkPoint& b) { |
119 double denom = SkTMax(fabs(fX), SkTMax(fabs(fY), | 123 if (approximately_equal(a.fX, b.fX) && approximately_equal(a.fY, b.fY))
{ |
120 SkTMax(fabs(a.fX), fabs(a.fY)))); | |
121 if (denom == 0) { | |
122 return true; | 124 return true; |
123 } | 125 } |
124 double inv = 1 / denom; | 126 if (!RoughlyEqualUlps(a.fX, b.fX) || !RoughlyEqualUlps(a.fY, b.fY)) { |
125 return approximately_equal_half(fX * inv, a.fX * inv) | 127 return false; |
126 && approximately_equal_half(fY * inv, a.fY * inv); | 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 wi
thin ULPS tolerance? |
127 } | 137 } |
128 | 138 |
129 bool approximatelyZero() const { | 139 bool approximatelyZero() const { |
130 return approximately_zero(fX) && approximately_zero(fY); | 140 return approximately_zero(fX) && approximately_zero(fY); |
131 } | 141 } |
132 | 142 |
133 SkPoint asSkPoint() const { | 143 SkPoint asSkPoint() const { |
134 SkPoint pt = {SkDoubleToScalar(fX), SkDoubleToScalar(fY)}; | 144 SkPoint pt = {SkDoubleToScalar(fX), SkDoubleToScalar(fY)}; |
135 return pt; | 145 return pt; |
136 } | 146 } |
137 | 147 |
138 double distance(const SkDPoint& a) const { | 148 double distance(const SkDPoint& a) const { |
139 SkDVector temp = *this - a; | 149 SkDVector temp = *this - a; |
140 return temp.length(); | 150 return temp.length(); |
141 } | 151 } |
142 | 152 |
143 double distanceSquared(const SkDPoint& a) const { | 153 double distanceSquared(const SkDPoint& a) const { |
144 SkDVector temp = *this - a; | 154 SkDVector temp = *this - a; |
145 return temp.lengthSquared(); | 155 return temp.lengthSquared(); |
146 } | 156 } |
147 | 157 |
148 static SkDPoint Mid(const SkDPoint& a, const SkDPoint& b) { | 158 static SkDPoint Mid(const SkDPoint& a, const SkDPoint& b) { |
149 SkDPoint result; | 159 SkDPoint result; |
150 result.fX = (a.fX + b.fX) / 2; | 160 result.fX = (a.fX + b.fX) / 2; |
151 result.fY = (a.fY + b.fY) / 2; | 161 result.fY = (a.fY + b.fY) / 2; |
152 return result; | 162 return result; |
153 } | 163 } |
154 | 164 |
155 double moreRoughlyEqual(const SkDPoint& a) const { | 165 bool moreRoughlyEqual(const SkDPoint& a) const { |
156 return more_roughly_equal(a.fY, fY) && more_roughly_equal(a.fX, fX); | 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 dist
Sq 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? |
157 } | 174 } |
158 | 175 |
159 double roughlyEqual(const SkDPoint& a) const { | 176 bool roughlyEqual(const SkDPoint& a) const { |
160 return roughly_equal(a.fY, fY) && roughly_equal(a.fX, fX); | 177 return roughly_equal(a.fY, fY) && roughly_equal(a.fX, fX); |
161 } | 178 } |
162 | 179 |
163 #ifdef SK_DEBUG | 180 #ifdef SK_DEBUG |
164 void dump() { | 181 void dump() { |
165 SkDebugf("{"); | 182 SkDebugf("{"); |
166 DebugDumpDouble(fX); | 183 DebugDumpDouble(fX); |
167 SkDebugf(", "); | 184 SkDebugf(", "); |
168 DebugDumpDouble(fY); | 185 DebugDumpDouble(fY); |
169 SkDebugf("}"); | 186 SkDebugf("}"); |
170 } | 187 } |
171 | 188 |
172 static void DumpSkPoint(const SkPoint& pt) { | 189 static void DumpSkPoint(const SkPoint& pt) { |
173 SkDebugf("{"); | 190 SkDebugf("{"); |
174 DebugDumpFloat(pt.fX); | 191 DebugDumpFloat(pt.fX); |
175 SkDebugf(", "); | 192 SkDebugf(", "); |
176 DebugDumpFloat(pt.fY); | 193 DebugDumpFloat(pt.fY); |
177 SkDebugf("}"); | 194 SkDebugf("}"); |
178 } | 195 } |
179 #endif | 196 #endif |
180 }; | 197 }; |
181 | 198 |
182 #endif | 199 #endif |
OLD | NEW |