blob: f22322bbe16248ec44aa4454f98e260ea7a5e9a4 [file] [log] [blame]
caryclark45fa4472015-01-16 07:04:10 -08001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
deanm12670eb2016-04-26 14:09:01 -07007#ifndef SkPathOpsTSect_DEFINED
8#define SkPathOpsTSect_DEFINED
caryclark45fa4472015-01-16 07:04:10 -08009
10#include "SkChunkAlloc.h"
caryclark54359292015-03-26 07:52:43 -070011#include "SkPathOpsBounds.h"
caryclark45fa4472015-01-16 07:04:10 -080012#include "SkPathOpsRect.h"
caryclark45fa4472015-01-16 07:04:10 -080013#include "SkIntersections.h"
caryclark54359292015-03-26 07:52:43 -070014#include "SkTSort.h"
caryclark45fa4472015-01-16 07:04:10 -080015
caryclarked0935a2015-10-22 07:23:52 -070016#ifdef SK_DEBUG
17typedef uint8_t SkOpDebugBool;
18#else
19typedef bool SkOpDebugBool;
20#endif
21
caryclark1049f122015-04-20 08:31:59 -070022/* TCurve and OppCurve are one of { SkDQuadratic, SkDConic, SkDCubic } */
23template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -080024class SkTCoincident {
25public:
caryclark697ac1c2015-04-13 09:36:01 -070026 SkTCoincident() {
caryclarkdf386c52015-04-21 05:27:02 -070027 this->init();
caryclark1049f122015-04-20 08:31:59 -070028 }
29
caryclarked0935a2015-10-22 07:23:52 -070030 void debugInit() {
31#ifdef SK_DEBUG
32 this->fPerpPt.fX = this->fPerpPt.fY = SK_ScalarNaN;
33 this->fPerpT = SK_ScalarNaN;
caryclark6c3b9cd2016-09-26 05:36:58 -070034 this->fMatch = 0xFF;
caryclarked0935a2015-10-22 07:23:52 -070035#endif
36 }
37
38 char dumpIsCoincidentStr() const;
caryclark1049f122015-04-20 08:31:59 -070039 void dump() const;
40
caryclark6c3b9cd2016-09-26 05:36:58 -070041 bool isMatch() const {
42 SkASSERT(!!fMatch == fMatch);
43 return SkToBool(fMatch);
caryclark45fa4472015-01-16 07:04:10 -080044 }
45
46 void init() {
caryclarkdf386c52015-04-21 05:27:02 -070047 fPerpT = -1;
caryclark6c3b9cd2016-09-26 05:36:58 -070048 fMatch = false;
caryclarkdf386c52015-04-21 05:27:02 -070049 fPerpPt.fX = fPerpPt.fY = SK_ScalarNaN;
caryclark45fa4472015-01-16 07:04:10 -080050 }
51
52 void markCoincident() {
caryclark6c3b9cd2016-09-26 05:36:58 -070053 if (!fMatch) {
caryclark45fa4472015-01-16 07:04:10 -080054 fPerpT = -1;
55 }
caryclark6c3b9cd2016-09-26 05:36:58 -070056 fMatch = true;
caryclark45fa4472015-01-16 07:04:10 -080057 }
58
59 const SkDPoint& perpPt() const {
60 return fPerpPt;
61 }
62
63 double perpT() const {
64 return fPerpT;
65 }
66
caryclark1049f122015-04-20 08:31:59 -070067 void setPerp(const TCurve& c1, double t, const SkDPoint& cPt, const OppCurve& );
caryclark45fa4472015-01-16 07:04:10 -080068
69private:
70 SkDPoint fPerpPt;
71 double fPerpT; // perpendicular intersection on opposite curve
caryclark6c3b9cd2016-09-26 05:36:58 -070072 SkOpDebugBool fMatch;
caryclark45fa4472015-01-16 07:04:10 -080073};
74
caryclark1049f122015-04-20 08:31:59 -070075template<typename TCurve, typename OppCurve> class SkTSect;
76template<typename TCurve, typename OppCurve> class SkTSpan;
caryclark54359292015-03-26 07:52:43 -070077
caryclark1049f122015-04-20 08:31:59 -070078template<typename TCurve, typename OppCurve>
caryclark54359292015-03-26 07:52:43 -070079struct SkTSpanBounded {
caryclark1049f122015-04-20 08:31:59 -070080 SkTSpan<TCurve, OppCurve>* fBounded;
caryclark54359292015-03-26 07:52:43 -070081 SkTSpanBounded* fNext;
82};
caryclark45fa4472015-01-16 07:04:10 -080083
84/* Curve is either TCurve or SkDCubic */
caryclark1049f122015-04-20 08:31:59 -070085template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -080086class SkTSpan {
87public:
caryclark1049f122015-04-20 08:31:59 -070088 void addBounded(SkTSpan<OppCurve, TCurve>* , SkChunkAlloc* );
reed0dc4dd62015-03-24 13:55:33 -070089 double closestBoundedT(const SkDPoint& pt) const;
caryclark54359292015-03-26 07:52:43 -070090 bool contains(double t) const;
reed0dc4dd62015-03-24 13:55:33 -070091
caryclark1049f122015-04-20 08:31:59 -070092 void debugInit() {
93 TCurve dummy;
94 dummy.debugInit();
95 init(dummy);
96 initBounds(dummy);
caryclarkdf386c52015-04-21 05:27:02 -070097 fCoinStart.init();
98 fCoinEnd.init();
caryclark1049f122015-04-20 08:31:59 -070099 }
100
101 const SkTSect<OppCurve, TCurve>* debugOpp() const;
caryclark643ede62016-08-08 14:27:45 -0700102
103#ifdef SK_DEBUG
104 void debugSetGlobalState(SkOpGlobalState* state) {
105 fDebugGlobalState = state;
106 }
107#endif
108
caryclark54359292015-03-26 07:52:43 -0700109 const SkTSpan* debugSpan(int ) const;
110 const SkTSpan* debugT(double t) const;
111#ifdef SK_DEBUG
112 bool debugIsBefore(const SkTSpan* span) const;
113#endif
114 void dump() const;
caryclark26ad22a2015-10-16 09:03:38 -0700115 void dumpAll() const;
caryclark1049f122015-04-20 08:31:59 -0700116 void dumpBounded(int id) const;
117 void dumpBounds() const;
118 void dumpCoin() const;
caryclark45fa4472015-01-16 07:04:10 -0800119
120 double endT() const {
121 return fEndT;
122 }
123
caryclark1049f122015-04-20 08:31:59 -0700124 SkTSpan<OppCurve, TCurve>* findOppSpan(const SkTSpan<OppCurve, TCurve>* opp) const;
caryclark54359292015-03-26 07:52:43 -0700125
caryclark1049f122015-04-20 08:31:59 -0700126 SkTSpan<OppCurve, TCurve>* findOppT(double t) const {
127 SkTSpan<OppCurve, TCurve>* result = oppT(t);
caryclark643ede62016-08-08 14:27:45 -0700128 SkOPASSERT(result);
caryclark45fa4472015-01-16 07:04:10 -0800129 return result;
130 }
131
caryclark643ede62016-08-08 14:27:45 -0700132 SkDEBUGCODE(SkOpGlobalState* globalState() const { return fDebugGlobalState; })
133
caryclark54359292015-03-26 07:52:43 -0700134 bool hasOppT(double t) const {
135 return SkToBool(oppT(t));
136 }
137
caryclark1049f122015-04-20 08:31:59 -0700138 int hullsIntersect(SkTSpan<OppCurve, TCurve>* span, bool* start, bool* oppStart);
caryclark54359292015-03-26 07:52:43 -0700139 void init(const TCurve& );
140 void initBounds(const TCurve& );
141
142 bool isBounded() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700143 return fBounded != nullptr;
caryclark54359292015-03-26 07:52:43 -0700144 }
145
caryclark1049f122015-04-20 08:31:59 -0700146 bool linearsIntersect(SkTSpan<OppCurve, TCurve>* span);
caryclark54359292015-03-26 07:52:43 -0700147 double linearT(const SkDPoint& ) const;
148
149 void markCoincident() {
150 fCoinStart.markCoincident();
151 fCoinEnd.markCoincident();
152 }
caryclark45fa4472015-01-16 07:04:10 -0800153
154 const SkTSpan* next() const {
155 return fNext;
156 }
157
caryclark1049f122015-04-20 08:31:59 -0700158 bool onlyEndPointsInCommon(const SkTSpan<OppCurve, TCurve>* opp, bool* start,
159 bool* oppStart, bool* ptsInCommon);
caryclark54359292015-03-26 07:52:43 -0700160
caryclark45fa4472015-01-16 07:04:10 -0800161 const TCurve& part() const {
162 return fPart;
163 }
164
caryclark54359292015-03-26 07:52:43 -0700165 bool removeAllBounded();
caryclark1049f122015-04-20 08:31:59 -0700166 bool removeBounded(const SkTSpan<OppCurve, TCurve>* opp);
caryclark54359292015-03-26 07:52:43 -0700167
caryclark45fa4472015-01-16 07:04:10 -0800168 void reset() {
halcanary96fcdcc2015-08-27 07:41:13 -0700169 fBounded = nullptr;
caryclark45fa4472015-01-16 07:04:10 -0800170 }
171
caryclark54359292015-03-26 07:52:43 -0700172 void resetBounds(const TCurve& curve) {
173 fIsLinear = fIsLine = false;
174 initBounds(curve);
caryclark45fa4472015-01-16 07:04:10 -0800175 }
176
caryclark54359292015-03-26 07:52:43 -0700177 bool split(SkTSpan* work, SkChunkAlloc* heap) {
178 return splitAt(work, (work->fStartT + work->fEndT) * 0.5, heap);
179 }
180
181 bool splitAt(SkTSpan* work, double t, SkChunkAlloc* heap);
caryclark45fa4472015-01-16 07:04:10 -0800182
183 double startT() const {
184 return fStartT;
185 }
186
caryclark54359292015-03-26 07:52:43 -0700187private:
caryclark45fa4472015-01-16 07:04:10 -0800188
189 // implementation is for testing only
caryclark54359292015-03-26 07:52:43 -0700190 int debugID() const {
191 return PATH_OPS_DEBUG_T_SECT_RELEASE(fID, -1);
caryclark45fa4472015-01-16 07:04:10 -0800192 }
193
caryclark54359292015-03-26 07:52:43 -0700194 void dumpID() const;
caryclark45fa4472015-01-16 07:04:10 -0800195
caryclark1049f122015-04-20 08:31:59 -0700196 int hullCheck(const SkTSpan<OppCurve, TCurve>* opp, bool* start, bool* oppStart);
197 int linearIntersects(const OppCurve& ) const;
198 SkTSpan<OppCurve, TCurve>* oppT(double t) const;
caryclark45fa4472015-01-16 07:04:10 -0800199
caryclark45fa4472015-01-16 07:04:10 -0800200 void validate() const;
caryclark54359292015-03-26 07:52:43 -0700201 void validateBounded() const;
202 void validatePerpT(double oppT) const;
203 void validatePerpPt(double t, const SkDPoint& ) const;
caryclark45fa4472015-01-16 07:04:10 -0800204
205 TCurve fPart;
caryclark1049f122015-04-20 08:31:59 -0700206 SkTCoincident<TCurve, OppCurve> fCoinStart;
207 SkTCoincident<TCurve, OppCurve> fCoinEnd;
208 SkTSpanBounded<OppCurve, TCurve>* fBounded;
caryclark45fa4472015-01-16 07:04:10 -0800209 SkTSpan* fPrev;
210 SkTSpan* fNext;
211 SkDRect fBounds;
212 double fStartT;
213 double fEndT;
214 double fBoundsMax;
caryclarked0935a2015-10-22 07:23:52 -0700215 SkOpDebugBool fCollapsed;
216 SkOpDebugBool fHasPerp;
217 SkOpDebugBool fIsLinear;
218 SkOpDebugBool fIsLine;
219 SkOpDebugBool fDeleted;
caryclark643ede62016-08-08 14:27:45 -0700220 SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState);
csmartdaltonceeaa782016-08-10 10:07:57 -0700221 SkDEBUGCODE(SkTSect<TCurve, OppCurve>* fDebugSect);
caryclark54359292015-03-26 07:52:43 -0700222 PATH_OPS_DEBUG_T_SECT_CODE(int fID);
caryclark1049f122015-04-20 08:31:59 -0700223 friend class SkTSect<TCurve, OppCurve>;
224 friend class SkTSect<OppCurve, TCurve>;
225 friend class SkTSpan<OppCurve, TCurve>;
caryclark45fa4472015-01-16 07:04:10 -0800226};
227
caryclark1049f122015-04-20 08:31:59 -0700228template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -0800229class SkTSect {
230public:
caryclarke25a4f62016-07-26 09:26:29 -0700231 SkTSect(const TCurve& c SkDEBUGPARAMS(SkOpGlobalState* ) PATH_OPS_DEBUG_T_SECT_PARAMS(int id));
caryclark1049f122015-04-20 08:31:59 -0700232 static void BinarySearch(SkTSect* sect1, SkTSect<OppCurve, TCurve>* sect2,
233 SkIntersections* intersections);
caryclark45fa4472015-01-16 07:04:10 -0800234
caryclarke25a4f62016-07-26 09:26:29 -0700235 SkDEBUGCODE(SkOpGlobalState* globalState() { return fDebugGlobalState; })
caryclark45fa4472015-01-16 07:04:10 -0800236 // for testing only
caryclark1049f122015-04-20 08:31:59 -0700237 bool debugHasBounded(const SkTSpan<OppCurve, TCurve>* ) const;
caryclark54359292015-03-26 07:52:43 -0700238
caryclark1049f122015-04-20 08:31:59 -0700239 const SkTSect<OppCurve, TCurve>* debugOpp() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700240 return SkDEBUGRELEASE(fOppSect, nullptr);
caryclark54359292015-03-26 07:52:43 -0700241 }
242
caryclark1049f122015-04-20 08:31:59 -0700243 const SkTSpan<TCurve, OppCurve>* debugSpan(int id) const;
244 const SkTSpan<TCurve, OppCurve>* debugT(double t) const;
caryclark45fa4472015-01-16 07:04:10 -0800245 void dump() const;
caryclark1049f122015-04-20 08:31:59 -0700246 void dumpBoth(SkTSect<OppCurve, TCurve>* ) const;
247 void dumpBounded(int id) const;
248 void dumpBounds() const;
caryclark54359292015-03-26 07:52:43 -0700249 void dumpCoin() const;
250 void dumpCoinCurves() const;
caryclark45fa4472015-01-16 07:04:10 -0800251 void dumpCurves() const;
252
253private:
254 enum {
255 kZeroS1Set = 1,
256 kOneS1Set = 2,
257 kZeroS2Set = 4,
258 kOneS2Set = 8
259 };
260
caryclark1049f122015-04-20 08:31:59 -0700261 SkTSpan<TCurve, OppCurve>* addFollowing(SkTSpan<TCurve, OppCurve>* prior);
262 void addForPerp(SkTSpan<OppCurve, TCurve>* span, double t);
263 SkTSpan<TCurve, OppCurve>* addOne();
caryclark55888e42016-07-18 10:01:36 -0700264
caryclark1049f122015-04-20 08:31:59 -0700265 SkTSpan<TCurve, OppCurve>* addSplitAt(SkTSpan<TCurve, OppCurve>* span, double t) {
266 SkTSpan<TCurve, OppCurve>* result = this->addOne();
caryclark643ede62016-08-08 14:27:45 -0700267 SkDEBUGCODE(result->debugSetGlobalState(this->globalState()));
caryclark54359292015-03-26 07:52:43 -0700268 result->splitAt(span, t, &fHeap);
269 result->initBounds(fCurve);
270 span->initBounds(fCurve);
271 return result;
272 }
273
caryclark1049f122015-04-20 08:31:59 -0700274 bool binarySearchCoin(SkTSect<OppCurve, TCurve>* , double tStart, double tStep, double* t,
275 double* oppT);
276 SkTSpan<TCurve, OppCurve>* boundsMax() const;
caryclarkef7cee42016-09-06 09:05:54 -0700277 bool coincidentCheck(SkTSect<OppCurve, TCurve>* sect2);
caryclark26ad22a2015-10-16 09:03:38 -0700278 void coincidentForce(SkTSect<OppCurve, TCurve>* sect2, double start1s, double start1e);
caryclark54359292015-03-26 07:52:43 -0700279 bool coincidentHasT(double t);
caryclark1049f122015-04-20 08:31:59 -0700280 int collapsed() const;
281 void computePerpendiculars(SkTSect<OppCurve, TCurve>* sect2, SkTSpan<TCurve, OppCurve>* first,
282 SkTSpan<TCurve, OppCurve>* last);
283 int countConsecutiveSpans(SkTSpan<TCurve, OppCurve>* first,
284 SkTSpan<TCurve, OppCurve>** last) const;
caryclarkccec0f92015-03-24 07:28:17 -0700285
caryclark54359292015-03-26 07:52:43 -0700286 int debugID() const {
287 return PATH_OPS_DEBUG_T_SECT_RELEASE(fID, -1);
288 }
289
caryclarkef7cee42016-09-06 09:05:54 -0700290 bool deleteEmptySpans();
caryclark1049f122015-04-20 08:31:59 -0700291 void dumpCommon(const SkTSpan<TCurve, OppCurve>* ) const;
292 void dumpCommonCurves(const SkTSpan<TCurve, OppCurve>* ) const;
293 static int EndsEqual(const SkTSect* sect1, const SkTSect<OppCurve, TCurve>* sect2,
294 SkIntersections* );
caryclarkef7cee42016-09-06 09:05:54 -0700295 bool extractCoincident(SkTSect<OppCurve, TCurve>* sect2, SkTSpan<TCurve, OppCurve>* first,
296 SkTSpan<TCurve, OppCurve>* last, SkTSpan<TCurve, OppCurve>** result);
caryclark1049f122015-04-20 08:31:59 -0700297 SkTSpan<TCurve, OppCurve>* findCoincidentRun(SkTSpan<TCurve, OppCurve>* first,
298 SkTSpan<TCurve, OppCurve>** lastPtr);
299 int intersects(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp,
300 SkTSpan<OppCurve, TCurve>* oppSpan, int* oppResult);
caryclarked0935a2015-10-22 07:23:52 -0700301 bool isParallel(const SkDLine& thisLine, const SkTSect<OppCurve, TCurve>* opp) const;
caryclark1049f122015-04-20 08:31:59 -0700302 int linesIntersect(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp,
303 SkTSpan<OppCurve, TCurve>* oppSpan, SkIntersections* );
caryclarkef7cee42016-09-06 09:05:54 -0700304 bool markSpanGone(SkTSpan<TCurve, OppCurve>* span);
caryclark1049f122015-04-20 08:31:59 -0700305 bool matchedDirection(double t, const SkTSect<OppCurve, TCurve>* sect2, double t2) const;
306 void matchedDirCheck(double t, const SkTSect<OppCurve, TCurve>* sect2, double t2,
caryclark54359292015-03-26 07:52:43 -0700307 bool* calcMatched, bool* oppMatched) const;
caryclark1049f122015-04-20 08:31:59 -0700308 void mergeCoincidence(SkTSect<OppCurve, TCurve>* sect2);
309 SkTSpan<TCurve, OppCurve>* prev(SkTSpan<TCurve, OppCurve>* ) const;
310 void removeByPerpendicular(SkTSect<OppCurve, TCurve>* opp);
caryclark54359292015-03-26 07:52:43 -0700311 void recoverCollapsed();
caryclark1049f122015-04-20 08:31:59 -0700312 void removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween);
313 void removeAllBut(const SkTSpan<OppCurve, TCurve>* keep, SkTSpan<TCurve, OppCurve>* span,
314 SkTSect<OppCurve, TCurve>* opp);
caryclarkef7cee42016-09-06 09:05:54 -0700315 bool removeSpan(SkTSpan<TCurve, OppCurve>* span);
caryclark1049f122015-04-20 08:31:59 -0700316 void removeSpanRange(SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last);
317 void removeSpans(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp);
318 SkTSpan<TCurve, OppCurve>* spanAtT(double t, SkTSpan<TCurve, OppCurve>** priorSpan);
319 SkTSpan<TCurve, OppCurve>* tail();
320 void trim(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp);
321 void unlinkSpan(SkTSpan<TCurve, OppCurve>* span);
322 bool updateBounded(SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last,
323 SkTSpan<OppCurve, TCurve>* oppFirst);
reed0dc4dd62015-03-24 13:55:33 -0700324 void validate() const;
caryclark54359292015-03-26 07:52:43 -0700325 void validateBounded() const;
326
caryclark45fa4472015-01-16 07:04:10 -0800327 const TCurve& fCurve;
328 SkChunkAlloc fHeap;
caryclark1049f122015-04-20 08:31:59 -0700329 SkTSpan<TCurve, OppCurve>* fHead;
330 SkTSpan<TCurve, OppCurve>* fCoincident;
331 SkTSpan<TCurve, OppCurve>* fDeleted;
caryclark45fa4472015-01-16 07:04:10 -0800332 int fActiveCount;
caryclark6c3b9cd2016-09-26 05:36:58 -0700333 bool fRemovedStartT;
334 bool fRemovedEndT;
caryclarke25a4f62016-07-26 09:26:29 -0700335 SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState);
csmartdaltonceeaa782016-08-10 10:07:57 -0700336 SkDEBUGCODE(SkTSect<OppCurve, TCurve>* fOppSect);
caryclark54359292015-03-26 07:52:43 -0700337 PATH_OPS_DEBUG_T_SECT_CODE(int fID);
338 PATH_OPS_DEBUG_T_SECT_CODE(int fDebugCount);
caryclark45fa4472015-01-16 07:04:10 -0800339#if DEBUG_T_SECT
caryclark45fa4472015-01-16 07:04:10 -0800340 int fDebugAllocatedCount;
341#endif
caryclark1049f122015-04-20 08:31:59 -0700342 friend class SkTSpan<TCurve, OppCurve>;
343 friend class SkTSpan<OppCurve, TCurve>;
344 friend class SkTSect<OppCurve, TCurve>;
caryclark45fa4472015-01-16 07:04:10 -0800345};
346
347#define COINCIDENT_SPAN_COUNT 9
348
caryclark1049f122015-04-20 08:31:59 -0700349template<typename TCurve, typename OppCurve>
350void SkTCoincident<TCurve, OppCurve>::setPerp(const TCurve& c1, double t,
351 const SkDPoint& cPt, const OppCurve& c2) {
caryclark45fa4472015-01-16 07:04:10 -0800352 SkDVector dxdy = c1.dxdyAtT(t);
353 SkDLine perp = {{ cPt, {cPt.fX + dxdy.fY, cPt.fY - dxdy.fX} }};
354 SkIntersections i;
355 int used = i.intersectRay(c2, perp);
356 // only keep closest
caryclark54359292015-03-26 07:52:43 -0700357 if (used == 0 || used == 3) {
caryclarkdf386c52015-04-21 05:27:02 -0700358 this->init();
caryclark45fa4472015-01-16 07:04:10 -0800359 return;
caryclark55888e42016-07-18 10:01:36 -0700360 }
caryclark45fa4472015-01-16 07:04:10 -0800361 fPerpT = i[0][0];
362 fPerpPt = i.pt(0);
363 SkASSERT(used <= 2);
364 if (used == 2) {
365 double distSq = (fPerpPt - cPt).lengthSquared();
366 double dist2Sq = (i.pt(1) - cPt).lengthSquared();
367 if (dist2Sq < distSq) {
368 fPerpT = i[0][1];
369 fPerpPt = i.pt(1);
370 }
371 }
caryclark54359292015-03-26 07:52:43 -0700372#if DEBUG_T_SECT
caryclark1049f122015-04-20 08:31:59 -0700373 SkDebugf("setPerp t=%1.9g cPt=(%1.9g,%1.9g) %s oppT=%1.9g fPerpPt=(%1.9g,%1.9g)\n",
374 t, cPt.fX, cPt.fY,
375 cPt.approximatelyEqual(fPerpPt) ? "==" : "!=", fPerpT, fPerpPt.fX, fPerpPt.fY);
caryclark54359292015-03-26 07:52:43 -0700376#endif
caryclark6c3b9cd2016-09-26 05:36:58 -0700377 fMatch = cPt.approximatelyEqual(fPerpPt);
caryclark45fa4472015-01-16 07:04:10 -0800378#if DEBUG_T_SECT
caryclark6c3b9cd2016-09-26 05:36:58 -0700379 if (fMatch) {
caryclark45fa4472015-01-16 07:04:10 -0800380 SkDebugf(""); // allow setting breakpoint
381 }
382#endif
383}
384
caryclark1049f122015-04-20 08:31:59 -0700385template<typename TCurve, typename OppCurve>
386void SkTSpan<TCurve, OppCurve>::addBounded(SkTSpan<OppCurve, TCurve>* span, SkChunkAlloc* heap) {
halcanary385fe4d2015-08-26 13:07:48 -0700387 SkTSpanBounded<OppCurve, TCurve>* bounded = new (heap->allocThrow(
388 sizeof(SkTSpanBounded<OppCurve, TCurve>)))(SkTSpanBounded<OppCurve, TCurve>);
caryclark54359292015-03-26 07:52:43 -0700389 bounded->fBounded = span;
390 bounded->fNext = fBounded;
391 fBounded = bounded;
392}
393
caryclark1049f122015-04-20 08:31:59 -0700394template<typename TCurve, typename OppCurve>
395SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::addFollowing(
396 SkTSpan<TCurve, OppCurve>* prior) {
397 SkTSpan<TCurve, OppCurve>* result = this->addOne();
caryclark643ede62016-08-08 14:27:45 -0700398 SkDEBUGCODE(result->debugSetGlobalState(this->globalState()));
caryclark54359292015-03-26 07:52:43 -0700399 result->fStartT = prior ? prior->fEndT : 0;
caryclark1049f122015-04-20 08:31:59 -0700400 SkTSpan<TCurve, OppCurve>* next = prior ? prior->fNext : fHead;
caryclark54359292015-03-26 07:52:43 -0700401 result->fEndT = next ? next->fStartT : 1;
402 result->fPrev = prior;
403 result->fNext = next;
404 if (prior) {
405 prior->fNext = result;
406 } else {
407 fHead = result;
408 }
409 if (next) {
410 next->fPrev = result;
411 }
412 result->resetBounds(fCurve);
caryclark643ede62016-08-08 14:27:45 -0700413 result->validate();
caryclark54359292015-03-26 07:52:43 -0700414 return result;
415}
416
caryclark1049f122015-04-20 08:31:59 -0700417template<typename TCurve, typename OppCurve>
418void SkTSect<TCurve, OppCurve>::addForPerp(SkTSpan<OppCurve, TCurve>* span, double t) {
caryclark54359292015-03-26 07:52:43 -0700419 if (!span->hasOppT(t)) {
caryclark1049f122015-04-20 08:31:59 -0700420 SkTSpan<TCurve, OppCurve>* priorSpan;
421 SkTSpan<TCurve, OppCurve>* opp = this->spanAtT(t, &priorSpan);
caryclark54359292015-03-26 07:52:43 -0700422 if (!opp) {
423 opp = this->addFollowing(priorSpan);
424#if DEBUG_PERP
caryclark26ad22a2015-10-16 09:03:38 -0700425 SkDebugf("%s priorSpan=%d t=%1.9g opp=%d\n", __FUNCTION__, priorSpan ?
426 priorSpan->debugID() : -1, t, opp->debugID());
caryclark54359292015-03-26 07:52:43 -0700427#endif
428 }
429#if DEBUG_PERP
430 opp->dump(); SkDebugf("\n");
caryclark26ad22a2015-10-16 09:03:38 -0700431 SkDebugf("%s addBounded span=%d opp=%d\n", __FUNCTION__, priorSpan ?
432 priorSpan->debugID() : -1, opp->debugID());
caryclark54359292015-03-26 07:52:43 -0700433#endif
434 opp->addBounded(span, &fHeap);
435 span->addBounded(opp, &fHeap);
436 }
437 this->validate();
caryclark1049f122015-04-20 08:31:59 -0700438#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700439 span->validatePerpT(t);
caryclark1049f122015-04-20 08:31:59 -0700440#endif
caryclark54359292015-03-26 07:52:43 -0700441}
442
caryclark1049f122015-04-20 08:31:59 -0700443template<typename TCurve, typename OppCurve>
444double SkTSpan<TCurve, OppCurve>::closestBoundedT(const SkDPoint& pt) const {
caryclark54359292015-03-26 07:52:43 -0700445 double result = -1;
caryclark343382e2016-06-29 08:18:38 -0700446 double closest = DBL_MAX;
caryclark1049f122015-04-20 08:31:59 -0700447 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700448 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -0700449 const SkTSpan<OppCurve, TCurve>* test = testBounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700450 double startDist = test->fPart[0].distanceSquared(pt);
451 if (closest > startDist) {
452 closest = startDist;
453 result = test->fStartT;
454 }
caryclark1049f122015-04-20 08:31:59 -0700455 double endDist = test->fPart[OppCurve::kPointLast].distanceSquared(pt);
caryclark54359292015-03-26 07:52:43 -0700456 if (closest > endDist) {
457 closest = endDist;
458 result = test->fEndT;
459 }
460 testBounded = testBounded->fNext;
461 }
462 SkASSERT(between(0, result, 1));
463 return result;
464}
465
466#ifdef SK_DEBUG
caryclark1049f122015-04-20 08:31:59 -0700467template<typename TCurve, typename OppCurve>
468bool SkTSpan<TCurve, OppCurve>::debugIsBefore(const SkTSpan* span) const {
caryclark54359292015-03-26 07:52:43 -0700469 const SkTSpan* work = this;
470 do {
471 if (span == work) {
472 return true;
473 }
474 } while ((work = work->fNext));
475 return false;
476}
477#endif
478
caryclark1049f122015-04-20 08:31:59 -0700479template<typename TCurve, typename OppCurve>
480bool SkTSpan<TCurve, OppCurve>::contains(double t) const {
caryclark54359292015-03-26 07:52:43 -0700481 const SkTSpan* work = this;
482 do {
483 if (between(work->fStartT, t, work->fEndT)) {
484 return true;
485 }
486 } while ((work = work->fNext));
487 return false;
488}
489
caryclark1049f122015-04-20 08:31:59 -0700490template<typename TCurve, typename OppCurve>
491const SkTSect<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::debugOpp() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700492 return SkDEBUGRELEASE(fDebugSect->debugOpp(), nullptr);
caryclark54359292015-03-26 07:52:43 -0700493}
494
caryclark1049f122015-04-20 08:31:59 -0700495template<typename TCurve, typename OppCurve>
496SkTSpan<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::findOppSpan(
497 const SkTSpan<OppCurve, TCurve>* opp) const {
498 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700499 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700500 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700501 if (opp == test) {
502 return test;
503 }
504 bounded = bounded->fNext;
505 }
halcanary96fcdcc2015-08-27 07:41:13 -0700506 return nullptr;
caryclark54359292015-03-26 07:52:43 -0700507}
508
509// returns 0 if no hull intersection
510// 1 if hulls intersect
511// 2 if hulls only share a common endpoint
512// -1 if linear and further checking is required
caryclark1049f122015-04-20 08:31:59 -0700513template<typename TCurve, typename OppCurve>
514int SkTSpan<TCurve, OppCurve>::hullCheck(const SkTSpan<OppCurve, TCurve>* opp,
515 bool* start, bool* oppStart) {
caryclark54359292015-03-26 07:52:43 -0700516 if (fIsLinear) {
517 return -1;
518 }
519 bool ptsInCommon;
520 if (onlyEndPointsInCommon(opp, start, oppStart, &ptsInCommon)) {
521 SkASSERT(ptsInCommon);
522 return 2;
523 }
524 bool linear;
525 if (fPart.hullIntersects(opp->fPart, &linear)) {
526 if (!linear) { // check set true if linear
527 return 1;
528 }
529 fIsLinear = true;
530 fIsLine = fPart.controlsInside();
caryclark2bec26a2016-05-26 09:01:47 -0700531 return ptsInCommon ? 1 : -1;
caryclark54359292015-03-26 07:52:43 -0700532 } else { // hull is not linear; check set true if intersected at the end points
533 return ((int) ptsInCommon) << 1; // 0 or 2
534 }
535 return 0;
536}
537
538// OPTIMIZE ? If at_most_end_pts_in_common detects that one quad is near linear,
539// use line intersection to guess a better split than 0.5
540// OPTIMIZE Once at_most_end_pts_in_common detects linear, mark span so all future splits are linear
caryclark1049f122015-04-20 08:31:59 -0700541template<typename TCurve, typename OppCurve>
542int SkTSpan<TCurve, OppCurve>::hullsIntersect(SkTSpan<OppCurve, TCurve>* opp,
543 bool* start, bool* oppStart) {
caryclark54359292015-03-26 07:52:43 -0700544 if (!fBounds.intersects(opp->fBounds)) {
545 return 0;
546 }
547 int hullSect = this->hullCheck(opp, start, oppStart);
548 if (hullSect >= 0) {
549 return hullSect;
550 }
551 hullSect = opp->hullCheck(this, oppStart, start);
552 if (hullSect >= 0) {
553 return hullSect;
554 }
555 return -1;
556}
557
caryclark1049f122015-04-20 08:31:59 -0700558template<typename TCurve, typename OppCurve>
559void SkTSpan<TCurve, OppCurve>::init(const TCurve& c) {
halcanary96fcdcc2015-08-27 07:41:13 -0700560 fPrev = fNext = nullptr;
reed0dc4dd62015-03-24 13:55:33 -0700561 fStartT = 0;
562 fEndT = 1;
halcanary96fcdcc2015-08-27 07:41:13 -0700563 fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -0700564 resetBounds(c);
caryclark45fa4472015-01-16 07:04:10 -0800565}
566
caryclark1049f122015-04-20 08:31:59 -0700567template<typename TCurve, typename OppCurve>
568void SkTSpan<TCurve, OppCurve>::initBounds(const TCurve& c) {
reed0dc4dd62015-03-24 13:55:33 -0700569 fPart = c.subDivide(fStartT, fEndT);
570 fBounds.setBounds(fPart);
571 fCoinStart.init();
572 fCoinEnd.init();
573 fBoundsMax = SkTMax(fBounds.width(), fBounds.height());
574 fCollapsed = fPart.collapsed();
575 fHasPerp = false;
caryclark54359292015-03-26 07:52:43 -0700576 fDeleted = false;
reed0dc4dd62015-03-24 13:55:33 -0700577#if DEBUG_T_SECT
reed0dc4dd62015-03-24 13:55:33 -0700578 if (fCollapsed) {
579 SkDebugf(""); // for convenient breakpoints
caryclark45fa4472015-01-16 07:04:10 -0800580 }
581#endif
582}
583
caryclark1049f122015-04-20 08:31:59 -0700584template<typename TCurve, typename OppCurve>
585bool SkTSpan<TCurve, OppCurve>::linearsIntersect(SkTSpan<OppCurve, TCurve>* span) {
caryclark54359292015-03-26 07:52:43 -0700586 int result = this->linearIntersects(span->fPart);
587 if (result <= 1) {
588 return SkToBool(result);
caryclark45fa4472015-01-16 07:04:10 -0800589 }
caryclark54359292015-03-26 07:52:43 -0700590 SkASSERT(span->fIsLinear);
591 result = span->linearIntersects(this->fPart);
592// SkASSERT(result <= 1);
593 return SkToBool(result);
caryclark45fa4472015-01-16 07:04:10 -0800594}
595
caryclark1049f122015-04-20 08:31:59 -0700596template<typename TCurve, typename OppCurve>
597double SkTSpan<TCurve, OppCurve>::linearT(const SkDPoint& pt) const {
caryclark54359292015-03-26 07:52:43 -0700598 SkDVector len = fPart[TCurve::kPointLast] - fPart[0];
599 return fabs(len.fX) > fabs(len.fY)
600 ? (pt.fX - fPart[0].fX) / len.fX
601 : (pt.fY - fPart[0].fY) / len.fY;
caryclark45fa4472015-01-16 07:04:10 -0800602}
603
caryclark1049f122015-04-20 08:31:59 -0700604template<typename TCurve, typename OppCurve>
605int SkTSpan<TCurve, OppCurve>::linearIntersects(const OppCurve& q2) const {
caryclark45fa4472015-01-16 07:04:10 -0800606 // looks like q1 is near-linear
caryclark54359292015-03-26 07:52:43 -0700607 int start = 0, end = TCurve::kPointLast; // the outside points are usually the extremes
caryclark45fa4472015-01-16 07:04:10 -0800608 if (!fPart.controlsInside()) {
609 double dist = 0; // if there's any question, compute distance to find best outsiders
610 for (int outer = 0; outer < TCurve::kPointCount - 1; ++outer) {
611 for (int inner = outer + 1; inner < TCurve::kPointCount; ++inner) {
612 double test = (fPart[outer] - fPart[inner]).lengthSquared();
613 if (dist > test) {
614 continue;
615 }
616 dist = test;
617 start = outer;
618 end = inner;
619 }
620 }
621 }
622 // see if q2 is on one side of the line formed by the extreme points
623 double origX = fPart[start].fX;
624 double origY = fPart[start].fY;
625 double adj = fPart[end].fX - origX;
626 double opp = fPart[end].fY - origY;
caryclark54359292015-03-26 07:52:43 -0700627 double maxPart = SkTMax(fabs(adj), fabs(opp));
628 double sign = 0; // initialization to shut up warning in release build
caryclark1049f122015-04-20 08:31:59 -0700629 for (int n = 0; n < OppCurve::kPointCount; ++n) {
caryclark54359292015-03-26 07:52:43 -0700630 double dx = q2[n].fY - origY;
631 double dy = q2[n].fX - origX;
632 double maxVal = SkTMax(maxPart, SkTMax(fabs(dx), fabs(dy)));
caryclark45fa4472015-01-16 07:04:10 -0800633 double test = (q2[n].fY - origY) * adj - (q2[n].fX - origX) * opp;
caryclark54359292015-03-26 07:52:43 -0700634 if (precisely_zero_when_compared_to(test, maxVal)) {
635 return 1;
636 }
637 if (approximately_zero_when_compared_to(test, maxVal)) {
638 return 3;
caryclark45fa4472015-01-16 07:04:10 -0800639 }
640 if (n == 0) {
641 sign = test;
642 continue;
643 }
644 if (test * sign < 0) {
caryclark54359292015-03-26 07:52:43 -0700645 return 1;
caryclark45fa4472015-01-16 07:04:10 -0800646 }
647 }
caryclark54359292015-03-26 07:52:43 -0700648 return 0;
649}
650
caryclark1049f122015-04-20 08:31:59 -0700651template<typename TCurve, typename OppCurve>
652bool SkTSpan<TCurve, OppCurve>::onlyEndPointsInCommon(const SkTSpan<OppCurve, TCurve>* opp,
653 bool* start, bool* oppStart, bool* ptsInCommon) {
caryclark54359292015-03-26 07:52:43 -0700654 if (opp->fPart[0] == fPart[0]) {
655 *start = *oppStart = true;
656 } else if (opp->fPart[0] == fPart[TCurve::kPointLast]) {
657 *start = false;
658 *oppStart = true;
caryclark1049f122015-04-20 08:31:59 -0700659 } else if (opp->fPart[OppCurve::kPointLast] == fPart[0]) {
caryclark54359292015-03-26 07:52:43 -0700660 *start = true;
661 *oppStart = false;
caryclark1049f122015-04-20 08:31:59 -0700662 } else if (opp->fPart[OppCurve::kPointLast] == fPart[TCurve::kPointLast]) {
caryclark54359292015-03-26 07:52:43 -0700663 *start = *oppStart = false;
664 } else {
665 *ptsInCommon = false;
666 return false;
667 }
668 *ptsInCommon = true;
caryclark1049f122015-04-20 08:31:59 -0700669 const SkDPoint* otherPts[TCurve::kPointCount - 1], * oppOtherPts[OppCurve::kPointCount - 1];
caryclark54359292015-03-26 07:52:43 -0700670 int baseIndex = *start ? 0 : TCurve::kPointLast;
caryclark1049f122015-04-20 08:31:59 -0700671 fPart.otherPts(baseIndex, otherPts);
672 opp->fPart.otherPts(*oppStart ? 0 : OppCurve::kPointLast, oppOtherPts);
caryclark54359292015-03-26 07:52:43 -0700673 const SkDPoint& base = fPart[baseIndex];
caryclark1049f122015-04-20 08:31:59 -0700674 for (int o1 = 0; o1 < (int) SK_ARRAY_COUNT(otherPts); ++o1) {
675 SkDVector v1 = *otherPts[o1] - base;
676 for (int o2 = 0; o2 < (int) SK_ARRAY_COUNT(oppOtherPts); ++o2) {
677 SkDVector v2 = *oppOtherPts[o2] - base;
caryclark54359292015-03-26 07:52:43 -0700678 if (v2.dot(v1) >= 0) {
679 return false;
680 }
681 }
682 }
683 return true;
684}
685
caryclark1049f122015-04-20 08:31:59 -0700686template<typename TCurve, typename OppCurve>
687SkTSpan<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::oppT(double t) const {
688 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700689 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700690 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700691 if (between(test->fStartT, t, test->fEndT)) {
692 return test;
693 }
694 bounded = bounded->fNext;
695 }
halcanary96fcdcc2015-08-27 07:41:13 -0700696 return nullptr;
caryclark54359292015-03-26 07:52:43 -0700697}
698
caryclark1049f122015-04-20 08:31:59 -0700699template<typename TCurve, typename OppCurve>
700bool SkTSpan<TCurve, OppCurve>::removeAllBounded() {
caryclark54359292015-03-26 07:52:43 -0700701 bool deleteSpan = false;
caryclark1049f122015-04-20 08:31:59 -0700702 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700703 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700704 SkTSpan<OppCurve, TCurve>* opp = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700705 deleteSpan |= opp->removeBounded(this);
706 bounded = bounded->fNext;
707 }
708 return deleteSpan;
709}
710
caryclark1049f122015-04-20 08:31:59 -0700711template<typename TCurve, typename OppCurve>
712bool SkTSpan<TCurve, OppCurve>::removeBounded(const SkTSpan<OppCurve, TCurve>* opp) {
caryclark54359292015-03-26 07:52:43 -0700713 if (fHasPerp) {
714 bool foundStart = false;
715 bool foundEnd = false;
caryclark1049f122015-04-20 08:31:59 -0700716 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700717 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700718 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700719 if (opp != test) {
720 foundStart |= between(test->fStartT, fCoinStart.perpT(), test->fEndT);
721 foundEnd |= between(test->fStartT, fCoinEnd.perpT(), test->fEndT);
722 }
723 bounded = bounded->fNext;
724 }
725 if (!foundStart || !foundEnd) {
726 fHasPerp = false;
727 fCoinStart.init();
728 fCoinEnd.init();
729 }
730 }
caryclark1049f122015-04-20 08:31:59 -0700731 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
halcanary96fcdcc2015-08-27 07:41:13 -0700732 SkTSpanBounded<OppCurve, TCurve>* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -0700733 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700734 SkTSpanBounded<OppCurve, TCurve>* boundedNext = bounded->fNext;
caryclark54359292015-03-26 07:52:43 -0700735 if (opp == bounded->fBounded) {
736 if (prev) {
737 prev->fNext = boundedNext;
738 return false;
739 } else {
740 fBounded = boundedNext;
halcanary96fcdcc2015-08-27 07:41:13 -0700741 return fBounded == nullptr;
caryclark54359292015-03-26 07:52:43 -0700742 }
743 }
744 prev = bounded;
745 bounded = boundedNext;
746 }
caryclark643ede62016-08-08 14:27:45 -0700747 SkOPASSERT(0);
caryclark45fa4472015-01-16 07:04:10 -0800748 return false;
749}
750
caryclark1049f122015-04-20 08:31:59 -0700751template<typename TCurve, typename OppCurve>
752bool SkTSpan<TCurve, OppCurve>::splitAt(SkTSpan* work, double t, SkChunkAlloc* heap) {
caryclark45fa4472015-01-16 07:04:10 -0800753 fStartT = t;
754 fEndT = work->fEndT;
755 if (fStartT == fEndT) {
756 fCollapsed = true;
757 return false;
758 }
759 work->fEndT = t;
760 if (work->fStartT == work->fEndT) {
761 work->fCollapsed = true;
762 return false;
763 }
764 fPrev = work;
765 fNext = work->fNext;
766 fIsLinear = work->fIsLinear;
caryclark54359292015-03-26 07:52:43 -0700767 fIsLine = work->fIsLine;
768
caryclark45fa4472015-01-16 07:04:10 -0800769 work->fNext = this;
770 if (fNext) {
771 fNext->fPrev = this;
772 }
caryclark643ede62016-08-08 14:27:45 -0700773 this->validate();
caryclark1049f122015-04-20 08:31:59 -0700774 SkTSpanBounded<OppCurve, TCurve>* bounded = work->fBounded;
halcanary96fcdcc2015-08-27 07:41:13 -0700775 fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -0700776 while (bounded) {
777 this->addBounded(bounded->fBounded, heap);
778 bounded = bounded->fNext;
779 }
780 bounded = fBounded;
781 while (bounded) {
782 bounded->fBounded->addBounded(this, heap);
783 bounded = bounded->fNext;
caryclark45fa4472015-01-16 07:04:10 -0800784 }
785 return true;
786}
787
caryclark1049f122015-04-20 08:31:59 -0700788template<typename TCurve, typename OppCurve>
789void SkTSpan<TCurve, OppCurve>::validate() const {
caryclark643ede62016-08-08 14:27:45 -0700790#if DEBUG_VALIDATE
791 SkASSERT(this != fPrev);
792 SkASSERT(this != fNext);
halcanary96fcdcc2015-08-27 07:41:13 -0700793 SkASSERT(fNext == nullptr || fNext != fPrev);
794 SkASSERT(fNext == nullptr || this == fNext->fPrev);
795 SkASSERT(fPrev == nullptr || this == fPrev->fNext);
caryclark643ede62016-08-08 14:27:45 -0700796 this->validateBounded();
797#endif
798#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700799 SkASSERT(fBounds.width() || fBounds.height() || fCollapsed);
caryclarke839e782016-09-15 07:48:18 -0700800 SkASSERT(fBoundsMax == SkTMax(fBounds.width(), fBounds.height()) || fCollapsed == 0xFF);
caryclark45fa4472015-01-16 07:04:10 -0800801 SkASSERT(0 <= fStartT);
802 SkASSERT(fEndT <= 1);
caryclark54359292015-03-26 07:52:43 -0700803 SkASSERT(fStartT <= fEndT);
caryclarke839e782016-09-15 07:48:18 -0700804 SkASSERT(fBounded || fCollapsed == 0xFF);
caryclark54359292015-03-26 07:52:43 -0700805 if (fHasPerp) {
caryclark6c3b9cd2016-09-26 05:36:58 -0700806 if (fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700807 validatePerpT(fCoinStart.perpT());
808 validatePerpPt(fCoinStart.perpT(), fCoinStart.perpPt());
809 }
caryclark6c3b9cd2016-09-26 05:36:58 -0700810 if (fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700811 validatePerpT(fCoinEnd.perpT());
812 validatePerpPt(fCoinEnd.perpT(), fCoinEnd.perpPt());
813 }
caryclarkccec0f92015-03-24 07:28:17 -0700814 }
reed0dc4dd62015-03-24 13:55:33 -0700815#endif
caryclark54359292015-03-26 07:52:43 -0700816}
caryclarkccec0f92015-03-24 07:28:17 -0700817
caryclark1049f122015-04-20 08:31:59 -0700818template<typename TCurve, typename OppCurve>
819void SkTSpan<TCurve, OppCurve>::validateBounded() const {
caryclark54359292015-03-26 07:52:43 -0700820#if DEBUG_VALIDATE
caryclark1049f122015-04-20 08:31:59 -0700821 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700822 while (testBounded) {
csmartdaltonceeaa782016-08-10 10:07:57 -0700823 SkDEBUGCODE(const SkTSpan<OppCurve, TCurve>* overlap = testBounded->fBounded);
caryclark54359292015-03-26 07:52:43 -0700824 SkASSERT(!overlap->fDeleted);
caryclark643ede62016-08-08 14:27:45 -0700825#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700826 SkASSERT(((this->debugID() ^ overlap->debugID()) & 1) == 1);
827 SkASSERT(overlap->findOppSpan(this));
caryclark643ede62016-08-08 14:27:45 -0700828#endif
caryclark54359292015-03-26 07:52:43 -0700829 testBounded = testBounded->fNext;
830 }
831#endif
832}
833
caryclark1049f122015-04-20 08:31:59 -0700834template<typename TCurve, typename OppCurve>
835void SkTSpan<TCurve, OppCurve>::validatePerpT(double oppT) const {
836 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700837 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -0700838 const SkTSpan<OppCurve, TCurve>* overlap = testBounded->fBounded;
caryclark26ad22a2015-10-16 09:03:38 -0700839 if (precisely_between(overlap->fStartT, oppT, overlap->fEndT)) {
caryclark54359292015-03-26 07:52:43 -0700840 return;
841 }
842 testBounded = testBounded->fNext;
843 }
844 SkASSERT(0);
caryclark54359292015-03-26 07:52:43 -0700845}
846
caryclark1049f122015-04-20 08:31:59 -0700847template<typename TCurve, typename OppCurve>
848void SkTSpan<TCurve, OppCurve>::validatePerpPt(double t, const SkDPoint& pt) const {
849 SkASSERT(fDebugSect->fOppSect->fCurve.ptAtT(t) == pt);
caryclark54359292015-03-26 07:52:43 -0700850}
851
852
caryclark1049f122015-04-20 08:31:59 -0700853template<typename TCurve, typename OppCurve>
caryclarke25a4f62016-07-26 09:26:29 -0700854SkTSect<TCurve, OppCurve>::SkTSect(const TCurve& c
855 SkDEBUGPARAMS(SkOpGlobalState* debugGlobalState)
856 PATH_OPS_DEBUG_T_SECT_PARAMS(int id))
caryclark45fa4472015-01-16 07:04:10 -0800857 : fCurve(c)
caryclark1049f122015-04-20 08:31:59 -0700858 , fHeap(sizeof(SkTSpan<TCurve, OppCurve>) * 4)
halcanary96fcdcc2015-08-27 07:41:13 -0700859 , fCoincident(nullptr)
860 , fDeleted(nullptr)
caryclark45fa4472015-01-16 07:04:10 -0800861 , fActiveCount(0)
caryclarke25a4f62016-07-26 09:26:29 -0700862 SkDEBUGPARAMS(fDebugGlobalState(debugGlobalState))
caryclark54359292015-03-26 07:52:43 -0700863 PATH_OPS_DEBUG_T_SECT_PARAMS(fID(id))
864 PATH_OPS_DEBUG_T_SECT_PARAMS(fDebugCount(0))
865 PATH_OPS_DEBUG_T_SECT_PARAMS(fDebugAllocatedCount(0))
caryclark45fa4472015-01-16 07:04:10 -0800866{
867 fHead = addOne();
caryclark643ede62016-08-08 14:27:45 -0700868 SkDEBUGCODE(fHead->debugSetGlobalState(debugGlobalState));
caryclark45fa4472015-01-16 07:04:10 -0800869 fHead->init(c);
870}
871
caryclark1049f122015-04-20 08:31:59 -0700872template<typename TCurve, typename OppCurve>
873SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::addOne() {
874 SkTSpan<TCurve, OppCurve>* result;
caryclark45fa4472015-01-16 07:04:10 -0800875 if (fDeleted) {
876 result = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -0800877 fDeleted = result->fNext;
878 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700879 result = new (fHeap.allocThrow(sizeof(SkTSpan<TCurve, OppCurve>)))(
880 SkTSpan<TCurve, OppCurve>);
caryclark45fa4472015-01-16 07:04:10 -0800881#if DEBUG_T_SECT
882 ++fDebugAllocatedCount;
883#endif
884 }
caryclarked0935a2015-10-22 07:23:52 -0700885 result->reset();
caryclark08b32492015-04-06 11:41:29 -0700886 result->fHasPerp = false;
887 result->fDeleted = false;
caryclark55888e42016-07-18 10:01:36 -0700888 ++fActiveCount;
caryclark54359292015-03-26 07:52:43 -0700889 PATH_OPS_DEBUG_T_SECT_CODE(result->fID = fDebugCount++ * 2 + fID);
caryclark1049f122015-04-20 08:31:59 -0700890 SkDEBUGCODE(result->fDebugSect = this);
caryclarked0935a2015-10-22 07:23:52 -0700891#ifdef SK_DEBUG
892 result->fPart.debugInit();
893 result->fCoinStart.debugInit();
894 result->fCoinEnd.debugInit();
895 result->fPrev = result->fNext = nullptr;
896 result->fBounds.debugInit();
897 result->fStartT = result->fEndT = result->fBoundsMax = SK_ScalarNaN;
898 result->fCollapsed = result->fIsLinear = result->fIsLine = 0xFF;
899#endif
caryclark45fa4472015-01-16 07:04:10 -0800900 return result;
901}
902
caryclark1049f122015-04-20 08:31:59 -0700903template<typename TCurve, typename OppCurve>
904bool SkTSect<TCurve, OppCurve>::binarySearchCoin(SkTSect<OppCurve, TCurve>* sect2, double tStart,
905 double tStep, double* resultT, double* oppT) {
906 SkTSpan<TCurve, OppCurve> work;
caryclark45fa4472015-01-16 07:04:10 -0800907 double result = work.fStartT = work.fEndT = tStart;
caryclark1049f122015-04-20 08:31:59 -0700908 SkDEBUGCODE(work.fDebugSect = this);
caryclark45fa4472015-01-16 07:04:10 -0800909 SkDPoint last = fCurve.ptAtT(tStart);
910 SkDPoint oppPt;
911 bool flip = false;
caryclarkcdeff812016-07-22 03:34:19 -0700912 bool contained = false;
caryclark45fa4472015-01-16 07:04:10 -0800913 SkDEBUGCODE(bool down = tStep < 0);
caryclark1049f122015-04-20 08:31:59 -0700914 const OppCurve& opp = sect2->fCurve;
caryclark45fa4472015-01-16 07:04:10 -0800915 do {
916 tStep *= 0.5;
917 work.fStartT += tStep;
918 if (flip) {
919 tStep = -tStep;
920 flip = false;
921 }
922 work.initBounds(fCurve);
923 if (work.fCollapsed) {
924 return false;
925 }
926 if (last.approximatelyEqual(work.fPart[0])) {
927 break;
928 }
929 last = work.fPart[0];
930 work.fCoinStart.setPerp(fCurve, work.fStartT, last, opp);
caryclark6c3b9cd2016-09-26 05:36:58 -0700931 if (work.fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700932#if DEBUG_T_SECT
933 work.validatePerpPt(work.fCoinStart.perpT(), work.fCoinStart.perpPt());
934#endif
caryclark45fa4472015-01-16 07:04:10 -0800935 double oppTTest = work.fCoinStart.perpT();
caryclark54359292015-03-26 07:52:43 -0700936 if (sect2->fHead->contains(oppTTest)) {
caryclark45fa4472015-01-16 07:04:10 -0800937 *oppT = oppTTest;
938 oppPt = work.fCoinStart.perpPt();
caryclarkcdeff812016-07-22 03:34:19 -0700939 contained = true;
caryclark45fa4472015-01-16 07:04:10 -0800940 SkASSERT(down ? result > work.fStartT : result < work.fStartT);
941 result = work.fStartT;
942 continue;
943 }
944 }
945 tStep = -tStep;
946 flip = true;
947 } while (true);
caryclarkcdeff812016-07-22 03:34:19 -0700948 if (!contained) {
949 return false;
950 }
caryclark45fa4472015-01-16 07:04:10 -0800951 if (last.approximatelyEqual(fCurve[0])) {
952 result = 0;
953 } else if (last.approximatelyEqual(fCurve[TCurve::kPointLast])) {
954 result = 1;
955 }
956 if (oppPt.approximatelyEqual(opp[0])) {
957 *oppT = 0;
caryclark1049f122015-04-20 08:31:59 -0700958 } else if (oppPt.approximatelyEqual(opp[OppCurve::kPointLast])) {
caryclark45fa4472015-01-16 07:04:10 -0800959 *oppT = 1;
960 }
961 *resultT = result;
962 return true;
963}
964
965// OPTIMIZE ? keep a sorted list of sizes in the form of a doubly-linked list in quad span
966// so that each quad sect has a pointer to the largest, and can update it as spans
967// are split
caryclark1049f122015-04-20 08:31:59 -0700968template<typename TCurve, typename OppCurve>
969SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::boundsMax() const {
970 SkTSpan<TCurve, OppCurve>* test = fHead;
971 SkTSpan<TCurve, OppCurve>* largest = fHead;
caryclark54359292015-03-26 07:52:43 -0700972 bool lCollapsed = largest->fCollapsed;
caryclark45fa4472015-01-16 07:04:10 -0800973 while ((test = test->fNext)) {
caryclark54359292015-03-26 07:52:43 -0700974 bool tCollapsed = test->fCollapsed;
975 if ((lCollapsed && !tCollapsed) || (lCollapsed == tCollapsed &&
976 largest->fBoundsMax < test->fBoundsMax)) {
caryclark45fa4472015-01-16 07:04:10 -0800977 largest = test;
caryclark1049f122015-04-20 08:31:59 -0700978 lCollapsed = test->fCollapsed;
caryclark45fa4472015-01-16 07:04:10 -0800979 }
980 }
caryclark54359292015-03-26 07:52:43 -0700981 return largest;
caryclark45fa4472015-01-16 07:04:10 -0800982}
983
caryclark1049f122015-04-20 08:31:59 -0700984template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -0700985bool SkTSect<TCurve, OppCurve>::coincidentCheck(SkTSect<OppCurve, TCurve>* sect2) {
caryclark1049f122015-04-20 08:31:59 -0700986 SkTSpan<TCurve, OppCurve>* first = fHead;
987 SkTSpan<TCurve, OppCurve>* last, * next;
caryclark45fa4472015-01-16 07:04:10 -0800988 do {
caryclark54359292015-03-26 07:52:43 -0700989 int consecutive = this->countConsecutiveSpans(first, &last);
990 next = last->fNext;
caryclark45fa4472015-01-16 07:04:10 -0800991 if (consecutive < COINCIDENT_SPAN_COUNT) {
992 continue;
993 }
caryclark54359292015-03-26 07:52:43 -0700994 this->validate();
995 sect2->validate();
996 this->computePerpendiculars(sect2, first, last);
997 this->validate();
998 sect2->validate();
caryclark45fa4472015-01-16 07:04:10 -0800999 // check to see if a range of points are on the curve
caryclark1049f122015-04-20 08:31:59 -07001000 SkTSpan<TCurve, OppCurve>* coinStart = first;
caryclark54359292015-03-26 07:52:43 -07001001 do {
caryclarkef7cee42016-09-06 09:05:54 -07001002 bool success = this->extractCoincident(sect2, coinStart, last, &coinStart);
1003 if (!success) {
1004 return false;
1005 }
caryclark54359292015-03-26 07:52:43 -07001006 } while (coinStart && !last->fDeleted);
caryclark55888e42016-07-18 10:01:36 -07001007 if (!fHead || !sect2->fHead) {
1008 break;
1009 }
caryclark643ede62016-08-08 14:27:45 -07001010 if (!next || next->fDeleted) {
1011 break;
1012 }
caryclark45fa4472015-01-16 07:04:10 -08001013 } while ((first = next));
caryclarkef7cee42016-09-06 09:05:54 -07001014 return true;
caryclark45fa4472015-01-16 07:04:10 -08001015}
1016
caryclark1049f122015-04-20 08:31:59 -07001017template<typename TCurve, typename OppCurve>
caryclark26ad22a2015-10-16 09:03:38 -07001018void SkTSect<TCurve, OppCurve>::coincidentForce(SkTSect<OppCurve, TCurve>* sect2,
1019 double start1s, double start1e) {
1020 SkTSpan<TCurve, OppCurve>* first = fHead;
1021 SkTSpan<TCurve, OppCurve>* last = this->tail();
1022 SkTSpan<OppCurve, TCurve>* oppFirst = sect2->fHead;
1023 SkTSpan<OppCurve, TCurve>* oppLast = sect2->tail();
1024 bool deleteEmptySpans = this->updateBounded(first, last, oppFirst);
1025 deleteEmptySpans |= sect2->updateBounded(oppFirst, oppLast, first);
1026 this->removeSpanRange(first, last);
1027 sect2->removeSpanRange(oppFirst, oppLast);
1028 first->fStartT = start1s;
1029 first->fEndT = start1e;
1030 first->resetBounds(fCurve);
1031 first->fCoinStart.setPerp(fCurve, start1s, fCurve[0], sect2->fCurve);
1032 first->fCoinEnd.setPerp(fCurve, start1e, fCurve[TCurve::kPointLast], sect2->fCurve);
1033 bool oppMatched = first->fCoinStart.perpT() < first->fCoinEnd.perpT();
caryclarkef784fb2015-10-30 12:03:06 -07001034 double oppStartT = first->fCoinStart.perpT() == -1 ? 0 : SkTMax(0., first->fCoinStart.perpT());
1035 double oppEndT = first->fCoinEnd.perpT() == -1 ? 1 : SkTMin(1., first->fCoinEnd.perpT());
caryclark26ad22a2015-10-16 09:03:38 -07001036 if (!oppMatched) {
1037 SkTSwap(oppStartT, oppEndT);
1038 }
1039 oppFirst->fStartT = oppStartT;
1040 oppFirst->fEndT = oppEndT;
1041 oppFirst->resetBounds(sect2->fCurve);
1042 this->removeCoincident(first, false);
1043 sect2->removeCoincident(oppFirst, true);
1044 if (deleteEmptySpans) {
1045 this->deleteEmptySpans();
1046 sect2->deleteEmptySpans();
1047 }
1048}
1049
1050template<typename TCurve, typename OppCurve>
caryclark1049f122015-04-20 08:31:59 -07001051bool SkTSect<TCurve, OppCurve>::coincidentHasT(double t) {
1052 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001053 while (test) {
1054 if (between(test->fStartT, t, test->fEndT)) {
1055 return true;
1056 }
1057 test = test->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001058 }
caryclark54359292015-03-26 07:52:43 -07001059 return false;
caryclark45fa4472015-01-16 07:04:10 -08001060}
1061
caryclark1049f122015-04-20 08:31:59 -07001062template<typename TCurve, typename OppCurve>
1063int SkTSect<TCurve, OppCurve>::collapsed() const {
1064 int result = 0;
1065 const SkTSpan<TCurve, OppCurve>* test = fHead;
1066 while (test) {
1067 if (test->fCollapsed) {
1068 ++result;
1069 }
1070 test = test->next();
1071 }
1072 return result;
1073}
1074
1075template<typename TCurve, typename OppCurve>
1076void SkTSect<TCurve, OppCurve>::computePerpendiculars(SkTSect<OppCurve, TCurve>* sect2,
1077 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last) {
1078 const OppCurve& opp = sect2->fCurve;
1079 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001080 SkTSpan<TCurve, OppCurve>* prior = nullptr;
caryclark45fa4472015-01-16 07:04:10 -08001081 do {
caryclark54359292015-03-26 07:52:43 -07001082 if (!work->fHasPerp && !work->fCollapsed) {
1083 if (prior) {
1084 work->fCoinStart = prior->fCoinEnd;
1085 } else {
1086 work->fCoinStart.setPerp(fCurve, work->fStartT, work->fPart[0], opp);
caryclark45fa4472015-01-16 07:04:10 -08001087 }
caryclark6c3b9cd2016-09-26 05:36:58 -07001088 if (work->fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001089 double perpT = work->fCoinStart.perpT();
1090 if (sect2->coincidentHasT(perpT)) {
caryclarkdf386c52015-04-21 05:27:02 -07001091 work->fCoinStart.init();
caryclark54359292015-03-26 07:52:43 -07001092 } else {
1093 sect2->addForPerp(work, perpT);
1094 }
1095 }
1096 work->fCoinEnd.setPerp(fCurve, work->fEndT, work->fPart[TCurve::kPointLast], opp);
caryclark6c3b9cd2016-09-26 05:36:58 -07001097 if (work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001098 double perpT = work->fCoinEnd.perpT();
1099 if (sect2->coincidentHasT(perpT)) {
caryclarkdf386c52015-04-21 05:27:02 -07001100 work->fCoinEnd.init();
caryclark54359292015-03-26 07:52:43 -07001101 } else {
1102 sect2->addForPerp(work, perpT);
1103 }
1104 }
1105 work->fHasPerp = true;
caryclark45fa4472015-01-16 07:04:10 -08001106 }
1107 if (work == last) {
1108 break;
1109 }
caryclark54359292015-03-26 07:52:43 -07001110 prior = work;
caryclark45fa4472015-01-16 07:04:10 -08001111 work = work->fNext;
1112 SkASSERT(work);
1113 } while (true);
caryclark54359292015-03-26 07:52:43 -07001114}
1115
caryclark1049f122015-04-20 08:31:59 -07001116template<typename TCurve, typename OppCurve>
1117int SkTSect<TCurve, OppCurve>::countConsecutiveSpans(SkTSpan<TCurve, OppCurve>* first,
1118 SkTSpan<TCurve, OppCurve>** lastPtr) const {
caryclark54359292015-03-26 07:52:43 -07001119 int consecutive = 1;
caryclark1049f122015-04-20 08:31:59 -07001120 SkTSpan<TCurve, OppCurve>* last = first;
caryclark54359292015-03-26 07:52:43 -07001121 do {
caryclark1049f122015-04-20 08:31:59 -07001122 SkTSpan<TCurve, OppCurve>* next = last->fNext;
caryclark54359292015-03-26 07:52:43 -07001123 if (!next) {
1124 break;
1125 }
1126 if (next->fStartT > last->fEndT) {
1127 break;
1128 }
1129 ++consecutive;
1130 last = next;
1131 } while (true);
1132 *lastPtr = last;
1133 return consecutive;
1134}
1135
caryclark1049f122015-04-20 08:31:59 -07001136template<typename TCurve, typename OppCurve>
1137bool SkTSect<TCurve, OppCurve>::debugHasBounded(const SkTSpan<OppCurve, TCurve>* span) const {
1138 const SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001139 if (!test) {
1140 return false;
1141 }
1142 do {
1143 if (test->findOppSpan(span)) {
1144 return true;
1145 }
1146 } while ((test = test->next()));
1147 return false;
1148}
1149
caryclark1049f122015-04-20 08:31:59 -07001150template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001151bool SkTSect<TCurve, OppCurve>::deleteEmptySpans() {
caryclark1049f122015-04-20 08:31:59 -07001152 SkTSpan<TCurve, OppCurve>* test;
1153 SkTSpan<TCurve, OppCurve>* next = fHead;
caryclark54359292015-03-26 07:52:43 -07001154 while ((test = next)) {
1155 next = test->fNext;
1156 if (!test->fBounded) {
caryclarkef7cee42016-09-06 09:05:54 -07001157 if (!this->removeSpan(test)) {
1158 return false;
1159 }
caryclark54359292015-03-26 07:52:43 -07001160 }
1161 }
caryclarkef7cee42016-09-06 09:05:54 -07001162 return true;
caryclark54359292015-03-26 07:52:43 -07001163}
1164
caryclark1049f122015-04-20 08:31:59 -07001165template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001166bool SkTSect<TCurve, OppCurve>::extractCoincident(
caryclark1049f122015-04-20 08:31:59 -07001167 SkTSect<OppCurve, TCurve>* sect2,
caryclarkef7cee42016-09-06 09:05:54 -07001168 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last,
1169 SkTSpan<TCurve, OppCurve>** result) {
caryclark1049f122015-04-20 08:31:59 -07001170 first = findCoincidentRun(first, &last);
caryclarka1b42d92016-08-16 10:25:29 -07001171 if (!first || !last) {
caryclarkef7cee42016-09-06 09:05:54 -07001172 *result = nullptr;
1173 return true;
caryclark45fa4472015-01-16 07:04:10 -08001174 }
1175 // march outwards to find limit of coincidence from here to previous and next spans
1176 double startT = first->fStartT;
caryclarkd8bc16b2015-03-26 09:05:12 -07001177 double oppStartT SK_INIT_TO_AVOID_WARNING;
caryclark54359292015-03-26 07:52:43 -07001178 double oppEndT SK_INIT_TO_AVOID_WARNING;
caryclark1049f122015-04-20 08:31:59 -07001179 SkTSpan<TCurve, OppCurve>* prev = first->fPrev;
caryclark6c3b9cd2016-09-26 05:36:58 -07001180 SkASSERT(first->fCoinStart.isMatch());
caryclark1049f122015-04-20 08:31:59 -07001181 SkTSpan<OppCurve, TCurve>* oppFirst = first->findOppT(first->fCoinStart.perpT());
caryclark6c3b9cd2016-09-26 05:36:58 -07001182 SkOPASSERT(last->fCoinEnd.isMatch());
caryclark54359292015-03-26 07:52:43 -07001183 bool oppMatched = first->fCoinStart.perpT() < first->fCoinEnd.perpT();
1184 double coinStart;
1185 SkDEBUGCODE(double coinEnd);
caryclark1049f122015-04-20 08:31:59 -07001186 SkTSpan<OppCurve, TCurve>* cutFirst;
caryclark54359292015-03-26 07:52:43 -07001187 if (prev && prev->fEndT == startT
1188 && this->binarySearchCoin(sect2, startT, prev->fStartT - startT, &coinStart,
1189 &oppStartT)
caryclark1049f122015-04-20 08:31:59 -07001190 && prev->fStartT < coinStart && coinStart < startT
1191 && (cutFirst = prev->oppT(oppStartT))) {
1192 oppFirst = cutFirst;
caryclark54359292015-03-26 07:52:43 -07001193 first = this->addSplitAt(prev, coinStart);
1194 first->markCoincident();
1195 prev->fCoinEnd.markCoincident();
1196 if (oppFirst->fStartT < oppStartT && oppStartT < oppFirst->fEndT) {
caryclark1049f122015-04-20 08:31:59 -07001197 SkTSpan<OppCurve, TCurve>* oppHalf = sect2->addSplitAt(oppFirst, oppStartT);
caryclark54359292015-03-26 07:52:43 -07001198 if (oppMatched) {
1199 oppFirst->fCoinEnd.markCoincident();
1200 oppHalf->markCoincident();
1201 oppFirst = oppHalf;
1202 } else {
1203 oppFirst->markCoincident();
1204 oppHalf->fCoinStart.markCoincident();
1205 }
1206 }
1207 } else {
1208 SkDEBUGCODE(coinStart = first->fStartT);
1209 SkDEBUGCODE(oppStartT = oppMatched ? oppFirst->fStartT : oppFirst->fEndT);
1210 }
caryclark1049f122015-04-20 08:31:59 -07001211 // FIXME: incomplete : if we're not at the end, find end of coin
1212 SkTSpan<OppCurve, TCurve>* oppLast;
caryclark6c3b9cd2016-09-26 05:36:58 -07001213 SkOPASSERT(last->fCoinEnd.isMatch());
caryclark54359292015-03-26 07:52:43 -07001214 oppLast = last->findOppT(last->fCoinEnd.perpT());
1215 SkDEBUGCODE(coinEnd = last->fEndT);
caryclark643ede62016-08-08 14:27:45 -07001216#ifdef SK_DEBUG
1217 if (!this->globalState() || !this->globalState()->debugSkipAssert()) {
1218 oppEndT = oppMatched ? oppLast->fEndT : oppLast->fStartT;
1219 }
1220#endif
caryclark54359292015-03-26 07:52:43 -07001221 if (!oppMatched) {
1222 SkTSwap(oppFirst, oppLast);
1223 SkTSwap(oppStartT, oppEndT);
1224 }
caryclarke25a4f62016-07-26 09:26:29 -07001225 SkOPASSERT(oppStartT < oppEndT);
caryclark54359292015-03-26 07:52:43 -07001226 SkASSERT(coinStart == first->fStartT);
1227 SkASSERT(coinEnd == last->fEndT);
caryclark643ede62016-08-08 14:27:45 -07001228 SkOPASSERT(oppStartT == oppFirst->fStartT);
1229 SkOPASSERT(oppEndT == oppLast->fEndT);
1230 if (!oppFirst) {
caryclarkef7cee42016-09-06 09:05:54 -07001231 *result = nullptr;
1232 return true;
caryclark643ede62016-08-08 14:27:45 -07001233 }
caryclark42942862016-08-19 07:01:33 -07001234 if (!oppLast) {
caryclarkef7cee42016-09-06 09:05:54 -07001235 *result = nullptr;
1236 return true;
caryclark42942862016-08-19 07:01:33 -07001237 }
caryclark54359292015-03-26 07:52:43 -07001238 // reduce coincident runs to single entries
1239 this->validate();
1240 sect2->validate();
caryclark1049f122015-04-20 08:31:59 -07001241 bool deleteEmptySpans = this->updateBounded(first, last, oppFirst);
1242 deleteEmptySpans |= sect2->updateBounded(oppFirst, oppLast, first);
caryclark54359292015-03-26 07:52:43 -07001243 this->removeSpanRange(first, last);
1244 sect2->removeSpanRange(oppFirst, oppLast);
1245 first->fEndT = last->fEndT;
1246 first->resetBounds(this->fCurve);
1247 first->fCoinStart.setPerp(fCurve, first->fStartT, first->fPart[0], sect2->fCurve);
1248 first->fCoinEnd.setPerp(fCurve, first->fEndT, first->fPart[TCurve::kPointLast], sect2->fCurve);
1249 oppStartT = first->fCoinStart.perpT();
1250 oppEndT = first->fCoinEnd.perpT();
1251 if (between(0, oppStartT, 1) && between(0, oppEndT, 1)) {
1252 if (!oppMatched) {
1253 SkTSwap(oppStartT, oppEndT);
1254 }
1255 oppFirst->fStartT = oppStartT;
1256 oppFirst->fEndT = oppEndT;
1257 oppFirst->resetBounds(sect2->fCurve);
1258 }
1259 this->validateBounded();
1260 sect2->validateBounded();
1261 last = first->fNext;
1262 this->removeCoincident(first, false);
1263 sect2->removeCoincident(oppFirst, true);
caryclark1049f122015-04-20 08:31:59 -07001264 if (deleteEmptySpans) {
caryclarkef7cee42016-09-06 09:05:54 -07001265 if (!this->deleteEmptySpans() || !sect2->deleteEmptySpans()) {
1266 *result = nullptr;
1267 return false;
1268 }
caryclark54359292015-03-26 07:52:43 -07001269 }
1270 this->validate();
1271 sect2->validate();
caryclarkef7cee42016-09-06 09:05:54 -07001272 *result = last && !last->fDeleted && fHead && sect2->fHead ? last : nullptr;
1273 return true;
caryclark54359292015-03-26 07:52:43 -07001274}
1275
caryclark1049f122015-04-20 08:31:59 -07001276template<typename TCurve, typename OppCurve>
1277SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::findCoincidentRun(
1278 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>** lastPtr) {
1279 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001280 SkTSpan<TCurve, OppCurve>* lastCandidate = nullptr;
1281 first = nullptr;
caryclark54359292015-03-26 07:52:43 -07001282 // find the first fully coincident span
1283 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07001284 if (work->fCoinStart.isMatch()) {
caryclark1049f122015-04-20 08:31:59 -07001285#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -07001286 work->validatePerpT(work->fCoinStart.perpT());
1287 work->validatePerpPt(work->fCoinStart.perpT(), work->fCoinStart.perpPt());
caryclark1049f122015-04-20 08:31:59 -07001288#endif
caryclark54359292015-03-26 07:52:43 -07001289 SkASSERT(work->hasOppT(work->fCoinStart.perpT()));
caryclark6c3b9cd2016-09-26 05:36:58 -07001290 if (!work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001291 break;
1292 }
1293 lastCandidate = work;
1294 if (!first) {
1295 first = work;
1296 }
caryclark1049f122015-04-20 08:31:59 -07001297 } else if (first && work->fCollapsed) {
1298 *lastPtr = lastCandidate;
1299 return first;
caryclark54359292015-03-26 07:52:43 -07001300 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001301 lastCandidate = nullptr;
caryclark643ede62016-08-08 14:27:45 -07001302 SkOPASSERT(!first);
caryclark54359292015-03-26 07:52:43 -07001303 }
1304 if (work == *lastPtr) {
1305 return first;
1306 }
1307 work = work->fNext;
caryclarke25a4f62016-07-26 09:26:29 -07001308 if (!work) {
1309 return nullptr;
1310 }
caryclark54359292015-03-26 07:52:43 -07001311 } while (true);
1312 if (lastCandidate) {
1313 *lastPtr = lastCandidate;
1314 }
1315 return first;
1316}
1317
caryclark1049f122015-04-20 08:31:59 -07001318template<typename TCurve, typename OppCurve>
1319int SkTSect<TCurve, OppCurve>::intersects(SkTSpan<TCurve, OppCurve>* span,
1320 SkTSect<OppCurve, TCurve>* opp,
1321 SkTSpan<OppCurve, TCurve>* oppSpan, int* oppResult) {
caryclark54359292015-03-26 07:52:43 -07001322 bool spanStart, oppStart;
1323 int hullResult = span->hullsIntersect(oppSpan, &spanStart, &oppStart);
1324 if (hullResult >= 0) {
1325 if (hullResult == 2) { // hulls have one point in common
1326 if (!span->fBounded || !span->fBounded->fNext) {
1327 SkASSERT(!span->fBounded || span->fBounded->fBounded == oppSpan);
1328 if (spanStart) {
1329 span->fEndT = span->fStartT;
caryclark45fa4472015-01-16 07:04:10 -08001330 } else {
caryclark54359292015-03-26 07:52:43 -07001331 span->fStartT = span->fEndT;
1332 }
1333 } else {
1334 hullResult = 1;
1335 }
1336 if (!oppSpan->fBounded || !oppSpan->fBounded->fNext) {
1337 SkASSERT(!oppSpan->fBounded || oppSpan->fBounded->fBounded == span);
1338 if (oppStart) {
1339 oppSpan->fEndT = oppSpan->fStartT;
1340 } else {
1341 oppSpan->fStartT = oppSpan->fEndT;
1342 }
1343 *oppResult = 2;
1344 } else {
1345 *oppResult = 1;
1346 }
1347 } else {
1348 *oppResult = 1;
1349 }
1350 return hullResult;
1351 }
1352 if (span->fIsLine && oppSpan->fIsLine) {
1353 SkIntersections i;
1354 int sects = this->linesIntersect(span, opp, oppSpan, &i);
caryclark26ad22a2015-10-16 09:03:38 -07001355 if (sects == 2) {
1356 return *oppResult = 1;
1357 }
caryclark54359292015-03-26 07:52:43 -07001358 if (!sects) {
1359 return -1;
1360 }
1361 span->fStartT = span->fEndT = i[0][0];
1362 oppSpan->fStartT = oppSpan->fEndT = i[1][0];
1363 return *oppResult = 2;
1364 }
1365 if (span->fIsLinear || oppSpan->fIsLinear) {
1366 return *oppResult = (int) span->linearsIntersect(oppSpan);
1367 }
1368 return *oppResult = 1;
1369}
1370
caryclarked0935a2015-10-22 07:23:52 -07001371template<typename TCurve>
1372static bool is_parallel(const SkDLine& thisLine, const TCurve& opp) {
1373 if (!opp.IsConic()) {
1374 return false; // FIXME : breaks a lot of stuff now
1375 }
1376 int finds = 0;
1377 SkDLine thisPerp;
1378 thisPerp.fPts[0].fX = thisLine.fPts[1].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1379 thisPerp.fPts[0].fY = thisLine.fPts[1].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1380 thisPerp.fPts[1] = thisLine.fPts[1];
1381 SkIntersections perpRayI;
1382 perpRayI.intersectRay(opp, thisPerp);
1383 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1384 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[1]);
1385 }
1386 thisPerp.fPts[1].fX = thisLine.fPts[0].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1387 thisPerp.fPts[1].fY = thisLine.fPts[0].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1388 thisPerp.fPts[0] = thisLine.fPts[0];
1389 perpRayI.intersectRay(opp, thisPerp);
1390 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1391 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[0]);
1392 }
1393 return finds >= 2;
1394}
1395
caryclark54359292015-03-26 07:52:43 -07001396// while the intersection points are sufficiently far apart:
1397// construct the tangent lines from the intersections
1398// find the point where the tangent line intersects the opposite curve
caryclark1049f122015-04-20 08:31:59 -07001399template<typename TCurve, typename OppCurve>
1400int SkTSect<TCurve, OppCurve>::linesIntersect(SkTSpan<TCurve, OppCurve>* span,
1401 SkTSect<OppCurve, TCurve>* opp,
1402 SkTSpan<OppCurve, TCurve>* oppSpan, SkIntersections* i) {
caryclark54359292015-03-26 07:52:43 -07001403 SkIntersections thisRayI, oppRayI;
1404 SkDLine thisLine = {{ span->fPart[0], span->fPart[TCurve::kPointLast] }};
caryclark1049f122015-04-20 08:31:59 -07001405 SkDLine oppLine = {{ oppSpan->fPart[0], oppSpan->fPart[OppCurve::kPointLast] }};
caryclark54359292015-03-26 07:52:43 -07001406 int loopCount = 0;
1407 double bestDistSq = DBL_MAX;
caryclark1049f122015-04-20 08:31:59 -07001408 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1409 return 0;
1410 }
1411 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1412 return 0;
1413 }
caryclark26ad22a2015-10-16 09:03:38 -07001414 // if the ends of each line intersect the opposite curve, the lines are coincident
1415 if (thisRayI.used() > 1) {
1416 int ptMatches = 0;
1417 for (int tIndex = 0; tIndex < thisRayI.used(); ++tIndex) {
1418 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(thisLine.fPts); ++lIndex) {
1419 ptMatches += thisRayI.pt(tIndex).approximatelyEqual(thisLine.fPts[lIndex]);
1420 }
1421 }
caryclarked0935a2015-10-22 07:23:52 -07001422 if (ptMatches == 2 || is_parallel(thisLine, opp->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001423 return 2;
1424 }
1425 }
1426 if (oppRayI.used() > 1) {
1427 int ptMatches = 0;
1428 for (int oIndex = 0; oIndex < oppRayI.used(); ++oIndex) {
1429 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(thisLine.fPts); ++lIndex) {
1430 ptMatches += oppRayI.pt(oIndex).approximatelyEqual(oppLine.fPts[lIndex]);
1431 }
1432 }
caryclarked0935a2015-10-22 07:23:52 -07001433 if (ptMatches == 2|| is_parallel(oppLine, this->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001434 return 2;
1435 }
1436 }
caryclark54359292015-03-26 07:52:43 -07001437 do {
caryclark54359292015-03-26 07:52:43 -07001438 // pick the closest pair of points
1439 double closest = DBL_MAX;
1440 int closeIndex SK_INIT_TO_AVOID_WARNING;
1441 int oppCloseIndex SK_INIT_TO_AVOID_WARNING;
1442 for (int index = 0; index < oppRayI.used(); ++index) {
1443 if (!roughly_between(span->fStartT, oppRayI[0][index], span->fEndT)) {
1444 continue;
1445 }
1446 for (int oIndex = 0; oIndex < thisRayI.used(); ++oIndex) {
1447 if (!roughly_between(oppSpan->fStartT, thisRayI[0][oIndex], oppSpan->fEndT)) {
1448 continue;
1449 }
1450 double distSq = thisRayI.pt(index).distanceSquared(oppRayI.pt(oIndex));
1451 if (closest > distSq) {
1452 closest = distSq;
1453 closeIndex = index;
1454 oppCloseIndex = oIndex;
caryclarkccec0f92015-03-24 07:28:17 -07001455 }
caryclarkccec0f92015-03-24 07:28:17 -07001456 }
reed0dc4dd62015-03-24 13:55:33 -07001457 }
caryclark54359292015-03-26 07:52:43 -07001458 if (closest == DBL_MAX) {
caryclark1049f122015-04-20 08:31:59 -07001459 break;
reed0dc4dd62015-03-24 13:55:33 -07001460 }
caryclark54359292015-03-26 07:52:43 -07001461 const SkDPoint& oppIPt = thisRayI.pt(oppCloseIndex);
1462 const SkDPoint& iPt = oppRayI.pt(closeIndex);
1463 if (between(span->fStartT, oppRayI[0][closeIndex], span->fEndT)
1464 && between(oppSpan->fStartT, thisRayI[0][oppCloseIndex], oppSpan->fEndT)
1465 && oppIPt.approximatelyEqual(iPt)) {
1466 i->merge(oppRayI, closeIndex, thisRayI, oppCloseIndex);
1467 return i->used();
1468 }
1469 double distSq = oppIPt.distanceSquared(iPt);
1470 if (bestDistSq < distSq || ++loopCount > 5) {
caryclark1049f122015-04-20 08:31:59 -07001471 return 0;
caryclark54359292015-03-26 07:52:43 -07001472 }
1473 bestDistSq = distSq;
caryclark1049f122015-04-20 08:31:59 -07001474 double oppStart = oppRayI[0][closeIndex];
1475 thisLine[0] = fCurve.ptAtT(oppStart);
1476 thisLine[1] = thisLine[0] + fCurve.dxdyAtT(oppStart);
1477 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1478 break;
1479 }
1480 double start = thisRayI[0][oppCloseIndex];
1481 oppLine[0] = opp->fCurve.ptAtT(start);
1482 oppLine[1] = oppLine[0] + opp->fCurve.dxdyAtT(start);
1483 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1484 break;
1485 }
caryclark54359292015-03-26 07:52:43 -07001486 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001487 // convergence may fail if the curves are nearly coincident
1488 SkTCoincident<OppCurve, TCurve> oCoinS, oCoinE;
1489 oCoinS.setPerp(opp->fCurve, oppSpan->fStartT, oppSpan->fPart[0], fCurve);
1490 oCoinE.setPerp(opp->fCurve, oppSpan->fEndT, oppSpan->fPart[OppCurve::kPointLast], fCurve);
1491 double tStart = oCoinS.perpT();
1492 double tEnd = oCoinE.perpT();
1493 bool swap = tStart > tEnd;
1494 if (swap) {
1495 SkTSwap(tStart, tEnd);
1496 }
1497 tStart = SkTMax(tStart, span->fStartT);
1498 tEnd = SkTMin(tEnd, span->fEndT);
1499 if (tStart > tEnd) {
1500 return 0;
1501 }
1502 SkDVector perpS, perpE;
1503 if (tStart == span->fStartT) {
1504 SkTCoincident<TCurve, OppCurve> coinS;
1505 coinS.setPerp(fCurve, span->fStartT, span->fPart[0], opp->fCurve);
1506 perpS = span->fPart[0] - coinS.perpPt();
1507 } else if (swap) {
1508 perpS = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1509 } else {
1510 perpS = oCoinS.perpPt() - oppSpan->fPart[0];
1511 }
1512 if (tEnd == span->fEndT) {
1513 SkTCoincident<TCurve, OppCurve> coinE;
1514 coinE.setPerp(fCurve, span->fEndT, span->fPart[TCurve::kPointLast], opp->fCurve);
1515 perpE = span->fPart[TCurve::kPointLast] - coinE.perpPt();
1516 } else if (swap) {
1517 perpE = oCoinS.perpPt() - oppSpan->fPart[0];
1518 } else {
1519 perpE = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1520 }
1521 if (perpS.dot(perpE) >= 0) {
1522 return 0;
1523 }
1524 SkTCoincident<TCurve, OppCurve> coinW;
1525 double workT = tStart;
1526 double tStep = tEnd - tStart;
1527 SkDPoint workPt;
1528 do {
1529 tStep *= 0.5;
1530 if (precisely_zero(tStep)) {
1531 return 0;
1532 }
1533 workT += tStep;
1534 workPt = fCurve.ptAtT(workT);
1535 coinW.setPerp(fCurve, workT, workPt, opp->fCurve);
caryclark27c015d2016-09-23 05:47:20 -07001536 double perpT = coinW.perpT();
caryclark6c3b9cd2016-09-26 05:36:58 -07001537 if (coinW.isMatch() ? !between(oppSpan->fStartT, perpT, oppSpan->fEndT) : perpT < 0) {
caryclarkb6693002015-12-16 12:28:35 -08001538 continue;
1539 }
caryclark1049f122015-04-20 08:31:59 -07001540 SkDVector perpW = workPt - coinW.perpPt();
1541 if ((perpS.dot(perpW) >= 0) == (tStep < 0)) {
1542 tStep = -tStep;
1543 }
caryclarkb6693002015-12-16 12:28:35 -08001544 if (workPt.approximatelyEqual(coinW.perpPt())) {
1545 break;
1546 }
1547 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001548 double oppTTest = coinW.perpT();
1549 if (!opp->fHead->contains(oppTTest)) {
1550 return 0;
1551 }
1552 i->setMax(1);
1553 i->insert(workT, oppTTest, workPt);
1554 return 1;
caryclark54359292015-03-26 07:52:43 -07001555}
1556
1557
caryclark1049f122015-04-20 08:31:59 -07001558template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001559bool SkTSect<TCurve, OppCurve>::markSpanGone(SkTSpan<TCurve, OppCurve>* span) {
1560 if (--fActiveCount < 0) {
1561 return false;
1562 }
caryclark54359292015-03-26 07:52:43 -07001563 span->fNext = fDeleted;
1564 fDeleted = span;
caryclarke25a4f62016-07-26 09:26:29 -07001565 SkOPASSERT(!span->fDeleted);
caryclark54359292015-03-26 07:52:43 -07001566 span->fDeleted = true;
caryclarkef7cee42016-09-06 09:05:54 -07001567 return true;
caryclark54359292015-03-26 07:52:43 -07001568}
1569
caryclark1049f122015-04-20 08:31:59 -07001570template<typename TCurve, typename OppCurve>
1571bool SkTSect<TCurve, OppCurve>::matchedDirection(double t, const SkTSect<OppCurve, TCurve>* sect2,
1572 double t2) const {
caryclark54359292015-03-26 07:52:43 -07001573 SkDVector dxdy = this->fCurve.dxdyAtT(t);
1574 SkDVector dxdy2 = sect2->fCurve.dxdyAtT(t2);
1575 return dxdy.dot(dxdy2) >= 0;
1576}
1577
caryclark1049f122015-04-20 08:31:59 -07001578template<typename TCurve, typename OppCurve>
1579void SkTSect<TCurve, OppCurve>::matchedDirCheck(double t, const SkTSect<OppCurve, TCurve>* sect2,
1580 double t2, bool* calcMatched, bool* oppMatched) const {
caryclark54359292015-03-26 07:52:43 -07001581 if (*calcMatched) {
caryclark55888e42016-07-18 10:01:36 -07001582 SkASSERT(*oppMatched == this->matchedDirection(t, sect2, t2));
caryclark54359292015-03-26 07:52:43 -07001583 } else {
1584 *oppMatched = this->matchedDirection(t, sect2, t2);
1585 *calcMatched = true;
1586 }
1587}
1588
caryclark1049f122015-04-20 08:31:59 -07001589template<typename TCurve, typename OppCurve>
1590void SkTSect<TCurve, OppCurve>::mergeCoincidence(SkTSect<OppCurve, TCurve>* sect2) {
caryclark54359292015-03-26 07:52:43 -07001591 double smallLimit = 0;
1592 do {
1593 // find the smallest unprocessed span
halcanary96fcdcc2015-08-27 07:41:13 -07001594 SkTSpan<TCurve, OppCurve>* smaller = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001595 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001596 do {
caryclark221a4bb2016-10-07 11:15:15 -07001597 if (!test) {
1598 return;
1599 }
caryclark54359292015-03-26 07:52:43 -07001600 if (test->fStartT < smallLimit) {
1601 continue;
1602 }
1603 if (smaller && smaller->fEndT < test->fStartT) {
1604 continue;
1605 }
1606 smaller = test;
1607 } while ((test = test->fNext));
1608 if (!smaller) {
1609 return;
1610 }
1611 smallLimit = smaller->fEndT;
1612 // find next larger span
halcanary96fcdcc2015-08-27 07:41:13 -07001613 SkTSpan<TCurve, OppCurve>* prior = nullptr;
1614 SkTSpan<TCurve, OppCurve>* larger = nullptr;
1615 SkTSpan<TCurve, OppCurve>* largerPrior = nullptr;
caryclark54359292015-03-26 07:52:43 -07001616 test = fCoincident;
1617 do {
1618 if (test->fStartT < smaller->fEndT) {
1619 continue;
1620 }
caryclark221a4bb2016-10-07 11:15:15 -07001621 SkOPASSERT(test->fStartT != smaller->fEndT);
caryclark54359292015-03-26 07:52:43 -07001622 if (larger && larger->fStartT < test->fStartT) {
1623 continue;
1624 }
1625 largerPrior = prior;
1626 larger = test;
1627 } while ((prior = test), (test = test->fNext));
1628 if (!larger) {
1629 continue;
1630 }
1631 // check middle t value to see if it is coincident as well
1632 double midT = (smaller->fEndT + larger->fStartT) / 2;
1633 SkDPoint midPt = fCurve.ptAtT(midT);
caryclark1049f122015-04-20 08:31:59 -07001634 SkTCoincident<TCurve, OppCurve> coin;
caryclark54359292015-03-26 07:52:43 -07001635 coin.setPerp(fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07001636 if (coin.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001637 smaller->fEndT = larger->fEndT;
1638 smaller->fCoinEnd = larger->fCoinEnd;
1639 if (largerPrior) {
1640 largerPrior->fNext = larger->fNext;
caryclark643ede62016-08-08 14:27:45 -07001641 largerPrior->validate();
caryclark54359292015-03-26 07:52:43 -07001642 } else {
1643 fCoincident = larger->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001644 }
1645 }
caryclark54359292015-03-26 07:52:43 -07001646 } while (true);
1647}
1648
caryclark1049f122015-04-20 08:31:59 -07001649template<typename TCurve, typename OppCurve>
1650SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::prev(
1651 SkTSpan<TCurve, OppCurve>* span) const {
halcanary96fcdcc2015-08-27 07:41:13 -07001652 SkTSpan<TCurve, OppCurve>* result = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001653 SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001654 while (span != test) {
1655 result = test;
1656 test = test->fNext;
1657 SkASSERT(test);
caryclarkccec0f92015-03-24 07:28:17 -07001658 }
caryclark55888e42016-07-18 10:01:36 -07001659 return result;
caryclarkccec0f92015-03-24 07:28:17 -07001660}
1661
caryclark1049f122015-04-20 08:31:59 -07001662template<typename TCurve, typename OppCurve>
1663void SkTSect<TCurve, OppCurve>::recoverCollapsed() {
1664 SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001665 while (deleted) {
caryclark1049f122015-04-20 08:31:59 -07001666 SkTSpan<TCurve, OppCurve>* delNext = deleted->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001667 if (deleted->fCollapsed) {
caryclark1049f122015-04-20 08:31:59 -07001668 SkTSpan<TCurve, OppCurve>** spanPtr = &fHead;
caryclark45fa4472015-01-16 07:04:10 -08001669 while (*spanPtr && (*spanPtr)->fEndT <= deleted->fStartT) {
1670 spanPtr = &(*spanPtr)->fNext;
1671 }
1672 deleted->fNext = *spanPtr;
1673 *spanPtr = deleted;
1674 }
1675 deleted = delNext;
1676 }
1677}
1678
caryclark1049f122015-04-20 08:31:59 -07001679template<typename TCurve, typename OppCurve>
1680void SkTSect<TCurve, OppCurve>::removeAllBut(const SkTSpan<OppCurve, TCurve>* keep,
1681 SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp) {
1682 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001683 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001684 SkTSpan<OppCurve, TCurve>* bounded = testBounded->fBounded;
1685 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001686 // may have been deleted when opp did 'remove all but'
1687 if (bounded != keep && !bounded->fDeleted) {
1688 SkAssertResult(SkDEBUGCODE(!) span->removeBounded(bounded));
1689 if (bounded->removeBounded(span)) {
1690 opp->removeSpan(bounded);
1691 }
caryclarkccec0f92015-03-24 07:28:17 -07001692 }
caryclark54359292015-03-26 07:52:43 -07001693 testBounded = next;
caryclarkccec0f92015-03-24 07:28:17 -07001694 }
caryclark54359292015-03-26 07:52:43 -07001695 SkASSERT(!span->fDeleted);
1696 SkASSERT(span->findOppSpan(keep));
1697 SkASSERT(keep->findOppSpan(span));
caryclarkccec0f92015-03-24 07:28:17 -07001698}
1699
caryclark1049f122015-04-20 08:31:59 -07001700template<typename TCurve, typename OppCurve>
1701void SkTSect<TCurve, OppCurve>::removeByPerpendicular(SkTSect<OppCurve, TCurve>* opp) {
1702 SkTSpan<TCurve, OppCurve>* test = fHead;
1703 SkTSpan<TCurve, OppCurve>* next;
caryclark54359292015-03-26 07:52:43 -07001704 do {
1705 next = test->fNext;
1706 if (test->fCoinStart.perpT() < 0 || test->fCoinEnd.perpT() < 0) {
1707 continue;
reed0dc4dd62015-03-24 13:55:33 -07001708 }
caryclark54359292015-03-26 07:52:43 -07001709 SkDVector startV = test->fCoinStart.perpPt() - test->fPart[0];
1710 SkDVector endV = test->fCoinEnd.perpPt() - test->fPart[TCurve::kPointLast];
1711#if DEBUG_T_SECT
1712 SkDebugf("%s startV=(%1.9g,%1.9g) endV=(%1.9g,%1.9g) dot=%1.9g\n", __FUNCTION__,
1713 startV.fX, startV.fY, endV.fX, endV.fY, startV.dot(endV));
1714#endif
1715 if (startV.dot(endV) <= 0) {
1716 continue;
1717 }
1718 this->removeSpans(test, opp);
1719 } while ((test = next));
1720}
1721
caryclark1049f122015-04-20 08:31:59 -07001722template<typename TCurve, typename OppCurve>
1723void SkTSect<TCurve, OppCurve>::removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween) {
caryclark54359292015-03-26 07:52:43 -07001724 this->unlinkSpan(span);
1725 if (isBetween || between(0, span->fCoinStart.perpT(), 1)) {
1726 --fActiveCount;
1727 span->fNext = fCoincident;
1728 fCoincident = span;
1729 } else {
1730 this->markSpanGone(span);
reed0dc4dd62015-03-24 13:55:33 -07001731 }
caryclarkccec0f92015-03-24 07:28:17 -07001732}
1733
caryclark1049f122015-04-20 08:31:59 -07001734template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001735bool SkTSect<TCurve, OppCurve>::removeSpan(SkTSpan<TCurve, OppCurve>* span) {
caryclark6c3b9cd2016-09-26 05:36:58 -07001736 if (!span->fStartT) {
1737 fRemovedStartT = true;
1738 }
1739 if (1 == span->fEndT) {
1740 fRemovedEndT = true;
1741 }
caryclark54359292015-03-26 07:52:43 -07001742 this->unlinkSpan(span);
caryclarkef7cee42016-09-06 09:05:54 -07001743 return this->markSpanGone(span);
caryclark54359292015-03-26 07:52:43 -07001744}
1745
caryclark1049f122015-04-20 08:31:59 -07001746template<typename TCurve, typename OppCurve>
1747void SkTSect<TCurve, OppCurve>::removeSpanRange(SkTSpan<TCurve, OppCurve>* first,
1748 SkTSpan<TCurve, OppCurve>* last) {
caryclark54359292015-03-26 07:52:43 -07001749 if (first == last) {
1750 return;
1751 }
caryclark1049f122015-04-20 08:31:59 -07001752 SkTSpan<TCurve, OppCurve>* span = first;
caryclark54359292015-03-26 07:52:43 -07001753 SkASSERT(span);
caryclark1049f122015-04-20 08:31:59 -07001754 SkTSpan<TCurve, OppCurve>* final = last->fNext;
1755 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001756 while ((span = next) && span != final) {
1757 next = span->fNext;
1758 this->markSpanGone(span);
1759 }
1760 if (final) {
1761 final->fPrev = first;
1762 }
1763 first->fNext = final;
caryclark643ede62016-08-08 14:27:45 -07001764 first->validate();
caryclark54359292015-03-26 07:52:43 -07001765}
1766
caryclark1049f122015-04-20 08:31:59 -07001767template<typename TCurve, typename OppCurve>
1768void SkTSect<TCurve, OppCurve>::removeSpans(SkTSpan<TCurve, OppCurve>* span,
1769 SkTSect<OppCurve, TCurve>* opp) {
1770 SkTSpanBounded<OppCurve, TCurve>* bounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001771 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -07001772 SkTSpan<OppCurve, TCurve>* spanBounded = bounded->fBounded;
1773 SkTSpanBounded<OppCurve, TCurve>* next = bounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001774 if (span->removeBounded(spanBounded)) { // shuffles last into position 0
1775 this->removeSpan(span);
1776 }
1777 if (spanBounded->removeBounded(span)) {
1778 opp->removeSpan(spanBounded);
1779 }
1780 SkASSERT(!span->fDeleted || !opp->debugHasBounded(span));
1781 bounded = next;
reed0dc4dd62015-03-24 13:55:33 -07001782 }
1783}
caryclarkccec0f92015-03-24 07:28:17 -07001784
caryclark1049f122015-04-20 08:31:59 -07001785template<typename TCurve, typename OppCurve>
1786SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::spanAtT(double t,
1787 SkTSpan<TCurve, OppCurve>** priorSpan) {
1788 SkTSpan<TCurve, OppCurve>* test = fHead;
halcanary96fcdcc2015-08-27 07:41:13 -07001789 SkTSpan<TCurve, OppCurve>* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -07001790 while (test && test->fEndT < t) {
1791 prev = test;
1792 test = test->fNext;
reed0dc4dd62015-03-24 13:55:33 -07001793 }
caryclark54359292015-03-26 07:52:43 -07001794 *priorSpan = prev;
halcanary96fcdcc2015-08-27 07:41:13 -07001795 return test && test->fStartT <= t ? test : nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001796}
1797
caryclark1049f122015-04-20 08:31:59 -07001798template<typename TCurve, typename OppCurve>
1799SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::tail() {
1800 SkTSpan<TCurve, OppCurve>* result = fHead;
1801 SkTSpan<TCurve, OppCurve>* next = fHead;
reed0dc4dd62015-03-24 13:55:33 -07001802 while ((next = next->fNext)) {
1803 if (next->fEndT > result->fEndT) {
1804 result = next;
1805 }
1806 }
1807 return result;
1808}
1809
1810/* Each span has a range of opposite spans it intersects. After the span is split in two,
1811 adjust the range to its new size */
caryclark1049f122015-04-20 08:31:59 -07001812template<typename TCurve, typename OppCurve>
1813void SkTSect<TCurve, OppCurve>::trim(SkTSpan<TCurve, OppCurve>* span,
1814 SkTSect<OppCurve, TCurve>* opp) {
reed0dc4dd62015-03-24 13:55:33 -07001815 span->initBounds(fCurve);
caryclark1049f122015-04-20 08:31:59 -07001816 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001817 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001818 SkTSpan<OppCurve, TCurve>* test = testBounded->fBounded;
1819 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001820 int oppSects, sects = this->intersects(span, opp, test, &oppSects);
1821 if (sects >= 1) {
1822 if (oppSects == 2) {
1823 test->initBounds(opp->fCurve);
1824 opp->removeAllBut(span, test, this);
1825 }
1826 if (sects == 2) {
1827 span->initBounds(fCurve);
1828 this->removeAllBut(test, span, opp);
1829 return;
1830 }
reed0dc4dd62015-03-24 13:55:33 -07001831 } else {
caryclark54359292015-03-26 07:52:43 -07001832 if (span->removeBounded(test)) {
1833 this->removeSpan(span);
1834 }
1835 if (test->removeBounded(span)) {
1836 opp->removeSpan(test);
1837 }
1838 }
1839 testBounded = next;
1840 }
1841}
1842
caryclark1049f122015-04-20 08:31:59 -07001843template<typename TCurve, typename OppCurve>
1844void SkTSect<TCurve, OppCurve>::unlinkSpan(SkTSpan<TCurve, OppCurve>* span) {
1845 SkTSpan<TCurve, OppCurve>* prev = span->fPrev;
1846 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001847 if (prev) {
1848 prev->fNext = next;
1849 if (next) {
1850 next->fPrev = prev;
caryclark643ede62016-08-08 14:27:45 -07001851 next->validate();
caryclark54359292015-03-26 07:52:43 -07001852 }
1853 } else {
1854 fHead = next;
1855 if (next) {
halcanary96fcdcc2015-08-27 07:41:13 -07001856 next->fPrev = nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001857 }
1858 }
1859}
1860
caryclark1049f122015-04-20 08:31:59 -07001861template<typename TCurve, typename OppCurve>
1862bool SkTSect<TCurve, OppCurve>::updateBounded(SkTSpan<TCurve, OppCurve>* first,
1863 SkTSpan<TCurve, OppCurve>* last, SkTSpan<OppCurve, TCurve>* oppFirst) {
1864 SkTSpan<TCurve, OppCurve>* test = first;
1865 const SkTSpan<TCurve, OppCurve>* final = last->next();
caryclark54359292015-03-26 07:52:43 -07001866 bool deleteSpan = false;
1867 do {
1868 deleteSpan |= test->removeAllBounded();
caryclarke25a4f62016-07-26 09:26:29 -07001869 } while ((test = test->fNext) != final && test);
halcanary96fcdcc2015-08-27 07:41:13 -07001870 first->fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -07001871 first->addBounded(oppFirst, &fHeap);
1872 // cannot call validate until remove span range is called
1873 return deleteSpan;
1874}
1875
1876
caryclark1049f122015-04-20 08:31:59 -07001877template<typename TCurve, typename OppCurve>
1878void SkTSect<TCurve, OppCurve>::validate() const {
caryclark643ede62016-08-08 14:27:45 -07001879#if DEBUG_VALIDATE
caryclark45fa4472015-01-16 07:04:10 -08001880 int count = 0;
caryclark643ede62016-08-08 14:27:45 -07001881 double last = 0;
caryclark45fa4472015-01-16 07:04:10 -08001882 if (fHead) {
caryclark1049f122015-04-20 08:31:59 -07001883 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark45fa4472015-01-16 07:04:10 -08001884 SkASSERT(!span->fPrev);
caryclark643ede62016-08-08 14:27:45 -07001885 const SkTSpan<TCurve, OppCurve>* next;
caryclark45fa4472015-01-16 07:04:10 -08001886 do {
1887 span->validate();
1888 SkASSERT(span->fStartT >= last);
caryclark643ede62016-08-08 14:27:45 -07001889 last = span->fEndT;
caryclark45fa4472015-01-16 07:04:10 -08001890 ++count;
caryclark643ede62016-08-08 14:27:45 -07001891 next = span->fNext;
1892 SkASSERT(next != span);
1893 } while ((span = next) != nullptr);
caryclark45fa4472015-01-16 07:04:10 -08001894 }
1895 SkASSERT(count == fActiveCount);
caryclark643ede62016-08-08 14:27:45 -07001896#endif
1897#if DEBUG_T_SECT
caryclark45fa4472015-01-16 07:04:10 -08001898 SkASSERT(fActiveCount <= fDebugAllocatedCount);
1899 int deletedCount = 0;
caryclark1049f122015-04-20 08:31:59 -07001900 const SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001901 while (deleted) {
1902 ++deletedCount;
1903 deleted = deleted->fNext;
1904 }
caryclark1049f122015-04-20 08:31:59 -07001905 const SkTSpan<TCurve, OppCurve>* coincident = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001906 while (coincident) {
1907 ++deletedCount;
1908 coincident = coincident->fNext;
1909 }
caryclark45fa4472015-01-16 07:04:10 -08001910 SkASSERT(fActiveCount + deletedCount == fDebugAllocatedCount);
caryclarkccec0f92015-03-24 07:28:17 -07001911#endif
caryclark54359292015-03-26 07:52:43 -07001912}
1913
caryclark1049f122015-04-20 08:31:59 -07001914template<typename TCurve, typename OppCurve>
1915void SkTSect<TCurve, OppCurve>::validateBounded() const {
caryclark643ede62016-08-08 14:27:45 -07001916#if DEBUG_VALIDATE
caryclark54359292015-03-26 07:52:43 -07001917 if (!fHead) {
1918 return;
1919 }
caryclark1049f122015-04-20 08:31:59 -07001920 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark54359292015-03-26 07:52:43 -07001921 do {
1922 span->validateBounded();
halcanary96fcdcc2015-08-27 07:41:13 -07001923 } while ((span = span->fNext) != nullptr);
caryclark54359292015-03-26 07:52:43 -07001924#endif
1925}
caryclark45fa4472015-01-16 07:04:10 -08001926
caryclark1049f122015-04-20 08:31:59 -07001927template<typename TCurve, typename OppCurve>
1928int SkTSect<TCurve, OppCurve>::EndsEqual(const SkTSect<TCurve, OppCurve>* sect1,
1929 const SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark45fa4472015-01-16 07:04:10 -08001930 int zeroOneSet = 0;
caryclark54359292015-03-26 07:52:43 -07001931 if (sect1->fCurve[0] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001932 zeroOneSet |= kZeroS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001933 intersections->insert(0, 0, sect1->fCurve[0]);
1934 }
caryclark1049f122015-04-20 08:31:59 -07001935 if (sect1->fCurve[0] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001936 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark54359292015-03-26 07:52:43 -07001937 intersections->insert(0, 1, sect1->fCurve[0]);
1938 }
1939 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001940 zeroOneSet |= kOneS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001941 intersections->insert(1, 0, sect1->fCurve[TCurve::kPointLast]);
1942 }
caryclark1049f122015-04-20 08:31:59 -07001943 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001944 zeroOneSet |= kOneS1Set | kOneS2Set;
reed0dc4dd62015-03-24 13:55:33 -07001945 intersections->insert(1, 1, sect1->fCurve[TCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001946 }
1947 // check for zero
1948 if (!(zeroOneSet & (kZeroS1Set | kZeroS2Set))
1949 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[0])) {
1950 zeroOneSet |= kZeroS1Set | kZeroS2Set;
1951 intersections->insertNear(0, 0, sect1->fCurve[0], sect2->fCurve[0]);
1952 }
1953 if (!(zeroOneSet & (kZeroS1Set | kOneS2Set))
caryclark1049f122015-04-20 08:31:59 -07001954 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07001955 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark1049f122015-04-20 08:31:59 -07001956 intersections->insertNear(0, 1, sect1->fCurve[0], sect2->fCurve[OppCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001957 }
1958 // check for one
1959 if (!(zeroOneSet & (kOneS1Set | kZeroS2Set))
1960 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[0])) {
1961 zeroOneSet |= kOneS1Set | kZeroS2Set;
1962 intersections->insertNear(1, 0, sect1->fCurve[TCurve::kPointLast], sect2->fCurve[0]);
1963 }
1964 if (!(zeroOneSet & (kOneS1Set | kOneS2Set))
1965 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[
caryclark1049f122015-04-20 08:31:59 -07001966 OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07001967 zeroOneSet |= kOneS1Set | kOneS2Set;
1968 intersections->insertNear(1, 1, sect1->fCurve[TCurve::kPointLast],
caryclark1049f122015-04-20 08:31:59 -07001969 sect2->fCurve[OppCurve::kPointLast]);
caryclark45fa4472015-01-16 07:04:10 -08001970 }
1971 return zeroOneSet;
1972}
1973
caryclark1049f122015-04-20 08:31:59 -07001974template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08001975struct SkClosestRecord {
caryclark54359292015-03-26 07:52:43 -07001976 bool operator<(const SkClosestRecord& rh) const {
1977 return fClosest < rh.fClosest;
1978 }
1979
caryclark45fa4472015-01-16 07:04:10 -08001980 void addIntersection(SkIntersections* intersections) const {
1981 double r1t = fC1Index ? fC1Span->endT() : fC1Span->startT();
1982 double r2t = fC2Index ? fC2Span->endT() : fC2Span->startT();
1983 intersections->insert(r1t, r2t, fC1Span->part()[fC1Index]);
1984 }
1985
caryclark1049f122015-04-20 08:31:59 -07001986 void findEnd(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2,
caryclark45fa4472015-01-16 07:04:10 -08001987 int c1Index, int c2Index) {
1988 const TCurve& c1 = span1->part();
caryclark1049f122015-04-20 08:31:59 -07001989 const OppCurve& c2 = span2->part();
caryclark45fa4472015-01-16 07:04:10 -08001990 if (!c1[c1Index].approximatelyEqual(c2[c2Index])) {
1991 return;
1992 }
1993 double dist = c1[c1Index].distanceSquared(c2[c2Index]);
1994 if (fClosest < dist) {
1995 return;
1996 }
1997 fC1Span = span1;
1998 fC2Span = span2;
1999 fC1StartT = span1->startT();
2000 fC1EndT = span1->endT();
2001 fC2StartT = span2->startT();
2002 fC2EndT = span2->endT();
2003 fC1Index = c1Index;
2004 fC2Index = c2Index;
2005 fClosest = dist;
2006 }
2007
caryclarkcdeff812016-07-22 03:34:19 -07002008 bool matesWith(const SkClosestRecord& mate SkDEBUGPARAMS(SkIntersections* i)) const {
caryclark45fa4472015-01-16 07:04:10 -08002009 SkASSERT(fC1Span == mate.fC1Span || fC1Span->endT() <= mate.fC1Span->startT()
2010 || mate.fC1Span->endT() <= fC1Span->startT());
caryclarkcdeff812016-07-22 03:34:19 -07002011 SkOPOBJASSERT(i, fC2Span == mate.fC2Span || fC2Span->endT() <= mate.fC2Span->startT()
caryclark45fa4472015-01-16 07:04:10 -08002012 || mate.fC2Span->endT() <= fC2Span->startT());
2013 return fC1Span == mate.fC1Span || fC1Span->endT() == mate.fC1Span->startT()
2014 || fC1Span->startT() == mate.fC1Span->endT()
2015 || fC2Span == mate.fC2Span
2016 || fC2Span->endT() == mate.fC2Span->startT()
2017 || fC2Span->startT() == mate.fC2Span->endT();
2018 }
2019
2020 void merge(const SkClosestRecord& mate) {
2021 fC1Span = mate.fC1Span;
2022 fC2Span = mate.fC2Span;
2023 fClosest = mate.fClosest;
2024 fC1Index = mate.fC1Index;
2025 fC2Index = mate.fC2Index;
2026 }
caryclark55888e42016-07-18 10:01:36 -07002027
caryclark45fa4472015-01-16 07:04:10 -08002028 void reset() {
2029 fClosest = FLT_MAX;
halcanary96fcdcc2015-08-27 07:41:13 -07002030 SkDEBUGCODE(fC1Span = nullptr);
2031 SkDEBUGCODE(fC2Span = nullptr);
caryclark45fa4472015-01-16 07:04:10 -08002032 SkDEBUGCODE(fC1Index = fC2Index = -1);
2033 }
2034
2035 void update(const SkClosestRecord& mate) {
2036 fC1StartT = SkTMin(fC1StartT, mate.fC1StartT);
2037 fC1EndT = SkTMax(fC1EndT, mate.fC1EndT);
2038 fC2StartT = SkTMin(fC2StartT, mate.fC2StartT);
2039 fC2EndT = SkTMax(fC2EndT, mate.fC2EndT);
2040 }
2041
caryclark1049f122015-04-20 08:31:59 -07002042 const SkTSpan<TCurve, OppCurve>* fC1Span;
2043 const SkTSpan<OppCurve, TCurve>* fC2Span;
caryclark45fa4472015-01-16 07:04:10 -08002044 double fC1StartT;
2045 double fC1EndT;
2046 double fC2StartT;
2047 double fC2EndT;
2048 double fClosest;
2049 int fC1Index;
2050 int fC2Index;
2051};
2052
caryclark1049f122015-04-20 08:31:59 -07002053template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08002054struct SkClosestSect {
2055 SkClosestSect()
2056 : fUsed(0) {
2057 fClosest.push_back().reset();
2058 }
2059
caryclarkcdeff812016-07-22 03:34:19 -07002060 bool find(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2
2061 SkDEBUGPARAMS(SkIntersections* i)) {
caryclark1049f122015-04-20 08:31:59 -07002062 SkClosestRecord<TCurve, OppCurve>* record = &fClosest[fUsed];
caryclark45fa4472015-01-16 07:04:10 -08002063 record->findEnd(span1, span2, 0, 0);
caryclark1049f122015-04-20 08:31:59 -07002064 record->findEnd(span1, span2, 0, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002065 record->findEnd(span1, span2, TCurve::kPointLast, 0);
caryclark1049f122015-04-20 08:31:59 -07002066 record->findEnd(span1, span2, TCurve::kPointLast, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002067 if (record->fClosest == FLT_MAX) {
caryclark54359292015-03-26 07:52:43 -07002068 return false;
caryclark45fa4472015-01-16 07:04:10 -08002069 }
2070 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002071 SkClosestRecord<TCurve, OppCurve>* test = &fClosest[index];
caryclarkcdeff812016-07-22 03:34:19 -07002072 if (test->matesWith(*record SkDEBUGPARAMS(i))) {
caryclark45fa4472015-01-16 07:04:10 -08002073 if (test->fClosest > record->fClosest) {
2074 test->merge(*record);
2075 }
2076 test->update(*record);
2077 record->reset();
caryclark54359292015-03-26 07:52:43 -07002078 return false;
caryclark45fa4472015-01-16 07:04:10 -08002079 }
2080 }
2081 ++fUsed;
2082 fClosest.push_back().reset();
caryclark54359292015-03-26 07:52:43 -07002083 return true;
caryclark45fa4472015-01-16 07:04:10 -08002084 }
2085
2086 void finish(SkIntersections* intersections) const {
caryclark1049f122015-04-20 08:31:59 -07002087 SkSTArray<TCurve::kMaxIntersections * 3,
2088 const SkClosestRecord<TCurve, OppCurve>*, true> closestPtrs;
caryclark45fa4472015-01-16 07:04:10 -08002089 for (int index = 0; index < fUsed; ++index) {
caryclark54359292015-03-26 07:52:43 -07002090 closestPtrs.push_back(&fClosest[index]);
2091 }
caryclark1049f122015-04-20 08:31:59 -07002092 SkTQSort<const SkClosestRecord<TCurve, OppCurve> >(closestPtrs.begin(), closestPtrs.end()
2093 - 1);
caryclark54359292015-03-26 07:52:43 -07002094 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002095 const SkClosestRecord<TCurve, OppCurve>* test = closestPtrs[index];
caryclark54359292015-03-26 07:52:43 -07002096 test->addIntersection(intersections);
caryclark45fa4472015-01-16 07:04:10 -08002097 }
2098 }
2099
caryclark54359292015-03-26 07:52:43 -07002100 // this is oversized so that an extra records can merge into final one
caryclark1049f122015-04-20 08:31:59 -07002101 SkSTArray<TCurve::kMaxIntersections * 2, SkClosestRecord<TCurve, OppCurve>, true> fClosest;
caryclark45fa4472015-01-16 07:04:10 -08002102 int fUsed;
2103};
2104
2105// returns true if the rect is too small to consider
caryclark1049f122015-04-20 08:31:59 -07002106template<typename TCurve, typename OppCurve>
2107void SkTSect<TCurve, OppCurve>::BinarySearch(SkTSect<TCurve, OppCurve>* sect1,
2108 SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark54359292015-03-26 07:52:43 -07002109#if DEBUG_T_SECT_DUMP > 1
2110 gDumpTSectNum = 0;
2111#endif
caryclark1049f122015-04-20 08:31:59 -07002112 SkDEBUGCODE(sect1->fOppSect = sect2);
2113 SkDEBUGCODE(sect2->fOppSect = sect1);
caryclark45fa4472015-01-16 07:04:10 -08002114 intersections->reset();
caryclark94c902e2015-08-18 07:12:43 -07002115 intersections->setMax(TCurve::kMaxIntersections + 3); // give extra for slop
caryclark1049f122015-04-20 08:31:59 -07002116 SkTSpan<TCurve, OppCurve>* span1 = sect1->fHead;
2117 SkTSpan<OppCurve, TCurve>* span2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002118 int oppSect, sect = sect1->intersects(span1, sect2, span2, &oppSect);
2119// SkASSERT(between(0, sect, 2));
2120 if (!sect) {
caryclark45fa4472015-01-16 07:04:10 -08002121 return;
2122 }
caryclark54359292015-03-26 07:52:43 -07002123 if (sect == 2 && oppSect == 2) {
caryclark45fa4472015-01-16 07:04:10 -08002124 (void) EndsEqual(sect1, sect2, intersections);
2125 return;
2126 }
caryclark54359292015-03-26 07:52:43 -07002127 span1->addBounded(span2, &sect1->fHeap);
2128 span2->addBounded(span1, &sect2->fHeap);
caryclark26ad22a2015-10-16 09:03:38 -07002129 const int kMaxCoinLoopCount = 8;
2130 int coinLoopCount = kMaxCoinLoopCount;
2131 double start1s SK_INIT_TO_AVOID_WARNING;
2132 double start1e SK_INIT_TO_AVOID_WARNING;
caryclark45fa4472015-01-16 07:04:10 -08002133 do {
2134 // find the largest bounds
caryclark1049f122015-04-20 08:31:59 -07002135 SkTSpan<TCurve, OppCurve>* largest1 = sect1->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002136 if (!largest1) {
2137 break;
2138 }
caryclark1049f122015-04-20 08:31:59 -07002139 SkTSpan<OppCurve, TCurve>* largest2 = sect2->boundsMax();
caryclark6c3b9cd2016-09-26 05:36:58 -07002140 sect1->fRemovedStartT = sect1->fRemovedEndT = false;
2141 sect2->fRemovedStartT = sect2->fRemovedEndT = false;
caryclark45fa4472015-01-16 07:04:10 -08002142 // split it
caryclark1049f122015-04-20 08:31:59 -07002143 if (!largest2 || (largest1 && (largest1->fBoundsMax > largest2->fBoundsMax
2144 || (!largest1->fCollapsed && largest2->fCollapsed)))) {
2145 if (largest1->fCollapsed) {
2146 break;
2147 }
2148 // trim parts that don't intersect the opposite
2149 SkTSpan<TCurve, OppCurve>* half1 = sect1->addOne();
caryclark643ede62016-08-08 14:27:45 -07002150 SkDEBUGCODE(half1->debugSetGlobalState(sect1->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002151 if (!half1->split(largest1, &sect1->fHeap)) {
2152 break;
2153 }
2154 sect1->trim(largest1, sect2);
2155 sect1->trim(half1, sect2);
2156 } else {
2157 if (largest2->fCollapsed) {
2158 break;
2159 }
2160 // trim parts that don't intersect the opposite
2161 SkTSpan<OppCurve, TCurve>* half2 = sect2->addOne();
caryclark643ede62016-08-08 14:27:45 -07002162 SkDEBUGCODE(half2->debugSetGlobalState(sect2->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002163 if (!half2->split(largest2, &sect2->fHeap)) {
2164 break;
2165 }
2166 sect2->trim(largest2, sect1);
2167 sect2->trim(half2, sect1);
caryclark45fa4472015-01-16 07:04:10 -08002168 }
caryclark54359292015-03-26 07:52:43 -07002169 sect1->validate();
2170 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002171#if DEBUG_T_SECT_LOOP_COUNT
2172 intersections->debugBumpLoopCount(SkIntersections::kIterations_DebugLoop);
2173#endif
caryclark45fa4472015-01-16 07:04:10 -08002174 // if there are 9 or more continuous spans on both sects, suspect coincidence
2175 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2176 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
caryclark26ad22a2015-10-16 09:03:38 -07002177 if (coinLoopCount == kMaxCoinLoopCount) {
2178 start1s = sect1->fHead->fStartT;
2179 start1e = sect1->tail()->fEndT;
2180 }
caryclarkef7cee42016-09-06 09:05:54 -07002181 if (!sect1->coincidentCheck(sect2)) {
2182 return;
2183 }
caryclark54359292015-03-26 07:52:43 -07002184 sect1->validate();
2185 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002186#if DEBUG_T_SECT_LOOP_COUNT
2187 intersections->debugBumpLoopCount(SkIntersections::kCoinCheck_DebugLoop);
2188#endif
caryclarkef784fb2015-10-30 12:03:06 -07002189 if (!--coinLoopCount && sect1->fHead && sect2->fHead) {
caryclark26ad22a2015-10-16 09:03:38 -07002190 /* All known working cases resolve in two tries. Sadly, cubicConicTests[0]
2191 gets stuck in a loop. It adds an extension to allow a coincident end
2192 perpendicular to track its intersection in the opposite curve. However,
2193 the bounding box of the extension does not intersect the original curve,
caryclark55888e42016-07-18 10:01:36 -07002194 so the extension is discarded, only to be added again the next time around. */
caryclark26ad22a2015-10-16 09:03:38 -07002195 sect1->coincidentForce(sect2, start1s, start1e);
2196 sect1->validate();
2197 sect2->validate();
2198 }
caryclark45fa4472015-01-16 07:04:10 -08002199 }
caryclark54359292015-03-26 07:52:43 -07002200 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2201 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
2202 sect1->computePerpendiculars(sect2, sect1->fHead, sect1->tail());
2203 sect2->computePerpendiculars(sect1, sect2->fHead, sect2->tail());
2204 sect1->removeByPerpendicular(sect2);
2205 sect1->validate();
2206 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002207#if DEBUG_T_SECT_LOOP_COUNT
2208 intersections->debugBumpLoopCount(SkIntersections::kComputePerp_DebugLoop);
2209#endif
caryclark1049f122015-04-20 08:31:59 -07002210 if (sect1->collapsed() > TCurve::kMaxIntersections) {
2211 break;
2212 }
caryclark54359292015-03-26 07:52:43 -07002213 }
2214#if DEBUG_T_SECT_DUMP
2215 sect1->dumpBoth(sect2);
caryclark45fa4472015-01-16 07:04:10 -08002216#endif
2217 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002218 break;
caryclark45fa4472015-01-16 07:04:10 -08002219 }
2220 } while (true);
caryclark1049f122015-04-20 08:31:59 -07002221 SkTSpan<TCurve, OppCurve>* coincident = sect1->fCoincident;
caryclark54359292015-03-26 07:52:43 -07002222 if (coincident) {
2223 // if there is more than one coincident span, check loosely to see if they should be joined
2224 if (coincident->fNext) {
2225 sect1->mergeCoincidence(sect2);
2226 coincident = sect1->fCoincident;
2227 }
2228 SkASSERT(sect2->fCoincident); // courtesy check : coincidence only looks at sect 1
caryclark45fa4472015-01-16 07:04:10 -08002229 do {
caryclark221a4bb2016-10-07 11:15:15 -07002230 if (!coincident) {
2231 return;
2232 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002233 if (!coincident->fCoinStart.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002234 continue;
2235 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002236 if (!coincident->fCoinEnd.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002237 continue;
2238 }
caryclark54359292015-03-26 07:52:43 -07002239 int index = intersections->insertCoincident(coincident->fStartT,
2240 coincident->fCoinStart.perpT(), coincident->fPart[0]);
2241 if ((intersections->insertCoincident(coincident->fEndT,
2242 coincident->fCoinEnd.perpT(),
2243 coincident->fPart[TCurve::kPointLast]) < 0) && index >= 0) {
caryclark45fa4472015-01-16 07:04:10 -08002244 intersections->clearCoincidence(index);
2245 }
caryclark54359292015-03-26 07:52:43 -07002246 } while ((coincident = coincident->fNext));
2247 }
caryclark1049f122015-04-20 08:31:59 -07002248 int zeroOneSet = EndsEqual(sect1, sect2, intersections);
caryclark54359292015-03-26 07:52:43 -07002249 if (!sect1->fHead || !sect2->fHead) {
caryclark6c3b9cd2016-09-26 05:36:58 -07002250 // if the final iteration contains an end (0 or 1),
2251 if (sect1->fRemovedStartT && !(zeroOneSet & kZeroS1Set)) {
2252 SkTCoincident<TCurve, OppCurve> perp; // intersect perpendicular with opposite curve
2253 perp.setPerp(sect1->fCurve, 0, sect1->fCurve.fPts[0], sect2->fCurve);
2254 if (perp.isMatch()) {
2255 intersections->insert(0, perp.perpT(), perp.perpPt());
2256 }
2257 }
2258 if (sect1->fRemovedEndT && !(zeroOneSet & kOneS1Set)) {
2259 SkTCoincident<TCurve, OppCurve> perp;
2260 perp.setPerp(sect1->fCurve, 1, sect1->fCurve.fPts[TCurve::kPointLast], sect2->fCurve);
2261 if (perp.isMatch()) {
2262 intersections->insert(1, perp.perpT(), perp.perpPt());
2263 }
2264 }
2265 if (sect2->fRemovedStartT && !(zeroOneSet & kZeroS2Set)) {
2266 SkTCoincident<OppCurve, TCurve> perp;
2267 perp.setPerp(sect2->fCurve, 0, sect2->fCurve.fPts[0], sect1->fCurve);
2268 if (perp.isMatch()) {
2269 intersections->insert(perp.perpT(), 0, perp.perpPt());
2270 }
2271 }
2272 if (sect2->fRemovedEndT && !(zeroOneSet & kOneS2Set)) {
2273 SkTCoincident<OppCurve, TCurve> perp;
2274 perp.setPerp(sect2->fCurve, 1, sect2->fCurve.fPts[OppCurve::kPointLast], sect1->fCurve);
2275 if (perp.isMatch()) {
2276 intersections->insert(perp.perpT(), 1, perp.perpPt());
2277 }
2278 }
caryclark54359292015-03-26 07:52:43 -07002279 return;
caryclark45fa4472015-01-16 07:04:10 -08002280 }
caryclark45fa4472015-01-16 07:04:10 -08002281 sect1->recoverCollapsed();
2282 sect2->recoverCollapsed();
caryclark1049f122015-04-20 08:31:59 -07002283 SkTSpan<TCurve, OppCurve>* result1 = sect1->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002284 // check heads and tails for zero and ones and insert them if we haven't already done so
caryclark1049f122015-04-20 08:31:59 -07002285 const SkTSpan<TCurve, OppCurve>* head1 = result1;
caryclark45fa4472015-01-16 07:04:10 -08002286 if (!(zeroOneSet & kZeroS1Set) && approximately_less_than_zero(head1->fStartT)) {
2287 const SkDPoint& start1 = sect1->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002288 if (head1->isBounded()) {
2289 double t = head1->closestBoundedT(start1);
2290 if (sect2->fCurve.ptAtT(t).approximatelyEqual(start1)) {
2291 intersections->insert(0, t, start1);
2292 }
caryclark45fa4472015-01-16 07:04:10 -08002293 }
2294 }
caryclark1049f122015-04-20 08:31:59 -07002295 const SkTSpan<OppCurve, TCurve>* head2 = sect2->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002296 if (!(zeroOneSet & kZeroS2Set) && approximately_less_than_zero(head2->fStartT)) {
2297 const SkDPoint& start2 = sect2->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002298 if (head2->isBounded()) {
2299 double t = head2->closestBoundedT(start2);
2300 if (sect1->fCurve.ptAtT(t).approximatelyEqual(start2)) {
2301 intersections->insert(t, 0, start2);
2302 }
caryclark45fa4472015-01-16 07:04:10 -08002303 }
2304 }
caryclark1049f122015-04-20 08:31:59 -07002305 const SkTSpan<TCurve, OppCurve>* tail1 = sect1->tail();
caryclark45fa4472015-01-16 07:04:10 -08002306 if (!(zeroOneSet & kOneS1Set) && approximately_greater_than_one(tail1->fEndT)) {
2307 const SkDPoint& end1 = sect1->fCurve[TCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002308 if (tail1->isBounded()) {
2309 double t = tail1->closestBoundedT(end1);
2310 if (sect2->fCurve.ptAtT(t).approximatelyEqual(end1)) {
2311 intersections->insert(1, t, end1);
2312 }
caryclark45fa4472015-01-16 07:04:10 -08002313 }
2314 }
caryclark1049f122015-04-20 08:31:59 -07002315 const SkTSpan<OppCurve, TCurve>* tail2 = sect2->tail();
caryclark45fa4472015-01-16 07:04:10 -08002316 if (!(zeroOneSet & kOneS2Set) && approximately_greater_than_one(tail2->fEndT)) {
caryclark1049f122015-04-20 08:31:59 -07002317 const SkDPoint& end2 = sect2->fCurve[OppCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002318 if (tail2->isBounded()) {
2319 double t = tail2->closestBoundedT(end2);
2320 if (sect1->fCurve.ptAtT(t).approximatelyEqual(end2)) {
2321 intersections->insert(t, 1, end2);
2322 }
caryclark45fa4472015-01-16 07:04:10 -08002323 }
2324 }
caryclark1049f122015-04-20 08:31:59 -07002325 SkClosestSect<TCurve, OppCurve> closest;
caryclark45fa4472015-01-16 07:04:10 -08002326 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07002327 while (result1 && result1->fCoinStart.isMatch() && result1->fCoinEnd.isMatch()) {
caryclark45fa4472015-01-16 07:04:10 -08002328 result1 = result1->fNext;
2329 }
2330 if (!result1) {
2331 break;
2332 }
caryclark1049f122015-04-20 08:31:59 -07002333 SkTSpan<OppCurve, TCurve>* result2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002334 bool found = false;
caryclark45fa4472015-01-16 07:04:10 -08002335 while (result2) {
caryclarkcdeff812016-07-22 03:34:19 -07002336 found |= closest.find(result1, result2 SkDEBUGPARAMS(intersections));
caryclark45fa4472015-01-16 07:04:10 -08002337 result2 = result2->fNext;
2338 }
caryclark45fa4472015-01-16 07:04:10 -08002339 } while ((result1 = result1->fNext));
2340 closest.finish(intersections);
caryclark54359292015-03-26 07:52:43 -07002341 // if there is more than one intersection and it isn't already coincident, check
2342 int last = intersections->used() - 1;
2343 for (int index = 0; index < last; ) {
2344 if (intersections->isCoincident(index) && intersections->isCoincident(index + 1)) {
2345 ++index;
2346 continue;
2347 }
2348 double midT = ((*intersections)[0][index] + (*intersections)[0][index + 1]) / 2;
2349 SkDPoint midPt = sect1->fCurve.ptAtT(midT);
2350 // intersect perpendicular with opposite curve
caryclark1049f122015-04-20 08:31:59 -07002351 SkTCoincident<TCurve, OppCurve> perp;
caryclark54359292015-03-26 07:52:43 -07002352 perp.setPerp(sect1->fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002353 if (!perp.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07002354 ++index;
2355 continue;
2356 }
2357 if (intersections->isCoincident(index)) {
2358 intersections->removeOne(index);
2359 --last;
2360 } else if (intersections->isCoincident(index + 1)) {
2361 intersections->removeOne(index + 1);
2362 --last;
2363 } else {
2364 intersections->setCoincident(index++);
2365 }
2366 intersections->setCoincident(index);
2367 }
2368 SkASSERT(intersections->used() <= TCurve::kMaxIntersections);
caryclark45fa4472015-01-16 07:04:10 -08002369}
deanm12670eb2016-04-26 14:09:01 -07002370
2371#endif