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