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(); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame^] | 28 | #ifdef SK_DEBUG |
| 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 | |
| 39 | void addCoincident(int index, SkOpContour* other, int otherIndex, |
| 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 | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame^] | 66 | void addPartialCoincident(int index, SkOpContour* other, int otherIndex, |
| 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 | |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame^] | 75 | int addT(int segIndex, SkOpContour* other, int otherIndex, const SkPoint& pt, double newT, |
| 76 | bool isNear) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 77 | setContainsIntercepts(); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame^] | 78 | return fSegments[segIndex].addT(&other->fSegments[otherIndex], pt, newT, isNear); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | int addSelfT(int segIndex, SkOpContour* other, int otherIndex, const SkPoint& pt, double newT) { |
| 82 | setContainsIntercepts(); |
| 83 | return fSegments[segIndex].addSelfT(&other->fSegments[otherIndex], pt, newT); |
| 84 | } |
| 85 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 86 | const SkPathOpsBounds& bounds() const { |
| 87 | return fBounds; |
| 88 | } |
| 89 | |
| 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 | |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 93 | void checkEnds() { |
| 94 | if (!fContainsCurves) { |
| 95 | return; |
| 96 | } |
| 97 | int segmentCount = fSegments.count(); |
| 98 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 99 | SkOpSegment* segment = &fSegments[sIndex]; |
| 100 | if (segment->verb() == SkPath::kLine_Verb) { |
| 101 | continue; |
| 102 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame^] | 103 | segment->checkEnds(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // if same point has different T values, choose a common T |
| 108 | void checkTiny() { |
| 109 | int segmentCount = fSegments.count(); |
| 110 | if (segmentCount <= 2) { |
| 111 | return; |
| 112 | } |
| 113 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 114 | fSegments[sIndex].checkTiny(); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 118 | void complete() { |
| 119 | setBounds(); |
| 120 | fContainsIntercepts = false; |
| 121 | } |
| 122 | |
| 123 | bool containsCubics() const { |
| 124 | return fContainsCubics; |
| 125 | } |
| 126 | |
| 127 | bool crosses(const SkOpContour* crosser) const { |
| 128 | for (int index = 0; index < fCrosses.count(); ++index) { |
| 129 | if (fCrosses[index] == crosser) { |
| 130 | return true; |
| 131 | } |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | bool done() const { |
| 137 | return fDone; |
| 138 | } |
| 139 | |
| 140 | const SkPoint& end() const { |
| 141 | const SkOpSegment& segment = fSegments.back(); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 142 | return segment.pts()[SkPathOpsVerbToPoints(segment.verb())]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 143 | } |
| 144 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 145 | void fixOtherTIndex() { |
| 146 | int segmentCount = fSegments.count(); |
| 147 | for (int sIndex = 0; sIndex < segmentCount; ++sIndex) { |
| 148 | fSegments[sIndex].fixOtherTIndex(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | SkOpSegment* nonVerticalSegment(int* start, int* end); |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 153 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 154 | bool operand() const { |
| 155 | return fOperand; |
| 156 | } |
| 157 | |
| 158 | void reset() { |
| 159 | fSegments.reset(); |
| 160 | fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax); |
| 161 | fContainsCurves = fContainsCubics = fContainsIntercepts = fDone = false; |
| 162 | } |
| 163 | |
| 164 | SkTArray<SkOpSegment>& segments() { |
| 165 | return fSegments; |
| 166 | } |
| 167 | |
| 168 | void setContainsIntercepts() { |
| 169 | fContainsIntercepts = true; |
| 170 | } |
| 171 | |
| 172 | void setOperand(bool isOp) { |
| 173 | fOperand = isOp; |
| 174 | } |
| 175 | |
| 176 | void setOppXor(bool isOppXor) { |
| 177 | fOppXor = isOppXor; |
| 178 | int segmentCount = fSegments.count(); |
| 179 | for (int test = 0; test < segmentCount; ++test) { |
| 180 | fSegments[test].setOppXor(isOppXor); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void setXor(bool isXor) { |
| 185 | fXor = isXor; |
| 186 | } |
| 187 | |
| 188 | void sortSegments(); |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 189 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 190 | const SkPoint& start() const { |
| 191 | return fSegments.front().pts()[0]; |
| 192 | } |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 193 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 194 | void toPath(SkPathWriter* path) const; |
skia.committer@gmail.com | 3284017 | 2013-04-09 07:01:27 +0000 | [diff] [blame] | 195 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 196 | void toPartialBackward(SkPathWriter* path) const { |
| 197 | int segmentCount = fSegments.count(); |
| 198 | for (int test = segmentCount - 1; test >= 0; --test) { |
| 199 | fSegments[test].addCurveTo(1, 0, path, true); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void toPartialForward(SkPathWriter* path) const { |
| 204 | int segmentCount = fSegments.count(); |
| 205 | for (int test = 0; test < segmentCount; ++test) { |
| 206 | fSegments[test].addCurveTo(0, 1, path, true); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void topSortableSegment(const SkPoint& topLeft, SkPoint* bestXY, SkOpSegment** topStart); |
| 211 | SkOpSegment* undoneSegment(int* start, int* end); |
| 212 | |
| 213 | int updateSegment(int index, const SkPoint* pts) { |
| 214 | SkOpSegment& segment = fSegments[index]; |
| 215 | segment.updatePts(pts); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 216 | return SkPathOpsVerbToPoints(segment.verb()) + 1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | #if DEBUG_TEST |
| 220 | SkTArray<SkOpSegment>& debugSegments() { |
| 221 | return fSegments; |
| 222 | } |
| 223 | #endif |
| 224 | |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 225 | #if DEBUG_ACTIVE_SPANS || DEBUG_ACTIVE_SPANS_FIRST_ONLY |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 226 | void debugShowActiveSpans() { |
| 227 | for (int index = 0; index < fSegments.count(); ++index) { |
| 228 | fSegments[index].debugShowActiveSpans(); |
| 229 | } |
| 230 | } |
| 231 | #endif |
| 232 | |
| 233 | #if DEBUG_SHOW_WINDING |
| 234 | int debugShowWindingValues(int totalSegments, int ofInterest); |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 235 | static void debugShowWindingValues(const SkTArray<SkOpContour*, true>& contourList); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 236 | #endif |
| 237 | |
| 238 | private: |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame^] | 239 | void calcCommonCoincidentWinding(const SkCoincidence& coincidence); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 240 | void setBounds(); |
| 241 | |
| 242 | SkTArray<SkOpSegment> fSegments; |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 243 | SkTArray<SkOpSegment*, true> fSortedSegments; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 244 | int fFirstSorted; |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 245 | SkTArray<SkCoincidence, true> fCoincidences; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame^] | 246 | SkTArray<SkCoincidence, true> fPartialCoincidences; |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 247 | SkTArray<const SkOpContour*, true> fCrosses; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 248 | SkPathOpsBounds fBounds; |
| 249 | bool fContainsIntercepts; // FIXME: is this used by anybody? |
| 250 | bool fContainsCubics; |
| 251 | bool fContainsCurves; |
| 252 | bool fDone; |
| 253 | bool fOperand; // true for the second argument to a binary operator |
| 254 | bool fXor; |
| 255 | bool fOppXor; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame^] | 256 | #ifdef SK_DEBUG |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 257 | int fID; |
| 258 | #endif |
| 259 | }; |
| 260 | |
| 261 | #endif |