blob: f3402daa671a5c3bd4ea43588c71ca1528bc41c7 [file] [log] [blame]
caryclark45fa4472015-01-16 07:04:10 -08001/*
2 * Copyright 2014 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 */
deanm12670eb2016-04-26 14:09:01 -07007#ifndef SkPathOpsTSect_DEFINED
8#define SkPathOpsTSect_DEFINED
caryclark45fa4472015-01-16 07:04:10 -08009
10#include "SkChunkAlloc.h"
caryclark54359292015-03-26 07:52:43 -070011#include "SkPathOpsBounds.h"
caryclark45fa4472015-01-16 07:04:10 -080012#include "SkPathOpsRect.h"
caryclark45fa4472015-01-16 07:04:10 -080013#include "SkIntersections.h"
caryclark54359292015-03-26 07:52:43 -070014#include "SkTSort.h"
caryclark45fa4472015-01-16 07:04:10 -080015
caryclarked0935a2015-10-22 07:23:52 -070016#ifdef SK_DEBUG
17typedef uint8_t SkOpDebugBool;
18#else
19typedef bool SkOpDebugBool;
20#endif
21
caryclark1049f122015-04-20 08:31:59 -070022/* TCurve and OppCurve are one of { SkDQuadratic, SkDConic, SkDCubic } */
23template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -080024class SkTCoincident {
25public:
caryclark697ac1c2015-04-13 09:36:01 -070026 SkTCoincident() {
caryclarkdf386c52015-04-21 05:27:02 -070027 this->init();
caryclark1049f122015-04-20 08:31:59 -070028 }
29
caryclarked0935a2015-10-22 07:23:52 -070030 void debugInit() {
31#ifdef SK_DEBUG
32 this->fPerpPt.fX = this->fPerpPt.fY = SK_ScalarNaN;
33 this->fPerpT = SK_ScalarNaN;
caryclark6c3b9cd2016-09-26 05:36:58 -070034 this->fMatch = 0xFF;
caryclarked0935a2015-10-22 07:23:52 -070035#endif
36 }
37
38 char dumpIsCoincidentStr() const;
caryclark1049f122015-04-20 08:31:59 -070039 void dump() const;
40
caryclark6c3b9cd2016-09-26 05:36:58 -070041 bool isMatch() const {
42 SkASSERT(!!fMatch == fMatch);
43 return SkToBool(fMatch);
caryclark45fa4472015-01-16 07:04:10 -080044 }
45
46 void init() {
caryclarkdf386c52015-04-21 05:27:02 -070047 fPerpT = -1;
caryclark6c3b9cd2016-09-26 05:36:58 -070048 fMatch = false;
caryclarkdf386c52015-04-21 05:27:02 -070049 fPerpPt.fX = fPerpPt.fY = SK_ScalarNaN;
caryclark45fa4472015-01-16 07:04:10 -080050 }
51
52 void markCoincident() {
caryclark6c3b9cd2016-09-26 05:36:58 -070053 if (!fMatch) {
caryclark45fa4472015-01-16 07:04:10 -080054 fPerpT = -1;
55 }
caryclark6c3b9cd2016-09-26 05:36:58 -070056 fMatch = true;
caryclark45fa4472015-01-16 07:04:10 -080057 }
58
59 const SkDPoint& perpPt() const {
60 return fPerpPt;
61 }
62
63 double perpT() const {
64 return fPerpT;
65 }
66
caryclark1049f122015-04-20 08:31:59 -070067 void setPerp(const TCurve& c1, double t, const SkDPoint& cPt, const OppCurve& );
caryclark45fa4472015-01-16 07:04:10 -080068
69private:
70 SkDPoint fPerpPt;
71 double fPerpT; // perpendicular intersection on opposite curve
caryclark6c3b9cd2016-09-26 05:36:58 -070072 SkOpDebugBool fMatch;
caryclark45fa4472015-01-16 07:04:10 -080073};
74
caryclark1049f122015-04-20 08:31:59 -070075template<typename TCurve, typename OppCurve> class SkTSect;
76template<typename TCurve, typename OppCurve> class SkTSpan;
caryclark54359292015-03-26 07:52:43 -070077
caryclark1049f122015-04-20 08:31:59 -070078template<typename TCurve, typename OppCurve>
caryclark54359292015-03-26 07:52:43 -070079struct SkTSpanBounded {
caryclark1049f122015-04-20 08:31:59 -070080 SkTSpan<TCurve, OppCurve>* fBounded;
caryclark54359292015-03-26 07:52:43 -070081 SkTSpanBounded* fNext;
82};
caryclark45fa4472015-01-16 07:04:10 -080083
84/* Curve is either TCurve or SkDCubic */
caryclark1049f122015-04-20 08:31:59 -070085template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -080086class SkTSpan {
87public:
caryclark1049f122015-04-20 08:31:59 -070088 void addBounded(SkTSpan<OppCurve, TCurve>* , SkChunkAlloc* );
reed0dc4dd62015-03-24 13:55:33 -070089 double closestBoundedT(const SkDPoint& pt) const;
caryclark54359292015-03-26 07:52:43 -070090 bool contains(double t) const;
reed0dc4dd62015-03-24 13:55:33 -070091
caryclark1049f122015-04-20 08:31:59 -070092 void debugInit() {
93 TCurve dummy;
94 dummy.debugInit();
95 init(dummy);
96 initBounds(dummy);
caryclarkdf386c52015-04-21 05:27:02 -070097 fCoinStart.init();
98 fCoinEnd.init();
caryclark1049f122015-04-20 08:31:59 -070099 }
100
101 const SkTSect<OppCurve, TCurve>* debugOpp() const;
caryclark643ede62016-08-08 14:27:45 -0700102
103#ifdef SK_DEBUG
104 void debugSetGlobalState(SkOpGlobalState* state) {
105 fDebugGlobalState = state;
106 }
107#endif
108
caryclark54359292015-03-26 07:52:43 -0700109 const SkTSpan* debugSpan(int ) const;
110 const SkTSpan* debugT(double t) const;
111#ifdef SK_DEBUG
112 bool debugIsBefore(const SkTSpan* span) const;
113#endif
114 void dump() const;
caryclark26ad22a2015-10-16 09:03:38 -0700115 void dumpAll() const;
caryclark1049f122015-04-20 08:31:59 -0700116 void dumpBounded(int id) const;
117 void dumpBounds() const;
118 void dumpCoin() const;
caryclark45fa4472015-01-16 07:04:10 -0800119
120 double endT() const {
121 return fEndT;
122 }
123
caryclark1049f122015-04-20 08:31:59 -0700124 SkTSpan<OppCurve, TCurve>* findOppSpan(const SkTSpan<OppCurve, TCurve>* opp) const;
caryclark54359292015-03-26 07:52:43 -0700125
caryclark1049f122015-04-20 08:31:59 -0700126 SkTSpan<OppCurve, TCurve>* findOppT(double t) const {
127 SkTSpan<OppCurve, TCurve>* result = oppT(t);
caryclark643ede62016-08-08 14:27:45 -0700128 SkOPASSERT(result);
caryclark45fa4472015-01-16 07:04:10 -0800129 return result;
130 }
131
caryclark643ede62016-08-08 14:27:45 -0700132 SkDEBUGCODE(SkOpGlobalState* globalState() const { return fDebugGlobalState; })
133
caryclark54359292015-03-26 07:52:43 -0700134 bool hasOppT(double t) const {
135 return SkToBool(oppT(t));
136 }
137
caryclark1049f122015-04-20 08:31:59 -0700138 int hullsIntersect(SkTSpan<OppCurve, TCurve>* span, bool* start, bool* oppStart);
caryclark54359292015-03-26 07:52:43 -0700139 void init(const TCurve& );
caryclarka35ab3e2016-10-20 08:32:18 -0700140 bool initBounds(const TCurve& );
caryclark54359292015-03-26 07:52:43 -0700141
142 bool isBounded() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700143 return fBounded != nullptr;
caryclark54359292015-03-26 07:52:43 -0700144 }
145
caryclark1049f122015-04-20 08:31:59 -0700146 bool linearsIntersect(SkTSpan<OppCurve, TCurve>* span);
caryclark54359292015-03-26 07:52:43 -0700147 double linearT(const SkDPoint& ) const;
148
149 void markCoincident() {
150 fCoinStart.markCoincident();
151 fCoinEnd.markCoincident();
152 }
caryclark45fa4472015-01-16 07:04:10 -0800153
154 const SkTSpan* next() const {
155 return fNext;
156 }
157
caryclark1049f122015-04-20 08:31:59 -0700158 bool onlyEndPointsInCommon(const SkTSpan<OppCurve, TCurve>* opp, bool* start,
159 bool* oppStart, bool* ptsInCommon);
caryclark54359292015-03-26 07:52:43 -0700160
caryclark45fa4472015-01-16 07:04:10 -0800161 const TCurve& part() const {
162 return fPart;
163 }
164
caryclark54359292015-03-26 07:52:43 -0700165 bool removeAllBounded();
caryclark1049f122015-04-20 08:31:59 -0700166 bool removeBounded(const SkTSpan<OppCurve, TCurve>* opp);
caryclark54359292015-03-26 07:52:43 -0700167
caryclark45fa4472015-01-16 07:04:10 -0800168 void reset() {
halcanary96fcdcc2015-08-27 07:41:13 -0700169 fBounded = nullptr;
caryclark45fa4472015-01-16 07:04:10 -0800170 }
171
caryclark54359292015-03-26 07:52:43 -0700172 void resetBounds(const TCurve& curve) {
173 fIsLinear = fIsLine = false;
174 initBounds(curve);
caryclark45fa4472015-01-16 07:04:10 -0800175 }
176
caryclark54359292015-03-26 07:52:43 -0700177 bool split(SkTSpan* work, SkChunkAlloc* heap) {
178 return splitAt(work, (work->fStartT + work->fEndT) * 0.5, heap);
179 }
180
181 bool splitAt(SkTSpan* work, double t, SkChunkAlloc* heap);
caryclark45fa4472015-01-16 07:04:10 -0800182
183 double startT() const {
184 return fStartT;
185 }
186
caryclark54359292015-03-26 07:52:43 -0700187private:
caryclark45fa4472015-01-16 07:04:10 -0800188
189 // implementation is for testing only
caryclark54359292015-03-26 07:52:43 -0700190 int debugID() const {
191 return PATH_OPS_DEBUG_T_SECT_RELEASE(fID, -1);
caryclark45fa4472015-01-16 07:04:10 -0800192 }
193
caryclark54359292015-03-26 07:52:43 -0700194 void dumpID() const;
caryclark45fa4472015-01-16 07:04:10 -0800195
caryclark1049f122015-04-20 08:31:59 -0700196 int hullCheck(const SkTSpan<OppCurve, TCurve>* opp, bool* start, bool* oppStart);
197 int linearIntersects(const OppCurve& ) const;
198 SkTSpan<OppCurve, TCurve>* oppT(double t) const;
caryclark45fa4472015-01-16 07:04:10 -0800199
caryclark45fa4472015-01-16 07:04:10 -0800200 void validate() const;
caryclark54359292015-03-26 07:52:43 -0700201 void validateBounded() const;
202 void validatePerpT(double oppT) const;
203 void validatePerpPt(double t, const SkDPoint& ) const;
caryclark45fa4472015-01-16 07:04:10 -0800204
205 TCurve fPart;
caryclark1049f122015-04-20 08:31:59 -0700206 SkTCoincident<TCurve, OppCurve> fCoinStart;
207 SkTCoincident<TCurve, OppCurve> fCoinEnd;
208 SkTSpanBounded<OppCurve, TCurve>* fBounded;
caryclark45fa4472015-01-16 07:04:10 -0800209 SkTSpan* fPrev;
210 SkTSpan* fNext;
211 SkDRect fBounds;
212 double fStartT;
213 double fEndT;
214 double fBoundsMax;
caryclarked0935a2015-10-22 07:23:52 -0700215 SkOpDebugBool fCollapsed;
216 SkOpDebugBool fHasPerp;
217 SkOpDebugBool fIsLinear;
218 SkOpDebugBool fIsLine;
219 SkOpDebugBool fDeleted;
caryclark643ede62016-08-08 14:27:45 -0700220 SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState);
csmartdaltonceeaa782016-08-10 10:07:57 -0700221 SkDEBUGCODE(SkTSect<TCurve, OppCurve>* fDebugSect);
caryclark54359292015-03-26 07:52:43 -0700222 PATH_OPS_DEBUG_T_SECT_CODE(int fID);
caryclark1049f122015-04-20 08:31:59 -0700223 friend class SkTSect<TCurve, OppCurve>;
224 friend class SkTSect<OppCurve, TCurve>;
225 friend class SkTSpan<OppCurve, TCurve>;
caryclark45fa4472015-01-16 07:04:10 -0800226};
227
caryclark1049f122015-04-20 08:31:59 -0700228template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -0800229class SkTSect {
230public:
caryclarke25a4f62016-07-26 09:26:29 -0700231 SkTSect(const TCurve& c SkDEBUGPARAMS(SkOpGlobalState* ) PATH_OPS_DEBUG_T_SECT_PARAMS(int id));
caryclark1049f122015-04-20 08:31:59 -0700232 static void BinarySearch(SkTSect* sect1, SkTSect<OppCurve, TCurve>* sect2,
233 SkIntersections* intersections);
caryclark45fa4472015-01-16 07:04:10 -0800234
caryclarke25a4f62016-07-26 09:26:29 -0700235 SkDEBUGCODE(SkOpGlobalState* globalState() { return fDebugGlobalState; })
caryclark45fa4472015-01-16 07:04:10 -0800236 // for testing only
caryclark1049f122015-04-20 08:31:59 -0700237 bool debugHasBounded(const SkTSpan<OppCurve, TCurve>* ) const;
caryclark54359292015-03-26 07:52:43 -0700238
caryclark1049f122015-04-20 08:31:59 -0700239 const SkTSect<OppCurve, TCurve>* debugOpp() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700240 return SkDEBUGRELEASE(fOppSect, nullptr);
caryclark54359292015-03-26 07:52:43 -0700241 }
242
caryclark1049f122015-04-20 08:31:59 -0700243 const SkTSpan<TCurve, OppCurve>* debugSpan(int id) const;
244 const SkTSpan<TCurve, OppCurve>* debugT(double t) const;
caryclark45fa4472015-01-16 07:04:10 -0800245 void dump() const;
caryclark1049f122015-04-20 08:31:59 -0700246 void dumpBoth(SkTSect<OppCurve, TCurve>* ) const;
247 void dumpBounded(int id) const;
248 void dumpBounds() const;
caryclark54359292015-03-26 07:52:43 -0700249 void dumpCoin() const;
250 void dumpCoinCurves() const;
caryclark45fa4472015-01-16 07:04:10 -0800251 void dumpCurves() const;
252
253private:
254 enum {
255 kZeroS1Set = 1,
256 kOneS1Set = 2,
257 kZeroS2Set = 4,
258 kOneS2Set = 8
259 };
260
caryclark1049f122015-04-20 08:31:59 -0700261 SkTSpan<TCurve, OppCurve>* addFollowing(SkTSpan<TCurve, OppCurve>* prior);
262 void addForPerp(SkTSpan<OppCurve, TCurve>* span, double t);
263 SkTSpan<TCurve, OppCurve>* addOne();
caryclark55888e42016-07-18 10:01:36 -0700264
caryclark1049f122015-04-20 08:31:59 -0700265 SkTSpan<TCurve, OppCurve>* addSplitAt(SkTSpan<TCurve, OppCurve>* span, double t) {
266 SkTSpan<TCurve, OppCurve>* result = this->addOne();
caryclark643ede62016-08-08 14:27:45 -0700267 SkDEBUGCODE(result->debugSetGlobalState(this->globalState()));
caryclark54359292015-03-26 07:52:43 -0700268 result->splitAt(span, t, &fHeap);
269 result->initBounds(fCurve);
270 span->initBounds(fCurve);
271 return result;
272 }
273
caryclark1049f122015-04-20 08:31:59 -0700274 bool binarySearchCoin(SkTSect<OppCurve, TCurve>* , double tStart, double tStep, double* t,
275 double* oppT);
276 SkTSpan<TCurve, OppCurve>* boundsMax() const;
caryclarkef7cee42016-09-06 09:05:54 -0700277 bool coincidentCheck(SkTSect<OppCurve, TCurve>* sect2);
caryclark26ad22a2015-10-16 09:03:38 -0700278 void coincidentForce(SkTSect<OppCurve, TCurve>* sect2, double start1s, double start1e);
caryclark54359292015-03-26 07:52:43 -0700279 bool coincidentHasT(double t);
caryclark1049f122015-04-20 08:31:59 -0700280 int collapsed() const;
281 void computePerpendiculars(SkTSect<OppCurve, TCurve>* sect2, SkTSpan<TCurve, OppCurve>* first,
282 SkTSpan<TCurve, OppCurve>* last);
283 int countConsecutiveSpans(SkTSpan<TCurve, OppCurve>* first,
284 SkTSpan<TCurve, OppCurve>** last) const;
caryclarkccec0f92015-03-24 07:28:17 -0700285
caryclark54359292015-03-26 07:52:43 -0700286 int debugID() const {
287 return PATH_OPS_DEBUG_T_SECT_RELEASE(fID, -1);
288 }
289
caryclarkef7cee42016-09-06 09:05:54 -0700290 bool deleteEmptySpans();
caryclark1049f122015-04-20 08:31:59 -0700291 void dumpCommon(const SkTSpan<TCurve, OppCurve>* ) const;
292 void dumpCommonCurves(const SkTSpan<TCurve, OppCurve>* ) const;
293 static int EndsEqual(const SkTSect* sect1, const SkTSect<OppCurve, TCurve>* sect2,
294 SkIntersections* );
caryclarkef7cee42016-09-06 09:05:54 -0700295 bool extractCoincident(SkTSect<OppCurve, TCurve>* sect2, SkTSpan<TCurve, OppCurve>* first,
296 SkTSpan<TCurve, OppCurve>* last, SkTSpan<TCurve, OppCurve>** result);
caryclark1049f122015-04-20 08:31:59 -0700297 SkTSpan<TCurve, OppCurve>* findCoincidentRun(SkTSpan<TCurve, OppCurve>* first,
298 SkTSpan<TCurve, OppCurve>** lastPtr);
299 int intersects(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp,
300 SkTSpan<OppCurve, TCurve>* oppSpan, int* oppResult);
caryclarked0935a2015-10-22 07:23:52 -0700301 bool isParallel(const SkDLine& thisLine, const SkTSect<OppCurve, TCurve>* opp) const;
caryclark1049f122015-04-20 08:31:59 -0700302 int linesIntersect(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp,
303 SkTSpan<OppCurve, TCurve>* oppSpan, SkIntersections* );
caryclarkef7cee42016-09-06 09:05:54 -0700304 bool markSpanGone(SkTSpan<TCurve, OppCurve>* span);
caryclark1049f122015-04-20 08:31:59 -0700305 bool matchedDirection(double t, const SkTSect<OppCurve, TCurve>* sect2, double t2) const;
306 void matchedDirCheck(double t, const SkTSect<OppCurve, TCurve>* sect2, double t2,
caryclark54359292015-03-26 07:52:43 -0700307 bool* calcMatched, bool* oppMatched) const;
caryclark1049f122015-04-20 08:31:59 -0700308 void mergeCoincidence(SkTSect<OppCurve, TCurve>* sect2);
309 SkTSpan<TCurve, OppCurve>* prev(SkTSpan<TCurve, OppCurve>* ) const;
310 void removeByPerpendicular(SkTSect<OppCurve, TCurve>* opp);
caryclark54359292015-03-26 07:52:43 -0700311 void recoverCollapsed();
caryclark1049f122015-04-20 08:31:59 -0700312 void removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween);
313 void removeAllBut(const SkTSpan<OppCurve, TCurve>* keep, SkTSpan<TCurve, OppCurve>* span,
314 SkTSect<OppCurve, TCurve>* opp);
caryclarkef7cee42016-09-06 09:05:54 -0700315 bool removeSpan(SkTSpan<TCurve, OppCurve>* span);
caryclark1049f122015-04-20 08:31:59 -0700316 void removeSpanRange(SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last);
317 void removeSpans(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp);
caryclark34efb702016-10-24 08:19:06 -0700318 void removedEndCheck(SkTSpan<TCurve, OppCurve>* span);
319
320 void resetRemovedEnds() {
321 fRemovedStartT = fRemovedEndT = false;
322 }
323
caryclark1049f122015-04-20 08:31:59 -0700324 SkTSpan<TCurve, OppCurve>* spanAtT(double t, SkTSpan<TCurve, OppCurve>** priorSpan);
325 SkTSpan<TCurve, OppCurve>* tail();
caryclarka35ab3e2016-10-20 08:32:18 -0700326 bool trim(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp);
caryclark1049f122015-04-20 08:31:59 -0700327 void unlinkSpan(SkTSpan<TCurve, OppCurve>* span);
328 bool updateBounded(SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last,
329 SkTSpan<OppCurve, TCurve>* oppFirst);
reed0dc4dd62015-03-24 13:55:33 -0700330 void validate() const;
caryclark54359292015-03-26 07:52:43 -0700331 void validateBounded() const;
332
caryclark45fa4472015-01-16 07:04:10 -0800333 const TCurve& fCurve;
334 SkChunkAlloc fHeap;
caryclark1049f122015-04-20 08:31:59 -0700335 SkTSpan<TCurve, OppCurve>* fHead;
336 SkTSpan<TCurve, OppCurve>* fCoincident;
337 SkTSpan<TCurve, OppCurve>* fDeleted;
caryclark45fa4472015-01-16 07:04:10 -0800338 int fActiveCount;
caryclark6c3b9cd2016-09-26 05:36:58 -0700339 bool fRemovedStartT;
340 bool fRemovedEndT;
caryclarke25a4f62016-07-26 09:26:29 -0700341 SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState);
csmartdaltonceeaa782016-08-10 10:07:57 -0700342 SkDEBUGCODE(SkTSect<OppCurve, TCurve>* fOppSect);
caryclark54359292015-03-26 07:52:43 -0700343 PATH_OPS_DEBUG_T_SECT_CODE(int fID);
344 PATH_OPS_DEBUG_T_SECT_CODE(int fDebugCount);
caryclark45fa4472015-01-16 07:04:10 -0800345#if DEBUG_T_SECT
caryclark45fa4472015-01-16 07:04:10 -0800346 int fDebugAllocatedCount;
347#endif
caryclark1049f122015-04-20 08:31:59 -0700348 friend class SkTSpan<TCurve, OppCurve>;
349 friend class SkTSpan<OppCurve, TCurve>;
350 friend class SkTSect<OppCurve, TCurve>;
caryclark45fa4472015-01-16 07:04:10 -0800351};
352
353#define COINCIDENT_SPAN_COUNT 9
354
caryclark1049f122015-04-20 08:31:59 -0700355template<typename TCurve, typename OppCurve>
356void SkTCoincident<TCurve, OppCurve>::setPerp(const TCurve& c1, double t,
357 const SkDPoint& cPt, const OppCurve& c2) {
caryclark45fa4472015-01-16 07:04:10 -0800358 SkDVector dxdy = c1.dxdyAtT(t);
359 SkDLine perp = {{ cPt, {cPt.fX + dxdy.fY, cPt.fY - dxdy.fX} }};
caryclarka35ab3e2016-10-20 08:32:18 -0700360 SkIntersections i SkDEBUGCODE((c1.globalState()));
caryclark45fa4472015-01-16 07:04:10 -0800361 int used = i.intersectRay(c2, perp);
362 // only keep closest
caryclark54359292015-03-26 07:52:43 -0700363 if (used == 0 || used == 3) {
caryclarkdf386c52015-04-21 05:27:02 -0700364 this->init();
caryclark45fa4472015-01-16 07:04:10 -0800365 return;
caryclark55888e42016-07-18 10:01:36 -0700366 }
caryclark45fa4472015-01-16 07:04:10 -0800367 fPerpT = i[0][0];
368 fPerpPt = i.pt(0);
369 SkASSERT(used <= 2);
370 if (used == 2) {
371 double distSq = (fPerpPt - cPt).lengthSquared();
372 double dist2Sq = (i.pt(1) - cPt).lengthSquared();
373 if (dist2Sq < distSq) {
374 fPerpT = i[0][1];
375 fPerpPt = i.pt(1);
376 }
377 }
caryclark54359292015-03-26 07:52:43 -0700378#if DEBUG_T_SECT
caryclark1049f122015-04-20 08:31:59 -0700379 SkDebugf("setPerp t=%1.9g cPt=(%1.9g,%1.9g) %s oppT=%1.9g fPerpPt=(%1.9g,%1.9g)\n",
380 t, cPt.fX, cPt.fY,
381 cPt.approximatelyEqual(fPerpPt) ? "==" : "!=", fPerpT, fPerpPt.fX, fPerpPt.fY);
caryclark54359292015-03-26 07:52:43 -0700382#endif
caryclark6c3b9cd2016-09-26 05:36:58 -0700383 fMatch = cPt.approximatelyEqual(fPerpPt);
caryclark45fa4472015-01-16 07:04:10 -0800384#if DEBUG_T_SECT
caryclark6c3b9cd2016-09-26 05:36:58 -0700385 if (fMatch) {
caryclark45fa4472015-01-16 07:04:10 -0800386 SkDebugf(""); // allow setting breakpoint
387 }
388#endif
389}
390
caryclark1049f122015-04-20 08:31:59 -0700391template<typename TCurve, typename OppCurve>
392void SkTSpan<TCurve, OppCurve>::addBounded(SkTSpan<OppCurve, TCurve>* span, SkChunkAlloc* heap) {
halcanary385fe4d2015-08-26 13:07:48 -0700393 SkTSpanBounded<OppCurve, TCurve>* bounded = new (heap->allocThrow(
394 sizeof(SkTSpanBounded<OppCurve, TCurve>)))(SkTSpanBounded<OppCurve, TCurve>);
caryclark54359292015-03-26 07:52:43 -0700395 bounded->fBounded = span;
396 bounded->fNext = fBounded;
397 fBounded = bounded;
398}
399
caryclark1049f122015-04-20 08:31:59 -0700400template<typename TCurve, typename OppCurve>
401SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::addFollowing(
402 SkTSpan<TCurve, OppCurve>* prior) {
403 SkTSpan<TCurve, OppCurve>* result = this->addOne();
caryclark643ede62016-08-08 14:27:45 -0700404 SkDEBUGCODE(result->debugSetGlobalState(this->globalState()));
caryclark54359292015-03-26 07:52:43 -0700405 result->fStartT = prior ? prior->fEndT : 0;
caryclark1049f122015-04-20 08:31:59 -0700406 SkTSpan<TCurve, OppCurve>* next = prior ? prior->fNext : fHead;
caryclark54359292015-03-26 07:52:43 -0700407 result->fEndT = next ? next->fStartT : 1;
408 result->fPrev = prior;
409 result->fNext = next;
410 if (prior) {
411 prior->fNext = result;
412 } else {
413 fHead = result;
414 }
415 if (next) {
416 next->fPrev = result;
417 }
418 result->resetBounds(fCurve);
caryclark643ede62016-08-08 14:27:45 -0700419 result->validate();
caryclark54359292015-03-26 07:52:43 -0700420 return result;
421}
422
caryclark1049f122015-04-20 08:31:59 -0700423template<typename TCurve, typename OppCurve>
424void SkTSect<TCurve, OppCurve>::addForPerp(SkTSpan<OppCurve, TCurve>* span, double t) {
caryclark54359292015-03-26 07:52:43 -0700425 if (!span->hasOppT(t)) {
caryclark1049f122015-04-20 08:31:59 -0700426 SkTSpan<TCurve, OppCurve>* priorSpan;
427 SkTSpan<TCurve, OppCurve>* opp = this->spanAtT(t, &priorSpan);
caryclark54359292015-03-26 07:52:43 -0700428 if (!opp) {
429 opp = this->addFollowing(priorSpan);
430#if DEBUG_PERP
caryclark26ad22a2015-10-16 09:03:38 -0700431 SkDebugf("%s priorSpan=%d t=%1.9g opp=%d\n", __FUNCTION__, priorSpan ?
432 priorSpan->debugID() : -1, t, opp->debugID());
caryclark54359292015-03-26 07:52:43 -0700433#endif
434 }
435#if DEBUG_PERP
436 opp->dump(); SkDebugf("\n");
caryclark26ad22a2015-10-16 09:03:38 -0700437 SkDebugf("%s addBounded span=%d opp=%d\n", __FUNCTION__, priorSpan ?
438 priorSpan->debugID() : -1, opp->debugID());
caryclark54359292015-03-26 07:52:43 -0700439#endif
440 opp->addBounded(span, &fHeap);
441 span->addBounded(opp, &fHeap);
442 }
443 this->validate();
caryclark1049f122015-04-20 08:31:59 -0700444#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700445 span->validatePerpT(t);
caryclark1049f122015-04-20 08:31:59 -0700446#endif
caryclark54359292015-03-26 07:52:43 -0700447}
448
caryclark1049f122015-04-20 08:31:59 -0700449template<typename TCurve, typename OppCurve>
450double SkTSpan<TCurve, OppCurve>::closestBoundedT(const SkDPoint& pt) const {
caryclark54359292015-03-26 07:52:43 -0700451 double result = -1;
caryclark343382e2016-06-29 08:18:38 -0700452 double closest = DBL_MAX;
caryclark1049f122015-04-20 08:31:59 -0700453 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700454 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -0700455 const SkTSpan<OppCurve, TCurve>* test = testBounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700456 double startDist = test->fPart[0].distanceSquared(pt);
457 if (closest > startDist) {
458 closest = startDist;
459 result = test->fStartT;
460 }
caryclark1049f122015-04-20 08:31:59 -0700461 double endDist = test->fPart[OppCurve::kPointLast].distanceSquared(pt);
caryclark54359292015-03-26 07:52:43 -0700462 if (closest > endDist) {
463 closest = endDist;
464 result = test->fEndT;
465 }
466 testBounded = testBounded->fNext;
467 }
468 SkASSERT(between(0, result, 1));
469 return result;
470}
471
472#ifdef SK_DEBUG
caryclark1049f122015-04-20 08:31:59 -0700473template<typename TCurve, typename OppCurve>
474bool SkTSpan<TCurve, OppCurve>::debugIsBefore(const SkTSpan* span) const {
caryclark54359292015-03-26 07:52:43 -0700475 const SkTSpan* work = this;
476 do {
477 if (span == work) {
478 return true;
479 }
480 } while ((work = work->fNext));
481 return false;
482}
483#endif
484
caryclark1049f122015-04-20 08:31:59 -0700485template<typename TCurve, typename OppCurve>
486bool SkTSpan<TCurve, OppCurve>::contains(double t) const {
caryclark54359292015-03-26 07:52:43 -0700487 const SkTSpan* work = this;
488 do {
489 if (between(work->fStartT, t, work->fEndT)) {
490 return true;
491 }
492 } while ((work = work->fNext));
493 return false;
494}
495
caryclark1049f122015-04-20 08:31:59 -0700496template<typename TCurve, typename OppCurve>
497const SkTSect<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::debugOpp() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700498 return SkDEBUGRELEASE(fDebugSect->debugOpp(), nullptr);
caryclark54359292015-03-26 07:52:43 -0700499}
500
caryclark1049f122015-04-20 08:31:59 -0700501template<typename TCurve, typename OppCurve>
502SkTSpan<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::findOppSpan(
503 const SkTSpan<OppCurve, TCurve>* opp) const {
504 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700505 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700506 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700507 if (opp == test) {
508 return test;
509 }
510 bounded = bounded->fNext;
511 }
halcanary96fcdcc2015-08-27 07:41:13 -0700512 return nullptr;
caryclark54359292015-03-26 07:52:43 -0700513}
514
515// returns 0 if no hull intersection
516// 1 if hulls intersect
517// 2 if hulls only share a common endpoint
518// -1 if linear and further checking is required
caryclark1049f122015-04-20 08:31:59 -0700519template<typename TCurve, typename OppCurve>
520int SkTSpan<TCurve, OppCurve>::hullCheck(const SkTSpan<OppCurve, TCurve>* opp,
521 bool* start, bool* oppStart) {
caryclark54359292015-03-26 07:52:43 -0700522 if (fIsLinear) {
523 return -1;
524 }
525 bool ptsInCommon;
526 if (onlyEndPointsInCommon(opp, start, oppStart, &ptsInCommon)) {
527 SkASSERT(ptsInCommon);
528 return 2;
529 }
530 bool linear;
531 if (fPart.hullIntersects(opp->fPart, &linear)) {
532 if (!linear) { // check set true if linear
533 return 1;
534 }
535 fIsLinear = true;
536 fIsLine = fPart.controlsInside();
caryclark2bec26a2016-05-26 09:01:47 -0700537 return ptsInCommon ? 1 : -1;
caryclark54359292015-03-26 07:52:43 -0700538 } else { // hull is not linear; check set true if intersected at the end points
539 return ((int) ptsInCommon) << 1; // 0 or 2
540 }
541 return 0;
542}
543
544// OPTIMIZE ? If at_most_end_pts_in_common detects that one quad is near linear,
545// use line intersection to guess a better split than 0.5
546// OPTIMIZE Once at_most_end_pts_in_common detects linear, mark span so all future splits are linear
caryclark1049f122015-04-20 08:31:59 -0700547template<typename TCurve, typename OppCurve>
548int SkTSpan<TCurve, OppCurve>::hullsIntersect(SkTSpan<OppCurve, TCurve>* opp,
549 bool* start, bool* oppStart) {
caryclark54359292015-03-26 07:52:43 -0700550 if (!fBounds.intersects(opp->fBounds)) {
551 return 0;
552 }
553 int hullSect = this->hullCheck(opp, start, oppStart);
554 if (hullSect >= 0) {
555 return hullSect;
556 }
557 hullSect = opp->hullCheck(this, oppStart, start);
558 if (hullSect >= 0) {
559 return hullSect;
560 }
561 return -1;
562}
563
caryclark1049f122015-04-20 08:31:59 -0700564template<typename TCurve, typename OppCurve>
565void SkTSpan<TCurve, OppCurve>::init(const TCurve& c) {
halcanary96fcdcc2015-08-27 07:41:13 -0700566 fPrev = fNext = nullptr;
reed0dc4dd62015-03-24 13:55:33 -0700567 fStartT = 0;
568 fEndT = 1;
halcanary96fcdcc2015-08-27 07:41:13 -0700569 fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -0700570 resetBounds(c);
caryclark45fa4472015-01-16 07:04:10 -0800571}
572
caryclark1049f122015-04-20 08:31:59 -0700573template<typename TCurve, typename OppCurve>
caryclarka35ab3e2016-10-20 08:32:18 -0700574bool SkTSpan<TCurve, OppCurve>::initBounds(const TCurve& c) {
reed0dc4dd62015-03-24 13:55:33 -0700575 fPart = c.subDivide(fStartT, fEndT);
576 fBounds.setBounds(fPart);
577 fCoinStart.init();
578 fCoinEnd.init();
579 fBoundsMax = SkTMax(fBounds.width(), fBounds.height());
580 fCollapsed = fPart.collapsed();
581 fHasPerp = false;
caryclark54359292015-03-26 07:52:43 -0700582 fDeleted = false;
reed0dc4dd62015-03-24 13:55:33 -0700583#if DEBUG_T_SECT
reed0dc4dd62015-03-24 13:55:33 -0700584 if (fCollapsed) {
585 SkDebugf(""); // for convenient breakpoints
caryclark45fa4472015-01-16 07:04:10 -0800586 }
587#endif
caryclarka35ab3e2016-10-20 08:32:18 -0700588 return fBounds.valid();
caryclark45fa4472015-01-16 07:04:10 -0800589}
590
caryclark1049f122015-04-20 08:31:59 -0700591template<typename TCurve, typename OppCurve>
592bool SkTSpan<TCurve, OppCurve>::linearsIntersect(SkTSpan<OppCurve, TCurve>* span) {
caryclark54359292015-03-26 07:52:43 -0700593 int result = this->linearIntersects(span->fPart);
594 if (result <= 1) {
595 return SkToBool(result);
caryclark45fa4472015-01-16 07:04:10 -0800596 }
caryclark54359292015-03-26 07:52:43 -0700597 SkASSERT(span->fIsLinear);
598 result = span->linearIntersects(this->fPart);
599// SkASSERT(result <= 1);
600 return SkToBool(result);
caryclark45fa4472015-01-16 07:04:10 -0800601}
602
caryclark1049f122015-04-20 08:31:59 -0700603template<typename TCurve, typename OppCurve>
604double SkTSpan<TCurve, OppCurve>::linearT(const SkDPoint& pt) const {
caryclark54359292015-03-26 07:52:43 -0700605 SkDVector len = fPart[TCurve::kPointLast] - fPart[0];
606 return fabs(len.fX) > fabs(len.fY)
607 ? (pt.fX - fPart[0].fX) / len.fX
608 : (pt.fY - fPart[0].fY) / len.fY;
caryclark45fa4472015-01-16 07:04:10 -0800609}
610
caryclark1049f122015-04-20 08:31:59 -0700611template<typename TCurve, typename OppCurve>
612int SkTSpan<TCurve, OppCurve>::linearIntersects(const OppCurve& q2) const {
caryclark45fa4472015-01-16 07:04:10 -0800613 // looks like q1 is near-linear
caryclark54359292015-03-26 07:52:43 -0700614 int start = 0, end = TCurve::kPointLast; // the outside points are usually the extremes
caryclark45fa4472015-01-16 07:04:10 -0800615 if (!fPart.controlsInside()) {
616 double dist = 0; // if there's any question, compute distance to find best outsiders
617 for (int outer = 0; outer < TCurve::kPointCount - 1; ++outer) {
618 for (int inner = outer + 1; inner < TCurve::kPointCount; ++inner) {
619 double test = (fPart[outer] - fPart[inner]).lengthSquared();
620 if (dist > test) {
621 continue;
622 }
623 dist = test;
624 start = outer;
625 end = inner;
626 }
627 }
628 }
629 // see if q2 is on one side of the line formed by the extreme points
630 double origX = fPart[start].fX;
631 double origY = fPart[start].fY;
632 double adj = fPart[end].fX - origX;
633 double opp = fPart[end].fY - origY;
caryclark54359292015-03-26 07:52:43 -0700634 double maxPart = SkTMax(fabs(adj), fabs(opp));
635 double sign = 0; // initialization to shut up warning in release build
caryclark1049f122015-04-20 08:31:59 -0700636 for (int n = 0; n < OppCurve::kPointCount; ++n) {
caryclark54359292015-03-26 07:52:43 -0700637 double dx = q2[n].fY - origY;
638 double dy = q2[n].fX - origX;
639 double maxVal = SkTMax(maxPart, SkTMax(fabs(dx), fabs(dy)));
caryclark45fa4472015-01-16 07:04:10 -0800640 double test = (q2[n].fY - origY) * adj - (q2[n].fX - origX) * opp;
caryclark54359292015-03-26 07:52:43 -0700641 if (precisely_zero_when_compared_to(test, maxVal)) {
642 return 1;
643 }
644 if (approximately_zero_when_compared_to(test, maxVal)) {
645 return 3;
caryclark45fa4472015-01-16 07:04:10 -0800646 }
647 if (n == 0) {
648 sign = test;
649 continue;
650 }
651 if (test * sign < 0) {
caryclark54359292015-03-26 07:52:43 -0700652 return 1;
caryclark45fa4472015-01-16 07:04:10 -0800653 }
654 }
caryclark54359292015-03-26 07:52:43 -0700655 return 0;
656}
657
caryclark1049f122015-04-20 08:31:59 -0700658template<typename TCurve, typename OppCurve>
659bool SkTSpan<TCurve, OppCurve>::onlyEndPointsInCommon(const SkTSpan<OppCurve, TCurve>* opp,
660 bool* start, bool* oppStart, bool* ptsInCommon) {
caryclark54359292015-03-26 07:52:43 -0700661 if (opp->fPart[0] == fPart[0]) {
662 *start = *oppStart = true;
663 } else if (opp->fPart[0] == fPart[TCurve::kPointLast]) {
664 *start = false;
665 *oppStart = true;
caryclark1049f122015-04-20 08:31:59 -0700666 } else if (opp->fPart[OppCurve::kPointLast] == fPart[0]) {
caryclark54359292015-03-26 07:52:43 -0700667 *start = true;
668 *oppStart = false;
caryclark1049f122015-04-20 08:31:59 -0700669 } else if (opp->fPart[OppCurve::kPointLast] == fPart[TCurve::kPointLast]) {
caryclark54359292015-03-26 07:52:43 -0700670 *start = *oppStart = false;
671 } else {
672 *ptsInCommon = false;
673 return false;
674 }
675 *ptsInCommon = true;
caryclark1049f122015-04-20 08:31:59 -0700676 const SkDPoint* otherPts[TCurve::kPointCount - 1], * oppOtherPts[OppCurve::kPointCount - 1];
caryclark54359292015-03-26 07:52:43 -0700677 int baseIndex = *start ? 0 : TCurve::kPointLast;
caryclark1049f122015-04-20 08:31:59 -0700678 fPart.otherPts(baseIndex, otherPts);
679 opp->fPart.otherPts(*oppStart ? 0 : OppCurve::kPointLast, oppOtherPts);
caryclark54359292015-03-26 07:52:43 -0700680 const SkDPoint& base = fPart[baseIndex];
caryclark1049f122015-04-20 08:31:59 -0700681 for (int o1 = 0; o1 < (int) SK_ARRAY_COUNT(otherPts); ++o1) {
682 SkDVector v1 = *otherPts[o1] - base;
683 for (int o2 = 0; o2 < (int) SK_ARRAY_COUNT(oppOtherPts); ++o2) {
684 SkDVector v2 = *oppOtherPts[o2] - base;
caryclark54359292015-03-26 07:52:43 -0700685 if (v2.dot(v1) >= 0) {
686 return false;
687 }
688 }
689 }
690 return true;
691}
692
caryclark1049f122015-04-20 08:31:59 -0700693template<typename TCurve, typename OppCurve>
694SkTSpan<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::oppT(double t) const {
695 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700696 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700697 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700698 if (between(test->fStartT, t, test->fEndT)) {
699 return test;
700 }
701 bounded = bounded->fNext;
702 }
halcanary96fcdcc2015-08-27 07:41:13 -0700703 return nullptr;
caryclark54359292015-03-26 07:52:43 -0700704}
705
caryclark1049f122015-04-20 08:31:59 -0700706template<typename TCurve, typename OppCurve>
707bool SkTSpan<TCurve, OppCurve>::removeAllBounded() {
caryclark54359292015-03-26 07:52:43 -0700708 bool deleteSpan = false;
caryclark1049f122015-04-20 08:31:59 -0700709 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700710 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700711 SkTSpan<OppCurve, TCurve>* opp = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700712 deleteSpan |= opp->removeBounded(this);
713 bounded = bounded->fNext;
714 }
715 return deleteSpan;
716}
717
caryclark1049f122015-04-20 08:31:59 -0700718template<typename TCurve, typename OppCurve>
719bool SkTSpan<TCurve, OppCurve>::removeBounded(const SkTSpan<OppCurve, TCurve>* opp) {
caryclark54359292015-03-26 07:52:43 -0700720 if (fHasPerp) {
721 bool foundStart = false;
722 bool foundEnd = false;
caryclark1049f122015-04-20 08:31:59 -0700723 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700724 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700725 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700726 if (opp != test) {
727 foundStart |= between(test->fStartT, fCoinStart.perpT(), test->fEndT);
728 foundEnd |= between(test->fStartT, fCoinEnd.perpT(), test->fEndT);
729 }
730 bounded = bounded->fNext;
731 }
732 if (!foundStart || !foundEnd) {
733 fHasPerp = false;
734 fCoinStart.init();
735 fCoinEnd.init();
736 }
737 }
caryclark1049f122015-04-20 08:31:59 -0700738 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
halcanary96fcdcc2015-08-27 07:41:13 -0700739 SkTSpanBounded<OppCurve, TCurve>* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -0700740 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700741 SkTSpanBounded<OppCurve, TCurve>* boundedNext = bounded->fNext;
caryclark54359292015-03-26 07:52:43 -0700742 if (opp == bounded->fBounded) {
743 if (prev) {
744 prev->fNext = boundedNext;
745 return false;
746 } else {
747 fBounded = boundedNext;
halcanary96fcdcc2015-08-27 07:41:13 -0700748 return fBounded == nullptr;
caryclark54359292015-03-26 07:52:43 -0700749 }
750 }
751 prev = bounded;
752 bounded = boundedNext;
753 }
caryclark643ede62016-08-08 14:27:45 -0700754 SkOPASSERT(0);
caryclark45fa4472015-01-16 07:04:10 -0800755 return false;
756}
757
caryclark1049f122015-04-20 08:31:59 -0700758template<typename TCurve, typename OppCurve>
759bool SkTSpan<TCurve, OppCurve>::splitAt(SkTSpan* work, double t, SkChunkAlloc* heap) {
caryclark45fa4472015-01-16 07:04:10 -0800760 fStartT = t;
761 fEndT = work->fEndT;
762 if (fStartT == fEndT) {
763 fCollapsed = true;
764 return false;
765 }
766 work->fEndT = t;
767 if (work->fStartT == work->fEndT) {
768 work->fCollapsed = true;
769 return false;
770 }
771 fPrev = work;
772 fNext = work->fNext;
773 fIsLinear = work->fIsLinear;
caryclark54359292015-03-26 07:52:43 -0700774 fIsLine = work->fIsLine;
775
caryclark45fa4472015-01-16 07:04:10 -0800776 work->fNext = this;
777 if (fNext) {
778 fNext->fPrev = this;
779 }
caryclark643ede62016-08-08 14:27:45 -0700780 this->validate();
caryclark1049f122015-04-20 08:31:59 -0700781 SkTSpanBounded<OppCurve, TCurve>* bounded = work->fBounded;
halcanary96fcdcc2015-08-27 07:41:13 -0700782 fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -0700783 while (bounded) {
784 this->addBounded(bounded->fBounded, heap);
785 bounded = bounded->fNext;
786 }
787 bounded = fBounded;
788 while (bounded) {
789 bounded->fBounded->addBounded(this, heap);
790 bounded = bounded->fNext;
caryclark45fa4472015-01-16 07:04:10 -0800791 }
792 return true;
793}
794
caryclark1049f122015-04-20 08:31:59 -0700795template<typename TCurve, typename OppCurve>
796void SkTSpan<TCurve, OppCurve>::validate() const {
caryclark643ede62016-08-08 14:27:45 -0700797#if DEBUG_VALIDATE
798 SkASSERT(this != fPrev);
799 SkASSERT(this != fNext);
halcanary96fcdcc2015-08-27 07:41:13 -0700800 SkASSERT(fNext == nullptr || fNext != fPrev);
801 SkASSERT(fNext == nullptr || this == fNext->fPrev);
802 SkASSERT(fPrev == nullptr || this == fPrev->fNext);
caryclark643ede62016-08-08 14:27:45 -0700803 this->validateBounded();
804#endif
805#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700806 SkASSERT(fBounds.width() || fBounds.height() || fCollapsed);
caryclarke839e782016-09-15 07:48:18 -0700807 SkASSERT(fBoundsMax == SkTMax(fBounds.width(), fBounds.height()) || fCollapsed == 0xFF);
caryclark45fa4472015-01-16 07:04:10 -0800808 SkASSERT(0 <= fStartT);
809 SkASSERT(fEndT <= 1);
caryclark54359292015-03-26 07:52:43 -0700810 SkASSERT(fStartT <= fEndT);
caryclarke839e782016-09-15 07:48:18 -0700811 SkASSERT(fBounded || fCollapsed == 0xFF);
caryclark54359292015-03-26 07:52:43 -0700812 if (fHasPerp) {
caryclark6c3b9cd2016-09-26 05:36:58 -0700813 if (fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700814 validatePerpT(fCoinStart.perpT());
815 validatePerpPt(fCoinStart.perpT(), fCoinStart.perpPt());
816 }
caryclark6c3b9cd2016-09-26 05:36:58 -0700817 if (fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700818 validatePerpT(fCoinEnd.perpT());
819 validatePerpPt(fCoinEnd.perpT(), fCoinEnd.perpPt());
820 }
caryclarkccec0f92015-03-24 07:28:17 -0700821 }
reed0dc4dd62015-03-24 13:55:33 -0700822#endif
caryclark54359292015-03-26 07:52:43 -0700823}
caryclarkccec0f92015-03-24 07:28:17 -0700824
caryclark1049f122015-04-20 08:31:59 -0700825template<typename TCurve, typename OppCurve>
826void SkTSpan<TCurve, OppCurve>::validateBounded() const {
caryclark54359292015-03-26 07:52:43 -0700827#if DEBUG_VALIDATE
caryclark1049f122015-04-20 08:31:59 -0700828 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700829 while (testBounded) {
csmartdaltonceeaa782016-08-10 10:07:57 -0700830 SkDEBUGCODE(const SkTSpan<OppCurve, TCurve>* overlap = testBounded->fBounded);
caryclark54359292015-03-26 07:52:43 -0700831 SkASSERT(!overlap->fDeleted);
caryclark643ede62016-08-08 14:27:45 -0700832#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700833 SkASSERT(((this->debugID() ^ overlap->debugID()) & 1) == 1);
834 SkASSERT(overlap->findOppSpan(this));
caryclark643ede62016-08-08 14:27:45 -0700835#endif
caryclark54359292015-03-26 07:52:43 -0700836 testBounded = testBounded->fNext;
837 }
838#endif
839}
840
caryclark1049f122015-04-20 08:31:59 -0700841template<typename TCurve, typename OppCurve>
842void SkTSpan<TCurve, OppCurve>::validatePerpT(double oppT) const {
843 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700844 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -0700845 const SkTSpan<OppCurve, TCurve>* overlap = testBounded->fBounded;
caryclark26ad22a2015-10-16 09:03:38 -0700846 if (precisely_between(overlap->fStartT, oppT, overlap->fEndT)) {
caryclark54359292015-03-26 07:52:43 -0700847 return;
848 }
849 testBounded = testBounded->fNext;
850 }
851 SkASSERT(0);
caryclark54359292015-03-26 07:52:43 -0700852}
853
caryclark1049f122015-04-20 08:31:59 -0700854template<typename TCurve, typename OppCurve>
855void SkTSpan<TCurve, OppCurve>::validatePerpPt(double t, const SkDPoint& pt) const {
856 SkASSERT(fDebugSect->fOppSect->fCurve.ptAtT(t) == pt);
caryclark54359292015-03-26 07:52:43 -0700857}
858
859
caryclark1049f122015-04-20 08:31:59 -0700860template<typename TCurve, typename OppCurve>
caryclarke25a4f62016-07-26 09:26:29 -0700861SkTSect<TCurve, OppCurve>::SkTSect(const TCurve& c
862 SkDEBUGPARAMS(SkOpGlobalState* debugGlobalState)
863 PATH_OPS_DEBUG_T_SECT_PARAMS(int id))
caryclark45fa4472015-01-16 07:04:10 -0800864 : fCurve(c)
caryclark1049f122015-04-20 08:31:59 -0700865 , fHeap(sizeof(SkTSpan<TCurve, OppCurve>) * 4)
halcanary96fcdcc2015-08-27 07:41:13 -0700866 , fCoincident(nullptr)
867 , fDeleted(nullptr)
caryclark45fa4472015-01-16 07:04:10 -0800868 , fActiveCount(0)
caryclarke25a4f62016-07-26 09:26:29 -0700869 SkDEBUGPARAMS(fDebugGlobalState(debugGlobalState))
caryclark54359292015-03-26 07:52:43 -0700870 PATH_OPS_DEBUG_T_SECT_PARAMS(fID(id))
871 PATH_OPS_DEBUG_T_SECT_PARAMS(fDebugCount(0))
872 PATH_OPS_DEBUG_T_SECT_PARAMS(fDebugAllocatedCount(0))
caryclark45fa4472015-01-16 07:04:10 -0800873{
caryclark34efb702016-10-24 08:19:06 -0700874 this->resetRemovedEnds();
875 fHead = this->addOne();
caryclark643ede62016-08-08 14:27:45 -0700876 SkDEBUGCODE(fHead->debugSetGlobalState(debugGlobalState));
caryclark45fa4472015-01-16 07:04:10 -0800877 fHead->init(c);
878}
879
caryclark1049f122015-04-20 08:31:59 -0700880template<typename TCurve, typename OppCurve>
881SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::addOne() {
882 SkTSpan<TCurve, OppCurve>* result;
caryclark45fa4472015-01-16 07:04:10 -0800883 if (fDeleted) {
884 result = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -0800885 fDeleted = result->fNext;
886 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700887 result = new (fHeap.allocThrow(sizeof(SkTSpan<TCurve, OppCurve>)))(
888 SkTSpan<TCurve, OppCurve>);
caryclark45fa4472015-01-16 07:04:10 -0800889#if DEBUG_T_SECT
890 ++fDebugAllocatedCount;
891#endif
892 }
caryclarked0935a2015-10-22 07:23:52 -0700893 result->reset();
caryclark08b32492015-04-06 11:41:29 -0700894 result->fHasPerp = false;
895 result->fDeleted = false;
caryclark55888e42016-07-18 10:01:36 -0700896 ++fActiveCount;
caryclark54359292015-03-26 07:52:43 -0700897 PATH_OPS_DEBUG_T_SECT_CODE(result->fID = fDebugCount++ * 2 + fID);
caryclark1049f122015-04-20 08:31:59 -0700898 SkDEBUGCODE(result->fDebugSect = this);
caryclarked0935a2015-10-22 07:23:52 -0700899#ifdef SK_DEBUG
900 result->fPart.debugInit();
901 result->fCoinStart.debugInit();
902 result->fCoinEnd.debugInit();
903 result->fPrev = result->fNext = nullptr;
904 result->fBounds.debugInit();
905 result->fStartT = result->fEndT = result->fBoundsMax = SK_ScalarNaN;
906 result->fCollapsed = result->fIsLinear = result->fIsLine = 0xFF;
907#endif
caryclark45fa4472015-01-16 07:04:10 -0800908 return result;
909}
910
caryclark1049f122015-04-20 08:31:59 -0700911template<typename TCurve, typename OppCurve>
912bool SkTSect<TCurve, OppCurve>::binarySearchCoin(SkTSect<OppCurve, TCurve>* sect2, double tStart,
913 double tStep, double* resultT, double* oppT) {
914 SkTSpan<TCurve, OppCurve> work;
caryclark45fa4472015-01-16 07:04:10 -0800915 double result = work.fStartT = work.fEndT = tStart;
caryclark1049f122015-04-20 08:31:59 -0700916 SkDEBUGCODE(work.fDebugSect = this);
caryclark45fa4472015-01-16 07:04:10 -0800917 SkDPoint last = fCurve.ptAtT(tStart);
918 SkDPoint oppPt;
919 bool flip = false;
caryclarkcdeff812016-07-22 03:34:19 -0700920 bool contained = false;
caryclark45fa4472015-01-16 07:04:10 -0800921 SkDEBUGCODE(bool down = tStep < 0);
caryclark1049f122015-04-20 08:31:59 -0700922 const OppCurve& opp = sect2->fCurve;
caryclark45fa4472015-01-16 07:04:10 -0800923 do {
924 tStep *= 0.5;
925 work.fStartT += tStep;
926 if (flip) {
927 tStep = -tStep;
928 flip = false;
929 }
930 work.initBounds(fCurve);
931 if (work.fCollapsed) {
932 return false;
933 }
934 if (last.approximatelyEqual(work.fPart[0])) {
935 break;
936 }
937 last = work.fPart[0];
938 work.fCoinStart.setPerp(fCurve, work.fStartT, last, opp);
caryclark6c3b9cd2016-09-26 05:36:58 -0700939 if (work.fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700940#if DEBUG_T_SECT
941 work.validatePerpPt(work.fCoinStart.perpT(), work.fCoinStart.perpPt());
942#endif
caryclark45fa4472015-01-16 07:04:10 -0800943 double oppTTest = work.fCoinStart.perpT();
caryclark54359292015-03-26 07:52:43 -0700944 if (sect2->fHead->contains(oppTTest)) {
caryclark45fa4472015-01-16 07:04:10 -0800945 *oppT = oppTTest;
946 oppPt = work.fCoinStart.perpPt();
caryclarkcdeff812016-07-22 03:34:19 -0700947 contained = true;
caryclark45fa4472015-01-16 07:04:10 -0800948 SkASSERT(down ? result > work.fStartT : result < work.fStartT);
949 result = work.fStartT;
950 continue;
951 }
952 }
953 tStep = -tStep;
954 flip = true;
955 } while (true);
caryclarkcdeff812016-07-22 03:34:19 -0700956 if (!contained) {
957 return false;
958 }
caryclark45fa4472015-01-16 07:04:10 -0800959 if (last.approximatelyEqual(fCurve[0])) {
960 result = 0;
961 } else if (last.approximatelyEqual(fCurve[TCurve::kPointLast])) {
962 result = 1;
963 }
964 if (oppPt.approximatelyEqual(opp[0])) {
965 *oppT = 0;
caryclark1049f122015-04-20 08:31:59 -0700966 } else if (oppPt.approximatelyEqual(opp[OppCurve::kPointLast])) {
caryclark45fa4472015-01-16 07:04:10 -0800967 *oppT = 1;
968 }
969 *resultT = result;
970 return true;
971}
972
973// OPTIMIZE ? keep a sorted list of sizes in the form of a doubly-linked list in quad span
974// so that each quad sect has a pointer to the largest, and can update it as spans
975// are split
caryclark1049f122015-04-20 08:31:59 -0700976template<typename TCurve, typename OppCurve>
977SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::boundsMax() const {
978 SkTSpan<TCurve, OppCurve>* test = fHead;
979 SkTSpan<TCurve, OppCurve>* largest = fHead;
caryclark54359292015-03-26 07:52:43 -0700980 bool lCollapsed = largest->fCollapsed;
caryclark45fa4472015-01-16 07:04:10 -0800981 while ((test = test->fNext)) {
caryclark54359292015-03-26 07:52:43 -0700982 bool tCollapsed = test->fCollapsed;
983 if ((lCollapsed && !tCollapsed) || (lCollapsed == tCollapsed &&
984 largest->fBoundsMax < test->fBoundsMax)) {
caryclark45fa4472015-01-16 07:04:10 -0800985 largest = test;
caryclark1049f122015-04-20 08:31:59 -0700986 lCollapsed = test->fCollapsed;
caryclark45fa4472015-01-16 07:04:10 -0800987 }
988 }
caryclark54359292015-03-26 07:52:43 -0700989 return largest;
caryclark45fa4472015-01-16 07:04:10 -0800990}
991
caryclark1049f122015-04-20 08:31:59 -0700992template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -0700993bool SkTSect<TCurve, OppCurve>::coincidentCheck(SkTSect<OppCurve, TCurve>* sect2) {
caryclark1049f122015-04-20 08:31:59 -0700994 SkTSpan<TCurve, OppCurve>* first = fHead;
caryclark9feb6322016-10-25 08:58:26 -0700995 if (!first) {
996 return false;
997 }
caryclark1049f122015-04-20 08:31:59 -0700998 SkTSpan<TCurve, OppCurve>* last, * next;
caryclark45fa4472015-01-16 07:04:10 -0800999 do {
caryclark54359292015-03-26 07:52:43 -07001000 int consecutive = this->countConsecutiveSpans(first, &last);
1001 next = last->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001002 if (consecutive < COINCIDENT_SPAN_COUNT) {
1003 continue;
1004 }
caryclark54359292015-03-26 07:52:43 -07001005 this->validate();
1006 sect2->validate();
1007 this->computePerpendiculars(sect2, first, last);
1008 this->validate();
1009 sect2->validate();
caryclark45fa4472015-01-16 07:04:10 -08001010 // check to see if a range of points are on the curve
caryclark1049f122015-04-20 08:31:59 -07001011 SkTSpan<TCurve, OppCurve>* coinStart = first;
caryclark54359292015-03-26 07:52:43 -07001012 do {
caryclarkef7cee42016-09-06 09:05:54 -07001013 bool success = this->extractCoincident(sect2, coinStart, last, &coinStart);
1014 if (!success) {
1015 return false;
1016 }
caryclark54359292015-03-26 07:52:43 -07001017 } while (coinStart && !last->fDeleted);
caryclark55888e42016-07-18 10:01:36 -07001018 if (!fHead || !sect2->fHead) {
1019 break;
1020 }
caryclark643ede62016-08-08 14:27:45 -07001021 if (!next || next->fDeleted) {
1022 break;
1023 }
caryclark45fa4472015-01-16 07:04:10 -08001024 } while ((first = next));
caryclarkef7cee42016-09-06 09:05:54 -07001025 return true;
caryclark45fa4472015-01-16 07:04:10 -08001026}
1027
caryclark1049f122015-04-20 08:31:59 -07001028template<typename TCurve, typename OppCurve>
caryclark26ad22a2015-10-16 09:03:38 -07001029void SkTSect<TCurve, OppCurve>::coincidentForce(SkTSect<OppCurve, TCurve>* sect2,
1030 double start1s, double start1e) {
1031 SkTSpan<TCurve, OppCurve>* first = fHead;
1032 SkTSpan<TCurve, OppCurve>* last = this->tail();
1033 SkTSpan<OppCurve, TCurve>* oppFirst = sect2->fHead;
1034 SkTSpan<OppCurve, TCurve>* oppLast = sect2->tail();
1035 bool deleteEmptySpans = this->updateBounded(first, last, oppFirst);
1036 deleteEmptySpans |= sect2->updateBounded(oppFirst, oppLast, first);
1037 this->removeSpanRange(first, last);
1038 sect2->removeSpanRange(oppFirst, oppLast);
1039 first->fStartT = start1s;
1040 first->fEndT = start1e;
1041 first->resetBounds(fCurve);
1042 first->fCoinStart.setPerp(fCurve, start1s, fCurve[0], sect2->fCurve);
1043 first->fCoinEnd.setPerp(fCurve, start1e, fCurve[TCurve::kPointLast], sect2->fCurve);
1044 bool oppMatched = first->fCoinStart.perpT() < first->fCoinEnd.perpT();
caryclarkef784fb2015-10-30 12:03:06 -07001045 double oppStartT = first->fCoinStart.perpT() == -1 ? 0 : SkTMax(0., first->fCoinStart.perpT());
1046 double oppEndT = first->fCoinEnd.perpT() == -1 ? 1 : SkTMin(1., first->fCoinEnd.perpT());
caryclark26ad22a2015-10-16 09:03:38 -07001047 if (!oppMatched) {
1048 SkTSwap(oppStartT, oppEndT);
1049 }
1050 oppFirst->fStartT = oppStartT;
1051 oppFirst->fEndT = oppEndT;
1052 oppFirst->resetBounds(sect2->fCurve);
1053 this->removeCoincident(first, false);
1054 sect2->removeCoincident(oppFirst, true);
1055 if (deleteEmptySpans) {
1056 this->deleteEmptySpans();
1057 sect2->deleteEmptySpans();
1058 }
1059}
1060
1061template<typename TCurve, typename OppCurve>
caryclark1049f122015-04-20 08:31:59 -07001062bool SkTSect<TCurve, OppCurve>::coincidentHasT(double t) {
1063 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001064 while (test) {
1065 if (between(test->fStartT, t, test->fEndT)) {
1066 return true;
1067 }
1068 test = test->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001069 }
caryclark54359292015-03-26 07:52:43 -07001070 return false;
caryclark45fa4472015-01-16 07:04:10 -08001071}
1072
caryclark1049f122015-04-20 08:31:59 -07001073template<typename TCurve, typename OppCurve>
1074int SkTSect<TCurve, OppCurve>::collapsed() const {
1075 int result = 0;
1076 const SkTSpan<TCurve, OppCurve>* test = fHead;
1077 while (test) {
1078 if (test->fCollapsed) {
1079 ++result;
1080 }
1081 test = test->next();
1082 }
1083 return result;
1084}
1085
1086template<typename TCurve, typename OppCurve>
1087void SkTSect<TCurve, OppCurve>::computePerpendiculars(SkTSect<OppCurve, TCurve>* sect2,
1088 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last) {
1089 const OppCurve& opp = sect2->fCurve;
1090 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001091 SkTSpan<TCurve, OppCurve>* prior = nullptr;
caryclark45fa4472015-01-16 07:04:10 -08001092 do {
caryclark54359292015-03-26 07:52:43 -07001093 if (!work->fHasPerp && !work->fCollapsed) {
1094 if (prior) {
1095 work->fCoinStart = prior->fCoinEnd;
1096 } else {
1097 work->fCoinStart.setPerp(fCurve, work->fStartT, work->fPart[0], opp);
caryclark45fa4472015-01-16 07:04:10 -08001098 }
caryclark6c3b9cd2016-09-26 05:36:58 -07001099 if (work->fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001100 double perpT = work->fCoinStart.perpT();
1101 if (sect2->coincidentHasT(perpT)) {
caryclarkdf386c52015-04-21 05:27:02 -07001102 work->fCoinStart.init();
caryclark54359292015-03-26 07:52:43 -07001103 } else {
1104 sect2->addForPerp(work, perpT);
1105 }
1106 }
1107 work->fCoinEnd.setPerp(fCurve, work->fEndT, work->fPart[TCurve::kPointLast], opp);
caryclark6c3b9cd2016-09-26 05:36:58 -07001108 if (work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001109 double perpT = work->fCoinEnd.perpT();
1110 if (sect2->coincidentHasT(perpT)) {
caryclarkdf386c52015-04-21 05:27:02 -07001111 work->fCoinEnd.init();
caryclark54359292015-03-26 07:52:43 -07001112 } else {
1113 sect2->addForPerp(work, perpT);
1114 }
1115 }
1116 work->fHasPerp = true;
caryclark45fa4472015-01-16 07:04:10 -08001117 }
1118 if (work == last) {
1119 break;
1120 }
caryclark54359292015-03-26 07:52:43 -07001121 prior = work;
caryclark45fa4472015-01-16 07:04:10 -08001122 work = work->fNext;
1123 SkASSERT(work);
1124 } while (true);
caryclark54359292015-03-26 07:52:43 -07001125}
1126
caryclark1049f122015-04-20 08:31:59 -07001127template<typename TCurve, typename OppCurve>
1128int SkTSect<TCurve, OppCurve>::countConsecutiveSpans(SkTSpan<TCurve, OppCurve>* first,
1129 SkTSpan<TCurve, OppCurve>** lastPtr) const {
caryclark54359292015-03-26 07:52:43 -07001130 int consecutive = 1;
caryclark1049f122015-04-20 08:31:59 -07001131 SkTSpan<TCurve, OppCurve>* last = first;
caryclark54359292015-03-26 07:52:43 -07001132 do {
caryclark1049f122015-04-20 08:31:59 -07001133 SkTSpan<TCurve, OppCurve>* next = last->fNext;
caryclark54359292015-03-26 07:52:43 -07001134 if (!next) {
1135 break;
1136 }
1137 if (next->fStartT > last->fEndT) {
1138 break;
1139 }
1140 ++consecutive;
1141 last = next;
1142 } while (true);
1143 *lastPtr = last;
1144 return consecutive;
1145}
1146
caryclark1049f122015-04-20 08:31:59 -07001147template<typename TCurve, typename OppCurve>
1148bool SkTSect<TCurve, OppCurve>::debugHasBounded(const SkTSpan<OppCurve, TCurve>* span) const {
1149 const SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001150 if (!test) {
1151 return false;
1152 }
1153 do {
1154 if (test->findOppSpan(span)) {
1155 return true;
1156 }
1157 } while ((test = test->next()));
1158 return false;
1159}
1160
caryclark1049f122015-04-20 08:31:59 -07001161template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001162bool SkTSect<TCurve, OppCurve>::deleteEmptySpans() {
caryclark1049f122015-04-20 08:31:59 -07001163 SkTSpan<TCurve, OppCurve>* test;
1164 SkTSpan<TCurve, OppCurve>* next = fHead;
caryclark54359292015-03-26 07:52:43 -07001165 while ((test = next)) {
1166 next = test->fNext;
1167 if (!test->fBounded) {
caryclarkef7cee42016-09-06 09:05:54 -07001168 if (!this->removeSpan(test)) {
1169 return false;
1170 }
caryclark54359292015-03-26 07:52:43 -07001171 }
1172 }
caryclarkef7cee42016-09-06 09:05:54 -07001173 return true;
caryclark54359292015-03-26 07:52:43 -07001174}
1175
caryclark1049f122015-04-20 08:31:59 -07001176template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001177bool SkTSect<TCurve, OppCurve>::extractCoincident(
caryclark1049f122015-04-20 08:31:59 -07001178 SkTSect<OppCurve, TCurve>* sect2,
caryclarkef7cee42016-09-06 09:05:54 -07001179 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last,
1180 SkTSpan<TCurve, OppCurve>** result) {
caryclark1049f122015-04-20 08:31:59 -07001181 first = findCoincidentRun(first, &last);
caryclarka1b42d92016-08-16 10:25:29 -07001182 if (!first || !last) {
caryclarkef7cee42016-09-06 09:05:54 -07001183 *result = nullptr;
1184 return true;
caryclark45fa4472015-01-16 07:04:10 -08001185 }
1186 // march outwards to find limit of coincidence from here to previous and next spans
1187 double startT = first->fStartT;
caryclarkd8bc16b2015-03-26 09:05:12 -07001188 double oppStartT SK_INIT_TO_AVOID_WARNING;
caryclark54359292015-03-26 07:52:43 -07001189 double oppEndT SK_INIT_TO_AVOID_WARNING;
caryclark1049f122015-04-20 08:31:59 -07001190 SkTSpan<TCurve, OppCurve>* prev = first->fPrev;
caryclark6c3b9cd2016-09-26 05:36:58 -07001191 SkASSERT(first->fCoinStart.isMatch());
caryclark1049f122015-04-20 08:31:59 -07001192 SkTSpan<OppCurve, TCurve>* oppFirst = first->findOppT(first->fCoinStart.perpT());
caryclark6c3b9cd2016-09-26 05:36:58 -07001193 SkOPASSERT(last->fCoinEnd.isMatch());
caryclark54359292015-03-26 07:52:43 -07001194 bool oppMatched = first->fCoinStart.perpT() < first->fCoinEnd.perpT();
1195 double coinStart;
1196 SkDEBUGCODE(double coinEnd);
caryclark1049f122015-04-20 08:31:59 -07001197 SkTSpan<OppCurve, TCurve>* cutFirst;
caryclark54359292015-03-26 07:52:43 -07001198 if (prev && prev->fEndT == startT
1199 && this->binarySearchCoin(sect2, startT, prev->fStartT - startT, &coinStart,
1200 &oppStartT)
caryclark1049f122015-04-20 08:31:59 -07001201 && prev->fStartT < coinStart && coinStart < startT
1202 && (cutFirst = prev->oppT(oppStartT))) {
1203 oppFirst = cutFirst;
caryclark54359292015-03-26 07:52:43 -07001204 first = this->addSplitAt(prev, coinStart);
1205 first->markCoincident();
1206 prev->fCoinEnd.markCoincident();
1207 if (oppFirst->fStartT < oppStartT && oppStartT < oppFirst->fEndT) {
caryclark1049f122015-04-20 08:31:59 -07001208 SkTSpan<OppCurve, TCurve>* oppHalf = sect2->addSplitAt(oppFirst, oppStartT);
caryclark54359292015-03-26 07:52:43 -07001209 if (oppMatched) {
1210 oppFirst->fCoinEnd.markCoincident();
1211 oppHalf->markCoincident();
1212 oppFirst = oppHalf;
1213 } else {
1214 oppFirst->markCoincident();
1215 oppHalf->fCoinStart.markCoincident();
1216 }
1217 }
1218 } else {
1219 SkDEBUGCODE(coinStart = first->fStartT);
caryclarka35ab3e2016-10-20 08:32:18 -07001220 FAIL_IF(!oppFirst);
caryclark54359292015-03-26 07:52:43 -07001221 SkDEBUGCODE(oppStartT = oppMatched ? oppFirst->fStartT : oppFirst->fEndT);
1222 }
caryclark1049f122015-04-20 08:31:59 -07001223 // FIXME: incomplete : if we're not at the end, find end of coin
1224 SkTSpan<OppCurve, TCurve>* oppLast;
caryclark6c3b9cd2016-09-26 05:36:58 -07001225 SkOPASSERT(last->fCoinEnd.isMatch());
caryclark54359292015-03-26 07:52:43 -07001226 oppLast = last->findOppT(last->fCoinEnd.perpT());
1227 SkDEBUGCODE(coinEnd = last->fEndT);
caryclark643ede62016-08-08 14:27:45 -07001228#ifdef SK_DEBUG
1229 if (!this->globalState() || !this->globalState()->debugSkipAssert()) {
1230 oppEndT = oppMatched ? oppLast->fEndT : oppLast->fStartT;
1231 }
1232#endif
caryclark54359292015-03-26 07:52:43 -07001233 if (!oppMatched) {
1234 SkTSwap(oppFirst, oppLast);
1235 SkTSwap(oppStartT, oppEndT);
1236 }
caryclarke25a4f62016-07-26 09:26:29 -07001237 SkOPASSERT(oppStartT < oppEndT);
caryclark54359292015-03-26 07:52:43 -07001238 SkASSERT(coinStart == first->fStartT);
1239 SkASSERT(coinEnd == last->fEndT);
caryclark643ede62016-08-08 14:27:45 -07001240 SkOPASSERT(oppStartT == oppFirst->fStartT);
1241 SkOPASSERT(oppEndT == oppLast->fEndT);
1242 if (!oppFirst) {
caryclarkef7cee42016-09-06 09:05:54 -07001243 *result = nullptr;
1244 return true;
caryclark643ede62016-08-08 14:27:45 -07001245 }
caryclark42942862016-08-19 07:01:33 -07001246 if (!oppLast) {
caryclarkef7cee42016-09-06 09:05:54 -07001247 *result = nullptr;
1248 return true;
caryclark42942862016-08-19 07:01:33 -07001249 }
caryclark54359292015-03-26 07:52:43 -07001250 // reduce coincident runs to single entries
1251 this->validate();
1252 sect2->validate();
caryclark1049f122015-04-20 08:31:59 -07001253 bool deleteEmptySpans = this->updateBounded(first, last, oppFirst);
1254 deleteEmptySpans |= sect2->updateBounded(oppFirst, oppLast, first);
caryclark54359292015-03-26 07:52:43 -07001255 this->removeSpanRange(first, last);
1256 sect2->removeSpanRange(oppFirst, oppLast);
1257 first->fEndT = last->fEndT;
1258 first->resetBounds(this->fCurve);
1259 first->fCoinStart.setPerp(fCurve, first->fStartT, first->fPart[0], sect2->fCurve);
1260 first->fCoinEnd.setPerp(fCurve, first->fEndT, first->fPart[TCurve::kPointLast], sect2->fCurve);
1261 oppStartT = first->fCoinStart.perpT();
1262 oppEndT = first->fCoinEnd.perpT();
1263 if (between(0, oppStartT, 1) && between(0, oppEndT, 1)) {
1264 if (!oppMatched) {
1265 SkTSwap(oppStartT, oppEndT);
1266 }
1267 oppFirst->fStartT = oppStartT;
1268 oppFirst->fEndT = oppEndT;
1269 oppFirst->resetBounds(sect2->fCurve);
1270 }
1271 this->validateBounded();
1272 sect2->validateBounded();
1273 last = first->fNext;
1274 this->removeCoincident(first, false);
1275 sect2->removeCoincident(oppFirst, true);
caryclark1049f122015-04-20 08:31:59 -07001276 if (deleteEmptySpans) {
caryclarkef7cee42016-09-06 09:05:54 -07001277 if (!this->deleteEmptySpans() || !sect2->deleteEmptySpans()) {
1278 *result = nullptr;
1279 return false;
1280 }
caryclark54359292015-03-26 07:52:43 -07001281 }
1282 this->validate();
1283 sect2->validate();
caryclarkef7cee42016-09-06 09:05:54 -07001284 *result = last && !last->fDeleted && fHead && sect2->fHead ? last : nullptr;
1285 return true;
caryclark54359292015-03-26 07:52:43 -07001286}
1287
caryclark1049f122015-04-20 08:31:59 -07001288template<typename TCurve, typename OppCurve>
1289SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::findCoincidentRun(
1290 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>** lastPtr) {
1291 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001292 SkTSpan<TCurve, OppCurve>* lastCandidate = nullptr;
1293 first = nullptr;
caryclark54359292015-03-26 07:52:43 -07001294 // find the first fully coincident span
1295 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07001296 if (work->fCoinStart.isMatch()) {
caryclark1049f122015-04-20 08:31:59 -07001297#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -07001298 work->validatePerpT(work->fCoinStart.perpT());
1299 work->validatePerpPt(work->fCoinStart.perpT(), work->fCoinStart.perpPt());
caryclark1049f122015-04-20 08:31:59 -07001300#endif
caryclarka35ab3e2016-10-20 08:32:18 -07001301 SkOPASSERT(work->hasOppT(work->fCoinStart.perpT()));
caryclark6c3b9cd2016-09-26 05:36:58 -07001302 if (!work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001303 break;
1304 }
1305 lastCandidate = work;
1306 if (!first) {
1307 first = work;
1308 }
caryclark1049f122015-04-20 08:31:59 -07001309 } else if (first && work->fCollapsed) {
1310 *lastPtr = lastCandidate;
1311 return first;
caryclark54359292015-03-26 07:52:43 -07001312 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001313 lastCandidate = nullptr;
caryclark643ede62016-08-08 14:27:45 -07001314 SkOPASSERT(!first);
caryclark54359292015-03-26 07:52:43 -07001315 }
1316 if (work == *lastPtr) {
1317 return first;
1318 }
1319 work = work->fNext;
caryclarke25a4f62016-07-26 09:26:29 -07001320 if (!work) {
1321 return nullptr;
1322 }
caryclark54359292015-03-26 07:52:43 -07001323 } while (true);
1324 if (lastCandidate) {
1325 *lastPtr = lastCandidate;
1326 }
1327 return first;
1328}
1329
caryclark1049f122015-04-20 08:31:59 -07001330template<typename TCurve, typename OppCurve>
1331int SkTSect<TCurve, OppCurve>::intersects(SkTSpan<TCurve, OppCurve>* span,
1332 SkTSect<OppCurve, TCurve>* opp,
1333 SkTSpan<OppCurve, TCurve>* oppSpan, int* oppResult) {
caryclark54359292015-03-26 07:52:43 -07001334 bool spanStart, oppStart;
1335 int hullResult = span->hullsIntersect(oppSpan, &spanStart, &oppStart);
1336 if (hullResult >= 0) {
1337 if (hullResult == 2) { // hulls have one point in common
1338 if (!span->fBounded || !span->fBounded->fNext) {
1339 SkASSERT(!span->fBounded || span->fBounded->fBounded == oppSpan);
1340 if (spanStart) {
1341 span->fEndT = span->fStartT;
caryclark45fa4472015-01-16 07:04:10 -08001342 } else {
caryclark54359292015-03-26 07:52:43 -07001343 span->fStartT = span->fEndT;
1344 }
1345 } else {
1346 hullResult = 1;
1347 }
1348 if (!oppSpan->fBounded || !oppSpan->fBounded->fNext) {
1349 SkASSERT(!oppSpan->fBounded || oppSpan->fBounded->fBounded == span);
1350 if (oppStart) {
1351 oppSpan->fEndT = oppSpan->fStartT;
1352 } else {
1353 oppSpan->fStartT = oppSpan->fEndT;
1354 }
1355 *oppResult = 2;
1356 } else {
1357 *oppResult = 1;
1358 }
1359 } else {
1360 *oppResult = 1;
1361 }
1362 return hullResult;
1363 }
1364 if (span->fIsLine && oppSpan->fIsLine) {
1365 SkIntersections i;
1366 int sects = this->linesIntersect(span, opp, oppSpan, &i);
caryclark26ad22a2015-10-16 09:03:38 -07001367 if (sects == 2) {
1368 return *oppResult = 1;
1369 }
caryclark54359292015-03-26 07:52:43 -07001370 if (!sects) {
1371 return -1;
1372 }
caryclark34efb702016-10-24 08:19:06 -07001373 this->removedEndCheck(span);
caryclark54359292015-03-26 07:52:43 -07001374 span->fStartT = span->fEndT = i[0][0];
caryclark34efb702016-10-24 08:19:06 -07001375 opp->removedEndCheck(oppSpan);
caryclark54359292015-03-26 07:52:43 -07001376 oppSpan->fStartT = oppSpan->fEndT = i[1][0];
1377 return *oppResult = 2;
1378 }
1379 if (span->fIsLinear || oppSpan->fIsLinear) {
1380 return *oppResult = (int) span->linearsIntersect(oppSpan);
1381 }
1382 return *oppResult = 1;
1383}
1384
caryclarked0935a2015-10-22 07:23:52 -07001385template<typename TCurve>
1386static bool is_parallel(const SkDLine& thisLine, const TCurve& opp) {
1387 if (!opp.IsConic()) {
1388 return false; // FIXME : breaks a lot of stuff now
1389 }
1390 int finds = 0;
1391 SkDLine thisPerp;
1392 thisPerp.fPts[0].fX = thisLine.fPts[1].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1393 thisPerp.fPts[0].fY = thisLine.fPts[1].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1394 thisPerp.fPts[1] = thisLine.fPts[1];
1395 SkIntersections perpRayI;
1396 perpRayI.intersectRay(opp, thisPerp);
1397 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1398 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[1]);
1399 }
1400 thisPerp.fPts[1].fX = thisLine.fPts[0].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1401 thisPerp.fPts[1].fY = thisLine.fPts[0].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1402 thisPerp.fPts[0] = thisLine.fPts[0];
1403 perpRayI.intersectRay(opp, thisPerp);
1404 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1405 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[0]);
1406 }
1407 return finds >= 2;
1408}
1409
caryclark54359292015-03-26 07:52:43 -07001410// while the intersection points are sufficiently far apart:
1411// construct the tangent lines from the intersections
1412// find the point where the tangent line intersects the opposite curve
caryclark1049f122015-04-20 08:31:59 -07001413template<typename TCurve, typename OppCurve>
1414int SkTSect<TCurve, OppCurve>::linesIntersect(SkTSpan<TCurve, OppCurve>* span,
1415 SkTSect<OppCurve, TCurve>* opp,
1416 SkTSpan<OppCurve, TCurve>* oppSpan, SkIntersections* i) {
caryclarka35ab3e2016-10-20 08:32:18 -07001417 SkIntersections thisRayI SkDEBUGCODE((span->fDebugGlobalState));
1418 SkIntersections oppRayI SkDEBUGCODE((span->fDebugGlobalState));
caryclark54359292015-03-26 07:52:43 -07001419 SkDLine thisLine = {{ span->fPart[0], span->fPart[TCurve::kPointLast] }};
caryclark1049f122015-04-20 08:31:59 -07001420 SkDLine oppLine = {{ oppSpan->fPart[0], oppSpan->fPart[OppCurve::kPointLast] }};
caryclark54359292015-03-26 07:52:43 -07001421 int loopCount = 0;
1422 double bestDistSq = DBL_MAX;
caryclark1049f122015-04-20 08:31:59 -07001423 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1424 return 0;
1425 }
1426 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1427 return 0;
1428 }
caryclark26ad22a2015-10-16 09:03:38 -07001429 // if the ends of each line intersect the opposite curve, the lines are coincident
1430 if (thisRayI.used() > 1) {
1431 int ptMatches = 0;
1432 for (int tIndex = 0; tIndex < thisRayI.used(); ++tIndex) {
1433 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(thisLine.fPts); ++lIndex) {
1434 ptMatches += thisRayI.pt(tIndex).approximatelyEqual(thisLine.fPts[lIndex]);
1435 }
1436 }
caryclarked0935a2015-10-22 07:23:52 -07001437 if (ptMatches == 2 || is_parallel(thisLine, opp->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001438 return 2;
1439 }
1440 }
1441 if (oppRayI.used() > 1) {
1442 int ptMatches = 0;
1443 for (int oIndex = 0; oIndex < oppRayI.used(); ++oIndex) {
1444 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(thisLine.fPts); ++lIndex) {
1445 ptMatches += oppRayI.pt(oIndex).approximatelyEqual(oppLine.fPts[lIndex]);
1446 }
1447 }
caryclarked0935a2015-10-22 07:23:52 -07001448 if (ptMatches == 2|| is_parallel(oppLine, this->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001449 return 2;
1450 }
1451 }
caryclark54359292015-03-26 07:52:43 -07001452 do {
caryclark54359292015-03-26 07:52:43 -07001453 // pick the closest pair of points
1454 double closest = DBL_MAX;
1455 int closeIndex SK_INIT_TO_AVOID_WARNING;
1456 int oppCloseIndex SK_INIT_TO_AVOID_WARNING;
1457 for (int index = 0; index < oppRayI.used(); ++index) {
1458 if (!roughly_between(span->fStartT, oppRayI[0][index], span->fEndT)) {
1459 continue;
1460 }
1461 for (int oIndex = 0; oIndex < thisRayI.used(); ++oIndex) {
1462 if (!roughly_between(oppSpan->fStartT, thisRayI[0][oIndex], oppSpan->fEndT)) {
1463 continue;
1464 }
1465 double distSq = thisRayI.pt(index).distanceSquared(oppRayI.pt(oIndex));
1466 if (closest > distSq) {
1467 closest = distSq;
1468 closeIndex = index;
1469 oppCloseIndex = oIndex;
caryclarkccec0f92015-03-24 07:28:17 -07001470 }
caryclarkccec0f92015-03-24 07:28:17 -07001471 }
reed0dc4dd62015-03-24 13:55:33 -07001472 }
caryclark54359292015-03-26 07:52:43 -07001473 if (closest == DBL_MAX) {
caryclark1049f122015-04-20 08:31:59 -07001474 break;
reed0dc4dd62015-03-24 13:55:33 -07001475 }
caryclark54359292015-03-26 07:52:43 -07001476 const SkDPoint& oppIPt = thisRayI.pt(oppCloseIndex);
1477 const SkDPoint& iPt = oppRayI.pt(closeIndex);
1478 if (between(span->fStartT, oppRayI[0][closeIndex], span->fEndT)
1479 && between(oppSpan->fStartT, thisRayI[0][oppCloseIndex], oppSpan->fEndT)
1480 && oppIPt.approximatelyEqual(iPt)) {
1481 i->merge(oppRayI, closeIndex, thisRayI, oppCloseIndex);
1482 return i->used();
1483 }
1484 double distSq = oppIPt.distanceSquared(iPt);
1485 if (bestDistSq < distSq || ++loopCount > 5) {
caryclark1049f122015-04-20 08:31:59 -07001486 return 0;
caryclark54359292015-03-26 07:52:43 -07001487 }
1488 bestDistSq = distSq;
caryclark1049f122015-04-20 08:31:59 -07001489 double oppStart = oppRayI[0][closeIndex];
1490 thisLine[0] = fCurve.ptAtT(oppStart);
1491 thisLine[1] = thisLine[0] + fCurve.dxdyAtT(oppStart);
1492 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1493 break;
1494 }
1495 double start = thisRayI[0][oppCloseIndex];
1496 oppLine[0] = opp->fCurve.ptAtT(start);
1497 oppLine[1] = oppLine[0] + opp->fCurve.dxdyAtT(start);
1498 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1499 break;
1500 }
caryclark54359292015-03-26 07:52:43 -07001501 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001502 // convergence may fail if the curves are nearly coincident
1503 SkTCoincident<OppCurve, TCurve> oCoinS, oCoinE;
1504 oCoinS.setPerp(opp->fCurve, oppSpan->fStartT, oppSpan->fPart[0], fCurve);
1505 oCoinE.setPerp(opp->fCurve, oppSpan->fEndT, oppSpan->fPart[OppCurve::kPointLast], fCurve);
1506 double tStart = oCoinS.perpT();
1507 double tEnd = oCoinE.perpT();
1508 bool swap = tStart > tEnd;
1509 if (swap) {
1510 SkTSwap(tStart, tEnd);
1511 }
1512 tStart = SkTMax(tStart, span->fStartT);
1513 tEnd = SkTMin(tEnd, span->fEndT);
1514 if (tStart > tEnd) {
1515 return 0;
1516 }
1517 SkDVector perpS, perpE;
1518 if (tStart == span->fStartT) {
1519 SkTCoincident<TCurve, OppCurve> coinS;
1520 coinS.setPerp(fCurve, span->fStartT, span->fPart[0], opp->fCurve);
1521 perpS = span->fPart[0] - coinS.perpPt();
1522 } else if (swap) {
1523 perpS = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1524 } else {
1525 perpS = oCoinS.perpPt() - oppSpan->fPart[0];
1526 }
1527 if (tEnd == span->fEndT) {
1528 SkTCoincident<TCurve, OppCurve> coinE;
1529 coinE.setPerp(fCurve, span->fEndT, span->fPart[TCurve::kPointLast], opp->fCurve);
1530 perpE = span->fPart[TCurve::kPointLast] - coinE.perpPt();
1531 } else if (swap) {
1532 perpE = oCoinS.perpPt() - oppSpan->fPart[0];
1533 } else {
1534 perpE = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1535 }
1536 if (perpS.dot(perpE) >= 0) {
1537 return 0;
1538 }
1539 SkTCoincident<TCurve, OppCurve> coinW;
1540 double workT = tStart;
1541 double tStep = tEnd - tStart;
1542 SkDPoint workPt;
1543 do {
1544 tStep *= 0.5;
1545 if (precisely_zero(tStep)) {
1546 return 0;
1547 }
1548 workT += tStep;
1549 workPt = fCurve.ptAtT(workT);
1550 coinW.setPerp(fCurve, workT, workPt, opp->fCurve);
caryclark27c015d2016-09-23 05:47:20 -07001551 double perpT = coinW.perpT();
caryclark6c3b9cd2016-09-26 05:36:58 -07001552 if (coinW.isMatch() ? !between(oppSpan->fStartT, perpT, oppSpan->fEndT) : perpT < 0) {
caryclarkb6693002015-12-16 12:28:35 -08001553 continue;
1554 }
caryclark1049f122015-04-20 08:31:59 -07001555 SkDVector perpW = workPt - coinW.perpPt();
1556 if ((perpS.dot(perpW) >= 0) == (tStep < 0)) {
1557 tStep = -tStep;
1558 }
caryclarkb6693002015-12-16 12:28:35 -08001559 if (workPt.approximatelyEqual(coinW.perpPt())) {
1560 break;
1561 }
1562 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001563 double oppTTest = coinW.perpT();
1564 if (!opp->fHead->contains(oppTTest)) {
1565 return 0;
1566 }
1567 i->setMax(1);
1568 i->insert(workT, oppTTest, workPt);
1569 return 1;
caryclark54359292015-03-26 07:52:43 -07001570}
1571
1572
caryclark1049f122015-04-20 08:31:59 -07001573template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001574bool SkTSect<TCurve, OppCurve>::markSpanGone(SkTSpan<TCurve, OppCurve>* span) {
1575 if (--fActiveCount < 0) {
1576 return false;
1577 }
caryclark54359292015-03-26 07:52:43 -07001578 span->fNext = fDeleted;
1579 fDeleted = span;
caryclarke25a4f62016-07-26 09:26:29 -07001580 SkOPASSERT(!span->fDeleted);
caryclark54359292015-03-26 07:52:43 -07001581 span->fDeleted = true;
caryclarkef7cee42016-09-06 09:05:54 -07001582 return true;
caryclark54359292015-03-26 07:52:43 -07001583}
1584
caryclark1049f122015-04-20 08:31:59 -07001585template<typename TCurve, typename OppCurve>
1586bool SkTSect<TCurve, OppCurve>::matchedDirection(double t, const SkTSect<OppCurve, TCurve>* sect2,
1587 double t2) const {
caryclark54359292015-03-26 07:52:43 -07001588 SkDVector dxdy = this->fCurve.dxdyAtT(t);
1589 SkDVector dxdy2 = sect2->fCurve.dxdyAtT(t2);
1590 return dxdy.dot(dxdy2) >= 0;
1591}
1592
caryclark1049f122015-04-20 08:31:59 -07001593template<typename TCurve, typename OppCurve>
1594void SkTSect<TCurve, OppCurve>::matchedDirCheck(double t, const SkTSect<OppCurve, TCurve>* sect2,
1595 double t2, bool* calcMatched, bool* oppMatched) const {
caryclark54359292015-03-26 07:52:43 -07001596 if (*calcMatched) {
caryclark55888e42016-07-18 10:01:36 -07001597 SkASSERT(*oppMatched == this->matchedDirection(t, sect2, t2));
caryclark54359292015-03-26 07:52:43 -07001598 } else {
1599 *oppMatched = this->matchedDirection(t, sect2, t2);
1600 *calcMatched = true;
1601 }
1602}
1603
caryclark1049f122015-04-20 08:31:59 -07001604template<typename TCurve, typename OppCurve>
1605void SkTSect<TCurve, OppCurve>::mergeCoincidence(SkTSect<OppCurve, TCurve>* sect2) {
caryclark54359292015-03-26 07:52:43 -07001606 double smallLimit = 0;
1607 do {
1608 // find the smallest unprocessed span
halcanary96fcdcc2015-08-27 07:41:13 -07001609 SkTSpan<TCurve, OppCurve>* smaller = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001610 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001611 do {
caryclark221a4bb2016-10-07 11:15:15 -07001612 if (!test) {
1613 return;
1614 }
caryclark54359292015-03-26 07:52:43 -07001615 if (test->fStartT < smallLimit) {
1616 continue;
1617 }
1618 if (smaller && smaller->fEndT < test->fStartT) {
1619 continue;
1620 }
1621 smaller = test;
1622 } while ((test = test->fNext));
1623 if (!smaller) {
1624 return;
1625 }
1626 smallLimit = smaller->fEndT;
1627 // find next larger span
halcanary96fcdcc2015-08-27 07:41:13 -07001628 SkTSpan<TCurve, OppCurve>* prior = nullptr;
1629 SkTSpan<TCurve, OppCurve>* larger = nullptr;
1630 SkTSpan<TCurve, OppCurve>* largerPrior = nullptr;
caryclark54359292015-03-26 07:52:43 -07001631 test = fCoincident;
1632 do {
1633 if (test->fStartT < smaller->fEndT) {
1634 continue;
1635 }
caryclark221a4bb2016-10-07 11:15:15 -07001636 SkOPASSERT(test->fStartT != smaller->fEndT);
caryclark54359292015-03-26 07:52:43 -07001637 if (larger && larger->fStartT < test->fStartT) {
1638 continue;
1639 }
1640 largerPrior = prior;
1641 larger = test;
1642 } while ((prior = test), (test = test->fNext));
1643 if (!larger) {
1644 continue;
1645 }
1646 // check middle t value to see if it is coincident as well
1647 double midT = (smaller->fEndT + larger->fStartT) / 2;
1648 SkDPoint midPt = fCurve.ptAtT(midT);
caryclark1049f122015-04-20 08:31:59 -07001649 SkTCoincident<TCurve, OppCurve> coin;
caryclark54359292015-03-26 07:52:43 -07001650 coin.setPerp(fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07001651 if (coin.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001652 smaller->fEndT = larger->fEndT;
1653 smaller->fCoinEnd = larger->fCoinEnd;
1654 if (largerPrior) {
1655 largerPrior->fNext = larger->fNext;
caryclark643ede62016-08-08 14:27:45 -07001656 largerPrior->validate();
caryclark54359292015-03-26 07:52:43 -07001657 } else {
1658 fCoincident = larger->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001659 }
1660 }
caryclark54359292015-03-26 07:52:43 -07001661 } while (true);
1662}
1663
caryclark1049f122015-04-20 08:31:59 -07001664template<typename TCurve, typename OppCurve>
1665SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::prev(
1666 SkTSpan<TCurve, OppCurve>* span) const {
halcanary96fcdcc2015-08-27 07:41:13 -07001667 SkTSpan<TCurve, OppCurve>* result = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001668 SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001669 while (span != test) {
1670 result = test;
1671 test = test->fNext;
1672 SkASSERT(test);
caryclarkccec0f92015-03-24 07:28:17 -07001673 }
caryclark55888e42016-07-18 10:01:36 -07001674 return result;
caryclarkccec0f92015-03-24 07:28:17 -07001675}
1676
caryclark1049f122015-04-20 08:31:59 -07001677template<typename TCurve, typename OppCurve>
1678void SkTSect<TCurve, OppCurve>::recoverCollapsed() {
1679 SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001680 while (deleted) {
caryclark1049f122015-04-20 08:31:59 -07001681 SkTSpan<TCurve, OppCurve>* delNext = deleted->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001682 if (deleted->fCollapsed) {
caryclark1049f122015-04-20 08:31:59 -07001683 SkTSpan<TCurve, OppCurve>** spanPtr = &fHead;
caryclark45fa4472015-01-16 07:04:10 -08001684 while (*spanPtr && (*spanPtr)->fEndT <= deleted->fStartT) {
1685 spanPtr = &(*spanPtr)->fNext;
1686 }
1687 deleted->fNext = *spanPtr;
1688 *spanPtr = deleted;
1689 }
1690 deleted = delNext;
1691 }
1692}
1693
caryclark1049f122015-04-20 08:31:59 -07001694template<typename TCurve, typename OppCurve>
1695void SkTSect<TCurve, OppCurve>::removeAllBut(const SkTSpan<OppCurve, TCurve>* keep,
1696 SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp) {
1697 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001698 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001699 SkTSpan<OppCurve, TCurve>* bounded = testBounded->fBounded;
1700 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001701 // may have been deleted when opp did 'remove all but'
1702 if (bounded != keep && !bounded->fDeleted) {
1703 SkAssertResult(SkDEBUGCODE(!) span->removeBounded(bounded));
1704 if (bounded->removeBounded(span)) {
1705 opp->removeSpan(bounded);
1706 }
caryclarkccec0f92015-03-24 07:28:17 -07001707 }
caryclark54359292015-03-26 07:52:43 -07001708 testBounded = next;
caryclarkccec0f92015-03-24 07:28:17 -07001709 }
caryclark54359292015-03-26 07:52:43 -07001710 SkASSERT(!span->fDeleted);
1711 SkASSERT(span->findOppSpan(keep));
1712 SkASSERT(keep->findOppSpan(span));
caryclarkccec0f92015-03-24 07:28:17 -07001713}
1714
caryclark1049f122015-04-20 08:31:59 -07001715template<typename TCurve, typename OppCurve>
1716void SkTSect<TCurve, OppCurve>::removeByPerpendicular(SkTSect<OppCurve, TCurve>* opp) {
1717 SkTSpan<TCurve, OppCurve>* test = fHead;
1718 SkTSpan<TCurve, OppCurve>* next;
caryclark54359292015-03-26 07:52:43 -07001719 do {
1720 next = test->fNext;
1721 if (test->fCoinStart.perpT() < 0 || test->fCoinEnd.perpT() < 0) {
1722 continue;
reed0dc4dd62015-03-24 13:55:33 -07001723 }
caryclark54359292015-03-26 07:52:43 -07001724 SkDVector startV = test->fCoinStart.perpPt() - test->fPart[0];
1725 SkDVector endV = test->fCoinEnd.perpPt() - test->fPart[TCurve::kPointLast];
1726#if DEBUG_T_SECT
1727 SkDebugf("%s startV=(%1.9g,%1.9g) endV=(%1.9g,%1.9g) dot=%1.9g\n", __FUNCTION__,
1728 startV.fX, startV.fY, endV.fX, endV.fY, startV.dot(endV));
1729#endif
1730 if (startV.dot(endV) <= 0) {
1731 continue;
1732 }
1733 this->removeSpans(test, opp);
1734 } while ((test = next));
1735}
1736
caryclark1049f122015-04-20 08:31:59 -07001737template<typename TCurve, typename OppCurve>
1738void SkTSect<TCurve, OppCurve>::removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween) {
caryclark54359292015-03-26 07:52:43 -07001739 this->unlinkSpan(span);
1740 if (isBetween || between(0, span->fCoinStart.perpT(), 1)) {
1741 --fActiveCount;
1742 span->fNext = fCoincident;
1743 fCoincident = span;
1744 } else {
1745 this->markSpanGone(span);
reed0dc4dd62015-03-24 13:55:33 -07001746 }
caryclarkccec0f92015-03-24 07:28:17 -07001747}
1748
caryclark1049f122015-04-20 08:31:59 -07001749template<typename TCurve, typename OppCurve>
caryclark34efb702016-10-24 08:19:06 -07001750void SkTSect<TCurve, OppCurve>::removedEndCheck(SkTSpan<TCurve, OppCurve>* span) {
caryclark6c3b9cd2016-09-26 05:36:58 -07001751 if (!span->fStartT) {
1752 fRemovedStartT = true;
1753 }
1754 if (1 == span->fEndT) {
1755 fRemovedEndT = true;
1756 }
caryclark34efb702016-10-24 08:19:06 -07001757}
1758
1759template<typename TCurve, typename OppCurve>
1760bool SkTSect<TCurve, OppCurve>::removeSpan(SkTSpan<TCurve, OppCurve>* span) {\
1761 this->removedEndCheck(span);
caryclark54359292015-03-26 07:52:43 -07001762 this->unlinkSpan(span);
caryclarkef7cee42016-09-06 09:05:54 -07001763 return this->markSpanGone(span);
caryclark54359292015-03-26 07:52:43 -07001764}
1765
caryclark1049f122015-04-20 08:31:59 -07001766template<typename TCurve, typename OppCurve>
1767void SkTSect<TCurve, OppCurve>::removeSpanRange(SkTSpan<TCurve, OppCurve>* first,
1768 SkTSpan<TCurve, OppCurve>* last) {
caryclark54359292015-03-26 07:52:43 -07001769 if (first == last) {
1770 return;
1771 }
caryclark1049f122015-04-20 08:31:59 -07001772 SkTSpan<TCurve, OppCurve>* span = first;
caryclark54359292015-03-26 07:52:43 -07001773 SkASSERT(span);
caryclark1049f122015-04-20 08:31:59 -07001774 SkTSpan<TCurve, OppCurve>* final = last->fNext;
1775 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001776 while ((span = next) && span != final) {
1777 next = span->fNext;
1778 this->markSpanGone(span);
1779 }
1780 if (final) {
1781 final->fPrev = first;
1782 }
1783 first->fNext = final;
caryclark643ede62016-08-08 14:27:45 -07001784 first->validate();
caryclark54359292015-03-26 07:52:43 -07001785}
1786
caryclark1049f122015-04-20 08:31:59 -07001787template<typename TCurve, typename OppCurve>
1788void SkTSect<TCurve, OppCurve>::removeSpans(SkTSpan<TCurve, OppCurve>* span,
1789 SkTSect<OppCurve, TCurve>* opp) {
1790 SkTSpanBounded<OppCurve, TCurve>* bounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001791 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -07001792 SkTSpan<OppCurve, TCurve>* spanBounded = bounded->fBounded;
1793 SkTSpanBounded<OppCurve, TCurve>* next = bounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001794 if (span->removeBounded(spanBounded)) { // shuffles last into position 0
1795 this->removeSpan(span);
1796 }
1797 if (spanBounded->removeBounded(span)) {
1798 opp->removeSpan(spanBounded);
1799 }
1800 SkASSERT(!span->fDeleted || !opp->debugHasBounded(span));
1801 bounded = next;
reed0dc4dd62015-03-24 13:55:33 -07001802 }
1803}
caryclarkccec0f92015-03-24 07:28:17 -07001804
caryclark1049f122015-04-20 08:31:59 -07001805template<typename TCurve, typename OppCurve>
1806SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::spanAtT(double t,
1807 SkTSpan<TCurve, OppCurve>** priorSpan) {
1808 SkTSpan<TCurve, OppCurve>* test = fHead;
halcanary96fcdcc2015-08-27 07:41:13 -07001809 SkTSpan<TCurve, OppCurve>* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -07001810 while (test && test->fEndT < t) {
1811 prev = test;
1812 test = test->fNext;
reed0dc4dd62015-03-24 13:55:33 -07001813 }
caryclark54359292015-03-26 07:52:43 -07001814 *priorSpan = prev;
halcanary96fcdcc2015-08-27 07:41:13 -07001815 return test && test->fStartT <= t ? test : nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001816}
1817
caryclark1049f122015-04-20 08:31:59 -07001818template<typename TCurve, typename OppCurve>
1819SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::tail() {
1820 SkTSpan<TCurve, OppCurve>* result = fHead;
1821 SkTSpan<TCurve, OppCurve>* next = fHead;
reed0dc4dd62015-03-24 13:55:33 -07001822 while ((next = next->fNext)) {
1823 if (next->fEndT > result->fEndT) {
1824 result = next;
1825 }
1826 }
1827 return result;
1828}
1829
1830/* Each span has a range of opposite spans it intersects. After the span is split in two,
1831 adjust the range to its new size */
caryclark1049f122015-04-20 08:31:59 -07001832template<typename TCurve, typename OppCurve>
caryclarka35ab3e2016-10-20 08:32:18 -07001833bool SkTSect<TCurve, OppCurve>::trim(SkTSpan<TCurve, OppCurve>* span,
caryclark1049f122015-04-20 08:31:59 -07001834 SkTSect<OppCurve, TCurve>* opp) {
caryclarka35ab3e2016-10-20 08:32:18 -07001835 FAIL_IF(!span->initBounds(fCurve));
caryclark1049f122015-04-20 08:31:59 -07001836 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001837 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001838 SkTSpan<OppCurve, TCurve>* test = testBounded->fBounded;
1839 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001840 int oppSects, sects = this->intersects(span, opp, test, &oppSects);
1841 if (sects >= 1) {
1842 if (oppSects == 2) {
1843 test->initBounds(opp->fCurve);
1844 opp->removeAllBut(span, test, this);
1845 }
1846 if (sects == 2) {
1847 span->initBounds(fCurve);
1848 this->removeAllBut(test, span, opp);
caryclarka35ab3e2016-10-20 08:32:18 -07001849 return true;
caryclark54359292015-03-26 07:52:43 -07001850 }
reed0dc4dd62015-03-24 13:55:33 -07001851 } else {
caryclark54359292015-03-26 07:52:43 -07001852 if (span->removeBounded(test)) {
1853 this->removeSpan(span);
1854 }
1855 if (test->removeBounded(span)) {
1856 opp->removeSpan(test);
1857 }
1858 }
1859 testBounded = next;
1860 }
caryclarka35ab3e2016-10-20 08:32:18 -07001861 return true;
caryclark54359292015-03-26 07:52:43 -07001862}
1863
caryclark1049f122015-04-20 08:31:59 -07001864template<typename TCurve, typename OppCurve>
1865void SkTSect<TCurve, OppCurve>::unlinkSpan(SkTSpan<TCurve, OppCurve>* span) {
1866 SkTSpan<TCurve, OppCurve>* prev = span->fPrev;
1867 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001868 if (prev) {
1869 prev->fNext = next;
1870 if (next) {
1871 next->fPrev = prev;
caryclark643ede62016-08-08 14:27:45 -07001872 next->validate();
caryclark54359292015-03-26 07:52:43 -07001873 }
1874 } else {
1875 fHead = next;
1876 if (next) {
halcanary96fcdcc2015-08-27 07:41:13 -07001877 next->fPrev = nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001878 }
1879 }
1880}
1881
caryclark1049f122015-04-20 08:31:59 -07001882template<typename TCurve, typename OppCurve>
1883bool SkTSect<TCurve, OppCurve>::updateBounded(SkTSpan<TCurve, OppCurve>* first,
1884 SkTSpan<TCurve, OppCurve>* last, SkTSpan<OppCurve, TCurve>* oppFirst) {
1885 SkTSpan<TCurve, OppCurve>* test = first;
1886 const SkTSpan<TCurve, OppCurve>* final = last->next();
caryclark54359292015-03-26 07:52:43 -07001887 bool deleteSpan = false;
1888 do {
1889 deleteSpan |= test->removeAllBounded();
caryclarke25a4f62016-07-26 09:26:29 -07001890 } while ((test = test->fNext) != final && test);
halcanary96fcdcc2015-08-27 07:41:13 -07001891 first->fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -07001892 first->addBounded(oppFirst, &fHeap);
1893 // cannot call validate until remove span range is called
1894 return deleteSpan;
1895}
1896
1897
caryclark1049f122015-04-20 08:31:59 -07001898template<typename TCurve, typename OppCurve>
1899void SkTSect<TCurve, OppCurve>::validate() const {
caryclark643ede62016-08-08 14:27:45 -07001900#if DEBUG_VALIDATE
caryclark45fa4472015-01-16 07:04:10 -08001901 int count = 0;
caryclark643ede62016-08-08 14:27:45 -07001902 double last = 0;
caryclark45fa4472015-01-16 07:04:10 -08001903 if (fHead) {
caryclark1049f122015-04-20 08:31:59 -07001904 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark45fa4472015-01-16 07:04:10 -08001905 SkASSERT(!span->fPrev);
caryclark643ede62016-08-08 14:27:45 -07001906 const SkTSpan<TCurve, OppCurve>* next;
caryclark45fa4472015-01-16 07:04:10 -08001907 do {
1908 span->validate();
1909 SkASSERT(span->fStartT >= last);
caryclark643ede62016-08-08 14:27:45 -07001910 last = span->fEndT;
caryclark45fa4472015-01-16 07:04:10 -08001911 ++count;
caryclark643ede62016-08-08 14:27:45 -07001912 next = span->fNext;
1913 SkASSERT(next != span);
1914 } while ((span = next) != nullptr);
caryclark45fa4472015-01-16 07:04:10 -08001915 }
1916 SkASSERT(count == fActiveCount);
caryclark643ede62016-08-08 14:27:45 -07001917#endif
1918#if DEBUG_T_SECT
caryclark45fa4472015-01-16 07:04:10 -08001919 SkASSERT(fActiveCount <= fDebugAllocatedCount);
1920 int deletedCount = 0;
caryclark1049f122015-04-20 08:31:59 -07001921 const SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001922 while (deleted) {
1923 ++deletedCount;
1924 deleted = deleted->fNext;
1925 }
caryclark1049f122015-04-20 08:31:59 -07001926 const SkTSpan<TCurve, OppCurve>* coincident = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001927 while (coincident) {
1928 ++deletedCount;
1929 coincident = coincident->fNext;
1930 }
caryclark45fa4472015-01-16 07:04:10 -08001931 SkASSERT(fActiveCount + deletedCount == fDebugAllocatedCount);
caryclarkccec0f92015-03-24 07:28:17 -07001932#endif
caryclark54359292015-03-26 07:52:43 -07001933}
1934
caryclark1049f122015-04-20 08:31:59 -07001935template<typename TCurve, typename OppCurve>
1936void SkTSect<TCurve, OppCurve>::validateBounded() const {
caryclark643ede62016-08-08 14:27:45 -07001937#if DEBUG_VALIDATE
caryclark54359292015-03-26 07:52:43 -07001938 if (!fHead) {
1939 return;
1940 }
caryclark1049f122015-04-20 08:31:59 -07001941 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark54359292015-03-26 07:52:43 -07001942 do {
1943 span->validateBounded();
halcanary96fcdcc2015-08-27 07:41:13 -07001944 } while ((span = span->fNext) != nullptr);
caryclark54359292015-03-26 07:52:43 -07001945#endif
1946}
caryclark45fa4472015-01-16 07:04:10 -08001947
caryclark1049f122015-04-20 08:31:59 -07001948template<typename TCurve, typename OppCurve>
1949int SkTSect<TCurve, OppCurve>::EndsEqual(const SkTSect<TCurve, OppCurve>* sect1,
1950 const SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark45fa4472015-01-16 07:04:10 -08001951 int zeroOneSet = 0;
caryclark54359292015-03-26 07:52:43 -07001952 if (sect1->fCurve[0] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001953 zeroOneSet |= kZeroS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001954 intersections->insert(0, 0, sect1->fCurve[0]);
1955 }
caryclark1049f122015-04-20 08:31:59 -07001956 if (sect1->fCurve[0] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001957 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark54359292015-03-26 07:52:43 -07001958 intersections->insert(0, 1, sect1->fCurve[0]);
1959 }
1960 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001961 zeroOneSet |= kOneS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001962 intersections->insert(1, 0, sect1->fCurve[TCurve::kPointLast]);
1963 }
caryclark1049f122015-04-20 08:31:59 -07001964 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001965 zeroOneSet |= kOneS1Set | kOneS2Set;
reed0dc4dd62015-03-24 13:55:33 -07001966 intersections->insert(1, 1, sect1->fCurve[TCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001967 }
1968 // check for zero
1969 if (!(zeroOneSet & (kZeroS1Set | kZeroS2Set))
1970 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[0])) {
1971 zeroOneSet |= kZeroS1Set | kZeroS2Set;
1972 intersections->insertNear(0, 0, sect1->fCurve[0], sect2->fCurve[0]);
1973 }
1974 if (!(zeroOneSet & (kZeroS1Set | kOneS2Set))
caryclark1049f122015-04-20 08:31:59 -07001975 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07001976 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark1049f122015-04-20 08:31:59 -07001977 intersections->insertNear(0, 1, sect1->fCurve[0], sect2->fCurve[OppCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001978 }
1979 // check for one
1980 if (!(zeroOneSet & (kOneS1Set | kZeroS2Set))
1981 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[0])) {
1982 zeroOneSet |= kOneS1Set | kZeroS2Set;
1983 intersections->insertNear(1, 0, sect1->fCurve[TCurve::kPointLast], sect2->fCurve[0]);
1984 }
1985 if (!(zeroOneSet & (kOneS1Set | kOneS2Set))
1986 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[
caryclark1049f122015-04-20 08:31:59 -07001987 OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07001988 zeroOneSet |= kOneS1Set | kOneS2Set;
1989 intersections->insertNear(1, 1, sect1->fCurve[TCurve::kPointLast],
caryclark1049f122015-04-20 08:31:59 -07001990 sect2->fCurve[OppCurve::kPointLast]);
caryclark45fa4472015-01-16 07:04:10 -08001991 }
1992 return zeroOneSet;
1993}
1994
caryclark1049f122015-04-20 08:31:59 -07001995template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08001996struct SkClosestRecord {
caryclark54359292015-03-26 07:52:43 -07001997 bool operator<(const SkClosestRecord& rh) const {
1998 return fClosest < rh.fClosest;
1999 }
2000
caryclark45fa4472015-01-16 07:04:10 -08002001 void addIntersection(SkIntersections* intersections) const {
2002 double r1t = fC1Index ? fC1Span->endT() : fC1Span->startT();
2003 double r2t = fC2Index ? fC2Span->endT() : fC2Span->startT();
2004 intersections->insert(r1t, r2t, fC1Span->part()[fC1Index]);
2005 }
2006
caryclark1049f122015-04-20 08:31:59 -07002007 void findEnd(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2,
caryclark45fa4472015-01-16 07:04:10 -08002008 int c1Index, int c2Index) {
2009 const TCurve& c1 = span1->part();
caryclark1049f122015-04-20 08:31:59 -07002010 const OppCurve& c2 = span2->part();
caryclark45fa4472015-01-16 07:04:10 -08002011 if (!c1[c1Index].approximatelyEqual(c2[c2Index])) {
2012 return;
2013 }
2014 double dist = c1[c1Index].distanceSquared(c2[c2Index]);
2015 if (fClosest < dist) {
2016 return;
2017 }
2018 fC1Span = span1;
2019 fC2Span = span2;
2020 fC1StartT = span1->startT();
2021 fC1EndT = span1->endT();
2022 fC2StartT = span2->startT();
2023 fC2EndT = span2->endT();
2024 fC1Index = c1Index;
2025 fC2Index = c2Index;
2026 fClosest = dist;
2027 }
2028
caryclarkcdeff812016-07-22 03:34:19 -07002029 bool matesWith(const SkClosestRecord& mate SkDEBUGPARAMS(SkIntersections* i)) const {
caryclark45fa4472015-01-16 07:04:10 -08002030 SkASSERT(fC1Span == mate.fC1Span || fC1Span->endT() <= mate.fC1Span->startT()
2031 || mate.fC1Span->endT() <= fC1Span->startT());
caryclarkcdeff812016-07-22 03:34:19 -07002032 SkOPOBJASSERT(i, fC2Span == mate.fC2Span || fC2Span->endT() <= mate.fC2Span->startT()
caryclark45fa4472015-01-16 07:04:10 -08002033 || mate.fC2Span->endT() <= fC2Span->startT());
2034 return fC1Span == mate.fC1Span || fC1Span->endT() == mate.fC1Span->startT()
2035 || fC1Span->startT() == mate.fC1Span->endT()
2036 || fC2Span == mate.fC2Span
2037 || fC2Span->endT() == mate.fC2Span->startT()
2038 || fC2Span->startT() == mate.fC2Span->endT();
2039 }
2040
2041 void merge(const SkClosestRecord& mate) {
2042 fC1Span = mate.fC1Span;
2043 fC2Span = mate.fC2Span;
2044 fClosest = mate.fClosest;
2045 fC1Index = mate.fC1Index;
2046 fC2Index = mate.fC2Index;
2047 }
caryclark55888e42016-07-18 10:01:36 -07002048
caryclark45fa4472015-01-16 07:04:10 -08002049 void reset() {
2050 fClosest = FLT_MAX;
halcanary96fcdcc2015-08-27 07:41:13 -07002051 SkDEBUGCODE(fC1Span = nullptr);
2052 SkDEBUGCODE(fC2Span = nullptr);
caryclark45fa4472015-01-16 07:04:10 -08002053 SkDEBUGCODE(fC1Index = fC2Index = -1);
2054 }
2055
2056 void update(const SkClosestRecord& mate) {
2057 fC1StartT = SkTMin(fC1StartT, mate.fC1StartT);
2058 fC1EndT = SkTMax(fC1EndT, mate.fC1EndT);
2059 fC2StartT = SkTMin(fC2StartT, mate.fC2StartT);
2060 fC2EndT = SkTMax(fC2EndT, mate.fC2EndT);
2061 }
2062
caryclark1049f122015-04-20 08:31:59 -07002063 const SkTSpan<TCurve, OppCurve>* fC1Span;
2064 const SkTSpan<OppCurve, TCurve>* fC2Span;
caryclark45fa4472015-01-16 07:04:10 -08002065 double fC1StartT;
2066 double fC1EndT;
2067 double fC2StartT;
2068 double fC2EndT;
2069 double fClosest;
2070 int fC1Index;
2071 int fC2Index;
2072};
2073
caryclark1049f122015-04-20 08:31:59 -07002074template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08002075struct SkClosestSect {
2076 SkClosestSect()
2077 : fUsed(0) {
2078 fClosest.push_back().reset();
2079 }
2080
caryclarkcdeff812016-07-22 03:34:19 -07002081 bool find(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2
2082 SkDEBUGPARAMS(SkIntersections* i)) {
caryclark1049f122015-04-20 08:31:59 -07002083 SkClosestRecord<TCurve, OppCurve>* record = &fClosest[fUsed];
caryclark45fa4472015-01-16 07:04:10 -08002084 record->findEnd(span1, span2, 0, 0);
caryclark1049f122015-04-20 08:31:59 -07002085 record->findEnd(span1, span2, 0, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002086 record->findEnd(span1, span2, TCurve::kPointLast, 0);
caryclark1049f122015-04-20 08:31:59 -07002087 record->findEnd(span1, span2, TCurve::kPointLast, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002088 if (record->fClosest == FLT_MAX) {
caryclark54359292015-03-26 07:52:43 -07002089 return false;
caryclark45fa4472015-01-16 07:04:10 -08002090 }
2091 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002092 SkClosestRecord<TCurve, OppCurve>* test = &fClosest[index];
caryclarkcdeff812016-07-22 03:34:19 -07002093 if (test->matesWith(*record SkDEBUGPARAMS(i))) {
caryclark45fa4472015-01-16 07:04:10 -08002094 if (test->fClosest > record->fClosest) {
2095 test->merge(*record);
2096 }
2097 test->update(*record);
2098 record->reset();
caryclark54359292015-03-26 07:52:43 -07002099 return false;
caryclark45fa4472015-01-16 07:04:10 -08002100 }
2101 }
2102 ++fUsed;
2103 fClosest.push_back().reset();
caryclark54359292015-03-26 07:52:43 -07002104 return true;
caryclark45fa4472015-01-16 07:04:10 -08002105 }
2106
2107 void finish(SkIntersections* intersections) const {
caryclark1049f122015-04-20 08:31:59 -07002108 SkSTArray<TCurve::kMaxIntersections * 3,
2109 const SkClosestRecord<TCurve, OppCurve>*, true> closestPtrs;
caryclark45fa4472015-01-16 07:04:10 -08002110 for (int index = 0; index < fUsed; ++index) {
caryclark54359292015-03-26 07:52:43 -07002111 closestPtrs.push_back(&fClosest[index]);
2112 }
caryclark1049f122015-04-20 08:31:59 -07002113 SkTQSort<const SkClosestRecord<TCurve, OppCurve> >(closestPtrs.begin(), closestPtrs.end()
2114 - 1);
caryclark54359292015-03-26 07:52:43 -07002115 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002116 const SkClosestRecord<TCurve, OppCurve>* test = closestPtrs[index];
caryclark54359292015-03-26 07:52:43 -07002117 test->addIntersection(intersections);
caryclark45fa4472015-01-16 07:04:10 -08002118 }
2119 }
2120
caryclark54359292015-03-26 07:52:43 -07002121 // this is oversized so that an extra records can merge into final one
caryclark1049f122015-04-20 08:31:59 -07002122 SkSTArray<TCurve::kMaxIntersections * 2, SkClosestRecord<TCurve, OppCurve>, true> fClosest;
caryclark45fa4472015-01-16 07:04:10 -08002123 int fUsed;
2124};
2125
2126// returns true if the rect is too small to consider
caryclark1049f122015-04-20 08:31:59 -07002127template<typename TCurve, typename OppCurve>
2128void SkTSect<TCurve, OppCurve>::BinarySearch(SkTSect<TCurve, OppCurve>* sect1,
2129 SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark54359292015-03-26 07:52:43 -07002130#if DEBUG_T_SECT_DUMP > 1
2131 gDumpTSectNum = 0;
2132#endif
caryclark1049f122015-04-20 08:31:59 -07002133 SkDEBUGCODE(sect1->fOppSect = sect2);
2134 SkDEBUGCODE(sect2->fOppSect = sect1);
caryclark45fa4472015-01-16 07:04:10 -08002135 intersections->reset();
caryclarka35ab3e2016-10-20 08:32:18 -07002136 intersections->setMax(TCurve::kMaxIntersections + 4); // give extra for slop
caryclark1049f122015-04-20 08:31:59 -07002137 SkTSpan<TCurve, OppCurve>* span1 = sect1->fHead;
2138 SkTSpan<OppCurve, TCurve>* span2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002139 int oppSect, sect = sect1->intersects(span1, sect2, span2, &oppSect);
2140// SkASSERT(between(0, sect, 2));
2141 if (!sect) {
caryclark45fa4472015-01-16 07:04:10 -08002142 return;
2143 }
caryclark54359292015-03-26 07:52:43 -07002144 if (sect == 2 && oppSect == 2) {
caryclark45fa4472015-01-16 07:04:10 -08002145 (void) EndsEqual(sect1, sect2, intersections);
2146 return;
2147 }
caryclark54359292015-03-26 07:52:43 -07002148 span1->addBounded(span2, &sect1->fHeap);
2149 span2->addBounded(span1, &sect2->fHeap);
caryclark26ad22a2015-10-16 09:03:38 -07002150 const int kMaxCoinLoopCount = 8;
2151 int coinLoopCount = kMaxCoinLoopCount;
2152 double start1s SK_INIT_TO_AVOID_WARNING;
2153 double start1e SK_INIT_TO_AVOID_WARNING;
caryclark45fa4472015-01-16 07:04:10 -08002154 do {
2155 // find the largest bounds
caryclark1049f122015-04-20 08:31:59 -07002156 SkTSpan<TCurve, OppCurve>* largest1 = sect1->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002157 if (!largest1) {
2158 break;
2159 }
caryclark1049f122015-04-20 08:31:59 -07002160 SkTSpan<OppCurve, TCurve>* largest2 = sect2->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002161 // split it
caryclark1049f122015-04-20 08:31:59 -07002162 if (!largest2 || (largest1 && (largest1->fBoundsMax > largest2->fBoundsMax
2163 || (!largest1->fCollapsed && largest2->fCollapsed)))) {
2164 if (largest1->fCollapsed) {
2165 break;
2166 }
caryclark34efb702016-10-24 08:19:06 -07002167 sect1->resetRemovedEnds();
2168 sect2->resetRemovedEnds();
caryclark1049f122015-04-20 08:31:59 -07002169 // trim parts that don't intersect the opposite
2170 SkTSpan<TCurve, OppCurve>* half1 = sect1->addOne();
caryclark643ede62016-08-08 14:27:45 -07002171 SkDEBUGCODE(half1->debugSetGlobalState(sect1->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002172 if (!half1->split(largest1, &sect1->fHeap)) {
2173 break;
2174 }
caryclarka35ab3e2016-10-20 08:32:18 -07002175 if (!sect1->trim(largest1, sect2)) {
2176 SkOPOBJASSERT(intersections, 0);
2177 return;
2178 }
2179 if (!sect1->trim(half1, sect2)) {
2180 SkOPOBJASSERT(intersections, 0);
2181 return;
2182 }
caryclark1049f122015-04-20 08:31:59 -07002183 } else {
2184 if (largest2->fCollapsed) {
2185 break;
2186 }
caryclark34efb702016-10-24 08:19:06 -07002187 sect1->resetRemovedEnds();
2188 sect2->resetRemovedEnds();
caryclark1049f122015-04-20 08:31:59 -07002189 // trim parts that don't intersect the opposite
2190 SkTSpan<OppCurve, TCurve>* half2 = sect2->addOne();
caryclark643ede62016-08-08 14:27:45 -07002191 SkDEBUGCODE(half2->debugSetGlobalState(sect2->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002192 if (!half2->split(largest2, &sect2->fHeap)) {
2193 break;
2194 }
caryclarka35ab3e2016-10-20 08:32:18 -07002195 if (!sect2->trim(largest2, sect1)) {
2196 SkOPOBJASSERT(intersections, 0);
2197 return;
2198 }
2199 if (!sect2->trim(half2, sect1)) {
2200 SkOPOBJASSERT(intersections, 0);
2201 return;
2202 }
caryclark45fa4472015-01-16 07:04:10 -08002203 }
caryclark54359292015-03-26 07:52:43 -07002204 sect1->validate();
2205 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002206#if DEBUG_T_SECT_LOOP_COUNT
2207 intersections->debugBumpLoopCount(SkIntersections::kIterations_DebugLoop);
2208#endif
caryclark45fa4472015-01-16 07:04:10 -08002209 // if there are 9 or more continuous spans on both sects, suspect coincidence
2210 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2211 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
caryclark26ad22a2015-10-16 09:03:38 -07002212 if (coinLoopCount == kMaxCoinLoopCount) {
2213 start1s = sect1->fHead->fStartT;
2214 start1e = sect1->tail()->fEndT;
2215 }
caryclarkef7cee42016-09-06 09:05:54 -07002216 if (!sect1->coincidentCheck(sect2)) {
2217 return;
2218 }
caryclark54359292015-03-26 07:52:43 -07002219 sect1->validate();
2220 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002221#if DEBUG_T_SECT_LOOP_COUNT
2222 intersections->debugBumpLoopCount(SkIntersections::kCoinCheck_DebugLoop);
2223#endif
caryclarkef784fb2015-10-30 12:03:06 -07002224 if (!--coinLoopCount && sect1->fHead && sect2->fHead) {
caryclark26ad22a2015-10-16 09:03:38 -07002225 /* All known working cases resolve in two tries. Sadly, cubicConicTests[0]
2226 gets stuck in a loop. It adds an extension to allow a coincident end
2227 perpendicular to track its intersection in the opposite curve. However,
2228 the bounding box of the extension does not intersect the original curve,
caryclark55888e42016-07-18 10:01:36 -07002229 so the extension is discarded, only to be added again the next time around. */
caryclark26ad22a2015-10-16 09:03:38 -07002230 sect1->coincidentForce(sect2, start1s, start1e);
2231 sect1->validate();
2232 sect2->validate();
2233 }
caryclark45fa4472015-01-16 07:04:10 -08002234 }
caryclark54359292015-03-26 07:52:43 -07002235 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2236 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
2237 sect1->computePerpendiculars(sect2, sect1->fHead, sect1->tail());
2238 sect2->computePerpendiculars(sect1, sect2->fHead, sect2->tail());
2239 sect1->removeByPerpendicular(sect2);
2240 sect1->validate();
2241 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002242#if DEBUG_T_SECT_LOOP_COUNT
2243 intersections->debugBumpLoopCount(SkIntersections::kComputePerp_DebugLoop);
2244#endif
caryclark1049f122015-04-20 08:31:59 -07002245 if (sect1->collapsed() > TCurve::kMaxIntersections) {
2246 break;
2247 }
caryclark54359292015-03-26 07:52:43 -07002248 }
2249#if DEBUG_T_SECT_DUMP
2250 sect1->dumpBoth(sect2);
caryclark45fa4472015-01-16 07:04:10 -08002251#endif
2252 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002253 break;
caryclark45fa4472015-01-16 07:04:10 -08002254 }
2255 } while (true);
caryclark1049f122015-04-20 08:31:59 -07002256 SkTSpan<TCurve, OppCurve>* coincident = sect1->fCoincident;
caryclark54359292015-03-26 07:52:43 -07002257 if (coincident) {
2258 // if there is more than one coincident span, check loosely to see if they should be joined
2259 if (coincident->fNext) {
2260 sect1->mergeCoincidence(sect2);
2261 coincident = sect1->fCoincident;
2262 }
2263 SkASSERT(sect2->fCoincident); // courtesy check : coincidence only looks at sect 1
caryclark45fa4472015-01-16 07:04:10 -08002264 do {
caryclark221a4bb2016-10-07 11:15:15 -07002265 if (!coincident) {
2266 return;
2267 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002268 if (!coincident->fCoinStart.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002269 continue;
2270 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002271 if (!coincident->fCoinEnd.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002272 continue;
2273 }
caryclark54359292015-03-26 07:52:43 -07002274 int index = intersections->insertCoincident(coincident->fStartT,
2275 coincident->fCoinStart.perpT(), coincident->fPart[0]);
2276 if ((intersections->insertCoincident(coincident->fEndT,
2277 coincident->fCoinEnd.perpT(),
2278 coincident->fPart[TCurve::kPointLast]) < 0) && index >= 0) {
caryclark45fa4472015-01-16 07:04:10 -08002279 intersections->clearCoincidence(index);
2280 }
caryclark54359292015-03-26 07:52:43 -07002281 } while ((coincident = coincident->fNext));
2282 }
caryclark1049f122015-04-20 08:31:59 -07002283 int zeroOneSet = EndsEqual(sect1, sect2, intersections);
caryclark34efb702016-10-24 08:19:06 -07002284// if (!sect1->fHead || !sect2->fHead) {
caryclark6c3b9cd2016-09-26 05:36:58 -07002285 // if the final iteration contains an end (0 or 1),
2286 if (sect1->fRemovedStartT && !(zeroOneSet & kZeroS1Set)) {
2287 SkTCoincident<TCurve, OppCurve> perp; // intersect perpendicular with opposite curve
caryclarka35ab3e2016-10-20 08:32:18 -07002288 perp.setPerp(sect1->fCurve, 0, sect1->fCurve[0], sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002289 if (perp.isMatch()) {
2290 intersections->insert(0, perp.perpT(), perp.perpPt());
2291 }
2292 }
2293 if (sect1->fRemovedEndT && !(zeroOneSet & kOneS1Set)) {
2294 SkTCoincident<TCurve, OppCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002295 perp.setPerp(sect1->fCurve, 1, sect1->fCurve[TCurve::kPointLast], sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002296 if (perp.isMatch()) {
2297 intersections->insert(1, perp.perpT(), perp.perpPt());
2298 }
2299 }
2300 if (sect2->fRemovedStartT && !(zeroOneSet & kZeroS2Set)) {
2301 SkTCoincident<OppCurve, TCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002302 perp.setPerp(sect2->fCurve, 0, sect2->fCurve[0], sect1->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002303 if (perp.isMatch()) {
2304 intersections->insert(perp.perpT(), 0, perp.perpPt());
2305 }
2306 }
2307 if (sect2->fRemovedEndT && !(zeroOneSet & kOneS2Set)) {
2308 SkTCoincident<OppCurve, TCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002309 perp.setPerp(sect2->fCurve, 1, sect2->fCurve[OppCurve::kPointLast], sect1->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002310 if (perp.isMatch()) {
2311 intersections->insert(perp.perpT(), 1, perp.perpPt());
2312 }
2313 }
caryclark34efb702016-10-24 08:19:06 -07002314// }
2315 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002316 return;
caryclark45fa4472015-01-16 07:04:10 -08002317 }
caryclark45fa4472015-01-16 07:04:10 -08002318 sect1->recoverCollapsed();
2319 sect2->recoverCollapsed();
caryclark1049f122015-04-20 08:31:59 -07002320 SkTSpan<TCurve, OppCurve>* result1 = sect1->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002321 // check heads and tails for zero and ones and insert them if we haven't already done so
caryclark1049f122015-04-20 08:31:59 -07002322 const SkTSpan<TCurve, OppCurve>* head1 = result1;
caryclark45fa4472015-01-16 07:04:10 -08002323 if (!(zeroOneSet & kZeroS1Set) && approximately_less_than_zero(head1->fStartT)) {
2324 const SkDPoint& start1 = sect1->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002325 if (head1->isBounded()) {
2326 double t = head1->closestBoundedT(start1);
2327 if (sect2->fCurve.ptAtT(t).approximatelyEqual(start1)) {
2328 intersections->insert(0, t, start1);
2329 }
caryclark45fa4472015-01-16 07:04:10 -08002330 }
2331 }
caryclark1049f122015-04-20 08:31:59 -07002332 const SkTSpan<OppCurve, TCurve>* head2 = sect2->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002333 if (!(zeroOneSet & kZeroS2Set) && approximately_less_than_zero(head2->fStartT)) {
2334 const SkDPoint& start2 = sect2->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002335 if (head2->isBounded()) {
2336 double t = head2->closestBoundedT(start2);
2337 if (sect1->fCurve.ptAtT(t).approximatelyEqual(start2)) {
2338 intersections->insert(t, 0, start2);
2339 }
caryclark45fa4472015-01-16 07:04:10 -08002340 }
2341 }
caryclark1049f122015-04-20 08:31:59 -07002342 const SkTSpan<TCurve, OppCurve>* tail1 = sect1->tail();
caryclark45fa4472015-01-16 07:04:10 -08002343 if (!(zeroOneSet & kOneS1Set) && approximately_greater_than_one(tail1->fEndT)) {
2344 const SkDPoint& end1 = sect1->fCurve[TCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002345 if (tail1->isBounded()) {
2346 double t = tail1->closestBoundedT(end1);
2347 if (sect2->fCurve.ptAtT(t).approximatelyEqual(end1)) {
2348 intersections->insert(1, t, end1);
2349 }
caryclark45fa4472015-01-16 07:04:10 -08002350 }
2351 }
caryclark1049f122015-04-20 08:31:59 -07002352 const SkTSpan<OppCurve, TCurve>* tail2 = sect2->tail();
caryclark45fa4472015-01-16 07:04:10 -08002353 if (!(zeroOneSet & kOneS2Set) && approximately_greater_than_one(tail2->fEndT)) {
caryclark1049f122015-04-20 08:31:59 -07002354 const SkDPoint& end2 = sect2->fCurve[OppCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002355 if (tail2->isBounded()) {
2356 double t = tail2->closestBoundedT(end2);
2357 if (sect1->fCurve.ptAtT(t).approximatelyEqual(end2)) {
2358 intersections->insert(t, 1, end2);
2359 }
caryclark45fa4472015-01-16 07:04:10 -08002360 }
2361 }
caryclark1049f122015-04-20 08:31:59 -07002362 SkClosestSect<TCurve, OppCurve> closest;
caryclark45fa4472015-01-16 07:04:10 -08002363 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07002364 while (result1 && result1->fCoinStart.isMatch() && result1->fCoinEnd.isMatch()) {
caryclark45fa4472015-01-16 07:04:10 -08002365 result1 = result1->fNext;
2366 }
2367 if (!result1) {
2368 break;
2369 }
caryclark1049f122015-04-20 08:31:59 -07002370 SkTSpan<OppCurve, TCurve>* result2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002371 bool found = false;
caryclark45fa4472015-01-16 07:04:10 -08002372 while (result2) {
caryclarkcdeff812016-07-22 03:34:19 -07002373 found |= closest.find(result1, result2 SkDEBUGPARAMS(intersections));
caryclark45fa4472015-01-16 07:04:10 -08002374 result2 = result2->fNext;
2375 }
caryclark45fa4472015-01-16 07:04:10 -08002376 } while ((result1 = result1->fNext));
2377 closest.finish(intersections);
caryclark54359292015-03-26 07:52:43 -07002378 // if there is more than one intersection and it isn't already coincident, check
2379 int last = intersections->used() - 1;
2380 for (int index = 0; index < last; ) {
2381 if (intersections->isCoincident(index) && intersections->isCoincident(index + 1)) {
2382 ++index;
2383 continue;
2384 }
2385 double midT = ((*intersections)[0][index] + (*intersections)[0][index + 1]) / 2;
2386 SkDPoint midPt = sect1->fCurve.ptAtT(midT);
2387 // intersect perpendicular with opposite curve
caryclark1049f122015-04-20 08:31:59 -07002388 SkTCoincident<TCurve, OppCurve> perp;
caryclark54359292015-03-26 07:52:43 -07002389 perp.setPerp(sect1->fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002390 if (!perp.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07002391 ++index;
2392 continue;
2393 }
2394 if (intersections->isCoincident(index)) {
2395 intersections->removeOne(index);
2396 --last;
2397 } else if (intersections->isCoincident(index + 1)) {
2398 intersections->removeOne(index + 1);
2399 --last;
2400 } else {
2401 intersections->setCoincident(index++);
2402 }
2403 intersections->setCoincident(index);
2404 }
caryclarka35ab3e2016-10-20 08:32:18 -07002405 SkOPOBJASSERT(intersections, intersections->used() <= TCurve::kMaxIntersections);
caryclark45fa4472015-01-16 07:04:10 -08002406}
deanm12670eb2016-04-26 14:09:01 -07002407
2408#endif