blob: 2c3391bb44c521a615f36fd42de766be335bc28f [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 */
caryclarkeed356d2016-09-14 07:18:20 -07007#include "SkOpSpan.h"
caryclark@google.coma5e55922013-05-07 18:51:31 +00008#include "SkPathOpsPoint.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +00009#include "SkPathWriter.h"
caryclarkeed356d2016-09-14 07:18:20 -070010#include "SkTSort.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) {
34 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",
caryclarkeed356d2016-09-14 07:18:20 -070037 pt1.fX, pt1.fY, pt2->fPt.fX, pt2->fPt.fY, weight);
caryclark1049f122015-04-20 08:31:59 -070038#endif
caryclarkeed356d2016-09-14 07:18:20 -070039 fCurrent.conicTo(pt1, pt2->fPt, 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) {
43 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",
caryclarkeed356d2016-09-14 07:18:20 -070046 pt1.fX, pt1.fY, pt2.fX, pt2.fY, pt3->fPt.fX, pt3->fPt.fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +000047#endif
caryclarkeed356d2016-09-14 07:18:20 -070048 fCurrent.cubicTo(pt1, pt2, pt3->fPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +000049}
50
caryclarkeed356d2016-09-14 07:18:20 -070051void SkPathWriter::deferredLine(const SkOpPtT* pt) {
52 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.
caryclark@google.com07393ca2013-04-08 11:47:37 +000056 return;
57 }
caryclarkeed356d2016-09-14 07:18:20 -070058 if (pt->contains(fDefer[0])) {
59 // FIXME: why we're adding a degenerate line?
60 return;
61 }
62 SkASSERT(!this->matchedLast(pt));
63 if (fDefer[1] && this->changedSlopes(pt)) {
64 this->lineTo();
caryclark@google.com07393ca2013-04-08 11:47:37 +000065 fDefer[0] = fDefer[1];
66 }
67 fDefer[1] = pt;
68}
69
caryclarkeed356d2016-09-14 07:18:20 -070070void SkPathWriter::deferredMove(const SkOpPtT* pt) {
71 if (!fDefer[1]) {
72 fFirstPtT = fDefer[0] = pt;
73 return;
caryclark@google.com07393ca2013-04-08 11:47:37 +000074 }
caryclarkeed356d2016-09-14 07:18:20 -070075 SkASSERT(fDefer[0]);
76 if (!this->matchedLast(pt)) {
77 this->finishContour();
78 fFirstPtT = fDefer[0] = pt;
79 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000080}
81
caryclarkeed356d2016-09-14 07:18:20 -070082void SkPathWriter::finishContour() {
83 if (!this->matchedLast(fDefer[0])) {
84 this->lineTo();
85 }
86 if (fCurrent.isEmpty()) {
87 return;
88 }
89 if (this->isClosed()) {
90 this->close();
91 } else {
92 SkASSERT(fDefer[1]);
93 fEndPtTs.push(fFirstPtT);
94 fEndPtTs.push(fDefer[1]);
95 fPartials.push_back(fCurrent);
96 this->init();
97 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000098}
99
100void SkPathWriter::init() {
caryclarkeed356d2016-09-14 07:18:20 -0700101 fCurrent.reset();
102 fFirstPtT = fDefer[0] = fDefer[1] = nullptr;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000103}
104
105bool SkPathWriter::isClosed() const {
caryclarkeed356d2016-09-14 07:18:20 -0700106 return this->matchedLast(fFirstPtT);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000107}
108
109void SkPathWriter::lineTo() {
caryclarkeed356d2016-09-14 07:18:20 -0700110 if (fCurrent.isEmpty()) {
111 this->moveTo();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000112 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000113#if DEBUG_PATH_CONSTRUCTION
caryclarkeed356d2016-09-14 07:18:20 -0700114 SkDebugf("path.lineTo(%1.9g,%1.9g);\n", fDefer[1]->fPt.fX, fDefer[1]->fPt.fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000115#endif
caryclarkeed356d2016-09-14 07:18:20 -0700116 fCurrent.lineTo(fDefer[1]->fPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000117}
118
caryclarkeed356d2016-09-14 07:18:20 -0700119bool SkPathWriter::matchedLast(const SkOpPtT* test) const {
120 if (test == fDefer[1]) {
121 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000122 }
caryclarkeed356d2016-09-14 07:18:20 -0700123 if (!test) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000124 return false;
125 }
caryclarkeed356d2016-09-14 07:18:20 -0700126 if (!fDefer[1]) {
127 return false;
128 }
129 return test->contains(fDefer[1]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000130}
131
132void SkPathWriter::moveTo() {
caryclarkeed356d2016-09-14 07:18:20 -0700133#if DEBUG_PATH_CONSTRUCTION
134 SkDebugf("path.moveTo(%1.9g,%1.9g);\n", fFirstPtT->fPt.fX, fFirstPtT->fPt.fY);
135#endif
136 fCurrent.moveTo(fFirstPtT->fPt);
137}
138
139void SkPathWriter::quadTo(const SkPoint& pt1, const SkOpPtT* pt2) {
140 this->update(pt2);
141#if DEBUG_PATH_CONSTRUCTION
142 SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n",
143 pt1.fX, pt1.fY, pt2->fPt.fX, pt2->fPt.fY);
144#endif
145 fCurrent.quadTo(pt1, pt2->fPt);
146}
147
148void SkPathWriter::update(const SkOpPtT* pt) {
149 if (!fDefer[1]) {
150 this->moveTo();
151 } else if (!this->matchedLast(fDefer[0])) {
152 this->lineTo();
153 }
154 fDefer[0] = fDefer[1] = pt; // set both to know that there is not a pending deferred line
155}
156
157bool SkPathWriter::someAssemblyRequired() {
158 this->finishContour();
159 return fEndPtTs.count() > 0;
160}
161
162bool SkPathWriter::changedSlopes(const SkOpPtT* ptT) const {
163 if (matchedLast(fDefer[0])) {
164 return false;
165 }
166 SkVector deferDxdy = fDefer[1]->fPt - fDefer[0]->fPt;
167 SkVector lineDxdy = ptT->fPt - fDefer[1]->fPt;
168 return deferDxdy.fX * lineDxdy.fY != deferDxdy.fY * lineDxdy.fX;
169}
170
171class DistanceLessThan {
172public:
173 DistanceLessThan(double* distances) : fDistances(distances) { }
174 double* fDistances;
175 bool operator()(const int one, const int two) {
176 return fDistances[one] < fDistances[two];
177 }
178};
179
180 /*
181 check start and end of each contour
182 if not the same, record them
183 match them up
184 connect closest
185 reassemble contour pieces into new path
186 */
187void SkPathWriter::assemble() {
188#if DEBUG_SHOW_TEST_NAME
189 SkDebugf("</div>\n");
190#endif
191 if (!this->someAssemblyRequired()) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000192 return;
193 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000194#if DEBUG_PATH_CONSTRUCTION
caryclarkeed356d2016-09-14 07:18:20 -0700195 SkDebugf("%s\n", __FUNCTION__);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000196#endif
caryclarkeed356d2016-09-14 07:18:20 -0700197 SkOpPtT const* const* runs = fEndPtTs.begin(); // starts, ends of partial contours
198 int endCount = fEndPtTs.count(); // all starts and ends
199 SkASSERT(endCount > 0);
200 SkASSERT(endCount == fPartials.count() * 2);
201#if DEBUG_ASSEMBLE
202 for (int index = 0; index < endCount; index += 2) {
203 const SkOpPtT* eStart = runs[index];
204 const SkOpPtT* eEnd = runs[index + 1];
205 SkASSERT(eStart != eEnd);
206 SkASSERT(!eStart->contains(eEnd));
207 SkDebugf("%s contour start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n", __FUNCTION__,
208 eStart->fPt.fX, eStart->fPt.fY, eEnd->fPt.fX, eEnd->fPt.fY);
209 }
210#endif
211 SkTDArray<int> sLink, eLink;
212 int linkCount = endCount / 2; // number of partial contours
213 sLink.append(linkCount);
214 eLink.append(linkCount);
215 int rIndex, iIndex;
216 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
217 sLink[rIndex] = eLink[rIndex] = SK_MaxS32;
218 }
219 const int entries = endCount * (endCount - 1) / 2; // folded triangle
220 SkSTArray<8, double, true> distances(entries);
221 SkSTArray<8, int, true> sortedDist(entries);
222 SkSTArray<8, int, true> distLookup(entries);
223 int rRow = 0;
224 int dIndex = 0;
225 for (rIndex = 0; rIndex < endCount - 1; ++rIndex) {
226 const SkOpPtT* oPtT = runs[rIndex];
227 for (iIndex = rIndex + 1; iIndex < endCount; ++iIndex) {
228 const SkOpPtT* iPtT = runs[iIndex];
229 double dx = iPtT->fPt.fX - oPtT->fPt.fX;
230 double dy = iPtT->fPt.fY - oPtT->fPt.fY;
231 double dist = dx * dx + dy * dy;
232 distLookup.push_back(rRow + iIndex);
233 distances.push_back(dist); // oStart distance from iStart
234 sortedDist.push_back(dIndex++);
235 }
236 rRow += endCount;
237 }
238 SkASSERT(dIndex == entries);
239 SkTQSort<int>(sortedDist.begin(), sortedDist.end() - 1, DistanceLessThan(distances.begin()));
240 int remaining = linkCount; // number of start/end pairs
241 for (rIndex = 0; rIndex < entries; ++rIndex) {
242 int pair = sortedDist[rIndex];
243 pair = distLookup[pair];
244 int row = pair / endCount;
245 int col = pair - row * endCount;
246 int ndxOne = row >> 1;
247 bool endOne = row & 1;
248 int* linkOne = endOne ? eLink.begin() : sLink.begin();
249 if (linkOne[ndxOne] != SK_MaxS32) {
250 continue;
251 }
252 int ndxTwo = col >> 1;
253 bool endTwo = col & 1;
254 int* linkTwo = endTwo ? eLink.begin() : sLink.begin();
255 if (linkTwo[ndxTwo] != SK_MaxS32) {
256 continue;
257 }
258 SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]);
259 bool flip = endOne == endTwo;
260 linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo;
261 linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne;
262 if (!--remaining) {
263 break;
264 }
265 }
266 SkASSERT(!remaining);
267#if DEBUG_ASSEMBLE
268 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
269 int s = sLink[rIndex];
270 int e = eLink[rIndex];
271 SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e',
272 s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e);
273 }
274#endif
275 rIndex = 0;
276 do {
277 bool forward = true;
278 bool first = true;
279 int sIndex = sLink[rIndex];
280 SkASSERT(sIndex != SK_MaxS32);
281 sLink[rIndex] = SK_MaxS32;
282 int eIndex;
283 if (sIndex < 0) {
284 eIndex = sLink[~sIndex];
285 sLink[~sIndex] = SK_MaxS32;
286 } else {
287 eIndex = eLink[sIndex];
288 eLink[sIndex] = SK_MaxS32;
289 }
290 SkASSERT(eIndex != SK_MaxS32);
291#if DEBUG_ASSEMBLE
292 SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e',
293 sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e',
294 eIndex < 0 ? ~eIndex : eIndex);
295#endif
296 do {
297 const SkPath& contour = fPartials[rIndex];
298 if (forward) {
299 fPathPtr->addPath(contour,
300 first ? SkPath::kAppend_AddPathMode : SkPath::kExtend_AddPathMode);
301 } else {
302 SkASSERT(!first);
303 fPathPtr->reverseAddPath(contour);
304 }
305 if (first) {
306 first = false;
307 }
308#if DEBUG_ASSEMBLE
309 SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex,
310 eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex,
311 sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex));
312#endif
313 if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) {
314 fPathPtr->close();
315 break;
316 }
317 if (forward) {
318 eIndex = eLink[rIndex];
319 SkASSERT(eIndex != SK_MaxS32);
320 eLink[rIndex] = SK_MaxS32;
321 if (eIndex >= 0) {
322 SkASSERT(sLink[eIndex] == rIndex);
323 sLink[eIndex] = SK_MaxS32;
324 } else {
325 SkASSERT(eLink[~eIndex] == ~rIndex);
326 eLink[~eIndex] = SK_MaxS32;
327 }
328 } else {
329 eIndex = sLink[rIndex];
330 SkASSERT(eIndex != SK_MaxS32);
331 sLink[rIndex] = SK_MaxS32;
332 if (eIndex >= 0) {
333 SkASSERT(eLink[eIndex] == rIndex);
334 eLink[eIndex] = SK_MaxS32;
335 } else {
336 SkASSERT(sLink[~eIndex] == ~rIndex);
337 sLink[~eIndex] = SK_MaxS32;
338 }
339 }
340 rIndex = eIndex;
341 if (rIndex < 0) {
342 forward ^= 1;
343 rIndex = ~rIndex;
344 }
345 } while (true);
346 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
347 if (sLink[rIndex] != SK_MaxS32) {
348 break;
349 }
350 }
351 } while (rIndex < linkCount);
352#if DEBUG_ASSEMBLE
353 for (rIndex = 0; rIndex < linkCount; ++rIndex) {
354 SkASSERT(sLink[rIndex] == SK_MaxS32);
355 SkASSERT(eLink[rIndex] == SK_MaxS32);
356 }
357#endif
358 return;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000359}