blob: f4f2aa56f2e9d608d628ef3d14a0a656c79cdeca [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
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000154 // OPTIMIZATION: mark as debugging only if used solely by tests
155 const SkTDArray<SkOpSpan>& spans() const {
156 return fTs;
157 }
158
caryclark@google.com07393ca2013-04-08 11:47:37 +0000159 int spanSign(const SkOpAngle* angle) const {
160 SkASSERT(angle->segment() == this);
161 return spanSign(angle->start(), angle->end());
162 }
163
164 int spanSign(int startIndex, int endIndex) const {
165 int result = startIndex < endIndex ? -fTs[startIndex].fWindValue : fTs[endIndex].fWindValue;
166#if DEBUG_WIND_BUMP
167 SkDebugf("%s spanSign=%d\n", __FUNCTION__, result);
168#endif
169 return result;
170 }
171
172 // OPTIMIZATION: mark as debugging only if used solely by tests
173 double t(int tIndex) const {
174 return fTs[tIndex].fT;
175 }
176
177 double tAtMid(int start, int end, double mid) const {
178 return fTs[start].fT * (1 - mid) + fTs[end].fT * mid;
179 }
180
181 bool unsortable(int index) const {
182 return fTs[index].fUnsortableStart || fTs[index].fUnsortableEnd;
183 }
184
185 void updatePts(const SkPoint pts[]) {
186 fPts = pts;
187 }
skia.committer@gmail.com32840172013-04-09 07:01:27 +0000188
caryclark@google.com07393ca2013-04-08 11:47:37 +0000189 SkPath::Verb verb() const {
190 return fVerb;
191 }
192
193 int windSum(int tIndex) const {
194 return fTs[tIndex].fWindSum;
195 }
196
197 int windValue(int tIndex) const {
198 return fTs[tIndex].fWindValue;
199 }
200
201 SkScalar xAtT(int index) const {
202 return xAtT(&fTs[index]);
203 }
204
205 SkScalar xAtT(const SkOpSpan* span) const {
206 return xyAtT(span).fX;
207 }
208
209 const SkPoint& xyAtT(const SkOpSpan* span) const {
210 return span->fPt;
211 }
212
213 // used only by right angle winding finding
214 SkPoint xyAtT(double mid) const {
215 return (*CurvePointAtT[fVerb])(fPts, mid);
216 }
217
218 const SkPoint& xyAtT(int index) const {
219 return xyAtT(&fTs[index]);
220 }
skia.committer@gmail.com32840172013-04-09 07:01:27 +0000221
caryclark@google.com07393ca2013-04-08 11:47:37 +0000222 SkScalar yAtT(int index) const {
223 return yAtT(&fTs[index]);
224 }
225
226 SkScalar yAtT(const SkOpSpan* span) const {
227 return xyAtT(span).fY;
228 }
229
230 bool activeAngle(int index, int* done, SkTDArray<SkOpAngle>* angles);
231 SkPoint activeLeftTop(bool onlySortable, int* firstT) const;
232 bool activeOp(int index, int endIndex, int xorMiMask, int xorSuMask, SkPathOp op);
233 bool activeOp(int xorMiMask, int xorSuMask, int index, int endIndex, SkPathOp op,
234 int* sumMiWinding, int* sumSuWinding, int* maxWinding, int* sumWinding,
235 int* oppMaxWinding, int* oppSumWinding);
236 bool activeWinding(int index, int endIndex);
237 bool activeWinding(int index, int endIndex, int* maxWinding, int* sumWinding);
238 void addCubic(const SkPoint pts[4], bool operand, bool evenOdd);
239 void addCurveTo(int start, int end, SkPathWriter* path, bool active) const;
240 void addLine(const SkPoint pts[2], bool operand, bool evenOdd);
241 void addOtherT(int index, double otherT, int otherIndex);
242 void addQuad(const SkPoint pts[3], bool operand, bool evenOdd);
243 int addSelfT(SkOpSegment* other, const SkPoint& pt, double newT);
244 int addT(SkOpSegment* other, const SkPoint& pt, double newT);
245 void addTCancel(double startT, double endT, SkOpSegment* other, double oStartT, double oEndT);
246 void addTCoincident(double startT, double endT, SkOpSegment* other, double oStartT,
247 double oEndT);
248 void addTPair(double t, SkOpSegment* other, double otherT, bool borrowWind, const SkPoint& pt);
249 int addUnsortableT(SkOpSegment* other, bool start, const SkPoint& pt, double newT);
250 bool betweenTs(int lesser, double testT, int greater) const;
251 int computeSum(int startIndex, int endIndex, bool binary);
252 int crossedSpanY(const SkPoint& basePt, SkScalar* bestY, double* hitT, bool* hitSomething,
253 double mid, bool opp, bool current) const;
254 SkOpSegment* findNextOp(SkTDArray<SkOpSpan*>* chase, int* nextStart, int* nextEnd,
255 bool* unsortable, SkPathOp op, const int xorMiMask,
256 const int xorSuMask);
257 SkOpSegment* findNextWinding(SkTDArray<SkOpSpan*>* chase, int* nextStart, int* nextEnd,
258 bool* unsortable);
259 SkOpSegment* findNextXor(int* nextStart, int* nextEnd, bool* unsortable);
260 void findTooCloseToCall();
261 SkOpSegment* findTop(int* tIndex, int* endIndex, bool* unsortable, bool onlySortable);
262 void fixOtherTIndex();
263 void initWinding(int start, int end);
264 void initWinding(int start, int end, double tHit, int winding, SkScalar hitDx, int oppWind,
265 SkScalar hitOppDx);
266 bool isLinear(int start, int end) const;
267 bool isMissing(double startT) const;
268 bool isSimple(int end) const;
269 SkOpSpan* markAndChaseDoneBinary(int index, int endIndex);
270 SkOpSpan* markAndChaseDoneUnary(int index, int endIndex);
271 SkOpSpan* markAndChaseWinding(const SkOpAngle* angle, int winding, int oppWinding);
272 SkOpSpan* markAngle(int maxWinding, int sumWinding, int oppMaxWinding, int oppSumWinding,
273 bool activeAngle, const SkOpAngle* angle);
274 void markDone(int index, int winding);
275 void markDoneBinary(int index);
276 void markDoneUnary(int index);
277 SkOpSpan* markOneWinding(const char* funName, int tIndex, int winding);
278 SkOpSpan* markOneWinding(const char* funName, int tIndex, int winding, int oppWinding);
279 void markWinding(int index, int winding);
280 void markWinding(int index, int winding, int oppWinding);
281 bool nextCandidate(int* start, int* end) const;
282 int nextExactSpan(int from, int step) const;
283 int nextSpan(int from, int step) const;
284 void setUpWindings(int index, int endIndex, int* sumMiWinding, int* sumSuWinding,
285 int* maxWinding, int* sumWinding, int* oppMaxWinding, int* oppSumWinding);
286 static bool SortAngles(const SkTDArray<SkOpAngle>& angles, SkTDArray<SkOpAngle*>* angleList);
287 void subDivide(int start, int end, SkPoint edge[4]) const;
288 void undoneSpan(int* start, int* end);
289 int updateOppWindingReverse(const SkOpAngle* angle) const;
290 int updateWindingReverse(const SkOpAngle* angle) const;
291 static bool UseInnerWinding(int outerWinding, int innerWinding);
292 int windingAtT(double tHit, int tIndex, bool crossOpp, SkScalar* dx) const;
293 int windSum(const SkOpAngle* angle) const;
294 int windValue(const SkOpAngle* angle) const;
295
296#if DEBUG_DUMP
297 int debugID() const {
298 return fID;
299 }
300#endif
301#if DEBUG_ACTIVE_SPANS
302 void debugShowActiveSpans() const;
303#endif
304#if DEBUG_SORT || DEBUG_SWAP_TOP
305 void debugShowSort(const char* fun, const SkTDArray<SkOpAngle*>& angles, int first,
306 const int contourWinding, const int oppContourWinding) const;
307 void debugShowSort(const char* fun, const SkTDArray<SkOpAngle*>& angles, int first);
308#endif
309#if DEBUG_CONCIDENT
310 void debugShowTs() const;
311#endif
312#if DEBUG_SHOW_WINDING
313 int debugShowWindingValues(int slotCount, int ofInterest) const;
314#endif
315
316private:
317 bool activeAngleOther(int index, int* done, SkTDArray<SkOpAngle>* angles);
318 bool activeAngleInner(int index, int* done, SkTDArray<SkOpAngle>* angles);
319 void addAngle(SkTDArray<SkOpAngle>* angles, int start, int end) const;
320 void addCancelOutsides(double tStart, double oStart, SkOpSegment* other, double oEnd);
321 void addCoinOutsides(const SkTDArray<double>& outsideTs, SkOpSegment* other, double oEnd);
322 void addTwoAngles(int start, int end, SkTDArray<SkOpAngle>* angles) const;
323 int advanceCoincidentOther(const SkOpSpan* test, double oEndT, int oIndex);
324 int advanceCoincidentThis(const SkOpSpan* oTest, bool opp, int index);
325 void buildAngles(int index, SkTDArray<SkOpAngle>* angles, bool includeOpp) const;
326 void buildAnglesInner(int index, SkTDArray<SkOpAngle>* angles) const;
327 int bumpCoincidentThis(const SkOpSpan& oTest, bool opp, int index,
328 SkTDArray<double>* outsideTs);
329 int bumpCoincidentOther(const SkOpSpan& test, double oEndT, int& oIndex,
330 SkTDArray<double>* oOutsideTs);
331 bool bumpSpan(SkOpSpan* span, int windDelta, int oppDelta);
332 bool clockwise(int tStart, int tEnd) const;
333 void decrementSpan(SkOpSpan* span);
334 bool equalPoints(int greaterTIndex, int lesserTIndex);
335 int findStartingEdge(const SkTDArray<SkOpAngle*>& sorted, int start, int end);
336 void init(const SkPoint pts[], SkPath::Verb verb, bool operand, bool evenOdd);
337 void matchWindingValue(int tIndex, double t, bool borrowWind);
338 SkOpSpan* markAndChaseDone(int index, int endIndex, int winding);
339 SkOpSpan* markAndChaseDoneBinary(const SkOpAngle* angle, int winding, int oppWinding);
340 SkOpSpan* markAndChaseWinding(const SkOpAngle* angle, const int winding);
341 SkOpSpan* markAndChaseWinding(int index, int endIndex, int winding, int oppWinding);
342 SkOpSpan* markAngle(int maxWinding, int sumWinding, bool activeAngle, const SkOpAngle* angle);
343 void markDoneBinary(int index, int winding, int oppWinding);
344 SkOpSpan* markAndChaseDoneUnary(const SkOpAngle* angle, int winding);
345 void markOneDone(const char* funName, int tIndex, int winding);
346 void markOneDoneBinary(const char* funName, int tIndex);
347 void markOneDoneBinary(const char* funName, int tIndex, int winding, int oppWinding);
348 void markOneDoneUnary(const char* funName, int tIndex);
349 void markUnsortable(int start, int end);
350 bool monotonicInY(int tStart, int tEnd) const;
351 bool multipleSpans(int end) const;
352 SkOpSegment* nextChase(int* index, const int step, int* min, SkOpSpan** last);
353 bool serpentine(int tStart, int tEnd) const;
354 void subDivideBounds(int start, int end, SkPathOpsBounds* bounds) const;
355 bool tiny(const SkOpAngle* angle) const;
356 static void TrackOutside(SkTDArray<double>* outsideTs, double end, double start);
357 int updateOppWinding(int index, int endIndex) const;
358 int updateOppWinding(const SkOpAngle* angle) const;
359 int updateWinding(int index, int endIndex) const;
360 int updateWinding(const SkOpAngle* angle) const;
361 SkOpSpan* verifyOneWinding(const char* funName, int tIndex);
362 SkOpSpan* verifyOneWindingU(const char* funName, int tIndex);
363 int windValueAt(double t) const;
364 void zeroSpan(SkOpSpan* span);
365
366#if DEBUG_SWAP_TOP
367 bool controlsContainedByEnds(int tStart, int tEnd) const;
368#endif
369#if DEBUG_CONCIDENT
370 void debugAddTPair(double t, const SkOpSegment& other, double otherT) const;
371#endif
372#if DEBUG_MARK_DONE || DEBUG_UNSORTABLE
373 void debugShowNewWinding(const char* fun, const SkOpSpan& span, int winding);
374 void debugShowNewWinding(const char* fun, const SkOpSpan& span, int winding, int oppWinding);
375#endif
376#if DEBUG_WINDING
377 static char as_digit(int value) {
378 return value < 0 ? '?' : value <= 9 ? '0' + value : '+';
379 }
380#endif
381
382 const SkPoint* fPts;
383 SkPathOpsBounds fBounds;
384 SkTDArray<SkOpSpan> fTs; // two or more (always includes t=0 t=1)
385 // OPTIMIZATION: could pack donespans, verb, operand, xor into 1 int-sized value
386 int fDoneSpans; // quick check that segment is finished
387 // OPTIMIZATION: force the following to be byte-sized
388 SkPath::Verb fVerb;
389 bool fOperand;
390 bool fXor; // set if original contour had even-odd fill
391 bool fOppXor; // set if opposite operand had even-odd fill
392#if DEBUG_DUMP
393 int fID;
394#endif
395};
396
397#endif