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