caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 7 | #include "SkOpSpan.h" |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 8 | #include "SkPathOpsPoint.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 9 | #include "SkPathWriter.h" |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 10 | #include "SkTSort.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 11 | |
| 12 | // wrap path to keep track of whether the contour is initialized and non-empty |
| 13 | SkPathWriter::SkPathWriter(SkPath& path) |
| 14 | : fPathPtr(&path) |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 15 | { |
| 16 | init(); |
| 17 | } |
| 18 | |
| 19 | void SkPathWriter::close() { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 20 | if (fCurrent.isEmpty()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 21 | return; |
| 22 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 23 | SkASSERT(this->isClosed()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 24 | #if DEBUG_PATH_CONSTRUCTION |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 25 | SkDebugf("path.close();\n"); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 26 | #endif |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 27 | fCurrent.close(); |
| 28 | fPathPtr->addPath(fCurrent); |
| 29 | fCurrent.reset(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 30 | init(); |
| 31 | } |
| 32 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 33 | void SkPathWriter::conicTo(const SkPoint& pt1, const SkOpPtT* pt2, SkScalar weight) { |
| 34 | this->update(pt2); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 35 | #if DEBUG_PATH_CONSTRUCTION |
| 36 | SkDebugf("path.conicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g);\n", |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 37 | pt1.fX, pt1.fY, pt2->fPt.fX, pt2->fPt.fY, weight); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 38 | #endif |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 39 | fCurrent.conicTo(pt1, pt2->fPt, weight); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 40 | } |
| 41 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 42 | void SkPathWriter::cubicTo(const SkPoint& pt1, const SkPoint& pt2, const SkOpPtT* pt3) { |
| 43 | this->update(pt3); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 44 | #if DEBUG_PATH_CONSTRUCTION |
| 45 | SkDebugf("path.cubicTo(%1.9g,%1.9g, %1.9g,%1.9g, %1.9g,%1.9g);\n", |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 46 | pt1.fX, pt1.fY, pt2.fX, pt2.fY, pt3->fPt.fX, pt3->fPt.fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 47 | #endif |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 48 | fCurrent.cubicTo(pt1, pt2, pt3->fPt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 49 | } |
| 50 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 51 | void 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.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 56 | return; |
| 57 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 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(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 65 | fDefer[0] = fDefer[1]; |
| 66 | } |
| 67 | fDefer[1] = pt; |
| 68 | } |
| 69 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 70 | void SkPathWriter::deferredMove(const SkOpPtT* pt) { |
| 71 | if (!fDefer[1]) { |
| 72 | fFirstPtT = fDefer[0] = pt; |
| 73 | return; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 74 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 75 | SkASSERT(fDefer[0]); |
| 76 | if (!this->matchedLast(pt)) { |
| 77 | this->finishContour(); |
| 78 | fFirstPtT = fDefer[0] = pt; |
| 79 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 80 | } |
| 81 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 82 | void SkPathWriter::finishContour() { |
| 83 | if (!this->matchedLast(fDefer[0])) { |
caryclark | 1c10607 | 2016-09-22 12:52:20 -0700 | [diff] [blame] | 84 | if (!fDefer[1]) { |
| 85 | return; |
| 86 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 87 | this->lineTo(); |
| 88 | } |
| 89 | if (fCurrent.isEmpty()) { |
| 90 | return; |
| 91 | } |
| 92 | if (this->isClosed()) { |
| 93 | this->close(); |
| 94 | } else { |
| 95 | SkASSERT(fDefer[1]); |
| 96 | fEndPtTs.push(fFirstPtT); |
| 97 | fEndPtTs.push(fDefer[1]); |
| 98 | fPartials.push_back(fCurrent); |
| 99 | this->init(); |
| 100 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void SkPathWriter::init() { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 104 | fCurrent.reset(); |
| 105 | fFirstPtT = fDefer[0] = fDefer[1] = nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool SkPathWriter::isClosed() const { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 109 | return this->matchedLast(fFirstPtT); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void SkPathWriter::lineTo() { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 113 | if (fCurrent.isEmpty()) { |
| 114 | this->moveTo(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 115 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 116 | #if DEBUG_PATH_CONSTRUCTION |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 117 | SkDebugf("path.lineTo(%1.9g,%1.9g);\n", fDefer[1]->fPt.fX, fDefer[1]->fPt.fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 118 | #endif |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 119 | fCurrent.lineTo(fDefer[1]->fPt); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 120 | } |
| 121 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 122 | bool SkPathWriter::matchedLast(const SkOpPtT* test) const { |
| 123 | if (test == fDefer[1]) { |
| 124 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 125 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 126 | if (!test) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 127 | return false; |
| 128 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 129 | if (!fDefer[1]) { |
| 130 | return false; |
| 131 | } |
| 132 | return test->contains(fDefer[1]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void SkPathWriter::moveTo() { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 136 | #if DEBUG_PATH_CONSTRUCTION |
| 137 | SkDebugf("path.moveTo(%1.9g,%1.9g);\n", fFirstPtT->fPt.fX, fFirstPtT->fPt.fY); |
| 138 | #endif |
| 139 | fCurrent.moveTo(fFirstPtT->fPt); |
| 140 | } |
| 141 | |
| 142 | void SkPathWriter::quadTo(const SkPoint& pt1, const SkOpPtT* pt2) { |
| 143 | this->update(pt2); |
| 144 | #if DEBUG_PATH_CONSTRUCTION |
| 145 | SkDebugf("path.quadTo(%1.9g,%1.9g, %1.9g,%1.9g);\n", |
| 146 | pt1.fX, pt1.fY, pt2->fPt.fX, pt2->fPt.fY); |
| 147 | #endif |
| 148 | fCurrent.quadTo(pt1, pt2->fPt); |
| 149 | } |
| 150 | |
| 151 | void SkPathWriter::update(const SkOpPtT* pt) { |
| 152 | if (!fDefer[1]) { |
| 153 | this->moveTo(); |
| 154 | } else if (!this->matchedLast(fDefer[0])) { |
| 155 | this->lineTo(); |
| 156 | } |
| 157 | fDefer[0] = fDefer[1] = pt; // set both to know that there is not a pending deferred line |
| 158 | } |
| 159 | |
| 160 | bool SkPathWriter::someAssemblyRequired() { |
| 161 | this->finishContour(); |
| 162 | return fEndPtTs.count() > 0; |
| 163 | } |
| 164 | |
| 165 | bool SkPathWriter::changedSlopes(const SkOpPtT* ptT) const { |
| 166 | if (matchedLast(fDefer[0])) { |
| 167 | return false; |
| 168 | } |
| 169 | SkVector deferDxdy = fDefer[1]->fPt - fDefer[0]->fPt; |
| 170 | SkVector lineDxdy = ptT->fPt - fDefer[1]->fPt; |
| 171 | return deferDxdy.fX * lineDxdy.fY != deferDxdy.fY * lineDxdy.fX; |
| 172 | } |
| 173 | |
| 174 | class DistanceLessThan { |
| 175 | public: |
| 176 | DistanceLessThan(double* distances) : fDistances(distances) { } |
| 177 | double* fDistances; |
| 178 | bool operator()(const int one, const int two) { |
| 179 | return fDistances[one] < fDistances[two]; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | /* |
| 184 | check start and end of each contour |
| 185 | if not the same, record them |
| 186 | match them up |
| 187 | connect closest |
| 188 | reassemble contour pieces into new path |
| 189 | */ |
| 190 | void SkPathWriter::assemble() { |
| 191 | #if DEBUG_SHOW_TEST_NAME |
| 192 | SkDebugf("</div>\n"); |
| 193 | #endif |
| 194 | if (!this->someAssemblyRequired()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 195 | return; |
| 196 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 197 | #if DEBUG_PATH_CONSTRUCTION |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 198 | SkDebugf("%s\n", __FUNCTION__); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 199 | #endif |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 200 | SkOpPtT const* const* runs = fEndPtTs.begin(); // starts, ends of partial contours |
| 201 | int endCount = fEndPtTs.count(); // all starts and ends |
| 202 | SkASSERT(endCount > 0); |
| 203 | SkASSERT(endCount == fPartials.count() * 2); |
| 204 | #if DEBUG_ASSEMBLE |
| 205 | for (int index = 0; index < endCount; index += 2) { |
| 206 | const SkOpPtT* eStart = runs[index]; |
| 207 | const SkOpPtT* eEnd = runs[index + 1]; |
| 208 | SkASSERT(eStart != eEnd); |
| 209 | SkASSERT(!eStart->contains(eEnd)); |
| 210 | SkDebugf("%s contour start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n", __FUNCTION__, |
| 211 | eStart->fPt.fX, eStart->fPt.fY, eEnd->fPt.fX, eEnd->fPt.fY); |
| 212 | } |
| 213 | #endif |
| 214 | SkTDArray<int> sLink, eLink; |
| 215 | int linkCount = endCount / 2; // number of partial contours |
| 216 | sLink.append(linkCount); |
| 217 | eLink.append(linkCount); |
| 218 | int rIndex, iIndex; |
| 219 | for (rIndex = 0; rIndex < linkCount; ++rIndex) { |
| 220 | sLink[rIndex] = eLink[rIndex] = SK_MaxS32; |
| 221 | } |
| 222 | const int entries = endCount * (endCount - 1) / 2; // folded triangle |
| 223 | SkSTArray<8, double, true> distances(entries); |
| 224 | SkSTArray<8, int, true> sortedDist(entries); |
| 225 | SkSTArray<8, int, true> distLookup(entries); |
| 226 | int rRow = 0; |
| 227 | int dIndex = 0; |
| 228 | for (rIndex = 0; rIndex < endCount - 1; ++rIndex) { |
| 229 | const SkOpPtT* oPtT = runs[rIndex]; |
| 230 | for (iIndex = rIndex + 1; iIndex < endCount; ++iIndex) { |
| 231 | const SkOpPtT* iPtT = runs[iIndex]; |
| 232 | double dx = iPtT->fPt.fX - oPtT->fPt.fX; |
| 233 | double dy = iPtT->fPt.fY - oPtT->fPt.fY; |
| 234 | double dist = dx * dx + dy * dy; |
| 235 | distLookup.push_back(rRow + iIndex); |
| 236 | distances.push_back(dist); // oStart distance from iStart |
| 237 | sortedDist.push_back(dIndex++); |
| 238 | } |
| 239 | rRow += endCount; |
| 240 | } |
| 241 | SkASSERT(dIndex == entries); |
| 242 | SkTQSort<int>(sortedDist.begin(), sortedDist.end() - 1, DistanceLessThan(distances.begin())); |
| 243 | int remaining = linkCount; // number of start/end pairs |
| 244 | for (rIndex = 0; rIndex < entries; ++rIndex) { |
| 245 | int pair = sortedDist[rIndex]; |
| 246 | pair = distLookup[pair]; |
| 247 | int row = pair / endCount; |
| 248 | int col = pair - row * endCount; |
| 249 | int ndxOne = row >> 1; |
| 250 | bool endOne = row & 1; |
| 251 | int* linkOne = endOne ? eLink.begin() : sLink.begin(); |
| 252 | if (linkOne[ndxOne] != SK_MaxS32) { |
| 253 | continue; |
| 254 | } |
| 255 | int ndxTwo = col >> 1; |
| 256 | bool endTwo = col & 1; |
| 257 | int* linkTwo = endTwo ? eLink.begin() : sLink.begin(); |
| 258 | if (linkTwo[ndxTwo] != SK_MaxS32) { |
| 259 | continue; |
| 260 | } |
| 261 | SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]); |
| 262 | bool flip = endOne == endTwo; |
| 263 | linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo; |
| 264 | linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne; |
| 265 | if (!--remaining) { |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | SkASSERT(!remaining); |
| 270 | #if DEBUG_ASSEMBLE |
| 271 | for (rIndex = 0; rIndex < linkCount; ++rIndex) { |
| 272 | int s = sLink[rIndex]; |
| 273 | int e = eLink[rIndex]; |
| 274 | SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e', |
| 275 | s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e); |
| 276 | } |
| 277 | #endif |
| 278 | rIndex = 0; |
| 279 | do { |
| 280 | bool forward = true; |
| 281 | bool first = true; |
| 282 | int sIndex = sLink[rIndex]; |
| 283 | SkASSERT(sIndex != SK_MaxS32); |
| 284 | sLink[rIndex] = SK_MaxS32; |
| 285 | int eIndex; |
| 286 | if (sIndex < 0) { |
| 287 | eIndex = sLink[~sIndex]; |
| 288 | sLink[~sIndex] = SK_MaxS32; |
| 289 | } else { |
| 290 | eIndex = eLink[sIndex]; |
| 291 | eLink[sIndex] = SK_MaxS32; |
| 292 | } |
| 293 | SkASSERT(eIndex != SK_MaxS32); |
| 294 | #if DEBUG_ASSEMBLE |
| 295 | SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e', |
| 296 | sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e', |
| 297 | eIndex < 0 ? ~eIndex : eIndex); |
| 298 | #endif |
| 299 | do { |
| 300 | const SkPath& contour = fPartials[rIndex]; |
| 301 | if (forward) { |
| 302 | fPathPtr->addPath(contour, |
| 303 | first ? SkPath::kAppend_AddPathMode : SkPath::kExtend_AddPathMode); |
| 304 | } else { |
| 305 | SkASSERT(!first); |
| 306 | fPathPtr->reverseAddPath(contour); |
| 307 | } |
| 308 | if (first) { |
| 309 | first = false; |
| 310 | } |
| 311 | #if DEBUG_ASSEMBLE |
| 312 | SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex, |
| 313 | eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex, |
| 314 | sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)); |
| 315 | #endif |
| 316 | if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) { |
| 317 | fPathPtr->close(); |
| 318 | break; |
| 319 | } |
| 320 | if (forward) { |
| 321 | eIndex = eLink[rIndex]; |
| 322 | SkASSERT(eIndex != SK_MaxS32); |
| 323 | eLink[rIndex] = SK_MaxS32; |
| 324 | if (eIndex >= 0) { |
| 325 | SkASSERT(sLink[eIndex] == rIndex); |
| 326 | sLink[eIndex] = SK_MaxS32; |
| 327 | } else { |
| 328 | SkASSERT(eLink[~eIndex] == ~rIndex); |
| 329 | eLink[~eIndex] = SK_MaxS32; |
| 330 | } |
| 331 | } else { |
| 332 | eIndex = sLink[rIndex]; |
| 333 | SkASSERT(eIndex != SK_MaxS32); |
| 334 | sLink[rIndex] = SK_MaxS32; |
| 335 | if (eIndex >= 0) { |
| 336 | SkASSERT(eLink[eIndex] == rIndex); |
| 337 | eLink[eIndex] = SK_MaxS32; |
| 338 | } else { |
| 339 | SkASSERT(sLink[~eIndex] == ~rIndex); |
| 340 | sLink[~eIndex] = SK_MaxS32; |
| 341 | } |
| 342 | } |
| 343 | rIndex = eIndex; |
| 344 | if (rIndex < 0) { |
| 345 | forward ^= 1; |
| 346 | rIndex = ~rIndex; |
| 347 | } |
| 348 | } while (true); |
| 349 | for (rIndex = 0; rIndex < linkCount; ++rIndex) { |
| 350 | if (sLink[rIndex] != SK_MaxS32) { |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } while (rIndex < linkCount); |
| 355 | #if DEBUG_ASSEMBLE |
| 356 | for (rIndex = 0; rIndex < linkCount; ++rIndex) { |
| 357 | SkASSERT(sLink[rIndex] == SK_MaxS32); |
| 358 | SkASSERT(eLink[rIndex] == SK_MaxS32); |
| 359 | } |
| 360 | #endif |
| 361 | return; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 362 | } |