blob: 9548c3b4f77957f838dd136c515df43f7a33f976 [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) {
35 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",
caryclarkeed356d2016-09-14 07:18:20 -070038 pt1.fX, pt1.fY, pt2->fPt.fX, pt2->fPt.fY, weight);
caryclark1049f122015-04-20 08:31:59 -070039#endif
caryclarkeed356d2016-09-14 07:18:20 -070040 fCurrent.conicTo(pt1, pt2->fPt, 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) {
44 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",
caryclarkeed356d2016-09-14 07:18:20 -070047 pt1.fX, pt1.fY, pt2.fX, pt2.fY, pt3->fPt.fX, pt3->fPt.fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +000048#endif
caryclarkeed356d2016-09-14 07:18:20 -070049 fCurrent.cubicTo(pt1, pt2, pt3->fPt);
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 Reedf9ecb4e2018-08-08 15:14:28 +0000100 fEndPtTs.push(fFirstPtT);
101 fEndPtTs.push(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) {
147 this->update(pt2);
148#if DEBUG_PATH_CONSTRUCTION
149 SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n",
150 pt1.fX, pt1.fY, pt2->fPt.fX, pt2->fPt.fY);
151#endif
152 fCurrent.quadTo(pt1, pt2->fPt);
153}
154
155void SkPathWriter::update(const SkOpPtT* pt) {
156 if (!fDefer[1]) {
157 this->moveTo();
158 } else if (!this->matchedLast(fDefer[0])) {
159 this->lineTo();
160 }
161 fDefer[0] = fDefer[1] = pt; // set both to know that there is not a pending deferred line
162}
163
164bool SkPathWriter::someAssemblyRequired() {
165 this->finishContour();
166 return fEndPtTs.count() > 0;
167}
168
169bool SkPathWriter::changedSlopes(const SkOpPtT* ptT) const {
170 if (matchedLast(fDefer[0])) {
171 return false;
172 }
173 SkVector deferDxdy = fDefer[1]->fPt - fDefer[0]->fPt;
174 SkVector lineDxdy = ptT->fPt - fDefer[1]->fPt;
175 return deferDxdy.fX * lineDxdy.fY != deferDxdy.fY * lineDxdy.fX;
176}
177
178class DistanceLessThan {
179public:
180 DistanceLessThan(double* distances) : fDistances(distances) { }
181 double* fDistances;
182 bool operator()(const int one, const int two) {
183 return fDistances[one] < fDistances[two];
184 }
185};
186
187 /*
188 check start and end of each contour
189 if not the same, record them
190 match them up
191 connect closest
192 reassemble contour pieces into new path
193 */
194void SkPathWriter::assemble() {
195#if DEBUG_SHOW_TEST_NAME
196 SkDebugf("</div>\n");
197#endif
198 if (!this->someAssemblyRequired()) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000199 return;
200 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000201#if DEBUG_PATH_CONSTRUCTION
caryclarkeed356d2016-09-14 07:18:20 -0700202 SkDebugf("%s\n", __FUNCTION__);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000203#endif
caryclarkeed356d2016-09-14 07:18:20 -0700204 SkOpPtT const* const* runs = fEndPtTs.begin(); // starts, ends of partial contours
205 int endCount = fEndPtTs.count(); // all starts and ends
206 SkASSERT(endCount > 0);
207 SkASSERT(endCount == fPartials.count() * 2);
208#if DEBUG_ASSEMBLE
209 for (int index = 0; index < endCount; index += 2) {
210 const SkOpPtT* eStart = runs[index];
211 const SkOpPtT* eEnd = runs[index + 1];
212 SkASSERT(eStart != eEnd);
213 SkASSERT(!eStart->contains(eEnd));
214 SkDebugf("%s contour start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n", __FUNCTION__,
215 eStart->fPt.fX, eStart->fPt.fY, eEnd->fPt.fX, eEnd->fPt.fY);
216 }
217#endif
Cary Clarkcadc5062018-08-06 17:24:04 -0400218 // lengthen any partial contour adjacent to a simple segment
219 for (int pIndex = 0; pIndex < endCount; pIndex++) {
220 SkOpPtT* opPtT = const_cast<SkOpPtT*>(runs[pIndex]);
221 SkPath dummy;
222 SkPathWriter partWriter(dummy);
223 do {
224 if (!zero_or_one(opPtT->fT)) {
225 break;
226 }
227 SkOpSpanBase* opSpanBase = opPtT->span();
228 SkOpSpanBase* start = opPtT->fT ? opSpanBase->prev() : opSpanBase->upCast()->next();
229 int step = opPtT->fT ? 1 : -1;
230 const SkOpSegment* opSegment = opSpanBase->segment();
231 const SkOpSegment* nextSegment = opSegment->isSimple(&start, &step);
232 if (!nextSegment) {
233 break;
234 }
235 SkOpSpanBase* opSpanEnd = start->t() ? start->prev() : start->upCast()->next();
236 if (start->starter(opSpanEnd)->alreadyAdded()) {
237 break;
238 }
239 nextSegment->addCurveTo(start, opSpanEnd, &partWriter);
240 opPtT = opSpanEnd->ptT();
241 SkOpPtT** runsPtr = const_cast<SkOpPtT**>(&runs[pIndex]);
242 *runsPtr = opPtT;
243 } while (true);
244 partWriter.finishContour();
245 const SkTArray<SkPath>& partPartials = partWriter.partials();
246 if (!partPartials.count()) {
247 continue;
248 }
249 // if pIndex is even, reverse and prepend to fPartials; otherwise, append
250 SkPath& partial = const_cast<SkPath&>(fPartials[pIndex >> 1]);
251 const SkPath& part = partPartials[0];
252 if (pIndex & 1) {
253 partial.addPath(part, SkPath::kExtend_AddPathMode);
254 } else {
255 SkPath reverse;
256 reverse.reverseAddPath(part);
257 reverse.addPath(partial, SkPath::kExtend_AddPathMode);
258 partial = reverse;
259 }
260 }
caryclarkeed356d2016-09-14 07:18:20 -0700261 SkTDArray<int> sLink, eLink;
262 int linkCount = endCount / 2; // number of partial contours
263 sLink.append(linkCount);
264 eLink.append(linkCount);
265 int rIndex, iIndex;
266 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
267 sLink[rIndex] = eLink[rIndex] = SK_MaxS32;
268 }
269 const int entries = endCount * (endCount - 1) / 2; // folded triangle
270 SkSTArray<8, double, true> distances(entries);
271 SkSTArray<8, int, true> sortedDist(entries);
272 SkSTArray<8, int, true> distLookup(entries);
273 int rRow = 0;
274 int dIndex = 0;
275 for (rIndex = 0; rIndex < endCount - 1; ++rIndex) {
276 const SkOpPtT* oPtT = runs[rIndex];
277 for (iIndex = rIndex + 1; iIndex < endCount; ++iIndex) {
278 const SkOpPtT* iPtT = runs[iIndex];
279 double dx = iPtT->fPt.fX - oPtT->fPt.fX;
280 double dy = iPtT->fPt.fY - oPtT->fPt.fY;
281 double dist = dx * dx + dy * dy;
282 distLookup.push_back(rRow + iIndex);
283 distances.push_back(dist); // oStart distance from iStart
Ben Wagner63fd7602017-10-09 15:45:33 -0400284 sortedDist.push_back(dIndex++);
caryclarkeed356d2016-09-14 07:18:20 -0700285 }
286 rRow += endCount;
287 }
288 SkASSERT(dIndex == entries);
289 SkTQSort<int>(sortedDist.begin(), sortedDist.end() - 1, DistanceLessThan(distances.begin()));
290 int remaining = linkCount; // number of start/end pairs
291 for (rIndex = 0; rIndex < entries; ++rIndex) {
292 int pair = sortedDist[rIndex];
293 pair = distLookup[pair];
294 int row = pair / endCount;
295 int col = pair - row * endCount;
296 int ndxOne = row >> 1;
297 bool endOne = row & 1;
298 int* linkOne = endOne ? eLink.begin() : sLink.begin();
299 if (linkOne[ndxOne] != SK_MaxS32) {
300 continue;
301 }
302 int ndxTwo = col >> 1;
303 bool endTwo = col & 1;
304 int* linkTwo = endTwo ? eLink.begin() : sLink.begin();
305 if (linkTwo[ndxTwo] != SK_MaxS32) {
306 continue;
307 }
308 SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]);
309 bool flip = endOne == endTwo;
310 linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo;
311 linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne;
312 if (!--remaining) {
313 break;
314 }
315 }
316 SkASSERT(!remaining);
317#if DEBUG_ASSEMBLE
318 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
319 int s = sLink[rIndex];
320 int e = eLink[rIndex];
321 SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e',
322 s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e);
323 }
324#endif
325 rIndex = 0;
326 do {
327 bool forward = true;
328 bool first = true;
329 int sIndex = sLink[rIndex];
330 SkASSERT(sIndex != SK_MaxS32);
331 sLink[rIndex] = SK_MaxS32;
332 int eIndex;
333 if (sIndex < 0) {
334 eIndex = sLink[~sIndex];
335 sLink[~sIndex] = SK_MaxS32;
336 } else {
337 eIndex = eLink[sIndex];
338 eLink[sIndex] = SK_MaxS32;
339 }
340 SkASSERT(eIndex != SK_MaxS32);
341#if DEBUG_ASSEMBLE
342 SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e',
343 sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e',
344 eIndex < 0 ? ~eIndex : eIndex);
345#endif
346 do {
347 const SkPath& contour = fPartials[rIndex];
Cary Clark1d314432018-07-10 10:57:54 -0400348 if (!first) {
349 SkPoint prior, next;
350 SkAssertResult(fPathPtr->getLastPt(&prior));
351 if (forward) {
352 next = contour.getPoint(0);
353 } else {
354 SkAssertResult(contour.getLastPt(&next));
355 }
356 if (prior != next) {
357 /* TODO: if there is a gap between open path written so far and path to come,
358 connect by following segments from one to the other, rather than introducing
359 a diagonal to connect the two.
360 */
361 SkDebugf("");
362 }
363 }
caryclarkeed356d2016-09-14 07:18:20 -0700364 if (forward) {
365 fPathPtr->addPath(contour,
366 first ? SkPath::kAppend_AddPathMode : SkPath::kExtend_AddPathMode);
367 } else {
368 SkASSERT(!first);
caryclark51c56782016-11-07 05:09:28 -0800369 fPathPtr->reversePathTo(contour);
caryclarkeed356d2016-09-14 07:18:20 -0700370 }
371 if (first) {
372 first = false;
373 }
374#if DEBUG_ASSEMBLE
375 SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex,
376 eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex,
377 sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex));
378#endif
379 if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) {
380 fPathPtr->close();
381 break;
382 }
383 if (forward) {
384 eIndex = eLink[rIndex];
385 SkASSERT(eIndex != SK_MaxS32);
386 eLink[rIndex] = SK_MaxS32;
387 if (eIndex >= 0) {
388 SkASSERT(sLink[eIndex] == rIndex);
389 sLink[eIndex] = SK_MaxS32;
390 } else {
391 SkASSERT(eLink[~eIndex] == ~rIndex);
392 eLink[~eIndex] = SK_MaxS32;
393 }
394 } else {
395 eIndex = sLink[rIndex];
396 SkASSERT(eIndex != SK_MaxS32);
397 sLink[rIndex] = SK_MaxS32;
398 if (eIndex >= 0) {
399 SkASSERT(eLink[eIndex] == rIndex);
400 eLink[eIndex] = SK_MaxS32;
401 } else {
402 SkASSERT(sLink[~eIndex] == ~rIndex);
403 sLink[~eIndex] = SK_MaxS32;
404 }
405 }
406 rIndex = eIndex;
407 if (rIndex < 0) {
408 forward ^= 1;
409 rIndex = ~rIndex;
410 }
411 } while (true);
412 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
413 if (sLink[rIndex] != SK_MaxS32) {
414 break;
415 }
416 }
417 } while (rIndex < linkCount);
418#if DEBUG_ASSEMBLE
419 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
420 SkASSERT(sLink[rIndex] == SK_MaxS32);
421 SkASSERT(eLink[rIndex] == SK_MaxS32);
422 }
423#endif
424 return;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000425}