blob: 27764d1cad8560b01f8d7f58c340469c83a35e49 [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 */
Chris Dalton957189b2020-05-07 12:47:26 -06007#include "src/core/SkPathPriv.h"
John Stiles6e9ead92020-07-14 00:13:51 +00008#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/pathops/SkPathOpsBounds.h"
10#include "src/pathops/SkPathOpsConic.h"
11#include "src/pathops/SkPathOpsCubic.h"
12#include "src/pathops/SkPathOpsLine.h"
13#include "src/pathops/SkPathOpsQuad.h"
14#include "src/pathops/SkPathOpsTSect.h"
15#include "src/pathops/SkReduceOrder.h"
16#include "tests/PathOpsTestCommon.h"
caryclark54359292015-03-26 07:52:43 -070017
Ben Wagnerf08d1d02018-06-18 15:11:00 -040018#include <utility>
19
caryclark54359292015-03-26 07:52:43 -070020static double calc_t_div(const SkDCubic& cubic, double precision, double start) {
21 const double adjust = sqrt(3.) / 36;
22 SkDCubic sub;
23 const SkDCubic* cPtr;
24 if (start == 0) {
25 cPtr = &cubic;
26 } else {
27 // OPTIMIZE: special-case half-split ?
28 sub = cubic.subDivide(start, 1);
29 cPtr = &sub;
30 }
31 const SkDCubic& c = *cPtr;
32 double dx = c[3].fX - 3 * (c[2].fX - c[1].fX) - c[0].fX;
33 double dy = c[3].fY - 3 * (c[2].fY - c[1].fY) - c[0].fY;
34 double dist = sqrt(dx * dx + dy * dy);
35 double tDiv3 = precision / (adjust * dist);
36 double t = SkDCubeRoot(tDiv3);
37 if (start > 0) {
38 t = start + (1 - start) * t;
39 }
40 return t;
41}
42
43static bool add_simple_ts(const SkDCubic& cubic, double precision, SkTArray<double, true>* ts) {
44 double tDiv = calc_t_div(cubic, precision, 0);
45 if (tDiv >= 1) {
46 return true;
47 }
48 if (tDiv >= 0.5) {
49 ts->push_back(0.5);
50 return true;
51 }
52 return false;
53}
54
55static void addTs(const SkDCubic& cubic, double precision, double start, double end,
56 SkTArray<double, true>* ts) {
57 double tDiv = calc_t_div(cubic, precision, 0);
58 double parts = ceil(1.0 / tDiv);
59 for (double index = 0; index < parts; ++index) {
60 double newT = start + (index / parts) * (end - start);
61 if (newT > 0 && newT < 1) {
62 ts->push_back(newT);
63 }
64 }
65}
66
67static void toQuadraticTs(const SkDCubic* cubic, double precision, SkTArray<double, true>* ts) {
68 SkReduceOrder reducer;
69 int order = reducer.reduce(*cubic, SkReduceOrder::kAllow_Quadratics);
70 if (order < 3) {
71 return;
72 }
73 double inflectT[5];
74 int inflections = cubic->findInflections(inflectT);
75 SkASSERT(inflections <= 2);
76 if (!cubic->endsAreExtremaInXOrY()) {
77 inflections += cubic->findMaxCurvature(&inflectT[inflections]);
78 SkASSERT(inflections <= 5);
79 }
John Stiles886a9042020-07-14 16:28:33 -040080 SkTQSort<double>(inflectT, inflectT + inflections);
caryclark54359292015-03-26 07:52:43 -070081 // OPTIMIZATION: is this filtering common enough that it needs to be pulled out into its
82 // own subroutine?
83 while (inflections && approximately_less_than_zero(inflectT[0])) {
84 memmove(inflectT, &inflectT[1], sizeof(inflectT[0]) * --inflections);
85 }
86 int start = 0;
87 int next = 1;
88 while (next < inflections) {
89 if (!approximately_equal(inflectT[start], inflectT[next])) {
90 ++start;
91 ++next;
92 continue;
93 }
94 memmove(&inflectT[start], &inflectT[next], sizeof(inflectT[0]) * (--inflections - start));
95 }
96
97 while (inflections && approximately_greater_than_one(inflectT[inflections - 1])) {
98 --inflections;
99 }
100 SkDCubicPair pair;
101 if (inflections == 1) {
102 pair = cubic->chopAt(inflectT[0]);
103 int orderP1 = reducer.reduce(pair.first(), SkReduceOrder::kNo_Quadratics);
104 if (orderP1 < 2) {
105 --inflections;
106 } else {
107 int orderP2 = reducer.reduce(pair.second(), SkReduceOrder::kNo_Quadratics);
108 if (orderP2 < 2) {
109 --inflections;
110 }
111 }
112 }
113 if (inflections == 0 && add_simple_ts(*cubic, precision, ts)) {
114 return;
115 }
116 if (inflections == 1) {
117 pair = cubic->chopAt(inflectT[0]);
118 addTs(pair.first(), precision, 0, inflectT[0], ts);
119 addTs(pair.second(), precision, inflectT[0], 1, ts);
120 return;
121 }
122 if (inflections > 1) {
123 SkDCubic part = cubic->subDivide(0, inflectT[0]);
124 addTs(part, precision, 0, inflectT[0], ts);
125 int last = inflections - 1;
126 for (int idx = 0; idx < last; ++idx) {
127 part = cubic->subDivide(inflectT[idx], inflectT[idx + 1]);
128 addTs(part, precision, inflectT[idx], inflectT[idx + 1], ts);
129 }
130 part = cubic->subDivide(inflectT[last], 1);
131 addTs(part, precision, inflectT[last], 1, ts);
132 return;
133 }
134 addTs(*cubic, precision, 0, 1, ts);
135}
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000136
caryclark@google.comd892bd82013-06-17 14:10:36 +0000137void CubicToQuads(const SkDCubic& cubic, double precision, SkTArray<SkDQuad, true>& quads) {
138 SkTArray<double, true> ts;
caryclark54359292015-03-26 07:52:43 -0700139 toQuadraticTs(&cubic, precision, &ts);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000140 if (ts.count() <= 0) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000141 SkDQuad quad = cubic.toQuad();
caryclark@google.comd892bd82013-06-17 14:10:36 +0000142 quads.push_back(quad);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000143 return;
144 }
145 double tStart = 0;
146 for (int i1 = 0; i1 <= ts.count(); ++i1) {
147 const double tEnd = i1 < ts.count() ? ts[i1] : 1;
caryclarkaec25102015-04-29 08:28:30 -0700148 SkDRect bounds;
149 bounds.setBounds(cubic);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000150 SkDCubic part = cubic.subDivide(tStart, tEnd);
151 SkDQuad quad = part.toQuad();
caryclarkaec25102015-04-29 08:28:30 -0700152 if (quad[1].fX < bounds.fLeft) {
153 quad[1].fX = bounds.fLeft;
154 } else if (quad[1].fX > bounds.fRight) {
155 quad[1].fX = bounds.fRight;
156 }
157 if (quad[1].fY < bounds.fTop) {
158 quad[1].fY = bounds.fTop;
159 } else if (quad[1].fY > bounds.fBottom) {
160 quad[1].fY = bounds.fBottom;
161 }
caryclark@google.comd892bd82013-06-17 14:10:36 +0000162 quads.push_back(quad);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000163 tStart = tEnd;
164 }
165}
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000166
caryclarke4097e32014-06-18 07:24:19 -0700167void CubicPathToQuads(const SkPath& cubicPath, SkPath* quadPath) {
168 quadPath->reset();
169 SkDCubic cubic;
170 SkTArray<SkDQuad, true> quads;
Chris Dalton957189b2020-05-07 12:47:26 -0600171 for (auto [verb, pts, w] : SkPathPriv::Iterate(cubicPath)) {
caryclarke4097e32014-06-18 07:24:19 -0700172 switch (verb) {
Chris Dalton957189b2020-05-07 12:47:26 -0600173 case SkPathVerb::kMove:
caryclarke4097e32014-06-18 07:24:19 -0700174 quadPath->moveTo(pts[0].fX, pts[0].fY);
175 continue;
Chris Dalton957189b2020-05-07 12:47:26 -0600176 case SkPathVerb::kLine:
caryclarke4097e32014-06-18 07:24:19 -0700177 quadPath->lineTo(pts[1].fX, pts[1].fY);
178 break;
Chris Dalton957189b2020-05-07 12:47:26 -0600179 case SkPathVerb::kQuad:
caryclarke4097e32014-06-18 07:24:19 -0700180 quadPath->quadTo(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
181 break;
Chris Dalton957189b2020-05-07 12:47:26 -0600182 case SkPathVerb::kCubic:
caryclarke4097e32014-06-18 07:24:19 -0700183 quads.reset();
184 cubic.set(pts);
185 CubicToQuads(cubic, cubic.calcPrecision(), quads);
186 for (int index = 0; index < quads.count(); ++index) {
187 SkPoint qPts[2] = {
188 quads[index][1].asSkPoint(),
189 quads[index][2].asSkPoint()
190 };
191 quadPath->quadTo(qPts[0].fX, qPts[0].fY, qPts[1].fX, qPts[1].fY);
192 }
193 break;
Chris Dalton957189b2020-05-07 12:47:26 -0600194 case SkPathVerb::kClose:
caryclarke4097e32014-06-18 07:24:19 -0700195 quadPath->close();
196 break;
197 default:
198 SkDEBUGFAIL("bad verb");
199 return;
200 }
201 }
202}
203
204void CubicPathToSimple(const SkPath& cubicPath, SkPath* simplePath) {
205 simplePath->reset();
206 SkDCubic cubic;
Chris Dalton957189b2020-05-07 12:47:26 -0600207 for (auto [verb, pts, w] : SkPathPriv::Iterate(cubicPath)) {
caryclarke4097e32014-06-18 07:24:19 -0700208 switch (verb) {
Chris Dalton957189b2020-05-07 12:47:26 -0600209 case SkPathVerb::kMove:
caryclarke4097e32014-06-18 07:24:19 -0700210 simplePath->moveTo(pts[0].fX, pts[0].fY);
211 continue;
Chris Dalton957189b2020-05-07 12:47:26 -0600212 case SkPathVerb::kLine:
caryclarke4097e32014-06-18 07:24:19 -0700213 simplePath->lineTo(pts[1].fX, pts[1].fY);
214 break;
Chris Dalton957189b2020-05-07 12:47:26 -0600215 case SkPathVerb::kQuad:
caryclarke4097e32014-06-18 07:24:19 -0700216 simplePath->quadTo(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
217 break;
Chris Dalton957189b2020-05-07 12:47:26 -0600218 case SkPathVerb::kCubic: {
caryclarke4097e32014-06-18 07:24:19 -0700219 cubic.set(pts);
220 double tInflects[2];
221 int inflections = cubic.findInflections(tInflects);
222 if (inflections > 1 && tInflects[0] > tInflects[1]) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400223 using std::swap;
224 swap(tInflects[0], tInflects[1]);
caryclarke4097e32014-06-18 07:24:19 -0700225 }
226 double lo = 0;
227 for (int index = 0; index <= inflections; ++index) {
228 double hi = index < inflections ? tInflects[index] : 1;
229 SkDCubic part = cubic.subDivide(lo, hi);
230 SkPoint cPts[3];
231 cPts[0] = part[1].asSkPoint();
232 cPts[1] = part[2].asSkPoint();
233 cPts[2] = part[3].asSkPoint();
234 simplePath->cubicTo(cPts[0].fX, cPts[0].fY, cPts[1].fX, cPts[1].fY,
235 cPts[2].fX, cPts[2].fY);
236 lo = hi;
237 }
238 break;
halcanary9d524f22016-03-29 09:03:52 -0700239 }
Chris Dalton957189b2020-05-07 12:47:26 -0600240 case SkPathVerb::kClose:
caryclarke4097e32014-06-18 07:24:19 -0700241 simplePath->close();
242 break;
243 default:
244 SkDEBUGFAIL("bad verb");
245 return;
246 }
247 }
248}
249
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000250bool ValidBounds(const SkPathOpsBounds& bounds) {
251 if (SkScalarIsNaN(bounds.fLeft)) {
252 return false;
253 }
254 if (SkScalarIsNaN(bounds.fTop)) {
255 return false;
256 }
257 if (SkScalarIsNaN(bounds.fRight)) {
258 return false;
259 }
260 return !SkScalarIsNaN(bounds.fBottom);
261}
262
caryclark1049f122015-04-20 08:31:59 -0700263bool ValidConic(const SkDConic& conic) {
264 for (int index = 0; index < SkDConic::kPointCount; ++index) {
265 if (!ValidPoint(conic[index])) {
266 return false;
267 }
268 }
269 if (SkDoubleIsNaN(conic.fWeight)) {
270 return false;
271 }
272 return true;
273}
274
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000275bool ValidCubic(const SkDCubic& cubic) {
276 for (int index = 0; index < 4; ++index) {
277 if (!ValidPoint(cubic[index])) {
278 return false;
279 }
280 }
281 return true;
282}
283
284bool ValidLine(const SkDLine& line) {
285 for (int index = 0; index < 2; ++index) {
286 if (!ValidPoint(line[index])) {
287 return false;
288 }
289 }
290 return true;
291}
292
293bool ValidPoint(const SkDPoint& pt) {
294 if (SkDoubleIsNaN(pt.fX)) {
295 return false;
296 }
skia.committer@gmail.comeebe6f42013-07-17 07:01:13 +0000297 return !SkDoubleIsNaN(pt.fY);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000298}
299
300bool ValidPoints(const SkPoint* pts, int count) {
301 for (int index = 0; index < count; ++index) {
302 if (SkScalarIsNaN(pts[index].fX)) {
303 return false;
304 }
305 if (SkScalarIsNaN(pts[index].fY)) {
306 return false;
307 }
308 }
309 return true;
310}
311
312bool ValidQuad(const SkDQuad& quad) {
313 for (int index = 0; index < 3; ++index) {
314 if (!ValidPoint(quad[index])) {
315 return false;
316 }
317 }
318 return true;
319}
320
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000321bool ValidVector(const SkDVector& v) {
322 if (SkDoubleIsNaN(v.fX)) {
323 return false;
324 }
skia.committer@gmail.comeebe6f42013-07-17 07:01:13 +0000325 return !SkDoubleIsNaN(v.fY);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000326}