Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/pathops/SkPathWriter.cpp

Issue 2321973005: Rewriting path writer (Closed)
Patch Set: revert unneeded test changes Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/pathops/SkPathWriter.h ('k') | tests/PathOpsDebug.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 #include "SkOpSpan.h"
7 #include "SkPathOpsPoint.h" 8 #include "SkPathOpsPoint.h"
8 #include "SkPathWriter.h" 9 #include "SkPathWriter.h"
10 #include "SkTSort.h"
9 11
10 // wrap path to keep track of whether the contour is initialized and non-empty 12 // wrap path to keep track of whether the contour is initialized and non-empty
11 SkPathWriter::SkPathWriter(SkPath& path) 13 SkPathWriter::SkPathWriter(SkPath& path)
12 : fPathPtr(&path) 14 : fPathPtr(&path)
13 , fCloses(0)
14 , fMoves(0)
15 { 15 {
16 init(); 16 init();
17 } 17 }
18 18
19 void SkPathWriter::close() { 19 void SkPathWriter::close() {
20 if (!fHasMove) { 20 if (fCurrent.isEmpty()) {
21 return; 21 return;
22 } 22 }
23 bool callClose = isClosed(); 23 SkASSERT(this->isClosed());
24 lineTo(); 24 #if DEBUG_PATH_CONSTRUCTION
25 if (fEmpty) { 25 SkDebugf("path.close();\n");
26 return; 26 #endif
27 } 27 fCurrent.close();
28 if (callClose) { 28 fPathPtr->addPath(fCurrent);
29 #if DEBUG_PATH_CONSTRUCTION 29 fCurrent.reset();
30 SkDebugf("path.close();\n");
31 #endif
32 fPathPtr->close();
33 fCloses++;
34 }
35 init(); 30 init();
36 } 31 }
37 32
38 void SkPathWriter::conicTo(const SkPoint& pt1, const SkPoint& pt2, SkScalar weig ht) { 33 void SkPathWriter::conicTo(const SkPoint& pt1, const SkOpPtT* pt2, SkScalar weig ht) {
39 lineTo(); 34 this->update(pt2);
40 if (fEmpty && AlmostEqualUlps(fDefer[0], pt1) && AlmostEqualUlps(pt1, pt2)) {
41 deferredLine(pt2);
42 return;
43 }
44 moveTo();
45 fDefer[1] = pt2;
46 nudge();
47 fDefer[0] = fDefer[1];
48 #if DEBUG_PATH_CONSTRUCTION 35 #if DEBUG_PATH_CONSTRUCTION
49 SkDebugf("path.conicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g);\n", 36 SkDebugf("path.conicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g);\n",
50 pt1.fX, pt1.fY, fDefer[1].fX, fDefer[1].fY, weight); 37 pt1.fX, pt1.fY, pt2->fPt.fX, pt2->fPt.fY, weight);
51 #endif 38 #endif
52 fPathPtr->conicTo(pt1.fX, pt1.fY, fDefer[1].fX, fDefer[1].fY, weight); 39 fCurrent.conicTo(pt1, pt2->fPt, weight);
53 fEmpty = false; 40 }
54 } 41
55 42 void SkPathWriter::cubicTo(const SkPoint& pt1, const SkPoint& pt2, const SkOpPtT * pt3) {
56 void SkPathWriter::cubicTo(const SkPoint& pt1, const SkPoint& pt2, const SkPoint & pt3) { 43 this->update(pt3);
57 lineTo();
58 if (fEmpty && AlmostEqualUlps(fDefer[0], pt1) && AlmostEqualUlps(pt1, pt2)
59 && AlmostEqualUlps(pt2, pt3)) {
60 deferredLine(pt3);
61 return;
62 }
63 moveTo();
64 fDefer[1] = pt3;
65 nudge();
66 fDefer[0] = fDefer[1];
67 #if DEBUG_PATH_CONSTRUCTION 44 #if DEBUG_PATH_CONSTRUCTION
68 SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n", 45 SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n",
69 pt1.fX, pt1.fY, pt2.fX, pt2.fY, fDefer[1].fX, fDefer[1].fY); 46 pt1.fX, pt1.fY, pt2.fX, pt2.fY, pt3->fPt.fX, pt3->fPt.fY);
70 #endif 47 #endif
71 fPathPtr->cubicTo(pt1.fX, pt1.fY, pt2.fX, pt2.fY, fDefer[1].fX, fDefer[1].fY ); 48 fCurrent.cubicTo(pt1, pt2, pt3->fPt);
72 fEmpty = false; 49 }
73 } 50
74 51 void SkPathWriter::deferredLine(const SkOpPtT* pt) {
75 void SkPathWriter::deferredLine(const SkPoint& pt) { 52 SkASSERT(fFirstPtT);
76 if (pt == fDefer[1]) { 53 SkASSERT(fDefer[0]);
77 return; 54 if (fDefer[0] == pt) {
78 } 55 // FIXME: why we're adding a degenerate line? Caller should have preflig hted this.
79 if (changedSlopes(pt)) { 56 return;
80 lineTo(); 57 }
58 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();
81 fDefer[0] = fDefer[1]; 65 fDefer[0] = fDefer[1];
82 } 66 }
83 fDefer[1] = pt; 67 fDefer[1] = pt;
84 } 68 }
85 69
86 void SkPathWriter::deferredMove(const SkPoint& pt) { 70 void SkPathWriter::deferredMove(const SkOpPtT* pt) {
87 fMoved = true; 71 if (!fDefer[1]) {
88 fHasMove = true; 72 fFirstPtT = fDefer[0] = pt;
89 fEmpty = true; 73 return;
90 fDefer[0] = fDefer[1] = pt; 74 }
91 } 75 SkASSERT(fDefer[0]);
92 76 if (!this->matchedLast(pt)) {
93 void SkPathWriter::deferredMoveLine(const SkPoint& pt) { 77 this->finishContour();
94 if (!fHasMove) { 78 fFirstPtT = fDefer[0] = pt;
95 deferredMove(pt); 79 }
96 } 80 }
97 deferredLine(pt); 81
98 } 82 void SkPathWriter::finishContour() {
99 83 if (!this->matchedLast(fDefer[0])) {
100 bool SkPathWriter::hasMove() const { 84 this->lineTo();
101 return fHasMove; 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 }
102 } 98 }
103 99
104 void SkPathWriter::init() { 100 void SkPathWriter::init() {
105 fEmpty = true; 101 fCurrent.reset();
106 fHasMove = false; 102 fFirstPtT = fDefer[0] = fDefer[1] = nullptr;
107 fMoved = false;
108 } 103 }
109 104
110 bool SkPathWriter::isClosed() const { 105 bool SkPathWriter::isClosed() const {
111 return !fEmpty && SkDPoint::ApproximatelyEqual(fFirstPt, fDefer[1]); 106 return this->matchedLast(fFirstPtT);
112 } 107 }
113 108
114 void SkPathWriter::lineTo() { 109 void SkPathWriter::lineTo() {
115 if (fDefer[0] == fDefer[1]) { 110 if (fCurrent.isEmpty()) {
116 return; 111 this->moveTo();
117 } 112 }
118 moveTo(); 113 #if DEBUG_PATH_CONSTRUCTION
119 nudge(); 114 SkDebugf("path.lineTo(%1.9g,%1.9g);\n", fDefer[1]->fPt.fX, fDefer[1]->fPt.fY );
120 fEmpty = false; 115 #endif
121 #if DEBUG_PATH_CONSTRUCTION 116 fCurrent.lineTo(fDefer[1]->fPt);
122 SkDebugf("path.lineTo(%1.9g,%1.9g);\n", fDefer[1].fX, fDefer[1].fY); 117 }
123 #endif 118
124 fPathPtr->lineTo(fDefer[1].fX, fDefer[1].fY); 119 bool SkPathWriter::matchedLast(const SkOpPtT* test) const {
125 fDefer[0] = fDefer[1]; 120 if (test == fDefer[1]) {
126 } 121 return true;
127 122 }
128 const SkPath* SkPathWriter::nativePath() const { 123 if (!test) {
129 return fPathPtr; 124 return false;
130 } 125 }
131 126 if (!fDefer[1]) {
132 void SkPathWriter::nudge() { 127 return false;
133 if (fEmpty || !AlmostEqualUlps(fDefer[1].fX, fFirstPt.fX) 128 }
134 || !AlmostEqualUlps(fDefer[1].fY, fFirstPt.fY)) { 129 return test->contains(fDefer[1]);
135 return; 130 }
136 } 131
137 fDefer[1] = fFirstPt; 132 void SkPathWriter::moveTo() {
138 } 133 #if DEBUG_PATH_CONSTRUCTION
139 134 SkDebugf("path.moveTo(%1.9g,%1.9g);\n", fFirstPtT->fPt.fX, fFirstPtT->fPt.fY );
140 void SkPathWriter::quadTo(const SkPoint& pt1, const SkPoint& pt2) { 135 #endif
141 lineTo(); 136 fCurrent.moveTo(fFirstPtT->fPt);
142 if (fEmpty && AlmostEqualUlps(fDefer[0], pt1) && AlmostEqualUlps(pt1, pt2)) { 137 }
143 deferredLine(pt2); 138
144 return; 139 void SkPathWriter::quadTo(const SkPoint& pt1, const SkOpPtT* pt2) {
145 } 140 this->update(pt2);
146 moveTo();
147 fDefer[1] = pt2;
148 nudge();
149 fDefer[0] = fDefer[1];
150 #if DEBUG_PATH_CONSTRUCTION 141 #if DEBUG_PATH_CONSTRUCTION
151 SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n", 142 SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n",
152 pt1.fX, pt1.fY, fDefer[1].fX, fDefer[1].fY); 143 pt1.fX, pt1.fY, pt2->fPt.fX, pt2->fPt.fY);
153 #endif 144 #endif
154 fPathPtr->quadTo(pt1.fX, pt1.fY, fDefer[1].fX, fDefer[1].fY); 145 fCurrent.quadTo(pt1, pt2->fPt);
155 fEmpty = false; 146 }
156 } 147
157 148 void SkPathWriter::update(const SkOpPtT* pt) {
158 bool SkPathWriter::someAssemblyRequired() const { 149 if (!fDefer[1]) {
159 return fCloses < fMoves; 150 this->moveTo();
160 } 151 } else if (!this->matchedLast(fDefer[0])) {
161 152 this->lineTo();
162 bool SkPathWriter::changedSlopes(const SkPoint& pt) const { 153 }
163 if (fDefer[0] == fDefer[1]) { 154 fDefer[0] = fDefer[1] = pt; // set both to know that there is not a pending deferred line
155 }
156
157 bool SkPathWriter::someAssemblyRequired() {
158 this->finishContour();
159 return fEndPtTs.count() > 0;
160 }
161
162 bool SkPathWriter::changedSlopes(const SkOpPtT* ptT) const {
163 if (matchedLast(fDefer[0])) {
164 return false; 164 return false;
165 } 165 }
166 SkScalar deferDx = fDefer[1].fX - fDefer[0].fX; 166 SkVector deferDxdy = fDefer[1]->fPt - fDefer[0]->fPt;
167 SkScalar deferDy = fDefer[1].fY - fDefer[0].fY; 167 SkVector lineDxdy = ptT->fPt - fDefer[1]->fPt;
168 SkScalar lineDx = pt.fX - fDefer[1].fX; 168 return deferDxdy.fX * lineDxdy.fY != deferDxdy.fY * lineDxdy.fX;
169 SkScalar lineDy = pt.fY - fDefer[1].fY; 169 }
170 return deferDx * lineDy != deferDy * lineDx; 170
171 } 171 class DistanceLessThan {
172 172 public:
173 void SkPathWriter::moveTo() { 173 DistanceLessThan(double* distances) : fDistances(distances) { }
174 if (!fMoved) { 174 double* fDistances;
175 return; 175 bool operator()(const int one, const int two) {
176 } 176 return fDistances[one] < fDistances[two];
177 fFirstPt = fDefer[0]; 177 }
178 #if DEBUG_PATH_CONSTRUCTION 178 };
179 SkDebugf("path.moveTo(%1.9g,%1.9g);\n", fDefer[0].fX, fDefer[0].fY); 179
180 #endif 180 /*
181 fPathPtr->moveTo(fDefer[0].fX, fDefer[0].fY); 181 check start and end of each contour
182 fMoved = false; 182 if not the same, record them
183 fMoves++; 183 match them up
184 } 184 connect closest
185 reassemble contour pieces into new path
186 */
187 void SkPathWriter::assemble() {
188 #if DEBUG_SHOW_TEST_NAME
189 SkDebugf("</div>\n");
190 #endif
191 if (!this->someAssemblyRequired()) {
192 return;
193 }
194 #if DEBUG_PATH_CONSTRUCTION
195 SkDebugf("%s\n", __FUNCTION__);
196 #endif
197 SkOpPtT const* const* runs = fEndPtTs.begin(); // starts, ends of partial c ontours
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", __FUNCTIO N__,
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(dis tances.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_Ad dPathMode);
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;
359 }
OLDNEW
« no previous file with comments | « src/pathops/SkPathWriter.h ('k') | tests/PathOpsDebug.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698