caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
| 7 | #ifndef SkOpContour_DEFINED |
| 8 | #define SkOpContour_DEFINED |
| 9 | |
| 10 | #include "SkOpSegment.h" |
| 11 | #include "SkTArray.h" |
| 12 | |
| 13 | class SkIntersections; |
| 14 | class SkOpContour; |
| 15 | class SkPathWriter; |
| 16 | |
| 17 | struct SkCoincidence { |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 18 | SkOpContour* fOther; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 19 | int fSegments[2]; |
| 20 | double fTs[2][2]; |
| 21 | SkPoint fPts[2]; |
| 22 | }; |
| 23 | |
| 24 | class SkOpContour { |
| 25 | public: |
| 26 | SkOpContour() { |
| 27 | reset(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 28 | #if defined(SK_DEBUG) || !FORCE_RELEASE |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 29 | fID = ++SkPathOpsDebug::gContourID; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 30 | #endif |
| 31 | } |
| 32 | |
| 33 | bool operator<(const SkOpContour& rh) const { |
| 34 | return fBounds.fTop == rh.fBounds.fTop |
| 35 | ? fBounds.fLeft < rh.fBounds.fLeft |
| 36 | : fBounds.fTop < rh.fBounds.fTop; |
| 37 | } |
| 38 | |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 39 | bool addCoincident(int index, SkOpContour* other, int otherIndex, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 40 | const SkIntersections& ts, bool swap); |
| 41 | void addCoincidentPoints(); |
| 42 | |
| 43 | void addCross(const SkOpContour* crosser) { |
| 44 | #ifdef DEBUG_CROSS |
| 45 | for (int index = 0; index < fCrosses.count(); ++index) { |
| 46 | SkASSERT(fCrosses[index] != crosser); |
| 47 | } |
| 48 | #endif |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 49 | fCrosses.push_back(crosser); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void addCubic(const SkPoint pts[4]) { |
| 53 | fSegments.push_back().addCubic(pts, fOperand, fXor); |
| 54 | fContainsCurves = fContainsCubics = true; |
| 55 | } |
| 56 | |
| 57 | int addLine(const SkPoint pts[2]) { |
| 58 | fSegments.push_back().addLine(pts, fOperand, fXor); |
| 59 | return fSegments.count(); |
| 60 | } |
| 61 | |
| 62 | void addOtherT(int segIndex, int tIndex, double otherT, int otherIndex) { |
| 63 | fSegments[segIndex].addOtherT(tIndex, otherT, otherIndex); |
| 64 | } |
| 65 | |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 66 | bool addPartialCoincident(int index, SkOpContour* other, int otherIndex, |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 67 | const SkIntersections& ts, int ptIndex, bool swap); |
| 68 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 69 | int addQuad(const SkPoint pts[3]) { |
| 70 | fSegments.push_back().addQuad(pts, fOperand, fXor); |
| 71 | fContainsCurves = true; |
| 72 | return fSegments.count(); |
| 73 | } |
| 74 | |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 75 | int addT(int segIndex, SkOpContour* other, int otherIndex, const SkPoint& pt, double newT) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 76 | setContainsIntercepts(); |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 77 | return fSegments[segIndex].addT(&other->fSegments[otherIndex], pt, newT); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 78 | } |
| 79 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 80 | int addSelfT(int segIndex, const SkPoint& pt, double newT) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 81 | setContainsIntercepts(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 82 | return fSegments[segIndex].addSelfT(pt, newT); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 83 | } |
| 84 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 85 | const SkPathOpsBounds& bounds() const { |
| 86 | return fBounds; |
| 87 | } |
| 88 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 89 | bool calcAngles(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 90 | void calcCoincidentWinding(); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 91 | void calcPartialCoincidentWinding(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 92 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 93 | void checkDuplicates() { |
| 94 | int segmentCount = fSegments.count(); |
| 95 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 96 | SkOpSegment& segment = fSegments[sIndex]; |
| 97 | if (segment.count() > 2) { |
| 98 | segment.checkDuplicates(); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 103 | void checkEnds() { |
| 104 | if (!fContainsCurves) { |
| 105 | return; |
| 106 | } |
| 107 | int segmentCount = fSegments.count(); |
| 108 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 109 | SkOpSegment* segment = &fSegments[sIndex]; |
| 110 | if (segment->verb() == SkPath::kLine_Verb) { |
| 111 | continue; |
| 112 | } |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 113 | if (segment->done()) { |
| 114 | continue; // likely coincident, nothing to do |
| 115 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 116 | segment->checkEnds(); |
| 117 | } |
| 118 | } |
| 119 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 120 | void checkMultiples() { |
| 121 | int segmentCount = fSegments.count(); |
| 122 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 123 | SkOpSegment& segment = fSegments[sIndex]; |
| 124 | if (segment.count() > 2) { |
| 125 | segment.checkMultiples(); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void checkSmall() { |
| 131 | int segmentCount = fSegments.count(); |
| 132 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 133 | SkOpSegment& segment = fSegments[sIndex]; |
| 134 | if (segment.hasSmall()) { |
| 135 | segment.checkSmall(); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 140 | // if same point has different T values, choose a common T |
| 141 | void checkTiny() { |
| 142 | int segmentCount = fSegments.count(); |
| 143 | if (segmentCount <= 2) { |
| 144 | return; |
| 145 | } |
| 146 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 147 | SkOpSegment& segment = fSegments[sIndex]; |
| 148 | if (segment.hasTiny()) { |
| 149 | segment.checkTiny(); |
| 150 | } |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 154 | void complete() { |
| 155 | setBounds(); |
| 156 | fContainsIntercepts = false; |
| 157 | } |
| 158 | |
| 159 | bool containsCubics() const { |
| 160 | return fContainsCubics; |
| 161 | } |
| 162 | |
| 163 | bool crosses(const SkOpContour* crosser) const { |
| 164 | for (int index = 0; index < fCrosses.count(); ++index) { |
| 165 | if (fCrosses[index] == crosser) { |
| 166 | return true; |
| 167 | } |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | bool done() const { |
| 173 | return fDone; |
| 174 | } |
| 175 | |
| 176 | const SkPoint& end() const { |
| 177 | const SkOpSegment& segment = fSegments.back(); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 178 | return segment.pts()[SkPathOpsVerbToPoints(segment.verb())]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 179 | } |
| 180 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 181 | void fixOtherTIndex() { |
| 182 | int segmentCount = fSegments.count(); |
| 183 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 184 | fSegments[sIndex].fixOtherTIndex(); |
| 185 | } |
| 186 | } |
| 187 | |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 188 | void joinCoincidence() { |
| 189 | joinCoincidence(fCoincidences, false); |
| 190 | joinCoincidence(fPartialCoincidences, true); |
| 191 | } |
| 192 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 193 | SkOpSegment* nonVerticalSegment(int* start, int* end); |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 194 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 195 | bool operand() const { |
| 196 | return fOperand; |
| 197 | } |
| 198 | |
| 199 | void reset() { |
| 200 | fSegments.reset(); |
| 201 | fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax); |
| 202 | fContainsCurves = fContainsCubics = fContainsIntercepts = fDone = false; |
| 203 | } |
| 204 | |
| 205 | SkTArray<SkOpSegment>& segments() { |
| 206 | return fSegments; |
| 207 | } |
| 208 | |
| 209 | void setContainsIntercepts() { |
| 210 | fContainsIntercepts = true; |
| 211 | } |
| 212 | |
| 213 | void setOperand(bool isOp) { |
| 214 | fOperand = isOp; |
| 215 | } |
| 216 | |
| 217 | void setOppXor(bool isOppXor) { |
| 218 | fOppXor = isOppXor; |
| 219 | int segmentCount = fSegments.count(); |
| 220 | for (int test = 0; test < segmentCount; ++test) { |
| 221 | fSegments[test].setOppXor(isOppXor); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void setXor(bool isXor) { |
| 226 | fXor = isXor; |
| 227 | } |
| 228 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 229 | void sortAngles(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 230 | void sortSegments(); |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 231 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 232 | const SkPoint& start() const { |
| 233 | return fSegments.front().pts()[0]; |
| 234 | } |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 235 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 236 | void toPath(SkPathWriter* path) const; |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 237 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 238 | void toPartialBackward(SkPathWriter* path) const { |
| 239 | int segmentCount = fSegments.count(); |
| 240 | for (int test = segmentCount - 1; test >= 0; --test) { |
| 241 | fSegments[test].addCurveTo(1, 0, path, true); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void toPartialForward(SkPathWriter* path) const { |
| 246 | int segmentCount = fSegments.count(); |
| 247 | for (int test = 0; test < segmentCount; ++test) { |
| 248 | fSegments[test].addCurveTo(0, 1, path, true); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void topSortableSegment(const SkPoint& topLeft, SkPoint* bestXY, SkOpSegment** topStart); |
| 253 | SkOpSegment* undoneSegment(int* start, int* end); |
| 254 | |
| 255 | int updateSegment(int index, const SkPoint* pts) { |
| 256 | SkOpSegment& segment = fSegments[index]; |
| 257 | segment.updatePts(pts); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 258 | return SkPathOpsVerbToPoints(segment.verb()) + 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | #if DEBUG_TEST |
| 262 | SkTArray<SkOpSegment>& debugSegments() { |
| 263 | return fSegments; |
| 264 | } |
| 265 | #endif |
| 266 | |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 267 | #if DEBUG_ACTIVE_SPANS || DEBUG_ACTIVE_SPANS_FIRST_ONLY |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 268 | void debugShowActiveSpans() { |
| 269 | for (int index = 0; index < fSegments.count(); ++index) { |
| 270 | fSegments[index].debugShowActiveSpans(); |
| 271 | } |
| 272 | } |
| 273 | #endif |
| 274 | |
| 275 | #if DEBUG_SHOW_WINDING |
| 276 | int debugShowWindingValues(int totalSegments, int ofInterest); |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 277 | static void debugShowWindingValues(const SkTArray<SkOpContour*, true>& contourList); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 278 | #endif |
| 279 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 280 | // available to test routines only |
| 281 | void dump() const; |
| 282 | void dumpAngles() const; |
| 283 | void dumpPts() const; |
| 284 | void dumpSpans() const; |
| 285 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 286 | private: |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 287 | void calcCommonCoincidentWinding(const SkCoincidence& ); |
| 288 | void joinCoincidence(const SkTArray<SkCoincidence, true>& , bool partial); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 289 | void setBounds(); |
| 290 | |
| 291 | SkTArray<SkOpSegment> fSegments; |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 292 | SkTArray<SkOpSegment*, true> fSortedSegments; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 293 | int fFirstSorted; |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 294 | SkTArray<SkCoincidence, true> fCoincidences; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 295 | SkTArray<SkCoincidence, true> fPartialCoincidences; |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 296 | SkTArray<const SkOpContour*, true> fCrosses; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 297 | SkPathOpsBounds fBounds; |
| 298 | bool fContainsIntercepts; // FIXME: is this used by anybody? |
| 299 | bool fContainsCubics; |
| 300 | bool fContainsCurves; |
| 301 | bool fDone; |
| 302 | bool fOperand; // true for the second argument to a binary operator |
| 303 | bool fXor; |
| 304 | bool fOppXor; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 305 | #if defined(SK_DEBUG) || !FORCE_RELEASE |
| 306 | int debugID() const { return fID; } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 307 | int fID; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame^] | 308 | #else |
| 309 | int debugID() const { return -1; } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 310 | #endif |
| 311 | }; |
| 312 | |
| 313 | #endif |