blob: ef0799de4f35aff8f978b1fb21e73de1b61d459e [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();
caryclark1049f122015-04-20 08:31:59 -0700312 void removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween);
313 void removeAllBut(const SkTSpan<OppCurve, TCurve>* keep, SkTSpan<TCurve, OppCurve>* span,
314 SkTSect<OppCurve, TCurve>* opp);
caryclarkef7cee42016-09-06 09:05:54 -0700315 bool removeSpan(SkTSpan<TCurve, OppCurve>* span);
caryclark1049f122015-04-20 08:31:59 -0700316 void removeSpanRange(SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last);
317 void removeSpans(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp);
caryclark34efb702016-10-24 08:19:06 -0700318 void removedEndCheck(SkTSpan<TCurve, OppCurve>* span);
319
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);
caryclark1049f122015-04-20 08:31:59 -0700327 void unlinkSpan(SkTSpan<TCurve, OppCurve>* span);
328 bool updateBounded(SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last,
329 SkTSpan<OppCurve, TCurve>* oppFirst);
reed0dc4dd62015-03-24 13:55:33 -0700330 void validate() const;
caryclark54359292015-03-26 07:52:43 -0700331 void validateBounded() const;
332
caryclark45fa4472015-01-16 07:04:10 -0800333 const TCurve& fCurve;
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;
1276 this->removeCoincident(first, false);
1277 sect2->removeCoincident(oppFirst, true);
caryclark1049f122015-04-20 08:31:59 -07001278 if (deleteEmptySpans) {
caryclarkef7cee42016-09-06 09:05:54 -07001279 if (!this->deleteEmptySpans() || !sect2->deleteEmptySpans()) {
1280 *result = nullptr;
1281 return false;
1282 }
caryclark54359292015-03-26 07:52:43 -07001283 }
1284 this->validate();
1285 sect2->validate();
caryclarkef7cee42016-09-06 09:05:54 -07001286 *result = last && !last->fDeleted && fHead && sect2->fHead ? last : nullptr;
1287 return true;
caryclark54359292015-03-26 07:52:43 -07001288}
1289
caryclark1049f122015-04-20 08:31:59 -07001290template<typename TCurve, typename OppCurve>
1291SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::findCoincidentRun(
1292 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>** lastPtr) {
1293 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001294 SkTSpan<TCurve, OppCurve>* lastCandidate = nullptr;
1295 first = nullptr;
caryclark54359292015-03-26 07:52:43 -07001296 // find the first fully coincident span
1297 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07001298 if (work->fCoinStart.isMatch()) {
caryclark1049f122015-04-20 08:31:59 -07001299#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -07001300 work->validatePerpT(work->fCoinStart.perpT());
1301 work->validatePerpPt(work->fCoinStart.perpT(), work->fCoinStart.perpPt());
caryclark1049f122015-04-20 08:31:59 -07001302#endif
caryclarka35ab3e2016-10-20 08:32:18 -07001303 SkOPASSERT(work->hasOppT(work->fCoinStart.perpT()));
caryclark6c3b9cd2016-09-26 05:36:58 -07001304 if (!work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001305 break;
1306 }
1307 lastCandidate = work;
1308 if (!first) {
1309 first = work;
1310 }
caryclark1049f122015-04-20 08:31:59 -07001311 } else if (first && work->fCollapsed) {
1312 *lastPtr = lastCandidate;
1313 return first;
caryclark54359292015-03-26 07:52:43 -07001314 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001315 lastCandidate = nullptr;
caryclark643ede62016-08-08 14:27:45 -07001316 SkOPASSERT(!first);
caryclark54359292015-03-26 07:52:43 -07001317 }
1318 if (work == *lastPtr) {
1319 return first;
1320 }
1321 work = work->fNext;
caryclarke25a4f62016-07-26 09:26:29 -07001322 if (!work) {
1323 return nullptr;
1324 }
caryclark54359292015-03-26 07:52:43 -07001325 } while (true);
1326 if (lastCandidate) {
1327 *lastPtr = lastCandidate;
1328 }
1329 return first;
1330}
1331
caryclark1049f122015-04-20 08:31:59 -07001332template<typename TCurve, typename OppCurve>
1333int SkTSect<TCurve, OppCurve>::intersects(SkTSpan<TCurve, OppCurve>* span,
1334 SkTSect<OppCurve, TCurve>* opp,
1335 SkTSpan<OppCurve, TCurve>* oppSpan, int* oppResult) {
caryclark54359292015-03-26 07:52:43 -07001336 bool spanStart, oppStart;
1337 int hullResult = span->hullsIntersect(oppSpan, &spanStart, &oppStart);
1338 if (hullResult >= 0) {
1339 if (hullResult == 2) { // hulls have one point in common
1340 if (!span->fBounded || !span->fBounded->fNext) {
1341 SkASSERT(!span->fBounded || span->fBounded->fBounded == oppSpan);
1342 if (spanStart) {
1343 span->fEndT = span->fStartT;
caryclark45fa4472015-01-16 07:04:10 -08001344 } else {
caryclark54359292015-03-26 07:52:43 -07001345 span->fStartT = span->fEndT;
1346 }
1347 } else {
1348 hullResult = 1;
1349 }
1350 if (!oppSpan->fBounded || !oppSpan->fBounded->fNext) {
1351 SkASSERT(!oppSpan->fBounded || oppSpan->fBounded->fBounded == span);
1352 if (oppStart) {
1353 oppSpan->fEndT = oppSpan->fStartT;
1354 } else {
1355 oppSpan->fStartT = oppSpan->fEndT;
1356 }
1357 *oppResult = 2;
1358 } else {
1359 *oppResult = 1;
1360 }
1361 } else {
1362 *oppResult = 1;
1363 }
1364 return hullResult;
1365 }
1366 if (span->fIsLine && oppSpan->fIsLine) {
1367 SkIntersections i;
1368 int sects = this->linesIntersect(span, opp, oppSpan, &i);
caryclark26ad22a2015-10-16 09:03:38 -07001369 if (sects == 2) {
1370 return *oppResult = 1;
1371 }
caryclark54359292015-03-26 07:52:43 -07001372 if (!sects) {
1373 return -1;
1374 }
caryclark34efb702016-10-24 08:19:06 -07001375 this->removedEndCheck(span);
caryclark54359292015-03-26 07:52:43 -07001376 span->fStartT = span->fEndT = i[0][0];
caryclark34efb702016-10-24 08:19:06 -07001377 opp->removedEndCheck(oppSpan);
caryclark54359292015-03-26 07:52:43 -07001378 oppSpan->fStartT = oppSpan->fEndT = i[1][0];
1379 return *oppResult = 2;
1380 }
1381 if (span->fIsLinear || oppSpan->fIsLinear) {
1382 return *oppResult = (int) span->linearsIntersect(oppSpan);
1383 }
1384 return *oppResult = 1;
1385}
1386
caryclarked0935a2015-10-22 07:23:52 -07001387template<typename TCurve>
1388static bool is_parallel(const SkDLine& thisLine, const TCurve& opp) {
1389 if (!opp.IsConic()) {
1390 return false; // FIXME : breaks a lot of stuff now
1391 }
1392 int finds = 0;
1393 SkDLine thisPerp;
1394 thisPerp.fPts[0].fX = thisLine.fPts[1].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1395 thisPerp.fPts[0].fY = thisLine.fPts[1].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1396 thisPerp.fPts[1] = thisLine.fPts[1];
1397 SkIntersections perpRayI;
1398 perpRayI.intersectRay(opp, thisPerp);
1399 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1400 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[1]);
1401 }
1402 thisPerp.fPts[1].fX = thisLine.fPts[0].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1403 thisPerp.fPts[1].fY = thisLine.fPts[0].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1404 thisPerp.fPts[0] = thisLine.fPts[0];
1405 perpRayI.intersectRay(opp, thisPerp);
1406 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1407 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[0]);
1408 }
1409 return finds >= 2;
1410}
1411
caryclark54359292015-03-26 07:52:43 -07001412// while the intersection points are sufficiently far apart:
1413// construct the tangent lines from the intersections
1414// find the point where the tangent line intersects the opposite curve
caryclark1049f122015-04-20 08:31:59 -07001415template<typename TCurve, typename OppCurve>
1416int SkTSect<TCurve, OppCurve>::linesIntersect(SkTSpan<TCurve, OppCurve>* span,
1417 SkTSect<OppCurve, TCurve>* opp,
1418 SkTSpan<OppCurve, TCurve>* oppSpan, SkIntersections* i) {
caryclarka35ab3e2016-10-20 08:32:18 -07001419 SkIntersections thisRayI SkDEBUGCODE((span->fDebugGlobalState));
1420 SkIntersections oppRayI SkDEBUGCODE((span->fDebugGlobalState));
caryclark54359292015-03-26 07:52:43 -07001421 SkDLine thisLine = {{ span->fPart[0], span->fPart[TCurve::kPointLast] }};
caryclark1049f122015-04-20 08:31:59 -07001422 SkDLine oppLine = {{ oppSpan->fPart[0], oppSpan->fPart[OppCurve::kPointLast] }};
caryclark54359292015-03-26 07:52:43 -07001423 int loopCount = 0;
1424 double bestDistSq = DBL_MAX;
caryclark1049f122015-04-20 08:31:59 -07001425 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1426 return 0;
1427 }
1428 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1429 return 0;
1430 }
caryclark26ad22a2015-10-16 09:03:38 -07001431 // if the ends of each line intersect the opposite curve, the lines are coincident
1432 if (thisRayI.used() > 1) {
1433 int ptMatches = 0;
1434 for (int tIndex = 0; tIndex < thisRayI.used(); ++tIndex) {
1435 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(thisLine.fPts); ++lIndex) {
1436 ptMatches += thisRayI.pt(tIndex).approximatelyEqual(thisLine.fPts[lIndex]);
1437 }
1438 }
caryclarked0935a2015-10-22 07:23:52 -07001439 if (ptMatches == 2 || is_parallel(thisLine, opp->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001440 return 2;
1441 }
1442 }
1443 if (oppRayI.used() > 1) {
1444 int ptMatches = 0;
1445 for (int oIndex = 0; oIndex < oppRayI.used(); ++oIndex) {
1446 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(thisLine.fPts); ++lIndex) {
1447 ptMatches += oppRayI.pt(oIndex).approximatelyEqual(oppLine.fPts[lIndex]);
1448 }
1449 }
caryclarked0935a2015-10-22 07:23:52 -07001450 if (ptMatches == 2|| is_parallel(oppLine, this->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001451 return 2;
1452 }
1453 }
caryclark54359292015-03-26 07:52:43 -07001454 do {
caryclark54359292015-03-26 07:52:43 -07001455 // pick the closest pair of points
1456 double closest = DBL_MAX;
1457 int closeIndex SK_INIT_TO_AVOID_WARNING;
1458 int oppCloseIndex SK_INIT_TO_AVOID_WARNING;
1459 for (int index = 0; index < oppRayI.used(); ++index) {
1460 if (!roughly_between(span->fStartT, oppRayI[0][index], span->fEndT)) {
1461 continue;
1462 }
1463 for (int oIndex = 0; oIndex < thisRayI.used(); ++oIndex) {
1464 if (!roughly_between(oppSpan->fStartT, thisRayI[0][oIndex], oppSpan->fEndT)) {
1465 continue;
1466 }
1467 double distSq = thisRayI.pt(index).distanceSquared(oppRayI.pt(oIndex));
1468 if (closest > distSq) {
1469 closest = distSq;
1470 closeIndex = index;
1471 oppCloseIndex = oIndex;
caryclarkccec0f92015-03-24 07:28:17 -07001472 }
caryclarkccec0f92015-03-24 07:28:17 -07001473 }
reed0dc4dd62015-03-24 13:55:33 -07001474 }
caryclark54359292015-03-26 07:52:43 -07001475 if (closest == DBL_MAX) {
caryclark1049f122015-04-20 08:31:59 -07001476 break;
reed0dc4dd62015-03-24 13:55:33 -07001477 }
caryclark54359292015-03-26 07:52:43 -07001478 const SkDPoint& oppIPt = thisRayI.pt(oppCloseIndex);
1479 const SkDPoint& iPt = oppRayI.pt(closeIndex);
1480 if (between(span->fStartT, oppRayI[0][closeIndex], span->fEndT)
1481 && between(oppSpan->fStartT, thisRayI[0][oppCloseIndex], oppSpan->fEndT)
1482 && oppIPt.approximatelyEqual(iPt)) {
1483 i->merge(oppRayI, closeIndex, thisRayI, oppCloseIndex);
1484 return i->used();
1485 }
1486 double distSq = oppIPt.distanceSquared(iPt);
1487 if (bestDistSq < distSq || ++loopCount > 5) {
caryclark1049f122015-04-20 08:31:59 -07001488 return 0;
caryclark54359292015-03-26 07:52:43 -07001489 }
1490 bestDistSq = distSq;
caryclark1049f122015-04-20 08:31:59 -07001491 double oppStart = oppRayI[0][closeIndex];
1492 thisLine[0] = fCurve.ptAtT(oppStart);
1493 thisLine[1] = thisLine[0] + fCurve.dxdyAtT(oppStart);
1494 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1495 break;
1496 }
1497 double start = thisRayI[0][oppCloseIndex];
1498 oppLine[0] = opp->fCurve.ptAtT(start);
1499 oppLine[1] = oppLine[0] + opp->fCurve.dxdyAtT(start);
1500 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1501 break;
1502 }
caryclark54359292015-03-26 07:52:43 -07001503 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001504 // convergence may fail if the curves are nearly coincident
1505 SkTCoincident<OppCurve, TCurve> oCoinS, oCoinE;
1506 oCoinS.setPerp(opp->fCurve, oppSpan->fStartT, oppSpan->fPart[0], fCurve);
1507 oCoinE.setPerp(opp->fCurve, oppSpan->fEndT, oppSpan->fPart[OppCurve::kPointLast], fCurve);
1508 double tStart = oCoinS.perpT();
1509 double tEnd = oCoinE.perpT();
1510 bool swap = tStart > tEnd;
1511 if (swap) {
1512 SkTSwap(tStart, tEnd);
1513 }
1514 tStart = SkTMax(tStart, span->fStartT);
1515 tEnd = SkTMin(tEnd, span->fEndT);
1516 if (tStart > tEnd) {
1517 return 0;
1518 }
1519 SkDVector perpS, perpE;
1520 if (tStart == span->fStartT) {
1521 SkTCoincident<TCurve, OppCurve> coinS;
1522 coinS.setPerp(fCurve, span->fStartT, span->fPart[0], opp->fCurve);
1523 perpS = span->fPart[0] - coinS.perpPt();
1524 } else if (swap) {
1525 perpS = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1526 } else {
1527 perpS = oCoinS.perpPt() - oppSpan->fPart[0];
1528 }
1529 if (tEnd == span->fEndT) {
1530 SkTCoincident<TCurve, OppCurve> coinE;
1531 coinE.setPerp(fCurve, span->fEndT, span->fPart[TCurve::kPointLast], opp->fCurve);
1532 perpE = span->fPart[TCurve::kPointLast] - coinE.perpPt();
1533 } else if (swap) {
1534 perpE = oCoinS.perpPt() - oppSpan->fPart[0];
1535 } else {
1536 perpE = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1537 }
1538 if (perpS.dot(perpE) >= 0) {
1539 return 0;
1540 }
1541 SkTCoincident<TCurve, OppCurve> coinW;
1542 double workT = tStart;
1543 double tStep = tEnd - tStart;
1544 SkDPoint workPt;
1545 do {
1546 tStep *= 0.5;
1547 if (precisely_zero(tStep)) {
1548 return 0;
1549 }
1550 workT += tStep;
1551 workPt = fCurve.ptAtT(workT);
1552 coinW.setPerp(fCurve, workT, workPt, opp->fCurve);
caryclark27c015d2016-09-23 05:47:20 -07001553 double perpT = coinW.perpT();
caryclark6c3b9cd2016-09-26 05:36:58 -07001554 if (coinW.isMatch() ? !between(oppSpan->fStartT, perpT, oppSpan->fEndT) : perpT < 0) {
caryclarkb6693002015-12-16 12:28:35 -08001555 continue;
1556 }
caryclark1049f122015-04-20 08:31:59 -07001557 SkDVector perpW = workPt - coinW.perpPt();
1558 if ((perpS.dot(perpW) >= 0) == (tStep < 0)) {
1559 tStep = -tStep;
1560 }
caryclarkb6693002015-12-16 12:28:35 -08001561 if (workPt.approximatelyEqual(coinW.perpPt())) {
1562 break;
1563 }
1564 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001565 double oppTTest = coinW.perpT();
1566 if (!opp->fHead->contains(oppTTest)) {
1567 return 0;
1568 }
1569 i->setMax(1);
1570 i->insert(workT, oppTTest, workPt);
1571 return 1;
caryclark54359292015-03-26 07:52:43 -07001572}
1573
1574
caryclark1049f122015-04-20 08:31:59 -07001575template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001576bool SkTSect<TCurve, OppCurve>::markSpanGone(SkTSpan<TCurve, OppCurve>* span) {
1577 if (--fActiveCount < 0) {
1578 return false;
1579 }
caryclark54359292015-03-26 07:52:43 -07001580 span->fNext = fDeleted;
1581 fDeleted = span;
caryclarke25a4f62016-07-26 09:26:29 -07001582 SkOPASSERT(!span->fDeleted);
caryclark54359292015-03-26 07:52:43 -07001583 span->fDeleted = true;
caryclarkef7cee42016-09-06 09:05:54 -07001584 return true;
caryclark54359292015-03-26 07:52:43 -07001585}
1586
caryclark1049f122015-04-20 08:31:59 -07001587template<typename TCurve, typename OppCurve>
1588bool SkTSect<TCurve, OppCurve>::matchedDirection(double t, const SkTSect<OppCurve, TCurve>* sect2,
1589 double t2) const {
caryclark54359292015-03-26 07:52:43 -07001590 SkDVector dxdy = this->fCurve.dxdyAtT(t);
1591 SkDVector dxdy2 = sect2->fCurve.dxdyAtT(t2);
1592 return dxdy.dot(dxdy2) >= 0;
1593}
1594
caryclark1049f122015-04-20 08:31:59 -07001595template<typename TCurve, typename OppCurve>
1596void SkTSect<TCurve, OppCurve>::matchedDirCheck(double t, const SkTSect<OppCurve, TCurve>* sect2,
1597 double t2, bool* calcMatched, bool* oppMatched) const {
caryclark54359292015-03-26 07:52:43 -07001598 if (*calcMatched) {
caryclark55888e42016-07-18 10:01:36 -07001599 SkASSERT(*oppMatched == this->matchedDirection(t, sect2, t2));
caryclark54359292015-03-26 07:52:43 -07001600 } else {
1601 *oppMatched = this->matchedDirection(t, sect2, t2);
1602 *calcMatched = true;
1603 }
1604}
1605
caryclark1049f122015-04-20 08:31:59 -07001606template<typename TCurve, typename OppCurve>
1607void SkTSect<TCurve, OppCurve>::mergeCoincidence(SkTSect<OppCurve, TCurve>* sect2) {
caryclark54359292015-03-26 07:52:43 -07001608 double smallLimit = 0;
1609 do {
1610 // find the smallest unprocessed span
halcanary96fcdcc2015-08-27 07:41:13 -07001611 SkTSpan<TCurve, OppCurve>* smaller = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001612 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001613 do {
caryclark221a4bb2016-10-07 11:15:15 -07001614 if (!test) {
1615 return;
1616 }
caryclark54359292015-03-26 07:52:43 -07001617 if (test->fStartT < smallLimit) {
1618 continue;
1619 }
1620 if (smaller && smaller->fEndT < test->fStartT) {
1621 continue;
1622 }
1623 smaller = test;
1624 } while ((test = test->fNext));
1625 if (!smaller) {
1626 return;
1627 }
1628 smallLimit = smaller->fEndT;
1629 // find next larger span
halcanary96fcdcc2015-08-27 07:41:13 -07001630 SkTSpan<TCurve, OppCurve>* prior = nullptr;
1631 SkTSpan<TCurve, OppCurve>* larger = nullptr;
1632 SkTSpan<TCurve, OppCurve>* largerPrior = nullptr;
caryclark54359292015-03-26 07:52:43 -07001633 test = fCoincident;
1634 do {
1635 if (test->fStartT < smaller->fEndT) {
1636 continue;
1637 }
caryclark221a4bb2016-10-07 11:15:15 -07001638 SkOPASSERT(test->fStartT != smaller->fEndT);
caryclark54359292015-03-26 07:52:43 -07001639 if (larger && larger->fStartT < test->fStartT) {
1640 continue;
1641 }
1642 largerPrior = prior;
1643 larger = test;
1644 } while ((prior = test), (test = test->fNext));
1645 if (!larger) {
1646 continue;
1647 }
1648 // check middle t value to see if it is coincident as well
1649 double midT = (smaller->fEndT + larger->fStartT) / 2;
1650 SkDPoint midPt = fCurve.ptAtT(midT);
caryclark1049f122015-04-20 08:31:59 -07001651 SkTCoincident<TCurve, OppCurve> coin;
caryclark54359292015-03-26 07:52:43 -07001652 coin.setPerp(fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07001653 if (coin.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001654 smaller->fEndT = larger->fEndT;
1655 smaller->fCoinEnd = larger->fCoinEnd;
1656 if (largerPrior) {
1657 largerPrior->fNext = larger->fNext;
caryclark643ede62016-08-08 14:27:45 -07001658 largerPrior->validate();
caryclark54359292015-03-26 07:52:43 -07001659 } else {
1660 fCoincident = larger->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001661 }
1662 }
caryclark54359292015-03-26 07:52:43 -07001663 } while (true);
1664}
1665
caryclark1049f122015-04-20 08:31:59 -07001666template<typename TCurve, typename OppCurve>
1667SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::prev(
1668 SkTSpan<TCurve, OppCurve>* span) const {
halcanary96fcdcc2015-08-27 07:41:13 -07001669 SkTSpan<TCurve, OppCurve>* result = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001670 SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001671 while (span != test) {
1672 result = test;
1673 test = test->fNext;
1674 SkASSERT(test);
caryclarkccec0f92015-03-24 07:28:17 -07001675 }
caryclark55888e42016-07-18 10:01:36 -07001676 return result;
caryclarkccec0f92015-03-24 07:28:17 -07001677}
1678
caryclark1049f122015-04-20 08:31:59 -07001679template<typename TCurve, typename OppCurve>
1680void SkTSect<TCurve, OppCurve>::recoverCollapsed() {
1681 SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001682 while (deleted) {
caryclark1049f122015-04-20 08:31:59 -07001683 SkTSpan<TCurve, OppCurve>* delNext = deleted->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001684 if (deleted->fCollapsed) {
caryclark1049f122015-04-20 08:31:59 -07001685 SkTSpan<TCurve, OppCurve>** spanPtr = &fHead;
caryclark45fa4472015-01-16 07:04:10 -08001686 while (*spanPtr && (*spanPtr)->fEndT <= deleted->fStartT) {
1687 spanPtr = &(*spanPtr)->fNext;
1688 }
1689 deleted->fNext = *spanPtr;
1690 *spanPtr = deleted;
1691 }
1692 deleted = delNext;
1693 }
1694}
1695
caryclark1049f122015-04-20 08:31:59 -07001696template<typename TCurve, typename OppCurve>
1697void SkTSect<TCurve, OppCurve>::removeAllBut(const SkTSpan<OppCurve, TCurve>* keep,
1698 SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp) {
1699 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001700 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001701 SkTSpan<OppCurve, TCurve>* bounded = testBounded->fBounded;
1702 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001703 // may have been deleted when opp did 'remove all but'
1704 if (bounded != keep && !bounded->fDeleted) {
1705 SkAssertResult(SkDEBUGCODE(!) span->removeBounded(bounded));
1706 if (bounded->removeBounded(span)) {
1707 opp->removeSpan(bounded);
1708 }
caryclarkccec0f92015-03-24 07:28:17 -07001709 }
caryclark54359292015-03-26 07:52:43 -07001710 testBounded = next;
caryclarkccec0f92015-03-24 07:28:17 -07001711 }
caryclark54359292015-03-26 07:52:43 -07001712 SkASSERT(!span->fDeleted);
1713 SkASSERT(span->findOppSpan(keep));
1714 SkASSERT(keep->findOppSpan(span));
caryclarkccec0f92015-03-24 07:28:17 -07001715}
1716
caryclark1049f122015-04-20 08:31:59 -07001717template<typename TCurve, typename OppCurve>
1718void SkTSect<TCurve, OppCurve>::removeByPerpendicular(SkTSect<OppCurve, TCurve>* opp) {
1719 SkTSpan<TCurve, OppCurve>* test = fHead;
1720 SkTSpan<TCurve, OppCurve>* next;
caryclark54359292015-03-26 07:52:43 -07001721 do {
1722 next = test->fNext;
1723 if (test->fCoinStart.perpT() < 0 || test->fCoinEnd.perpT() < 0) {
1724 continue;
reed0dc4dd62015-03-24 13:55:33 -07001725 }
caryclark54359292015-03-26 07:52:43 -07001726 SkDVector startV = test->fCoinStart.perpPt() - test->fPart[0];
1727 SkDVector endV = test->fCoinEnd.perpPt() - test->fPart[TCurve::kPointLast];
1728#if DEBUG_T_SECT
1729 SkDebugf("%s startV=(%1.9g,%1.9g) endV=(%1.9g,%1.9g) dot=%1.9g\n", __FUNCTION__,
1730 startV.fX, startV.fY, endV.fX, endV.fY, startV.dot(endV));
1731#endif
1732 if (startV.dot(endV) <= 0) {
1733 continue;
1734 }
1735 this->removeSpans(test, opp);
1736 } while ((test = next));
1737}
1738
caryclark1049f122015-04-20 08:31:59 -07001739template<typename TCurve, typename OppCurve>
1740void SkTSect<TCurve, OppCurve>::removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween) {
caryclark54359292015-03-26 07:52:43 -07001741 this->unlinkSpan(span);
1742 if (isBetween || between(0, span->fCoinStart.perpT(), 1)) {
1743 --fActiveCount;
1744 span->fNext = fCoincident;
1745 fCoincident = span;
1746 } else {
1747 this->markSpanGone(span);
reed0dc4dd62015-03-24 13:55:33 -07001748 }
caryclarkccec0f92015-03-24 07:28:17 -07001749}
1750
caryclark1049f122015-04-20 08:31:59 -07001751template<typename TCurve, typename OppCurve>
caryclark34efb702016-10-24 08:19:06 -07001752void SkTSect<TCurve, OppCurve>::removedEndCheck(SkTSpan<TCurve, OppCurve>* span) {
caryclark6c3b9cd2016-09-26 05:36:58 -07001753 if (!span->fStartT) {
1754 fRemovedStartT = true;
1755 }
1756 if (1 == span->fEndT) {
1757 fRemovedEndT = true;
1758 }
caryclark34efb702016-10-24 08:19:06 -07001759}
1760
1761template<typename TCurve, typename OppCurve>
1762bool SkTSect<TCurve, OppCurve>::removeSpan(SkTSpan<TCurve, OppCurve>* span) {\
1763 this->removedEndCheck(span);
caryclark54359292015-03-26 07:52:43 -07001764 this->unlinkSpan(span);
caryclarkef7cee42016-09-06 09:05:54 -07001765 return this->markSpanGone(span);
caryclark54359292015-03-26 07:52:43 -07001766}
1767
caryclark1049f122015-04-20 08:31:59 -07001768template<typename TCurve, typename OppCurve>
1769void SkTSect<TCurve, OppCurve>::removeSpanRange(SkTSpan<TCurve, OppCurve>* first,
1770 SkTSpan<TCurve, OppCurve>* last) {
caryclark54359292015-03-26 07:52:43 -07001771 if (first == last) {
1772 return;
1773 }
caryclark1049f122015-04-20 08:31:59 -07001774 SkTSpan<TCurve, OppCurve>* span = first;
caryclark54359292015-03-26 07:52:43 -07001775 SkASSERT(span);
caryclark1049f122015-04-20 08:31:59 -07001776 SkTSpan<TCurve, OppCurve>* final = last->fNext;
1777 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001778 while ((span = next) && span != final) {
1779 next = span->fNext;
1780 this->markSpanGone(span);
1781 }
1782 if (final) {
1783 final->fPrev = first;
1784 }
1785 first->fNext = final;
caryclark643ede62016-08-08 14:27:45 -07001786 first->validate();
caryclark54359292015-03-26 07:52:43 -07001787}
1788
caryclark1049f122015-04-20 08:31:59 -07001789template<typename TCurve, typename OppCurve>
1790void SkTSect<TCurve, OppCurve>::removeSpans(SkTSpan<TCurve, OppCurve>* span,
1791 SkTSect<OppCurve, TCurve>* opp) {
1792 SkTSpanBounded<OppCurve, TCurve>* bounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001793 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -07001794 SkTSpan<OppCurve, TCurve>* spanBounded = bounded->fBounded;
1795 SkTSpanBounded<OppCurve, TCurve>* next = bounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001796 if (span->removeBounded(spanBounded)) { // shuffles last into position 0
1797 this->removeSpan(span);
1798 }
1799 if (spanBounded->removeBounded(span)) {
1800 opp->removeSpan(spanBounded);
1801 }
1802 SkASSERT(!span->fDeleted || !opp->debugHasBounded(span));
1803 bounded = next;
reed0dc4dd62015-03-24 13:55:33 -07001804 }
1805}
caryclarkccec0f92015-03-24 07:28:17 -07001806
caryclark1049f122015-04-20 08:31:59 -07001807template<typename TCurve, typename OppCurve>
1808SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::spanAtT(double t,
1809 SkTSpan<TCurve, OppCurve>** priorSpan) {
1810 SkTSpan<TCurve, OppCurve>* test = fHead;
halcanary96fcdcc2015-08-27 07:41:13 -07001811 SkTSpan<TCurve, OppCurve>* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -07001812 while (test && test->fEndT < t) {
1813 prev = test;
1814 test = test->fNext;
reed0dc4dd62015-03-24 13:55:33 -07001815 }
caryclark54359292015-03-26 07:52:43 -07001816 *priorSpan = prev;
halcanary96fcdcc2015-08-27 07:41:13 -07001817 return test && test->fStartT <= t ? test : nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001818}
1819
caryclark1049f122015-04-20 08:31:59 -07001820template<typename TCurve, typename OppCurve>
1821SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::tail() {
1822 SkTSpan<TCurve, OppCurve>* result = fHead;
1823 SkTSpan<TCurve, OppCurve>* next = fHead;
reed0dc4dd62015-03-24 13:55:33 -07001824 while ((next = next->fNext)) {
1825 if (next->fEndT > result->fEndT) {
1826 result = next;
1827 }
1828 }
1829 return result;
1830}
1831
1832/* Each span has a range of opposite spans it intersects. After the span is split in two,
1833 adjust the range to its new size */
caryclark1049f122015-04-20 08:31:59 -07001834template<typename TCurve, typename OppCurve>
caryclarka35ab3e2016-10-20 08:32:18 -07001835bool SkTSect<TCurve, OppCurve>::trim(SkTSpan<TCurve, OppCurve>* span,
caryclark1049f122015-04-20 08:31:59 -07001836 SkTSect<OppCurve, TCurve>* opp) {
caryclarka35ab3e2016-10-20 08:32:18 -07001837 FAIL_IF(!span->initBounds(fCurve));
caryclark1049f122015-04-20 08:31:59 -07001838 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001839 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001840 SkTSpan<OppCurve, TCurve>* test = testBounded->fBounded;
1841 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001842 int oppSects, sects = this->intersects(span, opp, test, &oppSects);
1843 if (sects >= 1) {
1844 if (oppSects == 2) {
1845 test->initBounds(opp->fCurve);
1846 opp->removeAllBut(span, test, this);
1847 }
1848 if (sects == 2) {
1849 span->initBounds(fCurve);
1850 this->removeAllBut(test, span, opp);
caryclarka35ab3e2016-10-20 08:32:18 -07001851 return true;
caryclark54359292015-03-26 07:52:43 -07001852 }
reed0dc4dd62015-03-24 13:55:33 -07001853 } else {
caryclark54359292015-03-26 07:52:43 -07001854 if (span->removeBounded(test)) {
1855 this->removeSpan(span);
1856 }
1857 if (test->removeBounded(span)) {
1858 opp->removeSpan(test);
1859 }
1860 }
1861 testBounded = next;
1862 }
caryclarka35ab3e2016-10-20 08:32:18 -07001863 return true;
caryclark54359292015-03-26 07:52:43 -07001864}
1865
caryclark1049f122015-04-20 08:31:59 -07001866template<typename TCurve, typename OppCurve>
1867void SkTSect<TCurve, OppCurve>::unlinkSpan(SkTSpan<TCurve, OppCurve>* span) {
1868 SkTSpan<TCurve, OppCurve>* prev = span->fPrev;
1869 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001870 if (prev) {
1871 prev->fNext = next;
1872 if (next) {
1873 next->fPrev = prev;
caryclark643ede62016-08-08 14:27:45 -07001874 next->validate();
caryclark54359292015-03-26 07:52:43 -07001875 }
1876 } else {
1877 fHead = next;
1878 if (next) {
halcanary96fcdcc2015-08-27 07:41:13 -07001879 next->fPrev = nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001880 }
1881 }
1882}
1883
caryclark1049f122015-04-20 08:31:59 -07001884template<typename TCurve, typename OppCurve>
1885bool SkTSect<TCurve, OppCurve>::updateBounded(SkTSpan<TCurve, OppCurve>* first,
1886 SkTSpan<TCurve, OppCurve>* last, SkTSpan<OppCurve, TCurve>* oppFirst) {
1887 SkTSpan<TCurve, OppCurve>* test = first;
1888 const SkTSpan<TCurve, OppCurve>* final = last->next();
caryclark54359292015-03-26 07:52:43 -07001889 bool deleteSpan = false;
1890 do {
1891 deleteSpan |= test->removeAllBounded();
caryclarke25a4f62016-07-26 09:26:29 -07001892 } while ((test = test->fNext) != final && test);
halcanary96fcdcc2015-08-27 07:41:13 -07001893 first->fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -07001894 first->addBounded(oppFirst, &fHeap);
1895 // cannot call validate until remove span range is called
1896 return deleteSpan;
1897}
1898
1899
caryclark1049f122015-04-20 08:31:59 -07001900template<typename TCurve, typename OppCurve>
1901void SkTSect<TCurve, OppCurve>::validate() const {
caryclark643ede62016-08-08 14:27:45 -07001902#if DEBUG_VALIDATE
caryclark45fa4472015-01-16 07:04:10 -08001903 int count = 0;
caryclark643ede62016-08-08 14:27:45 -07001904 double last = 0;
caryclark45fa4472015-01-16 07:04:10 -08001905 if (fHead) {
caryclark1049f122015-04-20 08:31:59 -07001906 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark45fa4472015-01-16 07:04:10 -08001907 SkASSERT(!span->fPrev);
caryclark643ede62016-08-08 14:27:45 -07001908 const SkTSpan<TCurve, OppCurve>* next;
caryclark45fa4472015-01-16 07:04:10 -08001909 do {
1910 span->validate();
1911 SkASSERT(span->fStartT >= last);
caryclark643ede62016-08-08 14:27:45 -07001912 last = span->fEndT;
caryclark45fa4472015-01-16 07:04:10 -08001913 ++count;
caryclark643ede62016-08-08 14:27:45 -07001914 next = span->fNext;
1915 SkASSERT(next != span);
1916 } while ((span = next) != nullptr);
caryclark45fa4472015-01-16 07:04:10 -08001917 }
1918 SkASSERT(count == fActiveCount);
caryclark643ede62016-08-08 14:27:45 -07001919#endif
1920#if DEBUG_T_SECT
caryclark45fa4472015-01-16 07:04:10 -08001921 SkASSERT(fActiveCount <= fDebugAllocatedCount);
1922 int deletedCount = 0;
caryclark1049f122015-04-20 08:31:59 -07001923 const SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001924 while (deleted) {
1925 ++deletedCount;
1926 deleted = deleted->fNext;
1927 }
caryclark1049f122015-04-20 08:31:59 -07001928 const SkTSpan<TCurve, OppCurve>* coincident = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001929 while (coincident) {
1930 ++deletedCount;
1931 coincident = coincident->fNext;
1932 }
caryclark45fa4472015-01-16 07:04:10 -08001933 SkASSERT(fActiveCount + deletedCount == fDebugAllocatedCount);
caryclarkccec0f92015-03-24 07:28:17 -07001934#endif
caryclark54359292015-03-26 07:52:43 -07001935}
1936
caryclark1049f122015-04-20 08:31:59 -07001937template<typename TCurve, typename OppCurve>
1938void SkTSect<TCurve, OppCurve>::validateBounded() const {
caryclark643ede62016-08-08 14:27:45 -07001939#if DEBUG_VALIDATE
caryclark54359292015-03-26 07:52:43 -07001940 if (!fHead) {
1941 return;
1942 }
caryclark1049f122015-04-20 08:31:59 -07001943 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark54359292015-03-26 07:52:43 -07001944 do {
1945 span->validateBounded();
halcanary96fcdcc2015-08-27 07:41:13 -07001946 } while ((span = span->fNext) != nullptr);
caryclark54359292015-03-26 07:52:43 -07001947#endif
1948}
caryclark45fa4472015-01-16 07:04:10 -08001949
caryclark1049f122015-04-20 08:31:59 -07001950template<typename TCurve, typename OppCurve>
1951int SkTSect<TCurve, OppCurve>::EndsEqual(const SkTSect<TCurve, OppCurve>* sect1,
1952 const SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark45fa4472015-01-16 07:04:10 -08001953 int zeroOneSet = 0;
caryclark54359292015-03-26 07:52:43 -07001954 if (sect1->fCurve[0] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001955 zeroOneSet |= kZeroS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001956 intersections->insert(0, 0, sect1->fCurve[0]);
1957 }
caryclark1049f122015-04-20 08:31:59 -07001958 if (sect1->fCurve[0] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001959 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark54359292015-03-26 07:52:43 -07001960 intersections->insert(0, 1, sect1->fCurve[0]);
1961 }
1962 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001963 zeroOneSet |= kOneS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001964 intersections->insert(1, 0, sect1->fCurve[TCurve::kPointLast]);
1965 }
caryclark1049f122015-04-20 08:31:59 -07001966 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001967 zeroOneSet |= kOneS1Set | kOneS2Set;
reed0dc4dd62015-03-24 13:55:33 -07001968 intersections->insert(1, 1, sect1->fCurve[TCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001969 }
1970 // check for zero
1971 if (!(zeroOneSet & (kZeroS1Set | kZeroS2Set))
1972 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[0])) {
1973 zeroOneSet |= kZeroS1Set | kZeroS2Set;
1974 intersections->insertNear(0, 0, sect1->fCurve[0], sect2->fCurve[0]);
1975 }
1976 if (!(zeroOneSet & (kZeroS1Set | kOneS2Set))
caryclark1049f122015-04-20 08:31:59 -07001977 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07001978 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark1049f122015-04-20 08:31:59 -07001979 intersections->insertNear(0, 1, sect1->fCurve[0], sect2->fCurve[OppCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001980 }
1981 // check for one
1982 if (!(zeroOneSet & (kOneS1Set | kZeroS2Set))
1983 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[0])) {
1984 zeroOneSet |= kOneS1Set | kZeroS2Set;
1985 intersections->insertNear(1, 0, sect1->fCurve[TCurve::kPointLast], sect2->fCurve[0]);
1986 }
1987 if (!(zeroOneSet & (kOneS1Set | kOneS2Set))
1988 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[
caryclark1049f122015-04-20 08:31:59 -07001989 OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07001990 zeroOneSet |= kOneS1Set | kOneS2Set;
1991 intersections->insertNear(1, 1, sect1->fCurve[TCurve::kPointLast],
caryclark1049f122015-04-20 08:31:59 -07001992 sect2->fCurve[OppCurve::kPointLast]);
caryclark45fa4472015-01-16 07:04:10 -08001993 }
1994 return zeroOneSet;
1995}
1996
caryclark1049f122015-04-20 08:31:59 -07001997template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08001998struct SkClosestRecord {
caryclark54359292015-03-26 07:52:43 -07001999 bool operator<(const SkClosestRecord& rh) const {
2000 return fClosest < rh.fClosest;
2001 }
2002
caryclark45fa4472015-01-16 07:04:10 -08002003 void addIntersection(SkIntersections* intersections) const {
2004 double r1t = fC1Index ? fC1Span->endT() : fC1Span->startT();
2005 double r2t = fC2Index ? fC2Span->endT() : fC2Span->startT();
2006 intersections->insert(r1t, r2t, fC1Span->part()[fC1Index]);
2007 }
2008
caryclark1049f122015-04-20 08:31:59 -07002009 void findEnd(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2,
caryclark45fa4472015-01-16 07:04:10 -08002010 int c1Index, int c2Index) {
2011 const TCurve& c1 = span1->part();
caryclark1049f122015-04-20 08:31:59 -07002012 const OppCurve& c2 = span2->part();
caryclark45fa4472015-01-16 07:04:10 -08002013 if (!c1[c1Index].approximatelyEqual(c2[c2Index])) {
2014 return;
2015 }
2016 double dist = c1[c1Index].distanceSquared(c2[c2Index]);
2017 if (fClosest < dist) {
2018 return;
2019 }
2020 fC1Span = span1;
2021 fC2Span = span2;
2022 fC1StartT = span1->startT();
2023 fC1EndT = span1->endT();
2024 fC2StartT = span2->startT();
2025 fC2EndT = span2->endT();
2026 fC1Index = c1Index;
2027 fC2Index = c2Index;
2028 fClosest = dist;
2029 }
2030
caryclarkcdeff812016-07-22 03:34:19 -07002031 bool matesWith(const SkClosestRecord& mate SkDEBUGPARAMS(SkIntersections* i)) const {
Cary Clark67116382017-01-03 16:25:18 -05002032 SkOPOBJASSERT(i, fC1Span == mate.fC1Span || fC1Span->endT() <= mate.fC1Span->startT()
caryclark45fa4472015-01-16 07:04:10 -08002033 || mate.fC1Span->endT() <= fC1Span->startT());
caryclarkcdeff812016-07-22 03:34:19 -07002034 SkOPOBJASSERT(i, fC2Span == mate.fC2Span || fC2Span->endT() <= mate.fC2Span->startT()
caryclark45fa4472015-01-16 07:04:10 -08002035 || mate.fC2Span->endT() <= fC2Span->startT());
2036 return fC1Span == mate.fC1Span || fC1Span->endT() == mate.fC1Span->startT()
2037 || fC1Span->startT() == mate.fC1Span->endT()
2038 || fC2Span == mate.fC2Span
2039 || fC2Span->endT() == mate.fC2Span->startT()
2040 || fC2Span->startT() == mate.fC2Span->endT();
2041 }
2042
2043 void merge(const SkClosestRecord& mate) {
2044 fC1Span = mate.fC1Span;
2045 fC2Span = mate.fC2Span;
2046 fClosest = mate.fClosest;
2047 fC1Index = mate.fC1Index;
2048 fC2Index = mate.fC2Index;
2049 }
caryclark55888e42016-07-18 10:01:36 -07002050
caryclark45fa4472015-01-16 07:04:10 -08002051 void reset() {
2052 fClosest = FLT_MAX;
halcanary96fcdcc2015-08-27 07:41:13 -07002053 SkDEBUGCODE(fC1Span = nullptr);
2054 SkDEBUGCODE(fC2Span = nullptr);
caryclark45fa4472015-01-16 07:04:10 -08002055 SkDEBUGCODE(fC1Index = fC2Index = -1);
2056 }
2057
2058 void update(const SkClosestRecord& mate) {
2059 fC1StartT = SkTMin(fC1StartT, mate.fC1StartT);
2060 fC1EndT = SkTMax(fC1EndT, mate.fC1EndT);
2061 fC2StartT = SkTMin(fC2StartT, mate.fC2StartT);
2062 fC2EndT = SkTMax(fC2EndT, mate.fC2EndT);
2063 }
2064
caryclark1049f122015-04-20 08:31:59 -07002065 const SkTSpan<TCurve, OppCurve>* fC1Span;
2066 const SkTSpan<OppCurve, TCurve>* fC2Span;
caryclark45fa4472015-01-16 07:04:10 -08002067 double fC1StartT;
2068 double fC1EndT;
2069 double fC2StartT;
2070 double fC2EndT;
2071 double fClosest;
2072 int fC1Index;
2073 int fC2Index;
2074};
2075
caryclark1049f122015-04-20 08:31:59 -07002076template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08002077struct SkClosestSect {
2078 SkClosestSect()
2079 : fUsed(0) {
2080 fClosest.push_back().reset();
2081 }
2082
caryclarkcdeff812016-07-22 03:34:19 -07002083 bool find(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2
2084 SkDEBUGPARAMS(SkIntersections* i)) {
caryclark1049f122015-04-20 08:31:59 -07002085 SkClosestRecord<TCurve, OppCurve>* record = &fClosest[fUsed];
caryclark45fa4472015-01-16 07:04:10 -08002086 record->findEnd(span1, span2, 0, 0);
caryclark1049f122015-04-20 08:31:59 -07002087 record->findEnd(span1, span2, 0, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002088 record->findEnd(span1, span2, TCurve::kPointLast, 0);
caryclark1049f122015-04-20 08:31:59 -07002089 record->findEnd(span1, span2, TCurve::kPointLast, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002090 if (record->fClosest == FLT_MAX) {
caryclark54359292015-03-26 07:52:43 -07002091 return false;
caryclark45fa4472015-01-16 07:04:10 -08002092 }
2093 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002094 SkClosestRecord<TCurve, OppCurve>* test = &fClosest[index];
caryclarkcdeff812016-07-22 03:34:19 -07002095 if (test->matesWith(*record SkDEBUGPARAMS(i))) {
caryclark45fa4472015-01-16 07:04:10 -08002096 if (test->fClosest > record->fClosest) {
2097 test->merge(*record);
2098 }
2099 test->update(*record);
2100 record->reset();
caryclark54359292015-03-26 07:52:43 -07002101 return false;
caryclark45fa4472015-01-16 07:04:10 -08002102 }
2103 }
2104 ++fUsed;
2105 fClosest.push_back().reset();
caryclark54359292015-03-26 07:52:43 -07002106 return true;
caryclark45fa4472015-01-16 07:04:10 -08002107 }
2108
2109 void finish(SkIntersections* intersections) const {
caryclark1049f122015-04-20 08:31:59 -07002110 SkSTArray<TCurve::kMaxIntersections * 3,
2111 const SkClosestRecord<TCurve, OppCurve>*, true> closestPtrs;
caryclark45fa4472015-01-16 07:04:10 -08002112 for (int index = 0; index < fUsed; ++index) {
caryclark54359292015-03-26 07:52:43 -07002113 closestPtrs.push_back(&fClosest[index]);
2114 }
caryclark1049f122015-04-20 08:31:59 -07002115 SkTQSort<const SkClosestRecord<TCurve, OppCurve> >(closestPtrs.begin(), closestPtrs.end()
2116 - 1);
caryclark54359292015-03-26 07:52:43 -07002117 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002118 const SkClosestRecord<TCurve, OppCurve>* test = closestPtrs[index];
caryclark54359292015-03-26 07:52:43 -07002119 test->addIntersection(intersections);
caryclark45fa4472015-01-16 07:04:10 -08002120 }
2121 }
2122
caryclark54359292015-03-26 07:52:43 -07002123 // this is oversized so that an extra records can merge into final one
caryclark1049f122015-04-20 08:31:59 -07002124 SkSTArray<TCurve::kMaxIntersections * 2, SkClosestRecord<TCurve, OppCurve>, true> fClosest;
caryclark45fa4472015-01-16 07:04:10 -08002125 int fUsed;
2126};
2127
2128// returns true if the rect is too small to consider
caryclark1049f122015-04-20 08:31:59 -07002129template<typename TCurve, typename OppCurve>
2130void SkTSect<TCurve, OppCurve>::BinarySearch(SkTSect<TCurve, OppCurve>* sect1,
2131 SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark54359292015-03-26 07:52:43 -07002132#if DEBUG_T_SECT_DUMP > 1
2133 gDumpTSectNum = 0;
2134#endif
caryclark1049f122015-04-20 08:31:59 -07002135 SkDEBUGCODE(sect1->fOppSect = sect2);
2136 SkDEBUGCODE(sect2->fOppSect = sect1);
caryclark45fa4472015-01-16 07:04:10 -08002137 intersections->reset();
caryclarka35ab3e2016-10-20 08:32:18 -07002138 intersections->setMax(TCurve::kMaxIntersections + 4); // give extra for slop
caryclark1049f122015-04-20 08:31:59 -07002139 SkTSpan<TCurve, OppCurve>* span1 = sect1->fHead;
2140 SkTSpan<OppCurve, TCurve>* span2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002141 int oppSect, sect = sect1->intersects(span1, sect2, span2, &oppSect);
2142// SkASSERT(between(0, sect, 2));
2143 if (!sect) {
caryclark45fa4472015-01-16 07:04:10 -08002144 return;
2145 }
caryclark54359292015-03-26 07:52:43 -07002146 if (sect == 2 && oppSect == 2) {
caryclark45fa4472015-01-16 07:04:10 -08002147 (void) EndsEqual(sect1, sect2, intersections);
2148 return;
2149 }
caryclark54359292015-03-26 07:52:43 -07002150 span1->addBounded(span2, &sect1->fHeap);
2151 span2->addBounded(span1, &sect2->fHeap);
caryclark26ad22a2015-10-16 09:03:38 -07002152 const int kMaxCoinLoopCount = 8;
2153 int coinLoopCount = kMaxCoinLoopCount;
2154 double start1s SK_INIT_TO_AVOID_WARNING;
2155 double start1e SK_INIT_TO_AVOID_WARNING;
caryclark45fa4472015-01-16 07:04:10 -08002156 do {
2157 // find the largest bounds
caryclark1049f122015-04-20 08:31:59 -07002158 SkTSpan<TCurve, OppCurve>* largest1 = sect1->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002159 if (!largest1) {
2160 break;
2161 }
caryclark1049f122015-04-20 08:31:59 -07002162 SkTSpan<OppCurve, TCurve>* largest2 = sect2->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002163 // split it
caryclark1049f122015-04-20 08:31:59 -07002164 if (!largest2 || (largest1 && (largest1->fBoundsMax > largest2->fBoundsMax
2165 || (!largest1->fCollapsed && largest2->fCollapsed)))) {
2166 if (largest1->fCollapsed) {
2167 break;
2168 }
caryclark34efb702016-10-24 08:19:06 -07002169 sect1->resetRemovedEnds();
2170 sect2->resetRemovedEnds();
caryclark1049f122015-04-20 08:31:59 -07002171 // trim parts that don't intersect the opposite
2172 SkTSpan<TCurve, OppCurve>* half1 = sect1->addOne();
caryclark643ede62016-08-08 14:27:45 -07002173 SkDEBUGCODE(half1->debugSetGlobalState(sect1->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002174 if (!half1->split(largest1, &sect1->fHeap)) {
2175 break;
2176 }
caryclarka35ab3e2016-10-20 08:32:18 -07002177 if (!sect1->trim(largest1, sect2)) {
2178 SkOPOBJASSERT(intersections, 0);
2179 return;
2180 }
2181 if (!sect1->trim(half1, sect2)) {
2182 SkOPOBJASSERT(intersections, 0);
2183 return;
2184 }
caryclark1049f122015-04-20 08:31:59 -07002185 } else {
2186 if (largest2->fCollapsed) {
2187 break;
2188 }
caryclark34efb702016-10-24 08:19:06 -07002189 sect1->resetRemovedEnds();
2190 sect2->resetRemovedEnds();
caryclark1049f122015-04-20 08:31:59 -07002191 // trim parts that don't intersect the opposite
2192 SkTSpan<OppCurve, TCurve>* half2 = sect2->addOne();
caryclark643ede62016-08-08 14:27:45 -07002193 SkDEBUGCODE(half2->debugSetGlobalState(sect2->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002194 if (!half2->split(largest2, &sect2->fHeap)) {
2195 break;
2196 }
caryclarka35ab3e2016-10-20 08:32:18 -07002197 if (!sect2->trim(largest2, sect1)) {
2198 SkOPOBJASSERT(intersections, 0);
2199 return;
2200 }
2201 if (!sect2->trim(half2, sect1)) {
2202 SkOPOBJASSERT(intersections, 0);
2203 return;
2204 }
caryclark45fa4472015-01-16 07:04:10 -08002205 }
caryclark54359292015-03-26 07:52:43 -07002206 sect1->validate();
2207 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002208#if DEBUG_T_SECT_LOOP_COUNT
2209 intersections->debugBumpLoopCount(SkIntersections::kIterations_DebugLoop);
2210#endif
caryclark45fa4472015-01-16 07:04:10 -08002211 // if there are 9 or more continuous spans on both sects, suspect coincidence
2212 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2213 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
caryclark26ad22a2015-10-16 09:03:38 -07002214 if (coinLoopCount == kMaxCoinLoopCount) {
2215 start1s = sect1->fHead->fStartT;
2216 start1e = sect1->tail()->fEndT;
2217 }
caryclarkef7cee42016-09-06 09:05:54 -07002218 if (!sect1->coincidentCheck(sect2)) {
2219 return;
2220 }
caryclark54359292015-03-26 07:52:43 -07002221 sect1->validate();
2222 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002223#if DEBUG_T_SECT_LOOP_COUNT
2224 intersections->debugBumpLoopCount(SkIntersections::kCoinCheck_DebugLoop);
2225#endif
caryclarkef784fb2015-10-30 12:03:06 -07002226 if (!--coinLoopCount && sect1->fHead && sect2->fHead) {
caryclark26ad22a2015-10-16 09:03:38 -07002227 /* All known working cases resolve in two tries. Sadly, cubicConicTests[0]
2228 gets stuck in a loop. It adds an extension to allow a coincident end
2229 perpendicular to track its intersection in the opposite curve. However,
2230 the bounding box of the extension does not intersect the original curve,
caryclark55888e42016-07-18 10:01:36 -07002231 so the extension is discarded, only to be added again the next time around. */
caryclark26ad22a2015-10-16 09:03:38 -07002232 sect1->coincidentForce(sect2, start1s, start1e);
2233 sect1->validate();
2234 sect2->validate();
2235 }
caryclark45fa4472015-01-16 07:04:10 -08002236 }
caryclark54359292015-03-26 07:52:43 -07002237 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2238 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
Cary Clark59ed4822016-12-08 16:17:56 -05002239 if (!sect1->fHead) {
2240 return;
2241 }
caryclark54359292015-03-26 07:52:43 -07002242 sect1->computePerpendiculars(sect2, sect1->fHead, sect1->tail());
Cary Clark59ed4822016-12-08 16:17:56 -05002243 if (!sect2->fHead) {
2244 return;
2245 }
caryclark54359292015-03-26 07:52:43 -07002246 sect2->computePerpendiculars(sect1, sect2->fHead, sect2->tail());
2247 sect1->removeByPerpendicular(sect2);
2248 sect1->validate();
2249 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002250#if DEBUG_T_SECT_LOOP_COUNT
2251 intersections->debugBumpLoopCount(SkIntersections::kComputePerp_DebugLoop);
2252#endif
caryclark1049f122015-04-20 08:31:59 -07002253 if (sect1->collapsed() > TCurve::kMaxIntersections) {
2254 break;
2255 }
caryclark54359292015-03-26 07:52:43 -07002256 }
2257#if DEBUG_T_SECT_DUMP
2258 sect1->dumpBoth(sect2);
caryclark45fa4472015-01-16 07:04:10 -08002259#endif
2260 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002261 break;
caryclark45fa4472015-01-16 07:04:10 -08002262 }
2263 } while (true);
caryclark1049f122015-04-20 08:31:59 -07002264 SkTSpan<TCurve, OppCurve>* coincident = sect1->fCoincident;
caryclark54359292015-03-26 07:52:43 -07002265 if (coincident) {
2266 // if there is more than one coincident span, check loosely to see if they should be joined
2267 if (coincident->fNext) {
2268 sect1->mergeCoincidence(sect2);
2269 coincident = sect1->fCoincident;
2270 }
2271 SkASSERT(sect2->fCoincident); // courtesy check : coincidence only looks at sect 1
caryclark45fa4472015-01-16 07:04:10 -08002272 do {
caryclark221a4bb2016-10-07 11:15:15 -07002273 if (!coincident) {
2274 return;
2275 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002276 if (!coincident->fCoinStart.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002277 continue;
2278 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002279 if (!coincident->fCoinEnd.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002280 continue;
2281 }
Cary Clark67116382017-01-03 16:25:18 -05002282 double perpT = coincident->fCoinStart.perpT();
2283 if (perpT < 0) {
2284 return;
2285 }
caryclark54359292015-03-26 07:52:43 -07002286 int index = intersections->insertCoincident(coincident->fStartT,
Cary Clark67116382017-01-03 16:25:18 -05002287 perpT, coincident->fPart[0]);
caryclark54359292015-03-26 07:52:43 -07002288 if ((intersections->insertCoincident(coincident->fEndT,
2289 coincident->fCoinEnd.perpT(),
2290 coincident->fPart[TCurve::kPointLast]) < 0) && index >= 0) {
caryclark45fa4472015-01-16 07:04:10 -08002291 intersections->clearCoincidence(index);
2292 }
caryclark54359292015-03-26 07:52:43 -07002293 } while ((coincident = coincident->fNext));
2294 }
caryclark1049f122015-04-20 08:31:59 -07002295 int zeroOneSet = EndsEqual(sect1, sect2, intersections);
caryclark34efb702016-10-24 08:19:06 -07002296// if (!sect1->fHead || !sect2->fHead) {
caryclark6c3b9cd2016-09-26 05:36:58 -07002297 // if the final iteration contains an end (0 or 1),
2298 if (sect1->fRemovedStartT && !(zeroOneSet & kZeroS1Set)) {
2299 SkTCoincident<TCurve, OppCurve> perp; // intersect perpendicular with opposite curve
caryclarka35ab3e2016-10-20 08:32:18 -07002300 perp.setPerp(sect1->fCurve, 0, sect1->fCurve[0], sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002301 if (perp.isMatch()) {
2302 intersections->insert(0, perp.perpT(), perp.perpPt());
2303 }
2304 }
2305 if (sect1->fRemovedEndT && !(zeroOneSet & kOneS1Set)) {
2306 SkTCoincident<TCurve, OppCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002307 perp.setPerp(sect1->fCurve, 1, sect1->fCurve[TCurve::kPointLast], sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002308 if (perp.isMatch()) {
2309 intersections->insert(1, perp.perpT(), perp.perpPt());
2310 }
2311 }
2312 if (sect2->fRemovedStartT && !(zeroOneSet & kZeroS2Set)) {
2313 SkTCoincident<OppCurve, TCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002314 perp.setPerp(sect2->fCurve, 0, sect2->fCurve[0], sect1->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002315 if (perp.isMatch()) {
2316 intersections->insert(perp.perpT(), 0, perp.perpPt());
2317 }
2318 }
2319 if (sect2->fRemovedEndT && !(zeroOneSet & kOneS2Set)) {
2320 SkTCoincident<OppCurve, TCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002321 perp.setPerp(sect2->fCurve, 1, sect2->fCurve[OppCurve::kPointLast], sect1->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002322 if (perp.isMatch()) {
2323 intersections->insert(perp.perpT(), 1, perp.perpPt());
2324 }
2325 }
caryclark34efb702016-10-24 08:19:06 -07002326// }
2327 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002328 return;
caryclark45fa4472015-01-16 07:04:10 -08002329 }
caryclark45fa4472015-01-16 07:04:10 -08002330 sect1->recoverCollapsed();
2331 sect2->recoverCollapsed();
caryclark1049f122015-04-20 08:31:59 -07002332 SkTSpan<TCurve, OppCurve>* result1 = sect1->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002333 // check heads and tails for zero and ones and insert them if we haven't already done so
caryclark1049f122015-04-20 08:31:59 -07002334 const SkTSpan<TCurve, OppCurve>* head1 = result1;
caryclark45fa4472015-01-16 07:04:10 -08002335 if (!(zeroOneSet & kZeroS1Set) && approximately_less_than_zero(head1->fStartT)) {
2336 const SkDPoint& start1 = sect1->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002337 if (head1->isBounded()) {
2338 double t = head1->closestBoundedT(start1);
2339 if (sect2->fCurve.ptAtT(t).approximatelyEqual(start1)) {
2340 intersections->insert(0, t, start1);
2341 }
caryclark45fa4472015-01-16 07:04:10 -08002342 }
2343 }
caryclark1049f122015-04-20 08:31:59 -07002344 const SkTSpan<OppCurve, TCurve>* head2 = sect2->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002345 if (!(zeroOneSet & kZeroS2Set) && approximately_less_than_zero(head2->fStartT)) {
2346 const SkDPoint& start2 = sect2->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002347 if (head2->isBounded()) {
2348 double t = head2->closestBoundedT(start2);
2349 if (sect1->fCurve.ptAtT(t).approximatelyEqual(start2)) {
2350 intersections->insert(t, 0, start2);
2351 }
caryclark45fa4472015-01-16 07:04:10 -08002352 }
2353 }
caryclark1049f122015-04-20 08:31:59 -07002354 const SkTSpan<TCurve, OppCurve>* tail1 = sect1->tail();
caryclark45fa4472015-01-16 07:04:10 -08002355 if (!(zeroOneSet & kOneS1Set) && approximately_greater_than_one(tail1->fEndT)) {
2356 const SkDPoint& end1 = sect1->fCurve[TCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002357 if (tail1->isBounded()) {
2358 double t = tail1->closestBoundedT(end1);
2359 if (sect2->fCurve.ptAtT(t).approximatelyEqual(end1)) {
2360 intersections->insert(1, t, end1);
2361 }
caryclark45fa4472015-01-16 07:04:10 -08002362 }
2363 }
caryclark1049f122015-04-20 08:31:59 -07002364 const SkTSpan<OppCurve, TCurve>* tail2 = sect2->tail();
caryclark45fa4472015-01-16 07:04:10 -08002365 if (!(zeroOneSet & kOneS2Set) && approximately_greater_than_one(tail2->fEndT)) {
caryclark1049f122015-04-20 08:31:59 -07002366 const SkDPoint& end2 = sect2->fCurve[OppCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002367 if (tail2->isBounded()) {
2368 double t = tail2->closestBoundedT(end2);
2369 if (sect1->fCurve.ptAtT(t).approximatelyEqual(end2)) {
2370 intersections->insert(t, 1, end2);
2371 }
caryclark45fa4472015-01-16 07:04:10 -08002372 }
2373 }
caryclark1049f122015-04-20 08:31:59 -07002374 SkClosestSect<TCurve, OppCurve> closest;
caryclark45fa4472015-01-16 07:04:10 -08002375 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07002376 while (result1 && result1->fCoinStart.isMatch() && result1->fCoinEnd.isMatch()) {
caryclark45fa4472015-01-16 07:04:10 -08002377 result1 = result1->fNext;
2378 }
2379 if (!result1) {
2380 break;
2381 }
caryclark1049f122015-04-20 08:31:59 -07002382 SkTSpan<OppCurve, TCurve>* result2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002383 bool found = false;
caryclark45fa4472015-01-16 07:04:10 -08002384 while (result2) {
caryclarkcdeff812016-07-22 03:34:19 -07002385 found |= closest.find(result1, result2 SkDEBUGPARAMS(intersections));
caryclark45fa4472015-01-16 07:04:10 -08002386 result2 = result2->fNext;
2387 }
caryclark45fa4472015-01-16 07:04:10 -08002388 } while ((result1 = result1->fNext));
2389 closest.finish(intersections);
caryclark54359292015-03-26 07:52:43 -07002390 // if there is more than one intersection and it isn't already coincident, check
2391 int last = intersections->used() - 1;
2392 for (int index = 0; index < last; ) {
2393 if (intersections->isCoincident(index) && intersections->isCoincident(index + 1)) {
2394 ++index;
2395 continue;
2396 }
2397 double midT = ((*intersections)[0][index] + (*intersections)[0][index + 1]) / 2;
2398 SkDPoint midPt = sect1->fCurve.ptAtT(midT);
2399 // intersect perpendicular with opposite curve
caryclark1049f122015-04-20 08:31:59 -07002400 SkTCoincident<TCurve, OppCurve> perp;
caryclark54359292015-03-26 07:52:43 -07002401 perp.setPerp(sect1->fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002402 if (!perp.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07002403 ++index;
2404 continue;
2405 }
2406 if (intersections->isCoincident(index)) {
2407 intersections->removeOne(index);
2408 --last;
2409 } else if (intersections->isCoincident(index + 1)) {
2410 intersections->removeOne(index + 1);
2411 --last;
2412 } else {
2413 intersections->setCoincident(index++);
2414 }
2415 intersections->setCoincident(index);
2416 }
caryclarka35ab3e2016-10-20 08:32:18 -07002417 SkOPOBJASSERT(intersections, intersections->used() <= TCurve::kMaxIntersections);
caryclark45fa4472015-01-16 07:04:10 -08002418}
deanm12670eb2016-04-26 14:09:01 -07002419
2420#endif