blob: 3cbd29e77edc69260ed0e8e9dd309c491fb2f073 [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 */
7#ifndef SkOpSegment_DEFINE
8#define SkOpSegment_DEFINE
9
10#include "SkOpAngle.h"
caryclark@google.comcffbcc32013-06-04 17:59:42 +000011#include "SkOpSpan.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000012#include "SkPathOpsBounds.h"
13#include "SkPathOpsCurve.h"
caryclark@google.comd892bd82013-06-17 14:10:36 +000014#include "SkTArray.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000015#include "SkTDArray.h"
16
17class SkPathWriter;
18
19class SkOpSegment {
20public:
21 SkOpSegment() {
22#if DEBUG_DUMP
23 fID = ++gSegmentID;
24#endif
25 }
26
27 bool operator<(const SkOpSegment& rh) const {
28 return fBounds.fTop < rh.fBounds.fTop;
29 }
30
31 const SkPathOpsBounds& bounds() const {
32 return fBounds;
33 }
34
35 // OPTIMIZE
36 // when the edges are initially walked, they don't automatically get the prior and next
37 // edges assigned to positions t=0 and t=1. Doing that would remove the need for this check,
38 // and would additionally remove the need for similar checks in condition edges. It would
39 // also allow intersection code to assume end of segment intersections (maybe?)
40 bool complete() const {
41 int count = fTs.count();
42 return count > 1 && fTs[0].fT == 0 && fTs[--count].fT == 1;
43 }
44
caryclark@google.comcffbcc32013-06-04 17:59:42 +000045 int count() const {
46 return fTs.count();
47 }
48
caryclark@google.com07393ca2013-04-08 11:47:37 +000049 bool done() const {
50 SkASSERT(fDoneSpans <= fTs.count());
51 return fDoneSpans == fTs.count();
52 }
53
54 bool done(int min) const {
55 return fTs[min].fDone;
56 }
57
58 bool done(const SkOpAngle* angle) const {
59 return done(SkMin32(angle->start(), angle->end()));
60 }
61
62 SkVector dxdy(int index) const {
reed@google.com277c3f82013-05-31 15:17:50 +000063 return (*CurveSlopeAtT[SkPathOpsVerbToPoints(fVerb)])(fPts, fTs[index].fT);
caryclark@google.com07393ca2013-04-08 11:47:37 +000064 }
65
66 SkScalar dy(int index) const {
67 return dxdy(index).fY;
68 }
69
70 bool intersected() const {
71 return fTs.count() > 0;
72 }
73
74 bool isCanceled(int tIndex) const {
75 return fTs[tIndex].fWindValue == 0 && fTs[tIndex].fOppValue == 0;
76 }
77
78 bool isConnected(int startIndex, int endIndex) const {
79 return fTs[startIndex].fWindSum != SK_MinS32 || fTs[endIndex].fWindSum != SK_MinS32;
80 }
81
82 bool isHorizontal() const {
83 return fBounds.fTop == fBounds.fBottom;
84 }
85
86 bool isVertical() const {
87 return fBounds.fLeft == fBounds.fRight;
88 }
89
90 bool isVertical(int start, int end) const {
reed@google.com277c3f82013-05-31 15:17:50 +000091 return (*CurveIsVertical[SkPathOpsVerbToPoints(fVerb)])(fPts, start, end);
caryclark@google.com07393ca2013-04-08 11:47:37 +000092 }
93
94 bool operand() const {
95 return fOperand;
96 }
97
98 int oppSign(const SkOpAngle* angle) const {
99 SkASSERT(angle->segment() == this);
100 return oppSign(angle->start(), angle->end());
101 }
102
103 int oppSign(int startIndex, int endIndex) const {
104 int result = startIndex < endIndex ? -fTs[startIndex].fOppValue : fTs[endIndex].fOppValue;
105#if DEBUG_WIND_BUMP
106 SkDebugf("%s oppSign=%d\n", __FUNCTION__, result);
107#endif
108 return result;
109 }
110
111 int oppSum(int tIndex) const {
112 return fTs[tIndex].fOppSum;
113 }
114
115 int oppSum(const SkOpAngle* angle) const {
116 int lesser = SkMin32(angle->start(), angle->end());
117 return fTs[lesser].fOppSum;
118 }
119
120 int oppValue(int tIndex) const {
121 return fTs[tIndex].fOppValue;
122 }
123
124 int oppValue(const SkOpAngle* angle) const {
125 int lesser = SkMin32(angle->start(), angle->end());
126 return fTs[lesser].fOppValue;
127 }
128
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000129 const SkOpSegment* other(int index) const {
130 return fTs[index].fOther;
131 }
132
caryclark@google.com07393ca2013-04-08 11:47:37 +0000133 const SkPoint* pts() const {
134 return fPts;
135 }
136
137 void reset() {
138 init(NULL, (SkPath::Verb) -1, false, false);
139 fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax);
140 fTs.reset();
141 }
142
143 void setOppXor(bool isOppXor) {
144 fOppXor = isOppXor;
145 }
146
caryclark@google.com07393ca2013-04-08 11:47:37 +0000147 void setUpWinding(int index, int endIndex, int* maxWinding, int* sumWinding) {
148 int deltaSum = spanSign(index, endIndex);
149 *maxWinding = *sumWinding;
150 *sumWinding -= deltaSum;
151 }
152
153 // OPTIMIZATION: mark as debugging only if used solely by tests
154 const SkOpSpan& span(int tIndex) const {
155 return fTs[tIndex];
156 }
157
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000158 // OPTIMIZATION: mark as debugging only if used solely by tests
159 const SkTDArray<SkOpSpan>& spans() const {
160 return fTs;
161 }
162
caryclark@google.com07393ca2013-04-08 11:47:37 +0000163 int spanSign(const SkOpAngle* angle) const {
164 SkASSERT(angle->segment() == this);
165 return spanSign(angle->start(), angle->end());
166 }
167
168 int spanSign(int startIndex, int endIndex) const {
169 int result = startIndex < endIndex ? -fTs[startIndex].fWindValue : fTs[endIndex].fWindValue;
170#if DEBUG_WIND_BUMP
171 SkDebugf("%s spanSign=%d\n", __FUNCTION__, result);
172#endif
173 return result;
174 }
175
176 // OPTIMIZATION: mark as debugging only if used solely by tests
177 double t(int tIndex) const {
178 return fTs[tIndex].fT;
179 }
180
181 double tAtMid(int start, int end, double mid) const {
182 return fTs[start].fT * (1 - mid) + fTs[end].fT * mid;
183 }
184
185 bool unsortable(int index) const {
186 return fTs[index].fUnsortableStart || fTs[index].fUnsortableEnd;
187 }
188
189 void updatePts(const SkPoint pts[]) {
190 fPts = pts;
191 }
skia.committer@gmail.com32840172013-04-09 07:01:27 +0000192
caryclark@google.com07393ca2013-04-08 11:47:37 +0000193 SkPath::Verb verb() const {
194 return fVerb;
195 }
196
197 int windSum(int tIndex) const {
198 return fTs[tIndex].fWindSum;
199 }
200
201 int windValue(int tIndex) const {
202 return fTs[tIndex].fWindValue;
203 }
204
205 SkScalar xAtT(int index) const {
206 return xAtT(&fTs[index]);
207 }
208
209 SkScalar xAtT(const SkOpSpan* span) const {
210 return xyAtT(span).fX;
211 }
212
213 const SkPoint& xyAtT(const SkOpSpan* span) const {
214 return span->fPt;
215 }
216
217 // used only by right angle winding finding
218 SkPoint xyAtT(double mid) const {
reed@google.com277c3f82013-05-31 15:17:50 +0000219 return (*CurvePointAtT[SkPathOpsVerbToPoints(fVerb)])(fPts, mid);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000220 }
221
222 const SkPoint& xyAtT(int index) const {
223 return xyAtT(&fTs[index]);
224 }
skia.committer@gmail.com32840172013-04-09 07:01:27 +0000225
caryclark@google.com07393ca2013-04-08 11:47:37 +0000226 SkScalar yAtT(int index) const {
227 return yAtT(&fTs[index]);
228 }
229
230 SkScalar yAtT(const SkOpSpan* span) const {
231 return xyAtT(span).fY;
232 }
233
caryclark@google.comd892bd82013-06-17 14:10:36 +0000234 bool activeAngle(int index, int* done, SkTArray<SkOpAngle, true>* angles);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000235 SkPoint activeLeftTop(bool onlySortable, int* firstT) const;
236 bool activeOp(int index, int endIndex, int xorMiMask, int xorSuMask, SkPathOp op);
237 bool activeOp(int xorMiMask, int xorSuMask, int index, int endIndex, SkPathOp op,
238 int* sumMiWinding, int* sumSuWinding, int* maxWinding, int* sumWinding,
239 int* oppMaxWinding, int* oppSumWinding);
240 bool activeWinding(int index, int endIndex);
241 bool activeWinding(int index, int endIndex, int* maxWinding, int* sumWinding);
242 void addCubic(const SkPoint pts[4], bool operand, bool evenOdd);
243 void addCurveTo(int start, int end, SkPathWriter* path, bool active) const;
244 void addLine(const SkPoint pts[2], bool operand, bool evenOdd);
245 void addOtherT(int index, double otherT, int otherIndex);
246 void addQuad(const SkPoint pts[3], bool operand, bool evenOdd);
247 int addSelfT(SkOpSegment* other, const SkPoint& pt, double newT);
248 int addT(SkOpSegment* other, const SkPoint& pt, double newT);
249 void addTCancel(double startT, double endT, SkOpSegment* other, double oStartT, double oEndT);
250 void addTCoincident(double startT, double endT, SkOpSegment* other, double oStartT,
251 double oEndT);
252 void addTPair(double t, SkOpSegment* other, double otherT, bool borrowWind, const SkPoint& pt);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000253 void addTPair(double t, SkOpSegment* other, double otherT, bool borrowWind, const SkPoint& pt,
254 const SkPoint& oPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000255 int addUnsortableT(SkOpSegment* other, bool start, const SkPoint& pt, double newT);
256 bool betweenTs(int lesser, double testT, int greater) const;
257 int computeSum(int startIndex, int endIndex, bool binary);
258 int crossedSpanY(const SkPoint& basePt, SkScalar* bestY, double* hitT, bool* hitSomething,
259 double mid, bool opp, bool current) const;
260 SkOpSegment* findNextOp(SkTDArray<SkOpSpan*>* chase, int* nextStart, int* nextEnd,
261 bool* unsortable, SkPathOp op, const int xorMiMask,
262 const int xorSuMask);
263 SkOpSegment* findNextWinding(SkTDArray<SkOpSpan*>* chase, int* nextStart, int* nextEnd,
264 bool* unsortable);
265 SkOpSegment* findNextXor(int* nextStart, int* nextEnd, bool* unsortable);
266 void findTooCloseToCall();
267 SkOpSegment* findTop(int* tIndex, int* endIndex, bool* unsortable, bool onlySortable);
268 void fixOtherTIndex();
269 void initWinding(int start, int end);
270 void initWinding(int start, int end, double tHit, int winding, SkScalar hitDx, int oppWind,
271 SkScalar hitOppDx);
272 bool isLinear(int start, int end) const;
273 bool isMissing(double startT) const;
274 bool isSimple(int end) const;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000275 bool isTiny(const SkOpAngle* angle) const;
276 bool isTiny(int index) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000277 SkOpSpan* markAndChaseDoneBinary(int index, int endIndex);
278 SkOpSpan* markAndChaseDoneUnary(int index, int endIndex);
279 SkOpSpan* markAndChaseWinding(const SkOpAngle* angle, int winding, int oppWinding);
280 SkOpSpan* markAngle(int maxWinding, int sumWinding, int oppMaxWinding, int oppSumWinding,
281 bool activeAngle, const SkOpAngle* angle);
282 void markDone(int index, int winding);
283 void markDoneBinary(int index);
284 void markDoneUnary(int index);
285 SkOpSpan* markOneWinding(const char* funName, int tIndex, int winding);
286 SkOpSpan* markOneWinding(const char* funName, int tIndex, int winding, int oppWinding);
287 void markWinding(int index, int winding);
288 void markWinding(int index, int winding, int oppWinding);
289 bool nextCandidate(int* start, int* end) const;
290 int nextExactSpan(int from, int step) const;
291 int nextSpan(int from, int step) const;
292 void setUpWindings(int index, int endIndex, int* sumMiWinding, int* sumSuWinding,
293 int* maxWinding, int* sumWinding, int* oppMaxWinding, int* oppSumWinding);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000294 enum SortAngleKind {
295 kMustBeOrdered_SortAngleKind, // required for winding calc
296 kMayBeUnordered_SortAngleKind // ok for find top
297 };
caryclark@google.comd892bd82013-06-17 14:10:36 +0000298 static bool SortAngles(const SkTArray<SkOpAngle, true>& angles,
299 SkTArray<SkOpAngle*, true>* angleList,
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000300 SortAngleKind );
301 bool subDivide(int start, int end, SkPoint edge[4]) const;
302 bool subDivide(int start, int end, SkDCubic* result) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000303 void undoneSpan(int* start, int* end);
304 int updateOppWindingReverse(const SkOpAngle* angle) const;
305 int updateWindingReverse(const SkOpAngle* angle) const;
306 static bool UseInnerWinding(int outerWinding, int innerWinding);
307 int windingAtT(double tHit, int tIndex, bool crossOpp, SkScalar* dx) const;
308 int windSum(const SkOpAngle* angle) const;
309 int windValue(const SkOpAngle* angle) const;
310
311#if DEBUG_DUMP
312 int debugID() const {
313 return fID;
314 }
315#endif
caryclark@google.coma5e55922013-05-07 18:51:31 +0000316#if DEBUG_ACTIVE_SPANS || DEBUG_ACTIVE_SPANS_FIRST_ONLY
caryclark@google.com07393ca2013-04-08 11:47:37 +0000317 void debugShowActiveSpans() const;
318#endif
319#if DEBUG_SORT || DEBUG_SWAP_TOP
caryclark@google.comd892bd82013-06-17 14:10:36 +0000320 void debugShowSort(const char* fun, const SkTArray<SkOpAngle*, true>& angles, int first,
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000321 const int contourWinding, const int oppContourWinding, bool sortable) const;
322 void debugShowSort(const char* fun, const SkTArray<SkOpAngle*, true>& angles, int first,
323 bool sortable);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000324#endif
325#if DEBUG_CONCIDENT
326 void debugShowTs() const;
327#endif
328#if DEBUG_SHOW_WINDING
329 int debugShowWindingValues(int slotCount, int ofInterest) const;
330#endif
331
332private:
caryclark@google.comd892bd82013-06-17 14:10:36 +0000333 bool activeAngleOther(int index, int* done, SkTArray<SkOpAngle, true>* angles);
334 bool activeAngleInner(int index, int* done, SkTArray<SkOpAngle, true>* angles);
335 void addAngle(SkTArray<SkOpAngle, true>* angles, int start, int end) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000336 void addCancelOutsides(double tStart, double oStart, SkOpSegment* other, double oEnd);
caryclark@google.comd892bd82013-06-17 14:10:36 +0000337 void addCoinOutsides(const SkTArray<double, true>& outsideTs, SkOpSegment* other, double oEnd);
338 void addTwoAngles(int start, int end, SkTArray<SkOpAngle, true>* angles) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000339 int advanceCoincidentOther(const SkOpSpan* test, double oEndT, int oIndex);
340 int advanceCoincidentThis(const SkOpSpan* oTest, bool opp, int index);
caryclark@google.comd892bd82013-06-17 14:10:36 +0000341 void buildAngles(int index, SkTArray<SkOpAngle, true>* angles, bool includeOpp) const;
342 void buildAnglesInner(int index, SkTArray<SkOpAngle, true>* angles) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000343 int bumpCoincidentThis(const SkOpSpan& oTest, bool opp, int index,
caryclark@google.comd892bd82013-06-17 14:10:36 +0000344 SkTArray<double, true>* outsideTs);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000345 int bumpCoincidentOther(const SkOpSpan& test, double oEndT, int& oIndex,
caryclark@google.comd892bd82013-06-17 14:10:36 +0000346 SkTArray<double, true>* oOutsideTs);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000347 bool bumpSpan(SkOpSpan* span, int windDelta, int oppDelta);
348 bool clockwise(int tStart, int tEnd) const;
349 void decrementSpan(SkOpSpan* span);
350 bool equalPoints(int greaterTIndex, int lesserTIndex);
caryclark@google.comd892bd82013-06-17 14:10:36 +0000351 int findStartingEdge(const SkTArray<SkOpAngle*, true>& sorted, int start, int end);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000352 void init(const SkPoint pts[], SkPath::Verb verb, bool operand, bool evenOdd);
353 void matchWindingValue(int tIndex, double t, bool borrowWind);
354 SkOpSpan* markAndChaseDone(int index, int endIndex, int winding);
355 SkOpSpan* markAndChaseDoneBinary(const SkOpAngle* angle, int winding, int oppWinding);
356 SkOpSpan* markAndChaseWinding(const SkOpAngle* angle, const int winding);
357 SkOpSpan* markAndChaseWinding(int index, int endIndex, int winding, int oppWinding);
358 SkOpSpan* markAngle(int maxWinding, int sumWinding, bool activeAngle, const SkOpAngle* angle);
359 void markDoneBinary(int index, int winding, int oppWinding);
360 SkOpSpan* markAndChaseDoneUnary(const SkOpAngle* angle, int winding);
361 void markOneDone(const char* funName, int tIndex, int winding);
362 void markOneDoneBinary(const char* funName, int tIndex);
363 void markOneDoneBinary(const char* funName, int tIndex, int winding, int oppWinding);
364 void markOneDoneUnary(const char* funName, int tIndex);
365 void markUnsortable(int start, int end);
366 bool monotonicInY(int tStart, int tEnd) const;
367 bool multipleSpans(int end) const;
368 SkOpSegment* nextChase(int* index, const int step, int* min, SkOpSpan** last);
369 bool serpentine(int tStart, int tEnd) const;
370 void subDivideBounds(int start, int end, SkPathOpsBounds* bounds) const;
caryclark@google.comd892bd82013-06-17 14:10:36 +0000371 static void TrackOutside(SkTArray<double, true>* outsideTs, double end, double start);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000372 int updateOppWinding(int index, int endIndex) const;
373 int updateOppWinding(const SkOpAngle* angle) const;
374 int updateWinding(int index, int endIndex) const;
375 int updateWinding(const SkOpAngle* angle) const;
376 SkOpSpan* verifyOneWinding(const char* funName, int tIndex);
377 SkOpSpan* verifyOneWindingU(const char* funName, int tIndex);
378 int windValueAt(double t) const;
379 void zeroSpan(SkOpSpan* span);
380
381#if DEBUG_SWAP_TOP
382 bool controlsContainedByEnds(int tStart, int tEnd) const;
383#endif
384#if DEBUG_CONCIDENT
385 void debugAddTPair(double t, const SkOpSegment& other, double otherT) const;
386#endif
387#if DEBUG_MARK_DONE || DEBUG_UNSORTABLE
388 void debugShowNewWinding(const char* fun, const SkOpSpan& span, int winding);
389 void debugShowNewWinding(const char* fun, const SkOpSpan& span, int winding, int oppWinding);
390#endif
391#if DEBUG_WINDING
392 static char as_digit(int value) {
393 return value < 0 ? '?' : value <= 9 ? '0' + value : '+';
394 }
395#endif
396
397 const SkPoint* fPts;
398 SkPathOpsBounds fBounds;
caryclark@google.comd892bd82013-06-17 14:10:36 +0000399 // FIXME: can't convert to SkTArray because it uses insert
caryclark@google.com07393ca2013-04-08 11:47:37 +0000400 SkTDArray<SkOpSpan> fTs; // two or more (always includes t=0 t=1)
401 // OPTIMIZATION: could pack donespans, verb, operand, xor into 1 int-sized value
402 int fDoneSpans; // quick check that segment is finished
403 // OPTIMIZATION: force the following to be byte-sized
404 SkPath::Verb fVerb;
405 bool fOperand;
406 bool fXor; // set if original contour had even-odd fill
407 bool fOppXor; // set if opposite operand had even-odd fill
408#if DEBUG_DUMP
409 int fID;
410#endif
411};
412
413#endif