blob: fb00d9abc933107d0ae84ad191be5d3e70eb9cc1 [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,
275 double* oppT);
276 SkTSpan<TCurve, OppCurve>* boundsMax() const;
caryclarkef7cee42016-09-06 09:05:54 -0700277 bool coincidentCheck(SkTSect<OppCurve, TCurve>* sect2);
caryclark26ad22a2015-10-16 09:03:38 -0700278 void coincidentForce(SkTSect<OppCurve, TCurve>* sect2, double start1s, double start1e);
caryclark54359292015-03-26 07:52:43 -0700279 bool coincidentHasT(double t);
caryclark1049f122015-04-20 08:31:59 -0700280 int collapsed() const;
281 void computePerpendiculars(SkTSect<OppCurve, TCurve>* sect2, SkTSpan<TCurve, OppCurve>* first,
282 SkTSpan<TCurve, OppCurve>* last);
283 int countConsecutiveSpans(SkTSpan<TCurve, OppCurve>* first,
284 SkTSpan<TCurve, OppCurve>** last) const;
caryclarkccec0f92015-03-24 07:28:17 -0700285
caryclark54359292015-03-26 07:52:43 -0700286 int debugID() const {
287 return PATH_OPS_DEBUG_T_SECT_RELEASE(fID, -1);
288 }
289
caryclarkef7cee42016-09-06 09:05:54 -0700290 bool deleteEmptySpans();
caryclark1049f122015-04-20 08:31:59 -0700291 void dumpCommon(const SkTSpan<TCurve, OppCurve>* ) const;
292 void dumpCommonCurves(const SkTSpan<TCurve, OppCurve>* ) const;
293 static int EndsEqual(const SkTSect* sect1, const SkTSect<OppCurve, TCurve>* sect2,
294 SkIntersections* );
caryclarkef7cee42016-09-06 09:05:54 -0700295 bool extractCoincident(SkTSect<OppCurve, TCurve>* sect2, SkTSpan<TCurve, OppCurve>* first,
296 SkTSpan<TCurve, OppCurve>* last, SkTSpan<TCurve, OppCurve>** result);
caryclark1049f122015-04-20 08:31:59 -0700297 SkTSpan<TCurve, OppCurve>* findCoincidentRun(SkTSpan<TCurve, OppCurve>* first,
298 SkTSpan<TCurve, OppCurve>** lastPtr);
299 int intersects(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp,
300 SkTSpan<OppCurve, TCurve>* oppSpan, int* oppResult);
caryclarked0935a2015-10-22 07:23:52 -0700301 bool isParallel(const SkDLine& thisLine, const SkTSect<OppCurve, TCurve>* opp) const;
caryclark1049f122015-04-20 08:31:59 -0700302 int linesIntersect(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp,
303 SkTSpan<OppCurve, TCurve>* oppSpan, SkIntersections* );
caryclarkef7cee42016-09-06 09:05:54 -0700304 bool markSpanGone(SkTSpan<TCurve, OppCurve>* span);
caryclark1049f122015-04-20 08:31:59 -0700305 bool matchedDirection(double t, const SkTSect<OppCurve, TCurve>* sect2, double t2) const;
306 void matchedDirCheck(double t, const SkTSect<OppCurve, TCurve>* sect2, double t2,
caryclark54359292015-03-26 07:52:43 -0700307 bool* calcMatched, bool* oppMatched) const;
caryclark1049f122015-04-20 08:31:59 -0700308 void mergeCoincidence(SkTSect<OppCurve, TCurve>* sect2);
309 SkTSpan<TCurve, OppCurve>* prev(SkTSpan<TCurve, OppCurve>* ) const;
310 void removeByPerpendicular(SkTSect<OppCurve, TCurve>* opp);
caryclark54359292015-03-26 07:52:43 -0700311 void recoverCollapsed();
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);
caryclark643ede62016-08-08 14:27:45 -0700418 result->validate();
caryclark54359292015-03-26 07:52:43 -0700419 return result;
420}
421
caryclark1049f122015-04-20 08:31:59 -0700422template<typename TCurve, typename OppCurve>
423void SkTSect<TCurve, OppCurve>::addForPerp(SkTSpan<OppCurve, TCurve>* span, double t) {
caryclark54359292015-03-26 07:52:43 -0700424 if (!span->hasOppT(t)) {
caryclark1049f122015-04-20 08:31:59 -0700425 SkTSpan<TCurve, OppCurve>* priorSpan;
426 SkTSpan<TCurve, OppCurve>* opp = this->spanAtT(t, &priorSpan);
caryclark54359292015-03-26 07:52:43 -0700427 if (!opp) {
428 opp = this->addFollowing(priorSpan);
429#if DEBUG_PERP
caryclark26ad22a2015-10-16 09:03:38 -0700430 SkDebugf("%s priorSpan=%d t=%1.9g opp=%d\n", __FUNCTION__, priorSpan ?
431 priorSpan->debugID() : -1, t, opp->debugID());
caryclark54359292015-03-26 07:52:43 -0700432#endif
433 }
434#if DEBUG_PERP
435 opp->dump(); SkDebugf("\n");
caryclark26ad22a2015-10-16 09:03:38 -0700436 SkDebugf("%s addBounded span=%d opp=%d\n", __FUNCTION__, priorSpan ?
437 priorSpan->debugID() : -1, opp->debugID());
caryclark54359292015-03-26 07:52:43 -0700438#endif
439 opp->addBounded(span, &fHeap);
440 span->addBounded(opp, &fHeap);
441 }
442 this->validate();
caryclark1049f122015-04-20 08:31:59 -0700443#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700444 span->validatePerpT(t);
caryclark1049f122015-04-20 08:31:59 -0700445#endif
caryclark54359292015-03-26 07:52:43 -0700446}
447
caryclark1049f122015-04-20 08:31:59 -0700448template<typename TCurve, typename OppCurve>
449double SkTSpan<TCurve, OppCurve>::closestBoundedT(const SkDPoint& pt) const {
caryclark54359292015-03-26 07:52:43 -0700450 double result = -1;
caryclark343382e2016-06-29 08:18:38 -0700451 double closest = DBL_MAX;
caryclark1049f122015-04-20 08:31:59 -0700452 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700453 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -0700454 const SkTSpan<OppCurve, TCurve>* test = testBounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700455 double startDist = test->fPart[0].distanceSquared(pt);
456 if (closest > startDist) {
457 closest = startDist;
458 result = test->fStartT;
459 }
caryclark1049f122015-04-20 08:31:59 -0700460 double endDist = test->fPart[OppCurve::kPointLast].distanceSquared(pt);
caryclark54359292015-03-26 07:52:43 -0700461 if (closest > endDist) {
462 closest = endDist;
463 result = test->fEndT;
464 }
465 testBounded = testBounded->fNext;
466 }
467 SkASSERT(between(0, result, 1));
468 return result;
469}
470
471#ifdef SK_DEBUG
caryclark1049f122015-04-20 08:31:59 -0700472template<typename TCurve, typename OppCurve>
473bool SkTSpan<TCurve, OppCurve>::debugIsBefore(const SkTSpan* span) const {
caryclark54359292015-03-26 07:52:43 -0700474 const SkTSpan* work = this;
475 do {
476 if (span == work) {
477 return true;
478 }
479 } while ((work = work->fNext));
480 return false;
481}
482#endif
483
caryclark1049f122015-04-20 08:31:59 -0700484template<typename TCurve, typename OppCurve>
485bool SkTSpan<TCurve, OppCurve>::contains(double t) const {
caryclark54359292015-03-26 07:52:43 -0700486 const SkTSpan* work = this;
487 do {
488 if (between(work->fStartT, t, work->fEndT)) {
489 return true;
490 }
491 } while ((work = work->fNext));
492 return false;
493}
494
caryclark1049f122015-04-20 08:31:59 -0700495template<typename TCurve, typename OppCurve>
496const SkTSect<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::debugOpp() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700497 return SkDEBUGRELEASE(fDebugSect->debugOpp(), nullptr);
caryclark54359292015-03-26 07:52:43 -0700498}
499
caryclark1049f122015-04-20 08:31:59 -0700500template<typename TCurve, typename OppCurve>
501SkTSpan<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::findOppSpan(
502 const SkTSpan<OppCurve, TCurve>* opp) const {
503 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700504 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700505 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700506 if (opp == test) {
507 return test;
508 }
509 bounded = bounded->fNext;
510 }
halcanary96fcdcc2015-08-27 07:41:13 -0700511 return nullptr;
caryclark54359292015-03-26 07:52:43 -0700512}
513
514// returns 0 if no hull intersection
515// 1 if hulls intersect
516// 2 if hulls only share a common endpoint
517// -1 if linear and further checking is required
caryclark1049f122015-04-20 08:31:59 -0700518template<typename TCurve, typename OppCurve>
519int SkTSpan<TCurve, OppCurve>::hullCheck(const SkTSpan<OppCurve, TCurve>* opp,
520 bool* start, bool* oppStart) {
caryclark54359292015-03-26 07:52:43 -0700521 if (fIsLinear) {
522 return -1;
523 }
524 bool ptsInCommon;
525 if (onlyEndPointsInCommon(opp, start, oppStart, &ptsInCommon)) {
526 SkASSERT(ptsInCommon);
527 return 2;
528 }
529 bool linear;
530 if (fPart.hullIntersects(opp->fPart, &linear)) {
531 if (!linear) { // check set true if linear
532 return 1;
533 }
534 fIsLinear = true;
535 fIsLine = fPart.controlsInside();
caryclark2bec26a2016-05-26 09:01:47 -0700536 return ptsInCommon ? 1 : -1;
caryclark54359292015-03-26 07:52:43 -0700537 } else { // hull is not linear; check set true if intersected at the end points
538 return ((int) ptsInCommon) << 1; // 0 or 2
539 }
540 return 0;
541}
542
543// OPTIMIZE ? If at_most_end_pts_in_common detects that one quad is near linear,
544// use line intersection to guess a better split than 0.5
545// OPTIMIZE Once at_most_end_pts_in_common detects linear, mark span so all future splits are linear
caryclark1049f122015-04-20 08:31:59 -0700546template<typename TCurve, typename OppCurve>
547int SkTSpan<TCurve, OppCurve>::hullsIntersect(SkTSpan<OppCurve, TCurve>* opp,
548 bool* start, bool* oppStart) {
caryclark54359292015-03-26 07:52:43 -0700549 if (!fBounds.intersects(opp->fBounds)) {
550 return 0;
551 }
552 int hullSect = this->hullCheck(opp, start, oppStart);
553 if (hullSect >= 0) {
554 return hullSect;
555 }
556 hullSect = opp->hullCheck(this, oppStart, start);
557 if (hullSect >= 0) {
558 return hullSect;
559 }
560 return -1;
561}
562
caryclark1049f122015-04-20 08:31:59 -0700563template<typename TCurve, typename OppCurve>
564void SkTSpan<TCurve, OppCurve>::init(const TCurve& c) {
halcanary96fcdcc2015-08-27 07:41:13 -0700565 fPrev = fNext = nullptr;
reed0dc4dd62015-03-24 13:55:33 -0700566 fStartT = 0;
567 fEndT = 1;
halcanary96fcdcc2015-08-27 07:41:13 -0700568 fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -0700569 resetBounds(c);
caryclark45fa4472015-01-16 07:04:10 -0800570}
571
caryclark1049f122015-04-20 08:31:59 -0700572template<typename TCurve, typename OppCurve>
caryclarka35ab3e2016-10-20 08:32:18 -0700573bool SkTSpan<TCurve, OppCurve>::initBounds(const TCurve& c) {
reed0dc4dd62015-03-24 13:55:33 -0700574 fPart = c.subDivide(fStartT, fEndT);
575 fBounds.setBounds(fPart);
576 fCoinStart.init();
577 fCoinEnd.init();
578 fBoundsMax = SkTMax(fBounds.width(), fBounds.height());
579 fCollapsed = fPart.collapsed();
580 fHasPerp = false;
caryclark54359292015-03-26 07:52:43 -0700581 fDeleted = false;
reed0dc4dd62015-03-24 13:55:33 -0700582#if DEBUG_T_SECT
reed0dc4dd62015-03-24 13:55:33 -0700583 if (fCollapsed) {
584 SkDebugf(""); // for convenient breakpoints
caryclark45fa4472015-01-16 07:04:10 -0800585 }
586#endif
caryclarka35ab3e2016-10-20 08:32:18 -0700587 return fBounds.valid();
caryclark45fa4472015-01-16 07:04:10 -0800588}
589
caryclark1049f122015-04-20 08:31:59 -0700590template<typename TCurve, typename OppCurve>
591bool SkTSpan<TCurve, OppCurve>::linearsIntersect(SkTSpan<OppCurve, TCurve>* span) {
caryclark54359292015-03-26 07:52:43 -0700592 int result = this->linearIntersects(span->fPart);
593 if (result <= 1) {
594 return SkToBool(result);
caryclark45fa4472015-01-16 07:04:10 -0800595 }
caryclark54359292015-03-26 07:52:43 -0700596 SkASSERT(span->fIsLinear);
597 result = span->linearIntersects(this->fPart);
598// SkASSERT(result <= 1);
599 return SkToBool(result);
caryclark45fa4472015-01-16 07:04:10 -0800600}
601
caryclark1049f122015-04-20 08:31:59 -0700602template<typename TCurve, typename OppCurve>
603double SkTSpan<TCurve, OppCurve>::linearT(const SkDPoint& pt) const {
caryclark54359292015-03-26 07:52:43 -0700604 SkDVector len = fPart[TCurve::kPointLast] - fPart[0];
605 return fabs(len.fX) > fabs(len.fY)
606 ? (pt.fX - fPart[0].fX) / len.fX
607 : (pt.fY - fPart[0].fY) / len.fY;
caryclark45fa4472015-01-16 07:04:10 -0800608}
609
caryclark1049f122015-04-20 08:31:59 -0700610template<typename TCurve, typename OppCurve>
611int SkTSpan<TCurve, OppCurve>::linearIntersects(const OppCurve& q2) const {
caryclark45fa4472015-01-16 07:04:10 -0800612 // looks like q1 is near-linear
caryclark54359292015-03-26 07:52:43 -0700613 int start = 0, end = TCurve::kPointLast; // the outside points are usually the extremes
caryclark45fa4472015-01-16 07:04:10 -0800614 if (!fPart.controlsInside()) {
615 double dist = 0; // if there's any question, compute distance to find best outsiders
616 for (int outer = 0; outer < TCurve::kPointCount - 1; ++outer) {
617 for (int inner = outer + 1; inner < TCurve::kPointCount; ++inner) {
618 double test = (fPart[outer] - fPart[inner]).lengthSquared();
619 if (dist > test) {
620 continue;
621 }
622 dist = test;
623 start = outer;
624 end = inner;
625 }
626 }
627 }
628 // see if q2 is on one side of the line formed by the extreme points
629 double origX = fPart[start].fX;
630 double origY = fPart[start].fY;
631 double adj = fPart[end].fX - origX;
632 double opp = fPart[end].fY - origY;
caryclark54359292015-03-26 07:52:43 -0700633 double maxPart = SkTMax(fabs(adj), fabs(opp));
634 double sign = 0; // initialization to shut up warning in release build
caryclark1049f122015-04-20 08:31:59 -0700635 for (int n = 0; n < OppCurve::kPointCount; ++n) {
caryclark54359292015-03-26 07:52:43 -0700636 double dx = q2[n].fY - origY;
637 double dy = q2[n].fX - origX;
638 double maxVal = SkTMax(maxPart, SkTMax(fabs(dx), fabs(dy)));
caryclark45fa4472015-01-16 07:04:10 -0800639 double test = (q2[n].fY - origY) * adj - (q2[n].fX - origX) * opp;
caryclark54359292015-03-26 07:52:43 -0700640 if (precisely_zero_when_compared_to(test, maxVal)) {
641 return 1;
642 }
643 if (approximately_zero_when_compared_to(test, maxVal)) {
644 return 3;
caryclark45fa4472015-01-16 07:04:10 -0800645 }
646 if (n == 0) {
647 sign = test;
648 continue;
649 }
650 if (test * sign < 0) {
caryclark54359292015-03-26 07:52:43 -0700651 return 1;
caryclark45fa4472015-01-16 07:04:10 -0800652 }
653 }
caryclark54359292015-03-26 07:52:43 -0700654 return 0;
655}
656
caryclark1049f122015-04-20 08:31:59 -0700657template<typename TCurve, typename OppCurve>
658bool SkTSpan<TCurve, OppCurve>::onlyEndPointsInCommon(const SkTSpan<OppCurve, TCurve>* opp,
659 bool* start, bool* oppStart, bool* ptsInCommon) {
caryclark54359292015-03-26 07:52:43 -0700660 if (opp->fPart[0] == fPart[0]) {
661 *start = *oppStart = true;
662 } else if (opp->fPart[0] == fPart[TCurve::kPointLast]) {
663 *start = false;
664 *oppStart = true;
caryclark1049f122015-04-20 08:31:59 -0700665 } else if (opp->fPart[OppCurve::kPointLast] == fPart[0]) {
caryclark54359292015-03-26 07:52:43 -0700666 *start = true;
667 *oppStart = false;
caryclark1049f122015-04-20 08:31:59 -0700668 } else if (opp->fPart[OppCurve::kPointLast] == fPart[TCurve::kPointLast]) {
caryclark54359292015-03-26 07:52:43 -0700669 *start = *oppStart = false;
670 } else {
671 *ptsInCommon = false;
672 return false;
673 }
674 *ptsInCommon = true;
caryclark1049f122015-04-20 08:31:59 -0700675 const SkDPoint* otherPts[TCurve::kPointCount - 1], * oppOtherPts[OppCurve::kPointCount - 1];
caryclark54359292015-03-26 07:52:43 -0700676 int baseIndex = *start ? 0 : TCurve::kPointLast;
caryclark1049f122015-04-20 08:31:59 -0700677 fPart.otherPts(baseIndex, otherPts);
678 opp->fPart.otherPts(*oppStart ? 0 : OppCurve::kPointLast, oppOtherPts);
caryclark54359292015-03-26 07:52:43 -0700679 const SkDPoint& base = fPart[baseIndex];
caryclark1049f122015-04-20 08:31:59 -0700680 for (int o1 = 0; o1 < (int) SK_ARRAY_COUNT(otherPts); ++o1) {
681 SkDVector v1 = *otherPts[o1] - base;
682 for (int o2 = 0; o2 < (int) SK_ARRAY_COUNT(oppOtherPts); ++o2) {
683 SkDVector v2 = *oppOtherPts[o2] - base;
caryclark54359292015-03-26 07:52:43 -0700684 if (v2.dot(v1) >= 0) {
685 return false;
686 }
687 }
688 }
689 return true;
690}
691
caryclark1049f122015-04-20 08:31:59 -0700692template<typename TCurve, typename OppCurve>
693SkTSpan<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::oppT(double t) const {
694 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700695 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700696 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700697 if (between(test->fStartT, t, test->fEndT)) {
698 return test;
699 }
700 bounded = bounded->fNext;
701 }
halcanary96fcdcc2015-08-27 07:41:13 -0700702 return nullptr;
caryclark54359292015-03-26 07:52:43 -0700703}
704
caryclark1049f122015-04-20 08:31:59 -0700705template<typename TCurve, typename OppCurve>
706bool SkTSpan<TCurve, OppCurve>::removeAllBounded() {
caryclark54359292015-03-26 07:52:43 -0700707 bool deleteSpan = false;
caryclark1049f122015-04-20 08:31:59 -0700708 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700709 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700710 SkTSpan<OppCurve, TCurve>* opp = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700711 deleteSpan |= opp->removeBounded(this);
712 bounded = bounded->fNext;
713 }
714 return deleteSpan;
715}
716
caryclark1049f122015-04-20 08:31:59 -0700717template<typename TCurve, typename OppCurve>
718bool SkTSpan<TCurve, OppCurve>::removeBounded(const SkTSpan<OppCurve, TCurve>* opp) {
caryclark54359292015-03-26 07:52:43 -0700719 if (fHasPerp) {
720 bool foundStart = false;
721 bool foundEnd = false;
caryclark1049f122015-04-20 08:31:59 -0700722 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700723 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700724 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700725 if (opp != test) {
726 foundStart |= between(test->fStartT, fCoinStart.perpT(), test->fEndT);
727 foundEnd |= between(test->fStartT, fCoinEnd.perpT(), test->fEndT);
728 }
729 bounded = bounded->fNext;
730 }
731 if (!foundStart || !foundEnd) {
732 fHasPerp = false;
733 fCoinStart.init();
734 fCoinEnd.init();
735 }
736 }
caryclark1049f122015-04-20 08:31:59 -0700737 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
halcanary96fcdcc2015-08-27 07:41:13 -0700738 SkTSpanBounded<OppCurve, TCurve>* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -0700739 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700740 SkTSpanBounded<OppCurve, TCurve>* boundedNext = bounded->fNext;
caryclark54359292015-03-26 07:52:43 -0700741 if (opp == bounded->fBounded) {
742 if (prev) {
743 prev->fNext = boundedNext;
744 return false;
745 } else {
746 fBounded = boundedNext;
halcanary96fcdcc2015-08-27 07:41:13 -0700747 return fBounded == nullptr;
caryclark54359292015-03-26 07:52:43 -0700748 }
749 }
750 prev = bounded;
751 bounded = boundedNext;
752 }
caryclark643ede62016-08-08 14:27:45 -0700753 SkOPASSERT(0);
caryclark45fa4472015-01-16 07:04:10 -0800754 return false;
755}
756
caryclark1049f122015-04-20 08:31:59 -0700757template<typename TCurve, typename OppCurve>
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500758bool SkTSpan<TCurve, OppCurve>::splitAt(SkTSpan* work, double t, SkArenaAlloc* heap) {
caryclark45fa4472015-01-16 07:04:10 -0800759 fStartT = t;
760 fEndT = work->fEndT;
761 if (fStartT == fEndT) {
762 fCollapsed = true;
763 return false;
764 }
765 work->fEndT = t;
766 if (work->fStartT == work->fEndT) {
767 work->fCollapsed = true;
768 return false;
769 }
770 fPrev = work;
771 fNext = work->fNext;
772 fIsLinear = work->fIsLinear;
caryclark54359292015-03-26 07:52:43 -0700773 fIsLine = work->fIsLine;
774
caryclark45fa4472015-01-16 07:04:10 -0800775 work->fNext = this;
776 if (fNext) {
777 fNext->fPrev = this;
778 }
caryclark643ede62016-08-08 14:27:45 -0700779 this->validate();
caryclark1049f122015-04-20 08:31:59 -0700780 SkTSpanBounded<OppCurve, TCurve>* bounded = work->fBounded;
halcanary96fcdcc2015-08-27 07:41:13 -0700781 fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -0700782 while (bounded) {
783 this->addBounded(bounded->fBounded, heap);
784 bounded = bounded->fNext;
785 }
786 bounded = fBounded;
787 while (bounded) {
788 bounded->fBounded->addBounded(this, heap);
789 bounded = bounded->fNext;
caryclark45fa4472015-01-16 07:04:10 -0800790 }
791 return true;
792}
793
caryclark1049f122015-04-20 08:31:59 -0700794template<typename TCurve, typename OppCurve>
795void SkTSpan<TCurve, OppCurve>::validate() const {
caryclark643ede62016-08-08 14:27:45 -0700796#if DEBUG_VALIDATE
797 SkASSERT(this != fPrev);
798 SkASSERT(this != fNext);
halcanary96fcdcc2015-08-27 07:41:13 -0700799 SkASSERT(fNext == nullptr || fNext != fPrev);
800 SkASSERT(fNext == nullptr || this == fNext->fPrev);
801 SkASSERT(fPrev == nullptr || this == fPrev->fNext);
caryclark643ede62016-08-08 14:27:45 -0700802 this->validateBounded();
803#endif
804#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700805 SkASSERT(fBounds.width() || fBounds.height() || fCollapsed);
caryclarke839e782016-09-15 07:48:18 -0700806 SkASSERT(fBoundsMax == SkTMax(fBounds.width(), fBounds.height()) || fCollapsed == 0xFF);
caryclark45fa4472015-01-16 07:04:10 -0800807 SkASSERT(0 <= fStartT);
808 SkASSERT(fEndT <= 1);
caryclark54359292015-03-26 07:52:43 -0700809 SkASSERT(fStartT <= fEndT);
caryclarke839e782016-09-15 07:48:18 -0700810 SkASSERT(fBounded || fCollapsed == 0xFF);
caryclark54359292015-03-26 07:52:43 -0700811 if (fHasPerp) {
caryclark6c3b9cd2016-09-26 05:36:58 -0700812 if (fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700813 validatePerpT(fCoinStart.perpT());
814 validatePerpPt(fCoinStart.perpT(), fCoinStart.perpPt());
815 }
caryclark6c3b9cd2016-09-26 05:36:58 -0700816 if (fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700817 validatePerpT(fCoinEnd.perpT());
818 validatePerpPt(fCoinEnd.perpT(), fCoinEnd.perpPt());
819 }
caryclarkccec0f92015-03-24 07:28:17 -0700820 }
reed0dc4dd62015-03-24 13:55:33 -0700821#endif
caryclark54359292015-03-26 07:52:43 -0700822}
caryclarkccec0f92015-03-24 07:28:17 -0700823
caryclark1049f122015-04-20 08:31:59 -0700824template<typename TCurve, typename OppCurve>
825void SkTSpan<TCurve, OppCurve>::validateBounded() const {
caryclark54359292015-03-26 07:52:43 -0700826#if DEBUG_VALIDATE
caryclark1049f122015-04-20 08:31:59 -0700827 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700828 while (testBounded) {
csmartdaltonceeaa782016-08-10 10:07:57 -0700829 SkDEBUGCODE(const SkTSpan<OppCurve, TCurve>* overlap = testBounded->fBounded);
caryclark54359292015-03-26 07:52:43 -0700830 SkASSERT(!overlap->fDeleted);
caryclark643ede62016-08-08 14:27:45 -0700831#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700832 SkASSERT(((this->debugID() ^ overlap->debugID()) & 1) == 1);
833 SkASSERT(overlap->findOppSpan(this));
caryclark643ede62016-08-08 14:27:45 -0700834#endif
caryclark54359292015-03-26 07:52:43 -0700835 testBounded = testBounded->fNext;
836 }
837#endif
838}
839
caryclark1049f122015-04-20 08:31:59 -0700840template<typename TCurve, typename OppCurve>
841void SkTSpan<TCurve, OppCurve>::validatePerpT(double oppT) const {
842 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700843 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -0700844 const SkTSpan<OppCurve, TCurve>* overlap = testBounded->fBounded;
caryclark26ad22a2015-10-16 09:03:38 -0700845 if (precisely_between(overlap->fStartT, oppT, overlap->fEndT)) {
caryclark54359292015-03-26 07:52:43 -0700846 return;
847 }
848 testBounded = testBounded->fNext;
849 }
850 SkASSERT(0);
caryclark54359292015-03-26 07:52:43 -0700851}
852
caryclark1049f122015-04-20 08:31:59 -0700853template<typename TCurve, typename OppCurve>
854void SkTSpan<TCurve, OppCurve>::validatePerpPt(double t, const SkDPoint& pt) const {
855 SkASSERT(fDebugSect->fOppSect->fCurve.ptAtT(t) == pt);
caryclark54359292015-03-26 07:52:43 -0700856}
857
858
caryclark1049f122015-04-20 08:31:59 -0700859template<typename TCurve, typename OppCurve>
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500860SkTSect<TCurve, OppCurve>::SkTSect(const TCurve& c
caryclarke25a4f62016-07-26 09:26:29 -0700861 SkDEBUGPARAMS(SkOpGlobalState* debugGlobalState)
862 PATH_OPS_DEBUG_T_SECT_PARAMS(int id))
caryclark45fa4472015-01-16 07:04:10 -0800863 : fCurve(c)
caryclark1049f122015-04-20 08:31:59 -0700864 , fHeap(sizeof(SkTSpan<TCurve, OppCurve>) * 4)
halcanary96fcdcc2015-08-27 07:41:13 -0700865 , fCoincident(nullptr)
866 , fDeleted(nullptr)
caryclark45fa4472015-01-16 07:04:10 -0800867 , fActiveCount(0)
caryclarke25a4f62016-07-26 09:26:29 -0700868 SkDEBUGPARAMS(fDebugGlobalState(debugGlobalState))
caryclark54359292015-03-26 07:52:43 -0700869 PATH_OPS_DEBUG_T_SECT_PARAMS(fID(id))
870 PATH_OPS_DEBUG_T_SECT_PARAMS(fDebugCount(0))
871 PATH_OPS_DEBUG_T_SECT_PARAMS(fDebugAllocatedCount(0))
caryclark45fa4472015-01-16 07:04:10 -0800872{
caryclark34efb702016-10-24 08:19:06 -0700873 this->resetRemovedEnds();
874 fHead = this->addOne();
caryclark643ede62016-08-08 14:27:45 -0700875 SkDEBUGCODE(fHead->debugSetGlobalState(debugGlobalState));
caryclark45fa4472015-01-16 07:04:10 -0800876 fHead->init(c);
877}
878
caryclark1049f122015-04-20 08:31:59 -0700879template<typename TCurve, typename OppCurve>
880SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::addOne() {
881 SkTSpan<TCurve, OppCurve>* result;
caryclark45fa4472015-01-16 07:04:10 -0800882 if (fDeleted) {
883 result = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -0800884 fDeleted = result->fNext;
885 } else {
Herb Derbyc3cc5fa2017-03-07 11:11:47 -0500886 result = fHeap.make<SkTSpan<TCurve, OppCurve>>();
caryclark45fa4472015-01-16 07:04:10 -0800887#if DEBUG_T_SECT
888 ++fDebugAllocatedCount;
889#endif
890 }
caryclarked0935a2015-10-22 07:23:52 -0700891 result->reset();
caryclark08b32492015-04-06 11:41:29 -0700892 result->fHasPerp = false;
893 result->fDeleted = false;
caryclark55888e42016-07-18 10:01:36 -0700894 ++fActiveCount;
caryclark54359292015-03-26 07:52:43 -0700895 PATH_OPS_DEBUG_T_SECT_CODE(result->fID = fDebugCount++ * 2 + fID);
caryclark1049f122015-04-20 08:31:59 -0700896 SkDEBUGCODE(result->fDebugSect = this);
caryclarked0935a2015-10-22 07:23:52 -0700897#ifdef SK_DEBUG
898 result->fPart.debugInit();
899 result->fCoinStart.debugInit();
900 result->fCoinEnd.debugInit();
901 result->fPrev = result->fNext = nullptr;
902 result->fBounds.debugInit();
903 result->fStartT = result->fEndT = result->fBoundsMax = SK_ScalarNaN;
904 result->fCollapsed = result->fIsLinear = result->fIsLine = 0xFF;
905#endif
caryclark45fa4472015-01-16 07:04:10 -0800906 return result;
907}
908
caryclark1049f122015-04-20 08:31:59 -0700909template<typename TCurve, typename OppCurve>
910bool SkTSect<TCurve, OppCurve>::binarySearchCoin(SkTSect<OppCurve, TCurve>* sect2, double tStart,
911 double tStep, double* resultT, double* oppT) {
912 SkTSpan<TCurve, OppCurve> work;
caryclark45fa4472015-01-16 07:04:10 -0800913 double result = work.fStartT = work.fEndT = tStart;
caryclark1049f122015-04-20 08:31:59 -0700914 SkDEBUGCODE(work.fDebugSect = this);
caryclark45fa4472015-01-16 07:04:10 -0800915 SkDPoint last = fCurve.ptAtT(tStart);
916 SkDPoint oppPt;
917 bool flip = false;
caryclarkcdeff812016-07-22 03:34:19 -0700918 bool contained = false;
caryclark45fa4472015-01-16 07:04:10 -0800919 SkDEBUGCODE(bool down = tStep < 0);
caryclark1049f122015-04-20 08:31:59 -0700920 const OppCurve& opp = sect2->fCurve;
caryclark45fa4472015-01-16 07:04:10 -0800921 do {
922 tStep *= 0.5;
923 work.fStartT += tStep;
924 if (flip) {
925 tStep = -tStep;
926 flip = false;
927 }
928 work.initBounds(fCurve);
929 if (work.fCollapsed) {
930 return false;
931 }
932 if (last.approximatelyEqual(work.fPart[0])) {
933 break;
934 }
935 last = work.fPart[0];
936 work.fCoinStart.setPerp(fCurve, work.fStartT, last, opp);
caryclark6c3b9cd2016-09-26 05:36:58 -0700937 if (work.fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700938#if DEBUG_T_SECT
939 work.validatePerpPt(work.fCoinStart.perpT(), work.fCoinStart.perpPt());
940#endif
caryclark45fa4472015-01-16 07:04:10 -0800941 double oppTTest = work.fCoinStart.perpT();
caryclark54359292015-03-26 07:52:43 -0700942 if (sect2->fHead->contains(oppTTest)) {
caryclark45fa4472015-01-16 07:04:10 -0800943 *oppT = oppTTest;
944 oppPt = work.fCoinStart.perpPt();
caryclarkcdeff812016-07-22 03:34:19 -0700945 contained = true;
caryclark45fa4472015-01-16 07:04:10 -0800946 SkASSERT(down ? result > work.fStartT : result < work.fStartT);
947 result = work.fStartT;
948 continue;
949 }
950 }
951 tStep = -tStep;
952 flip = true;
953 } while (true);
caryclarkcdeff812016-07-22 03:34:19 -0700954 if (!contained) {
955 return false;
956 }
caryclark45fa4472015-01-16 07:04:10 -0800957 if (last.approximatelyEqual(fCurve[0])) {
958 result = 0;
959 } else if (last.approximatelyEqual(fCurve[TCurve::kPointLast])) {
960 result = 1;
961 }
962 if (oppPt.approximatelyEqual(opp[0])) {
963 *oppT = 0;
caryclark1049f122015-04-20 08:31:59 -0700964 } else if (oppPt.approximatelyEqual(opp[OppCurve::kPointLast])) {
caryclark45fa4472015-01-16 07:04:10 -0800965 *oppT = 1;
966 }
967 *resultT = result;
968 return true;
969}
970
971// OPTIMIZE ? keep a sorted list of sizes in the form of a doubly-linked list in quad span
972// so that each quad sect has a pointer to the largest, and can update it as spans
973// are split
caryclark1049f122015-04-20 08:31:59 -0700974template<typename TCurve, typename OppCurve>
975SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::boundsMax() const {
976 SkTSpan<TCurve, OppCurve>* test = fHead;
977 SkTSpan<TCurve, OppCurve>* largest = fHead;
caryclark54359292015-03-26 07:52:43 -0700978 bool lCollapsed = largest->fCollapsed;
caryclark45fa4472015-01-16 07:04:10 -0800979 while ((test = test->fNext)) {
caryclark54359292015-03-26 07:52:43 -0700980 bool tCollapsed = test->fCollapsed;
981 if ((lCollapsed && !tCollapsed) || (lCollapsed == tCollapsed &&
982 largest->fBoundsMax < test->fBoundsMax)) {
caryclark45fa4472015-01-16 07:04:10 -0800983 largest = test;
caryclark1049f122015-04-20 08:31:59 -0700984 lCollapsed = test->fCollapsed;
caryclark45fa4472015-01-16 07:04:10 -0800985 }
986 }
caryclark54359292015-03-26 07:52:43 -0700987 return largest;
caryclark45fa4472015-01-16 07:04:10 -0800988}
989
caryclark1049f122015-04-20 08:31:59 -0700990template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -0700991bool SkTSect<TCurve, OppCurve>::coincidentCheck(SkTSect<OppCurve, TCurve>* sect2) {
caryclark1049f122015-04-20 08:31:59 -0700992 SkTSpan<TCurve, OppCurve>* first = fHead;
caryclark9feb6322016-10-25 08:58:26 -0700993 if (!first) {
994 return false;
995 }
caryclark1049f122015-04-20 08:31:59 -0700996 SkTSpan<TCurve, OppCurve>* last, * next;
caryclark45fa4472015-01-16 07:04:10 -0800997 do {
caryclark54359292015-03-26 07:52:43 -0700998 int consecutive = this->countConsecutiveSpans(first, &last);
999 next = last->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001000 if (consecutive < COINCIDENT_SPAN_COUNT) {
1001 continue;
1002 }
caryclark54359292015-03-26 07:52:43 -07001003 this->validate();
1004 sect2->validate();
1005 this->computePerpendiculars(sect2, first, last);
1006 this->validate();
1007 sect2->validate();
caryclark45fa4472015-01-16 07:04:10 -08001008 // check to see if a range of points are on the curve
caryclark1049f122015-04-20 08:31:59 -07001009 SkTSpan<TCurve, OppCurve>* coinStart = first;
caryclark54359292015-03-26 07:52:43 -07001010 do {
caryclarkef7cee42016-09-06 09:05:54 -07001011 bool success = this->extractCoincident(sect2, coinStart, last, &coinStart);
1012 if (!success) {
1013 return false;
1014 }
caryclark54359292015-03-26 07:52:43 -07001015 } while (coinStart && !last->fDeleted);
caryclark55888e42016-07-18 10:01:36 -07001016 if (!fHead || !sect2->fHead) {
1017 break;
1018 }
caryclark643ede62016-08-08 14:27:45 -07001019 if (!next || next->fDeleted) {
1020 break;
1021 }
caryclark45fa4472015-01-16 07:04:10 -08001022 } while ((first = next));
caryclarkef7cee42016-09-06 09:05:54 -07001023 return true;
caryclark45fa4472015-01-16 07:04:10 -08001024}
1025
caryclark1049f122015-04-20 08:31:59 -07001026template<typename TCurve, typename OppCurve>
caryclark26ad22a2015-10-16 09:03:38 -07001027void SkTSect<TCurve, OppCurve>::coincidentForce(SkTSect<OppCurve, TCurve>* sect2,
1028 double start1s, double start1e) {
1029 SkTSpan<TCurve, OppCurve>* first = fHead;
1030 SkTSpan<TCurve, OppCurve>* last = this->tail();
1031 SkTSpan<OppCurve, TCurve>* oppFirst = sect2->fHead;
1032 SkTSpan<OppCurve, TCurve>* oppLast = sect2->tail();
1033 bool deleteEmptySpans = this->updateBounded(first, last, oppFirst);
1034 deleteEmptySpans |= sect2->updateBounded(oppFirst, oppLast, first);
1035 this->removeSpanRange(first, last);
1036 sect2->removeSpanRange(oppFirst, oppLast);
1037 first->fStartT = start1s;
1038 first->fEndT = start1e;
1039 first->resetBounds(fCurve);
1040 first->fCoinStart.setPerp(fCurve, start1s, fCurve[0], sect2->fCurve);
1041 first->fCoinEnd.setPerp(fCurve, start1e, fCurve[TCurve::kPointLast], sect2->fCurve);
1042 bool oppMatched = first->fCoinStart.perpT() < first->fCoinEnd.perpT();
caryclarkef784fb2015-10-30 12:03:06 -07001043 double oppStartT = first->fCoinStart.perpT() == -1 ? 0 : SkTMax(0., first->fCoinStart.perpT());
1044 double oppEndT = first->fCoinEnd.perpT() == -1 ? 1 : SkTMin(1., first->fCoinEnd.perpT());
caryclark26ad22a2015-10-16 09:03:38 -07001045 if (!oppMatched) {
1046 SkTSwap(oppStartT, oppEndT);
1047 }
1048 oppFirst->fStartT = oppStartT;
1049 oppFirst->fEndT = oppEndT;
1050 oppFirst->resetBounds(sect2->fCurve);
1051 this->removeCoincident(first, false);
1052 sect2->removeCoincident(oppFirst, true);
1053 if (deleteEmptySpans) {
1054 this->deleteEmptySpans();
1055 sect2->deleteEmptySpans();
1056 }
1057}
1058
1059template<typename TCurve, typename OppCurve>
caryclark1049f122015-04-20 08:31:59 -07001060bool SkTSect<TCurve, OppCurve>::coincidentHasT(double t) {
1061 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001062 while (test) {
1063 if (between(test->fStartT, t, test->fEndT)) {
1064 return true;
1065 }
1066 test = test->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001067 }
caryclark54359292015-03-26 07:52:43 -07001068 return false;
caryclark45fa4472015-01-16 07:04:10 -08001069}
1070
caryclark1049f122015-04-20 08:31:59 -07001071template<typename TCurve, typename OppCurve>
1072int SkTSect<TCurve, OppCurve>::collapsed() const {
1073 int result = 0;
1074 const SkTSpan<TCurve, OppCurve>* test = fHead;
1075 while (test) {
1076 if (test->fCollapsed) {
1077 ++result;
1078 }
1079 test = test->next();
1080 }
1081 return result;
1082}
1083
1084template<typename TCurve, typename OppCurve>
1085void SkTSect<TCurve, OppCurve>::computePerpendiculars(SkTSect<OppCurve, TCurve>* sect2,
1086 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last) {
1087 const OppCurve& opp = sect2->fCurve;
1088 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001089 SkTSpan<TCurve, OppCurve>* prior = nullptr;
caryclark45fa4472015-01-16 07:04:10 -08001090 do {
caryclark54359292015-03-26 07:52:43 -07001091 if (!work->fHasPerp && !work->fCollapsed) {
1092 if (prior) {
1093 work->fCoinStart = prior->fCoinEnd;
1094 } else {
1095 work->fCoinStart.setPerp(fCurve, work->fStartT, work->fPart[0], opp);
caryclark45fa4472015-01-16 07:04:10 -08001096 }
caryclark6c3b9cd2016-09-26 05:36:58 -07001097 if (work->fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001098 double perpT = work->fCoinStart.perpT();
1099 if (sect2->coincidentHasT(perpT)) {
caryclarkdf386c52015-04-21 05:27:02 -07001100 work->fCoinStart.init();
caryclark54359292015-03-26 07:52:43 -07001101 } else {
1102 sect2->addForPerp(work, perpT);
1103 }
1104 }
1105 work->fCoinEnd.setPerp(fCurve, work->fEndT, work->fPart[TCurve::kPointLast], opp);
caryclark6c3b9cd2016-09-26 05:36:58 -07001106 if (work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001107 double perpT = work->fCoinEnd.perpT();
1108 if (sect2->coincidentHasT(perpT)) {
caryclarkdf386c52015-04-21 05:27:02 -07001109 work->fCoinEnd.init();
caryclark54359292015-03-26 07:52:43 -07001110 } else {
1111 sect2->addForPerp(work, perpT);
1112 }
1113 }
1114 work->fHasPerp = true;
caryclark45fa4472015-01-16 07:04:10 -08001115 }
1116 if (work == last) {
1117 break;
1118 }
caryclark54359292015-03-26 07:52:43 -07001119 prior = work;
caryclark45fa4472015-01-16 07:04:10 -08001120 work = work->fNext;
1121 SkASSERT(work);
1122 } while (true);
caryclark54359292015-03-26 07:52:43 -07001123}
1124
caryclark1049f122015-04-20 08:31:59 -07001125template<typename TCurve, typename OppCurve>
1126int SkTSect<TCurve, OppCurve>::countConsecutiveSpans(SkTSpan<TCurve, OppCurve>* first,
1127 SkTSpan<TCurve, OppCurve>** lastPtr) const {
caryclark54359292015-03-26 07:52:43 -07001128 int consecutive = 1;
caryclark1049f122015-04-20 08:31:59 -07001129 SkTSpan<TCurve, OppCurve>* last = first;
caryclark54359292015-03-26 07:52:43 -07001130 do {
caryclark1049f122015-04-20 08:31:59 -07001131 SkTSpan<TCurve, OppCurve>* next = last->fNext;
caryclark54359292015-03-26 07:52:43 -07001132 if (!next) {
1133 break;
1134 }
1135 if (next->fStartT > last->fEndT) {
1136 break;
1137 }
1138 ++consecutive;
1139 last = next;
1140 } while (true);
1141 *lastPtr = last;
1142 return consecutive;
1143}
1144
caryclark1049f122015-04-20 08:31:59 -07001145template<typename TCurve, typename OppCurve>
1146bool SkTSect<TCurve, OppCurve>::debugHasBounded(const SkTSpan<OppCurve, TCurve>* span) const {
1147 const SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001148 if (!test) {
1149 return false;
1150 }
1151 do {
1152 if (test->findOppSpan(span)) {
1153 return true;
1154 }
1155 } while ((test = test->next()));
1156 return false;
1157}
1158
caryclark1049f122015-04-20 08:31:59 -07001159template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001160bool SkTSect<TCurve, OppCurve>::deleteEmptySpans() {
caryclark1049f122015-04-20 08:31:59 -07001161 SkTSpan<TCurve, OppCurve>* test;
1162 SkTSpan<TCurve, OppCurve>* next = fHead;
Cary Clark59ed4822016-12-08 16:17:56 -05001163 int safetyHatch = 1000;
caryclark54359292015-03-26 07:52:43 -07001164 while ((test = next)) {
1165 next = test->fNext;
1166 if (!test->fBounded) {
caryclarkef7cee42016-09-06 09:05:54 -07001167 if (!this->removeSpan(test)) {
1168 return false;
1169 }
caryclark54359292015-03-26 07:52:43 -07001170 }
Cary Clark59ed4822016-12-08 16:17:56 -05001171 if (--safetyHatch < 0) {
1172 return false;
1173 }
caryclark54359292015-03-26 07:52:43 -07001174 }
caryclarkef7cee42016-09-06 09:05:54 -07001175 return true;
caryclark54359292015-03-26 07:52:43 -07001176}
1177
caryclark1049f122015-04-20 08:31:59 -07001178template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001179bool SkTSect<TCurve, OppCurve>::extractCoincident(
caryclark1049f122015-04-20 08:31:59 -07001180 SkTSect<OppCurve, TCurve>* sect2,
caryclarkef7cee42016-09-06 09:05:54 -07001181 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last,
1182 SkTSpan<TCurve, OppCurve>** result) {
caryclark1049f122015-04-20 08:31:59 -07001183 first = findCoincidentRun(first, &last);
caryclarka1b42d92016-08-16 10:25:29 -07001184 if (!first || !last) {
caryclarkef7cee42016-09-06 09:05:54 -07001185 *result = nullptr;
1186 return true;
caryclark45fa4472015-01-16 07:04:10 -08001187 }
1188 // march outwards to find limit of coincidence from here to previous and next spans
1189 double startT = first->fStartT;
caryclarkd8bc16b2015-03-26 09:05:12 -07001190 double oppStartT SK_INIT_TO_AVOID_WARNING;
caryclark54359292015-03-26 07:52:43 -07001191 double oppEndT SK_INIT_TO_AVOID_WARNING;
caryclark1049f122015-04-20 08:31:59 -07001192 SkTSpan<TCurve, OppCurve>* prev = first->fPrev;
caryclark6c3b9cd2016-09-26 05:36:58 -07001193 SkASSERT(first->fCoinStart.isMatch());
caryclark1049f122015-04-20 08:31:59 -07001194 SkTSpan<OppCurve, TCurve>* oppFirst = first->findOppT(first->fCoinStart.perpT());
caryclark6c3b9cd2016-09-26 05:36:58 -07001195 SkOPASSERT(last->fCoinEnd.isMatch());
caryclark54359292015-03-26 07:52:43 -07001196 bool oppMatched = first->fCoinStart.perpT() < first->fCoinEnd.perpT();
1197 double coinStart;
1198 SkDEBUGCODE(double coinEnd);
caryclark1049f122015-04-20 08:31:59 -07001199 SkTSpan<OppCurve, TCurve>* cutFirst;
caryclark54359292015-03-26 07:52:43 -07001200 if (prev && prev->fEndT == startT
1201 && this->binarySearchCoin(sect2, startT, prev->fStartT - startT, &coinStart,
1202 &oppStartT)
caryclark1049f122015-04-20 08:31:59 -07001203 && prev->fStartT < coinStart && coinStart < startT
1204 && (cutFirst = prev->oppT(oppStartT))) {
1205 oppFirst = cutFirst;
caryclark54359292015-03-26 07:52:43 -07001206 first = this->addSplitAt(prev, coinStart);
1207 first->markCoincident();
1208 prev->fCoinEnd.markCoincident();
1209 if (oppFirst->fStartT < oppStartT && oppStartT < oppFirst->fEndT) {
caryclark1049f122015-04-20 08:31:59 -07001210 SkTSpan<OppCurve, TCurve>* oppHalf = sect2->addSplitAt(oppFirst, oppStartT);
caryclark54359292015-03-26 07:52:43 -07001211 if (oppMatched) {
1212 oppFirst->fCoinEnd.markCoincident();
1213 oppHalf->markCoincident();
1214 oppFirst = oppHalf;
1215 } else {
1216 oppFirst->markCoincident();
1217 oppHalf->fCoinStart.markCoincident();
1218 }
1219 }
1220 } else {
1221 SkDEBUGCODE(coinStart = first->fStartT);
caryclarka35ab3e2016-10-20 08:32:18 -07001222 FAIL_IF(!oppFirst);
caryclark54359292015-03-26 07:52:43 -07001223 SkDEBUGCODE(oppStartT = oppMatched ? oppFirst->fStartT : oppFirst->fEndT);
1224 }
caryclark1049f122015-04-20 08:31:59 -07001225 // FIXME: incomplete : if we're not at the end, find end of coin
1226 SkTSpan<OppCurve, TCurve>* oppLast;
caryclark6c3b9cd2016-09-26 05:36:58 -07001227 SkOPASSERT(last->fCoinEnd.isMatch());
caryclark54359292015-03-26 07:52:43 -07001228 oppLast = last->findOppT(last->fCoinEnd.perpT());
1229 SkDEBUGCODE(coinEnd = last->fEndT);
caryclark643ede62016-08-08 14:27:45 -07001230#ifdef SK_DEBUG
1231 if (!this->globalState() || !this->globalState()->debugSkipAssert()) {
1232 oppEndT = oppMatched ? oppLast->fEndT : oppLast->fStartT;
1233 }
1234#endif
caryclark54359292015-03-26 07:52:43 -07001235 if (!oppMatched) {
1236 SkTSwap(oppFirst, oppLast);
1237 SkTSwap(oppStartT, oppEndT);
1238 }
caryclarke25a4f62016-07-26 09:26:29 -07001239 SkOPASSERT(oppStartT < oppEndT);
caryclark54359292015-03-26 07:52:43 -07001240 SkASSERT(coinStart == first->fStartT);
1241 SkASSERT(coinEnd == last->fEndT);
caryclark643ede62016-08-08 14:27:45 -07001242 SkOPASSERT(oppStartT == oppFirst->fStartT);
1243 SkOPASSERT(oppEndT == oppLast->fEndT);
1244 if (!oppFirst) {
caryclarkef7cee42016-09-06 09:05:54 -07001245 *result = nullptr;
1246 return true;
caryclark643ede62016-08-08 14:27:45 -07001247 }
caryclark42942862016-08-19 07:01:33 -07001248 if (!oppLast) {
caryclarkef7cee42016-09-06 09:05:54 -07001249 *result = nullptr;
1250 return true;
caryclark42942862016-08-19 07:01:33 -07001251 }
caryclark54359292015-03-26 07:52:43 -07001252 // reduce coincident runs to single entries
1253 this->validate();
1254 sect2->validate();
caryclark1049f122015-04-20 08:31:59 -07001255 bool deleteEmptySpans = this->updateBounded(first, last, oppFirst);
1256 deleteEmptySpans |= sect2->updateBounded(oppFirst, oppLast, first);
caryclark54359292015-03-26 07:52:43 -07001257 this->removeSpanRange(first, last);
1258 sect2->removeSpanRange(oppFirst, oppLast);
1259 first->fEndT = last->fEndT;
1260 first->resetBounds(this->fCurve);
1261 first->fCoinStart.setPerp(fCurve, first->fStartT, first->fPart[0], sect2->fCurve);
1262 first->fCoinEnd.setPerp(fCurve, first->fEndT, first->fPart[TCurve::kPointLast], sect2->fCurve);
1263 oppStartT = first->fCoinStart.perpT();
1264 oppEndT = first->fCoinEnd.perpT();
1265 if (between(0, oppStartT, 1) && between(0, oppEndT, 1)) {
1266 if (!oppMatched) {
1267 SkTSwap(oppStartT, oppEndT);
1268 }
1269 oppFirst->fStartT = oppStartT;
1270 oppFirst->fEndT = oppEndT;
1271 oppFirst->resetBounds(sect2->fCurve);
1272 }
1273 this->validateBounded();
1274 sect2->validateBounded();
1275 last = first->fNext;
Cary Clark38702ab2017-09-05 18:11:55 -04001276 if (!this->removeCoincident(first, false)) {
1277 return false;
1278 }
1279 if (!sect2->removeCoincident(oppFirst, true)) {
1280 return false;
1281 }
caryclark1049f122015-04-20 08:31:59 -07001282 if (deleteEmptySpans) {
caryclarkef7cee42016-09-06 09:05:54 -07001283 if (!this->deleteEmptySpans() || !sect2->deleteEmptySpans()) {
1284 *result = nullptr;
1285 return false;
1286 }
caryclark54359292015-03-26 07:52:43 -07001287 }
1288 this->validate();
1289 sect2->validate();
caryclarkef7cee42016-09-06 09:05:54 -07001290 *result = last && !last->fDeleted && fHead && sect2->fHead ? last : nullptr;
1291 return true;
caryclark54359292015-03-26 07:52:43 -07001292}
1293
caryclark1049f122015-04-20 08:31:59 -07001294template<typename TCurve, typename OppCurve>
1295SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::findCoincidentRun(
1296 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>** lastPtr) {
1297 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001298 SkTSpan<TCurve, OppCurve>* lastCandidate = nullptr;
1299 first = nullptr;
caryclark54359292015-03-26 07:52:43 -07001300 // find the first fully coincident span
1301 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07001302 if (work->fCoinStart.isMatch()) {
caryclark1049f122015-04-20 08:31:59 -07001303#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -07001304 work->validatePerpT(work->fCoinStart.perpT());
1305 work->validatePerpPt(work->fCoinStart.perpT(), work->fCoinStart.perpPt());
caryclark1049f122015-04-20 08:31:59 -07001306#endif
caryclarka35ab3e2016-10-20 08:32:18 -07001307 SkOPASSERT(work->hasOppT(work->fCoinStart.perpT()));
caryclark6c3b9cd2016-09-26 05:36:58 -07001308 if (!work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001309 break;
1310 }
1311 lastCandidate = work;
1312 if (!first) {
1313 first = work;
1314 }
caryclark1049f122015-04-20 08:31:59 -07001315 } else if (first && work->fCollapsed) {
1316 *lastPtr = lastCandidate;
1317 return first;
caryclark54359292015-03-26 07:52:43 -07001318 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001319 lastCandidate = nullptr;
caryclark643ede62016-08-08 14:27:45 -07001320 SkOPASSERT(!first);
caryclark54359292015-03-26 07:52:43 -07001321 }
1322 if (work == *lastPtr) {
1323 return first;
1324 }
1325 work = work->fNext;
caryclarke25a4f62016-07-26 09:26:29 -07001326 if (!work) {
1327 return nullptr;
1328 }
caryclark54359292015-03-26 07:52:43 -07001329 } while (true);
1330 if (lastCandidate) {
1331 *lastPtr = lastCandidate;
1332 }
1333 return first;
1334}
1335
caryclark1049f122015-04-20 08:31:59 -07001336template<typename TCurve, typename OppCurve>
1337int SkTSect<TCurve, OppCurve>::intersects(SkTSpan<TCurve, OppCurve>* span,
1338 SkTSect<OppCurve, TCurve>* opp,
1339 SkTSpan<OppCurve, TCurve>* oppSpan, int* oppResult) {
caryclark54359292015-03-26 07:52:43 -07001340 bool spanStart, oppStart;
1341 int hullResult = span->hullsIntersect(oppSpan, &spanStart, &oppStart);
1342 if (hullResult >= 0) {
1343 if (hullResult == 2) { // hulls have one point in common
1344 if (!span->fBounded || !span->fBounded->fNext) {
1345 SkASSERT(!span->fBounded || span->fBounded->fBounded == oppSpan);
1346 if (spanStart) {
1347 span->fEndT = span->fStartT;
caryclark45fa4472015-01-16 07:04:10 -08001348 } else {
caryclark54359292015-03-26 07:52:43 -07001349 span->fStartT = span->fEndT;
1350 }
1351 } else {
1352 hullResult = 1;
1353 }
1354 if (!oppSpan->fBounded || !oppSpan->fBounded->fNext) {
1355 SkASSERT(!oppSpan->fBounded || oppSpan->fBounded->fBounded == span);
1356 if (oppStart) {
1357 oppSpan->fEndT = oppSpan->fStartT;
1358 } else {
1359 oppSpan->fStartT = oppSpan->fEndT;
1360 }
1361 *oppResult = 2;
1362 } else {
1363 *oppResult = 1;
1364 }
1365 } else {
1366 *oppResult = 1;
1367 }
1368 return hullResult;
1369 }
1370 if (span->fIsLine && oppSpan->fIsLine) {
1371 SkIntersections i;
1372 int sects = this->linesIntersect(span, opp, oppSpan, &i);
caryclark26ad22a2015-10-16 09:03:38 -07001373 if (sects == 2) {
1374 return *oppResult = 1;
1375 }
caryclark54359292015-03-26 07:52:43 -07001376 if (!sects) {
1377 return -1;
1378 }
caryclark34efb702016-10-24 08:19:06 -07001379 this->removedEndCheck(span);
caryclark54359292015-03-26 07:52:43 -07001380 span->fStartT = span->fEndT = i[0][0];
caryclark34efb702016-10-24 08:19:06 -07001381 opp->removedEndCheck(oppSpan);
caryclark54359292015-03-26 07:52:43 -07001382 oppSpan->fStartT = oppSpan->fEndT = i[1][0];
1383 return *oppResult = 2;
1384 }
1385 if (span->fIsLinear || oppSpan->fIsLinear) {
1386 return *oppResult = (int) span->linearsIntersect(oppSpan);
1387 }
1388 return *oppResult = 1;
1389}
1390
caryclarked0935a2015-10-22 07:23:52 -07001391template<typename TCurve>
1392static bool is_parallel(const SkDLine& thisLine, const TCurve& opp) {
1393 if (!opp.IsConic()) {
1394 return false; // FIXME : breaks a lot of stuff now
1395 }
1396 int finds = 0;
1397 SkDLine thisPerp;
1398 thisPerp.fPts[0].fX = thisLine.fPts[1].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1399 thisPerp.fPts[0].fY = thisLine.fPts[1].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1400 thisPerp.fPts[1] = thisLine.fPts[1];
1401 SkIntersections perpRayI;
1402 perpRayI.intersectRay(opp, thisPerp);
1403 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1404 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[1]);
1405 }
1406 thisPerp.fPts[1].fX = thisLine.fPts[0].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1407 thisPerp.fPts[1].fY = thisLine.fPts[0].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1408 thisPerp.fPts[0] = thisLine.fPts[0];
1409 perpRayI.intersectRay(opp, thisPerp);
1410 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1411 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[0]);
1412 }
1413 return finds >= 2;
1414}
1415
caryclark54359292015-03-26 07:52:43 -07001416// while the intersection points are sufficiently far apart:
1417// construct the tangent lines from the intersections
1418// find the point where the tangent line intersects the opposite curve
caryclark1049f122015-04-20 08:31:59 -07001419template<typename TCurve, typename OppCurve>
1420int SkTSect<TCurve, OppCurve>::linesIntersect(SkTSpan<TCurve, OppCurve>* span,
1421 SkTSect<OppCurve, TCurve>* opp,
1422 SkTSpan<OppCurve, TCurve>* oppSpan, SkIntersections* i) {
caryclarka35ab3e2016-10-20 08:32:18 -07001423 SkIntersections thisRayI SkDEBUGCODE((span->fDebugGlobalState));
1424 SkIntersections oppRayI SkDEBUGCODE((span->fDebugGlobalState));
caryclark54359292015-03-26 07:52:43 -07001425 SkDLine thisLine = {{ span->fPart[0], span->fPart[TCurve::kPointLast] }};
caryclark1049f122015-04-20 08:31:59 -07001426 SkDLine oppLine = {{ oppSpan->fPart[0], oppSpan->fPart[OppCurve::kPointLast] }};
caryclark54359292015-03-26 07:52:43 -07001427 int loopCount = 0;
1428 double bestDistSq = DBL_MAX;
caryclark1049f122015-04-20 08:31:59 -07001429 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1430 return 0;
1431 }
1432 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1433 return 0;
1434 }
caryclark26ad22a2015-10-16 09:03:38 -07001435 // if the ends of each line intersect the opposite curve, the lines are coincident
1436 if (thisRayI.used() > 1) {
1437 int ptMatches = 0;
1438 for (int tIndex = 0; tIndex < thisRayI.used(); ++tIndex) {
1439 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(thisLine.fPts); ++lIndex) {
1440 ptMatches += thisRayI.pt(tIndex).approximatelyEqual(thisLine.fPts[lIndex]);
1441 }
1442 }
caryclarked0935a2015-10-22 07:23:52 -07001443 if (ptMatches == 2 || is_parallel(thisLine, opp->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001444 return 2;
1445 }
1446 }
1447 if (oppRayI.used() > 1) {
1448 int ptMatches = 0;
1449 for (int oIndex = 0; oIndex < oppRayI.used(); ++oIndex) {
Cary Clarkd80870f2017-10-17 11:57:26 -04001450 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(oppLine.fPts); ++lIndex) {
caryclark26ad22a2015-10-16 09:03:38 -07001451 ptMatches += oppRayI.pt(oIndex).approximatelyEqual(oppLine.fPts[lIndex]);
1452 }
1453 }
caryclarked0935a2015-10-22 07:23:52 -07001454 if (ptMatches == 2|| is_parallel(oppLine, this->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001455 return 2;
1456 }
1457 }
caryclark54359292015-03-26 07:52:43 -07001458 do {
caryclark54359292015-03-26 07:52:43 -07001459 // pick the closest pair of points
1460 double closest = DBL_MAX;
1461 int closeIndex SK_INIT_TO_AVOID_WARNING;
1462 int oppCloseIndex SK_INIT_TO_AVOID_WARNING;
1463 for (int index = 0; index < oppRayI.used(); ++index) {
1464 if (!roughly_between(span->fStartT, oppRayI[0][index], span->fEndT)) {
1465 continue;
1466 }
1467 for (int oIndex = 0; oIndex < thisRayI.used(); ++oIndex) {
1468 if (!roughly_between(oppSpan->fStartT, thisRayI[0][oIndex], oppSpan->fEndT)) {
1469 continue;
1470 }
1471 double distSq = thisRayI.pt(index).distanceSquared(oppRayI.pt(oIndex));
1472 if (closest > distSq) {
1473 closest = distSq;
1474 closeIndex = index;
1475 oppCloseIndex = oIndex;
caryclarkccec0f92015-03-24 07:28:17 -07001476 }
caryclarkccec0f92015-03-24 07:28:17 -07001477 }
reed0dc4dd62015-03-24 13:55:33 -07001478 }
caryclark54359292015-03-26 07:52:43 -07001479 if (closest == DBL_MAX) {
caryclark1049f122015-04-20 08:31:59 -07001480 break;
reed0dc4dd62015-03-24 13:55:33 -07001481 }
caryclark54359292015-03-26 07:52:43 -07001482 const SkDPoint& oppIPt = thisRayI.pt(oppCloseIndex);
1483 const SkDPoint& iPt = oppRayI.pt(closeIndex);
1484 if (between(span->fStartT, oppRayI[0][closeIndex], span->fEndT)
1485 && between(oppSpan->fStartT, thisRayI[0][oppCloseIndex], oppSpan->fEndT)
1486 && oppIPt.approximatelyEqual(iPt)) {
1487 i->merge(oppRayI, closeIndex, thisRayI, oppCloseIndex);
1488 return i->used();
1489 }
1490 double distSq = oppIPt.distanceSquared(iPt);
1491 if (bestDistSq < distSq || ++loopCount > 5) {
caryclark1049f122015-04-20 08:31:59 -07001492 return 0;
caryclark54359292015-03-26 07:52:43 -07001493 }
1494 bestDistSq = distSq;
caryclark1049f122015-04-20 08:31:59 -07001495 double oppStart = oppRayI[0][closeIndex];
1496 thisLine[0] = fCurve.ptAtT(oppStart);
1497 thisLine[1] = thisLine[0] + fCurve.dxdyAtT(oppStart);
1498 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1499 break;
1500 }
1501 double start = thisRayI[0][oppCloseIndex];
1502 oppLine[0] = opp->fCurve.ptAtT(start);
1503 oppLine[1] = oppLine[0] + opp->fCurve.dxdyAtT(start);
1504 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1505 break;
1506 }
caryclark54359292015-03-26 07:52:43 -07001507 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001508 // convergence may fail if the curves are nearly coincident
1509 SkTCoincident<OppCurve, TCurve> oCoinS, oCoinE;
1510 oCoinS.setPerp(opp->fCurve, oppSpan->fStartT, oppSpan->fPart[0], fCurve);
1511 oCoinE.setPerp(opp->fCurve, oppSpan->fEndT, oppSpan->fPart[OppCurve::kPointLast], fCurve);
1512 double tStart = oCoinS.perpT();
1513 double tEnd = oCoinE.perpT();
1514 bool swap = tStart > tEnd;
1515 if (swap) {
1516 SkTSwap(tStart, tEnd);
1517 }
1518 tStart = SkTMax(tStart, span->fStartT);
1519 tEnd = SkTMin(tEnd, span->fEndT);
1520 if (tStart > tEnd) {
1521 return 0;
1522 }
1523 SkDVector perpS, perpE;
1524 if (tStart == span->fStartT) {
1525 SkTCoincident<TCurve, OppCurve> coinS;
1526 coinS.setPerp(fCurve, span->fStartT, span->fPart[0], opp->fCurve);
1527 perpS = span->fPart[0] - coinS.perpPt();
1528 } else if (swap) {
1529 perpS = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1530 } else {
1531 perpS = oCoinS.perpPt() - oppSpan->fPart[0];
1532 }
1533 if (tEnd == span->fEndT) {
1534 SkTCoincident<TCurve, OppCurve> coinE;
1535 coinE.setPerp(fCurve, span->fEndT, span->fPart[TCurve::kPointLast], opp->fCurve);
1536 perpE = span->fPart[TCurve::kPointLast] - coinE.perpPt();
1537 } else if (swap) {
1538 perpE = oCoinS.perpPt() - oppSpan->fPart[0];
1539 } else {
1540 perpE = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1541 }
1542 if (perpS.dot(perpE) >= 0) {
1543 return 0;
1544 }
1545 SkTCoincident<TCurve, OppCurve> coinW;
1546 double workT = tStart;
1547 double tStep = tEnd - tStart;
1548 SkDPoint workPt;
1549 do {
1550 tStep *= 0.5;
1551 if (precisely_zero(tStep)) {
1552 return 0;
1553 }
1554 workT += tStep;
1555 workPt = fCurve.ptAtT(workT);
1556 coinW.setPerp(fCurve, workT, workPt, opp->fCurve);
caryclark27c015d2016-09-23 05:47:20 -07001557 double perpT = coinW.perpT();
caryclark6c3b9cd2016-09-26 05:36:58 -07001558 if (coinW.isMatch() ? !between(oppSpan->fStartT, perpT, oppSpan->fEndT) : perpT < 0) {
caryclarkb6693002015-12-16 12:28:35 -08001559 continue;
1560 }
caryclark1049f122015-04-20 08:31:59 -07001561 SkDVector perpW = workPt - coinW.perpPt();
1562 if ((perpS.dot(perpW) >= 0) == (tStep < 0)) {
1563 tStep = -tStep;
1564 }
caryclarkb6693002015-12-16 12:28:35 -08001565 if (workPt.approximatelyEqual(coinW.perpPt())) {
1566 break;
1567 }
1568 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001569 double oppTTest = coinW.perpT();
1570 if (!opp->fHead->contains(oppTTest)) {
1571 return 0;
1572 }
1573 i->setMax(1);
1574 i->insert(workT, oppTTest, workPt);
1575 return 1;
caryclark54359292015-03-26 07:52:43 -07001576}
1577
1578
caryclark1049f122015-04-20 08:31:59 -07001579template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001580bool SkTSect<TCurve, OppCurve>::markSpanGone(SkTSpan<TCurve, OppCurve>* span) {
1581 if (--fActiveCount < 0) {
1582 return false;
1583 }
caryclark54359292015-03-26 07:52:43 -07001584 span->fNext = fDeleted;
1585 fDeleted = span;
caryclarke25a4f62016-07-26 09:26:29 -07001586 SkOPASSERT(!span->fDeleted);
caryclark54359292015-03-26 07:52:43 -07001587 span->fDeleted = true;
caryclarkef7cee42016-09-06 09:05:54 -07001588 return true;
caryclark54359292015-03-26 07:52:43 -07001589}
1590
caryclark1049f122015-04-20 08:31:59 -07001591template<typename TCurve, typename OppCurve>
1592bool SkTSect<TCurve, OppCurve>::matchedDirection(double t, const SkTSect<OppCurve, TCurve>* sect2,
1593 double t2) const {
caryclark54359292015-03-26 07:52:43 -07001594 SkDVector dxdy = this->fCurve.dxdyAtT(t);
1595 SkDVector dxdy2 = sect2->fCurve.dxdyAtT(t2);
1596 return dxdy.dot(dxdy2) >= 0;
1597}
1598
caryclark1049f122015-04-20 08:31:59 -07001599template<typename TCurve, typename OppCurve>
1600void SkTSect<TCurve, OppCurve>::matchedDirCheck(double t, const SkTSect<OppCurve, TCurve>* sect2,
1601 double t2, bool* calcMatched, bool* oppMatched) const {
caryclark54359292015-03-26 07:52:43 -07001602 if (*calcMatched) {
caryclark55888e42016-07-18 10:01:36 -07001603 SkASSERT(*oppMatched == this->matchedDirection(t, sect2, t2));
caryclark54359292015-03-26 07:52:43 -07001604 } else {
1605 *oppMatched = this->matchedDirection(t, sect2, t2);
1606 *calcMatched = true;
1607 }
1608}
1609
caryclark1049f122015-04-20 08:31:59 -07001610template<typename TCurve, typename OppCurve>
1611void SkTSect<TCurve, OppCurve>::mergeCoincidence(SkTSect<OppCurve, TCurve>* sect2) {
caryclark54359292015-03-26 07:52:43 -07001612 double smallLimit = 0;
1613 do {
1614 // find the smallest unprocessed span
halcanary96fcdcc2015-08-27 07:41:13 -07001615 SkTSpan<TCurve, OppCurve>* smaller = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001616 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001617 do {
caryclark221a4bb2016-10-07 11:15:15 -07001618 if (!test) {
1619 return;
1620 }
caryclark54359292015-03-26 07:52:43 -07001621 if (test->fStartT < smallLimit) {
1622 continue;
1623 }
1624 if (smaller && smaller->fEndT < test->fStartT) {
1625 continue;
1626 }
1627 smaller = test;
1628 } while ((test = test->fNext));
1629 if (!smaller) {
1630 return;
1631 }
1632 smallLimit = smaller->fEndT;
1633 // find next larger span
halcanary96fcdcc2015-08-27 07:41:13 -07001634 SkTSpan<TCurve, OppCurve>* prior = nullptr;
1635 SkTSpan<TCurve, OppCurve>* larger = nullptr;
1636 SkTSpan<TCurve, OppCurve>* largerPrior = nullptr;
caryclark54359292015-03-26 07:52:43 -07001637 test = fCoincident;
1638 do {
1639 if (test->fStartT < smaller->fEndT) {
1640 continue;
1641 }
caryclark221a4bb2016-10-07 11:15:15 -07001642 SkOPASSERT(test->fStartT != smaller->fEndT);
caryclark54359292015-03-26 07:52:43 -07001643 if (larger && larger->fStartT < test->fStartT) {
1644 continue;
1645 }
1646 largerPrior = prior;
1647 larger = test;
1648 } while ((prior = test), (test = test->fNext));
1649 if (!larger) {
1650 continue;
1651 }
1652 // check middle t value to see if it is coincident as well
1653 double midT = (smaller->fEndT + larger->fStartT) / 2;
1654 SkDPoint midPt = fCurve.ptAtT(midT);
caryclark1049f122015-04-20 08:31:59 -07001655 SkTCoincident<TCurve, OppCurve> coin;
caryclark54359292015-03-26 07:52:43 -07001656 coin.setPerp(fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07001657 if (coin.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001658 smaller->fEndT = larger->fEndT;
1659 smaller->fCoinEnd = larger->fCoinEnd;
1660 if (largerPrior) {
1661 largerPrior->fNext = larger->fNext;
caryclark643ede62016-08-08 14:27:45 -07001662 largerPrior->validate();
caryclark54359292015-03-26 07:52:43 -07001663 } else {
1664 fCoincident = larger->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001665 }
1666 }
caryclark54359292015-03-26 07:52:43 -07001667 } while (true);
1668}
1669
caryclark1049f122015-04-20 08:31:59 -07001670template<typename TCurve, typename OppCurve>
1671SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::prev(
1672 SkTSpan<TCurve, OppCurve>* span) const {
halcanary96fcdcc2015-08-27 07:41:13 -07001673 SkTSpan<TCurve, OppCurve>* result = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001674 SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001675 while (span != test) {
1676 result = test;
1677 test = test->fNext;
1678 SkASSERT(test);
caryclarkccec0f92015-03-24 07:28:17 -07001679 }
caryclark55888e42016-07-18 10:01:36 -07001680 return result;
caryclarkccec0f92015-03-24 07:28:17 -07001681}
1682
caryclark1049f122015-04-20 08:31:59 -07001683template<typename TCurve, typename OppCurve>
1684void SkTSect<TCurve, OppCurve>::recoverCollapsed() {
1685 SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001686 while (deleted) {
caryclark1049f122015-04-20 08:31:59 -07001687 SkTSpan<TCurve, OppCurve>* delNext = deleted->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001688 if (deleted->fCollapsed) {
caryclark1049f122015-04-20 08:31:59 -07001689 SkTSpan<TCurve, OppCurve>** spanPtr = &fHead;
caryclark45fa4472015-01-16 07:04:10 -08001690 while (*spanPtr && (*spanPtr)->fEndT <= deleted->fStartT) {
1691 spanPtr = &(*spanPtr)->fNext;
1692 }
1693 deleted->fNext = *spanPtr;
1694 *spanPtr = deleted;
1695 }
1696 deleted = delNext;
1697 }
1698}
1699
caryclark1049f122015-04-20 08:31:59 -07001700template<typename TCurve, typename OppCurve>
1701void SkTSect<TCurve, OppCurve>::removeAllBut(const SkTSpan<OppCurve, TCurve>* keep,
1702 SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp) {
1703 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001704 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001705 SkTSpan<OppCurve, TCurve>* bounded = testBounded->fBounded;
1706 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001707 // may have been deleted when opp did 'remove all but'
1708 if (bounded != keep && !bounded->fDeleted) {
1709 SkAssertResult(SkDEBUGCODE(!) span->removeBounded(bounded));
1710 if (bounded->removeBounded(span)) {
1711 opp->removeSpan(bounded);
1712 }
caryclarkccec0f92015-03-24 07:28:17 -07001713 }
caryclark54359292015-03-26 07:52:43 -07001714 testBounded = next;
caryclarkccec0f92015-03-24 07:28:17 -07001715 }
caryclark54359292015-03-26 07:52:43 -07001716 SkASSERT(!span->fDeleted);
1717 SkASSERT(span->findOppSpan(keep));
1718 SkASSERT(keep->findOppSpan(span));
caryclarkccec0f92015-03-24 07:28:17 -07001719}
1720
caryclark1049f122015-04-20 08:31:59 -07001721template<typename TCurve, typename OppCurve>
1722void SkTSect<TCurve, OppCurve>::removeByPerpendicular(SkTSect<OppCurve, TCurve>* opp) {
1723 SkTSpan<TCurve, OppCurve>* test = fHead;
1724 SkTSpan<TCurve, OppCurve>* next;
caryclark54359292015-03-26 07:52:43 -07001725 do {
1726 next = test->fNext;
1727 if (test->fCoinStart.perpT() < 0 || test->fCoinEnd.perpT() < 0) {
1728 continue;
reed0dc4dd62015-03-24 13:55:33 -07001729 }
caryclark54359292015-03-26 07:52:43 -07001730 SkDVector startV = test->fCoinStart.perpPt() - test->fPart[0];
1731 SkDVector endV = test->fCoinEnd.perpPt() - test->fPart[TCurve::kPointLast];
1732#if DEBUG_T_SECT
1733 SkDebugf("%s startV=(%1.9g,%1.9g) endV=(%1.9g,%1.9g) dot=%1.9g\n", __FUNCTION__,
1734 startV.fX, startV.fY, endV.fX, endV.fY, startV.dot(endV));
1735#endif
1736 if (startV.dot(endV) <= 0) {
1737 continue;
1738 }
1739 this->removeSpans(test, opp);
1740 } while ((test = next));
1741}
1742
caryclark1049f122015-04-20 08:31:59 -07001743template<typename TCurve, typename OppCurve>
Cary Clark38702ab2017-09-05 18:11:55 -04001744bool SkTSect<TCurve, OppCurve>::removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween) {
1745 if (!this->unlinkSpan(span)) {
1746 return false;
1747 }
caryclark54359292015-03-26 07:52:43 -07001748 if (isBetween || between(0, span->fCoinStart.perpT(), 1)) {
1749 --fActiveCount;
1750 span->fNext = fCoincident;
1751 fCoincident = span;
1752 } else {
1753 this->markSpanGone(span);
reed0dc4dd62015-03-24 13:55:33 -07001754 }
Cary Clark38702ab2017-09-05 18:11:55 -04001755 return true;
caryclarkccec0f92015-03-24 07:28:17 -07001756}
1757
caryclark1049f122015-04-20 08:31:59 -07001758template<typename TCurve, typename OppCurve>
caryclark34efb702016-10-24 08:19:06 -07001759void SkTSect<TCurve, OppCurve>::removedEndCheck(SkTSpan<TCurve, OppCurve>* span) {
caryclark6c3b9cd2016-09-26 05:36:58 -07001760 if (!span->fStartT) {
1761 fRemovedStartT = true;
1762 }
1763 if (1 == span->fEndT) {
1764 fRemovedEndT = true;
1765 }
caryclark34efb702016-10-24 08:19:06 -07001766}
1767
1768template<typename TCurve, typename OppCurve>
1769bool SkTSect<TCurve, OppCurve>::removeSpan(SkTSpan<TCurve, OppCurve>* span) {\
1770 this->removedEndCheck(span);
Cary Clark38702ab2017-09-05 18:11:55 -04001771 if (!this->unlinkSpan(span)) {
1772 return false;
1773 }
caryclarkef7cee42016-09-06 09:05:54 -07001774 return this->markSpanGone(span);
caryclark54359292015-03-26 07:52:43 -07001775}
1776
caryclark1049f122015-04-20 08:31:59 -07001777template<typename TCurve, typename OppCurve>
1778void SkTSect<TCurve, OppCurve>::removeSpanRange(SkTSpan<TCurve, OppCurve>* first,
1779 SkTSpan<TCurve, OppCurve>* last) {
caryclark54359292015-03-26 07:52:43 -07001780 if (first == last) {
1781 return;
1782 }
caryclark1049f122015-04-20 08:31:59 -07001783 SkTSpan<TCurve, OppCurve>* span = first;
caryclark54359292015-03-26 07:52:43 -07001784 SkASSERT(span);
caryclark1049f122015-04-20 08:31:59 -07001785 SkTSpan<TCurve, OppCurve>* final = last->fNext;
1786 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001787 while ((span = next) && span != final) {
1788 next = span->fNext;
1789 this->markSpanGone(span);
1790 }
1791 if (final) {
1792 final->fPrev = first;
1793 }
1794 first->fNext = final;
caryclark643ede62016-08-08 14:27:45 -07001795 first->validate();
caryclark54359292015-03-26 07:52:43 -07001796}
1797
caryclark1049f122015-04-20 08:31:59 -07001798template<typename TCurve, typename OppCurve>
1799void SkTSect<TCurve, OppCurve>::removeSpans(SkTSpan<TCurve, OppCurve>* span,
1800 SkTSect<OppCurve, TCurve>* opp) {
1801 SkTSpanBounded<OppCurve, TCurve>* bounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001802 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -07001803 SkTSpan<OppCurve, TCurve>* spanBounded = bounded->fBounded;
1804 SkTSpanBounded<OppCurve, TCurve>* next = bounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001805 if (span->removeBounded(spanBounded)) { // shuffles last into position 0
1806 this->removeSpan(span);
1807 }
1808 if (spanBounded->removeBounded(span)) {
1809 opp->removeSpan(spanBounded);
1810 }
1811 SkASSERT(!span->fDeleted || !opp->debugHasBounded(span));
1812 bounded = next;
reed0dc4dd62015-03-24 13:55:33 -07001813 }
1814}
caryclarkccec0f92015-03-24 07:28:17 -07001815
caryclark1049f122015-04-20 08:31:59 -07001816template<typename TCurve, typename OppCurve>
1817SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::spanAtT(double t,
1818 SkTSpan<TCurve, OppCurve>** priorSpan) {
1819 SkTSpan<TCurve, OppCurve>* test = fHead;
halcanary96fcdcc2015-08-27 07:41:13 -07001820 SkTSpan<TCurve, OppCurve>* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -07001821 while (test && test->fEndT < t) {
1822 prev = test;
1823 test = test->fNext;
reed0dc4dd62015-03-24 13:55:33 -07001824 }
caryclark54359292015-03-26 07:52:43 -07001825 *priorSpan = prev;
halcanary96fcdcc2015-08-27 07:41:13 -07001826 return test && test->fStartT <= t ? test : nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001827}
1828
caryclark1049f122015-04-20 08:31:59 -07001829template<typename TCurve, typename OppCurve>
1830SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::tail() {
1831 SkTSpan<TCurve, OppCurve>* result = fHead;
1832 SkTSpan<TCurve, OppCurve>* next = fHead;
reed0dc4dd62015-03-24 13:55:33 -07001833 while ((next = next->fNext)) {
1834 if (next->fEndT > result->fEndT) {
1835 result = next;
1836 }
1837 }
1838 return result;
1839}
1840
1841/* Each span has a range of opposite spans it intersects. After the span is split in two,
1842 adjust the range to its new size */
caryclark1049f122015-04-20 08:31:59 -07001843template<typename TCurve, typename OppCurve>
caryclarka35ab3e2016-10-20 08:32:18 -07001844bool SkTSect<TCurve, OppCurve>::trim(SkTSpan<TCurve, OppCurve>* span,
caryclark1049f122015-04-20 08:31:59 -07001845 SkTSect<OppCurve, TCurve>* opp) {
caryclarka35ab3e2016-10-20 08:32:18 -07001846 FAIL_IF(!span->initBounds(fCurve));
caryclark1049f122015-04-20 08:31:59 -07001847 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001848 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001849 SkTSpan<OppCurve, TCurve>* test = testBounded->fBounded;
1850 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001851 int oppSects, sects = this->intersects(span, opp, test, &oppSects);
1852 if (sects >= 1) {
1853 if (oppSects == 2) {
1854 test->initBounds(opp->fCurve);
1855 opp->removeAllBut(span, test, this);
1856 }
1857 if (sects == 2) {
1858 span->initBounds(fCurve);
1859 this->removeAllBut(test, span, opp);
caryclarka35ab3e2016-10-20 08:32:18 -07001860 return true;
caryclark54359292015-03-26 07:52:43 -07001861 }
reed0dc4dd62015-03-24 13:55:33 -07001862 } else {
caryclark54359292015-03-26 07:52:43 -07001863 if (span->removeBounded(test)) {
1864 this->removeSpan(span);
1865 }
1866 if (test->removeBounded(span)) {
1867 opp->removeSpan(test);
1868 }
1869 }
1870 testBounded = next;
1871 }
caryclarka35ab3e2016-10-20 08:32:18 -07001872 return true;
caryclark54359292015-03-26 07:52:43 -07001873}
1874
caryclark1049f122015-04-20 08:31:59 -07001875template<typename TCurve, typename OppCurve>
Cary Clark38702ab2017-09-05 18:11:55 -04001876bool SkTSect<TCurve, OppCurve>::unlinkSpan(SkTSpan<TCurve, OppCurve>* span) {
caryclark1049f122015-04-20 08:31:59 -07001877 SkTSpan<TCurve, OppCurve>* prev = span->fPrev;
1878 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001879 if (prev) {
1880 prev->fNext = next;
1881 if (next) {
1882 next->fPrev = prev;
Cary Clark38702ab2017-09-05 18:11:55 -04001883 if (next->fStartT > next->fEndT) {
1884 return false;
1885 }
caryclark643ede62016-08-08 14:27:45 -07001886 next->validate();
caryclark54359292015-03-26 07:52:43 -07001887 }
1888 } else {
1889 fHead = next;
1890 if (next) {
halcanary96fcdcc2015-08-27 07:41:13 -07001891 next->fPrev = nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001892 }
1893 }
Cary Clark38702ab2017-09-05 18:11:55 -04001894 return true;
reed0dc4dd62015-03-24 13:55:33 -07001895}
1896
caryclark1049f122015-04-20 08:31:59 -07001897template<typename TCurve, typename OppCurve>
1898bool SkTSect<TCurve, OppCurve>::updateBounded(SkTSpan<TCurve, OppCurve>* first,
1899 SkTSpan<TCurve, OppCurve>* last, SkTSpan<OppCurve, TCurve>* oppFirst) {
1900 SkTSpan<TCurve, OppCurve>* test = first;
1901 const SkTSpan<TCurve, OppCurve>* final = last->next();
caryclark54359292015-03-26 07:52:43 -07001902 bool deleteSpan = false;
1903 do {
1904 deleteSpan |= test->removeAllBounded();
caryclarke25a4f62016-07-26 09:26:29 -07001905 } while ((test = test->fNext) != final && test);
halcanary96fcdcc2015-08-27 07:41:13 -07001906 first->fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -07001907 first->addBounded(oppFirst, &fHeap);
1908 // cannot call validate until remove span range is called
1909 return deleteSpan;
1910}
1911
1912
caryclark1049f122015-04-20 08:31:59 -07001913template<typename TCurve, typename OppCurve>
1914void SkTSect<TCurve, OppCurve>::validate() const {
caryclark643ede62016-08-08 14:27:45 -07001915#if DEBUG_VALIDATE
caryclark45fa4472015-01-16 07:04:10 -08001916 int count = 0;
caryclark643ede62016-08-08 14:27:45 -07001917 double last = 0;
caryclark45fa4472015-01-16 07:04:10 -08001918 if (fHead) {
caryclark1049f122015-04-20 08:31:59 -07001919 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark45fa4472015-01-16 07:04:10 -08001920 SkASSERT(!span->fPrev);
caryclark643ede62016-08-08 14:27:45 -07001921 const SkTSpan<TCurve, OppCurve>* next;
caryclark45fa4472015-01-16 07:04:10 -08001922 do {
1923 span->validate();
1924 SkASSERT(span->fStartT >= last);
caryclark643ede62016-08-08 14:27:45 -07001925 last = span->fEndT;
caryclark45fa4472015-01-16 07:04:10 -08001926 ++count;
caryclark643ede62016-08-08 14:27:45 -07001927 next = span->fNext;
1928 SkASSERT(next != span);
1929 } while ((span = next) != nullptr);
caryclark45fa4472015-01-16 07:04:10 -08001930 }
1931 SkASSERT(count == fActiveCount);
caryclark643ede62016-08-08 14:27:45 -07001932#endif
1933#if DEBUG_T_SECT
caryclark45fa4472015-01-16 07:04:10 -08001934 SkASSERT(fActiveCount <= fDebugAllocatedCount);
1935 int deletedCount = 0;
caryclark1049f122015-04-20 08:31:59 -07001936 const SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001937 while (deleted) {
1938 ++deletedCount;
1939 deleted = deleted->fNext;
1940 }
caryclark1049f122015-04-20 08:31:59 -07001941 const SkTSpan<TCurve, OppCurve>* coincident = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001942 while (coincident) {
1943 ++deletedCount;
1944 coincident = coincident->fNext;
1945 }
caryclark45fa4472015-01-16 07:04:10 -08001946 SkASSERT(fActiveCount + deletedCount == fDebugAllocatedCount);
caryclarkccec0f92015-03-24 07:28:17 -07001947#endif
caryclark54359292015-03-26 07:52:43 -07001948}
1949
caryclark1049f122015-04-20 08:31:59 -07001950template<typename TCurve, typename OppCurve>
1951void SkTSect<TCurve, OppCurve>::validateBounded() const {
caryclark643ede62016-08-08 14:27:45 -07001952#if DEBUG_VALIDATE
caryclark54359292015-03-26 07:52:43 -07001953 if (!fHead) {
1954 return;
1955 }
caryclark1049f122015-04-20 08:31:59 -07001956 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark54359292015-03-26 07:52:43 -07001957 do {
1958 span->validateBounded();
halcanary96fcdcc2015-08-27 07:41:13 -07001959 } while ((span = span->fNext) != nullptr);
caryclark54359292015-03-26 07:52:43 -07001960#endif
1961}
caryclark45fa4472015-01-16 07:04:10 -08001962
caryclark1049f122015-04-20 08:31:59 -07001963template<typename TCurve, typename OppCurve>
1964int SkTSect<TCurve, OppCurve>::EndsEqual(const SkTSect<TCurve, OppCurve>* sect1,
1965 const SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark45fa4472015-01-16 07:04:10 -08001966 int zeroOneSet = 0;
caryclark54359292015-03-26 07:52:43 -07001967 if (sect1->fCurve[0] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001968 zeroOneSet |= kZeroS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001969 intersections->insert(0, 0, sect1->fCurve[0]);
1970 }
caryclark1049f122015-04-20 08:31:59 -07001971 if (sect1->fCurve[0] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001972 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark54359292015-03-26 07:52:43 -07001973 intersections->insert(0, 1, sect1->fCurve[0]);
1974 }
1975 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001976 zeroOneSet |= kOneS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001977 intersections->insert(1, 0, sect1->fCurve[TCurve::kPointLast]);
1978 }
caryclark1049f122015-04-20 08:31:59 -07001979 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001980 zeroOneSet |= kOneS1Set | kOneS2Set;
reed0dc4dd62015-03-24 13:55:33 -07001981 intersections->insert(1, 1, sect1->fCurve[TCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001982 }
1983 // check for zero
1984 if (!(zeroOneSet & (kZeroS1Set | kZeroS2Set))
1985 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[0])) {
1986 zeroOneSet |= kZeroS1Set | kZeroS2Set;
1987 intersections->insertNear(0, 0, sect1->fCurve[0], sect2->fCurve[0]);
1988 }
1989 if (!(zeroOneSet & (kZeroS1Set | kOneS2Set))
caryclark1049f122015-04-20 08:31:59 -07001990 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07001991 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark1049f122015-04-20 08:31:59 -07001992 intersections->insertNear(0, 1, sect1->fCurve[0], sect2->fCurve[OppCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001993 }
1994 // check for one
1995 if (!(zeroOneSet & (kOneS1Set | kZeroS2Set))
1996 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[0])) {
1997 zeroOneSet |= kOneS1Set | kZeroS2Set;
1998 intersections->insertNear(1, 0, sect1->fCurve[TCurve::kPointLast], sect2->fCurve[0]);
1999 }
2000 if (!(zeroOneSet & (kOneS1Set | kOneS2Set))
2001 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[
caryclark1049f122015-04-20 08:31:59 -07002002 OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07002003 zeroOneSet |= kOneS1Set | kOneS2Set;
2004 intersections->insertNear(1, 1, sect1->fCurve[TCurve::kPointLast],
caryclark1049f122015-04-20 08:31:59 -07002005 sect2->fCurve[OppCurve::kPointLast]);
caryclark45fa4472015-01-16 07:04:10 -08002006 }
2007 return zeroOneSet;
2008}
2009
caryclark1049f122015-04-20 08:31:59 -07002010template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08002011struct SkClosestRecord {
caryclark54359292015-03-26 07:52:43 -07002012 bool operator<(const SkClosestRecord& rh) const {
2013 return fClosest < rh.fClosest;
2014 }
2015
caryclark45fa4472015-01-16 07:04:10 -08002016 void addIntersection(SkIntersections* intersections) const {
2017 double r1t = fC1Index ? fC1Span->endT() : fC1Span->startT();
2018 double r2t = fC2Index ? fC2Span->endT() : fC2Span->startT();
2019 intersections->insert(r1t, r2t, fC1Span->part()[fC1Index]);
2020 }
2021
caryclark1049f122015-04-20 08:31:59 -07002022 void findEnd(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2,
caryclark45fa4472015-01-16 07:04:10 -08002023 int c1Index, int c2Index) {
2024 const TCurve& c1 = span1->part();
caryclark1049f122015-04-20 08:31:59 -07002025 const OppCurve& c2 = span2->part();
caryclark45fa4472015-01-16 07:04:10 -08002026 if (!c1[c1Index].approximatelyEqual(c2[c2Index])) {
2027 return;
2028 }
2029 double dist = c1[c1Index].distanceSquared(c2[c2Index]);
2030 if (fClosest < dist) {
2031 return;
2032 }
2033 fC1Span = span1;
2034 fC2Span = span2;
2035 fC1StartT = span1->startT();
2036 fC1EndT = span1->endT();
2037 fC2StartT = span2->startT();
2038 fC2EndT = span2->endT();
2039 fC1Index = c1Index;
2040 fC2Index = c2Index;
2041 fClosest = dist;
2042 }
2043
caryclarkcdeff812016-07-22 03:34:19 -07002044 bool matesWith(const SkClosestRecord& mate SkDEBUGPARAMS(SkIntersections* i)) const {
Cary Clark67116382017-01-03 16:25:18 -05002045 SkOPOBJASSERT(i, fC1Span == mate.fC1Span || fC1Span->endT() <= mate.fC1Span->startT()
caryclark45fa4472015-01-16 07:04:10 -08002046 || mate.fC1Span->endT() <= fC1Span->startT());
caryclarkcdeff812016-07-22 03:34:19 -07002047 SkOPOBJASSERT(i, fC2Span == mate.fC2Span || fC2Span->endT() <= mate.fC2Span->startT()
caryclark45fa4472015-01-16 07:04:10 -08002048 || mate.fC2Span->endT() <= fC2Span->startT());
2049 return fC1Span == mate.fC1Span || fC1Span->endT() == mate.fC1Span->startT()
2050 || fC1Span->startT() == mate.fC1Span->endT()
2051 || fC2Span == mate.fC2Span
2052 || fC2Span->endT() == mate.fC2Span->startT()
2053 || fC2Span->startT() == mate.fC2Span->endT();
2054 }
2055
2056 void merge(const SkClosestRecord& mate) {
2057 fC1Span = mate.fC1Span;
2058 fC2Span = mate.fC2Span;
2059 fClosest = mate.fClosest;
2060 fC1Index = mate.fC1Index;
2061 fC2Index = mate.fC2Index;
2062 }
caryclark55888e42016-07-18 10:01:36 -07002063
caryclark45fa4472015-01-16 07:04:10 -08002064 void reset() {
2065 fClosest = FLT_MAX;
halcanary96fcdcc2015-08-27 07:41:13 -07002066 SkDEBUGCODE(fC1Span = nullptr);
2067 SkDEBUGCODE(fC2Span = nullptr);
caryclark45fa4472015-01-16 07:04:10 -08002068 SkDEBUGCODE(fC1Index = fC2Index = -1);
2069 }
2070
2071 void update(const SkClosestRecord& mate) {
2072 fC1StartT = SkTMin(fC1StartT, mate.fC1StartT);
2073 fC1EndT = SkTMax(fC1EndT, mate.fC1EndT);
2074 fC2StartT = SkTMin(fC2StartT, mate.fC2StartT);
2075 fC2EndT = SkTMax(fC2EndT, mate.fC2EndT);
2076 }
2077
caryclark1049f122015-04-20 08:31:59 -07002078 const SkTSpan<TCurve, OppCurve>* fC1Span;
2079 const SkTSpan<OppCurve, TCurve>* fC2Span;
caryclark45fa4472015-01-16 07:04:10 -08002080 double fC1StartT;
2081 double fC1EndT;
2082 double fC2StartT;
2083 double fC2EndT;
2084 double fClosest;
2085 int fC1Index;
2086 int fC2Index;
2087};
2088
caryclark1049f122015-04-20 08:31:59 -07002089template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08002090struct SkClosestSect {
2091 SkClosestSect()
2092 : fUsed(0) {
2093 fClosest.push_back().reset();
2094 }
2095
caryclarkcdeff812016-07-22 03:34:19 -07002096 bool find(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2
2097 SkDEBUGPARAMS(SkIntersections* i)) {
caryclark1049f122015-04-20 08:31:59 -07002098 SkClosestRecord<TCurve, OppCurve>* record = &fClosest[fUsed];
caryclark45fa4472015-01-16 07:04:10 -08002099 record->findEnd(span1, span2, 0, 0);
caryclark1049f122015-04-20 08:31:59 -07002100 record->findEnd(span1, span2, 0, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002101 record->findEnd(span1, span2, TCurve::kPointLast, 0);
caryclark1049f122015-04-20 08:31:59 -07002102 record->findEnd(span1, span2, TCurve::kPointLast, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002103 if (record->fClosest == FLT_MAX) {
caryclark54359292015-03-26 07:52:43 -07002104 return false;
caryclark45fa4472015-01-16 07:04:10 -08002105 }
2106 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002107 SkClosestRecord<TCurve, OppCurve>* test = &fClosest[index];
caryclarkcdeff812016-07-22 03:34:19 -07002108 if (test->matesWith(*record SkDEBUGPARAMS(i))) {
caryclark45fa4472015-01-16 07:04:10 -08002109 if (test->fClosest > record->fClosest) {
2110 test->merge(*record);
2111 }
2112 test->update(*record);
2113 record->reset();
caryclark54359292015-03-26 07:52:43 -07002114 return false;
caryclark45fa4472015-01-16 07:04:10 -08002115 }
2116 }
2117 ++fUsed;
2118 fClosest.push_back().reset();
caryclark54359292015-03-26 07:52:43 -07002119 return true;
caryclark45fa4472015-01-16 07:04:10 -08002120 }
2121
2122 void finish(SkIntersections* intersections) const {
caryclark1049f122015-04-20 08:31:59 -07002123 SkSTArray<TCurve::kMaxIntersections * 3,
2124 const SkClosestRecord<TCurve, OppCurve>*, true> closestPtrs;
caryclark45fa4472015-01-16 07:04:10 -08002125 for (int index = 0; index < fUsed; ++index) {
caryclark54359292015-03-26 07:52:43 -07002126 closestPtrs.push_back(&fClosest[index]);
2127 }
caryclark1049f122015-04-20 08:31:59 -07002128 SkTQSort<const SkClosestRecord<TCurve, OppCurve> >(closestPtrs.begin(), closestPtrs.end()
2129 - 1);
caryclark54359292015-03-26 07:52:43 -07002130 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002131 const SkClosestRecord<TCurve, OppCurve>* test = closestPtrs[index];
caryclark54359292015-03-26 07:52:43 -07002132 test->addIntersection(intersections);
caryclark45fa4472015-01-16 07:04:10 -08002133 }
2134 }
2135
caryclark54359292015-03-26 07:52:43 -07002136 // this is oversized so that an extra records can merge into final one
caryclark1049f122015-04-20 08:31:59 -07002137 SkSTArray<TCurve::kMaxIntersections * 2, SkClosestRecord<TCurve, OppCurve>, true> fClosest;
caryclark45fa4472015-01-16 07:04:10 -08002138 int fUsed;
2139};
2140
2141// returns true if the rect is too small to consider
caryclark1049f122015-04-20 08:31:59 -07002142template<typename TCurve, typename OppCurve>
2143void SkTSect<TCurve, OppCurve>::BinarySearch(SkTSect<TCurve, OppCurve>* sect1,
2144 SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark54359292015-03-26 07:52:43 -07002145#if DEBUG_T_SECT_DUMP > 1
2146 gDumpTSectNum = 0;
2147#endif
caryclark1049f122015-04-20 08:31:59 -07002148 SkDEBUGCODE(sect1->fOppSect = sect2);
2149 SkDEBUGCODE(sect2->fOppSect = sect1);
caryclark45fa4472015-01-16 07:04:10 -08002150 intersections->reset();
caryclarka35ab3e2016-10-20 08:32:18 -07002151 intersections->setMax(TCurve::kMaxIntersections + 4); // give extra for slop
caryclark1049f122015-04-20 08:31:59 -07002152 SkTSpan<TCurve, OppCurve>* span1 = sect1->fHead;
2153 SkTSpan<OppCurve, TCurve>* span2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002154 int oppSect, sect = sect1->intersects(span1, sect2, span2, &oppSect);
2155// SkASSERT(between(0, sect, 2));
2156 if (!sect) {
caryclark45fa4472015-01-16 07:04:10 -08002157 return;
2158 }
caryclark54359292015-03-26 07:52:43 -07002159 if (sect == 2 && oppSect == 2) {
caryclark45fa4472015-01-16 07:04:10 -08002160 (void) EndsEqual(sect1, sect2, intersections);
2161 return;
2162 }
caryclark54359292015-03-26 07:52:43 -07002163 span1->addBounded(span2, &sect1->fHeap);
2164 span2->addBounded(span1, &sect2->fHeap);
caryclark26ad22a2015-10-16 09:03:38 -07002165 const int kMaxCoinLoopCount = 8;
2166 int coinLoopCount = kMaxCoinLoopCount;
2167 double start1s SK_INIT_TO_AVOID_WARNING;
2168 double start1e SK_INIT_TO_AVOID_WARNING;
caryclark45fa4472015-01-16 07:04:10 -08002169 do {
2170 // find the largest bounds
caryclark1049f122015-04-20 08:31:59 -07002171 SkTSpan<TCurve, OppCurve>* largest1 = sect1->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002172 if (!largest1) {
2173 break;
2174 }
caryclark1049f122015-04-20 08:31:59 -07002175 SkTSpan<OppCurve, TCurve>* largest2 = sect2->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002176 // split it
caryclark1049f122015-04-20 08:31:59 -07002177 if (!largest2 || (largest1 && (largest1->fBoundsMax > largest2->fBoundsMax
2178 || (!largest1->fCollapsed && largest2->fCollapsed)))) {
2179 if (largest1->fCollapsed) {
2180 break;
2181 }
caryclark34efb702016-10-24 08:19:06 -07002182 sect1->resetRemovedEnds();
2183 sect2->resetRemovedEnds();
caryclark1049f122015-04-20 08:31:59 -07002184 // trim parts that don't intersect the opposite
2185 SkTSpan<TCurve, OppCurve>* half1 = sect1->addOne();
caryclark643ede62016-08-08 14:27:45 -07002186 SkDEBUGCODE(half1->debugSetGlobalState(sect1->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002187 if (!half1->split(largest1, &sect1->fHeap)) {
2188 break;
2189 }
caryclarka35ab3e2016-10-20 08:32:18 -07002190 if (!sect1->trim(largest1, sect2)) {
2191 SkOPOBJASSERT(intersections, 0);
2192 return;
2193 }
2194 if (!sect1->trim(half1, sect2)) {
2195 SkOPOBJASSERT(intersections, 0);
2196 return;
2197 }
caryclark1049f122015-04-20 08:31:59 -07002198 } else {
2199 if (largest2->fCollapsed) {
2200 break;
2201 }
caryclark34efb702016-10-24 08:19:06 -07002202 sect1->resetRemovedEnds();
2203 sect2->resetRemovedEnds();
caryclark1049f122015-04-20 08:31:59 -07002204 // trim parts that don't intersect the opposite
2205 SkTSpan<OppCurve, TCurve>* half2 = sect2->addOne();
caryclark643ede62016-08-08 14:27:45 -07002206 SkDEBUGCODE(half2->debugSetGlobalState(sect2->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002207 if (!half2->split(largest2, &sect2->fHeap)) {
2208 break;
2209 }
caryclarka35ab3e2016-10-20 08:32:18 -07002210 if (!sect2->trim(largest2, sect1)) {
2211 SkOPOBJASSERT(intersections, 0);
2212 return;
2213 }
2214 if (!sect2->trim(half2, sect1)) {
2215 SkOPOBJASSERT(intersections, 0);
2216 return;
2217 }
caryclark45fa4472015-01-16 07:04:10 -08002218 }
caryclark54359292015-03-26 07:52:43 -07002219 sect1->validate();
2220 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002221#if DEBUG_T_SECT_LOOP_COUNT
2222 intersections->debugBumpLoopCount(SkIntersections::kIterations_DebugLoop);
2223#endif
caryclark45fa4472015-01-16 07:04:10 -08002224 // if there are 9 or more continuous spans on both sects, suspect coincidence
2225 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2226 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
caryclark26ad22a2015-10-16 09:03:38 -07002227 if (coinLoopCount == kMaxCoinLoopCount) {
2228 start1s = sect1->fHead->fStartT;
2229 start1e = sect1->tail()->fEndT;
2230 }
caryclarkef7cee42016-09-06 09:05:54 -07002231 if (!sect1->coincidentCheck(sect2)) {
2232 return;
2233 }
caryclark54359292015-03-26 07:52:43 -07002234 sect1->validate();
2235 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002236#if DEBUG_T_SECT_LOOP_COUNT
2237 intersections->debugBumpLoopCount(SkIntersections::kCoinCheck_DebugLoop);
2238#endif
caryclarkef784fb2015-10-30 12:03:06 -07002239 if (!--coinLoopCount && sect1->fHead && sect2->fHead) {
caryclark26ad22a2015-10-16 09:03:38 -07002240 /* All known working cases resolve in two tries. Sadly, cubicConicTests[0]
2241 gets stuck in a loop. It adds an extension to allow a coincident end
2242 perpendicular to track its intersection in the opposite curve. However,
2243 the bounding box of the extension does not intersect the original curve,
caryclark55888e42016-07-18 10:01:36 -07002244 so the extension is discarded, only to be added again the next time around. */
caryclark26ad22a2015-10-16 09:03:38 -07002245 sect1->coincidentForce(sect2, start1s, start1e);
2246 sect1->validate();
2247 sect2->validate();
2248 }
caryclark45fa4472015-01-16 07:04:10 -08002249 }
caryclark54359292015-03-26 07:52:43 -07002250 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2251 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
Cary Clark59ed4822016-12-08 16:17:56 -05002252 if (!sect1->fHead) {
2253 return;
2254 }
caryclark54359292015-03-26 07:52:43 -07002255 sect1->computePerpendiculars(sect2, sect1->fHead, sect1->tail());
Cary Clark59ed4822016-12-08 16:17:56 -05002256 if (!sect2->fHead) {
2257 return;
2258 }
caryclark54359292015-03-26 07:52:43 -07002259 sect2->computePerpendiculars(sect1, sect2->fHead, sect2->tail());
2260 sect1->removeByPerpendicular(sect2);
2261 sect1->validate();
2262 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002263#if DEBUG_T_SECT_LOOP_COUNT
2264 intersections->debugBumpLoopCount(SkIntersections::kComputePerp_DebugLoop);
2265#endif
caryclark1049f122015-04-20 08:31:59 -07002266 if (sect1->collapsed() > TCurve::kMaxIntersections) {
2267 break;
2268 }
caryclark54359292015-03-26 07:52:43 -07002269 }
2270#if DEBUG_T_SECT_DUMP
2271 sect1->dumpBoth(sect2);
caryclark45fa4472015-01-16 07:04:10 -08002272#endif
2273 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002274 break;
caryclark45fa4472015-01-16 07:04:10 -08002275 }
2276 } while (true);
caryclark1049f122015-04-20 08:31:59 -07002277 SkTSpan<TCurve, OppCurve>* coincident = sect1->fCoincident;
caryclark54359292015-03-26 07:52:43 -07002278 if (coincident) {
2279 // if there is more than one coincident span, check loosely to see if they should be joined
2280 if (coincident->fNext) {
2281 sect1->mergeCoincidence(sect2);
2282 coincident = sect1->fCoincident;
2283 }
2284 SkASSERT(sect2->fCoincident); // courtesy check : coincidence only looks at sect 1
caryclark45fa4472015-01-16 07:04:10 -08002285 do {
caryclark221a4bb2016-10-07 11:15:15 -07002286 if (!coincident) {
2287 return;
2288 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002289 if (!coincident->fCoinStart.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002290 continue;
2291 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002292 if (!coincident->fCoinEnd.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002293 continue;
2294 }
Cary Clark67116382017-01-03 16:25:18 -05002295 double perpT = coincident->fCoinStart.perpT();
2296 if (perpT < 0) {
2297 return;
2298 }
caryclark54359292015-03-26 07:52:43 -07002299 int index = intersections->insertCoincident(coincident->fStartT,
Cary Clark67116382017-01-03 16:25:18 -05002300 perpT, coincident->fPart[0]);
caryclark54359292015-03-26 07:52:43 -07002301 if ((intersections->insertCoincident(coincident->fEndT,
2302 coincident->fCoinEnd.perpT(),
2303 coincident->fPart[TCurve::kPointLast]) < 0) && index >= 0) {
caryclark45fa4472015-01-16 07:04:10 -08002304 intersections->clearCoincidence(index);
2305 }
caryclark54359292015-03-26 07:52:43 -07002306 } while ((coincident = coincident->fNext));
2307 }
caryclark1049f122015-04-20 08:31:59 -07002308 int zeroOneSet = EndsEqual(sect1, sect2, intersections);
caryclark34efb702016-10-24 08:19:06 -07002309// if (!sect1->fHead || !sect2->fHead) {
caryclark6c3b9cd2016-09-26 05:36:58 -07002310 // if the final iteration contains an end (0 or 1),
2311 if (sect1->fRemovedStartT && !(zeroOneSet & kZeroS1Set)) {
2312 SkTCoincident<TCurve, OppCurve> perp; // intersect perpendicular with opposite curve
caryclarka35ab3e2016-10-20 08:32:18 -07002313 perp.setPerp(sect1->fCurve, 0, sect1->fCurve[0], sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002314 if (perp.isMatch()) {
2315 intersections->insert(0, perp.perpT(), perp.perpPt());
2316 }
2317 }
2318 if (sect1->fRemovedEndT && !(zeroOneSet & kOneS1Set)) {
2319 SkTCoincident<TCurve, OppCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002320 perp.setPerp(sect1->fCurve, 1, sect1->fCurve[TCurve::kPointLast], sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002321 if (perp.isMatch()) {
2322 intersections->insert(1, perp.perpT(), perp.perpPt());
2323 }
2324 }
2325 if (sect2->fRemovedStartT && !(zeroOneSet & kZeroS2Set)) {
2326 SkTCoincident<OppCurve, TCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002327 perp.setPerp(sect2->fCurve, 0, sect2->fCurve[0], sect1->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002328 if (perp.isMatch()) {
2329 intersections->insert(perp.perpT(), 0, perp.perpPt());
2330 }
2331 }
2332 if (sect2->fRemovedEndT && !(zeroOneSet & kOneS2Set)) {
2333 SkTCoincident<OppCurve, TCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002334 perp.setPerp(sect2->fCurve, 1, sect2->fCurve[OppCurve::kPointLast], sect1->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002335 if (perp.isMatch()) {
2336 intersections->insert(perp.perpT(), 1, perp.perpPt());
2337 }
2338 }
caryclark34efb702016-10-24 08:19:06 -07002339// }
2340 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002341 return;
caryclark45fa4472015-01-16 07:04:10 -08002342 }
caryclark45fa4472015-01-16 07:04:10 -08002343 sect1->recoverCollapsed();
2344 sect2->recoverCollapsed();
caryclark1049f122015-04-20 08:31:59 -07002345 SkTSpan<TCurve, OppCurve>* result1 = sect1->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002346 // check heads and tails for zero and ones and insert them if we haven't already done so
caryclark1049f122015-04-20 08:31:59 -07002347 const SkTSpan<TCurve, OppCurve>* head1 = result1;
caryclark45fa4472015-01-16 07:04:10 -08002348 if (!(zeroOneSet & kZeroS1Set) && approximately_less_than_zero(head1->fStartT)) {
2349 const SkDPoint& start1 = sect1->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002350 if (head1->isBounded()) {
2351 double t = head1->closestBoundedT(start1);
2352 if (sect2->fCurve.ptAtT(t).approximatelyEqual(start1)) {
2353 intersections->insert(0, t, start1);
2354 }
caryclark45fa4472015-01-16 07:04:10 -08002355 }
2356 }
caryclark1049f122015-04-20 08:31:59 -07002357 const SkTSpan<OppCurve, TCurve>* head2 = sect2->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002358 if (!(zeroOneSet & kZeroS2Set) && approximately_less_than_zero(head2->fStartT)) {
2359 const SkDPoint& start2 = sect2->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002360 if (head2->isBounded()) {
2361 double t = head2->closestBoundedT(start2);
2362 if (sect1->fCurve.ptAtT(t).approximatelyEqual(start2)) {
2363 intersections->insert(t, 0, start2);
2364 }
caryclark45fa4472015-01-16 07:04:10 -08002365 }
2366 }
caryclark1049f122015-04-20 08:31:59 -07002367 const SkTSpan<TCurve, OppCurve>* tail1 = sect1->tail();
caryclark45fa4472015-01-16 07:04:10 -08002368 if (!(zeroOneSet & kOneS1Set) && approximately_greater_than_one(tail1->fEndT)) {
2369 const SkDPoint& end1 = sect1->fCurve[TCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002370 if (tail1->isBounded()) {
2371 double t = tail1->closestBoundedT(end1);
2372 if (sect2->fCurve.ptAtT(t).approximatelyEqual(end1)) {
2373 intersections->insert(1, t, end1);
2374 }
caryclark45fa4472015-01-16 07:04:10 -08002375 }
2376 }
caryclark1049f122015-04-20 08:31:59 -07002377 const SkTSpan<OppCurve, TCurve>* tail2 = sect2->tail();
caryclark45fa4472015-01-16 07:04:10 -08002378 if (!(zeroOneSet & kOneS2Set) && approximately_greater_than_one(tail2->fEndT)) {
caryclark1049f122015-04-20 08:31:59 -07002379 const SkDPoint& end2 = sect2->fCurve[OppCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002380 if (tail2->isBounded()) {
2381 double t = tail2->closestBoundedT(end2);
2382 if (sect1->fCurve.ptAtT(t).approximatelyEqual(end2)) {
2383 intersections->insert(t, 1, end2);
2384 }
caryclark45fa4472015-01-16 07:04:10 -08002385 }
2386 }
caryclark1049f122015-04-20 08:31:59 -07002387 SkClosestSect<TCurve, OppCurve> closest;
caryclark45fa4472015-01-16 07:04:10 -08002388 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07002389 while (result1 && result1->fCoinStart.isMatch() && result1->fCoinEnd.isMatch()) {
caryclark45fa4472015-01-16 07:04:10 -08002390 result1 = result1->fNext;
2391 }
2392 if (!result1) {
2393 break;
2394 }
caryclark1049f122015-04-20 08:31:59 -07002395 SkTSpan<OppCurve, TCurve>* result2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002396 bool found = false;
caryclark45fa4472015-01-16 07:04:10 -08002397 while (result2) {
caryclarkcdeff812016-07-22 03:34:19 -07002398 found |= closest.find(result1, result2 SkDEBUGPARAMS(intersections));
caryclark45fa4472015-01-16 07:04:10 -08002399 result2 = result2->fNext;
2400 }
caryclark45fa4472015-01-16 07:04:10 -08002401 } while ((result1 = result1->fNext));
2402 closest.finish(intersections);
caryclark54359292015-03-26 07:52:43 -07002403 // if there is more than one intersection and it isn't already coincident, check
2404 int last = intersections->used() - 1;
2405 for (int index = 0; index < last; ) {
2406 if (intersections->isCoincident(index) && intersections->isCoincident(index + 1)) {
2407 ++index;
2408 continue;
2409 }
2410 double midT = ((*intersections)[0][index] + (*intersections)[0][index + 1]) / 2;
2411 SkDPoint midPt = sect1->fCurve.ptAtT(midT);
2412 // intersect perpendicular with opposite curve
caryclark1049f122015-04-20 08:31:59 -07002413 SkTCoincident<TCurve, OppCurve> perp;
caryclark54359292015-03-26 07:52:43 -07002414 perp.setPerp(sect1->fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002415 if (!perp.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07002416 ++index;
2417 continue;
2418 }
2419 if (intersections->isCoincident(index)) {
2420 intersections->removeOne(index);
2421 --last;
2422 } else if (intersections->isCoincident(index + 1)) {
2423 intersections->removeOne(index + 1);
2424 --last;
2425 } else {
2426 intersections->setCoincident(index++);
2427 }
2428 intersections->setCoincident(index);
2429 }
caryclarka35ab3e2016-10-20 08:32:18 -07002430 SkOPOBJASSERT(intersections, intersections->used() <= TCurve::kMaxIntersections);
caryclark45fa4472015-01-16 07:04:10 -08002431}
deanm12670eb2016-04-26 14:09:01 -07002432
2433#endif