blob: 459c7316094fddcb78f8643e83ea3fdb7e51a2d7 [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 */
Cary Clarkcadc5062018-08-06 17:24:04 -04007#include "SkOpSegment.h"
caryclarkeed356d2016-09-14 07:18:20 -07008#include "SkOpSpan.h"
caryclark@google.coma5e55922013-05-07 18:51:31 +00009#include "SkPathOpsPoint.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000010#include "SkPathWriter.h"
caryclarkeed356d2016-09-14 07:18:20 -070011#include "SkTSort.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;
189 bool operator()(const int one, const int two) {
190 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() {
202#if DEBUG_SHOW_TEST_NAME
203 SkDebugf("</div>\n");
204#endif
205 if (!this->someAssemblyRequired()) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000206 return;
207 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000208#if DEBUG_PATH_CONSTRUCTION
caryclarkeed356d2016-09-14 07:18:20 -0700209 SkDebugf("%s\n", __FUNCTION__);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000210#endif
caryclarkeed356d2016-09-14 07:18:20 -0700211 SkOpPtT const* const* runs = fEndPtTs.begin(); // starts, ends of partial contours
212 int endCount = fEndPtTs.count(); // all starts and ends
213 SkASSERT(endCount > 0);
214 SkASSERT(endCount == fPartials.count() * 2);
215#if DEBUG_ASSEMBLE
216 for (int index = 0; index < endCount; index += 2) {
217 const SkOpPtT* eStart = runs[index];
218 const SkOpPtT* eEnd = runs[index + 1];
219 SkASSERT(eStart != eEnd);
220 SkASSERT(!eStart->contains(eEnd));
221 SkDebugf("%s contour start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n", __FUNCTION__,
222 eStart->fPt.fX, eStart->fPt.fY, eEnd->fPt.fX, eEnd->fPt.fY);
223 }
224#endif
Cary Clarkcadc5062018-08-06 17:24:04 -0400225 // lengthen any partial contour adjacent to a simple segment
226 for (int pIndex = 0; pIndex < endCount; pIndex++) {
227 SkOpPtT* opPtT = const_cast<SkOpPtT*>(runs[pIndex]);
228 SkPath dummy;
229 SkPathWriter partWriter(dummy);
230 do {
231 if (!zero_or_one(opPtT->fT)) {
232 break;
233 }
234 SkOpSpanBase* opSpanBase = opPtT->span();
235 SkOpSpanBase* start = opPtT->fT ? opSpanBase->prev() : opSpanBase->upCast()->next();
236 int step = opPtT->fT ? 1 : -1;
237 const SkOpSegment* opSegment = opSpanBase->segment();
238 const SkOpSegment* nextSegment = opSegment->isSimple(&start, &step);
239 if (!nextSegment) {
240 break;
241 }
242 SkOpSpanBase* opSpanEnd = start->t() ? start->prev() : start->upCast()->next();
243 if (start->starter(opSpanEnd)->alreadyAdded()) {
244 break;
245 }
246 nextSegment->addCurveTo(start, opSpanEnd, &partWriter);
247 opPtT = opSpanEnd->ptT();
248 SkOpPtT** runsPtr = const_cast<SkOpPtT**>(&runs[pIndex]);
249 *runsPtr = opPtT;
250 } while (true);
251 partWriter.finishContour();
252 const SkTArray<SkPath>& partPartials = partWriter.partials();
253 if (!partPartials.count()) {
254 continue;
255 }
256 // if pIndex is even, reverse and prepend to fPartials; otherwise, append
257 SkPath& partial = const_cast<SkPath&>(fPartials[pIndex >> 1]);
258 const SkPath& part = partPartials[0];
259 if (pIndex & 1) {
260 partial.addPath(part, SkPath::kExtend_AddPathMode);
261 } else {
262 SkPath reverse;
263 reverse.reverseAddPath(part);
264 reverse.addPath(partial, SkPath::kExtend_AddPathMode);
265 partial = reverse;
266 }
267 }
caryclarkeed356d2016-09-14 07:18:20 -0700268 SkTDArray<int> sLink, eLink;
269 int linkCount = endCount / 2; // number of partial contours
270 sLink.append(linkCount);
271 eLink.append(linkCount);
272 int rIndex, iIndex;
273 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
274 sLink[rIndex] = eLink[rIndex] = SK_MaxS32;
275 }
276 const int entries = endCount * (endCount - 1) / 2; // folded triangle
277 SkSTArray<8, double, true> distances(entries);
278 SkSTArray<8, int, true> sortedDist(entries);
279 SkSTArray<8, int, true> distLookup(entries);
280 int rRow = 0;
281 int dIndex = 0;
282 for (rIndex = 0; rIndex < endCount - 1; ++rIndex) {
283 const SkOpPtT* oPtT = runs[rIndex];
284 for (iIndex = rIndex + 1; iIndex < endCount; ++iIndex) {
285 const SkOpPtT* iPtT = runs[iIndex];
286 double dx = iPtT->fPt.fX - oPtT->fPt.fX;
287 double dy = iPtT->fPt.fY - oPtT->fPt.fY;
288 double dist = dx * dx + dy * dy;
289 distLookup.push_back(rRow + iIndex);
290 distances.push_back(dist); // oStart distance from iStart
Ben Wagner63fd7602017-10-09 15:45:33 -0400291 sortedDist.push_back(dIndex++);
caryclarkeed356d2016-09-14 07:18:20 -0700292 }
293 rRow += endCount;
294 }
295 SkASSERT(dIndex == entries);
296 SkTQSort<int>(sortedDist.begin(), sortedDist.end() - 1, DistanceLessThan(distances.begin()));
297 int remaining = linkCount; // number of start/end pairs
298 for (rIndex = 0; rIndex < entries; ++rIndex) {
299 int pair = sortedDist[rIndex];
300 pair = distLookup[pair];
301 int row = pair / endCount;
302 int col = pair - row * endCount;
303 int ndxOne = row >> 1;
304 bool endOne = row & 1;
305 int* linkOne = endOne ? eLink.begin() : sLink.begin();
306 if (linkOne[ndxOne] != SK_MaxS32) {
307 continue;
308 }
309 int ndxTwo = col >> 1;
310 bool endTwo = col & 1;
311 int* linkTwo = endTwo ? eLink.begin() : sLink.begin();
312 if (linkTwo[ndxTwo] != SK_MaxS32) {
313 continue;
314 }
315 SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]);
316 bool flip = endOne == endTwo;
317 linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo;
318 linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne;
319 if (!--remaining) {
320 break;
321 }
322 }
323 SkASSERT(!remaining);
324#if DEBUG_ASSEMBLE
325 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
326 int s = sLink[rIndex];
327 int e = eLink[rIndex];
328 SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e',
329 s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e);
330 }
331#endif
332 rIndex = 0;
333 do {
334 bool forward = true;
335 bool first = true;
336 int sIndex = sLink[rIndex];
337 SkASSERT(sIndex != SK_MaxS32);
338 sLink[rIndex] = SK_MaxS32;
339 int eIndex;
340 if (sIndex < 0) {
341 eIndex = sLink[~sIndex];
342 sLink[~sIndex] = SK_MaxS32;
343 } else {
344 eIndex = eLink[sIndex];
345 eLink[sIndex] = SK_MaxS32;
346 }
347 SkASSERT(eIndex != SK_MaxS32);
348#if DEBUG_ASSEMBLE
349 SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e',
350 sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e',
351 eIndex < 0 ? ~eIndex : eIndex);
352#endif
353 do {
354 const SkPath& contour = fPartials[rIndex];
Cary Clark1d314432018-07-10 10:57:54 -0400355 if (!first) {
356 SkPoint prior, next;
Cary Clark13795082018-11-07 16:18:39 -0500357 if (!fPathPtr->getLastPt(&prior)) {
358 return;
359 }
Cary Clark1d314432018-07-10 10:57:54 -0400360 if (forward) {
361 next = contour.getPoint(0);
362 } else {
363 SkAssertResult(contour.getLastPt(&next));
364 }
365 if (prior != next) {
366 /* TODO: if there is a gap between open path written so far and path to come,
367 connect by following segments from one to the other, rather than introducing
368 a diagonal to connect the two.
369 */
370 SkDebugf("");
371 }
372 }
caryclarkeed356d2016-09-14 07:18:20 -0700373 if (forward) {
374 fPathPtr->addPath(contour,
375 first ? SkPath::kAppend_AddPathMode : SkPath::kExtend_AddPathMode);
376 } else {
377 SkASSERT(!first);
caryclark51c56782016-11-07 05:09:28 -0800378 fPathPtr->reversePathTo(contour);
caryclarkeed356d2016-09-14 07:18:20 -0700379 }
380 if (first) {
381 first = false;
382 }
383#if DEBUG_ASSEMBLE
384 SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex,
385 eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex,
386 sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex));
387#endif
388 if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) {
389 fPathPtr->close();
390 break;
391 }
392 if (forward) {
393 eIndex = eLink[rIndex];
394 SkASSERT(eIndex != SK_MaxS32);
395 eLink[rIndex] = SK_MaxS32;
396 if (eIndex >= 0) {
397 SkASSERT(sLink[eIndex] == rIndex);
398 sLink[eIndex] = SK_MaxS32;
399 } else {
400 SkASSERT(eLink[~eIndex] == ~rIndex);
401 eLink[~eIndex] = SK_MaxS32;
402 }
403 } else {
404 eIndex = sLink[rIndex];
405 SkASSERT(eIndex != SK_MaxS32);
406 sLink[rIndex] = SK_MaxS32;
407 if (eIndex >= 0) {
408 SkASSERT(eLink[eIndex] == rIndex);
409 eLink[eIndex] = SK_MaxS32;
410 } else {
411 SkASSERT(sLink[~eIndex] == ~rIndex);
412 sLink[~eIndex] = SK_MaxS32;
413 }
414 }
415 rIndex = eIndex;
416 if (rIndex < 0) {
417 forward ^= 1;
418 rIndex = ~rIndex;
419 }
420 } while (true);
421 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
422 if (sLink[rIndex] != SK_MaxS32) {
423 break;
424 }
425 }
426 } while (rIndex < linkCount);
427#if DEBUG_ASSEMBLE
428 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
429 SkASSERT(sLink[rIndex] == SK_MaxS32);
430 SkASSERT(eLink[rIndex] == SK_MaxS32);
431 }
432#endif
433 return;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000434}