blob: 1e78f2a10811e1f1e6cf1e0912040f6714214731 [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
Herb Derbyc3cc5fa2017-03-07 11:11:47 -050010#include "SkArenaAlloc.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:
Herb Derbyc3cc5fa2017-03-07 11:11:47 -050088 void addBounded(SkTSpan<OppCurve, TCurve>* , SkArenaAlloc* );
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
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500177 bool split(SkTSpan* work, SkArenaAlloc* heap) {
caryclark54359292015-03-26 07:52:43 -0700178 return splitAt(work, (work->fStartT + work->fEndT) * 0.5, heap);
179 }
180
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500181 bool splitAt(SkTSpan* work, double t, SkArenaAlloc* 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,
Cary Clark74b42902018-03-09 07:38:47 -0500275 double* oppT, SkTSpan<OppCurve, TCurve>** oppFirst);
caryclark1049f122015-04-20 08:31:59 -0700276 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();
Cary Clark38702ab2017-09-05 18:11:55 -0400312 bool removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween);
caryclark1049f122015-04-20 08:31:59 -0700313 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
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500320 void resetRemovedEnds() {
caryclark34efb702016-10-24 08:19:06 -0700321 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);
Cary Clark38702ab2017-09-05 18:11:55 -0400327 bool unlinkSpan(SkTSpan<TCurve, OppCurve>* span);
caryclark1049f122015-04-20 08:31:59 -0700328 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;
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500334 SkArenaAlloc 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>
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500392void SkTSpan<TCurve, OppCurve>::addBounded(SkTSpan<OppCurve, TCurve>* span, SkArenaAlloc* heap) {
393 SkTSpanBounded<OppCurve, TCurve>* bounded = heap->make<SkTSpanBounded<OppCurve, TCurve>>();
caryclark54359292015-03-26 07:52:43 -0700394 bounded->fBounded = span;
395 bounded->fNext = fBounded;
396 fBounded = bounded;
397}
398
caryclark1049f122015-04-20 08:31:59 -0700399template<typename TCurve, typename OppCurve>
400SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::addFollowing(
401 SkTSpan<TCurve, OppCurve>* prior) {
402 SkTSpan<TCurve, OppCurve>* result = this->addOne();
caryclark643ede62016-08-08 14:27:45 -0700403 SkDEBUGCODE(result->debugSetGlobalState(this->globalState()));
caryclark54359292015-03-26 07:52:43 -0700404 result->fStartT = prior ? prior->fEndT : 0;
caryclark1049f122015-04-20 08:31:59 -0700405 SkTSpan<TCurve, OppCurve>* next = prior ? prior->fNext : fHead;
caryclark54359292015-03-26 07:52:43 -0700406 result->fEndT = next ? next->fStartT : 1;
407 result->fPrev = prior;
408 result->fNext = next;
409 if (prior) {
410 prior->fNext = result;
411 } else {
412 fHead = result;
413 }
414 if (next) {
415 next->fPrev = result;
416 }
417 result->resetBounds(fCurve);
Cary Clarkb8421ed2018-03-14 15:55:02 -0400418 // world may not be consistent to call validate here
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>
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500759bool SkTSpan<TCurve, OppCurve>::splitAt(SkTSpan* work, double t, SkArenaAlloc* 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>
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500861SkTSect<TCurve, OppCurve>::SkTSect(const TCurve& c
caryclarke25a4f62016-07-26 09:26:29 -0700862 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 {
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500887 result = fHeap.make<SkTSpan<TCurve, OppCurve>>();
caryclark45fa4472015-01-16 07:04:10 -0800888#if DEBUG_T_SECT
889 ++fDebugAllocatedCount;
890#endif
891 }
caryclarked0935a2015-10-22 07:23:52 -0700892 result->reset();
caryclark08b32492015-04-06 11:41:29 -0700893 result->fHasPerp = false;
894 result->fDeleted = false;
caryclark55888e42016-07-18 10:01:36 -0700895 ++fActiveCount;
caryclark54359292015-03-26 07:52:43 -0700896 PATH_OPS_DEBUG_T_SECT_CODE(result->fID = fDebugCount++ * 2 + fID);
caryclark1049f122015-04-20 08:31:59 -0700897 SkDEBUGCODE(result->fDebugSect = this);
caryclarked0935a2015-10-22 07:23:52 -0700898#ifdef SK_DEBUG
899 result->fPart.debugInit();
900 result->fCoinStart.debugInit();
901 result->fCoinEnd.debugInit();
902 result->fPrev = result->fNext = nullptr;
903 result->fBounds.debugInit();
904 result->fStartT = result->fEndT = result->fBoundsMax = SK_ScalarNaN;
905 result->fCollapsed = result->fIsLinear = result->fIsLine = 0xFF;
906#endif
caryclark45fa4472015-01-16 07:04:10 -0800907 return result;
908}
909
caryclark1049f122015-04-20 08:31:59 -0700910template<typename TCurve, typename OppCurve>
911bool SkTSect<TCurve, OppCurve>::binarySearchCoin(SkTSect<OppCurve, TCurve>* sect2, double tStart,
Cary Clark74b42902018-03-09 07:38:47 -0500912 double tStep, double* resultT, double* oppT, SkTSpan<OppCurve, TCurve>** oppFirst) {
caryclark1049f122015-04-20 08:31:59 -0700913 SkTSpan<TCurve, OppCurve> work;
caryclark45fa4472015-01-16 07:04:10 -0800914 double result = work.fStartT = work.fEndT = tStart;
caryclark1049f122015-04-20 08:31:59 -0700915 SkDEBUGCODE(work.fDebugSect = this);
caryclark45fa4472015-01-16 07:04:10 -0800916 SkDPoint last = fCurve.ptAtT(tStart);
917 SkDPoint oppPt;
918 bool flip = false;
caryclarkcdeff812016-07-22 03:34:19 -0700919 bool contained = false;
Cary Clark74b42902018-03-09 07:38:47 -0500920 bool down = tStep < 0;
caryclark1049f122015-04-20 08:31:59 -0700921 const OppCurve& opp = sect2->fCurve;
caryclark45fa4472015-01-16 07:04:10 -0800922 do {
923 tStep *= 0.5;
924 work.fStartT += tStep;
925 if (flip) {
926 tStep = -tStep;
927 flip = false;
928 }
929 work.initBounds(fCurve);
930 if (work.fCollapsed) {
931 return false;
932 }
933 if (last.approximatelyEqual(work.fPart[0])) {
934 break;
935 }
936 last = work.fPart[0];
937 work.fCoinStart.setPerp(fCurve, work.fStartT, last, opp);
caryclark6c3b9cd2016-09-26 05:36:58 -0700938 if (work.fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700939#if DEBUG_T_SECT
940 work.validatePerpPt(work.fCoinStart.perpT(), work.fCoinStart.perpPt());
941#endif
caryclark45fa4472015-01-16 07:04:10 -0800942 double oppTTest = work.fCoinStart.perpT();
caryclark54359292015-03-26 07:52:43 -0700943 if (sect2->fHead->contains(oppTTest)) {
caryclark45fa4472015-01-16 07:04:10 -0800944 *oppT = oppTTest;
945 oppPt = work.fCoinStart.perpPt();
caryclarkcdeff812016-07-22 03:34:19 -0700946 contained = true;
Cary Clark74b42902018-03-09 07:38:47 -0500947 if (down ? result <= work.fStartT : result >= work.fStartT) {
948 *oppFirst = nullptr; // signal caller to fail
949 return false;
950 }
caryclark45fa4472015-01-16 07:04:10 -0800951 result = work.fStartT;
952 continue;
953 }
954 }
955 tStep = -tStep;
956 flip = true;
957 } while (true);
caryclarkcdeff812016-07-22 03:34:19 -0700958 if (!contained) {
959 return false;
960 }
caryclark45fa4472015-01-16 07:04:10 -0800961 if (last.approximatelyEqual(fCurve[0])) {
962 result = 0;
963 } else if (last.approximatelyEqual(fCurve[TCurve::kPointLast])) {
964 result = 1;
965 }
966 if (oppPt.approximatelyEqual(opp[0])) {
967 *oppT = 0;
caryclark1049f122015-04-20 08:31:59 -0700968 } else if (oppPt.approximatelyEqual(opp[OppCurve::kPointLast])) {
caryclark45fa4472015-01-16 07:04:10 -0800969 *oppT = 1;
970 }
971 *resultT = result;
972 return true;
973}
974
975// OPTIMIZE ? keep a sorted list of sizes in the form of a doubly-linked list in quad span
976// so that each quad sect has a pointer to the largest, and can update it as spans
977// are split
caryclark1049f122015-04-20 08:31:59 -0700978template<typename TCurve, typename OppCurve>
979SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::boundsMax() const {
980 SkTSpan<TCurve, OppCurve>* test = fHead;
981 SkTSpan<TCurve, OppCurve>* largest = fHead;
caryclark54359292015-03-26 07:52:43 -0700982 bool lCollapsed = largest->fCollapsed;
caryclark45fa4472015-01-16 07:04:10 -0800983 while ((test = test->fNext)) {
caryclark54359292015-03-26 07:52:43 -0700984 bool tCollapsed = test->fCollapsed;
985 if ((lCollapsed && !tCollapsed) || (lCollapsed == tCollapsed &&
986 largest->fBoundsMax < test->fBoundsMax)) {
caryclark45fa4472015-01-16 07:04:10 -0800987 largest = test;
caryclark1049f122015-04-20 08:31:59 -0700988 lCollapsed = test->fCollapsed;
caryclark45fa4472015-01-16 07:04:10 -0800989 }
990 }
caryclark54359292015-03-26 07:52:43 -0700991 return largest;
caryclark45fa4472015-01-16 07:04:10 -0800992}
993
caryclark1049f122015-04-20 08:31:59 -0700994template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -0700995bool SkTSect<TCurve, OppCurve>::coincidentCheck(SkTSect<OppCurve, TCurve>* sect2) {
caryclark1049f122015-04-20 08:31:59 -0700996 SkTSpan<TCurve, OppCurve>* first = fHead;
caryclark9feb6322016-10-25 08:58:26 -0700997 if (!first) {
998 return false;
999 }
caryclark1049f122015-04-20 08:31:59 -07001000 SkTSpan<TCurve, OppCurve>* last, * next;
caryclark45fa4472015-01-16 07:04:10 -08001001 do {
caryclark54359292015-03-26 07:52:43 -07001002 int consecutive = this->countConsecutiveSpans(first, &last);
1003 next = last->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001004 if (consecutive < COINCIDENT_SPAN_COUNT) {
1005 continue;
1006 }
caryclark54359292015-03-26 07:52:43 -07001007 this->validate();
1008 sect2->validate();
1009 this->computePerpendiculars(sect2, first, last);
1010 this->validate();
1011 sect2->validate();
caryclark45fa4472015-01-16 07:04:10 -08001012 // check to see if a range of points are on the curve
caryclark1049f122015-04-20 08:31:59 -07001013 SkTSpan<TCurve, OppCurve>* coinStart = first;
caryclark54359292015-03-26 07:52:43 -07001014 do {
caryclarkef7cee42016-09-06 09:05:54 -07001015 bool success = this->extractCoincident(sect2, coinStart, last, &coinStart);
1016 if (!success) {
1017 return false;
1018 }
caryclark54359292015-03-26 07:52:43 -07001019 } while (coinStart && !last->fDeleted);
caryclark55888e42016-07-18 10:01:36 -07001020 if (!fHead || !sect2->fHead) {
1021 break;
1022 }
caryclark643ede62016-08-08 14:27:45 -07001023 if (!next || next->fDeleted) {
1024 break;
1025 }
caryclark45fa4472015-01-16 07:04:10 -08001026 } while ((first = next));
caryclarkef7cee42016-09-06 09:05:54 -07001027 return true;
caryclark45fa4472015-01-16 07:04:10 -08001028}
1029
caryclark1049f122015-04-20 08:31:59 -07001030template<typename TCurve, typename OppCurve>
caryclark26ad22a2015-10-16 09:03:38 -07001031void SkTSect<TCurve, OppCurve>::coincidentForce(SkTSect<OppCurve, TCurve>* sect2,
1032 double start1s, double start1e) {
1033 SkTSpan<TCurve, OppCurve>* first = fHead;
1034 SkTSpan<TCurve, OppCurve>* last = this->tail();
1035 SkTSpan<OppCurve, TCurve>* oppFirst = sect2->fHead;
1036 SkTSpan<OppCurve, TCurve>* oppLast = sect2->tail();
1037 bool deleteEmptySpans = this->updateBounded(first, last, oppFirst);
1038 deleteEmptySpans |= sect2->updateBounded(oppFirst, oppLast, first);
1039 this->removeSpanRange(first, last);
1040 sect2->removeSpanRange(oppFirst, oppLast);
1041 first->fStartT = start1s;
1042 first->fEndT = start1e;
1043 first->resetBounds(fCurve);
1044 first->fCoinStart.setPerp(fCurve, start1s, fCurve[0], sect2->fCurve);
1045 first->fCoinEnd.setPerp(fCurve, start1e, fCurve[TCurve::kPointLast], sect2->fCurve);
1046 bool oppMatched = first->fCoinStart.perpT() < first->fCoinEnd.perpT();
caryclarkef784fb2015-10-30 12:03:06 -07001047 double oppStartT = first->fCoinStart.perpT() == -1 ? 0 : SkTMax(0., first->fCoinStart.perpT());
1048 double oppEndT = first->fCoinEnd.perpT() == -1 ? 1 : SkTMin(1., first->fCoinEnd.perpT());
caryclark26ad22a2015-10-16 09:03:38 -07001049 if (!oppMatched) {
1050 SkTSwap(oppStartT, oppEndT);
1051 }
1052 oppFirst->fStartT = oppStartT;
1053 oppFirst->fEndT = oppEndT;
1054 oppFirst->resetBounds(sect2->fCurve);
1055 this->removeCoincident(first, false);
1056 sect2->removeCoincident(oppFirst, true);
1057 if (deleteEmptySpans) {
1058 this->deleteEmptySpans();
1059 sect2->deleteEmptySpans();
1060 }
1061}
1062
1063template<typename TCurve, typename OppCurve>
caryclark1049f122015-04-20 08:31:59 -07001064bool SkTSect<TCurve, OppCurve>::coincidentHasT(double t) {
1065 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001066 while (test) {
1067 if (between(test->fStartT, t, test->fEndT)) {
1068 return true;
1069 }
1070 test = test->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001071 }
caryclark54359292015-03-26 07:52:43 -07001072 return false;
caryclark45fa4472015-01-16 07:04:10 -08001073}
1074
caryclark1049f122015-04-20 08:31:59 -07001075template<typename TCurve, typename OppCurve>
1076int SkTSect<TCurve, OppCurve>::collapsed() const {
1077 int result = 0;
1078 const SkTSpan<TCurve, OppCurve>* test = fHead;
1079 while (test) {
1080 if (test->fCollapsed) {
1081 ++result;
1082 }
1083 test = test->next();
1084 }
1085 return result;
1086}
1087
1088template<typename TCurve, typename OppCurve>
1089void SkTSect<TCurve, OppCurve>::computePerpendiculars(SkTSect<OppCurve, TCurve>* sect2,
1090 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last) {
1091 const OppCurve& opp = sect2->fCurve;
1092 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001093 SkTSpan<TCurve, OppCurve>* prior = nullptr;
caryclark45fa4472015-01-16 07:04:10 -08001094 do {
caryclark54359292015-03-26 07:52:43 -07001095 if (!work->fHasPerp && !work->fCollapsed) {
1096 if (prior) {
1097 work->fCoinStart = prior->fCoinEnd;
1098 } else {
1099 work->fCoinStart.setPerp(fCurve, work->fStartT, work->fPart[0], opp);
caryclark45fa4472015-01-16 07:04:10 -08001100 }
caryclark6c3b9cd2016-09-26 05:36:58 -07001101 if (work->fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001102 double perpT = work->fCoinStart.perpT();
1103 if (sect2->coincidentHasT(perpT)) {
caryclarkdf386c52015-04-21 05:27:02 -07001104 work->fCoinStart.init();
caryclark54359292015-03-26 07:52:43 -07001105 } else {
1106 sect2->addForPerp(work, perpT);
1107 }
1108 }
1109 work->fCoinEnd.setPerp(fCurve, work->fEndT, work->fPart[TCurve::kPointLast], opp);
caryclark6c3b9cd2016-09-26 05:36:58 -07001110 if (work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001111 double perpT = work->fCoinEnd.perpT();
1112 if (sect2->coincidentHasT(perpT)) {
caryclarkdf386c52015-04-21 05:27:02 -07001113 work->fCoinEnd.init();
caryclark54359292015-03-26 07:52:43 -07001114 } else {
1115 sect2->addForPerp(work, perpT);
1116 }
1117 }
1118 work->fHasPerp = true;
caryclark45fa4472015-01-16 07:04:10 -08001119 }
1120 if (work == last) {
1121 break;
1122 }
caryclark54359292015-03-26 07:52:43 -07001123 prior = work;
caryclark45fa4472015-01-16 07:04:10 -08001124 work = work->fNext;
1125 SkASSERT(work);
1126 } while (true);
caryclark54359292015-03-26 07:52:43 -07001127}
1128
caryclark1049f122015-04-20 08:31:59 -07001129template<typename TCurve, typename OppCurve>
1130int SkTSect<TCurve, OppCurve>::countConsecutiveSpans(SkTSpan<TCurve, OppCurve>* first,
1131 SkTSpan<TCurve, OppCurve>** lastPtr) const {
caryclark54359292015-03-26 07:52:43 -07001132 int consecutive = 1;
caryclark1049f122015-04-20 08:31:59 -07001133 SkTSpan<TCurve, OppCurve>* last = first;
caryclark54359292015-03-26 07:52:43 -07001134 do {
caryclark1049f122015-04-20 08:31:59 -07001135 SkTSpan<TCurve, OppCurve>* next = last->fNext;
caryclark54359292015-03-26 07:52:43 -07001136 if (!next) {
1137 break;
1138 }
1139 if (next->fStartT > last->fEndT) {
1140 break;
1141 }
1142 ++consecutive;
1143 last = next;
1144 } while (true);
1145 *lastPtr = last;
1146 return consecutive;
1147}
1148
caryclark1049f122015-04-20 08:31:59 -07001149template<typename TCurve, typename OppCurve>
1150bool SkTSect<TCurve, OppCurve>::debugHasBounded(const SkTSpan<OppCurve, TCurve>* span) const {
1151 const SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001152 if (!test) {
1153 return false;
1154 }
1155 do {
1156 if (test->findOppSpan(span)) {
1157 return true;
1158 }
1159 } while ((test = test->next()));
1160 return false;
1161}
1162
caryclark1049f122015-04-20 08:31:59 -07001163template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001164bool SkTSect<TCurve, OppCurve>::deleteEmptySpans() {
caryclark1049f122015-04-20 08:31:59 -07001165 SkTSpan<TCurve, OppCurve>* test;
1166 SkTSpan<TCurve, OppCurve>* next = fHead;
Cary Clark59ed4822016-12-08 16:17:56 -05001167 int safetyHatch = 1000;
caryclark54359292015-03-26 07:52:43 -07001168 while ((test = next)) {
1169 next = test->fNext;
1170 if (!test->fBounded) {
caryclarkef7cee42016-09-06 09:05:54 -07001171 if (!this->removeSpan(test)) {
1172 return false;
1173 }
caryclark54359292015-03-26 07:52:43 -07001174 }
Cary Clark59ed4822016-12-08 16:17:56 -05001175 if (--safetyHatch < 0) {
1176 return false;
1177 }
caryclark54359292015-03-26 07:52:43 -07001178 }
caryclarkef7cee42016-09-06 09:05:54 -07001179 return true;
caryclark54359292015-03-26 07:52:43 -07001180}
1181
caryclark1049f122015-04-20 08:31:59 -07001182template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001183bool SkTSect<TCurve, OppCurve>::extractCoincident(
caryclark1049f122015-04-20 08:31:59 -07001184 SkTSect<OppCurve, TCurve>* sect2,
caryclarkef7cee42016-09-06 09:05:54 -07001185 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last,
1186 SkTSpan<TCurve, OppCurve>** result) {
caryclark1049f122015-04-20 08:31:59 -07001187 first = findCoincidentRun(first, &last);
caryclarka1b42d92016-08-16 10:25:29 -07001188 if (!first || !last) {
caryclarkef7cee42016-09-06 09:05:54 -07001189 *result = nullptr;
1190 return true;
caryclark45fa4472015-01-16 07:04:10 -08001191 }
1192 // march outwards to find limit of coincidence from here to previous and next spans
1193 double startT = first->fStartT;
caryclarkd8bc16b2015-03-26 09:05:12 -07001194 double oppStartT SK_INIT_TO_AVOID_WARNING;
caryclark54359292015-03-26 07:52:43 -07001195 double oppEndT SK_INIT_TO_AVOID_WARNING;
caryclark1049f122015-04-20 08:31:59 -07001196 SkTSpan<TCurve, OppCurve>* prev = first->fPrev;
caryclark6c3b9cd2016-09-26 05:36:58 -07001197 SkASSERT(first->fCoinStart.isMatch());
caryclark1049f122015-04-20 08:31:59 -07001198 SkTSpan<OppCurve, TCurve>* oppFirst = first->findOppT(first->fCoinStart.perpT());
caryclark6c3b9cd2016-09-26 05:36:58 -07001199 SkOPASSERT(last->fCoinEnd.isMatch());
caryclark54359292015-03-26 07:52:43 -07001200 bool oppMatched = first->fCoinStart.perpT() < first->fCoinEnd.perpT();
1201 double coinStart;
1202 SkDEBUGCODE(double coinEnd);
caryclark1049f122015-04-20 08:31:59 -07001203 SkTSpan<OppCurve, TCurve>* cutFirst;
caryclark54359292015-03-26 07:52:43 -07001204 if (prev && prev->fEndT == startT
1205 && this->binarySearchCoin(sect2, startT, prev->fStartT - startT, &coinStart,
Cary Clark74b42902018-03-09 07:38:47 -05001206 &oppStartT, &oppFirst)
caryclark1049f122015-04-20 08:31:59 -07001207 && prev->fStartT < coinStart && coinStart < startT
1208 && (cutFirst = prev->oppT(oppStartT))) {
1209 oppFirst = cutFirst;
caryclark54359292015-03-26 07:52:43 -07001210 first = this->addSplitAt(prev, coinStart);
1211 first->markCoincident();
1212 prev->fCoinEnd.markCoincident();
1213 if (oppFirst->fStartT < oppStartT && oppStartT < oppFirst->fEndT) {
caryclark1049f122015-04-20 08:31:59 -07001214 SkTSpan<OppCurve, TCurve>* oppHalf = sect2->addSplitAt(oppFirst, oppStartT);
caryclark54359292015-03-26 07:52:43 -07001215 if (oppMatched) {
1216 oppFirst->fCoinEnd.markCoincident();
1217 oppHalf->markCoincident();
1218 oppFirst = oppHalf;
1219 } else {
1220 oppFirst->markCoincident();
1221 oppHalf->fCoinStart.markCoincident();
1222 }
1223 }
1224 } else {
Cary Clark74b42902018-03-09 07:38:47 -05001225 if (!oppFirst) {
1226 return false;
1227 }
caryclark54359292015-03-26 07:52:43 -07001228 SkDEBUGCODE(coinStart = first->fStartT);
1229 SkDEBUGCODE(oppStartT = oppMatched ? oppFirst->fStartT : oppFirst->fEndT);
1230 }
caryclark1049f122015-04-20 08:31:59 -07001231 // FIXME: incomplete : if we're not at the end, find end of coin
1232 SkTSpan<OppCurve, TCurve>* oppLast;
caryclark6c3b9cd2016-09-26 05:36:58 -07001233 SkOPASSERT(last->fCoinEnd.isMatch());
caryclark54359292015-03-26 07:52:43 -07001234 oppLast = last->findOppT(last->fCoinEnd.perpT());
1235 SkDEBUGCODE(coinEnd = last->fEndT);
caryclark643ede62016-08-08 14:27:45 -07001236#ifdef SK_DEBUG
1237 if (!this->globalState() || !this->globalState()->debugSkipAssert()) {
1238 oppEndT = oppMatched ? oppLast->fEndT : oppLast->fStartT;
1239 }
1240#endif
caryclark54359292015-03-26 07:52:43 -07001241 if (!oppMatched) {
1242 SkTSwap(oppFirst, oppLast);
1243 SkTSwap(oppStartT, oppEndT);
1244 }
caryclarke25a4f62016-07-26 09:26:29 -07001245 SkOPASSERT(oppStartT < oppEndT);
caryclark54359292015-03-26 07:52:43 -07001246 SkASSERT(coinStart == first->fStartT);
1247 SkASSERT(coinEnd == last->fEndT);
caryclark643ede62016-08-08 14:27:45 -07001248 SkOPASSERT(oppStartT == oppFirst->fStartT);
1249 SkOPASSERT(oppEndT == oppLast->fEndT);
1250 if (!oppFirst) {
caryclarkef7cee42016-09-06 09:05:54 -07001251 *result = nullptr;
1252 return true;
caryclark643ede62016-08-08 14:27:45 -07001253 }
caryclark42942862016-08-19 07:01:33 -07001254 if (!oppLast) {
caryclarkef7cee42016-09-06 09:05:54 -07001255 *result = nullptr;
1256 return true;
caryclark42942862016-08-19 07:01:33 -07001257 }
caryclark54359292015-03-26 07:52:43 -07001258 // reduce coincident runs to single entries
1259 this->validate();
1260 sect2->validate();
caryclark1049f122015-04-20 08:31:59 -07001261 bool deleteEmptySpans = this->updateBounded(first, last, oppFirst);
1262 deleteEmptySpans |= sect2->updateBounded(oppFirst, oppLast, first);
caryclark54359292015-03-26 07:52:43 -07001263 this->removeSpanRange(first, last);
1264 sect2->removeSpanRange(oppFirst, oppLast);
1265 first->fEndT = last->fEndT;
1266 first->resetBounds(this->fCurve);
1267 first->fCoinStart.setPerp(fCurve, first->fStartT, first->fPart[0], sect2->fCurve);
1268 first->fCoinEnd.setPerp(fCurve, first->fEndT, first->fPart[TCurve::kPointLast], sect2->fCurve);
1269 oppStartT = first->fCoinStart.perpT();
1270 oppEndT = first->fCoinEnd.perpT();
1271 if (between(0, oppStartT, 1) && between(0, oppEndT, 1)) {
1272 if (!oppMatched) {
1273 SkTSwap(oppStartT, oppEndT);
1274 }
1275 oppFirst->fStartT = oppStartT;
1276 oppFirst->fEndT = oppEndT;
1277 oppFirst->resetBounds(sect2->fCurve);
1278 }
1279 this->validateBounded();
1280 sect2->validateBounded();
1281 last = first->fNext;
Cary Clark38702ab2017-09-05 18:11:55 -04001282 if (!this->removeCoincident(first, false)) {
1283 return false;
1284 }
1285 if (!sect2->removeCoincident(oppFirst, true)) {
1286 return false;
1287 }
caryclark1049f122015-04-20 08:31:59 -07001288 if (deleteEmptySpans) {
caryclarkef7cee42016-09-06 09:05:54 -07001289 if (!this->deleteEmptySpans() || !sect2->deleteEmptySpans()) {
1290 *result = nullptr;
1291 return false;
1292 }
caryclark54359292015-03-26 07:52:43 -07001293 }
1294 this->validate();
1295 sect2->validate();
caryclarkef7cee42016-09-06 09:05:54 -07001296 *result = last && !last->fDeleted && fHead && sect2->fHead ? last : nullptr;
1297 return true;
caryclark54359292015-03-26 07:52:43 -07001298}
1299
caryclark1049f122015-04-20 08:31:59 -07001300template<typename TCurve, typename OppCurve>
1301SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::findCoincidentRun(
1302 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>** lastPtr) {
1303 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001304 SkTSpan<TCurve, OppCurve>* lastCandidate = nullptr;
1305 first = nullptr;
caryclark54359292015-03-26 07:52:43 -07001306 // find the first fully coincident span
1307 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07001308 if (work->fCoinStart.isMatch()) {
caryclark1049f122015-04-20 08:31:59 -07001309#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -07001310 work->validatePerpT(work->fCoinStart.perpT());
1311 work->validatePerpPt(work->fCoinStart.perpT(), work->fCoinStart.perpPt());
caryclark1049f122015-04-20 08:31:59 -07001312#endif
caryclarka35ab3e2016-10-20 08:32:18 -07001313 SkOPASSERT(work->hasOppT(work->fCoinStart.perpT()));
caryclark6c3b9cd2016-09-26 05:36:58 -07001314 if (!work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001315 break;
1316 }
1317 lastCandidate = work;
1318 if (!first) {
1319 first = work;
1320 }
caryclark1049f122015-04-20 08:31:59 -07001321 } else if (first && work->fCollapsed) {
1322 *lastPtr = lastCandidate;
1323 return first;
caryclark54359292015-03-26 07:52:43 -07001324 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001325 lastCandidate = nullptr;
caryclark643ede62016-08-08 14:27:45 -07001326 SkOPASSERT(!first);
caryclark54359292015-03-26 07:52:43 -07001327 }
1328 if (work == *lastPtr) {
1329 return first;
1330 }
1331 work = work->fNext;
caryclarke25a4f62016-07-26 09:26:29 -07001332 if (!work) {
1333 return nullptr;
1334 }
caryclark54359292015-03-26 07:52:43 -07001335 } while (true);
1336 if (lastCandidate) {
1337 *lastPtr = lastCandidate;
1338 }
1339 return first;
1340}
1341
caryclark1049f122015-04-20 08:31:59 -07001342template<typename TCurve, typename OppCurve>
1343int SkTSect<TCurve, OppCurve>::intersects(SkTSpan<TCurve, OppCurve>* span,
1344 SkTSect<OppCurve, TCurve>* opp,
1345 SkTSpan<OppCurve, TCurve>* oppSpan, int* oppResult) {
caryclark54359292015-03-26 07:52:43 -07001346 bool spanStart, oppStart;
1347 int hullResult = span->hullsIntersect(oppSpan, &spanStart, &oppStart);
1348 if (hullResult >= 0) {
1349 if (hullResult == 2) { // hulls have one point in common
1350 if (!span->fBounded || !span->fBounded->fNext) {
1351 SkASSERT(!span->fBounded || span->fBounded->fBounded == oppSpan);
1352 if (spanStart) {
1353 span->fEndT = span->fStartT;
caryclark45fa4472015-01-16 07:04:10 -08001354 } else {
caryclark54359292015-03-26 07:52:43 -07001355 span->fStartT = span->fEndT;
1356 }
1357 } else {
1358 hullResult = 1;
1359 }
1360 if (!oppSpan->fBounded || !oppSpan->fBounded->fNext) {
1361 SkASSERT(!oppSpan->fBounded || oppSpan->fBounded->fBounded == span);
1362 if (oppStart) {
1363 oppSpan->fEndT = oppSpan->fStartT;
1364 } else {
1365 oppSpan->fStartT = oppSpan->fEndT;
1366 }
1367 *oppResult = 2;
1368 } else {
1369 *oppResult = 1;
1370 }
1371 } else {
1372 *oppResult = 1;
1373 }
1374 return hullResult;
1375 }
1376 if (span->fIsLine && oppSpan->fIsLine) {
1377 SkIntersections i;
1378 int sects = this->linesIntersect(span, opp, oppSpan, &i);
caryclark26ad22a2015-10-16 09:03:38 -07001379 if (sects == 2) {
1380 return *oppResult = 1;
1381 }
caryclark54359292015-03-26 07:52:43 -07001382 if (!sects) {
1383 return -1;
1384 }
caryclark34efb702016-10-24 08:19:06 -07001385 this->removedEndCheck(span);
caryclark54359292015-03-26 07:52:43 -07001386 span->fStartT = span->fEndT = i[0][0];
caryclark34efb702016-10-24 08:19:06 -07001387 opp->removedEndCheck(oppSpan);
caryclark54359292015-03-26 07:52:43 -07001388 oppSpan->fStartT = oppSpan->fEndT = i[1][0];
1389 return *oppResult = 2;
1390 }
1391 if (span->fIsLinear || oppSpan->fIsLinear) {
1392 return *oppResult = (int) span->linearsIntersect(oppSpan);
1393 }
1394 return *oppResult = 1;
1395}
1396
caryclarked0935a2015-10-22 07:23:52 -07001397template<typename TCurve>
1398static bool is_parallel(const SkDLine& thisLine, const TCurve& opp) {
1399 if (!opp.IsConic()) {
1400 return false; // FIXME : breaks a lot of stuff now
1401 }
1402 int finds = 0;
1403 SkDLine thisPerp;
1404 thisPerp.fPts[0].fX = thisLine.fPts[1].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1405 thisPerp.fPts[0].fY = thisLine.fPts[1].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1406 thisPerp.fPts[1] = thisLine.fPts[1];
1407 SkIntersections perpRayI;
1408 perpRayI.intersectRay(opp, thisPerp);
1409 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1410 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[1]);
1411 }
1412 thisPerp.fPts[1].fX = thisLine.fPts[0].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1413 thisPerp.fPts[1].fY = thisLine.fPts[0].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1414 thisPerp.fPts[0] = thisLine.fPts[0];
1415 perpRayI.intersectRay(opp, thisPerp);
1416 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1417 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[0]);
1418 }
1419 return finds >= 2;
1420}
1421
caryclark54359292015-03-26 07:52:43 -07001422// while the intersection points are sufficiently far apart:
1423// construct the tangent lines from the intersections
1424// find the point where the tangent line intersects the opposite curve
caryclark1049f122015-04-20 08:31:59 -07001425template<typename TCurve, typename OppCurve>
1426int SkTSect<TCurve, OppCurve>::linesIntersect(SkTSpan<TCurve, OppCurve>* span,
1427 SkTSect<OppCurve, TCurve>* opp,
1428 SkTSpan<OppCurve, TCurve>* oppSpan, SkIntersections* i) {
caryclarka35ab3e2016-10-20 08:32:18 -07001429 SkIntersections thisRayI SkDEBUGCODE((span->fDebugGlobalState));
1430 SkIntersections oppRayI SkDEBUGCODE((span->fDebugGlobalState));
caryclark54359292015-03-26 07:52:43 -07001431 SkDLine thisLine = {{ span->fPart[0], span->fPart[TCurve::kPointLast] }};
caryclark1049f122015-04-20 08:31:59 -07001432 SkDLine oppLine = {{ oppSpan->fPart[0], oppSpan->fPart[OppCurve::kPointLast] }};
caryclark54359292015-03-26 07:52:43 -07001433 int loopCount = 0;
1434 double bestDistSq = DBL_MAX;
caryclark1049f122015-04-20 08:31:59 -07001435 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1436 return 0;
1437 }
1438 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1439 return 0;
1440 }
caryclark26ad22a2015-10-16 09:03:38 -07001441 // if the ends of each line intersect the opposite curve, the lines are coincident
1442 if (thisRayI.used() > 1) {
1443 int ptMatches = 0;
1444 for (int tIndex = 0; tIndex < thisRayI.used(); ++tIndex) {
1445 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(thisLine.fPts); ++lIndex) {
1446 ptMatches += thisRayI.pt(tIndex).approximatelyEqual(thisLine.fPts[lIndex]);
1447 }
1448 }
caryclarked0935a2015-10-22 07:23:52 -07001449 if (ptMatches == 2 || is_parallel(thisLine, opp->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001450 return 2;
1451 }
1452 }
1453 if (oppRayI.used() > 1) {
1454 int ptMatches = 0;
1455 for (int oIndex = 0; oIndex < oppRayI.used(); ++oIndex) {
Cary Clarkd80870f2017-10-17 11:57:26 -04001456 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(oppLine.fPts); ++lIndex) {
caryclark26ad22a2015-10-16 09:03:38 -07001457 ptMatches += oppRayI.pt(oIndex).approximatelyEqual(oppLine.fPts[lIndex]);
1458 }
1459 }
caryclarked0935a2015-10-22 07:23:52 -07001460 if (ptMatches == 2|| is_parallel(oppLine, this->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001461 return 2;
1462 }
1463 }
caryclark54359292015-03-26 07:52:43 -07001464 do {
caryclark54359292015-03-26 07:52:43 -07001465 // pick the closest pair of points
1466 double closest = DBL_MAX;
1467 int closeIndex SK_INIT_TO_AVOID_WARNING;
1468 int oppCloseIndex SK_INIT_TO_AVOID_WARNING;
1469 for (int index = 0; index < oppRayI.used(); ++index) {
1470 if (!roughly_between(span->fStartT, oppRayI[0][index], span->fEndT)) {
1471 continue;
1472 }
1473 for (int oIndex = 0; oIndex < thisRayI.used(); ++oIndex) {
1474 if (!roughly_between(oppSpan->fStartT, thisRayI[0][oIndex], oppSpan->fEndT)) {
1475 continue;
1476 }
1477 double distSq = thisRayI.pt(index).distanceSquared(oppRayI.pt(oIndex));
1478 if (closest > distSq) {
1479 closest = distSq;
1480 closeIndex = index;
1481 oppCloseIndex = oIndex;
caryclarkccec0f92015-03-24 07:28:17 -07001482 }
caryclarkccec0f92015-03-24 07:28:17 -07001483 }
reed0dc4dd62015-03-24 13:55:33 -07001484 }
caryclark54359292015-03-26 07:52:43 -07001485 if (closest == DBL_MAX) {
caryclark1049f122015-04-20 08:31:59 -07001486 break;
reed0dc4dd62015-03-24 13:55:33 -07001487 }
caryclark54359292015-03-26 07:52:43 -07001488 const SkDPoint& oppIPt = thisRayI.pt(oppCloseIndex);
1489 const SkDPoint& iPt = oppRayI.pt(closeIndex);
1490 if (between(span->fStartT, oppRayI[0][closeIndex], span->fEndT)
1491 && between(oppSpan->fStartT, thisRayI[0][oppCloseIndex], oppSpan->fEndT)
1492 && oppIPt.approximatelyEqual(iPt)) {
1493 i->merge(oppRayI, closeIndex, thisRayI, oppCloseIndex);
1494 return i->used();
1495 }
1496 double distSq = oppIPt.distanceSquared(iPt);
1497 if (bestDistSq < distSq || ++loopCount > 5) {
caryclark1049f122015-04-20 08:31:59 -07001498 return 0;
caryclark54359292015-03-26 07:52:43 -07001499 }
1500 bestDistSq = distSq;
caryclark1049f122015-04-20 08:31:59 -07001501 double oppStart = oppRayI[0][closeIndex];
1502 thisLine[0] = fCurve.ptAtT(oppStart);
1503 thisLine[1] = thisLine[0] + fCurve.dxdyAtT(oppStart);
1504 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1505 break;
1506 }
1507 double start = thisRayI[0][oppCloseIndex];
1508 oppLine[0] = opp->fCurve.ptAtT(start);
1509 oppLine[1] = oppLine[0] + opp->fCurve.dxdyAtT(start);
1510 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1511 break;
1512 }
caryclark54359292015-03-26 07:52:43 -07001513 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001514 // convergence may fail if the curves are nearly coincident
1515 SkTCoincident<OppCurve, TCurve> oCoinS, oCoinE;
1516 oCoinS.setPerp(opp->fCurve, oppSpan->fStartT, oppSpan->fPart[0], fCurve);
1517 oCoinE.setPerp(opp->fCurve, oppSpan->fEndT, oppSpan->fPart[OppCurve::kPointLast], fCurve);
1518 double tStart = oCoinS.perpT();
1519 double tEnd = oCoinE.perpT();
1520 bool swap = tStart > tEnd;
1521 if (swap) {
1522 SkTSwap(tStart, tEnd);
1523 }
1524 tStart = SkTMax(tStart, span->fStartT);
1525 tEnd = SkTMin(tEnd, span->fEndT);
1526 if (tStart > tEnd) {
1527 return 0;
1528 }
1529 SkDVector perpS, perpE;
1530 if (tStart == span->fStartT) {
1531 SkTCoincident<TCurve, OppCurve> coinS;
1532 coinS.setPerp(fCurve, span->fStartT, span->fPart[0], opp->fCurve);
1533 perpS = span->fPart[0] - coinS.perpPt();
1534 } else if (swap) {
1535 perpS = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1536 } else {
1537 perpS = oCoinS.perpPt() - oppSpan->fPart[0];
1538 }
1539 if (tEnd == span->fEndT) {
1540 SkTCoincident<TCurve, OppCurve> coinE;
1541 coinE.setPerp(fCurve, span->fEndT, span->fPart[TCurve::kPointLast], opp->fCurve);
1542 perpE = span->fPart[TCurve::kPointLast] - coinE.perpPt();
1543 } else if (swap) {
1544 perpE = oCoinS.perpPt() - oppSpan->fPart[0];
1545 } else {
1546 perpE = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1547 }
1548 if (perpS.dot(perpE) >= 0) {
1549 return 0;
1550 }
1551 SkTCoincident<TCurve, OppCurve> coinW;
1552 double workT = tStart;
1553 double tStep = tEnd - tStart;
1554 SkDPoint workPt;
1555 do {
1556 tStep *= 0.5;
1557 if (precisely_zero(tStep)) {
1558 return 0;
1559 }
1560 workT += tStep;
1561 workPt = fCurve.ptAtT(workT);
1562 coinW.setPerp(fCurve, workT, workPt, opp->fCurve);
caryclark27c015d2016-09-23 05:47:20 -07001563 double perpT = coinW.perpT();
caryclark6c3b9cd2016-09-26 05:36:58 -07001564 if (coinW.isMatch() ? !between(oppSpan->fStartT, perpT, oppSpan->fEndT) : perpT < 0) {
caryclarkb6693002015-12-16 12:28:35 -08001565 continue;
1566 }
caryclark1049f122015-04-20 08:31:59 -07001567 SkDVector perpW = workPt - coinW.perpPt();
1568 if ((perpS.dot(perpW) >= 0) == (tStep < 0)) {
1569 tStep = -tStep;
1570 }
caryclarkb6693002015-12-16 12:28:35 -08001571 if (workPt.approximatelyEqual(coinW.perpPt())) {
1572 break;
1573 }
1574 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001575 double oppTTest = coinW.perpT();
1576 if (!opp->fHead->contains(oppTTest)) {
1577 return 0;
1578 }
1579 i->setMax(1);
1580 i->insert(workT, oppTTest, workPt);
1581 return 1;
caryclark54359292015-03-26 07:52:43 -07001582}
1583
1584
caryclark1049f122015-04-20 08:31:59 -07001585template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001586bool SkTSect<TCurve, OppCurve>::markSpanGone(SkTSpan<TCurve, OppCurve>* span) {
1587 if (--fActiveCount < 0) {
1588 return false;
1589 }
caryclark54359292015-03-26 07:52:43 -07001590 span->fNext = fDeleted;
1591 fDeleted = span;
caryclarke25a4f62016-07-26 09:26:29 -07001592 SkOPASSERT(!span->fDeleted);
caryclark54359292015-03-26 07:52:43 -07001593 span->fDeleted = true;
caryclarkef7cee42016-09-06 09:05:54 -07001594 return true;
caryclark54359292015-03-26 07:52:43 -07001595}
1596
caryclark1049f122015-04-20 08:31:59 -07001597template<typename TCurve, typename OppCurve>
1598bool SkTSect<TCurve, OppCurve>::matchedDirection(double t, const SkTSect<OppCurve, TCurve>* sect2,
1599 double t2) const {
caryclark54359292015-03-26 07:52:43 -07001600 SkDVector dxdy = this->fCurve.dxdyAtT(t);
1601 SkDVector dxdy2 = sect2->fCurve.dxdyAtT(t2);
1602 return dxdy.dot(dxdy2) >= 0;
1603}
1604
caryclark1049f122015-04-20 08:31:59 -07001605template<typename TCurve, typename OppCurve>
1606void SkTSect<TCurve, OppCurve>::matchedDirCheck(double t, const SkTSect<OppCurve, TCurve>* sect2,
1607 double t2, bool* calcMatched, bool* oppMatched) const {
caryclark54359292015-03-26 07:52:43 -07001608 if (*calcMatched) {
caryclark55888e42016-07-18 10:01:36 -07001609 SkASSERT(*oppMatched == this->matchedDirection(t, sect2, t2));
caryclark54359292015-03-26 07:52:43 -07001610 } else {
1611 *oppMatched = this->matchedDirection(t, sect2, t2);
1612 *calcMatched = true;
1613 }
1614}
1615
caryclark1049f122015-04-20 08:31:59 -07001616template<typename TCurve, typename OppCurve>
1617void SkTSect<TCurve, OppCurve>::mergeCoincidence(SkTSect<OppCurve, TCurve>* sect2) {
caryclark54359292015-03-26 07:52:43 -07001618 double smallLimit = 0;
1619 do {
1620 // find the smallest unprocessed span
halcanary96fcdcc2015-08-27 07:41:13 -07001621 SkTSpan<TCurve, OppCurve>* smaller = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001622 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001623 do {
caryclark221a4bb2016-10-07 11:15:15 -07001624 if (!test) {
1625 return;
1626 }
caryclark54359292015-03-26 07:52:43 -07001627 if (test->fStartT < smallLimit) {
1628 continue;
1629 }
1630 if (smaller && smaller->fEndT < test->fStartT) {
1631 continue;
1632 }
1633 smaller = test;
1634 } while ((test = test->fNext));
1635 if (!smaller) {
1636 return;
1637 }
1638 smallLimit = smaller->fEndT;
1639 // find next larger span
halcanary96fcdcc2015-08-27 07:41:13 -07001640 SkTSpan<TCurve, OppCurve>* prior = nullptr;
1641 SkTSpan<TCurve, OppCurve>* larger = nullptr;
1642 SkTSpan<TCurve, OppCurve>* largerPrior = nullptr;
caryclark54359292015-03-26 07:52:43 -07001643 test = fCoincident;
1644 do {
1645 if (test->fStartT < smaller->fEndT) {
1646 continue;
1647 }
caryclark221a4bb2016-10-07 11:15:15 -07001648 SkOPASSERT(test->fStartT != smaller->fEndT);
caryclark54359292015-03-26 07:52:43 -07001649 if (larger && larger->fStartT < test->fStartT) {
1650 continue;
1651 }
1652 largerPrior = prior;
1653 larger = test;
1654 } while ((prior = test), (test = test->fNext));
1655 if (!larger) {
1656 continue;
1657 }
1658 // check middle t value to see if it is coincident as well
1659 double midT = (smaller->fEndT + larger->fStartT) / 2;
1660 SkDPoint midPt = fCurve.ptAtT(midT);
caryclark1049f122015-04-20 08:31:59 -07001661 SkTCoincident<TCurve, OppCurve> coin;
caryclark54359292015-03-26 07:52:43 -07001662 coin.setPerp(fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07001663 if (coin.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001664 smaller->fEndT = larger->fEndT;
1665 smaller->fCoinEnd = larger->fCoinEnd;
1666 if (largerPrior) {
1667 largerPrior->fNext = larger->fNext;
caryclark643ede62016-08-08 14:27:45 -07001668 largerPrior->validate();
caryclark54359292015-03-26 07:52:43 -07001669 } else {
1670 fCoincident = larger->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001671 }
1672 }
caryclark54359292015-03-26 07:52:43 -07001673 } while (true);
1674}
1675
caryclark1049f122015-04-20 08:31:59 -07001676template<typename TCurve, typename OppCurve>
1677SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::prev(
1678 SkTSpan<TCurve, OppCurve>* span) const {
halcanary96fcdcc2015-08-27 07:41:13 -07001679 SkTSpan<TCurve, OppCurve>* result = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001680 SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001681 while (span != test) {
1682 result = test;
1683 test = test->fNext;
1684 SkASSERT(test);
caryclarkccec0f92015-03-24 07:28:17 -07001685 }
caryclark55888e42016-07-18 10:01:36 -07001686 return result;
caryclarkccec0f92015-03-24 07:28:17 -07001687}
1688
caryclark1049f122015-04-20 08:31:59 -07001689template<typename TCurve, typename OppCurve>
1690void SkTSect<TCurve, OppCurve>::recoverCollapsed() {
1691 SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001692 while (deleted) {
caryclark1049f122015-04-20 08:31:59 -07001693 SkTSpan<TCurve, OppCurve>* delNext = deleted->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001694 if (deleted->fCollapsed) {
caryclark1049f122015-04-20 08:31:59 -07001695 SkTSpan<TCurve, OppCurve>** spanPtr = &fHead;
caryclark45fa4472015-01-16 07:04:10 -08001696 while (*spanPtr && (*spanPtr)->fEndT <= deleted->fStartT) {
1697 spanPtr = &(*spanPtr)->fNext;
1698 }
1699 deleted->fNext = *spanPtr;
1700 *spanPtr = deleted;
1701 }
1702 deleted = delNext;
1703 }
1704}
1705
caryclark1049f122015-04-20 08:31:59 -07001706template<typename TCurve, typename OppCurve>
1707void SkTSect<TCurve, OppCurve>::removeAllBut(const SkTSpan<OppCurve, TCurve>* keep,
1708 SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp) {
1709 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001710 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001711 SkTSpan<OppCurve, TCurve>* bounded = testBounded->fBounded;
1712 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001713 // may have been deleted when opp did 'remove all but'
1714 if (bounded != keep && !bounded->fDeleted) {
1715 SkAssertResult(SkDEBUGCODE(!) span->removeBounded(bounded));
1716 if (bounded->removeBounded(span)) {
1717 opp->removeSpan(bounded);
1718 }
caryclarkccec0f92015-03-24 07:28:17 -07001719 }
caryclark54359292015-03-26 07:52:43 -07001720 testBounded = next;
caryclarkccec0f92015-03-24 07:28:17 -07001721 }
caryclark54359292015-03-26 07:52:43 -07001722 SkASSERT(!span->fDeleted);
1723 SkASSERT(span->findOppSpan(keep));
1724 SkASSERT(keep->findOppSpan(span));
caryclarkccec0f92015-03-24 07:28:17 -07001725}
1726
caryclark1049f122015-04-20 08:31:59 -07001727template<typename TCurve, typename OppCurve>
1728void SkTSect<TCurve, OppCurve>::removeByPerpendicular(SkTSect<OppCurve, TCurve>* opp) {
1729 SkTSpan<TCurve, OppCurve>* test = fHead;
1730 SkTSpan<TCurve, OppCurve>* next;
caryclark54359292015-03-26 07:52:43 -07001731 do {
1732 next = test->fNext;
1733 if (test->fCoinStart.perpT() < 0 || test->fCoinEnd.perpT() < 0) {
1734 continue;
reed0dc4dd62015-03-24 13:55:33 -07001735 }
caryclark54359292015-03-26 07:52:43 -07001736 SkDVector startV = test->fCoinStart.perpPt() - test->fPart[0];
1737 SkDVector endV = test->fCoinEnd.perpPt() - test->fPart[TCurve::kPointLast];
1738#if DEBUG_T_SECT
1739 SkDebugf("%s startV=(%1.9g,%1.9g) endV=(%1.9g,%1.9g) dot=%1.9g\n", __FUNCTION__,
1740 startV.fX, startV.fY, endV.fX, endV.fY, startV.dot(endV));
1741#endif
1742 if (startV.dot(endV) <= 0) {
1743 continue;
1744 }
1745 this->removeSpans(test, opp);
1746 } while ((test = next));
1747}
1748
caryclark1049f122015-04-20 08:31:59 -07001749template<typename TCurve, typename OppCurve>
Cary Clark38702ab2017-09-05 18:11:55 -04001750bool SkTSect<TCurve, OppCurve>::removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween) {
1751 if (!this->unlinkSpan(span)) {
1752 return false;
1753 }
caryclark54359292015-03-26 07:52:43 -07001754 if (isBetween || between(0, span->fCoinStart.perpT(), 1)) {
1755 --fActiveCount;
1756 span->fNext = fCoincident;
1757 fCoincident = span;
1758 } else {
1759 this->markSpanGone(span);
reed0dc4dd62015-03-24 13:55:33 -07001760 }
Cary Clark38702ab2017-09-05 18:11:55 -04001761 return true;
caryclarkccec0f92015-03-24 07:28:17 -07001762}
1763
caryclark1049f122015-04-20 08:31:59 -07001764template<typename TCurve, typename OppCurve>
caryclark34efb702016-10-24 08:19:06 -07001765void SkTSect<TCurve, OppCurve>::removedEndCheck(SkTSpan<TCurve, OppCurve>* span) {
caryclark6c3b9cd2016-09-26 05:36:58 -07001766 if (!span->fStartT) {
1767 fRemovedStartT = true;
1768 }
1769 if (1 == span->fEndT) {
1770 fRemovedEndT = true;
1771 }
caryclark34efb702016-10-24 08:19:06 -07001772}
1773
1774template<typename TCurve, typename OppCurve>
1775bool SkTSect<TCurve, OppCurve>::removeSpan(SkTSpan<TCurve, OppCurve>* span) {\
1776 this->removedEndCheck(span);
Cary Clark38702ab2017-09-05 18:11:55 -04001777 if (!this->unlinkSpan(span)) {
1778 return false;
1779 }
caryclarkef7cee42016-09-06 09:05:54 -07001780 return this->markSpanGone(span);
caryclark54359292015-03-26 07:52:43 -07001781}
1782
caryclark1049f122015-04-20 08:31:59 -07001783template<typename TCurve, typename OppCurve>
1784void SkTSect<TCurve, OppCurve>::removeSpanRange(SkTSpan<TCurve, OppCurve>* first,
1785 SkTSpan<TCurve, OppCurve>* last) {
caryclark54359292015-03-26 07:52:43 -07001786 if (first == last) {
1787 return;
1788 }
caryclark1049f122015-04-20 08:31:59 -07001789 SkTSpan<TCurve, OppCurve>* span = first;
caryclark54359292015-03-26 07:52:43 -07001790 SkASSERT(span);
caryclark1049f122015-04-20 08:31:59 -07001791 SkTSpan<TCurve, OppCurve>* final = last->fNext;
1792 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001793 while ((span = next) && span != final) {
1794 next = span->fNext;
1795 this->markSpanGone(span);
1796 }
1797 if (final) {
1798 final->fPrev = first;
1799 }
1800 first->fNext = final;
Cary Clarkb8421ed2018-03-14 15:55:02 -04001801 // world may not be ready for validation here
caryclark643ede62016-08-08 14:27:45 -07001802 first->validate();
caryclark54359292015-03-26 07:52:43 -07001803}
1804
caryclark1049f122015-04-20 08:31:59 -07001805template<typename TCurve, typename OppCurve>
1806void SkTSect<TCurve, OppCurve>::removeSpans(SkTSpan<TCurve, OppCurve>* span,
1807 SkTSect<OppCurve, TCurve>* opp) {
1808 SkTSpanBounded<OppCurve, TCurve>* bounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001809 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -07001810 SkTSpan<OppCurve, TCurve>* spanBounded = bounded->fBounded;
1811 SkTSpanBounded<OppCurve, TCurve>* next = bounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001812 if (span->removeBounded(spanBounded)) { // shuffles last into position 0
1813 this->removeSpan(span);
1814 }
1815 if (spanBounded->removeBounded(span)) {
1816 opp->removeSpan(spanBounded);
1817 }
1818 SkASSERT(!span->fDeleted || !opp->debugHasBounded(span));
1819 bounded = next;
reed0dc4dd62015-03-24 13:55:33 -07001820 }
1821}
caryclarkccec0f92015-03-24 07:28:17 -07001822
caryclark1049f122015-04-20 08:31:59 -07001823template<typename TCurve, typename OppCurve>
1824SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::spanAtT(double t,
1825 SkTSpan<TCurve, OppCurve>** priorSpan) {
1826 SkTSpan<TCurve, OppCurve>* test = fHead;
halcanary96fcdcc2015-08-27 07:41:13 -07001827 SkTSpan<TCurve, OppCurve>* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -07001828 while (test && test->fEndT < t) {
1829 prev = test;
1830 test = test->fNext;
reed0dc4dd62015-03-24 13:55:33 -07001831 }
caryclark54359292015-03-26 07:52:43 -07001832 *priorSpan = prev;
halcanary96fcdcc2015-08-27 07:41:13 -07001833 return test && test->fStartT <= t ? test : nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001834}
1835
caryclark1049f122015-04-20 08:31:59 -07001836template<typename TCurve, typename OppCurve>
1837SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::tail() {
1838 SkTSpan<TCurve, OppCurve>* result = fHead;
1839 SkTSpan<TCurve, OppCurve>* next = fHead;
reed0dc4dd62015-03-24 13:55:33 -07001840 while ((next = next->fNext)) {
1841 if (next->fEndT > result->fEndT) {
1842 result = next;
1843 }
1844 }
1845 return result;
1846}
1847
1848/* Each span has a range of opposite spans it intersects. After the span is split in two,
1849 adjust the range to its new size */
caryclark1049f122015-04-20 08:31:59 -07001850template<typename TCurve, typename OppCurve>
caryclarka35ab3e2016-10-20 08:32:18 -07001851bool SkTSect<TCurve, OppCurve>::trim(SkTSpan<TCurve, OppCurve>* span,
caryclark1049f122015-04-20 08:31:59 -07001852 SkTSect<OppCurve, TCurve>* opp) {
caryclarka35ab3e2016-10-20 08:32:18 -07001853 FAIL_IF(!span->initBounds(fCurve));
caryclark1049f122015-04-20 08:31:59 -07001854 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001855 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001856 SkTSpan<OppCurve, TCurve>* test = testBounded->fBounded;
1857 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001858 int oppSects, sects = this->intersects(span, opp, test, &oppSects);
1859 if (sects >= 1) {
1860 if (oppSects == 2) {
1861 test->initBounds(opp->fCurve);
1862 opp->removeAllBut(span, test, this);
1863 }
1864 if (sects == 2) {
1865 span->initBounds(fCurve);
1866 this->removeAllBut(test, span, opp);
caryclarka35ab3e2016-10-20 08:32:18 -07001867 return true;
caryclark54359292015-03-26 07:52:43 -07001868 }
reed0dc4dd62015-03-24 13:55:33 -07001869 } else {
caryclark54359292015-03-26 07:52:43 -07001870 if (span->removeBounded(test)) {
1871 this->removeSpan(span);
1872 }
1873 if (test->removeBounded(span)) {
1874 opp->removeSpan(test);
1875 }
1876 }
1877 testBounded = next;
1878 }
caryclarka35ab3e2016-10-20 08:32:18 -07001879 return true;
caryclark54359292015-03-26 07:52:43 -07001880}
1881
caryclark1049f122015-04-20 08:31:59 -07001882template<typename TCurve, typename OppCurve>
Cary Clark38702ab2017-09-05 18:11:55 -04001883bool SkTSect<TCurve, OppCurve>::unlinkSpan(SkTSpan<TCurve, OppCurve>* span) {
caryclark1049f122015-04-20 08:31:59 -07001884 SkTSpan<TCurve, OppCurve>* prev = span->fPrev;
1885 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001886 if (prev) {
1887 prev->fNext = next;
1888 if (next) {
1889 next->fPrev = prev;
Cary Clark38702ab2017-09-05 18:11:55 -04001890 if (next->fStartT > next->fEndT) {
1891 return false;
1892 }
Cary Clarkb8421ed2018-03-14 15:55:02 -04001893 // world may not be ready for validate here
caryclark643ede62016-08-08 14:27:45 -07001894 next->validate();
caryclark54359292015-03-26 07:52:43 -07001895 }
1896 } else {
1897 fHead = next;
1898 if (next) {
halcanary96fcdcc2015-08-27 07:41:13 -07001899 next->fPrev = nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001900 }
1901 }
Cary Clark38702ab2017-09-05 18:11:55 -04001902 return true;
reed0dc4dd62015-03-24 13:55:33 -07001903}
1904
caryclark1049f122015-04-20 08:31:59 -07001905template<typename TCurve, typename OppCurve>
1906bool SkTSect<TCurve, OppCurve>::updateBounded(SkTSpan<TCurve, OppCurve>* first,
1907 SkTSpan<TCurve, OppCurve>* last, SkTSpan<OppCurve, TCurve>* oppFirst) {
1908 SkTSpan<TCurve, OppCurve>* test = first;
1909 const SkTSpan<TCurve, OppCurve>* final = last->next();
caryclark54359292015-03-26 07:52:43 -07001910 bool deleteSpan = false;
1911 do {
1912 deleteSpan |= test->removeAllBounded();
caryclarke25a4f62016-07-26 09:26:29 -07001913 } while ((test = test->fNext) != final && test);
halcanary96fcdcc2015-08-27 07:41:13 -07001914 first->fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -07001915 first->addBounded(oppFirst, &fHeap);
1916 // cannot call validate until remove span range is called
1917 return deleteSpan;
1918}
1919
1920
caryclark1049f122015-04-20 08:31:59 -07001921template<typename TCurve, typename OppCurve>
1922void SkTSect<TCurve, OppCurve>::validate() const {
caryclark643ede62016-08-08 14:27:45 -07001923#if DEBUG_VALIDATE
caryclark45fa4472015-01-16 07:04:10 -08001924 int count = 0;
caryclark643ede62016-08-08 14:27:45 -07001925 double last = 0;
caryclark45fa4472015-01-16 07:04:10 -08001926 if (fHead) {
caryclark1049f122015-04-20 08:31:59 -07001927 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark45fa4472015-01-16 07:04:10 -08001928 SkASSERT(!span->fPrev);
caryclark643ede62016-08-08 14:27:45 -07001929 const SkTSpan<TCurve, OppCurve>* next;
caryclark45fa4472015-01-16 07:04:10 -08001930 do {
1931 span->validate();
1932 SkASSERT(span->fStartT >= last);
caryclark643ede62016-08-08 14:27:45 -07001933 last = span->fEndT;
caryclark45fa4472015-01-16 07:04:10 -08001934 ++count;
caryclark643ede62016-08-08 14:27:45 -07001935 next = span->fNext;
1936 SkASSERT(next != span);
1937 } while ((span = next) != nullptr);
caryclark45fa4472015-01-16 07:04:10 -08001938 }
1939 SkASSERT(count == fActiveCount);
caryclark643ede62016-08-08 14:27:45 -07001940#endif
1941#if DEBUG_T_SECT
caryclark45fa4472015-01-16 07:04:10 -08001942 SkASSERT(fActiveCount <= fDebugAllocatedCount);
1943 int deletedCount = 0;
caryclark1049f122015-04-20 08:31:59 -07001944 const SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001945 while (deleted) {
1946 ++deletedCount;
1947 deleted = deleted->fNext;
1948 }
caryclark1049f122015-04-20 08:31:59 -07001949 const SkTSpan<TCurve, OppCurve>* coincident = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001950 while (coincident) {
1951 ++deletedCount;
1952 coincident = coincident->fNext;
1953 }
caryclark45fa4472015-01-16 07:04:10 -08001954 SkASSERT(fActiveCount + deletedCount == fDebugAllocatedCount);
caryclarkccec0f92015-03-24 07:28:17 -07001955#endif
caryclark54359292015-03-26 07:52:43 -07001956}
1957
caryclark1049f122015-04-20 08:31:59 -07001958template<typename TCurve, typename OppCurve>
1959void SkTSect<TCurve, OppCurve>::validateBounded() const {
caryclark643ede62016-08-08 14:27:45 -07001960#if DEBUG_VALIDATE
caryclark54359292015-03-26 07:52:43 -07001961 if (!fHead) {
1962 return;
1963 }
caryclark1049f122015-04-20 08:31:59 -07001964 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark54359292015-03-26 07:52:43 -07001965 do {
1966 span->validateBounded();
halcanary96fcdcc2015-08-27 07:41:13 -07001967 } while ((span = span->fNext) != nullptr);
caryclark54359292015-03-26 07:52:43 -07001968#endif
1969}
caryclark45fa4472015-01-16 07:04:10 -08001970
caryclark1049f122015-04-20 08:31:59 -07001971template<typename TCurve, typename OppCurve>
1972int SkTSect<TCurve, OppCurve>::EndsEqual(const SkTSect<TCurve, OppCurve>* sect1,
1973 const SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark45fa4472015-01-16 07:04:10 -08001974 int zeroOneSet = 0;
caryclark54359292015-03-26 07:52:43 -07001975 if (sect1->fCurve[0] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001976 zeroOneSet |= kZeroS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001977 intersections->insert(0, 0, sect1->fCurve[0]);
1978 }
caryclark1049f122015-04-20 08:31:59 -07001979 if (sect1->fCurve[0] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001980 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark54359292015-03-26 07:52:43 -07001981 intersections->insert(0, 1, sect1->fCurve[0]);
1982 }
1983 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001984 zeroOneSet |= kOneS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001985 intersections->insert(1, 0, sect1->fCurve[TCurve::kPointLast]);
1986 }
caryclark1049f122015-04-20 08:31:59 -07001987 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001988 zeroOneSet |= kOneS1Set | kOneS2Set;
reed0dc4dd62015-03-24 13:55:33 -07001989 intersections->insert(1, 1, sect1->fCurve[TCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001990 }
1991 // check for zero
1992 if (!(zeroOneSet & (kZeroS1Set | kZeroS2Set))
1993 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[0])) {
1994 zeroOneSet |= kZeroS1Set | kZeroS2Set;
1995 intersections->insertNear(0, 0, sect1->fCurve[0], sect2->fCurve[0]);
1996 }
1997 if (!(zeroOneSet & (kZeroS1Set | kOneS2Set))
caryclark1049f122015-04-20 08:31:59 -07001998 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07001999 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark1049f122015-04-20 08:31:59 -07002000 intersections->insertNear(0, 1, sect1->fCurve[0], sect2->fCurve[OppCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07002001 }
2002 // check for one
2003 if (!(zeroOneSet & (kOneS1Set | kZeroS2Set))
2004 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[0])) {
2005 zeroOneSet |= kOneS1Set | kZeroS2Set;
2006 intersections->insertNear(1, 0, sect1->fCurve[TCurve::kPointLast], sect2->fCurve[0]);
2007 }
2008 if (!(zeroOneSet & (kOneS1Set | kOneS2Set))
2009 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[
caryclark1049f122015-04-20 08:31:59 -07002010 OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07002011 zeroOneSet |= kOneS1Set | kOneS2Set;
2012 intersections->insertNear(1, 1, sect1->fCurve[TCurve::kPointLast],
caryclark1049f122015-04-20 08:31:59 -07002013 sect2->fCurve[OppCurve::kPointLast]);
caryclark45fa4472015-01-16 07:04:10 -08002014 }
2015 return zeroOneSet;
2016}
2017
caryclark1049f122015-04-20 08:31:59 -07002018template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08002019struct SkClosestRecord {
caryclark54359292015-03-26 07:52:43 -07002020 bool operator<(const SkClosestRecord& rh) const {
2021 return fClosest < rh.fClosest;
2022 }
2023
caryclark45fa4472015-01-16 07:04:10 -08002024 void addIntersection(SkIntersections* intersections) const {
2025 double r1t = fC1Index ? fC1Span->endT() : fC1Span->startT();
2026 double r2t = fC2Index ? fC2Span->endT() : fC2Span->startT();
2027 intersections->insert(r1t, r2t, fC1Span->part()[fC1Index]);
2028 }
2029
caryclark1049f122015-04-20 08:31:59 -07002030 void findEnd(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2,
caryclark45fa4472015-01-16 07:04:10 -08002031 int c1Index, int c2Index) {
2032 const TCurve& c1 = span1->part();
caryclark1049f122015-04-20 08:31:59 -07002033 const OppCurve& c2 = span2->part();
caryclark45fa4472015-01-16 07:04:10 -08002034 if (!c1[c1Index].approximatelyEqual(c2[c2Index])) {
2035 return;
2036 }
2037 double dist = c1[c1Index].distanceSquared(c2[c2Index]);
2038 if (fClosest < dist) {
2039 return;
2040 }
2041 fC1Span = span1;
2042 fC2Span = span2;
2043 fC1StartT = span1->startT();
2044 fC1EndT = span1->endT();
2045 fC2StartT = span2->startT();
2046 fC2EndT = span2->endT();
2047 fC1Index = c1Index;
2048 fC2Index = c2Index;
2049 fClosest = dist;
2050 }
2051
caryclarkcdeff812016-07-22 03:34:19 -07002052 bool matesWith(const SkClosestRecord& mate SkDEBUGPARAMS(SkIntersections* i)) const {
Cary Clark67116382017-01-03 16:25:18 -05002053 SkOPOBJASSERT(i, fC1Span == mate.fC1Span || fC1Span->endT() <= mate.fC1Span->startT()
caryclark45fa4472015-01-16 07:04:10 -08002054 || mate.fC1Span->endT() <= fC1Span->startT());
caryclarkcdeff812016-07-22 03:34:19 -07002055 SkOPOBJASSERT(i, fC2Span == mate.fC2Span || fC2Span->endT() <= mate.fC2Span->startT()
caryclark45fa4472015-01-16 07:04:10 -08002056 || mate.fC2Span->endT() <= fC2Span->startT());
2057 return fC1Span == mate.fC1Span || fC1Span->endT() == mate.fC1Span->startT()
2058 || fC1Span->startT() == mate.fC1Span->endT()
2059 || fC2Span == mate.fC2Span
2060 || fC2Span->endT() == mate.fC2Span->startT()
2061 || fC2Span->startT() == mate.fC2Span->endT();
2062 }
2063
2064 void merge(const SkClosestRecord& mate) {
2065 fC1Span = mate.fC1Span;
2066 fC2Span = mate.fC2Span;
2067 fClosest = mate.fClosest;
2068 fC1Index = mate.fC1Index;
2069 fC2Index = mate.fC2Index;
2070 }
caryclark55888e42016-07-18 10:01:36 -07002071
caryclark45fa4472015-01-16 07:04:10 -08002072 void reset() {
2073 fClosest = FLT_MAX;
halcanary96fcdcc2015-08-27 07:41:13 -07002074 SkDEBUGCODE(fC1Span = nullptr);
2075 SkDEBUGCODE(fC2Span = nullptr);
caryclark45fa4472015-01-16 07:04:10 -08002076 SkDEBUGCODE(fC1Index = fC2Index = -1);
2077 }
2078
2079 void update(const SkClosestRecord& mate) {
2080 fC1StartT = SkTMin(fC1StartT, mate.fC1StartT);
2081 fC1EndT = SkTMax(fC1EndT, mate.fC1EndT);
2082 fC2StartT = SkTMin(fC2StartT, mate.fC2StartT);
2083 fC2EndT = SkTMax(fC2EndT, mate.fC2EndT);
2084 }
2085
caryclark1049f122015-04-20 08:31:59 -07002086 const SkTSpan<TCurve, OppCurve>* fC1Span;
2087 const SkTSpan<OppCurve, TCurve>* fC2Span;
caryclark45fa4472015-01-16 07:04:10 -08002088 double fC1StartT;
2089 double fC1EndT;
2090 double fC2StartT;
2091 double fC2EndT;
2092 double fClosest;
2093 int fC1Index;
2094 int fC2Index;
2095};
2096
caryclark1049f122015-04-20 08:31:59 -07002097template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08002098struct SkClosestSect {
2099 SkClosestSect()
2100 : fUsed(0) {
2101 fClosest.push_back().reset();
2102 }
2103
caryclarkcdeff812016-07-22 03:34:19 -07002104 bool find(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2
2105 SkDEBUGPARAMS(SkIntersections* i)) {
caryclark1049f122015-04-20 08:31:59 -07002106 SkClosestRecord<TCurve, OppCurve>* record = &fClosest[fUsed];
caryclark45fa4472015-01-16 07:04:10 -08002107 record->findEnd(span1, span2, 0, 0);
caryclark1049f122015-04-20 08:31:59 -07002108 record->findEnd(span1, span2, 0, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002109 record->findEnd(span1, span2, TCurve::kPointLast, 0);
caryclark1049f122015-04-20 08:31:59 -07002110 record->findEnd(span1, span2, TCurve::kPointLast, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002111 if (record->fClosest == FLT_MAX) {
caryclark54359292015-03-26 07:52:43 -07002112 return false;
caryclark45fa4472015-01-16 07:04:10 -08002113 }
2114 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002115 SkClosestRecord<TCurve, OppCurve>* test = &fClosest[index];
caryclarkcdeff812016-07-22 03:34:19 -07002116 if (test->matesWith(*record SkDEBUGPARAMS(i))) {
caryclark45fa4472015-01-16 07:04:10 -08002117 if (test->fClosest > record->fClosest) {
2118 test->merge(*record);
2119 }
2120 test->update(*record);
2121 record->reset();
caryclark54359292015-03-26 07:52:43 -07002122 return false;
caryclark45fa4472015-01-16 07:04:10 -08002123 }
2124 }
2125 ++fUsed;
2126 fClosest.push_back().reset();
caryclark54359292015-03-26 07:52:43 -07002127 return true;
caryclark45fa4472015-01-16 07:04:10 -08002128 }
2129
2130 void finish(SkIntersections* intersections) const {
caryclark1049f122015-04-20 08:31:59 -07002131 SkSTArray<TCurve::kMaxIntersections * 3,
2132 const SkClosestRecord<TCurve, OppCurve>*, true> closestPtrs;
caryclark45fa4472015-01-16 07:04:10 -08002133 for (int index = 0; index < fUsed; ++index) {
caryclark54359292015-03-26 07:52:43 -07002134 closestPtrs.push_back(&fClosest[index]);
2135 }
caryclark1049f122015-04-20 08:31:59 -07002136 SkTQSort<const SkClosestRecord<TCurve, OppCurve> >(closestPtrs.begin(), closestPtrs.end()
2137 - 1);
caryclark54359292015-03-26 07:52:43 -07002138 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002139 const SkClosestRecord<TCurve, OppCurve>* test = closestPtrs[index];
caryclark54359292015-03-26 07:52:43 -07002140 test->addIntersection(intersections);
caryclark45fa4472015-01-16 07:04:10 -08002141 }
2142 }
2143
caryclark54359292015-03-26 07:52:43 -07002144 // this is oversized so that an extra records can merge into final one
caryclark1049f122015-04-20 08:31:59 -07002145 SkSTArray<TCurve::kMaxIntersections * 2, SkClosestRecord<TCurve, OppCurve>, true> fClosest;
caryclark45fa4472015-01-16 07:04:10 -08002146 int fUsed;
2147};
2148
2149// returns true if the rect is too small to consider
caryclark1049f122015-04-20 08:31:59 -07002150template<typename TCurve, typename OppCurve>
2151void SkTSect<TCurve, OppCurve>::BinarySearch(SkTSect<TCurve, OppCurve>* sect1,
2152 SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark54359292015-03-26 07:52:43 -07002153#if DEBUG_T_SECT_DUMP > 1
2154 gDumpTSectNum = 0;
2155#endif
caryclark1049f122015-04-20 08:31:59 -07002156 SkDEBUGCODE(sect1->fOppSect = sect2);
2157 SkDEBUGCODE(sect2->fOppSect = sect1);
caryclark45fa4472015-01-16 07:04:10 -08002158 intersections->reset();
caryclarka35ab3e2016-10-20 08:32:18 -07002159 intersections->setMax(TCurve::kMaxIntersections + 4); // give extra for slop
caryclark1049f122015-04-20 08:31:59 -07002160 SkTSpan<TCurve, OppCurve>* span1 = sect1->fHead;
2161 SkTSpan<OppCurve, TCurve>* span2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002162 int oppSect, sect = sect1->intersects(span1, sect2, span2, &oppSect);
2163// SkASSERT(between(0, sect, 2));
2164 if (!sect) {
caryclark45fa4472015-01-16 07:04:10 -08002165 return;
2166 }
caryclark54359292015-03-26 07:52:43 -07002167 if (sect == 2 && oppSect == 2) {
caryclark45fa4472015-01-16 07:04:10 -08002168 (void) EndsEqual(sect1, sect2, intersections);
2169 return;
2170 }
caryclark54359292015-03-26 07:52:43 -07002171 span1->addBounded(span2, &sect1->fHeap);
2172 span2->addBounded(span1, &sect2->fHeap);
caryclark26ad22a2015-10-16 09:03:38 -07002173 const int kMaxCoinLoopCount = 8;
2174 int coinLoopCount = kMaxCoinLoopCount;
2175 double start1s SK_INIT_TO_AVOID_WARNING;
2176 double start1e SK_INIT_TO_AVOID_WARNING;
caryclark45fa4472015-01-16 07:04:10 -08002177 do {
2178 // find the largest bounds
caryclark1049f122015-04-20 08:31:59 -07002179 SkTSpan<TCurve, OppCurve>* largest1 = sect1->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002180 if (!largest1) {
2181 break;
2182 }
caryclark1049f122015-04-20 08:31:59 -07002183 SkTSpan<OppCurve, TCurve>* largest2 = sect2->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002184 // split it
caryclark1049f122015-04-20 08:31:59 -07002185 if (!largest2 || (largest1 && (largest1->fBoundsMax > largest2->fBoundsMax
2186 || (!largest1->fCollapsed && largest2->fCollapsed)))) {
2187 if (largest1->fCollapsed) {
2188 break;
2189 }
caryclark34efb702016-10-24 08:19:06 -07002190 sect1->resetRemovedEnds();
2191 sect2->resetRemovedEnds();
caryclark1049f122015-04-20 08:31:59 -07002192 // trim parts that don't intersect the opposite
2193 SkTSpan<TCurve, OppCurve>* half1 = sect1->addOne();
caryclark643ede62016-08-08 14:27:45 -07002194 SkDEBUGCODE(half1->debugSetGlobalState(sect1->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002195 if (!half1->split(largest1, &sect1->fHeap)) {
2196 break;
2197 }
caryclarka35ab3e2016-10-20 08:32:18 -07002198 if (!sect1->trim(largest1, sect2)) {
2199 SkOPOBJASSERT(intersections, 0);
2200 return;
2201 }
2202 if (!sect1->trim(half1, sect2)) {
2203 SkOPOBJASSERT(intersections, 0);
2204 return;
2205 }
caryclark1049f122015-04-20 08:31:59 -07002206 } else {
2207 if (largest2->fCollapsed) {
2208 break;
2209 }
caryclark34efb702016-10-24 08:19:06 -07002210 sect1->resetRemovedEnds();
2211 sect2->resetRemovedEnds();
caryclark1049f122015-04-20 08:31:59 -07002212 // trim parts that don't intersect the opposite
2213 SkTSpan<OppCurve, TCurve>* half2 = sect2->addOne();
caryclark643ede62016-08-08 14:27:45 -07002214 SkDEBUGCODE(half2->debugSetGlobalState(sect2->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002215 if (!half2->split(largest2, &sect2->fHeap)) {
2216 break;
2217 }
caryclarka35ab3e2016-10-20 08:32:18 -07002218 if (!sect2->trim(largest2, sect1)) {
2219 SkOPOBJASSERT(intersections, 0);
2220 return;
2221 }
2222 if (!sect2->trim(half2, sect1)) {
2223 SkOPOBJASSERT(intersections, 0);
2224 return;
2225 }
caryclark45fa4472015-01-16 07:04:10 -08002226 }
caryclark54359292015-03-26 07:52:43 -07002227 sect1->validate();
2228 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002229#if DEBUG_T_SECT_LOOP_COUNT
2230 intersections->debugBumpLoopCount(SkIntersections::kIterations_DebugLoop);
2231#endif
caryclark45fa4472015-01-16 07:04:10 -08002232 // if there are 9 or more continuous spans on both sects, suspect coincidence
2233 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2234 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
caryclark26ad22a2015-10-16 09:03:38 -07002235 if (coinLoopCount == kMaxCoinLoopCount) {
2236 start1s = sect1->fHead->fStartT;
2237 start1e = sect1->tail()->fEndT;
2238 }
caryclarkef7cee42016-09-06 09:05:54 -07002239 if (!sect1->coincidentCheck(sect2)) {
2240 return;
2241 }
caryclark54359292015-03-26 07:52:43 -07002242 sect1->validate();
2243 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002244#if DEBUG_T_SECT_LOOP_COUNT
2245 intersections->debugBumpLoopCount(SkIntersections::kCoinCheck_DebugLoop);
2246#endif
caryclarkef784fb2015-10-30 12:03:06 -07002247 if (!--coinLoopCount && sect1->fHead && sect2->fHead) {
caryclark26ad22a2015-10-16 09:03:38 -07002248 /* All known working cases resolve in two tries. Sadly, cubicConicTests[0]
2249 gets stuck in a loop. It adds an extension to allow a coincident end
2250 perpendicular to track its intersection in the opposite curve. However,
2251 the bounding box of the extension does not intersect the original curve,
caryclark55888e42016-07-18 10:01:36 -07002252 so the extension is discarded, only to be added again the next time around. */
caryclark26ad22a2015-10-16 09:03:38 -07002253 sect1->coincidentForce(sect2, start1s, start1e);
2254 sect1->validate();
2255 sect2->validate();
2256 }
caryclark45fa4472015-01-16 07:04:10 -08002257 }
caryclark54359292015-03-26 07:52:43 -07002258 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2259 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
Cary Clark59ed4822016-12-08 16:17:56 -05002260 if (!sect1->fHead) {
2261 return;
2262 }
caryclark54359292015-03-26 07:52:43 -07002263 sect1->computePerpendiculars(sect2, sect1->fHead, sect1->tail());
Cary Clark59ed4822016-12-08 16:17:56 -05002264 if (!sect2->fHead) {
2265 return;
2266 }
caryclark54359292015-03-26 07:52:43 -07002267 sect2->computePerpendiculars(sect1, sect2->fHead, sect2->tail());
2268 sect1->removeByPerpendicular(sect2);
2269 sect1->validate();
2270 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002271#if DEBUG_T_SECT_LOOP_COUNT
2272 intersections->debugBumpLoopCount(SkIntersections::kComputePerp_DebugLoop);
2273#endif
caryclark1049f122015-04-20 08:31:59 -07002274 if (sect1->collapsed() > TCurve::kMaxIntersections) {
2275 break;
2276 }
caryclark54359292015-03-26 07:52:43 -07002277 }
2278#if DEBUG_T_SECT_DUMP
2279 sect1->dumpBoth(sect2);
caryclark45fa4472015-01-16 07:04:10 -08002280#endif
2281 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002282 break;
caryclark45fa4472015-01-16 07:04:10 -08002283 }
2284 } while (true);
caryclark1049f122015-04-20 08:31:59 -07002285 SkTSpan<TCurve, OppCurve>* coincident = sect1->fCoincident;
caryclark54359292015-03-26 07:52:43 -07002286 if (coincident) {
2287 // if there is more than one coincident span, check loosely to see if they should be joined
2288 if (coincident->fNext) {
2289 sect1->mergeCoincidence(sect2);
2290 coincident = sect1->fCoincident;
2291 }
2292 SkASSERT(sect2->fCoincident); // courtesy check : coincidence only looks at sect 1
caryclark45fa4472015-01-16 07:04:10 -08002293 do {
caryclark221a4bb2016-10-07 11:15:15 -07002294 if (!coincident) {
2295 return;
2296 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002297 if (!coincident->fCoinStart.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002298 continue;
2299 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002300 if (!coincident->fCoinEnd.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002301 continue;
2302 }
Cary Clark67116382017-01-03 16:25:18 -05002303 double perpT = coincident->fCoinStart.perpT();
2304 if (perpT < 0) {
2305 return;
2306 }
caryclark54359292015-03-26 07:52:43 -07002307 int index = intersections->insertCoincident(coincident->fStartT,
Cary Clark67116382017-01-03 16:25:18 -05002308 perpT, coincident->fPart[0]);
caryclark54359292015-03-26 07:52:43 -07002309 if ((intersections->insertCoincident(coincident->fEndT,
2310 coincident->fCoinEnd.perpT(),
2311 coincident->fPart[TCurve::kPointLast]) < 0) && index >= 0) {
caryclark45fa4472015-01-16 07:04:10 -08002312 intersections->clearCoincidence(index);
2313 }
caryclark54359292015-03-26 07:52:43 -07002314 } while ((coincident = coincident->fNext));
2315 }
caryclark1049f122015-04-20 08:31:59 -07002316 int zeroOneSet = EndsEqual(sect1, sect2, intersections);
caryclark34efb702016-10-24 08:19:06 -07002317// if (!sect1->fHead || !sect2->fHead) {
caryclark6c3b9cd2016-09-26 05:36:58 -07002318 // if the final iteration contains an end (0 or 1),
2319 if (sect1->fRemovedStartT && !(zeroOneSet & kZeroS1Set)) {
2320 SkTCoincident<TCurve, OppCurve> perp; // intersect perpendicular with opposite curve
caryclarka35ab3e2016-10-20 08:32:18 -07002321 perp.setPerp(sect1->fCurve, 0, sect1->fCurve[0], sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002322 if (perp.isMatch()) {
2323 intersections->insert(0, perp.perpT(), perp.perpPt());
2324 }
2325 }
2326 if (sect1->fRemovedEndT && !(zeroOneSet & kOneS1Set)) {
2327 SkTCoincident<TCurve, OppCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002328 perp.setPerp(sect1->fCurve, 1, sect1->fCurve[TCurve::kPointLast], sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002329 if (perp.isMatch()) {
2330 intersections->insert(1, perp.perpT(), perp.perpPt());
2331 }
2332 }
2333 if (sect2->fRemovedStartT && !(zeroOneSet & kZeroS2Set)) {
2334 SkTCoincident<OppCurve, TCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002335 perp.setPerp(sect2->fCurve, 0, sect2->fCurve[0], sect1->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002336 if (perp.isMatch()) {
2337 intersections->insert(perp.perpT(), 0, perp.perpPt());
2338 }
2339 }
2340 if (sect2->fRemovedEndT && !(zeroOneSet & kOneS2Set)) {
2341 SkTCoincident<OppCurve, TCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002342 perp.setPerp(sect2->fCurve, 1, sect2->fCurve[OppCurve::kPointLast], sect1->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002343 if (perp.isMatch()) {
2344 intersections->insert(perp.perpT(), 1, perp.perpPt());
2345 }
2346 }
caryclark34efb702016-10-24 08:19:06 -07002347// }
2348 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002349 return;
caryclark45fa4472015-01-16 07:04:10 -08002350 }
caryclark45fa4472015-01-16 07:04:10 -08002351 sect1->recoverCollapsed();
2352 sect2->recoverCollapsed();
caryclark1049f122015-04-20 08:31:59 -07002353 SkTSpan<TCurve, OppCurve>* result1 = sect1->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002354 // check heads and tails for zero and ones and insert them if we haven't already done so
caryclark1049f122015-04-20 08:31:59 -07002355 const SkTSpan<TCurve, OppCurve>* head1 = result1;
caryclark45fa4472015-01-16 07:04:10 -08002356 if (!(zeroOneSet & kZeroS1Set) && approximately_less_than_zero(head1->fStartT)) {
2357 const SkDPoint& start1 = sect1->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002358 if (head1->isBounded()) {
2359 double t = head1->closestBoundedT(start1);
2360 if (sect2->fCurve.ptAtT(t).approximatelyEqual(start1)) {
2361 intersections->insert(0, t, start1);
2362 }
caryclark45fa4472015-01-16 07:04:10 -08002363 }
2364 }
caryclark1049f122015-04-20 08:31:59 -07002365 const SkTSpan<OppCurve, TCurve>* head2 = sect2->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002366 if (!(zeroOneSet & kZeroS2Set) && approximately_less_than_zero(head2->fStartT)) {
2367 const SkDPoint& start2 = sect2->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002368 if (head2->isBounded()) {
2369 double t = head2->closestBoundedT(start2);
2370 if (sect1->fCurve.ptAtT(t).approximatelyEqual(start2)) {
2371 intersections->insert(t, 0, start2);
2372 }
caryclark45fa4472015-01-16 07:04:10 -08002373 }
2374 }
caryclark1049f122015-04-20 08:31:59 -07002375 const SkTSpan<TCurve, OppCurve>* tail1 = sect1->tail();
caryclark45fa4472015-01-16 07:04:10 -08002376 if (!(zeroOneSet & kOneS1Set) && approximately_greater_than_one(tail1->fEndT)) {
2377 const SkDPoint& end1 = sect1->fCurve[TCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002378 if (tail1->isBounded()) {
2379 double t = tail1->closestBoundedT(end1);
2380 if (sect2->fCurve.ptAtT(t).approximatelyEqual(end1)) {
2381 intersections->insert(1, t, end1);
2382 }
caryclark45fa4472015-01-16 07:04:10 -08002383 }
2384 }
caryclark1049f122015-04-20 08:31:59 -07002385 const SkTSpan<OppCurve, TCurve>* tail2 = sect2->tail();
caryclark45fa4472015-01-16 07:04:10 -08002386 if (!(zeroOneSet & kOneS2Set) && approximately_greater_than_one(tail2->fEndT)) {
caryclark1049f122015-04-20 08:31:59 -07002387 const SkDPoint& end2 = sect2->fCurve[OppCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002388 if (tail2->isBounded()) {
2389 double t = tail2->closestBoundedT(end2);
2390 if (sect1->fCurve.ptAtT(t).approximatelyEqual(end2)) {
2391 intersections->insert(t, 1, end2);
2392 }
caryclark45fa4472015-01-16 07:04:10 -08002393 }
2394 }
caryclark1049f122015-04-20 08:31:59 -07002395 SkClosestSect<TCurve, OppCurve> closest;
caryclark45fa4472015-01-16 07:04:10 -08002396 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07002397 while (result1 && result1->fCoinStart.isMatch() && result1->fCoinEnd.isMatch()) {
caryclark45fa4472015-01-16 07:04:10 -08002398 result1 = result1->fNext;
2399 }
2400 if (!result1) {
2401 break;
2402 }
caryclark1049f122015-04-20 08:31:59 -07002403 SkTSpan<OppCurve, TCurve>* result2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002404 bool found = false;
caryclark45fa4472015-01-16 07:04:10 -08002405 while (result2) {
caryclarkcdeff812016-07-22 03:34:19 -07002406 found |= closest.find(result1, result2 SkDEBUGPARAMS(intersections));
caryclark45fa4472015-01-16 07:04:10 -08002407 result2 = result2->fNext;
2408 }
caryclark45fa4472015-01-16 07:04:10 -08002409 } while ((result1 = result1->fNext));
2410 closest.finish(intersections);
caryclark54359292015-03-26 07:52:43 -07002411 // if there is more than one intersection and it isn't already coincident, check
2412 int last = intersections->used() - 1;
2413 for (int index = 0; index < last; ) {
2414 if (intersections->isCoincident(index) && intersections->isCoincident(index + 1)) {
2415 ++index;
2416 continue;
2417 }
2418 double midT = ((*intersections)[0][index] + (*intersections)[0][index + 1]) / 2;
2419 SkDPoint midPt = sect1->fCurve.ptAtT(midT);
2420 // intersect perpendicular with opposite curve
caryclark1049f122015-04-20 08:31:59 -07002421 SkTCoincident<TCurve, OppCurve> perp;
caryclark54359292015-03-26 07:52:43 -07002422 perp.setPerp(sect1->fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002423 if (!perp.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07002424 ++index;
2425 continue;
2426 }
2427 if (intersections->isCoincident(index)) {
2428 intersections->removeOne(index);
2429 --last;
2430 } else if (intersections->isCoincident(index + 1)) {
2431 intersections->removeOne(index + 1);
2432 --last;
2433 } else {
2434 intersections->setCoincident(index++);
2435 }
2436 intersections->setCoincident(index);
2437 }
caryclarka35ab3e2016-10-20 08:32:18 -07002438 SkOPOBJASSERT(intersections, intersections->used() <= TCurve::kMaxIntersections);
caryclark45fa4472015-01-16 07:04:10 -08002439}
deanm12670eb2016-04-26 14:09:01 -07002440
2441#endif