blob: 94efcb53fe5531a783dde8f6e261f315cfb647b8 [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.com07393ca2013-04-08 11:47:37 +0000321 const int contourWinding, const int oppContourWinding) const;
caryclark@google.comd892bd82013-06-17 14:10:36 +0000322 void debugShowSort(const char* fun, const SkTArray<SkOpAngle*, true>& angles, int first);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000323#endif
324#if DEBUG_CONCIDENT
325 void debugShowTs() const;
326#endif
327#if DEBUG_SHOW_WINDING
328 int debugShowWindingValues(int slotCount, int ofInterest) const;
329#endif
330
331private:
caryclark@google.comd892bd82013-06-17 14:10:36 +0000332 bool activeAngleOther(int index, int* done, SkTArray<SkOpAngle, true>* angles);
333 bool activeAngleInner(int index, int* done, SkTArray<SkOpAngle, true>* angles);
334 void addAngle(SkTArray<SkOpAngle, true>* angles, int start, int end) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000335 void addCancelOutsides(double tStart, double oStart, SkOpSegment* other, double oEnd);
caryclark@google.comd892bd82013-06-17 14:10:36 +0000336 void addCoinOutsides(const SkTArray<double, true>& outsideTs, SkOpSegment* other, double oEnd);
337 void addTwoAngles(int start, int end, SkTArray<SkOpAngle, true>* angles) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000338 int advanceCoincidentOther(const SkOpSpan* test, double oEndT, int oIndex);
339 int advanceCoincidentThis(const SkOpSpan* oTest, bool opp, int index);
caryclark@google.comd892bd82013-06-17 14:10:36 +0000340 void buildAngles(int index, SkTArray<SkOpAngle, true>* angles, bool includeOpp) const;
341 void buildAnglesInner(int index, SkTArray<SkOpAngle, true>* angles) const;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000342 int bumpCoincidentThis(const SkOpSpan& oTest, bool opp, int index,
caryclark@google.comd892bd82013-06-17 14:10:36 +0000343 SkTArray<double, true>* outsideTs);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000344 int bumpCoincidentOther(const SkOpSpan& test, double oEndT, int& oIndex,
caryclark@google.comd892bd82013-06-17 14:10:36 +0000345 SkTArray<double, true>* oOutsideTs);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000346 bool bumpSpan(SkOpSpan* span, int windDelta, int oppDelta);
347 bool clockwise(int tStart, int tEnd) const;
348 void decrementSpan(SkOpSpan* span);
349 bool equalPoints(int greaterTIndex, int lesserTIndex);
caryclark@google.comd892bd82013-06-17 14:10:36 +0000350 int findStartingEdge(const SkTArray<SkOpAngle*, true>& sorted, int start, int end);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000351 void init(const SkPoint pts[], SkPath::Verb verb, bool operand, bool evenOdd);
352 void matchWindingValue(int tIndex, double t, bool borrowWind);
353 SkOpSpan* markAndChaseDone(int index, int endIndex, int winding);
354 SkOpSpan* markAndChaseDoneBinary(const SkOpAngle* angle, int winding, int oppWinding);
355 SkOpSpan* markAndChaseWinding(const SkOpAngle* angle, const int winding);
356 SkOpSpan* markAndChaseWinding(int index, int endIndex, int winding, int oppWinding);
357 SkOpSpan* markAngle(int maxWinding, int sumWinding, bool activeAngle, const SkOpAngle* angle);
358 void markDoneBinary(int index, int winding, int oppWinding);
359 SkOpSpan* markAndChaseDoneUnary(const SkOpAngle* angle, int winding);
360 void markOneDone(const char* funName, int tIndex, int winding);
361 void markOneDoneBinary(const char* funName, int tIndex);
362 void markOneDoneBinary(const char* funName, int tIndex, int winding, int oppWinding);
363 void markOneDoneUnary(const char* funName, int tIndex);
364 void markUnsortable(int start, int end);
365 bool monotonicInY(int tStart, int tEnd) const;
366 bool multipleSpans(int end) const;
367 SkOpSegment* nextChase(int* index, const int step, int* min, SkOpSpan** last);
368 bool serpentine(int tStart, int tEnd) const;
369 void subDivideBounds(int start, int end, SkPathOpsBounds* bounds) const;
caryclark@google.comd892bd82013-06-17 14:10:36 +0000370 static void TrackOutside(SkTArray<double, true>* outsideTs, double end, double start);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000371 int updateOppWinding(int index, int endIndex) const;
372 int updateOppWinding(const SkOpAngle* angle) const;
373 int updateWinding(int index, int endIndex) const;
374 int updateWinding(const SkOpAngle* angle) const;
375 SkOpSpan* verifyOneWinding(const char* funName, int tIndex);
376 SkOpSpan* verifyOneWindingU(const char* funName, int tIndex);
377 int windValueAt(double t) const;
378 void zeroSpan(SkOpSpan* span);
379
380#if DEBUG_SWAP_TOP
381 bool controlsContainedByEnds(int tStart, int tEnd) const;
382#endif
383#if DEBUG_CONCIDENT
384 void debugAddTPair(double t, const SkOpSegment& other, double otherT) const;
385#endif
386#if DEBUG_MARK_DONE || DEBUG_UNSORTABLE
387 void debugShowNewWinding(const char* fun, const SkOpSpan& span, int winding);
388 void debugShowNewWinding(const char* fun, const SkOpSpan& span, int winding, int oppWinding);
389#endif
390#if DEBUG_WINDING
391 static char as_digit(int value) {
392 return value < 0 ? '?' : value <= 9 ? '0' + value : '+';
393 }
394#endif
395
396 const SkPoint* fPts;
397 SkPathOpsBounds fBounds;
caryclark@google.comd892bd82013-06-17 14:10:36 +0000398 // FIXME: can't convert to SkTArray because it uses insert
caryclark@google.com07393ca2013-04-08 11:47:37 +0000399 SkTDArray<SkOpSpan> fTs; // two or more (always includes t=0 t=1)
400 // OPTIMIZATION: could pack donespans, verb, operand, xor into 1 int-sized value
401 int fDoneSpans; // quick check that segment is finished
402 // OPTIMIZATION: force the following to be byte-sized
403 SkPath::Verb fVerb;
404 bool fOperand;
405 bool fXor; // set if original contour had even-odd fill
406 bool fOppXor; // set if opposite operand had even-odd fill
407#if DEBUG_DUMP
408 int fID;
409#endif
410};
411
412#endif