blob: d1b3cd0179da6dccdfa310e03cfaf8e21f87c723 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
2 * Copyright 2013 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 SkOpContour_DEFINED
8#define SkOpContour_DEFINED
9
10#include "SkOpSegment.h"
11#include "SkTArray.h"
12
commit-bot@chromium.org0bcb8ca2014-04-25 13:42:21 +000013#if defined(SK_DEBUG) || !FORCE_RELEASE
14#include "SkThread.h"
15#endif
16
caryclark@google.com07393ca2013-04-08 11:47:37 +000017class SkIntersections;
18class SkOpContour;
19class SkPathWriter;
20
21struct SkCoincidence {
caryclark@google.com570863f2013-09-16 15:55:01 +000022 SkOpContour* fOther;
caryclark@google.com07393ca2013-04-08 11:47:37 +000023 int fSegments[2];
24 double fTs[2][2];
caryclarkdac1d172014-06-17 05:15:38 -070025 SkPoint fPts[2][2];
26 int fNearly[2];
caryclark@google.com07393ca2013-04-08 11:47:37 +000027};
28
29class SkOpContour {
30public:
31 SkOpContour() {
32 reset();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000033#if defined(SK_DEBUG) || !FORCE_RELEASE
commit-bot@chromium.org0bcb8ca2014-04-25 13:42:21 +000034 fID = sk_atomic_inc(&SkPathOpsDebug::gContourID);
caryclark@google.com07393ca2013-04-08 11:47:37 +000035#endif
36 }
37
38 bool operator<(const SkOpContour& rh) const {
39 return fBounds.fTop == rh.fBounds.fTop
40 ? fBounds.fLeft < rh.fBounds.fLeft
41 : fBounds.fTop < rh.fBounds.fTop;
42 }
43
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000044 bool addCoincident(int index, SkOpContour* other, int otherIndex,
caryclark@google.com07393ca2013-04-08 11:47:37 +000045 const SkIntersections& ts, bool swap);
46 void addCoincidentPoints();
47
48 void addCross(const SkOpContour* crosser) {
49#ifdef DEBUG_CROSS
50 for (int index = 0; index < fCrosses.count(); ++index) {
51 SkASSERT(fCrosses[index] != crosser);
52 }
53#endif
caryclark@google.comd892bd82013-06-17 14:10:36 +000054 fCrosses.push_back(crosser);
caryclark@google.com07393ca2013-04-08 11:47:37 +000055 }
56
57 void addCubic(const SkPoint pts[4]) {
58 fSegments.push_back().addCubic(pts, fOperand, fXor);
59 fContainsCurves = fContainsCubics = true;
60 }
61
62 int addLine(const SkPoint pts[2]) {
63 fSegments.push_back().addLine(pts, fOperand, fXor);
64 return fSegments.count();
65 }
66
67 void addOtherT(int segIndex, int tIndex, double otherT, int otherIndex) {
68 fSegments[segIndex].addOtherT(tIndex, otherT, otherIndex);
69 }
70
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000071 bool addPartialCoincident(int index, SkOpContour* other, int otherIndex,
caryclark@google.com570863f2013-09-16 15:55:01 +000072 const SkIntersections& ts, int ptIndex, bool swap);
73
caryclark@google.com07393ca2013-04-08 11:47:37 +000074 int addQuad(const SkPoint pts[3]) {
75 fSegments.push_back().addQuad(pts, fOperand, fXor);
76 fContainsCurves = true;
77 return fSegments.count();
78 }
79
commit-bot@chromium.org866f4e32013-11-21 17:04:29 +000080 int addT(int segIndex, SkOpContour* other, int otherIndex, const SkPoint& pt, double newT) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000081 setContainsIntercepts();
commit-bot@chromium.org866f4e32013-11-21 17:04:29 +000082 return fSegments[segIndex].addT(&other->fSegments[otherIndex], pt, newT);
caryclark@google.com07393ca2013-04-08 11:47:37 +000083 }
84
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000085 int addSelfT(int segIndex, const SkPoint& pt, double newT) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000086 setContainsIntercepts();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000087 return fSegments[segIndex].addSelfT(pt, newT);
caryclark@google.com07393ca2013-04-08 11:47:37 +000088 }
89
caryclarkdac1d172014-06-17 05:15:38 -070090 void align(const SkOpSegment::AlignedSpan& aligned, bool swap, SkCoincidence* coincidence);
91 void alignCoincidence(const SkOpSegment::AlignedSpan& aligned,
92 SkTArray<SkCoincidence, true>* coincidences);
93
94 void alignCoincidence(const SkOpSegment::AlignedSpan& aligned) {
95 alignCoincidence(aligned, &fCoincidences);
96 alignCoincidence(aligned, &fPartialCoincidences);
97 }
98
99 void alignMultiples(SkTDArray<SkOpSegment::AlignedSpan>* aligned) {
100 int segmentCount = fSegments.count();
101 for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
102 SkOpSegment& segment = fSegments[sIndex];
103 if (segment.hasMultiples()) {
104 segment.alignMultiples(aligned);
105 }
106 }
107 }
108
109 void alignTPt(int segmentIndex, const SkOpContour* other, int otherIndex,
110 bool swap, int tIndex, SkIntersections* ts, SkPoint* point) const;
111
caryclark@google.com07393ca2013-04-08 11:47:37 +0000112 const SkPathOpsBounds& bounds() const {
113 return fBounds;
114 }
115
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000116 bool calcAngles();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000117 void calcCoincidentWinding();
caryclark@google.com570863f2013-09-16 15:55:01 +0000118 void calcPartialCoincidentWinding();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000119
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000120 void checkDuplicates() {
121 int segmentCount = fSegments.count();
122 for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
123 SkOpSegment& segment = fSegments[sIndex];
124 if (segment.count() > 2) {
125 segment.checkDuplicates();
126 }
127 }
128 }
129
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000130 void checkEnds() {
131 if (!fContainsCurves) {
132 return;
133 }
134 int segmentCount = fSegments.count();
135 for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
136 SkOpSegment* segment = &fSegments[sIndex];
137 if (segment->verb() == SkPath::kLine_Verb) {
138 continue;
139 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000140 if (segment->done()) {
141 continue; // likely coincident, nothing to do
142 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000143 segment->checkEnds();
144 }
145 }
146
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000147 void checkMultiples() {
148 int segmentCount = fSegments.count();
149 for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
150 SkOpSegment& segment = fSegments[sIndex];
151 if (segment.count() > 2) {
152 segment.checkMultiples();
caryclarkdac1d172014-06-17 05:15:38 -0700153 fMultiples |= segment.hasMultiples();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000154 }
155 }
156 }
157
158 void checkSmall() {
159 int segmentCount = fSegments.count();
160 for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
161 SkOpSegment& segment = fSegments[sIndex];
caryclarkdac1d172014-06-17 05:15:38 -0700162 // OPTIMIZATION : skip segments that are done?
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000163 if (segment.hasSmall()) {
164 segment.checkSmall();
165 }
166 }
167 }
168
caryclark@google.com570863f2013-09-16 15:55:01 +0000169 // if same point has different T values, choose a common T
170 void checkTiny() {
171 int segmentCount = fSegments.count();
172 if (segmentCount <= 2) {
173 return;
174 }
175 for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000176 SkOpSegment& segment = fSegments[sIndex];
177 if (segment.hasTiny()) {
178 segment.checkTiny();
179 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000180 }
181 }
182
caryclark@google.com07393ca2013-04-08 11:47:37 +0000183 void complete() {
184 setBounds();
185 fContainsIntercepts = false;
186 }
187
188 bool containsCubics() const {
189 return fContainsCubics;
190 }
191
192 bool crosses(const SkOpContour* crosser) const {
193 for (int index = 0; index < fCrosses.count(); ++index) {
194 if (fCrosses[index] == crosser) {
195 return true;
196 }
197 }
198 return false;
199 }
200
201 bool done() const {
202 return fDone;
203 }
204
205 const SkPoint& end() const {
206 const SkOpSegment& segment = fSegments.back();
reed@google.com277c3f82013-05-31 15:17:50 +0000207 return segment.pts()[SkPathOpsVerbToPoints(segment.verb())];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000208 }
209
caryclark@google.com07393ca2013-04-08 11:47:37 +0000210 void fixOtherTIndex() {
211 int segmentCount = fSegments.count();
212 for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
213 fSegments[sIndex].fixOtherTIndex();
214 }
215 }
216
caryclarkdac1d172014-06-17 05:15:38 -0700217 bool hasMultiples() const {
218 return fMultiples;
219 }
220
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000221 void joinCoincidence() {
222 joinCoincidence(fCoincidences, false);
223 joinCoincidence(fPartialCoincidences, true);
224 }
225
caryclark@google.com07393ca2013-04-08 11:47:37 +0000226 SkOpSegment* nonVerticalSegment(int* start, int* end);
skia.committer@gmail.com32840172013-04-09 07:01:27 +0000227
caryclark@google.com07393ca2013-04-08 11:47:37 +0000228 bool operand() const {
229 return fOperand;
230 }
231
232 void reset() {
233 fSegments.reset();
234 fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax);
caryclarkdac1d172014-06-17 05:15:38 -0700235 fContainsCurves = fContainsCubics = fContainsIntercepts = fDone = fMultiples = false;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000236 }
237
caryclarkdac1d172014-06-17 05:15:38 -0700238 void resolveNearCoincidence();
239
caryclark@google.com07393ca2013-04-08 11:47:37 +0000240 SkTArray<SkOpSegment>& segments() {
241 return fSegments;
242 }
243
244 void setContainsIntercepts() {
245 fContainsIntercepts = true;
246 }
247
248 void setOperand(bool isOp) {
249 fOperand = isOp;
250 }
251
252 void setOppXor(bool isOppXor) {
253 fOppXor = isOppXor;
254 int segmentCount = fSegments.count();
255 for (int test = 0; test < segmentCount; ++test) {
256 fSegments[test].setOppXor(isOppXor);
257 }
258 }
259
260 void setXor(bool isXor) {
261 fXor = isXor;
262 }
263
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000264 void sortAngles();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000265 void sortSegments();
skia.committer@gmail.com32840172013-04-09 07:01:27 +0000266
caryclark@google.com07393ca2013-04-08 11:47:37 +0000267 const SkPoint& start() const {
268 return fSegments.front().pts()[0];
269 }
skia.committer@gmail.com32840172013-04-09 07:01:27 +0000270
caryclark@google.com07393ca2013-04-08 11:47:37 +0000271 void toPath(SkPathWriter* path) const;
skia.committer@gmail.com32840172013-04-09 07:01:27 +0000272
caryclark@google.com07393ca2013-04-08 11:47:37 +0000273 void toPartialBackward(SkPathWriter* path) const {
274 int segmentCount = fSegments.count();
275 for (int test = segmentCount - 1; test >= 0; --test) {
276 fSegments[test].addCurveTo(1, 0, path, true);
277 }
278 }
279
280 void toPartialForward(SkPathWriter* path) const {
281 int segmentCount = fSegments.count();
282 for (int test = 0; test < segmentCount; ++test) {
283 fSegments[test].addCurveTo(0, 1, path, true);
284 }
285 }
286
287 void topSortableSegment(const SkPoint& topLeft, SkPoint* bestXY, SkOpSegment** topStart);
288 SkOpSegment* undoneSegment(int* start, int* end);
289
290 int updateSegment(int index, const SkPoint* pts) {
291 SkOpSegment& segment = fSegments[index];
292 segment.updatePts(pts);
reed@google.com277c3f82013-05-31 15:17:50 +0000293 return SkPathOpsVerbToPoints(segment.verb()) + 1;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000294 }
295
296#if DEBUG_TEST
297 SkTArray<SkOpSegment>& debugSegments() {
298 return fSegments;
299 }
300#endif
301
caryclark@google.coma5e55922013-05-07 18:51:31 +0000302#if DEBUG_ACTIVE_SPANS || DEBUG_ACTIVE_SPANS_FIRST_ONLY
caryclark@google.com07393ca2013-04-08 11:47:37 +0000303 void debugShowActiveSpans() {
304 for (int index = 0; index < fSegments.count(); ++index) {
305 fSegments[index].debugShowActiveSpans();
306 }
307 }
308#endif
309
310#if DEBUG_SHOW_WINDING
311 int debugShowWindingValues(int totalSegments, int ofInterest);
caryclark@google.comd892bd82013-06-17 14:10:36 +0000312 static void debugShowWindingValues(const SkTArray<SkOpContour*, true>& contourList);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000313#endif
314
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000315 // available to test routines only
316 void dump() const;
317 void dumpAngles() const;
caryclarkdac1d172014-06-17 05:15:38 -0700318 void dumpCoincidence(const SkCoincidence& ) const;
319 void dumpCoincidences() const;
320 void dumpPt(int ) const;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000321 void dumpPts() const;
caryclarkdac1d172014-06-17 05:15:38 -0700322 void dumpSpan(int ) const;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000323 void dumpSpans() const;
324
caryclark@google.com07393ca2013-04-08 11:47:37 +0000325private:
caryclarkdac1d172014-06-17 05:15:38 -0700326 void alignPt(int index, SkPoint* point, int zeroPt) const;
327 int alignT(bool swap, int tIndex, SkIntersections* ts) const;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000328 void calcCommonCoincidentWinding(const SkCoincidence& );
caryclarkdac1d172014-06-17 05:15:38 -0700329 void checkCoincidentPair(const SkCoincidence& oneCoin, int oneIdx,
330 const SkCoincidence& twoCoin, int twoIdx, bool partial);
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000331 void joinCoincidence(const SkTArray<SkCoincidence, true>& , bool partial);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000332 void setBounds();
333
334 SkTArray<SkOpSegment> fSegments;
caryclark@google.comd892bd82013-06-17 14:10:36 +0000335 SkTArray<SkOpSegment*, true> fSortedSegments;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000336 int fFirstSorted;
caryclark@google.comd892bd82013-06-17 14:10:36 +0000337 SkTArray<SkCoincidence, true> fCoincidences;
caryclark@google.com570863f2013-09-16 15:55:01 +0000338 SkTArray<SkCoincidence, true> fPartialCoincidences;
caryclark@google.comd892bd82013-06-17 14:10:36 +0000339 SkTArray<const SkOpContour*, true> fCrosses;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000340 SkPathOpsBounds fBounds;
341 bool fContainsIntercepts; // FIXME: is this used by anybody?
342 bool fContainsCubics;
343 bool fContainsCurves;
344 bool fDone;
caryclarkdac1d172014-06-17 05:15:38 -0700345 bool fMultiples; // set if some segment has multiple identical intersections with other curves
caryclark@google.com07393ca2013-04-08 11:47:37 +0000346 bool fOperand; // true for the second argument to a binary operator
347 bool fXor;
348 bool fOppXor;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000349#if defined(SK_DEBUG) || !FORCE_RELEASE
350 int debugID() const { return fID; }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000351 int fID;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000352#else
353 int debugID() const { return -1; }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000354#endif
355};
356
357#endif