blob: 8357b2144491e61d19a76aed9381061addd3ddd4 [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 */
John Stiles6e9ead92020-07-14 00:13:51 +00007#include "src/core/SkTSort.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/pathops/SkOpSegment.h"
9#include "src/pathops/SkOpSpan.h"
10#include "src/pathops/SkPathOpsPoint.h"
11#include "src/pathops/SkPathWriter.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000012
13// wrap path to keep track of whether the contour is initialized and non-empty
14SkPathWriter::SkPathWriter(SkPath& path)
15 : fPathPtr(&path)
caryclark@google.com07393ca2013-04-08 11:47:37 +000016{
17 init();
18}
19
20void SkPathWriter::close() {
caryclarkeed356d2016-09-14 07:18:20 -070021 if (fCurrent.isEmpty()) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000022 return;
23 }
caryclarkeed356d2016-09-14 07:18:20 -070024 SkASSERT(this->isClosed());
caryclark@google.com07393ca2013-04-08 11:47:37 +000025#if DEBUG_PATH_CONSTRUCTION
caryclarkeed356d2016-09-14 07:18:20 -070026 SkDebugf("path.close();\n");
caryclark@google.com07393ca2013-04-08 11:47:37 +000027#endif
caryclarkeed356d2016-09-14 07:18:20 -070028 fCurrent.close();
29 fPathPtr->addPath(fCurrent);
30 fCurrent.reset();
caryclark@google.com07393ca2013-04-08 11:47:37 +000031 init();
32}
33
caryclarkeed356d2016-09-14 07:18:20 -070034void SkPathWriter::conicTo(const SkPoint& pt1, const SkOpPtT* pt2, SkScalar weight) {
Cary Clark98900b52018-08-17 12:38:22 -040035 SkPoint pt2pt = this->update(pt2);
caryclark1049f122015-04-20 08:31:59 -070036#if DEBUG_PATH_CONSTRUCTION
37 SkDebugf("path.conicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g);\n",
Cary Clark98900b52018-08-17 12:38:22 -040038 pt1.fX, pt1.fY, pt2pt.fX, pt2pt.fY, weight);
caryclark1049f122015-04-20 08:31:59 -070039#endif
Cary Clark98900b52018-08-17 12:38:22 -040040 fCurrent.conicTo(pt1, pt2pt, weight);
caryclark1049f122015-04-20 08:31:59 -070041}
42
caryclarkeed356d2016-09-14 07:18:20 -070043void SkPathWriter::cubicTo(const SkPoint& pt1, const SkPoint& pt2, const SkOpPtT* pt3) {
Cary Clark98900b52018-08-17 12:38:22 -040044 SkPoint pt3pt = this->update(pt3);
caryclark@google.com07393ca2013-04-08 11:47:37 +000045#if DEBUG_PATH_CONSTRUCTION
46 SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n",
Cary Clark98900b52018-08-17 12:38:22 -040047 pt1.fX, pt1.fY, pt2.fX, pt2.fY, pt3pt.fX, pt3pt.fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +000048#endif
Cary Clark98900b52018-08-17 12:38:22 -040049 fCurrent.cubicTo(pt1, pt2, pt3pt);
caryclark@google.com07393ca2013-04-08 11:47:37 +000050}
51
caryclarka35ab3e2016-10-20 08:32:18 -070052bool SkPathWriter::deferredLine(const SkOpPtT* pt) {
caryclarkeed356d2016-09-14 07:18:20 -070053 SkASSERT(fFirstPtT);
54 SkASSERT(fDefer[0]);
55 if (fDefer[0] == pt) {
56 // FIXME: why we're adding a degenerate line? Caller should have preflighted this.
caryclarka35ab3e2016-10-20 08:32:18 -070057 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +000058 }
caryclarkeed356d2016-09-14 07:18:20 -070059 if (pt->contains(fDefer[0])) {
60 // FIXME: why we're adding a degenerate line?
caryclarka35ab3e2016-10-20 08:32:18 -070061 return true;
caryclarkeed356d2016-09-14 07:18:20 -070062 }
caryclarka35ab3e2016-10-20 08:32:18 -070063 if (this->matchedLast(pt)) {
64 return false;
65 }
caryclarkeed356d2016-09-14 07:18:20 -070066 if (fDefer[1] && this->changedSlopes(pt)) {
67 this->lineTo();
caryclark@google.com07393ca2013-04-08 11:47:37 +000068 fDefer[0] = fDefer[1];
69 }
70 fDefer[1] = pt;
caryclarka35ab3e2016-10-20 08:32:18 -070071 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +000072}
73
caryclarkeed356d2016-09-14 07:18:20 -070074void SkPathWriter::deferredMove(const SkOpPtT* pt) {
75 if (!fDefer[1]) {
76 fFirstPtT = fDefer[0] = pt;
77 return;
caryclark@google.com07393ca2013-04-08 11:47:37 +000078 }
caryclarkeed356d2016-09-14 07:18:20 -070079 SkASSERT(fDefer[0]);
80 if (!this->matchedLast(pt)) {
81 this->finishContour();
82 fFirstPtT = fDefer[0] = pt;
83 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000084}
85
caryclarkeed356d2016-09-14 07:18:20 -070086void SkPathWriter::finishContour() {
87 if (!this->matchedLast(fDefer[0])) {
caryclark1c106072016-09-22 12:52:20 -070088 if (!fDefer[1]) {
89 return;
90 }
caryclarkeed356d2016-09-14 07:18:20 -070091 this->lineTo();
92 }
93 if (fCurrent.isEmpty()) {
94 return;
95 }
96 if (this->isClosed()) {
97 this->close();
98 } else {
99 SkASSERT(fDefer[1]);
Mike Reed5edcd312018-08-08 11:23:41 -0400100 fEndPtTs.push_back(fFirstPtT);
101 fEndPtTs.push_back(fDefer[1]);
caryclarkeed356d2016-09-14 07:18:20 -0700102 fPartials.push_back(fCurrent);
103 this->init();
104 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000105}
106
107void SkPathWriter::init() {
caryclarkeed356d2016-09-14 07:18:20 -0700108 fCurrent.reset();
109 fFirstPtT = fDefer[0] = fDefer[1] = nullptr;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000110}
111
112bool SkPathWriter::isClosed() const {
caryclarkeed356d2016-09-14 07:18:20 -0700113 return this->matchedLast(fFirstPtT);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000114}
115
116void SkPathWriter::lineTo() {
caryclarkeed356d2016-09-14 07:18:20 -0700117 if (fCurrent.isEmpty()) {
118 this->moveTo();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000119 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000120#if DEBUG_PATH_CONSTRUCTION
caryclarkeed356d2016-09-14 07:18:20 -0700121 SkDebugf("path.lineTo(%1.9g,%1.9g);\n", fDefer[1]->fPt.fX, fDefer[1]->fPt.fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000122#endif
caryclarkeed356d2016-09-14 07:18:20 -0700123 fCurrent.lineTo(fDefer[1]->fPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000124}
125
caryclarkeed356d2016-09-14 07:18:20 -0700126bool SkPathWriter::matchedLast(const SkOpPtT* test) const {
127 if (test == fDefer[1]) {
128 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000129 }
caryclarkeed356d2016-09-14 07:18:20 -0700130 if (!test) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000131 return false;
132 }
caryclarkeed356d2016-09-14 07:18:20 -0700133 if (!fDefer[1]) {
134 return false;
135 }
136 return test->contains(fDefer[1]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000137}
138
139void SkPathWriter::moveTo() {
caryclarkeed356d2016-09-14 07:18:20 -0700140#if DEBUG_PATH_CONSTRUCTION
141 SkDebugf("path.moveTo(%1.9g,%1.9g);\n", fFirstPtT->fPt.fX, fFirstPtT->fPt.fY);
142#endif
143 fCurrent.moveTo(fFirstPtT->fPt);
144}
145
146void SkPathWriter::quadTo(const SkPoint& pt1, const SkOpPtT* pt2) {
Cary Clark98900b52018-08-17 12:38:22 -0400147 SkPoint pt2pt = this->update(pt2);
caryclarkeed356d2016-09-14 07:18:20 -0700148#if DEBUG_PATH_CONSTRUCTION
149 SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n",
Cary Clark98900b52018-08-17 12:38:22 -0400150 pt1.fX, pt1.fY, pt2pt.fX, pt2pt.fY);
caryclarkeed356d2016-09-14 07:18:20 -0700151#endif
Cary Clark98900b52018-08-17 12:38:22 -0400152 fCurrent.quadTo(pt1, pt2pt);
caryclarkeed356d2016-09-14 07:18:20 -0700153}
154
Cary Clark98900b52018-08-17 12:38:22 -0400155// if last point to be written matches the current path's first point, alter the
156// last to avoid writing a degenerate lineTo when the path is closed
157SkPoint SkPathWriter::update(const SkOpPtT* pt) {
caryclarkeed356d2016-09-14 07:18:20 -0700158 if (!fDefer[1]) {
159 this->moveTo();
160 } else if (!this->matchedLast(fDefer[0])) {
161 this->lineTo();
162 }
Cary Clark98900b52018-08-17 12:38:22 -0400163 SkPoint result = pt->fPt;
164 if (fFirstPtT && result != fFirstPtT->fPt && fFirstPtT->contains(pt)) {
165 result = fFirstPtT->fPt;
166 }
caryclarkeed356d2016-09-14 07:18:20 -0700167 fDefer[0] = fDefer[1] = pt; // set both to know that there is not a pending deferred line
Cary Clark98900b52018-08-17 12:38:22 -0400168 return result;
caryclarkeed356d2016-09-14 07:18:20 -0700169}
170
171bool SkPathWriter::someAssemblyRequired() {
172 this->finishContour();
173 return fEndPtTs.count() > 0;
174}
175
176bool SkPathWriter::changedSlopes(const SkOpPtT* ptT) const {
177 if (matchedLast(fDefer[0])) {
178 return false;
179 }
180 SkVector deferDxdy = fDefer[1]->fPt - fDefer[0]->fPt;
181 SkVector lineDxdy = ptT->fPt - fDefer[1]->fPt;
182 return deferDxdy.fX * lineDxdy.fY != deferDxdy.fY * lineDxdy.fX;
183}
184
185class DistanceLessThan {
186public:
187 DistanceLessThan(double* distances) : fDistances(distances) { }
188 double* fDistances;
John Stiles955adbe2020-07-14 12:56:15 -0400189 bool operator()(const int one, const int two) const {
caryclarkeed356d2016-09-14 07:18:20 -0700190 return fDistances[one] < fDistances[two];
191 }
192};
193
194 /*
195 check start and end of each contour
196 if not the same, record them
197 match them up
198 connect closest
199 reassemble contour pieces into new path
200 */
201void SkPathWriter::assemble() {
caryclarkeed356d2016-09-14 07:18:20 -0700202 if (!this->someAssemblyRequired()) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000203 return;
204 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000205#if DEBUG_PATH_CONSTRUCTION
caryclarkeed356d2016-09-14 07:18:20 -0700206 SkDebugf("%s\n", __FUNCTION__);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000207#endif
caryclarkeed356d2016-09-14 07:18:20 -0700208 SkOpPtT const* const* runs = fEndPtTs.begin(); // starts, ends of partial contours
209 int endCount = fEndPtTs.count(); // all starts and ends
210 SkASSERT(endCount > 0);
211 SkASSERT(endCount == fPartials.count() * 2);
212#if DEBUG_ASSEMBLE
213 for (int index = 0; index < endCount; index += 2) {
214 const SkOpPtT* eStart = runs[index];
215 const SkOpPtT* eEnd = runs[index + 1];
216 SkASSERT(eStart != eEnd);
217 SkASSERT(!eStart->contains(eEnd));
218 SkDebugf("%s contour start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n", __FUNCTION__,
219 eStart->fPt.fX, eStart->fPt.fY, eEnd->fPt.fX, eEnd->fPt.fY);
220 }
221#endif
Cary Clarkcadc5062018-08-06 17:24:04 -0400222 // lengthen any partial contour adjacent to a simple segment
223 for (int pIndex = 0; pIndex < endCount; pIndex++) {
224 SkOpPtT* opPtT = const_cast<SkOpPtT*>(runs[pIndex]);
225 SkPath dummy;
226 SkPathWriter partWriter(dummy);
227 do {
228 if (!zero_or_one(opPtT->fT)) {
229 break;
230 }
231 SkOpSpanBase* opSpanBase = opPtT->span();
232 SkOpSpanBase* start = opPtT->fT ? opSpanBase->prev() : opSpanBase->upCast()->next();
233 int step = opPtT->fT ? 1 : -1;
234 const SkOpSegment* opSegment = opSpanBase->segment();
235 const SkOpSegment* nextSegment = opSegment->isSimple(&start, &step);
236 if (!nextSegment) {
237 break;
238 }
239 SkOpSpanBase* opSpanEnd = start->t() ? start->prev() : start->upCast()->next();
240 if (start->starter(opSpanEnd)->alreadyAdded()) {
241 break;
242 }
243 nextSegment->addCurveTo(start, opSpanEnd, &partWriter);
244 opPtT = opSpanEnd->ptT();
245 SkOpPtT** runsPtr = const_cast<SkOpPtT**>(&runs[pIndex]);
246 *runsPtr = opPtT;
247 } while (true);
248 partWriter.finishContour();
249 const SkTArray<SkPath>& partPartials = partWriter.partials();
250 if (!partPartials.count()) {
251 continue;
252 }
253 // if pIndex is even, reverse and prepend to fPartials; otherwise, append
254 SkPath& partial = const_cast<SkPath&>(fPartials[pIndex >> 1]);
255 const SkPath& part = partPartials[0];
256 if (pIndex & 1) {
257 partial.addPath(part, SkPath::kExtend_AddPathMode);
258 } else {
259 SkPath reverse;
260 reverse.reverseAddPath(part);
261 reverse.addPath(partial, SkPath::kExtend_AddPathMode);
262 partial = reverse;
263 }
264 }
caryclarkeed356d2016-09-14 07:18:20 -0700265 SkTDArray<int> sLink, eLink;
266 int linkCount = endCount / 2; // number of partial contours
267 sLink.append(linkCount);
268 eLink.append(linkCount);
269 int rIndex, iIndex;
270 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
271 sLink[rIndex] = eLink[rIndex] = SK_MaxS32;
272 }
273 const int entries = endCount * (endCount - 1) / 2; // folded triangle
274 SkSTArray<8, double, true> distances(entries);
275 SkSTArray<8, int, true> sortedDist(entries);
276 SkSTArray<8, int, true> distLookup(entries);
277 int rRow = 0;
278 int dIndex = 0;
279 for (rIndex = 0; rIndex < endCount - 1; ++rIndex) {
280 const SkOpPtT* oPtT = runs[rIndex];
281 for (iIndex = rIndex + 1; iIndex < endCount; ++iIndex) {
282 const SkOpPtT* iPtT = runs[iIndex];
283 double dx = iPtT->fPt.fX - oPtT->fPt.fX;
284 double dy = iPtT->fPt.fY - oPtT->fPt.fY;
285 double dist = dx * dx + dy * dy;
286 distLookup.push_back(rRow + iIndex);
287 distances.push_back(dist); // oStart distance from iStart
Ben Wagner63fd7602017-10-09 15:45:33 -0400288 sortedDist.push_back(dIndex++);
caryclarkeed356d2016-09-14 07:18:20 -0700289 }
290 rRow += endCount;
291 }
292 SkASSERT(dIndex == entries);
John Stiles6e9ead92020-07-14 00:13:51 +0000293 SkTQSort<int>(sortedDist.begin(), sortedDist.end() - 1, DistanceLessThan(distances.begin()));
caryclarkeed356d2016-09-14 07:18:20 -0700294 int remaining = linkCount; // number of start/end pairs
295 for (rIndex = 0; rIndex < entries; ++rIndex) {
296 int pair = sortedDist[rIndex];
297 pair = distLookup[pair];
298 int row = pair / endCount;
299 int col = pair - row * endCount;
300 int ndxOne = row >> 1;
301 bool endOne = row & 1;
302 int* linkOne = endOne ? eLink.begin() : sLink.begin();
303 if (linkOne[ndxOne] != SK_MaxS32) {
304 continue;
305 }
306 int ndxTwo = col >> 1;
307 bool endTwo = col & 1;
308 int* linkTwo = endTwo ? eLink.begin() : sLink.begin();
309 if (linkTwo[ndxTwo] != SK_MaxS32) {
310 continue;
311 }
312 SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]);
313 bool flip = endOne == endTwo;
314 linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo;
315 linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne;
316 if (!--remaining) {
317 break;
318 }
319 }
320 SkASSERT(!remaining);
321#if DEBUG_ASSEMBLE
322 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
323 int s = sLink[rIndex];
324 int e = eLink[rIndex];
325 SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e',
326 s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e);
327 }
328#endif
329 rIndex = 0;
330 do {
331 bool forward = true;
332 bool first = true;
333 int sIndex = sLink[rIndex];
334 SkASSERT(sIndex != SK_MaxS32);
335 sLink[rIndex] = SK_MaxS32;
336 int eIndex;
337 if (sIndex < 0) {
338 eIndex = sLink[~sIndex];
339 sLink[~sIndex] = SK_MaxS32;
340 } else {
341 eIndex = eLink[sIndex];
342 eLink[sIndex] = SK_MaxS32;
343 }
344 SkASSERT(eIndex != SK_MaxS32);
345#if DEBUG_ASSEMBLE
346 SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e',
347 sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e',
348 eIndex < 0 ? ~eIndex : eIndex);
349#endif
350 do {
351 const SkPath& contour = fPartials[rIndex];
Cary Clark1d314432018-07-10 10:57:54 -0400352 if (!first) {
353 SkPoint prior, next;
Cary Clark13795082018-11-07 16:18:39 -0500354 if (!fPathPtr->getLastPt(&prior)) {
355 return;
356 }
Cary Clark1d314432018-07-10 10:57:54 -0400357 if (forward) {
358 next = contour.getPoint(0);
359 } else {
360 SkAssertResult(contour.getLastPt(&next));
361 }
362 if (prior != next) {
363 /* TODO: if there is a gap between open path written so far and path to come,
364 connect by following segments from one to the other, rather than introducing
365 a diagonal to connect the two.
366 */
367 SkDebugf("");
368 }
369 }
caryclarkeed356d2016-09-14 07:18:20 -0700370 if (forward) {
371 fPathPtr->addPath(contour,
372 first ? SkPath::kAppend_AddPathMode : SkPath::kExtend_AddPathMode);
373 } else {
374 SkASSERT(!first);
caryclark51c56782016-11-07 05:09:28 -0800375 fPathPtr->reversePathTo(contour);
caryclarkeed356d2016-09-14 07:18:20 -0700376 }
377 if (first) {
378 first = false;
379 }
380#if DEBUG_ASSEMBLE
381 SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex,
382 eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex,
383 sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex));
384#endif
385 if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) {
386 fPathPtr->close();
387 break;
388 }
389 if (forward) {
390 eIndex = eLink[rIndex];
391 SkASSERT(eIndex != SK_MaxS32);
392 eLink[rIndex] = SK_MaxS32;
393 if (eIndex >= 0) {
394 SkASSERT(sLink[eIndex] == rIndex);
395 sLink[eIndex] = SK_MaxS32;
396 } else {
397 SkASSERT(eLink[~eIndex] == ~rIndex);
398 eLink[~eIndex] = SK_MaxS32;
399 }
400 } else {
401 eIndex = sLink[rIndex];
402 SkASSERT(eIndex != SK_MaxS32);
403 sLink[rIndex] = SK_MaxS32;
404 if (eIndex >= 0) {
405 SkASSERT(eLink[eIndex] == rIndex);
406 eLink[eIndex] = SK_MaxS32;
407 } else {
408 SkASSERT(sLink[~eIndex] == ~rIndex);
409 sLink[~eIndex] = SK_MaxS32;
410 }
411 }
412 rIndex = eIndex;
413 if (rIndex < 0) {
414 forward ^= 1;
415 rIndex = ~rIndex;
416 }
417 } while (true);
418 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
419 if (sLink[rIndex] != SK_MaxS32) {
420 break;
421 }
422 }
423 } while (rIndex < linkCount);
424#if DEBUG_ASSEMBLE
425 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
426 SkASSERT(sLink[rIndex] == SK_MaxS32);
427 SkASSERT(eLink[rIndex] == SK_MaxS32);
428 }
429#endif
430 return;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000431}