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