blob: c067d1d3e26fc0160c476434711916244106f8c7 [file] [log] [blame]
caryclark@google.com818b0cc2013-04-08 11:50:46 +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#include "PathOpsTestCommon.h"
caryclark@google.com8d0a5242013-07-16 16:11:16 +00008#include "SkPathOpsBounds.h"
caryclark1049f122015-04-20 08:31:59 -07009#include "SkPathOpsConic.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000010#include "SkPathOpsCubic.h"
caryclark@google.com8d0a5242013-07-16 16:11:16 +000011#include "SkPathOpsLine.h"
12#include "SkPathOpsQuad.h"
Cary Clark0949bee2018-03-19 09:42:00 -040013#include "SkPathOpsTSect.h"
caryclark54359292015-03-26 07:52:43 -070014#include "SkReduceOrder.h"
15#include "SkTSort.h"
16
Ben Wagnerf08d1d02018-06-18 15:11:00 -040017#include <utility>
18
caryclark54359292015-03-26 07:52:43 -070019static double calc_t_div(const SkDCubic& cubic, double precision, double start) {
20 const double adjust = sqrt(3.) / 36;
21 SkDCubic sub;
22 const SkDCubic* cPtr;
23 if (start == 0) {
24 cPtr = &cubic;
25 } else {
26 // OPTIMIZE: special-case half-split ?
27 sub = cubic.subDivide(start, 1);
28 cPtr = &sub;
29 }
30 const SkDCubic& c = *cPtr;
31 double dx = c[3].fX - 3 * (c[2].fX - c[1].fX) - c[0].fX;
32 double dy = c[3].fY - 3 * (c[2].fY - c[1].fY) - c[0].fY;
33 double dist = sqrt(dx * dx + dy * dy);
34 double tDiv3 = precision / (adjust * dist);
35 double t = SkDCubeRoot(tDiv3);
36 if (start > 0) {
37 t = start + (1 - start) * t;
38 }
39 return t;
40}
41
42static bool add_simple_ts(const SkDCubic& cubic, double precision, SkTArray<double, true>* ts) {
43 double tDiv = calc_t_div(cubic, precision, 0);
44 if (tDiv >= 1) {
45 return true;
46 }
47 if (tDiv >= 0.5) {
48 ts->push_back(0.5);
49 return true;
50 }
51 return false;
52}
53
54static void addTs(const SkDCubic& cubic, double precision, double start, double end,
55 SkTArray<double, true>* ts) {
56 double tDiv = calc_t_div(cubic, precision, 0);
57 double parts = ceil(1.0 / tDiv);
58 for (double index = 0; index < parts; ++index) {
59 double newT = start + (index / parts) * (end - start);
60 if (newT > 0 && newT < 1) {
61 ts->push_back(newT);
62 }
63 }
64}
65
66static void toQuadraticTs(const SkDCubic* cubic, double precision, SkTArray<double, true>* ts) {
67 SkReduceOrder reducer;
68 int order = reducer.reduce(*cubic, SkReduceOrder::kAllow_Quadratics);
69 if (order < 3) {
70 return;
71 }
72 double inflectT[5];
73 int inflections = cubic->findInflections(inflectT);
74 SkASSERT(inflections <= 2);
75 if (!cubic->endsAreExtremaInXOrY()) {
76 inflections += cubic->findMaxCurvature(&inflectT[inflections]);
77 SkASSERT(inflections <= 5);
78 }
79 SkTQSort<double>(inflectT, &inflectT[inflections - 1]);
80 // OPTIMIZATION: is this filtering common enough that it needs to be pulled out into its
81 // own subroutine?
82 while (inflections && approximately_less_than_zero(inflectT[0])) {
83 memmove(inflectT, &inflectT[1], sizeof(inflectT[0]) * --inflections);
84 }
85 int start = 0;
86 int next = 1;
87 while (next < inflections) {
88 if (!approximately_equal(inflectT[start], inflectT[next])) {
89 ++start;
90 ++next;
91 continue;
92 }
93 memmove(&inflectT[start], &inflectT[next], sizeof(inflectT[0]) * (--inflections - start));
94 }
95
96 while (inflections && approximately_greater_than_one(inflectT[inflections - 1])) {
97 --inflections;
98 }
99 SkDCubicPair pair;
100 if (inflections == 1) {
101 pair = cubic->chopAt(inflectT[0]);
102 int orderP1 = reducer.reduce(pair.first(), SkReduceOrder::kNo_Quadratics);
103 if (orderP1 < 2) {
104 --inflections;
105 } else {
106 int orderP2 = reducer.reduce(pair.second(), SkReduceOrder::kNo_Quadratics);
107 if (orderP2 < 2) {
108 --inflections;
109 }
110 }
111 }
112 if (inflections == 0 && add_simple_ts(*cubic, precision, ts)) {
113 return;
114 }
115 if (inflections == 1) {
116 pair = cubic->chopAt(inflectT[0]);
117 addTs(pair.first(), precision, 0, inflectT[0], ts);
118 addTs(pair.second(), precision, inflectT[0], 1, ts);
119 return;
120 }
121 if (inflections > 1) {
122 SkDCubic part = cubic->subDivide(0, inflectT[0]);
123 addTs(part, precision, 0, inflectT[0], ts);
124 int last = inflections - 1;
125 for (int idx = 0; idx < last; ++idx) {
126 part = cubic->subDivide(inflectT[idx], inflectT[idx + 1]);
127 addTs(part, precision, inflectT[idx], inflectT[idx + 1], ts);
128 }
129 part = cubic->subDivide(inflectT[last], 1);
130 addTs(part, precision, inflectT[last], 1, ts);
131 return;
132 }
133 addTs(*cubic, precision, 0, 1, ts);
134}
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000135
caryclark@google.comd892bd82013-06-17 14:10:36 +0000136void CubicToQuads(const SkDCubic& cubic, double precision, SkTArray<SkDQuad, true>& quads) {
137 SkTArray<double, true> ts;
caryclark54359292015-03-26 07:52:43 -0700138 toQuadraticTs(&cubic, precision, &ts);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000139 if (ts.count() <= 0) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000140 SkDQuad quad = cubic.toQuad();
caryclark@google.comd892bd82013-06-17 14:10:36 +0000141 quads.push_back(quad);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000142 return;
143 }
144 double tStart = 0;
145 for (int i1 = 0; i1 <= ts.count(); ++i1) {
146 const double tEnd = i1 < ts.count() ? ts[i1] : 1;
caryclarkaec25102015-04-29 08:28:30 -0700147 SkDRect bounds;
148 bounds.setBounds(cubic);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000149 SkDCubic part = cubic.subDivide(tStart, tEnd);
150 SkDQuad quad = part.toQuad();
caryclarkaec25102015-04-29 08:28:30 -0700151 if (quad[1].fX < bounds.fLeft) {
152 quad[1].fX = bounds.fLeft;
153 } else if (quad[1].fX > bounds.fRight) {
154 quad[1].fX = bounds.fRight;
155 }
156 if (quad[1].fY < bounds.fTop) {
157 quad[1].fY = bounds.fTop;
158 } else if (quad[1].fY > bounds.fBottom) {
159 quad[1].fY = bounds.fBottom;
160 }
caryclark@google.comd892bd82013-06-17 14:10:36 +0000161 quads.push_back(quad);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000162 tStart = tEnd;
163 }
164}
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000165
caryclarke4097e32014-06-18 07:24:19 -0700166void CubicPathToQuads(const SkPath& cubicPath, SkPath* quadPath) {
167 quadPath->reset();
168 SkDCubic cubic;
169 SkTArray<SkDQuad, true> quads;
170 SkPath::RawIter iter(cubicPath);
171 uint8_t verb;
172 SkPoint pts[4];
173 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
174 switch (verb) {
175 case SkPath::kMove_Verb:
176 quadPath->moveTo(pts[0].fX, pts[0].fY);
177 continue;
178 case SkPath::kLine_Verb:
179 quadPath->lineTo(pts[1].fX, pts[1].fY);
180 break;
181 case SkPath::kQuad_Verb:
182 quadPath->quadTo(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
183 break;
184 case SkPath::kCubic_Verb:
185 quads.reset();
186 cubic.set(pts);
187 CubicToQuads(cubic, cubic.calcPrecision(), quads);
188 for (int index = 0; index < quads.count(); ++index) {
189 SkPoint qPts[2] = {
190 quads[index][1].asSkPoint(),
191 quads[index][2].asSkPoint()
192 };
193 quadPath->quadTo(qPts[0].fX, qPts[0].fY, qPts[1].fX, qPts[1].fY);
194 }
195 break;
196 case SkPath::kClose_Verb:
197 quadPath->close();
198 break;
199 default:
200 SkDEBUGFAIL("bad verb");
201 return;
202 }
203 }
204}
205
206void CubicPathToSimple(const SkPath& cubicPath, SkPath* simplePath) {
207 simplePath->reset();
208 SkDCubic cubic;
209 SkPath::RawIter iter(cubicPath);
210 uint8_t verb;
211 SkPoint pts[4];
212 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
213 switch (verb) {
214 case SkPath::kMove_Verb:
215 simplePath->moveTo(pts[0].fX, pts[0].fY);
216 continue;
217 case SkPath::kLine_Verb:
218 simplePath->lineTo(pts[1].fX, pts[1].fY);
219 break;
220 case SkPath::kQuad_Verb:
221 simplePath->quadTo(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
222 break;
223 case SkPath::kCubic_Verb: {
224 cubic.set(pts);
225 double tInflects[2];
226 int inflections = cubic.findInflections(tInflects);
227 if (inflections > 1 && tInflects[0] > tInflects[1]) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400228 using std::swap;
229 swap(tInflects[0], tInflects[1]);
caryclarke4097e32014-06-18 07:24:19 -0700230 }
231 double lo = 0;
232 for (int index = 0; index <= inflections; ++index) {
233 double hi = index < inflections ? tInflects[index] : 1;
234 SkDCubic part = cubic.subDivide(lo, hi);
235 SkPoint cPts[3];
236 cPts[0] = part[1].asSkPoint();
237 cPts[1] = part[2].asSkPoint();
238 cPts[2] = part[3].asSkPoint();
239 simplePath->cubicTo(cPts[0].fX, cPts[0].fY, cPts[1].fX, cPts[1].fY,
240 cPts[2].fX, cPts[2].fY);
241 lo = hi;
242 }
243 break;
halcanary9d524f22016-03-29 09:03:52 -0700244 }
caryclarke4097e32014-06-18 07:24:19 -0700245 case SkPath::kClose_Verb:
246 simplePath->close();
247 break;
248 default:
249 SkDEBUGFAIL("bad verb");
250 return;
251 }
252 }
253}
254
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000255bool ValidBounds(const SkPathOpsBounds& bounds) {
256 if (SkScalarIsNaN(bounds.fLeft)) {
257 return false;
258 }
259 if (SkScalarIsNaN(bounds.fTop)) {
260 return false;
261 }
262 if (SkScalarIsNaN(bounds.fRight)) {
263 return false;
264 }
265 return !SkScalarIsNaN(bounds.fBottom);
266}
267
caryclark1049f122015-04-20 08:31:59 -0700268bool ValidConic(const SkDConic& conic) {
269 for (int index = 0; index < SkDConic::kPointCount; ++index) {
270 if (!ValidPoint(conic[index])) {
271 return false;
272 }
273 }
274 if (SkDoubleIsNaN(conic.fWeight)) {
275 return false;
276 }
277 return true;
278}
279
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000280bool ValidCubic(const SkDCubic& cubic) {
281 for (int index = 0; index < 4; ++index) {
282 if (!ValidPoint(cubic[index])) {
283 return false;
284 }
285 }
286 return true;
287}
288
289bool ValidLine(const SkDLine& line) {
290 for (int index = 0; index < 2; ++index) {
291 if (!ValidPoint(line[index])) {
292 return false;
293 }
294 }
295 return true;
296}
297
298bool ValidPoint(const SkDPoint& pt) {
299 if (SkDoubleIsNaN(pt.fX)) {
300 return false;
301 }
skia.committer@gmail.comeebe6f42013-07-17 07:01:13 +0000302 return !SkDoubleIsNaN(pt.fY);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000303}
304
305bool ValidPoints(const SkPoint* pts, int count) {
306 for (int index = 0; index < count; ++index) {
307 if (SkScalarIsNaN(pts[index].fX)) {
308 return false;
309 }
310 if (SkScalarIsNaN(pts[index].fY)) {
311 return false;
312 }
313 }
314 return true;
315}
316
317bool ValidQuad(const SkDQuad& quad) {
318 for (int index = 0; index < 3; ++index) {
319 if (!ValidPoint(quad[index])) {
320 return false;
321 }
322 }
323 return true;
324}
325
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000326bool ValidVector(const SkDVector& v) {
327 if (SkDoubleIsNaN(v.fX)) {
328 return false;
329 }
skia.committer@gmail.comeebe6f42013-07-17 07:01:13 +0000330 return !SkDoubleIsNaN(v.fY);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000331}