blob: 1d7acd4df91fec2c82a784da32fba194734ae6b8 [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "src/pathops/SkOpSegment.h"
8#include "src/pathops/SkOpSpan.h"
9#include "src/pathops/SkPathOpsPoint.h"
10#include "src/pathops/SkPathWriter.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000011
12// wrap path to keep track of whether the contour is initialized and non-empty
13SkPathWriter::SkPathWriter(SkPath& path)
14 : fPathPtr(&path)
caryclark@google.com07393ca2013-04-08 11:47:37 +000015{
16 init();
17}
18
19void SkPathWriter::close() {
caryclarkeed356d2016-09-14 07:18:20 -070020 if (fCurrent.isEmpty()) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000021 return;
22 }
caryclarkeed356d2016-09-14 07:18:20 -070023 SkASSERT(this->isClosed());
caryclark@google.com07393ca2013-04-08 11:47:37 +000024#if DEBUG_PATH_CONSTRUCTION
caryclarkeed356d2016-09-14 07:18:20 -070025 SkDebugf("path.close();\n");
caryclark@google.com07393ca2013-04-08 11:47:37 +000026#endif
caryclarkeed356d2016-09-14 07:18:20 -070027 fCurrent.close();
28 fPathPtr->addPath(fCurrent);
29 fCurrent.reset();
caryclark@google.com07393ca2013-04-08 11:47:37 +000030 init();
31}
32
caryclarkeed356d2016-09-14 07:18:20 -070033void SkPathWriter::conicTo(const SkPoint& pt1, const SkOpPtT* pt2, SkScalar weight) {
Cary Clark98900b52018-08-17 12:38:22 -040034 SkPoint pt2pt = this->update(pt2);
caryclark1049f122015-04-20 08:31:59 -070035#if DEBUG_PATH_CONSTRUCTION
36 SkDebugf("path.conicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g);\n",
Cary Clark98900b52018-08-17 12:38:22 -040037 pt1.fX, pt1.fY, pt2pt.fX, pt2pt.fY, weight);
caryclark1049f122015-04-20 08:31:59 -070038#endif
Cary Clark98900b52018-08-17 12:38:22 -040039 fCurrent.conicTo(pt1, pt2pt, weight);
caryclark1049f122015-04-20 08:31:59 -070040}
41
caryclarkeed356d2016-09-14 07:18:20 -070042void SkPathWriter::cubicTo(const SkPoint& pt1, const SkPoint& pt2, const SkOpPtT* pt3) {
Cary Clark98900b52018-08-17 12:38:22 -040043 SkPoint pt3pt = this->update(pt3);
caryclark@google.com07393ca2013-04-08 11:47:37 +000044#if DEBUG_PATH_CONSTRUCTION
45 SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n",
Cary Clark98900b52018-08-17 12:38:22 -040046 pt1.fX, pt1.fY, pt2.fX, pt2.fY, pt3pt.fX, pt3pt.fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +000047#endif
Cary Clark98900b52018-08-17 12:38:22 -040048 fCurrent.cubicTo(pt1, pt2, pt3pt);
caryclark@google.com07393ca2013-04-08 11:47:37 +000049}
50
caryclarka35ab3e2016-10-20 08:32:18 -070051bool SkPathWriter::deferredLine(const SkOpPtT* pt) {
caryclarkeed356d2016-09-14 07:18:20 -070052 SkASSERT(fFirstPtT);
53 SkASSERT(fDefer[0]);
54 if (fDefer[0] == pt) {
55 // FIXME: why we're adding a degenerate line? Caller should have preflighted this.
caryclarka35ab3e2016-10-20 08:32:18 -070056 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +000057 }
caryclarkeed356d2016-09-14 07:18:20 -070058 if (pt->contains(fDefer[0])) {
59 // FIXME: why we're adding a degenerate line?
caryclarka35ab3e2016-10-20 08:32:18 -070060 return true;
caryclarkeed356d2016-09-14 07:18:20 -070061 }
caryclarka35ab3e2016-10-20 08:32:18 -070062 if (this->matchedLast(pt)) {
63 return false;
64 }
caryclarkeed356d2016-09-14 07:18:20 -070065 if (fDefer[1] && this->changedSlopes(pt)) {
66 this->lineTo();
caryclark@google.com07393ca2013-04-08 11:47:37 +000067 fDefer[0] = fDefer[1];
68 }
69 fDefer[1] = pt;
caryclarka35ab3e2016-10-20 08:32:18 -070070 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +000071}
72
caryclarkeed356d2016-09-14 07:18:20 -070073void SkPathWriter::deferredMove(const SkOpPtT* pt) {
74 if (!fDefer[1]) {
75 fFirstPtT = fDefer[0] = pt;
76 return;
caryclark@google.com07393ca2013-04-08 11:47:37 +000077 }
caryclarkeed356d2016-09-14 07:18:20 -070078 SkASSERT(fDefer[0]);
79 if (!this->matchedLast(pt)) {
80 this->finishContour();
81 fFirstPtT = fDefer[0] = pt;
82 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000083}
84
caryclarkeed356d2016-09-14 07:18:20 -070085void SkPathWriter::finishContour() {
86 if (!this->matchedLast(fDefer[0])) {
caryclark1c106072016-09-22 12:52:20 -070087 if (!fDefer[1]) {
88 return;
89 }
caryclarkeed356d2016-09-14 07:18:20 -070090 this->lineTo();
91 }
92 if (fCurrent.isEmpty()) {
93 return;
94 }
95 if (this->isClosed()) {
96 this->close();
97 } else {
98 SkASSERT(fDefer[1]);
Mike Reed5edcd312018-08-08 11:23:41 -040099 fEndPtTs.push_back(fFirstPtT);
100 fEndPtTs.push_back(fDefer[1]);
caryclarkeed356d2016-09-14 07:18:20 -0700101 fPartials.push_back(fCurrent);
102 this->init();
103 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000104}
105
106void SkPathWriter::init() {
caryclarkeed356d2016-09-14 07:18:20 -0700107 fCurrent.reset();
108 fFirstPtT = fDefer[0] = fDefer[1] = nullptr;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000109}
110
111bool SkPathWriter::isClosed() const {
caryclarkeed356d2016-09-14 07:18:20 -0700112 return this->matchedLast(fFirstPtT);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000113}
114
115void SkPathWriter::lineTo() {
caryclarkeed356d2016-09-14 07:18:20 -0700116 if (fCurrent.isEmpty()) {
117 this->moveTo();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000118 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000119#if DEBUG_PATH_CONSTRUCTION
caryclarkeed356d2016-09-14 07:18:20 -0700120 SkDebugf("path.lineTo(%1.9g,%1.9g);\n", fDefer[1]->fPt.fX, fDefer[1]->fPt.fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000121#endif
caryclarkeed356d2016-09-14 07:18:20 -0700122 fCurrent.lineTo(fDefer[1]->fPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000123}
124
caryclarkeed356d2016-09-14 07:18:20 -0700125bool SkPathWriter::matchedLast(const SkOpPtT* test) const {
126 if (test == fDefer[1]) {
127 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000128 }
caryclarkeed356d2016-09-14 07:18:20 -0700129 if (!test) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000130 return false;
131 }
caryclarkeed356d2016-09-14 07:18:20 -0700132 if (!fDefer[1]) {
133 return false;
134 }
135 return test->contains(fDefer[1]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000136}
137
138void SkPathWriter::moveTo() {
caryclarkeed356d2016-09-14 07:18:20 -0700139#if DEBUG_PATH_CONSTRUCTION
140 SkDebugf("path.moveTo(%1.9g,%1.9g);\n", fFirstPtT->fPt.fX, fFirstPtT->fPt.fY);
141#endif
142 fCurrent.moveTo(fFirstPtT->fPt);
143}
144
145void SkPathWriter::quadTo(const SkPoint& pt1, const SkOpPtT* pt2) {
Cary Clark98900b52018-08-17 12:38:22 -0400146 SkPoint pt2pt = this->update(pt2);
caryclarkeed356d2016-09-14 07:18:20 -0700147#if DEBUG_PATH_CONSTRUCTION
148 SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n",
Cary Clark98900b52018-08-17 12:38:22 -0400149 pt1.fX, pt1.fY, pt2pt.fX, pt2pt.fY);
caryclarkeed356d2016-09-14 07:18:20 -0700150#endif
Cary Clark98900b52018-08-17 12:38:22 -0400151 fCurrent.quadTo(pt1, pt2pt);
caryclarkeed356d2016-09-14 07:18:20 -0700152}
153
Cary Clark98900b52018-08-17 12:38:22 -0400154// if last point to be written matches the current path's first point, alter the
155// last to avoid writing a degenerate lineTo when the path is closed
156SkPoint SkPathWriter::update(const SkOpPtT* pt) {
caryclarkeed356d2016-09-14 07:18:20 -0700157 if (!fDefer[1]) {
158 this->moveTo();
159 } else if (!this->matchedLast(fDefer[0])) {
160 this->lineTo();
161 }
Cary Clark98900b52018-08-17 12:38:22 -0400162 SkPoint result = pt->fPt;
163 if (fFirstPtT && result != fFirstPtT->fPt && fFirstPtT->contains(pt)) {
164 result = fFirstPtT->fPt;
165 }
caryclarkeed356d2016-09-14 07:18:20 -0700166 fDefer[0] = fDefer[1] = pt; // set both to know that there is not a pending deferred line
Cary Clark98900b52018-08-17 12:38:22 -0400167 return result;
caryclarkeed356d2016-09-14 07:18:20 -0700168}
169
170bool SkPathWriter::someAssemblyRequired() {
171 this->finishContour();
172 return fEndPtTs.count() > 0;
173}
174
175bool SkPathWriter::changedSlopes(const SkOpPtT* ptT) const {
176 if (matchedLast(fDefer[0])) {
177 return false;
178 }
179 SkVector deferDxdy = fDefer[1]->fPt - fDefer[0]->fPt;
180 SkVector lineDxdy = ptT->fPt - fDefer[1]->fPt;
181 return deferDxdy.fX * lineDxdy.fY != deferDxdy.fY * lineDxdy.fX;
182}
183
184class DistanceLessThan {
185public:
186 DistanceLessThan(double* distances) : fDistances(distances) { }
187 double* fDistances;
188 bool operator()(const int one, const int two) {
189 return fDistances[one] < fDistances[two];
190 }
191};
192
193 /*
194 check start and end of each contour
195 if not the same, record them
196 match them up
197 connect closest
198 reassemble contour pieces into new path
199 */
200void SkPathWriter::assemble() {
caryclarkeed356d2016-09-14 07:18:20 -0700201 if (!this->someAssemblyRequired()) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000202 return;
203 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000204#if DEBUG_PATH_CONSTRUCTION
caryclarkeed356d2016-09-14 07:18:20 -0700205 SkDebugf("%s\n", __FUNCTION__);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000206#endif
caryclarkeed356d2016-09-14 07:18:20 -0700207 SkOpPtT const* const* runs = fEndPtTs.begin(); // starts, ends of partial contours
208 int endCount = fEndPtTs.count(); // all starts and ends
209 SkASSERT(endCount > 0);
210 SkASSERT(endCount == fPartials.count() * 2);
211#if DEBUG_ASSEMBLE
212 for (int index = 0; index < endCount; index += 2) {
213 const SkOpPtT* eStart = runs[index];
214 const SkOpPtT* eEnd = runs[index + 1];
215 SkASSERT(eStart != eEnd);
216 SkASSERT(!eStart->contains(eEnd));
217 SkDebugf("%s contour start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n", __FUNCTION__,
218 eStart->fPt.fX, eStart->fPt.fY, eEnd->fPt.fX, eEnd->fPt.fY);
219 }
220#endif
Cary Clarkcadc5062018-08-06 17:24:04 -0400221 // lengthen any partial contour adjacent to a simple segment
222 for (int pIndex = 0; pIndex < endCount; pIndex++) {
223 SkOpPtT* opPtT = const_cast<SkOpPtT*>(runs[pIndex]);
224 SkPath dummy;
225 SkPathWriter partWriter(dummy);
226 do {
227 if (!zero_or_one(opPtT->fT)) {
228 break;
229 }
230 SkOpSpanBase* opSpanBase = opPtT->span();
231 SkOpSpanBase* start = opPtT->fT ? opSpanBase->prev() : opSpanBase->upCast()->next();
232 int step = opPtT->fT ? 1 : -1;
233 const SkOpSegment* opSegment = opSpanBase->segment();
234 const SkOpSegment* nextSegment = opSegment->isSimple(&start, &step);
235 if (!nextSegment) {
236 break;
237 }
238 SkOpSpanBase* opSpanEnd = start->t() ? start->prev() : start->upCast()->next();
239 if (start->starter(opSpanEnd)->alreadyAdded()) {
240 break;
241 }
242 nextSegment->addCurveTo(start, opSpanEnd, &partWriter);
243 opPtT = opSpanEnd->ptT();
244 SkOpPtT** runsPtr = const_cast<SkOpPtT**>(&runs[pIndex]);
245 *runsPtr = opPtT;
246 } while (true);
247 partWriter.finishContour();
248 const SkTArray<SkPath>& partPartials = partWriter.partials();
249 if (!partPartials.count()) {
250 continue;
251 }
252 // if pIndex is even, reverse and prepend to fPartials; otherwise, append
253 SkPath& partial = const_cast<SkPath&>(fPartials[pIndex >> 1]);
254 const SkPath& part = partPartials[0];
255 if (pIndex & 1) {
256 partial.addPath(part, SkPath::kExtend_AddPathMode);
257 } else {
258 SkPath reverse;
259 reverse.reverseAddPath(part);
260 reverse.addPath(partial, SkPath::kExtend_AddPathMode);
261 partial = reverse;
262 }
263 }
caryclarkeed356d2016-09-14 07:18:20 -0700264 SkTDArray<int> sLink, eLink;
265 int linkCount = endCount / 2; // number of partial contours
266 sLink.append(linkCount);
267 eLink.append(linkCount);
268 int rIndex, iIndex;
269 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
270 sLink[rIndex] = eLink[rIndex] = SK_MaxS32;
271 }
272 const int entries = endCount * (endCount - 1) / 2; // folded triangle
273 SkSTArray<8, double, true> distances(entries);
274 SkSTArray<8, int, true> sortedDist(entries);
275 SkSTArray<8, int, true> distLookup(entries);
276 int rRow = 0;
277 int dIndex = 0;
278 for (rIndex = 0; rIndex < endCount - 1; ++rIndex) {
279 const SkOpPtT* oPtT = runs[rIndex];
280 for (iIndex = rIndex + 1; iIndex < endCount; ++iIndex) {
281 const SkOpPtT* iPtT = runs[iIndex];
282 double dx = iPtT->fPt.fX - oPtT->fPt.fX;
283 double dy = iPtT->fPt.fY - oPtT->fPt.fY;
284 double dist = dx * dx + dy * dy;
285 distLookup.push_back(rRow + iIndex);
286 distances.push_back(dist); // oStart distance from iStart
Ben Wagner63fd7602017-10-09 15:45:33 -0400287 sortedDist.push_back(dIndex++);
caryclarkeed356d2016-09-14 07:18:20 -0700288 }
289 rRow += endCount;
290 }
291 SkASSERT(dIndex == entries);
John Stiles70474c12020-07-13 18:09:07 -0400292 std::sort(sortedDist.begin(), sortedDist.end(), DistanceLessThan(distances.begin()));
caryclarkeed356d2016-09-14 07:18:20 -0700293 int remaining = linkCount; // number of start/end pairs
294 for (rIndex = 0; rIndex < entries; ++rIndex) {
295 int pair = sortedDist[rIndex];
296 pair = distLookup[pair];
297 int row = pair / endCount;
298 int col = pair - row * endCount;
299 int ndxOne = row >> 1;
300 bool endOne = row & 1;
301 int* linkOne = endOne ? eLink.begin() : sLink.begin();
302 if (linkOne[ndxOne] != SK_MaxS32) {
303 continue;
304 }
305 int ndxTwo = col >> 1;
306 bool endTwo = col & 1;
307 int* linkTwo = endTwo ? eLink.begin() : sLink.begin();
308 if (linkTwo[ndxTwo] != SK_MaxS32) {
309 continue;
310 }
311 SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]);
312 bool flip = endOne == endTwo;
313 linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo;
314 linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne;
315 if (!--remaining) {
316 break;
317 }
318 }
319 SkASSERT(!remaining);
320#if DEBUG_ASSEMBLE
321 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
322 int s = sLink[rIndex];
323 int e = eLink[rIndex];
324 SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e',
325 s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e);
326 }
327#endif
328 rIndex = 0;
329 do {
330 bool forward = true;
331 bool first = true;
332 int sIndex = sLink[rIndex];
333 SkASSERT(sIndex != SK_MaxS32);
334 sLink[rIndex] = SK_MaxS32;
335 int eIndex;
336 if (sIndex < 0) {
337 eIndex = sLink[~sIndex];
338 sLink[~sIndex] = SK_MaxS32;
339 } else {
340 eIndex = eLink[sIndex];
341 eLink[sIndex] = SK_MaxS32;
342 }
343 SkASSERT(eIndex != SK_MaxS32);
344#if DEBUG_ASSEMBLE
345 SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e',
346 sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e',
347 eIndex < 0 ? ~eIndex : eIndex);
348#endif
349 do {
350 const SkPath& contour = fPartials[rIndex];
Cary Clark1d314432018-07-10 10:57:54 -0400351 if (!first) {
352 SkPoint prior, next;
Cary Clark13795082018-11-07 16:18:39 -0500353 if (!fPathPtr->getLastPt(&prior)) {
354 return;
355 }
Cary Clark1d314432018-07-10 10:57:54 -0400356 if (forward) {
357 next = contour.getPoint(0);
358 } else {
359 SkAssertResult(contour.getLastPt(&next));
360 }
361 if (prior != next) {
362 /* TODO: if there is a gap between open path written so far and path to come,
363 connect by following segments from one to the other, rather than introducing
364 a diagonal to connect the two.
365 */
366 SkDebugf("");
367 }
368 }
caryclarkeed356d2016-09-14 07:18:20 -0700369 if (forward) {
370 fPathPtr->addPath(contour,
371 first ? SkPath::kAppend_AddPathMode : SkPath::kExtend_AddPathMode);
372 } else {
373 SkASSERT(!first);
caryclark51c56782016-11-07 05:09:28 -0800374 fPathPtr->reversePathTo(contour);
caryclarkeed356d2016-09-14 07:18:20 -0700375 }
376 if (first) {
377 first = false;
378 }
379#if DEBUG_ASSEMBLE
380 SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex,
381 eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex,
382 sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex));
383#endif
384 if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) {
385 fPathPtr->close();
386 break;
387 }
388 if (forward) {
389 eIndex = eLink[rIndex];
390 SkASSERT(eIndex != SK_MaxS32);
391 eLink[rIndex] = SK_MaxS32;
392 if (eIndex >= 0) {
393 SkASSERT(sLink[eIndex] == rIndex);
394 sLink[eIndex] = SK_MaxS32;
395 } else {
396 SkASSERT(eLink[~eIndex] == ~rIndex);
397 eLink[~eIndex] = SK_MaxS32;
398 }
399 } else {
400 eIndex = sLink[rIndex];
401 SkASSERT(eIndex != SK_MaxS32);
402 sLink[rIndex] = SK_MaxS32;
403 if (eIndex >= 0) {
404 SkASSERT(eLink[eIndex] == rIndex);
405 eLink[eIndex] = SK_MaxS32;
406 } else {
407 SkASSERT(sLink[~eIndex] == ~rIndex);
408 sLink[~eIndex] = SK_MaxS32;
409 }
410 }
411 rIndex = eIndex;
412 if (rIndex < 0) {
413 forward ^= 1;
414 rIndex = ~rIndex;
415 }
416 } while (true);
417 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
418 if (sLink[rIndex] != SK_MaxS32) {
419 break;
420 }
421 }
422 } while (rIndex < linkCount);
423#if DEBUG_ASSEMBLE
424 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
425 SkASSERT(sLink[rIndex] == SK_MaxS32);
426 SkASSERT(eLink[rIndex] == SK_MaxS32);
427 }
428#endif
429 return;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000430}