blob: 5d74c9a998181a06347eb2a6877e3917b65d5b05 [file] [log] [blame]
caryclark45fa4472015-01-16 07:04:10 -08001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
deanm12670eb2016-04-26 14:09:01 -07007#ifndef SkPathOpsTSect_DEFINED
8#define SkPathOpsTSect_DEFINED
caryclark45fa4472015-01-16 07:04:10 -08009
10#include "SkChunkAlloc.h"
caryclark54359292015-03-26 07:52:43 -070011#include "SkPathOpsBounds.h"
caryclark45fa4472015-01-16 07:04:10 -080012#include "SkPathOpsRect.h"
caryclark45fa4472015-01-16 07:04:10 -080013#include "SkIntersections.h"
caryclark54359292015-03-26 07:52:43 -070014#include "SkTSort.h"
caryclark45fa4472015-01-16 07:04:10 -080015
caryclarked0935a2015-10-22 07:23:52 -070016#ifdef SK_DEBUG
17typedef uint8_t SkOpDebugBool;
18#else
19typedef bool SkOpDebugBool;
20#endif
21
caryclark1049f122015-04-20 08:31:59 -070022/* TCurve and OppCurve are one of { SkDQuadratic, SkDConic, SkDCubic } */
23template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -080024class SkTCoincident {
25public:
caryclark697ac1c2015-04-13 09:36:01 -070026 SkTCoincident() {
caryclarkdf386c52015-04-21 05:27:02 -070027 this->init();
caryclark1049f122015-04-20 08:31:59 -070028 }
29
caryclarked0935a2015-10-22 07:23:52 -070030 void debugInit() {
31#ifdef SK_DEBUG
32 this->fPerpPt.fX = this->fPerpPt.fY = SK_ScalarNaN;
33 this->fPerpT = SK_ScalarNaN;
caryclark6c3b9cd2016-09-26 05:36:58 -070034 this->fMatch = 0xFF;
caryclarked0935a2015-10-22 07:23:52 -070035#endif
36 }
37
38 char dumpIsCoincidentStr() const;
caryclark1049f122015-04-20 08:31:59 -070039 void dump() const;
40
caryclark6c3b9cd2016-09-26 05:36:58 -070041 bool isMatch() const {
42 SkASSERT(!!fMatch == fMatch);
43 return SkToBool(fMatch);
caryclark45fa4472015-01-16 07:04:10 -080044 }
45
46 void init() {
caryclarkdf386c52015-04-21 05:27:02 -070047 fPerpT = -1;
caryclark6c3b9cd2016-09-26 05:36:58 -070048 fMatch = false;
caryclarkdf386c52015-04-21 05:27:02 -070049 fPerpPt.fX = fPerpPt.fY = SK_ScalarNaN;
caryclark45fa4472015-01-16 07:04:10 -080050 }
51
52 void markCoincident() {
caryclark6c3b9cd2016-09-26 05:36:58 -070053 if (!fMatch) {
caryclark45fa4472015-01-16 07:04:10 -080054 fPerpT = -1;
55 }
caryclark6c3b9cd2016-09-26 05:36:58 -070056 fMatch = true;
caryclark45fa4472015-01-16 07:04:10 -080057 }
58
59 const SkDPoint& perpPt() const {
60 return fPerpPt;
61 }
62
63 double perpT() const {
64 return fPerpT;
65 }
66
caryclark1049f122015-04-20 08:31:59 -070067 void setPerp(const TCurve& c1, double t, const SkDPoint& cPt, const OppCurve& );
caryclark45fa4472015-01-16 07:04:10 -080068
69private:
70 SkDPoint fPerpPt;
71 double fPerpT; // perpendicular intersection on opposite curve
caryclark6c3b9cd2016-09-26 05:36:58 -070072 SkOpDebugBool fMatch;
caryclark45fa4472015-01-16 07:04:10 -080073};
74
caryclark1049f122015-04-20 08:31:59 -070075template<typename TCurve, typename OppCurve> class SkTSect;
76template<typename TCurve, typename OppCurve> class SkTSpan;
caryclark54359292015-03-26 07:52:43 -070077
caryclark1049f122015-04-20 08:31:59 -070078template<typename TCurve, typename OppCurve>
caryclark54359292015-03-26 07:52:43 -070079struct SkTSpanBounded {
caryclark1049f122015-04-20 08:31:59 -070080 SkTSpan<TCurve, OppCurve>* fBounded;
caryclark54359292015-03-26 07:52:43 -070081 SkTSpanBounded* fNext;
82};
caryclark45fa4472015-01-16 07:04:10 -080083
84/* Curve is either TCurve or SkDCubic */
caryclark1049f122015-04-20 08:31:59 -070085template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -080086class SkTSpan {
87public:
caryclark1049f122015-04-20 08:31:59 -070088 void addBounded(SkTSpan<OppCurve, TCurve>* , SkChunkAlloc* );
reed0dc4dd62015-03-24 13:55:33 -070089 double closestBoundedT(const SkDPoint& pt) const;
caryclark54359292015-03-26 07:52:43 -070090 bool contains(double t) const;
reed0dc4dd62015-03-24 13:55:33 -070091
caryclark1049f122015-04-20 08:31:59 -070092 void debugInit() {
93 TCurve dummy;
94 dummy.debugInit();
95 init(dummy);
96 initBounds(dummy);
caryclarkdf386c52015-04-21 05:27:02 -070097 fCoinStart.init();
98 fCoinEnd.init();
caryclark1049f122015-04-20 08:31:59 -070099 }
100
101 const SkTSect<OppCurve, TCurve>* debugOpp() const;
caryclark643ede62016-08-08 14:27:45 -0700102
103#ifdef SK_DEBUG
104 void debugSetGlobalState(SkOpGlobalState* state) {
105 fDebugGlobalState = state;
106 }
107#endif
108
caryclark54359292015-03-26 07:52:43 -0700109 const SkTSpan* debugSpan(int ) const;
110 const SkTSpan* debugT(double t) const;
111#ifdef SK_DEBUG
112 bool debugIsBefore(const SkTSpan* span) const;
113#endif
114 void dump() const;
caryclark26ad22a2015-10-16 09:03:38 -0700115 void dumpAll() const;
caryclark1049f122015-04-20 08:31:59 -0700116 void dumpBounded(int id) const;
117 void dumpBounds() const;
118 void dumpCoin() const;
caryclark45fa4472015-01-16 07:04:10 -0800119
120 double endT() const {
121 return fEndT;
122 }
123
caryclark1049f122015-04-20 08:31:59 -0700124 SkTSpan<OppCurve, TCurve>* findOppSpan(const SkTSpan<OppCurve, TCurve>* opp) const;
caryclark54359292015-03-26 07:52:43 -0700125
caryclark1049f122015-04-20 08:31:59 -0700126 SkTSpan<OppCurve, TCurve>* findOppT(double t) const {
127 SkTSpan<OppCurve, TCurve>* result = oppT(t);
caryclark643ede62016-08-08 14:27:45 -0700128 SkOPASSERT(result);
caryclark45fa4472015-01-16 07:04:10 -0800129 return result;
130 }
131
caryclark643ede62016-08-08 14:27:45 -0700132 SkDEBUGCODE(SkOpGlobalState* globalState() const { return fDebugGlobalState; })
133
caryclark54359292015-03-26 07:52:43 -0700134 bool hasOppT(double t) const {
135 return SkToBool(oppT(t));
136 }
137
caryclark1049f122015-04-20 08:31:59 -0700138 int hullsIntersect(SkTSpan<OppCurve, TCurve>* span, bool* start, bool* oppStart);
caryclark54359292015-03-26 07:52:43 -0700139 void init(const TCurve& );
caryclarka35ab3e2016-10-20 08:32:18 -0700140 bool initBounds(const TCurve& );
caryclark54359292015-03-26 07:52:43 -0700141
142 bool isBounded() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700143 return fBounded != nullptr;
caryclark54359292015-03-26 07:52:43 -0700144 }
145
caryclark1049f122015-04-20 08:31:59 -0700146 bool linearsIntersect(SkTSpan<OppCurve, TCurve>* span);
caryclark54359292015-03-26 07:52:43 -0700147 double linearT(const SkDPoint& ) const;
148
149 void markCoincident() {
150 fCoinStart.markCoincident();
151 fCoinEnd.markCoincident();
152 }
caryclark45fa4472015-01-16 07:04:10 -0800153
154 const SkTSpan* next() const {
155 return fNext;
156 }
157
caryclark1049f122015-04-20 08:31:59 -0700158 bool onlyEndPointsInCommon(const SkTSpan<OppCurve, TCurve>* opp, bool* start,
159 bool* oppStart, bool* ptsInCommon);
caryclark54359292015-03-26 07:52:43 -0700160
caryclark45fa4472015-01-16 07:04:10 -0800161 const TCurve& part() const {
162 return fPart;
163 }
164
caryclark54359292015-03-26 07:52:43 -0700165 bool removeAllBounded();
caryclark1049f122015-04-20 08:31:59 -0700166 bool removeBounded(const SkTSpan<OppCurve, TCurve>* opp);
caryclark54359292015-03-26 07:52:43 -0700167
caryclark45fa4472015-01-16 07:04:10 -0800168 void reset() {
halcanary96fcdcc2015-08-27 07:41:13 -0700169 fBounded = nullptr;
caryclark45fa4472015-01-16 07:04:10 -0800170 }
171
caryclark54359292015-03-26 07:52:43 -0700172 void resetBounds(const TCurve& curve) {
173 fIsLinear = fIsLine = false;
174 initBounds(curve);
caryclark45fa4472015-01-16 07:04:10 -0800175 }
176
caryclark54359292015-03-26 07:52:43 -0700177 bool split(SkTSpan* work, SkChunkAlloc* heap) {
178 return splitAt(work, (work->fStartT + work->fEndT) * 0.5, heap);
179 }
180
181 bool splitAt(SkTSpan* work, double t, SkChunkAlloc* heap);
caryclark45fa4472015-01-16 07:04:10 -0800182
183 double startT() const {
184 return fStartT;
185 }
186
caryclark54359292015-03-26 07:52:43 -0700187private:
caryclark45fa4472015-01-16 07:04:10 -0800188
189 // implementation is for testing only
caryclark54359292015-03-26 07:52:43 -0700190 int debugID() const {
191 return PATH_OPS_DEBUG_T_SECT_RELEASE(fID, -1);
caryclark45fa4472015-01-16 07:04:10 -0800192 }
193
caryclark54359292015-03-26 07:52:43 -0700194 void dumpID() const;
caryclark45fa4472015-01-16 07:04:10 -0800195
caryclark1049f122015-04-20 08:31:59 -0700196 int hullCheck(const SkTSpan<OppCurve, TCurve>* opp, bool* start, bool* oppStart);
197 int linearIntersects(const OppCurve& ) const;
198 SkTSpan<OppCurve, TCurve>* oppT(double t) const;
caryclark45fa4472015-01-16 07:04:10 -0800199
caryclark45fa4472015-01-16 07:04:10 -0800200 void validate() const;
caryclark54359292015-03-26 07:52:43 -0700201 void validateBounded() const;
202 void validatePerpT(double oppT) const;
203 void validatePerpPt(double t, const SkDPoint& ) const;
caryclark45fa4472015-01-16 07:04:10 -0800204
205 TCurve fPart;
caryclark1049f122015-04-20 08:31:59 -0700206 SkTCoincident<TCurve, OppCurve> fCoinStart;
207 SkTCoincident<TCurve, OppCurve> fCoinEnd;
208 SkTSpanBounded<OppCurve, TCurve>* fBounded;
caryclark45fa4472015-01-16 07:04:10 -0800209 SkTSpan* fPrev;
210 SkTSpan* fNext;
211 SkDRect fBounds;
212 double fStartT;
213 double fEndT;
214 double fBoundsMax;
caryclarked0935a2015-10-22 07:23:52 -0700215 SkOpDebugBool fCollapsed;
216 SkOpDebugBool fHasPerp;
217 SkOpDebugBool fIsLinear;
218 SkOpDebugBool fIsLine;
219 SkOpDebugBool fDeleted;
caryclark643ede62016-08-08 14:27:45 -0700220 SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState);
csmartdaltonceeaa782016-08-10 10:07:57 -0700221 SkDEBUGCODE(SkTSect<TCurve, OppCurve>* fDebugSect);
caryclark54359292015-03-26 07:52:43 -0700222 PATH_OPS_DEBUG_T_SECT_CODE(int fID);
caryclark1049f122015-04-20 08:31:59 -0700223 friend class SkTSect<TCurve, OppCurve>;
224 friend class SkTSect<OppCurve, TCurve>;
225 friend class SkTSpan<OppCurve, TCurve>;
caryclark45fa4472015-01-16 07:04:10 -0800226};
227
caryclark1049f122015-04-20 08:31:59 -0700228template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -0800229class SkTSect {
230public:
caryclarke25a4f62016-07-26 09:26:29 -0700231 SkTSect(const TCurve& c SkDEBUGPARAMS(SkOpGlobalState* ) PATH_OPS_DEBUG_T_SECT_PARAMS(int id));
caryclark1049f122015-04-20 08:31:59 -0700232 static void BinarySearch(SkTSect* sect1, SkTSect<OppCurve, TCurve>* sect2,
233 SkIntersections* intersections);
caryclark45fa4472015-01-16 07:04:10 -0800234
caryclarke25a4f62016-07-26 09:26:29 -0700235 SkDEBUGCODE(SkOpGlobalState* globalState() { return fDebugGlobalState; })
caryclark45fa4472015-01-16 07:04:10 -0800236 // for testing only
caryclark1049f122015-04-20 08:31:59 -0700237 bool debugHasBounded(const SkTSpan<OppCurve, TCurve>* ) const;
caryclark54359292015-03-26 07:52:43 -0700238
caryclark1049f122015-04-20 08:31:59 -0700239 const SkTSect<OppCurve, TCurve>* debugOpp() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700240 return SkDEBUGRELEASE(fOppSect, nullptr);
caryclark54359292015-03-26 07:52:43 -0700241 }
242
caryclark1049f122015-04-20 08:31:59 -0700243 const SkTSpan<TCurve, OppCurve>* debugSpan(int id) const;
244 const SkTSpan<TCurve, OppCurve>* debugT(double t) const;
caryclark45fa4472015-01-16 07:04:10 -0800245 void dump() const;
caryclark1049f122015-04-20 08:31:59 -0700246 void dumpBoth(SkTSect<OppCurve, TCurve>* ) const;
247 void dumpBounded(int id) const;
248 void dumpBounds() const;
caryclark54359292015-03-26 07:52:43 -0700249 void dumpCoin() const;
250 void dumpCoinCurves() const;
caryclark45fa4472015-01-16 07:04:10 -0800251 void dumpCurves() const;
252
253private:
254 enum {
255 kZeroS1Set = 1,
256 kOneS1Set = 2,
257 kZeroS2Set = 4,
258 kOneS2Set = 8
259 };
260
caryclark1049f122015-04-20 08:31:59 -0700261 SkTSpan<TCurve, OppCurve>* addFollowing(SkTSpan<TCurve, OppCurve>* prior);
262 void addForPerp(SkTSpan<OppCurve, TCurve>* span, double t);
263 SkTSpan<TCurve, OppCurve>* addOne();
caryclark55888e42016-07-18 10:01:36 -0700264
caryclark1049f122015-04-20 08:31:59 -0700265 SkTSpan<TCurve, OppCurve>* addSplitAt(SkTSpan<TCurve, OppCurve>* span, double t) {
266 SkTSpan<TCurve, OppCurve>* result = this->addOne();
caryclark643ede62016-08-08 14:27:45 -0700267 SkDEBUGCODE(result->debugSetGlobalState(this->globalState()));
caryclark54359292015-03-26 07:52:43 -0700268 result->splitAt(span, t, &fHeap);
269 result->initBounds(fCurve);
270 span->initBounds(fCurve);
271 return result;
272 }
273
caryclark1049f122015-04-20 08:31:59 -0700274 bool binarySearchCoin(SkTSect<OppCurve, TCurve>* , double tStart, double tStep, double* t,
275 double* oppT);
276 SkTSpan<TCurve, OppCurve>* boundsMax() const;
caryclarkef7cee42016-09-06 09:05:54 -0700277 bool coincidentCheck(SkTSect<OppCurve, TCurve>* sect2);
caryclark26ad22a2015-10-16 09:03:38 -0700278 void coincidentForce(SkTSect<OppCurve, TCurve>* sect2, double start1s, double start1e);
caryclark54359292015-03-26 07:52:43 -0700279 bool coincidentHasT(double t);
caryclark1049f122015-04-20 08:31:59 -0700280 int collapsed() const;
281 void computePerpendiculars(SkTSect<OppCurve, TCurve>* sect2, SkTSpan<TCurve, OppCurve>* first,
282 SkTSpan<TCurve, OppCurve>* last);
283 int countConsecutiveSpans(SkTSpan<TCurve, OppCurve>* first,
284 SkTSpan<TCurve, OppCurve>** last) const;
caryclarkccec0f92015-03-24 07:28:17 -0700285
caryclark54359292015-03-26 07:52:43 -0700286 int debugID() const {
287 return PATH_OPS_DEBUG_T_SECT_RELEASE(fID, -1);
288 }
289
caryclarkef7cee42016-09-06 09:05:54 -0700290 bool deleteEmptySpans();
caryclark1049f122015-04-20 08:31:59 -0700291 void dumpCommon(const SkTSpan<TCurve, OppCurve>* ) const;
292 void dumpCommonCurves(const SkTSpan<TCurve, OppCurve>* ) const;
293 static int EndsEqual(const SkTSect* sect1, const SkTSect<OppCurve, TCurve>* sect2,
294 SkIntersections* );
caryclarkef7cee42016-09-06 09:05:54 -0700295 bool extractCoincident(SkTSect<OppCurve, TCurve>* sect2, SkTSpan<TCurve, OppCurve>* first,
296 SkTSpan<TCurve, OppCurve>* last, SkTSpan<TCurve, OppCurve>** result);
caryclark1049f122015-04-20 08:31:59 -0700297 SkTSpan<TCurve, OppCurve>* findCoincidentRun(SkTSpan<TCurve, OppCurve>* first,
298 SkTSpan<TCurve, OppCurve>** lastPtr);
299 int intersects(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp,
300 SkTSpan<OppCurve, TCurve>* oppSpan, int* oppResult);
caryclarked0935a2015-10-22 07:23:52 -0700301 bool isParallel(const SkDLine& thisLine, const SkTSect<OppCurve, TCurve>* opp) const;
caryclark1049f122015-04-20 08:31:59 -0700302 int linesIntersect(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp,
303 SkTSpan<OppCurve, TCurve>* oppSpan, SkIntersections* );
caryclarkef7cee42016-09-06 09:05:54 -0700304 bool markSpanGone(SkTSpan<TCurve, OppCurve>* span);
caryclark1049f122015-04-20 08:31:59 -0700305 bool matchedDirection(double t, const SkTSect<OppCurve, TCurve>* sect2, double t2) const;
306 void matchedDirCheck(double t, const SkTSect<OppCurve, TCurve>* sect2, double t2,
caryclark54359292015-03-26 07:52:43 -0700307 bool* calcMatched, bool* oppMatched) const;
caryclark1049f122015-04-20 08:31:59 -0700308 void mergeCoincidence(SkTSect<OppCurve, TCurve>* sect2);
309 SkTSpan<TCurve, OppCurve>* prev(SkTSpan<TCurve, OppCurve>* ) const;
310 void removeByPerpendicular(SkTSect<OppCurve, TCurve>* opp);
caryclark54359292015-03-26 07:52:43 -0700311 void recoverCollapsed();
caryclark1049f122015-04-20 08:31:59 -0700312 void removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween);
313 void removeAllBut(const SkTSpan<OppCurve, TCurve>* keep, SkTSpan<TCurve, OppCurve>* span,
314 SkTSect<OppCurve, TCurve>* opp);
caryclarkef7cee42016-09-06 09:05:54 -0700315 bool removeSpan(SkTSpan<TCurve, OppCurve>* span);
caryclark1049f122015-04-20 08:31:59 -0700316 void removeSpanRange(SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last);
317 void removeSpans(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp);
caryclark34efb702016-10-24 08:19:06 -0700318 void removedEndCheck(SkTSpan<TCurve, OppCurve>* span);
319
320 void resetRemovedEnds() {
321 fRemovedStartT = fRemovedEndT = false;
322 }
323
caryclark1049f122015-04-20 08:31:59 -0700324 SkTSpan<TCurve, OppCurve>* spanAtT(double t, SkTSpan<TCurve, OppCurve>** priorSpan);
325 SkTSpan<TCurve, OppCurve>* tail();
caryclarka35ab3e2016-10-20 08:32:18 -0700326 bool trim(SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp);
caryclark1049f122015-04-20 08:31:59 -0700327 void unlinkSpan(SkTSpan<TCurve, OppCurve>* span);
328 bool updateBounded(SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last,
329 SkTSpan<OppCurve, TCurve>* oppFirst);
reed0dc4dd62015-03-24 13:55:33 -0700330 void validate() const;
caryclark54359292015-03-26 07:52:43 -0700331 void validateBounded() const;
332
caryclark45fa4472015-01-16 07:04:10 -0800333 const TCurve& fCurve;
334 SkChunkAlloc fHeap;
caryclark1049f122015-04-20 08:31:59 -0700335 SkTSpan<TCurve, OppCurve>* fHead;
336 SkTSpan<TCurve, OppCurve>* fCoincident;
337 SkTSpan<TCurve, OppCurve>* fDeleted;
caryclark45fa4472015-01-16 07:04:10 -0800338 int fActiveCount;
caryclark6c3b9cd2016-09-26 05:36:58 -0700339 bool fRemovedStartT;
340 bool fRemovedEndT;
caryclarke25a4f62016-07-26 09:26:29 -0700341 SkDEBUGCODE(SkOpGlobalState* fDebugGlobalState);
csmartdaltonceeaa782016-08-10 10:07:57 -0700342 SkDEBUGCODE(SkTSect<OppCurve, TCurve>* fOppSect);
caryclark54359292015-03-26 07:52:43 -0700343 PATH_OPS_DEBUG_T_SECT_CODE(int fID);
344 PATH_OPS_DEBUG_T_SECT_CODE(int fDebugCount);
caryclark45fa4472015-01-16 07:04:10 -0800345#if DEBUG_T_SECT
caryclark45fa4472015-01-16 07:04:10 -0800346 int fDebugAllocatedCount;
347#endif
caryclark1049f122015-04-20 08:31:59 -0700348 friend class SkTSpan<TCurve, OppCurve>;
349 friend class SkTSpan<OppCurve, TCurve>;
350 friend class SkTSect<OppCurve, TCurve>;
caryclark45fa4472015-01-16 07:04:10 -0800351};
352
353#define COINCIDENT_SPAN_COUNT 9
354
caryclark1049f122015-04-20 08:31:59 -0700355template<typename TCurve, typename OppCurve>
356void SkTCoincident<TCurve, OppCurve>::setPerp(const TCurve& c1, double t,
357 const SkDPoint& cPt, const OppCurve& c2) {
caryclark45fa4472015-01-16 07:04:10 -0800358 SkDVector dxdy = c1.dxdyAtT(t);
359 SkDLine perp = {{ cPt, {cPt.fX + dxdy.fY, cPt.fY - dxdy.fX} }};
caryclarka35ab3e2016-10-20 08:32:18 -0700360 SkIntersections i SkDEBUGCODE((c1.globalState()));
caryclark45fa4472015-01-16 07:04:10 -0800361 int used = i.intersectRay(c2, perp);
362 // only keep closest
caryclark54359292015-03-26 07:52:43 -0700363 if (used == 0 || used == 3) {
caryclarkdf386c52015-04-21 05:27:02 -0700364 this->init();
caryclark45fa4472015-01-16 07:04:10 -0800365 return;
caryclark55888e42016-07-18 10:01:36 -0700366 }
caryclark45fa4472015-01-16 07:04:10 -0800367 fPerpT = i[0][0];
368 fPerpPt = i.pt(0);
369 SkASSERT(used <= 2);
370 if (used == 2) {
371 double distSq = (fPerpPt - cPt).lengthSquared();
372 double dist2Sq = (i.pt(1) - cPt).lengthSquared();
373 if (dist2Sq < distSq) {
374 fPerpT = i[0][1];
375 fPerpPt = i.pt(1);
376 }
377 }
caryclark54359292015-03-26 07:52:43 -0700378#if DEBUG_T_SECT
caryclark1049f122015-04-20 08:31:59 -0700379 SkDebugf("setPerp t=%1.9g cPt=(%1.9g,%1.9g) %s oppT=%1.9g fPerpPt=(%1.9g,%1.9g)\n",
380 t, cPt.fX, cPt.fY,
381 cPt.approximatelyEqual(fPerpPt) ? "==" : "!=", fPerpT, fPerpPt.fX, fPerpPt.fY);
caryclark54359292015-03-26 07:52:43 -0700382#endif
caryclark6c3b9cd2016-09-26 05:36:58 -0700383 fMatch = cPt.approximatelyEqual(fPerpPt);
caryclark45fa4472015-01-16 07:04:10 -0800384#if DEBUG_T_SECT
caryclark6c3b9cd2016-09-26 05:36:58 -0700385 if (fMatch) {
caryclark45fa4472015-01-16 07:04:10 -0800386 SkDebugf(""); // allow setting breakpoint
387 }
388#endif
389}
390
caryclark1049f122015-04-20 08:31:59 -0700391template<typename TCurve, typename OppCurve>
392void SkTSpan<TCurve, OppCurve>::addBounded(SkTSpan<OppCurve, TCurve>* span, SkChunkAlloc* heap) {
halcanary385fe4d2015-08-26 13:07:48 -0700393 SkTSpanBounded<OppCurve, TCurve>* bounded = new (heap->allocThrow(
394 sizeof(SkTSpanBounded<OppCurve, TCurve>)))(SkTSpanBounded<OppCurve, TCurve>);
caryclark54359292015-03-26 07:52:43 -0700395 bounded->fBounded = span;
396 bounded->fNext = fBounded;
397 fBounded = bounded;
398}
399
caryclark1049f122015-04-20 08:31:59 -0700400template<typename TCurve, typename OppCurve>
401SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::addFollowing(
402 SkTSpan<TCurve, OppCurve>* prior) {
403 SkTSpan<TCurve, OppCurve>* result = this->addOne();
caryclark643ede62016-08-08 14:27:45 -0700404 SkDEBUGCODE(result->debugSetGlobalState(this->globalState()));
caryclark54359292015-03-26 07:52:43 -0700405 result->fStartT = prior ? prior->fEndT : 0;
caryclark1049f122015-04-20 08:31:59 -0700406 SkTSpan<TCurve, OppCurve>* next = prior ? prior->fNext : fHead;
caryclark54359292015-03-26 07:52:43 -0700407 result->fEndT = next ? next->fStartT : 1;
408 result->fPrev = prior;
409 result->fNext = next;
410 if (prior) {
411 prior->fNext = result;
412 } else {
413 fHead = result;
414 }
415 if (next) {
416 next->fPrev = result;
417 }
418 result->resetBounds(fCurve);
caryclark643ede62016-08-08 14:27:45 -0700419 result->validate();
caryclark54359292015-03-26 07:52:43 -0700420 return result;
421}
422
caryclark1049f122015-04-20 08:31:59 -0700423template<typename TCurve, typename OppCurve>
424void SkTSect<TCurve, OppCurve>::addForPerp(SkTSpan<OppCurve, TCurve>* span, double t) {
caryclark54359292015-03-26 07:52:43 -0700425 if (!span->hasOppT(t)) {
caryclark1049f122015-04-20 08:31:59 -0700426 SkTSpan<TCurve, OppCurve>* priorSpan;
427 SkTSpan<TCurve, OppCurve>* opp = this->spanAtT(t, &priorSpan);
caryclark54359292015-03-26 07:52:43 -0700428 if (!opp) {
429 opp = this->addFollowing(priorSpan);
430#if DEBUG_PERP
caryclark26ad22a2015-10-16 09:03:38 -0700431 SkDebugf("%s priorSpan=%d t=%1.9g opp=%d\n", __FUNCTION__, priorSpan ?
432 priorSpan->debugID() : -1, t, opp->debugID());
caryclark54359292015-03-26 07:52:43 -0700433#endif
434 }
435#if DEBUG_PERP
436 opp->dump(); SkDebugf("\n");
caryclark26ad22a2015-10-16 09:03:38 -0700437 SkDebugf("%s addBounded span=%d opp=%d\n", __FUNCTION__, priorSpan ?
438 priorSpan->debugID() : -1, opp->debugID());
caryclark54359292015-03-26 07:52:43 -0700439#endif
440 opp->addBounded(span, &fHeap);
441 span->addBounded(opp, &fHeap);
442 }
443 this->validate();
caryclark1049f122015-04-20 08:31:59 -0700444#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700445 span->validatePerpT(t);
caryclark1049f122015-04-20 08:31:59 -0700446#endif
caryclark54359292015-03-26 07:52:43 -0700447}
448
caryclark1049f122015-04-20 08:31:59 -0700449template<typename TCurve, typename OppCurve>
450double SkTSpan<TCurve, OppCurve>::closestBoundedT(const SkDPoint& pt) const {
caryclark54359292015-03-26 07:52:43 -0700451 double result = -1;
caryclark343382e2016-06-29 08:18:38 -0700452 double closest = DBL_MAX;
caryclark1049f122015-04-20 08:31:59 -0700453 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700454 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -0700455 const SkTSpan<OppCurve, TCurve>* test = testBounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700456 double startDist = test->fPart[0].distanceSquared(pt);
457 if (closest > startDist) {
458 closest = startDist;
459 result = test->fStartT;
460 }
caryclark1049f122015-04-20 08:31:59 -0700461 double endDist = test->fPart[OppCurve::kPointLast].distanceSquared(pt);
caryclark54359292015-03-26 07:52:43 -0700462 if (closest > endDist) {
463 closest = endDist;
464 result = test->fEndT;
465 }
466 testBounded = testBounded->fNext;
467 }
468 SkASSERT(between(0, result, 1));
469 return result;
470}
471
472#ifdef SK_DEBUG
caryclark1049f122015-04-20 08:31:59 -0700473template<typename TCurve, typename OppCurve>
474bool SkTSpan<TCurve, OppCurve>::debugIsBefore(const SkTSpan* span) const {
caryclark54359292015-03-26 07:52:43 -0700475 const SkTSpan* work = this;
476 do {
477 if (span == work) {
478 return true;
479 }
480 } while ((work = work->fNext));
481 return false;
482}
483#endif
484
caryclark1049f122015-04-20 08:31:59 -0700485template<typename TCurve, typename OppCurve>
486bool SkTSpan<TCurve, OppCurve>::contains(double t) const {
caryclark54359292015-03-26 07:52:43 -0700487 const SkTSpan* work = this;
488 do {
489 if (between(work->fStartT, t, work->fEndT)) {
490 return true;
491 }
492 } while ((work = work->fNext));
493 return false;
494}
495
caryclark1049f122015-04-20 08:31:59 -0700496template<typename TCurve, typename OppCurve>
497const SkTSect<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::debugOpp() const {
halcanary96fcdcc2015-08-27 07:41:13 -0700498 return SkDEBUGRELEASE(fDebugSect->debugOpp(), nullptr);
caryclark54359292015-03-26 07:52:43 -0700499}
500
caryclark1049f122015-04-20 08:31:59 -0700501template<typename TCurve, typename OppCurve>
502SkTSpan<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::findOppSpan(
503 const SkTSpan<OppCurve, TCurve>* opp) const {
504 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700505 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700506 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700507 if (opp == test) {
508 return test;
509 }
510 bounded = bounded->fNext;
511 }
halcanary96fcdcc2015-08-27 07:41:13 -0700512 return nullptr;
caryclark54359292015-03-26 07:52:43 -0700513}
514
515// returns 0 if no hull intersection
516// 1 if hulls intersect
517// 2 if hulls only share a common endpoint
518// -1 if linear and further checking is required
caryclark1049f122015-04-20 08:31:59 -0700519template<typename TCurve, typename OppCurve>
520int SkTSpan<TCurve, OppCurve>::hullCheck(const SkTSpan<OppCurve, TCurve>* opp,
521 bool* start, bool* oppStart) {
caryclark54359292015-03-26 07:52:43 -0700522 if (fIsLinear) {
523 return -1;
524 }
525 bool ptsInCommon;
526 if (onlyEndPointsInCommon(opp, start, oppStart, &ptsInCommon)) {
527 SkASSERT(ptsInCommon);
528 return 2;
529 }
530 bool linear;
531 if (fPart.hullIntersects(opp->fPart, &linear)) {
532 if (!linear) { // check set true if linear
533 return 1;
534 }
535 fIsLinear = true;
536 fIsLine = fPart.controlsInside();
caryclark2bec26a2016-05-26 09:01:47 -0700537 return ptsInCommon ? 1 : -1;
caryclark54359292015-03-26 07:52:43 -0700538 } else { // hull is not linear; check set true if intersected at the end points
539 return ((int) ptsInCommon) << 1; // 0 or 2
540 }
541 return 0;
542}
543
544// OPTIMIZE ? If at_most_end_pts_in_common detects that one quad is near linear,
545// use line intersection to guess a better split than 0.5
546// OPTIMIZE Once at_most_end_pts_in_common detects linear, mark span so all future splits are linear
caryclark1049f122015-04-20 08:31:59 -0700547template<typename TCurve, typename OppCurve>
548int SkTSpan<TCurve, OppCurve>::hullsIntersect(SkTSpan<OppCurve, TCurve>* opp,
549 bool* start, bool* oppStart) {
caryclark54359292015-03-26 07:52:43 -0700550 if (!fBounds.intersects(opp->fBounds)) {
551 return 0;
552 }
553 int hullSect = this->hullCheck(opp, start, oppStart);
554 if (hullSect >= 0) {
555 return hullSect;
556 }
557 hullSect = opp->hullCheck(this, oppStart, start);
558 if (hullSect >= 0) {
559 return hullSect;
560 }
561 return -1;
562}
563
caryclark1049f122015-04-20 08:31:59 -0700564template<typename TCurve, typename OppCurve>
565void SkTSpan<TCurve, OppCurve>::init(const TCurve& c) {
halcanary96fcdcc2015-08-27 07:41:13 -0700566 fPrev = fNext = nullptr;
reed0dc4dd62015-03-24 13:55:33 -0700567 fStartT = 0;
568 fEndT = 1;
halcanary96fcdcc2015-08-27 07:41:13 -0700569 fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -0700570 resetBounds(c);
caryclark45fa4472015-01-16 07:04:10 -0800571}
572
caryclark1049f122015-04-20 08:31:59 -0700573template<typename TCurve, typename OppCurve>
caryclarka35ab3e2016-10-20 08:32:18 -0700574bool SkTSpan<TCurve, OppCurve>::initBounds(const TCurve& c) {
reed0dc4dd62015-03-24 13:55:33 -0700575 fPart = c.subDivide(fStartT, fEndT);
576 fBounds.setBounds(fPart);
577 fCoinStart.init();
578 fCoinEnd.init();
579 fBoundsMax = SkTMax(fBounds.width(), fBounds.height());
580 fCollapsed = fPart.collapsed();
581 fHasPerp = false;
caryclark54359292015-03-26 07:52:43 -0700582 fDeleted = false;
reed0dc4dd62015-03-24 13:55:33 -0700583#if DEBUG_T_SECT
reed0dc4dd62015-03-24 13:55:33 -0700584 if (fCollapsed) {
585 SkDebugf(""); // for convenient breakpoints
caryclark45fa4472015-01-16 07:04:10 -0800586 }
587#endif
caryclarka35ab3e2016-10-20 08:32:18 -0700588 return fBounds.valid();
caryclark45fa4472015-01-16 07:04:10 -0800589}
590
caryclark1049f122015-04-20 08:31:59 -0700591template<typename TCurve, typename OppCurve>
592bool SkTSpan<TCurve, OppCurve>::linearsIntersect(SkTSpan<OppCurve, TCurve>* span) {
caryclark54359292015-03-26 07:52:43 -0700593 int result = this->linearIntersects(span->fPart);
594 if (result <= 1) {
595 return SkToBool(result);
caryclark45fa4472015-01-16 07:04:10 -0800596 }
caryclark54359292015-03-26 07:52:43 -0700597 SkASSERT(span->fIsLinear);
598 result = span->linearIntersects(this->fPart);
599// SkASSERT(result <= 1);
600 return SkToBool(result);
caryclark45fa4472015-01-16 07:04:10 -0800601}
602
caryclark1049f122015-04-20 08:31:59 -0700603template<typename TCurve, typename OppCurve>
604double SkTSpan<TCurve, OppCurve>::linearT(const SkDPoint& pt) const {
caryclark54359292015-03-26 07:52:43 -0700605 SkDVector len = fPart[TCurve::kPointLast] - fPart[0];
606 return fabs(len.fX) > fabs(len.fY)
607 ? (pt.fX - fPart[0].fX) / len.fX
608 : (pt.fY - fPart[0].fY) / len.fY;
caryclark45fa4472015-01-16 07:04:10 -0800609}
610
caryclark1049f122015-04-20 08:31:59 -0700611template<typename TCurve, typename OppCurve>
612int SkTSpan<TCurve, OppCurve>::linearIntersects(const OppCurve& q2) const {
caryclark45fa4472015-01-16 07:04:10 -0800613 // looks like q1 is near-linear
caryclark54359292015-03-26 07:52:43 -0700614 int start = 0, end = TCurve::kPointLast; // the outside points are usually the extremes
caryclark45fa4472015-01-16 07:04:10 -0800615 if (!fPart.controlsInside()) {
616 double dist = 0; // if there's any question, compute distance to find best outsiders
617 for (int outer = 0; outer < TCurve::kPointCount - 1; ++outer) {
618 for (int inner = outer + 1; inner < TCurve::kPointCount; ++inner) {
619 double test = (fPart[outer] - fPart[inner]).lengthSquared();
620 if (dist > test) {
621 continue;
622 }
623 dist = test;
624 start = outer;
625 end = inner;
626 }
627 }
628 }
629 // see if q2 is on one side of the line formed by the extreme points
630 double origX = fPart[start].fX;
631 double origY = fPart[start].fY;
632 double adj = fPart[end].fX - origX;
633 double opp = fPart[end].fY - origY;
caryclark54359292015-03-26 07:52:43 -0700634 double maxPart = SkTMax(fabs(adj), fabs(opp));
635 double sign = 0; // initialization to shut up warning in release build
caryclark1049f122015-04-20 08:31:59 -0700636 for (int n = 0; n < OppCurve::kPointCount; ++n) {
caryclark54359292015-03-26 07:52:43 -0700637 double dx = q2[n].fY - origY;
638 double dy = q2[n].fX - origX;
639 double maxVal = SkTMax(maxPart, SkTMax(fabs(dx), fabs(dy)));
caryclark45fa4472015-01-16 07:04:10 -0800640 double test = (q2[n].fY - origY) * adj - (q2[n].fX - origX) * opp;
caryclark54359292015-03-26 07:52:43 -0700641 if (precisely_zero_when_compared_to(test, maxVal)) {
642 return 1;
643 }
644 if (approximately_zero_when_compared_to(test, maxVal)) {
645 return 3;
caryclark45fa4472015-01-16 07:04:10 -0800646 }
647 if (n == 0) {
648 sign = test;
649 continue;
650 }
651 if (test * sign < 0) {
caryclark54359292015-03-26 07:52:43 -0700652 return 1;
caryclark45fa4472015-01-16 07:04:10 -0800653 }
654 }
caryclark54359292015-03-26 07:52:43 -0700655 return 0;
656}
657
caryclark1049f122015-04-20 08:31:59 -0700658template<typename TCurve, typename OppCurve>
659bool SkTSpan<TCurve, OppCurve>::onlyEndPointsInCommon(const SkTSpan<OppCurve, TCurve>* opp,
660 bool* start, bool* oppStart, bool* ptsInCommon) {
caryclark54359292015-03-26 07:52:43 -0700661 if (opp->fPart[0] == fPart[0]) {
662 *start = *oppStart = true;
663 } else if (opp->fPart[0] == fPart[TCurve::kPointLast]) {
664 *start = false;
665 *oppStart = true;
caryclark1049f122015-04-20 08:31:59 -0700666 } else if (opp->fPart[OppCurve::kPointLast] == fPart[0]) {
caryclark54359292015-03-26 07:52:43 -0700667 *start = true;
668 *oppStart = false;
caryclark1049f122015-04-20 08:31:59 -0700669 } else if (opp->fPart[OppCurve::kPointLast] == fPart[TCurve::kPointLast]) {
caryclark54359292015-03-26 07:52:43 -0700670 *start = *oppStart = false;
671 } else {
672 *ptsInCommon = false;
673 return false;
674 }
675 *ptsInCommon = true;
caryclark1049f122015-04-20 08:31:59 -0700676 const SkDPoint* otherPts[TCurve::kPointCount - 1], * oppOtherPts[OppCurve::kPointCount - 1];
caryclark54359292015-03-26 07:52:43 -0700677 int baseIndex = *start ? 0 : TCurve::kPointLast;
caryclark1049f122015-04-20 08:31:59 -0700678 fPart.otherPts(baseIndex, otherPts);
679 opp->fPart.otherPts(*oppStart ? 0 : OppCurve::kPointLast, oppOtherPts);
caryclark54359292015-03-26 07:52:43 -0700680 const SkDPoint& base = fPart[baseIndex];
caryclark1049f122015-04-20 08:31:59 -0700681 for (int o1 = 0; o1 < (int) SK_ARRAY_COUNT(otherPts); ++o1) {
682 SkDVector v1 = *otherPts[o1] - base;
683 for (int o2 = 0; o2 < (int) SK_ARRAY_COUNT(oppOtherPts); ++o2) {
684 SkDVector v2 = *oppOtherPts[o2] - base;
caryclark54359292015-03-26 07:52:43 -0700685 if (v2.dot(v1) >= 0) {
686 return false;
687 }
688 }
689 }
690 return true;
691}
692
caryclark1049f122015-04-20 08:31:59 -0700693template<typename TCurve, typename OppCurve>
694SkTSpan<OppCurve, TCurve>* SkTSpan<TCurve, OppCurve>::oppT(double t) const {
695 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700696 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700697 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700698 if (between(test->fStartT, t, test->fEndT)) {
699 return test;
700 }
701 bounded = bounded->fNext;
702 }
halcanary96fcdcc2015-08-27 07:41:13 -0700703 return nullptr;
caryclark54359292015-03-26 07:52:43 -0700704}
705
caryclark1049f122015-04-20 08:31:59 -0700706template<typename TCurve, typename OppCurve>
707bool SkTSpan<TCurve, OppCurve>::removeAllBounded() {
caryclark54359292015-03-26 07:52:43 -0700708 bool deleteSpan = false;
caryclark1049f122015-04-20 08:31:59 -0700709 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700710 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700711 SkTSpan<OppCurve, TCurve>* opp = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700712 deleteSpan |= opp->removeBounded(this);
713 bounded = bounded->fNext;
714 }
715 return deleteSpan;
716}
717
caryclark1049f122015-04-20 08:31:59 -0700718template<typename TCurve, typename OppCurve>
719bool SkTSpan<TCurve, OppCurve>::removeBounded(const SkTSpan<OppCurve, TCurve>* opp) {
caryclark54359292015-03-26 07:52:43 -0700720 if (fHasPerp) {
721 bool foundStart = false;
722 bool foundEnd = false;
caryclark1049f122015-04-20 08:31:59 -0700723 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700724 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700725 SkTSpan<OppCurve, TCurve>* test = bounded->fBounded;
caryclark54359292015-03-26 07:52:43 -0700726 if (opp != test) {
727 foundStart |= between(test->fStartT, fCoinStart.perpT(), test->fEndT);
728 foundEnd |= between(test->fStartT, fCoinEnd.perpT(), test->fEndT);
729 }
730 bounded = bounded->fNext;
731 }
732 if (!foundStart || !foundEnd) {
733 fHasPerp = false;
734 fCoinStart.init();
735 fCoinEnd.init();
736 }
737 }
caryclark1049f122015-04-20 08:31:59 -0700738 SkTSpanBounded<OppCurve, TCurve>* bounded = fBounded;
halcanary96fcdcc2015-08-27 07:41:13 -0700739 SkTSpanBounded<OppCurve, TCurve>* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -0700740 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -0700741 SkTSpanBounded<OppCurve, TCurve>* boundedNext = bounded->fNext;
caryclark54359292015-03-26 07:52:43 -0700742 if (opp == bounded->fBounded) {
743 if (prev) {
744 prev->fNext = boundedNext;
745 return false;
746 } else {
747 fBounded = boundedNext;
halcanary96fcdcc2015-08-27 07:41:13 -0700748 return fBounded == nullptr;
caryclark54359292015-03-26 07:52:43 -0700749 }
750 }
751 prev = bounded;
752 bounded = boundedNext;
753 }
caryclark643ede62016-08-08 14:27:45 -0700754 SkOPASSERT(0);
caryclark45fa4472015-01-16 07:04:10 -0800755 return false;
756}
757
caryclark1049f122015-04-20 08:31:59 -0700758template<typename TCurve, typename OppCurve>
759bool SkTSpan<TCurve, OppCurve>::splitAt(SkTSpan* work, double t, SkChunkAlloc* heap) {
caryclark45fa4472015-01-16 07:04:10 -0800760 fStartT = t;
761 fEndT = work->fEndT;
762 if (fStartT == fEndT) {
763 fCollapsed = true;
764 return false;
765 }
766 work->fEndT = t;
767 if (work->fStartT == work->fEndT) {
768 work->fCollapsed = true;
769 return false;
770 }
771 fPrev = work;
772 fNext = work->fNext;
773 fIsLinear = work->fIsLinear;
caryclark54359292015-03-26 07:52:43 -0700774 fIsLine = work->fIsLine;
775
caryclark45fa4472015-01-16 07:04:10 -0800776 work->fNext = this;
777 if (fNext) {
778 fNext->fPrev = this;
779 }
caryclark643ede62016-08-08 14:27:45 -0700780 this->validate();
caryclark1049f122015-04-20 08:31:59 -0700781 SkTSpanBounded<OppCurve, TCurve>* bounded = work->fBounded;
halcanary96fcdcc2015-08-27 07:41:13 -0700782 fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -0700783 while (bounded) {
784 this->addBounded(bounded->fBounded, heap);
785 bounded = bounded->fNext;
786 }
787 bounded = fBounded;
788 while (bounded) {
789 bounded->fBounded->addBounded(this, heap);
790 bounded = bounded->fNext;
caryclark45fa4472015-01-16 07:04:10 -0800791 }
792 return true;
793}
794
caryclark1049f122015-04-20 08:31:59 -0700795template<typename TCurve, typename OppCurve>
796void SkTSpan<TCurve, OppCurve>::validate() const {
caryclark643ede62016-08-08 14:27:45 -0700797#if DEBUG_VALIDATE
798 SkASSERT(this != fPrev);
799 SkASSERT(this != fNext);
halcanary96fcdcc2015-08-27 07:41:13 -0700800 SkASSERT(fNext == nullptr || fNext != fPrev);
801 SkASSERT(fNext == nullptr || this == fNext->fPrev);
802 SkASSERT(fPrev == nullptr || this == fPrev->fNext);
caryclark643ede62016-08-08 14:27:45 -0700803 this->validateBounded();
804#endif
805#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700806 SkASSERT(fBounds.width() || fBounds.height() || fCollapsed);
caryclarke839e782016-09-15 07:48:18 -0700807 SkASSERT(fBoundsMax == SkTMax(fBounds.width(), fBounds.height()) || fCollapsed == 0xFF);
caryclark45fa4472015-01-16 07:04:10 -0800808 SkASSERT(0 <= fStartT);
809 SkASSERT(fEndT <= 1);
caryclark54359292015-03-26 07:52:43 -0700810 SkASSERT(fStartT <= fEndT);
caryclarke839e782016-09-15 07:48:18 -0700811 SkASSERT(fBounded || fCollapsed == 0xFF);
caryclark54359292015-03-26 07:52:43 -0700812 if (fHasPerp) {
caryclark6c3b9cd2016-09-26 05:36:58 -0700813 if (fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700814 validatePerpT(fCoinStart.perpT());
815 validatePerpPt(fCoinStart.perpT(), fCoinStart.perpPt());
816 }
caryclark6c3b9cd2016-09-26 05:36:58 -0700817 if (fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700818 validatePerpT(fCoinEnd.perpT());
819 validatePerpPt(fCoinEnd.perpT(), fCoinEnd.perpPt());
820 }
caryclarkccec0f92015-03-24 07:28:17 -0700821 }
reed0dc4dd62015-03-24 13:55:33 -0700822#endif
caryclark54359292015-03-26 07:52:43 -0700823}
caryclarkccec0f92015-03-24 07:28:17 -0700824
caryclark1049f122015-04-20 08:31:59 -0700825template<typename TCurve, typename OppCurve>
826void SkTSpan<TCurve, OppCurve>::validateBounded() const {
caryclark54359292015-03-26 07:52:43 -0700827#if DEBUG_VALIDATE
caryclark1049f122015-04-20 08:31:59 -0700828 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700829 while (testBounded) {
csmartdaltonceeaa782016-08-10 10:07:57 -0700830 SkDEBUGCODE(const SkTSpan<OppCurve, TCurve>* overlap = testBounded->fBounded);
caryclark54359292015-03-26 07:52:43 -0700831 SkASSERT(!overlap->fDeleted);
caryclark643ede62016-08-08 14:27:45 -0700832#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -0700833 SkASSERT(((this->debugID() ^ overlap->debugID()) & 1) == 1);
834 SkASSERT(overlap->findOppSpan(this));
caryclark643ede62016-08-08 14:27:45 -0700835#endif
caryclark54359292015-03-26 07:52:43 -0700836 testBounded = testBounded->fNext;
837 }
838#endif
839}
840
caryclark1049f122015-04-20 08:31:59 -0700841template<typename TCurve, typename OppCurve>
842void SkTSpan<TCurve, OppCurve>::validatePerpT(double oppT) const {
843 const SkTSpanBounded<OppCurve, TCurve>* testBounded = fBounded;
caryclark54359292015-03-26 07:52:43 -0700844 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -0700845 const SkTSpan<OppCurve, TCurve>* overlap = testBounded->fBounded;
caryclark26ad22a2015-10-16 09:03:38 -0700846 if (precisely_between(overlap->fStartT, oppT, overlap->fEndT)) {
caryclark54359292015-03-26 07:52:43 -0700847 return;
848 }
849 testBounded = testBounded->fNext;
850 }
851 SkASSERT(0);
caryclark54359292015-03-26 07:52:43 -0700852}
853
caryclark1049f122015-04-20 08:31:59 -0700854template<typename TCurve, typename OppCurve>
855void SkTSpan<TCurve, OppCurve>::validatePerpPt(double t, const SkDPoint& pt) const {
856 SkASSERT(fDebugSect->fOppSect->fCurve.ptAtT(t) == pt);
caryclark54359292015-03-26 07:52:43 -0700857}
858
859
caryclark1049f122015-04-20 08:31:59 -0700860template<typename TCurve, typename OppCurve>
caryclarke25a4f62016-07-26 09:26:29 -0700861SkTSect<TCurve, OppCurve>::SkTSect(const TCurve& c
862 SkDEBUGPARAMS(SkOpGlobalState* debugGlobalState)
863 PATH_OPS_DEBUG_T_SECT_PARAMS(int id))
caryclark45fa4472015-01-16 07:04:10 -0800864 : fCurve(c)
caryclark1049f122015-04-20 08:31:59 -0700865 , fHeap(sizeof(SkTSpan<TCurve, OppCurve>) * 4)
halcanary96fcdcc2015-08-27 07:41:13 -0700866 , fCoincident(nullptr)
867 , fDeleted(nullptr)
caryclark45fa4472015-01-16 07:04:10 -0800868 , fActiveCount(0)
caryclarke25a4f62016-07-26 09:26:29 -0700869 SkDEBUGPARAMS(fDebugGlobalState(debugGlobalState))
caryclark54359292015-03-26 07:52:43 -0700870 PATH_OPS_DEBUG_T_SECT_PARAMS(fID(id))
871 PATH_OPS_DEBUG_T_SECT_PARAMS(fDebugCount(0))
872 PATH_OPS_DEBUG_T_SECT_PARAMS(fDebugAllocatedCount(0))
caryclark45fa4472015-01-16 07:04:10 -0800873{
caryclark34efb702016-10-24 08:19:06 -0700874 this->resetRemovedEnds();
875 fHead = this->addOne();
caryclark643ede62016-08-08 14:27:45 -0700876 SkDEBUGCODE(fHead->debugSetGlobalState(debugGlobalState));
caryclark45fa4472015-01-16 07:04:10 -0800877 fHead->init(c);
878}
879
caryclark1049f122015-04-20 08:31:59 -0700880template<typename TCurve, typename OppCurve>
881SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::addOne() {
882 SkTSpan<TCurve, OppCurve>* result;
caryclark45fa4472015-01-16 07:04:10 -0800883 if (fDeleted) {
884 result = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -0800885 fDeleted = result->fNext;
886 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700887 result = new (fHeap.allocThrow(sizeof(SkTSpan<TCurve, OppCurve>)))(
888 SkTSpan<TCurve, OppCurve>);
caryclark45fa4472015-01-16 07:04:10 -0800889#if DEBUG_T_SECT
890 ++fDebugAllocatedCount;
891#endif
892 }
caryclarked0935a2015-10-22 07:23:52 -0700893 result->reset();
caryclark08b32492015-04-06 11:41:29 -0700894 result->fHasPerp = false;
895 result->fDeleted = false;
caryclark55888e42016-07-18 10:01:36 -0700896 ++fActiveCount;
caryclark54359292015-03-26 07:52:43 -0700897 PATH_OPS_DEBUG_T_SECT_CODE(result->fID = fDebugCount++ * 2 + fID);
caryclark1049f122015-04-20 08:31:59 -0700898 SkDEBUGCODE(result->fDebugSect = this);
caryclarked0935a2015-10-22 07:23:52 -0700899#ifdef SK_DEBUG
900 result->fPart.debugInit();
901 result->fCoinStart.debugInit();
902 result->fCoinEnd.debugInit();
903 result->fPrev = result->fNext = nullptr;
904 result->fBounds.debugInit();
905 result->fStartT = result->fEndT = result->fBoundsMax = SK_ScalarNaN;
906 result->fCollapsed = result->fIsLinear = result->fIsLine = 0xFF;
907#endif
caryclark45fa4472015-01-16 07:04:10 -0800908 return result;
909}
910
caryclark1049f122015-04-20 08:31:59 -0700911template<typename TCurve, typename OppCurve>
912bool SkTSect<TCurve, OppCurve>::binarySearchCoin(SkTSect<OppCurve, TCurve>* sect2, double tStart,
913 double tStep, double* resultT, double* oppT) {
914 SkTSpan<TCurve, OppCurve> work;
caryclark45fa4472015-01-16 07:04:10 -0800915 double result = work.fStartT = work.fEndT = tStart;
caryclark1049f122015-04-20 08:31:59 -0700916 SkDEBUGCODE(work.fDebugSect = this);
caryclark45fa4472015-01-16 07:04:10 -0800917 SkDPoint last = fCurve.ptAtT(tStart);
918 SkDPoint oppPt;
919 bool flip = false;
caryclarkcdeff812016-07-22 03:34:19 -0700920 bool contained = false;
caryclark45fa4472015-01-16 07:04:10 -0800921 SkDEBUGCODE(bool down = tStep < 0);
caryclark1049f122015-04-20 08:31:59 -0700922 const OppCurve& opp = sect2->fCurve;
caryclark45fa4472015-01-16 07:04:10 -0800923 do {
924 tStep *= 0.5;
925 work.fStartT += tStep;
926 if (flip) {
927 tStep = -tStep;
928 flip = false;
929 }
930 work.initBounds(fCurve);
931 if (work.fCollapsed) {
932 return false;
933 }
934 if (last.approximatelyEqual(work.fPart[0])) {
935 break;
936 }
937 last = work.fPart[0];
938 work.fCoinStart.setPerp(fCurve, work.fStartT, last, opp);
caryclark6c3b9cd2016-09-26 05:36:58 -0700939 if (work.fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -0700940#if DEBUG_T_SECT
941 work.validatePerpPt(work.fCoinStart.perpT(), work.fCoinStart.perpPt());
942#endif
caryclark45fa4472015-01-16 07:04:10 -0800943 double oppTTest = work.fCoinStart.perpT();
caryclark54359292015-03-26 07:52:43 -0700944 if (sect2->fHead->contains(oppTTest)) {
caryclark45fa4472015-01-16 07:04:10 -0800945 *oppT = oppTTest;
946 oppPt = work.fCoinStart.perpPt();
caryclarkcdeff812016-07-22 03:34:19 -0700947 contained = true;
caryclark45fa4472015-01-16 07:04:10 -0800948 SkASSERT(down ? result > work.fStartT : result < work.fStartT);
949 result = work.fStartT;
950 continue;
951 }
952 }
953 tStep = -tStep;
954 flip = true;
955 } while (true);
caryclarkcdeff812016-07-22 03:34:19 -0700956 if (!contained) {
957 return false;
958 }
caryclark45fa4472015-01-16 07:04:10 -0800959 if (last.approximatelyEqual(fCurve[0])) {
960 result = 0;
961 } else if (last.approximatelyEqual(fCurve[TCurve::kPointLast])) {
962 result = 1;
963 }
964 if (oppPt.approximatelyEqual(opp[0])) {
965 *oppT = 0;
caryclark1049f122015-04-20 08:31:59 -0700966 } else if (oppPt.approximatelyEqual(opp[OppCurve::kPointLast])) {
caryclark45fa4472015-01-16 07:04:10 -0800967 *oppT = 1;
968 }
969 *resultT = result;
970 return true;
971}
972
973// OPTIMIZE ? keep a sorted list of sizes in the form of a doubly-linked list in quad span
974// so that each quad sect has a pointer to the largest, and can update it as spans
975// are split
caryclark1049f122015-04-20 08:31:59 -0700976template<typename TCurve, typename OppCurve>
977SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::boundsMax() const {
978 SkTSpan<TCurve, OppCurve>* test = fHead;
979 SkTSpan<TCurve, OppCurve>* largest = fHead;
caryclark54359292015-03-26 07:52:43 -0700980 bool lCollapsed = largest->fCollapsed;
caryclark45fa4472015-01-16 07:04:10 -0800981 while ((test = test->fNext)) {
caryclark54359292015-03-26 07:52:43 -0700982 bool tCollapsed = test->fCollapsed;
983 if ((lCollapsed && !tCollapsed) || (lCollapsed == tCollapsed &&
984 largest->fBoundsMax < test->fBoundsMax)) {
caryclark45fa4472015-01-16 07:04:10 -0800985 largest = test;
caryclark1049f122015-04-20 08:31:59 -0700986 lCollapsed = test->fCollapsed;
caryclark45fa4472015-01-16 07:04:10 -0800987 }
988 }
caryclark54359292015-03-26 07:52:43 -0700989 return largest;
caryclark45fa4472015-01-16 07:04:10 -0800990}
991
caryclark1049f122015-04-20 08:31:59 -0700992template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -0700993bool SkTSect<TCurve, OppCurve>::coincidentCheck(SkTSect<OppCurve, TCurve>* sect2) {
caryclark1049f122015-04-20 08:31:59 -0700994 SkTSpan<TCurve, OppCurve>* first = fHead;
caryclark9feb6322016-10-25 08:58:26 -0700995 if (!first) {
996 return false;
997 }
caryclark1049f122015-04-20 08:31:59 -0700998 SkTSpan<TCurve, OppCurve>* last, * next;
caryclark45fa4472015-01-16 07:04:10 -0800999 do {
caryclark54359292015-03-26 07:52:43 -07001000 int consecutive = this->countConsecutiveSpans(first, &last);
1001 next = last->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001002 if (consecutive < COINCIDENT_SPAN_COUNT) {
1003 continue;
1004 }
caryclark54359292015-03-26 07:52:43 -07001005 this->validate();
1006 sect2->validate();
1007 this->computePerpendiculars(sect2, first, last);
1008 this->validate();
1009 sect2->validate();
caryclark45fa4472015-01-16 07:04:10 -08001010 // check to see if a range of points are on the curve
caryclark1049f122015-04-20 08:31:59 -07001011 SkTSpan<TCurve, OppCurve>* coinStart = first;
caryclark54359292015-03-26 07:52:43 -07001012 do {
caryclarkef7cee42016-09-06 09:05:54 -07001013 bool success = this->extractCoincident(sect2, coinStart, last, &coinStart);
1014 if (!success) {
1015 return false;
1016 }
caryclark54359292015-03-26 07:52:43 -07001017 } while (coinStart && !last->fDeleted);
caryclark55888e42016-07-18 10:01:36 -07001018 if (!fHead || !sect2->fHead) {
1019 break;
1020 }
caryclark643ede62016-08-08 14:27:45 -07001021 if (!next || next->fDeleted) {
1022 break;
1023 }
caryclark45fa4472015-01-16 07:04:10 -08001024 } while ((first = next));
caryclarkef7cee42016-09-06 09:05:54 -07001025 return true;
caryclark45fa4472015-01-16 07:04:10 -08001026}
1027
caryclark1049f122015-04-20 08:31:59 -07001028template<typename TCurve, typename OppCurve>
caryclark26ad22a2015-10-16 09:03:38 -07001029void SkTSect<TCurve, OppCurve>::coincidentForce(SkTSect<OppCurve, TCurve>* sect2,
1030 double start1s, double start1e) {
1031 SkTSpan<TCurve, OppCurve>* first = fHead;
1032 SkTSpan<TCurve, OppCurve>* last = this->tail();
1033 SkTSpan<OppCurve, TCurve>* oppFirst = sect2->fHead;
1034 SkTSpan<OppCurve, TCurve>* oppLast = sect2->tail();
1035 bool deleteEmptySpans = this->updateBounded(first, last, oppFirst);
1036 deleteEmptySpans |= sect2->updateBounded(oppFirst, oppLast, first);
1037 this->removeSpanRange(first, last);
1038 sect2->removeSpanRange(oppFirst, oppLast);
1039 first->fStartT = start1s;
1040 first->fEndT = start1e;
1041 first->resetBounds(fCurve);
1042 first->fCoinStart.setPerp(fCurve, start1s, fCurve[0], sect2->fCurve);
1043 first->fCoinEnd.setPerp(fCurve, start1e, fCurve[TCurve::kPointLast], sect2->fCurve);
1044 bool oppMatched = first->fCoinStart.perpT() < first->fCoinEnd.perpT();
caryclarkef784fb2015-10-30 12:03:06 -07001045 double oppStartT = first->fCoinStart.perpT() == -1 ? 0 : SkTMax(0., first->fCoinStart.perpT());
1046 double oppEndT = first->fCoinEnd.perpT() == -1 ? 1 : SkTMin(1., first->fCoinEnd.perpT());
caryclark26ad22a2015-10-16 09:03:38 -07001047 if (!oppMatched) {
1048 SkTSwap(oppStartT, oppEndT);
1049 }
1050 oppFirst->fStartT = oppStartT;
1051 oppFirst->fEndT = oppEndT;
1052 oppFirst->resetBounds(sect2->fCurve);
1053 this->removeCoincident(first, false);
1054 sect2->removeCoincident(oppFirst, true);
1055 if (deleteEmptySpans) {
1056 this->deleteEmptySpans();
1057 sect2->deleteEmptySpans();
1058 }
1059}
1060
1061template<typename TCurve, typename OppCurve>
caryclark1049f122015-04-20 08:31:59 -07001062bool SkTSect<TCurve, OppCurve>::coincidentHasT(double t) {
1063 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001064 while (test) {
1065 if (between(test->fStartT, t, test->fEndT)) {
1066 return true;
1067 }
1068 test = test->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001069 }
caryclark54359292015-03-26 07:52:43 -07001070 return false;
caryclark45fa4472015-01-16 07:04:10 -08001071}
1072
caryclark1049f122015-04-20 08:31:59 -07001073template<typename TCurve, typename OppCurve>
1074int SkTSect<TCurve, OppCurve>::collapsed() const {
1075 int result = 0;
1076 const SkTSpan<TCurve, OppCurve>* test = fHead;
1077 while (test) {
1078 if (test->fCollapsed) {
1079 ++result;
1080 }
1081 test = test->next();
1082 }
1083 return result;
1084}
1085
1086template<typename TCurve, typename OppCurve>
1087void SkTSect<TCurve, OppCurve>::computePerpendiculars(SkTSect<OppCurve, TCurve>* sect2,
1088 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last) {
1089 const OppCurve& opp = sect2->fCurve;
1090 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001091 SkTSpan<TCurve, OppCurve>* prior = nullptr;
caryclark45fa4472015-01-16 07:04:10 -08001092 do {
caryclark54359292015-03-26 07:52:43 -07001093 if (!work->fHasPerp && !work->fCollapsed) {
1094 if (prior) {
1095 work->fCoinStart = prior->fCoinEnd;
1096 } else {
1097 work->fCoinStart.setPerp(fCurve, work->fStartT, work->fPart[0], opp);
caryclark45fa4472015-01-16 07:04:10 -08001098 }
caryclark6c3b9cd2016-09-26 05:36:58 -07001099 if (work->fCoinStart.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001100 double perpT = work->fCoinStart.perpT();
1101 if (sect2->coincidentHasT(perpT)) {
caryclarkdf386c52015-04-21 05:27:02 -07001102 work->fCoinStart.init();
caryclark54359292015-03-26 07:52:43 -07001103 } else {
1104 sect2->addForPerp(work, perpT);
1105 }
1106 }
1107 work->fCoinEnd.setPerp(fCurve, work->fEndT, work->fPart[TCurve::kPointLast], opp);
caryclark6c3b9cd2016-09-26 05:36:58 -07001108 if (work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001109 double perpT = work->fCoinEnd.perpT();
1110 if (sect2->coincidentHasT(perpT)) {
caryclarkdf386c52015-04-21 05:27:02 -07001111 work->fCoinEnd.init();
caryclark54359292015-03-26 07:52:43 -07001112 } else {
1113 sect2->addForPerp(work, perpT);
1114 }
1115 }
1116 work->fHasPerp = true;
caryclark45fa4472015-01-16 07:04:10 -08001117 }
1118 if (work == last) {
1119 break;
1120 }
caryclark54359292015-03-26 07:52:43 -07001121 prior = work;
caryclark45fa4472015-01-16 07:04:10 -08001122 work = work->fNext;
1123 SkASSERT(work);
1124 } while (true);
caryclark54359292015-03-26 07:52:43 -07001125}
1126
caryclark1049f122015-04-20 08:31:59 -07001127template<typename TCurve, typename OppCurve>
1128int SkTSect<TCurve, OppCurve>::countConsecutiveSpans(SkTSpan<TCurve, OppCurve>* first,
1129 SkTSpan<TCurve, OppCurve>** lastPtr) const {
caryclark54359292015-03-26 07:52:43 -07001130 int consecutive = 1;
caryclark1049f122015-04-20 08:31:59 -07001131 SkTSpan<TCurve, OppCurve>* last = first;
caryclark54359292015-03-26 07:52:43 -07001132 do {
caryclark1049f122015-04-20 08:31:59 -07001133 SkTSpan<TCurve, OppCurve>* next = last->fNext;
caryclark54359292015-03-26 07:52:43 -07001134 if (!next) {
1135 break;
1136 }
1137 if (next->fStartT > last->fEndT) {
1138 break;
1139 }
1140 ++consecutive;
1141 last = next;
1142 } while (true);
1143 *lastPtr = last;
1144 return consecutive;
1145}
1146
caryclark1049f122015-04-20 08:31:59 -07001147template<typename TCurve, typename OppCurve>
1148bool SkTSect<TCurve, OppCurve>::debugHasBounded(const SkTSpan<OppCurve, TCurve>* span) const {
1149 const SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001150 if (!test) {
1151 return false;
1152 }
1153 do {
1154 if (test->findOppSpan(span)) {
1155 return true;
1156 }
1157 } while ((test = test->next()));
1158 return false;
1159}
1160
caryclark1049f122015-04-20 08:31:59 -07001161template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001162bool SkTSect<TCurve, OppCurve>::deleteEmptySpans() {
caryclark1049f122015-04-20 08:31:59 -07001163 SkTSpan<TCurve, OppCurve>* test;
1164 SkTSpan<TCurve, OppCurve>* next = fHead;
Cary Clark59ed4822016-12-08 16:17:56 -05001165 int safetyHatch = 1000;
caryclark54359292015-03-26 07:52:43 -07001166 while ((test = next)) {
1167 next = test->fNext;
1168 if (!test->fBounded) {
caryclarkef7cee42016-09-06 09:05:54 -07001169 if (!this->removeSpan(test)) {
1170 return false;
1171 }
caryclark54359292015-03-26 07:52:43 -07001172 }
Cary Clark59ed4822016-12-08 16:17:56 -05001173 if (--safetyHatch < 0) {
1174 return false;
1175 }
caryclark54359292015-03-26 07:52:43 -07001176 }
caryclarkef7cee42016-09-06 09:05:54 -07001177 return true;
caryclark54359292015-03-26 07:52:43 -07001178}
1179
caryclark1049f122015-04-20 08:31:59 -07001180template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001181bool SkTSect<TCurve, OppCurve>::extractCoincident(
caryclark1049f122015-04-20 08:31:59 -07001182 SkTSect<OppCurve, TCurve>* sect2,
caryclarkef7cee42016-09-06 09:05:54 -07001183 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>* last,
1184 SkTSpan<TCurve, OppCurve>** result) {
caryclark1049f122015-04-20 08:31:59 -07001185 first = findCoincidentRun(first, &last);
caryclarka1b42d92016-08-16 10:25:29 -07001186 if (!first || !last) {
caryclarkef7cee42016-09-06 09:05:54 -07001187 *result = nullptr;
1188 return true;
caryclark45fa4472015-01-16 07:04:10 -08001189 }
1190 // march outwards to find limit of coincidence from here to previous and next spans
1191 double startT = first->fStartT;
caryclarkd8bc16b2015-03-26 09:05:12 -07001192 double oppStartT SK_INIT_TO_AVOID_WARNING;
caryclark54359292015-03-26 07:52:43 -07001193 double oppEndT SK_INIT_TO_AVOID_WARNING;
caryclark1049f122015-04-20 08:31:59 -07001194 SkTSpan<TCurve, OppCurve>* prev = first->fPrev;
caryclark6c3b9cd2016-09-26 05:36:58 -07001195 SkASSERT(first->fCoinStart.isMatch());
caryclark1049f122015-04-20 08:31:59 -07001196 SkTSpan<OppCurve, TCurve>* oppFirst = first->findOppT(first->fCoinStart.perpT());
caryclark6c3b9cd2016-09-26 05:36:58 -07001197 SkOPASSERT(last->fCoinEnd.isMatch());
caryclark54359292015-03-26 07:52:43 -07001198 bool oppMatched = first->fCoinStart.perpT() < first->fCoinEnd.perpT();
1199 double coinStart;
1200 SkDEBUGCODE(double coinEnd);
caryclark1049f122015-04-20 08:31:59 -07001201 SkTSpan<OppCurve, TCurve>* cutFirst;
caryclark54359292015-03-26 07:52:43 -07001202 if (prev && prev->fEndT == startT
1203 && this->binarySearchCoin(sect2, startT, prev->fStartT - startT, &coinStart,
1204 &oppStartT)
caryclark1049f122015-04-20 08:31:59 -07001205 && prev->fStartT < coinStart && coinStart < startT
1206 && (cutFirst = prev->oppT(oppStartT))) {
1207 oppFirst = cutFirst;
caryclark54359292015-03-26 07:52:43 -07001208 first = this->addSplitAt(prev, coinStart);
1209 first->markCoincident();
1210 prev->fCoinEnd.markCoincident();
1211 if (oppFirst->fStartT < oppStartT && oppStartT < oppFirst->fEndT) {
caryclark1049f122015-04-20 08:31:59 -07001212 SkTSpan<OppCurve, TCurve>* oppHalf = sect2->addSplitAt(oppFirst, oppStartT);
caryclark54359292015-03-26 07:52:43 -07001213 if (oppMatched) {
1214 oppFirst->fCoinEnd.markCoincident();
1215 oppHalf->markCoincident();
1216 oppFirst = oppHalf;
1217 } else {
1218 oppFirst->markCoincident();
1219 oppHalf->fCoinStart.markCoincident();
1220 }
1221 }
1222 } else {
1223 SkDEBUGCODE(coinStart = first->fStartT);
caryclarka35ab3e2016-10-20 08:32:18 -07001224 FAIL_IF(!oppFirst);
caryclark54359292015-03-26 07:52:43 -07001225 SkDEBUGCODE(oppStartT = oppMatched ? oppFirst->fStartT : oppFirst->fEndT);
1226 }
caryclark1049f122015-04-20 08:31:59 -07001227 // FIXME: incomplete : if we're not at the end, find end of coin
1228 SkTSpan<OppCurve, TCurve>* oppLast;
caryclark6c3b9cd2016-09-26 05:36:58 -07001229 SkOPASSERT(last->fCoinEnd.isMatch());
caryclark54359292015-03-26 07:52:43 -07001230 oppLast = last->findOppT(last->fCoinEnd.perpT());
1231 SkDEBUGCODE(coinEnd = last->fEndT);
caryclark643ede62016-08-08 14:27:45 -07001232#ifdef SK_DEBUG
1233 if (!this->globalState() || !this->globalState()->debugSkipAssert()) {
1234 oppEndT = oppMatched ? oppLast->fEndT : oppLast->fStartT;
1235 }
1236#endif
caryclark54359292015-03-26 07:52:43 -07001237 if (!oppMatched) {
1238 SkTSwap(oppFirst, oppLast);
1239 SkTSwap(oppStartT, oppEndT);
1240 }
caryclarke25a4f62016-07-26 09:26:29 -07001241 SkOPASSERT(oppStartT < oppEndT);
caryclark54359292015-03-26 07:52:43 -07001242 SkASSERT(coinStart == first->fStartT);
1243 SkASSERT(coinEnd == last->fEndT);
caryclark643ede62016-08-08 14:27:45 -07001244 SkOPASSERT(oppStartT == oppFirst->fStartT);
1245 SkOPASSERT(oppEndT == oppLast->fEndT);
1246 if (!oppFirst) {
caryclarkef7cee42016-09-06 09:05:54 -07001247 *result = nullptr;
1248 return true;
caryclark643ede62016-08-08 14:27:45 -07001249 }
caryclark42942862016-08-19 07:01:33 -07001250 if (!oppLast) {
caryclarkef7cee42016-09-06 09:05:54 -07001251 *result = nullptr;
1252 return true;
caryclark42942862016-08-19 07:01:33 -07001253 }
caryclark54359292015-03-26 07:52:43 -07001254 // reduce coincident runs to single entries
1255 this->validate();
1256 sect2->validate();
caryclark1049f122015-04-20 08:31:59 -07001257 bool deleteEmptySpans = this->updateBounded(first, last, oppFirst);
1258 deleteEmptySpans |= sect2->updateBounded(oppFirst, oppLast, first);
caryclark54359292015-03-26 07:52:43 -07001259 this->removeSpanRange(first, last);
1260 sect2->removeSpanRange(oppFirst, oppLast);
1261 first->fEndT = last->fEndT;
1262 first->resetBounds(this->fCurve);
1263 first->fCoinStart.setPerp(fCurve, first->fStartT, first->fPart[0], sect2->fCurve);
1264 first->fCoinEnd.setPerp(fCurve, first->fEndT, first->fPart[TCurve::kPointLast], sect2->fCurve);
1265 oppStartT = first->fCoinStart.perpT();
1266 oppEndT = first->fCoinEnd.perpT();
1267 if (between(0, oppStartT, 1) && between(0, oppEndT, 1)) {
1268 if (!oppMatched) {
1269 SkTSwap(oppStartT, oppEndT);
1270 }
1271 oppFirst->fStartT = oppStartT;
1272 oppFirst->fEndT = oppEndT;
1273 oppFirst->resetBounds(sect2->fCurve);
1274 }
1275 this->validateBounded();
1276 sect2->validateBounded();
1277 last = first->fNext;
1278 this->removeCoincident(first, false);
1279 sect2->removeCoincident(oppFirst, true);
caryclark1049f122015-04-20 08:31:59 -07001280 if (deleteEmptySpans) {
caryclarkef7cee42016-09-06 09:05:54 -07001281 if (!this->deleteEmptySpans() || !sect2->deleteEmptySpans()) {
1282 *result = nullptr;
1283 return false;
1284 }
caryclark54359292015-03-26 07:52:43 -07001285 }
1286 this->validate();
1287 sect2->validate();
caryclarkef7cee42016-09-06 09:05:54 -07001288 *result = last && !last->fDeleted && fHead && sect2->fHead ? last : nullptr;
1289 return true;
caryclark54359292015-03-26 07:52:43 -07001290}
1291
caryclark1049f122015-04-20 08:31:59 -07001292template<typename TCurve, typename OppCurve>
1293SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::findCoincidentRun(
1294 SkTSpan<TCurve, OppCurve>* first, SkTSpan<TCurve, OppCurve>** lastPtr) {
1295 SkTSpan<TCurve, OppCurve>* work = first;
halcanary96fcdcc2015-08-27 07:41:13 -07001296 SkTSpan<TCurve, OppCurve>* lastCandidate = nullptr;
1297 first = nullptr;
caryclark54359292015-03-26 07:52:43 -07001298 // find the first fully coincident span
1299 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07001300 if (work->fCoinStart.isMatch()) {
caryclark1049f122015-04-20 08:31:59 -07001301#if DEBUG_T_SECT
caryclark54359292015-03-26 07:52:43 -07001302 work->validatePerpT(work->fCoinStart.perpT());
1303 work->validatePerpPt(work->fCoinStart.perpT(), work->fCoinStart.perpPt());
caryclark1049f122015-04-20 08:31:59 -07001304#endif
caryclarka35ab3e2016-10-20 08:32:18 -07001305 SkOPASSERT(work->hasOppT(work->fCoinStart.perpT()));
caryclark6c3b9cd2016-09-26 05:36:58 -07001306 if (!work->fCoinEnd.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001307 break;
1308 }
1309 lastCandidate = work;
1310 if (!first) {
1311 first = work;
1312 }
caryclark1049f122015-04-20 08:31:59 -07001313 } else if (first && work->fCollapsed) {
1314 *lastPtr = lastCandidate;
1315 return first;
caryclark54359292015-03-26 07:52:43 -07001316 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001317 lastCandidate = nullptr;
caryclark643ede62016-08-08 14:27:45 -07001318 SkOPASSERT(!first);
caryclark54359292015-03-26 07:52:43 -07001319 }
1320 if (work == *lastPtr) {
1321 return first;
1322 }
1323 work = work->fNext;
caryclarke25a4f62016-07-26 09:26:29 -07001324 if (!work) {
1325 return nullptr;
1326 }
caryclark54359292015-03-26 07:52:43 -07001327 } while (true);
1328 if (lastCandidate) {
1329 *lastPtr = lastCandidate;
1330 }
1331 return first;
1332}
1333
caryclark1049f122015-04-20 08:31:59 -07001334template<typename TCurve, typename OppCurve>
1335int SkTSect<TCurve, OppCurve>::intersects(SkTSpan<TCurve, OppCurve>* span,
1336 SkTSect<OppCurve, TCurve>* opp,
1337 SkTSpan<OppCurve, TCurve>* oppSpan, int* oppResult) {
caryclark54359292015-03-26 07:52:43 -07001338 bool spanStart, oppStart;
1339 int hullResult = span->hullsIntersect(oppSpan, &spanStart, &oppStart);
1340 if (hullResult >= 0) {
1341 if (hullResult == 2) { // hulls have one point in common
1342 if (!span->fBounded || !span->fBounded->fNext) {
1343 SkASSERT(!span->fBounded || span->fBounded->fBounded == oppSpan);
1344 if (spanStart) {
1345 span->fEndT = span->fStartT;
caryclark45fa4472015-01-16 07:04:10 -08001346 } else {
caryclark54359292015-03-26 07:52:43 -07001347 span->fStartT = span->fEndT;
1348 }
1349 } else {
1350 hullResult = 1;
1351 }
1352 if (!oppSpan->fBounded || !oppSpan->fBounded->fNext) {
1353 SkASSERT(!oppSpan->fBounded || oppSpan->fBounded->fBounded == span);
1354 if (oppStart) {
1355 oppSpan->fEndT = oppSpan->fStartT;
1356 } else {
1357 oppSpan->fStartT = oppSpan->fEndT;
1358 }
1359 *oppResult = 2;
1360 } else {
1361 *oppResult = 1;
1362 }
1363 } else {
1364 *oppResult = 1;
1365 }
1366 return hullResult;
1367 }
1368 if (span->fIsLine && oppSpan->fIsLine) {
1369 SkIntersections i;
1370 int sects = this->linesIntersect(span, opp, oppSpan, &i);
caryclark26ad22a2015-10-16 09:03:38 -07001371 if (sects == 2) {
1372 return *oppResult = 1;
1373 }
caryclark54359292015-03-26 07:52:43 -07001374 if (!sects) {
1375 return -1;
1376 }
caryclark34efb702016-10-24 08:19:06 -07001377 this->removedEndCheck(span);
caryclark54359292015-03-26 07:52:43 -07001378 span->fStartT = span->fEndT = i[0][0];
caryclark34efb702016-10-24 08:19:06 -07001379 opp->removedEndCheck(oppSpan);
caryclark54359292015-03-26 07:52:43 -07001380 oppSpan->fStartT = oppSpan->fEndT = i[1][0];
1381 return *oppResult = 2;
1382 }
1383 if (span->fIsLinear || oppSpan->fIsLinear) {
1384 return *oppResult = (int) span->linearsIntersect(oppSpan);
1385 }
1386 return *oppResult = 1;
1387}
1388
caryclarked0935a2015-10-22 07:23:52 -07001389template<typename TCurve>
1390static bool is_parallel(const SkDLine& thisLine, const TCurve& opp) {
1391 if (!opp.IsConic()) {
1392 return false; // FIXME : breaks a lot of stuff now
1393 }
1394 int finds = 0;
1395 SkDLine thisPerp;
1396 thisPerp.fPts[0].fX = thisLine.fPts[1].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1397 thisPerp.fPts[0].fY = thisLine.fPts[1].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1398 thisPerp.fPts[1] = thisLine.fPts[1];
1399 SkIntersections perpRayI;
1400 perpRayI.intersectRay(opp, thisPerp);
1401 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1402 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[1]);
1403 }
1404 thisPerp.fPts[1].fX = thisLine.fPts[0].fX + (thisLine.fPts[1].fY - thisLine.fPts[0].fY);
1405 thisPerp.fPts[1].fY = thisLine.fPts[0].fY + (thisLine.fPts[0].fX - thisLine.fPts[1].fX);
1406 thisPerp.fPts[0] = thisLine.fPts[0];
1407 perpRayI.intersectRay(opp, thisPerp);
1408 for (int pIndex = 0; pIndex < perpRayI.used(); ++pIndex) {
1409 finds += perpRayI.pt(pIndex).approximatelyEqual(thisPerp.fPts[0]);
1410 }
1411 return finds >= 2;
1412}
1413
caryclark54359292015-03-26 07:52:43 -07001414// while the intersection points are sufficiently far apart:
1415// construct the tangent lines from the intersections
1416// find the point where the tangent line intersects the opposite curve
caryclark1049f122015-04-20 08:31:59 -07001417template<typename TCurve, typename OppCurve>
1418int SkTSect<TCurve, OppCurve>::linesIntersect(SkTSpan<TCurve, OppCurve>* span,
1419 SkTSect<OppCurve, TCurve>* opp,
1420 SkTSpan<OppCurve, TCurve>* oppSpan, SkIntersections* i) {
caryclarka35ab3e2016-10-20 08:32:18 -07001421 SkIntersections thisRayI SkDEBUGCODE((span->fDebugGlobalState));
1422 SkIntersections oppRayI SkDEBUGCODE((span->fDebugGlobalState));
caryclark54359292015-03-26 07:52:43 -07001423 SkDLine thisLine = {{ span->fPart[0], span->fPart[TCurve::kPointLast] }};
caryclark1049f122015-04-20 08:31:59 -07001424 SkDLine oppLine = {{ oppSpan->fPart[0], oppSpan->fPart[OppCurve::kPointLast] }};
caryclark54359292015-03-26 07:52:43 -07001425 int loopCount = 0;
1426 double bestDistSq = DBL_MAX;
caryclark1049f122015-04-20 08:31:59 -07001427 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1428 return 0;
1429 }
1430 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1431 return 0;
1432 }
caryclark26ad22a2015-10-16 09:03:38 -07001433 // if the ends of each line intersect the opposite curve, the lines are coincident
1434 if (thisRayI.used() > 1) {
1435 int ptMatches = 0;
1436 for (int tIndex = 0; tIndex < thisRayI.used(); ++tIndex) {
1437 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(thisLine.fPts); ++lIndex) {
1438 ptMatches += thisRayI.pt(tIndex).approximatelyEqual(thisLine.fPts[lIndex]);
1439 }
1440 }
caryclarked0935a2015-10-22 07:23:52 -07001441 if (ptMatches == 2 || is_parallel(thisLine, opp->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001442 return 2;
1443 }
1444 }
1445 if (oppRayI.used() > 1) {
1446 int ptMatches = 0;
1447 for (int oIndex = 0; oIndex < oppRayI.used(); ++oIndex) {
1448 for (int lIndex = 0; lIndex < (int) SK_ARRAY_COUNT(thisLine.fPts); ++lIndex) {
1449 ptMatches += oppRayI.pt(oIndex).approximatelyEqual(oppLine.fPts[lIndex]);
1450 }
1451 }
caryclarked0935a2015-10-22 07:23:52 -07001452 if (ptMatches == 2|| is_parallel(oppLine, this->fCurve)) {
caryclark26ad22a2015-10-16 09:03:38 -07001453 return 2;
1454 }
1455 }
caryclark54359292015-03-26 07:52:43 -07001456 do {
caryclark54359292015-03-26 07:52:43 -07001457 // pick the closest pair of points
1458 double closest = DBL_MAX;
1459 int closeIndex SK_INIT_TO_AVOID_WARNING;
1460 int oppCloseIndex SK_INIT_TO_AVOID_WARNING;
1461 for (int index = 0; index < oppRayI.used(); ++index) {
1462 if (!roughly_between(span->fStartT, oppRayI[0][index], span->fEndT)) {
1463 continue;
1464 }
1465 for (int oIndex = 0; oIndex < thisRayI.used(); ++oIndex) {
1466 if (!roughly_between(oppSpan->fStartT, thisRayI[0][oIndex], oppSpan->fEndT)) {
1467 continue;
1468 }
1469 double distSq = thisRayI.pt(index).distanceSquared(oppRayI.pt(oIndex));
1470 if (closest > distSq) {
1471 closest = distSq;
1472 closeIndex = index;
1473 oppCloseIndex = oIndex;
caryclarkccec0f92015-03-24 07:28:17 -07001474 }
caryclarkccec0f92015-03-24 07:28:17 -07001475 }
reed0dc4dd62015-03-24 13:55:33 -07001476 }
caryclark54359292015-03-26 07:52:43 -07001477 if (closest == DBL_MAX) {
caryclark1049f122015-04-20 08:31:59 -07001478 break;
reed0dc4dd62015-03-24 13:55:33 -07001479 }
caryclark54359292015-03-26 07:52:43 -07001480 const SkDPoint& oppIPt = thisRayI.pt(oppCloseIndex);
1481 const SkDPoint& iPt = oppRayI.pt(closeIndex);
1482 if (between(span->fStartT, oppRayI[0][closeIndex], span->fEndT)
1483 && between(oppSpan->fStartT, thisRayI[0][oppCloseIndex], oppSpan->fEndT)
1484 && oppIPt.approximatelyEqual(iPt)) {
1485 i->merge(oppRayI, closeIndex, thisRayI, oppCloseIndex);
1486 return i->used();
1487 }
1488 double distSq = oppIPt.distanceSquared(iPt);
1489 if (bestDistSq < distSq || ++loopCount > 5) {
caryclark1049f122015-04-20 08:31:59 -07001490 return 0;
caryclark54359292015-03-26 07:52:43 -07001491 }
1492 bestDistSq = distSq;
caryclark1049f122015-04-20 08:31:59 -07001493 double oppStart = oppRayI[0][closeIndex];
1494 thisLine[0] = fCurve.ptAtT(oppStart);
1495 thisLine[1] = thisLine[0] + fCurve.dxdyAtT(oppStart);
1496 if (!thisRayI.intersectRay(opp->fCurve, thisLine)) {
1497 break;
1498 }
1499 double start = thisRayI[0][oppCloseIndex];
1500 oppLine[0] = opp->fCurve.ptAtT(start);
1501 oppLine[1] = oppLine[0] + opp->fCurve.dxdyAtT(start);
1502 if (!oppRayI.intersectRay(this->fCurve, oppLine)) {
1503 break;
1504 }
caryclark54359292015-03-26 07:52:43 -07001505 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001506 // convergence may fail if the curves are nearly coincident
1507 SkTCoincident<OppCurve, TCurve> oCoinS, oCoinE;
1508 oCoinS.setPerp(opp->fCurve, oppSpan->fStartT, oppSpan->fPart[0], fCurve);
1509 oCoinE.setPerp(opp->fCurve, oppSpan->fEndT, oppSpan->fPart[OppCurve::kPointLast], fCurve);
1510 double tStart = oCoinS.perpT();
1511 double tEnd = oCoinE.perpT();
1512 bool swap = tStart > tEnd;
1513 if (swap) {
1514 SkTSwap(tStart, tEnd);
1515 }
1516 tStart = SkTMax(tStart, span->fStartT);
1517 tEnd = SkTMin(tEnd, span->fEndT);
1518 if (tStart > tEnd) {
1519 return 0;
1520 }
1521 SkDVector perpS, perpE;
1522 if (tStart == span->fStartT) {
1523 SkTCoincident<TCurve, OppCurve> coinS;
1524 coinS.setPerp(fCurve, span->fStartT, span->fPart[0], opp->fCurve);
1525 perpS = span->fPart[0] - coinS.perpPt();
1526 } else if (swap) {
1527 perpS = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1528 } else {
1529 perpS = oCoinS.perpPt() - oppSpan->fPart[0];
1530 }
1531 if (tEnd == span->fEndT) {
1532 SkTCoincident<TCurve, OppCurve> coinE;
1533 coinE.setPerp(fCurve, span->fEndT, span->fPart[TCurve::kPointLast], opp->fCurve);
1534 perpE = span->fPart[TCurve::kPointLast] - coinE.perpPt();
1535 } else if (swap) {
1536 perpE = oCoinS.perpPt() - oppSpan->fPart[0];
1537 } else {
1538 perpE = oCoinE.perpPt() - oppSpan->fPart[OppCurve::kPointLast];
1539 }
1540 if (perpS.dot(perpE) >= 0) {
1541 return 0;
1542 }
1543 SkTCoincident<TCurve, OppCurve> coinW;
1544 double workT = tStart;
1545 double tStep = tEnd - tStart;
1546 SkDPoint workPt;
1547 do {
1548 tStep *= 0.5;
1549 if (precisely_zero(tStep)) {
1550 return 0;
1551 }
1552 workT += tStep;
1553 workPt = fCurve.ptAtT(workT);
1554 coinW.setPerp(fCurve, workT, workPt, opp->fCurve);
caryclark27c015d2016-09-23 05:47:20 -07001555 double perpT = coinW.perpT();
caryclark6c3b9cd2016-09-26 05:36:58 -07001556 if (coinW.isMatch() ? !between(oppSpan->fStartT, perpT, oppSpan->fEndT) : perpT < 0) {
caryclarkb6693002015-12-16 12:28:35 -08001557 continue;
1558 }
caryclark1049f122015-04-20 08:31:59 -07001559 SkDVector perpW = workPt - coinW.perpPt();
1560 if ((perpS.dot(perpW) >= 0) == (tStep < 0)) {
1561 tStep = -tStep;
1562 }
caryclarkb6693002015-12-16 12:28:35 -08001563 if (workPt.approximatelyEqual(coinW.perpPt())) {
1564 break;
1565 }
1566 } while (true);
caryclark1049f122015-04-20 08:31:59 -07001567 double oppTTest = coinW.perpT();
1568 if (!opp->fHead->contains(oppTTest)) {
1569 return 0;
1570 }
1571 i->setMax(1);
1572 i->insert(workT, oppTTest, workPt);
1573 return 1;
caryclark54359292015-03-26 07:52:43 -07001574}
1575
1576
caryclark1049f122015-04-20 08:31:59 -07001577template<typename TCurve, typename OppCurve>
caryclarkef7cee42016-09-06 09:05:54 -07001578bool SkTSect<TCurve, OppCurve>::markSpanGone(SkTSpan<TCurve, OppCurve>* span) {
1579 if (--fActiveCount < 0) {
1580 return false;
1581 }
caryclark54359292015-03-26 07:52:43 -07001582 span->fNext = fDeleted;
1583 fDeleted = span;
caryclarke25a4f62016-07-26 09:26:29 -07001584 SkOPASSERT(!span->fDeleted);
caryclark54359292015-03-26 07:52:43 -07001585 span->fDeleted = true;
caryclarkef7cee42016-09-06 09:05:54 -07001586 return true;
caryclark54359292015-03-26 07:52:43 -07001587}
1588
caryclark1049f122015-04-20 08:31:59 -07001589template<typename TCurve, typename OppCurve>
1590bool SkTSect<TCurve, OppCurve>::matchedDirection(double t, const SkTSect<OppCurve, TCurve>* sect2,
1591 double t2) const {
caryclark54359292015-03-26 07:52:43 -07001592 SkDVector dxdy = this->fCurve.dxdyAtT(t);
1593 SkDVector dxdy2 = sect2->fCurve.dxdyAtT(t2);
1594 return dxdy.dot(dxdy2) >= 0;
1595}
1596
caryclark1049f122015-04-20 08:31:59 -07001597template<typename TCurve, typename OppCurve>
1598void SkTSect<TCurve, OppCurve>::matchedDirCheck(double t, const SkTSect<OppCurve, TCurve>* sect2,
1599 double t2, bool* calcMatched, bool* oppMatched) const {
caryclark54359292015-03-26 07:52:43 -07001600 if (*calcMatched) {
caryclark55888e42016-07-18 10:01:36 -07001601 SkASSERT(*oppMatched == this->matchedDirection(t, sect2, t2));
caryclark54359292015-03-26 07:52:43 -07001602 } else {
1603 *oppMatched = this->matchedDirection(t, sect2, t2);
1604 *calcMatched = true;
1605 }
1606}
1607
caryclark1049f122015-04-20 08:31:59 -07001608template<typename TCurve, typename OppCurve>
1609void SkTSect<TCurve, OppCurve>::mergeCoincidence(SkTSect<OppCurve, TCurve>* sect2) {
caryclark54359292015-03-26 07:52:43 -07001610 double smallLimit = 0;
1611 do {
1612 // find the smallest unprocessed span
halcanary96fcdcc2015-08-27 07:41:13 -07001613 SkTSpan<TCurve, OppCurve>* smaller = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001614 SkTSpan<TCurve, OppCurve>* test = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001615 do {
caryclark221a4bb2016-10-07 11:15:15 -07001616 if (!test) {
1617 return;
1618 }
caryclark54359292015-03-26 07:52:43 -07001619 if (test->fStartT < smallLimit) {
1620 continue;
1621 }
1622 if (smaller && smaller->fEndT < test->fStartT) {
1623 continue;
1624 }
1625 smaller = test;
1626 } while ((test = test->fNext));
1627 if (!smaller) {
1628 return;
1629 }
1630 smallLimit = smaller->fEndT;
1631 // find next larger span
halcanary96fcdcc2015-08-27 07:41:13 -07001632 SkTSpan<TCurve, OppCurve>* prior = nullptr;
1633 SkTSpan<TCurve, OppCurve>* larger = nullptr;
1634 SkTSpan<TCurve, OppCurve>* largerPrior = nullptr;
caryclark54359292015-03-26 07:52:43 -07001635 test = fCoincident;
1636 do {
1637 if (test->fStartT < smaller->fEndT) {
1638 continue;
1639 }
caryclark221a4bb2016-10-07 11:15:15 -07001640 SkOPASSERT(test->fStartT != smaller->fEndT);
caryclark54359292015-03-26 07:52:43 -07001641 if (larger && larger->fStartT < test->fStartT) {
1642 continue;
1643 }
1644 largerPrior = prior;
1645 larger = test;
1646 } while ((prior = test), (test = test->fNext));
1647 if (!larger) {
1648 continue;
1649 }
1650 // check middle t value to see if it is coincident as well
1651 double midT = (smaller->fEndT + larger->fStartT) / 2;
1652 SkDPoint midPt = fCurve.ptAtT(midT);
caryclark1049f122015-04-20 08:31:59 -07001653 SkTCoincident<TCurve, OppCurve> coin;
caryclark54359292015-03-26 07:52:43 -07001654 coin.setPerp(fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07001655 if (coin.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07001656 smaller->fEndT = larger->fEndT;
1657 smaller->fCoinEnd = larger->fCoinEnd;
1658 if (largerPrior) {
1659 largerPrior->fNext = larger->fNext;
caryclark643ede62016-08-08 14:27:45 -07001660 largerPrior->validate();
caryclark54359292015-03-26 07:52:43 -07001661 } else {
1662 fCoincident = larger->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001663 }
1664 }
caryclark54359292015-03-26 07:52:43 -07001665 } while (true);
1666}
1667
caryclark1049f122015-04-20 08:31:59 -07001668template<typename TCurve, typename OppCurve>
1669SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::prev(
1670 SkTSpan<TCurve, OppCurve>* span) const {
halcanary96fcdcc2015-08-27 07:41:13 -07001671 SkTSpan<TCurve, OppCurve>* result = nullptr;
caryclark1049f122015-04-20 08:31:59 -07001672 SkTSpan<TCurve, OppCurve>* test = fHead;
caryclark54359292015-03-26 07:52:43 -07001673 while (span != test) {
1674 result = test;
1675 test = test->fNext;
1676 SkASSERT(test);
caryclarkccec0f92015-03-24 07:28:17 -07001677 }
caryclark55888e42016-07-18 10:01:36 -07001678 return result;
caryclarkccec0f92015-03-24 07:28:17 -07001679}
1680
caryclark1049f122015-04-20 08:31:59 -07001681template<typename TCurve, typename OppCurve>
1682void SkTSect<TCurve, OppCurve>::recoverCollapsed() {
1683 SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001684 while (deleted) {
caryclark1049f122015-04-20 08:31:59 -07001685 SkTSpan<TCurve, OppCurve>* delNext = deleted->fNext;
caryclark45fa4472015-01-16 07:04:10 -08001686 if (deleted->fCollapsed) {
caryclark1049f122015-04-20 08:31:59 -07001687 SkTSpan<TCurve, OppCurve>** spanPtr = &fHead;
caryclark45fa4472015-01-16 07:04:10 -08001688 while (*spanPtr && (*spanPtr)->fEndT <= deleted->fStartT) {
1689 spanPtr = &(*spanPtr)->fNext;
1690 }
1691 deleted->fNext = *spanPtr;
1692 *spanPtr = deleted;
1693 }
1694 deleted = delNext;
1695 }
1696}
1697
caryclark1049f122015-04-20 08:31:59 -07001698template<typename TCurve, typename OppCurve>
1699void SkTSect<TCurve, OppCurve>::removeAllBut(const SkTSpan<OppCurve, TCurve>* keep,
1700 SkTSpan<TCurve, OppCurve>* span, SkTSect<OppCurve, TCurve>* opp) {
1701 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001702 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001703 SkTSpan<OppCurve, TCurve>* bounded = testBounded->fBounded;
1704 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001705 // may have been deleted when opp did 'remove all but'
1706 if (bounded != keep && !bounded->fDeleted) {
1707 SkAssertResult(SkDEBUGCODE(!) span->removeBounded(bounded));
1708 if (bounded->removeBounded(span)) {
1709 opp->removeSpan(bounded);
1710 }
caryclarkccec0f92015-03-24 07:28:17 -07001711 }
caryclark54359292015-03-26 07:52:43 -07001712 testBounded = next;
caryclarkccec0f92015-03-24 07:28:17 -07001713 }
caryclark54359292015-03-26 07:52:43 -07001714 SkASSERT(!span->fDeleted);
1715 SkASSERT(span->findOppSpan(keep));
1716 SkASSERT(keep->findOppSpan(span));
caryclarkccec0f92015-03-24 07:28:17 -07001717}
1718
caryclark1049f122015-04-20 08:31:59 -07001719template<typename TCurve, typename OppCurve>
1720void SkTSect<TCurve, OppCurve>::removeByPerpendicular(SkTSect<OppCurve, TCurve>* opp) {
1721 SkTSpan<TCurve, OppCurve>* test = fHead;
1722 SkTSpan<TCurve, OppCurve>* next;
caryclark54359292015-03-26 07:52:43 -07001723 do {
1724 next = test->fNext;
1725 if (test->fCoinStart.perpT() < 0 || test->fCoinEnd.perpT() < 0) {
1726 continue;
reed0dc4dd62015-03-24 13:55:33 -07001727 }
caryclark54359292015-03-26 07:52:43 -07001728 SkDVector startV = test->fCoinStart.perpPt() - test->fPart[0];
1729 SkDVector endV = test->fCoinEnd.perpPt() - test->fPart[TCurve::kPointLast];
1730#if DEBUG_T_SECT
1731 SkDebugf("%s startV=(%1.9g,%1.9g) endV=(%1.9g,%1.9g) dot=%1.9g\n", __FUNCTION__,
1732 startV.fX, startV.fY, endV.fX, endV.fY, startV.dot(endV));
1733#endif
1734 if (startV.dot(endV) <= 0) {
1735 continue;
1736 }
1737 this->removeSpans(test, opp);
1738 } while ((test = next));
1739}
1740
caryclark1049f122015-04-20 08:31:59 -07001741template<typename TCurve, typename OppCurve>
1742void SkTSect<TCurve, OppCurve>::removeCoincident(SkTSpan<TCurve, OppCurve>* span, bool isBetween) {
caryclark54359292015-03-26 07:52:43 -07001743 this->unlinkSpan(span);
1744 if (isBetween || between(0, span->fCoinStart.perpT(), 1)) {
1745 --fActiveCount;
1746 span->fNext = fCoincident;
1747 fCoincident = span;
1748 } else {
1749 this->markSpanGone(span);
reed0dc4dd62015-03-24 13:55:33 -07001750 }
caryclarkccec0f92015-03-24 07:28:17 -07001751}
1752
caryclark1049f122015-04-20 08:31:59 -07001753template<typename TCurve, typename OppCurve>
caryclark34efb702016-10-24 08:19:06 -07001754void SkTSect<TCurve, OppCurve>::removedEndCheck(SkTSpan<TCurve, OppCurve>* span) {
caryclark6c3b9cd2016-09-26 05:36:58 -07001755 if (!span->fStartT) {
1756 fRemovedStartT = true;
1757 }
1758 if (1 == span->fEndT) {
1759 fRemovedEndT = true;
1760 }
caryclark34efb702016-10-24 08:19:06 -07001761}
1762
1763template<typename TCurve, typename OppCurve>
1764bool SkTSect<TCurve, OppCurve>::removeSpan(SkTSpan<TCurve, OppCurve>* span) {\
1765 this->removedEndCheck(span);
caryclark54359292015-03-26 07:52:43 -07001766 this->unlinkSpan(span);
caryclarkef7cee42016-09-06 09:05:54 -07001767 return this->markSpanGone(span);
caryclark54359292015-03-26 07:52:43 -07001768}
1769
caryclark1049f122015-04-20 08:31:59 -07001770template<typename TCurve, typename OppCurve>
1771void SkTSect<TCurve, OppCurve>::removeSpanRange(SkTSpan<TCurve, OppCurve>* first,
1772 SkTSpan<TCurve, OppCurve>* last) {
caryclark54359292015-03-26 07:52:43 -07001773 if (first == last) {
1774 return;
1775 }
caryclark1049f122015-04-20 08:31:59 -07001776 SkTSpan<TCurve, OppCurve>* span = first;
caryclark54359292015-03-26 07:52:43 -07001777 SkASSERT(span);
caryclark1049f122015-04-20 08:31:59 -07001778 SkTSpan<TCurve, OppCurve>* final = last->fNext;
1779 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001780 while ((span = next) && span != final) {
1781 next = span->fNext;
1782 this->markSpanGone(span);
1783 }
1784 if (final) {
1785 final->fPrev = first;
1786 }
1787 first->fNext = final;
caryclark643ede62016-08-08 14:27:45 -07001788 first->validate();
caryclark54359292015-03-26 07:52:43 -07001789}
1790
caryclark1049f122015-04-20 08:31:59 -07001791template<typename TCurve, typename OppCurve>
1792void SkTSect<TCurve, OppCurve>::removeSpans(SkTSpan<TCurve, OppCurve>* span,
1793 SkTSect<OppCurve, TCurve>* opp) {
1794 SkTSpanBounded<OppCurve, TCurve>* bounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001795 while (bounded) {
caryclark1049f122015-04-20 08:31:59 -07001796 SkTSpan<OppCurve, TCurve>* spanBounded = bounded->fBounded;
1797 SkTSpanBounded<OppCurve, TCurve>* next = bounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001798 if (span->removeBounded(spanBounded)) { // shuffles last into position 0
1799 this->removeSpan(span);
1800 }
1801 if (spanBounded->removeBounded(span)) {
1802 opp->removeSpan(spanBounded);
1803 }
1804 SkASSERT(!span->fDeleted || !opp->debugHasBounded(span));
1805 bounded = next;
reed0dc4dd62015-03-24 13:55:33 -07001806 }
1807}
caryclarkccec0f92015-03-24 07:28:17 -07001808
caryclark1049f122015-04-20 08:31:59 -07001809template<typename TCurve, typename OppCurve>
1810SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::spanAtT(double t,
1811 SkTSpan<TCurve, OppCurve>** priorSpan) {
1812 SkTSpan<TCurve, OppCurve>* test = fHead;
halcanary96fcdcc2015-08-27 07:41:13 -07001813 SkTSpan<TCurve, OppCurve>* prev = nullptr;
caryclark54359292015-03-26 07:52:43 -07001814 while (test && test->fEndT < t) {
1815 prev = test;
1816 test = test->fNext;
reed0dc4dd62015-03-24 13:55:33 -07001817 }
caryclark54359292015-03-26 07:52:43 -07001818 *priorSpan = prev;
halcanary96fcdcc2015-08-27 07:41:13 -07001819 return test && test->fStartT <= t ? test : nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001820}
1821
caryclark1049f122015-04-20 08:31:59 -07001822template<typename TCurve, typename OppCurve>
1823SkTSpan<TCurve, OppCurve>* SkTSect<TCurve, OppCurve>::tail() {
1824 SkTSpan<TCurve, OppCurve>* result = fHead;
1825 SkTSpan<TCurve, OppCurve>* next = fHead;
reed0dc4dd62015-03-24 13:55:33 -07001826 while ((next = next->fNext)) {
1827 if (next->fEndT > result->fEndT) {
1828 result = next;
1829 }
1830 }
1831 return result;
1832}
1833
1834/* Each span has a range of opposite spans it intersects. After the span is split in two,
1835 adjust the range to its new size */
caryclark1049f122015-04-20 08:31:59 -07001836template<typename TCurve, typename OppCurve>
caryclarka35ab3e2016-10-20 08:32:18 -07001837bool SkTSect<TCurve, OppCurve>::trim(SkTSpan<TCurve, OppCurve>* span,
caryclark1049f122015-04-20 08:31:59 -07001838 SkTSect<OppCurve, TCurve>* opp) {
caryclarka35ab3e2016-10-20 08:32:18 -07001839 FAIL_IF(!span->initBounds(fCurve));
caryclark1049f122015-04-20 08:31:59 -07001840 const SkTSpanBounded<OppCurve, TCurve>* testBounded = span->fBounded;
caryclark54359292015-03-26 07:52:43 -07001841 while (testBounded) {
caryclark1049f122015-04-20 08:31:59 -07001842 SkTSpan<OppCurve, TCurve>* test = testBounded->fBounded;
1843 const SkTSpanBounded<OppCurve, TCurve>* next = testBounded->fNext;
caryclark54359292015-03-26 07:52:43 -07001844 int oppSects, sects = this->intersects(span, opp, test, &oppSects);
1845 if (sects >= 1) {
1846 if (oppSects == 2) {
1847 test->initBounds(opp->fCurve);
1848 opp->removeAllBut(span, test, this);
1849 }
1850 if (sects == 2) {
1851 span->initBounds(fCurve);
1852 this->removeAllBut(test, span, opp);
caryclarka35ab3e2016-10-20 08:32:18 -07001853 return true;
caryclark54359292015-03-26 07:52:43 -07001854 }
reed0dc4dd62015-03-24 13:55:33 -07001855 } else {
caryclark54359292015-03-26 07:52:43 -07001856 if (span->removeBounded(test)) {
1857 this->removeSpan(span);
1858 }
1859 if (test->removeBounded(span)) {
1860 opp->removeSpan(test);
1861 }
1862 }
1863 testBounded = next;
1864 }
caryclarka35ab3e2016-10-20 08:32:18 -07001865 return true;
caryclark54359292015-03-26 07:52:43 -07001866}
1867
caryclark1049f122015-04-20 08:31:59 -07001868template<typename TCurve, typename OppCurve>
1869void SkTSect<TCurve, OppCurve>::unlinkSpan(SkTSpan<TCurve, OppCurve>* span) {
1870 SkTSpan<TCurve, OppCurve>* prev = span->fPrev;
1871 SkTSpan<TCurve, OppCurve>* next = span->fNext;
caryclark54359292015-03-26 07:52:43 -07001872 if (prev) {
1873 prev->fNext = next;
1874 if (next) {
1875 next->fPrev = prev;
caryclark643ede62016-08-08 14:27:45 -07001876 next->validate();
caryclark54359292015-03-26 07:52:43 -07001877 }
1878 } else {
1879 fHead = next;
1880 if (next) {
halcanary96fcdcc2015-08-27 07:41:13 -07001881 next->fPrev = nullptr;
reed0dc4dd62015-03-24 13:55:33 -07001882 }
1883 }
1884}
1885
caryclark1049f122015-04-20 08:31:59 -07001886template<typename TCurve, typename OppCurve>
1887bool SkTSect<TCurve, OppCurve>::updateBounded(SkTSpan<TCurve, OppCurve>* first,
1888 SkTSpan<TCurve, OppCurve>* last, SkTSpan<OppCurve, TCurve>* oppFirst) {
1889 SkTSpan<TCurve, OppCurve>* test = first;
1890 const SkTSpan<TCurve, OppCurve>* final = last->next();
caryclark54359292015-03-26 07:52:43 -07001891 bool deleteSpan = false;
1892 do {
1893 deleteSpan |= test->removeAllBounded();
caryclarke25a4f62016-07-26 09:26:29 -07001894 } while ((test = test->fNext) != final && test);
halcanary96fcdcc2015-08-27 07:41:13 -07001895 first->fBounded = nullptr;
caryclark54359292015-03-26 07:52:43 -07001896 first->addBounded(oppFirst, &fHeap);
1897 // cannot call validate until remove span range is called
1898 return deleteSpan;
1899}
1900
1901
caryclark1049f122015-04-20 08:31:59 -07001902template<typename TCurve, typename OppCurve>
1903void SkTSect<TCurve, OppCurve>::validate() const {
caryclark643ede62016-08-08 14:27:45 -07001904#if DEBUG_VALIDATE
caryclark45fa4472015-01-16 07:04:10 -08001905 int count = 0;
caryclark643ede62016-08-08 14:27:45 -07001906 double last = 0;
caryclark45fa4472015-01-16 07:04:10 -08001907 if (fHead) {
caryclark1049f122015-04-20 08:31:59 -07001908 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark45fa4472015-01-16 07:04:10 -08001909 SkASSERT(!span->fPrev);
caryclark643ede62016-08-08 14:27:45 -07001910 const SkTSpan<TCurve, OppCurve>* next;
caryclark45fa4472015-01-16 07:04:10 -08001911 do {
1912 span->validate();
1913 SkASSERT(span->fStartT >= last);
caryclark643ede62016-08-08 14:27:45 -07001914 last = span->fEndT;
caryclark45fa4472015-01-16 07:04:10 -08001915 ++count;
caryclark643ede62016-08-08 14:27:45 -07001916 next = span->fNext;
1917 SkASSERT(next != span);
1918 } while ((span = next) != nullptr);
caryclark45fa4472015-01-16 07:04:10 -08001919 }
1920 SkASSERT(count == fActiveCount);
caryclark643ede62016-08-08 14:27:45 -07001921#endif
1922#if DEBUG_T_SECT
caryclark45fa4472015-01-16 07:04:10 -08001923 SkASSERT(fActiveCount <= fDebugAllocatedCount);
1924 int deletedCount = 0;
caryclark1049f122015-04-20 08:31:59 -07001925 const SkTSpan<TCurve, OppCurve>* deleted = fDeleted;
caryclark45fa4472015-01-16 07:04:10 -08001926 while (deleted) {
1927 ++deletedCount;
1928 deleted = deleted->fNext;
1929 }
caryclark1049f122015-04-20 08:31:59 -07001930 const SkTSpan<TCurve, OppCurve>* coincident = fCoincident;
caryclark54359292015-03-26 07:52:43 -07001931 while (coincident) {
1932 ++deletedCount;
1933 coincident = coincident->fNext;
1934 }
caryclark45fa4472015-01-16 07:04:10 -08001935 SkASSERT(fActiveCount + deletedCount == fDebugAllocatedCount);
caryclarkccec0f92015-03-24 07:28:17 -07001936#endif
caryclark54359292015-03-26 07:52:43 -07001937}
1938
caryclark1049f122015-04-20 08:31:59 -07001939template<typename TCurve, typename OppCurve>
1940void SkTSect<TCurve, OppCurve>::validateBounded() const {
caryclark643ede62016-08-08 14:27:45 -07001941#if DEBUG_VALIDATE
caryclark54359292015-03-26 07:52:43 -07001942 if (!fHead) {
1943 return;
1944 }
caryclark1049f122015-04-20 08:31:59 -07001945 const SkTSpan<TCurve, OppCurve>* span = fHead;
caryclark54359292015-03-26 07:52:43 -07001946 do {
1947 span->validateBounded();
halcanary96fcdcc2015-08-27 07:41:13 -07001948 } while ((span = span->fNext) != nullptr);
caryclark54359292015-03-26 07:52:43 -07001949#endif
1950}
caryclark45fa4472015-01-16 07:04:10 -08001951
caryclark1049f122015-04-20 08:31:59 -07001952template<typename TCurve, typename OppCurve>
1953int SkTSect<TCurve, OppCurve>::EndsEqual(const SkTSect<TCurve, OppCurve>* sect1,
1954 const SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark45fa4472015-01-16 07:04:10 -08001955 int zeroOneSet = 0;
caryclark54359292015-03-26 07:52:43 -07001956 if (sect1->fCurve[0] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001957 zeroOneSet |= kZeroS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001958 intersections->insert(0, 0, sect1->fCurve[0]);
1959 }
caryclark1049f122015-04-20 08:31:59 -07001960 if (sect1->fCurve[0] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001961 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark54359292015-03-26 07:52:43 -07001962 intersections->insert(0, 1, sect1->fCurve[0]);
1963 }
1964 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[0]) {
caryclarkccec0f92015-03-24 07:28:17 -07001965 zeroOneSet |= kOneS1Set | kZeroS2Set;
caryclark54359292015-03-26 07:52:43 -07001966 intersections->insert(1, 0, sect1->fCurve[TCurve::kPointLast]);
1967 }
caryclark1049f122015-04-20 08:31:59 -07001968 if (sect1->fCurve[TCurve::kPointLast] == sect2->fCurve[OppCurve::kPointLast]) {
caryclarkccec0f92015-03-24 07:28:17 -07001969 zeroOneSet |= kOneS1Set | kOneS2Set;
reed0dc4dd62015-03-24 13:55:33 -07001970 intersections->insert(1, 1, sect1->fCurve[TCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001971 }
1972 // check for zero
1973 if (!(zeroOneSet & (kZeroS1Set | kZeroS2Set))
1974 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[0])) {
1975 zeroOneSet |= kZeroS1Set | kZeroS2Set;
1976 intersections->insertNear(0, 0, sect1->fCurve[0], sect2->fCurve[0]);
1977 }
1978 if (!(zeroOneSet & (kZeroS1Set | kOneS2Set))
caryclark1049f122015-04-20 08:31:59 -07001979 && sect1->fCurve[0].approximatelyEqual(sect2->fCurve[OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07001980 zeroOneSet |= kZeroS1Set | kOneS2Set;
caryclark1049f122015-04-20 08:31:59 -07001981 intersections->insertNear(0, 1, sect1->fCurve[0], sect2->fCurve[OppCurve::kPointLast]);
caryclark54359292015-03-26 07:52:43 -07001982 }
1983 // check for one
1984 if (!(zeroOneSet & (kOneS1Set | kZeroS2Set))
1985 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[0])) {
1986 zeroOneSet |= kOneS1Set | kZeroS2Set;
1987 intersections->insertNear(1, 0, sect1->fCurve[TCurve::kPointLast], sect2->fCurve[0]);
1988 }
1989 if (!(zeroOneSet & (kOneS1Set | kOneS2Set))
1990 && sect1->fCurve[TCurve::kPointLast].approximatelyEqual(sect2->fCurve[
caryclark1049f122015-04-20 08:31:59 -07001991 OppCurve::kPointLast])) {
caryclark54359292015-03-26 07:52:43 -07001992 zeroOneSet |= kOneS1Set | kOneS2Set;
1993 intersections->insertNear(1, 1, sect1->fCurve[TCurve::kPointLast],
caryclark1049f122015-04-20 08:31:59 -07001994 sect2->fCurve[OppCurve::kPointLast]);
caryclark45fa4472015-01-16 07:04:10 -08001995 }
1996 return zeroOneSet;
1997}
1998
caryclark1049f122015-04-20 08:31:59 -07001999template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08002000struct SkClosestRecord {
caryclark54359292015-03-26 07:52:43 -07002001 bool operator<(const SkClosestRecord& rh) const {
2002 return fClosest < rh.fClosest;
2003 }
2004
caryclark45fa4472015-01-16 07:04:10 -08002005 void addIntersection(SkIntersections* intersections) const {
2006 double r1t = fC1Index ? fC1Span->endT() : fC1Span->startT();
2007 double r2t = fC2Index ? fC2Span->endT() : fC2Span->startT();
2008 intersections->insert(r1t, r2t, fC1Span->part()[fC1Index]);
2009 }
2010
caryclark1049f122015-04-20 08:31:59 -07002011 void findEnd(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2,
caryclark45fa4472015-01-16 07:04:10 -08002012 int c1Index, int c2Index) {
2013 const TCurve& c1 = span1->part();
caryclark1049f122015-04-20 08:31:59 -07002014 const OppCurve& c2 = span2->part();
caryclark45fa4472015-01-16 07:04:10 -08002015 if (!c1[c1Index].approximatelyEqual(c2[c2Index])) {
2016 return;
2017 }
2018 double dist = c1[c1Index].distanceSquared(c2[c2Index]);
2019 if (fClosest < dist) {
2020 return;
2021 }
2022 fC1Span = span1;
2023 fC2Span = span2;
2024 fC1StartT = span1->startT();
2025 fC1EndT = span1->endT();
2026 fC2StartT = span2->startT();
2027 fC2EndT = span2->endT();
2028 fC1Index = c1Index;
2029 fC2Index = c2Index;
2030 fClosest = dist;
2031 }
2032
caryclarkcdeff812016-07-22 03:34:19 -07002033 bool matesWith(const SkClosestRecord& mate SkDEBUGPARAMS(SkIntersections* i)) const {
caryclark45fa4472015-01-16 07:04:10 -08002034 SkASSERT(fC1Span == mate.fC1Span || fC1Span->endT() <= mate.fC1Span->startT()
2035 || mate.fC1Span->endT() <= fC1Span->startT());
caryclarkcdeff812016-07-22 03:34:19 -07002036 SkOPOBJASSERT(i, fC2Span == mate.fC2Span || fC2Span->endT() <= mate.fC2Span->startT()
caryclark45fa4472015-01-16 07:04:10 -08002037 || mate.fC2Span->endT() <= fC2Span->startT());
2038 return fC1Span == mate.fC1Span || fC1Span->endT() == mate.fC1Span->startT()
2039 || fC1Span->startT() == mate.fC1Span->endT()
2040 || fC2Span == mate.fC2Span
2041 || fC2Span->endT() == mate.fC2Span->startT()
2042 || fC2Span->startT() == mate.fC2Span->endT();
2043 }
2044
2045 void merge(const SkClosestRecord& mate) {
2046 fC1Span = mate.fC1Span;
2047 fC2Span = mate.fC2Span;
2048 fClosest = mate.fClosest;
2049 fC1Index = mate.fC1Index;
2050 fC2Index = mate.fC2Index;
2051 }
caryclark55888e42016-07-18 10:01:36 -07002052
caryclark45fa4472015-01-16 07:04:10 -08002053 void reset() {
2054 fClosest = FLT_MAX;
halcanary96fcdcc2015-08-27 07:41:13 -07002055 SkDEBUGCODE(fC1Span = nullptr);
2056 SkDEBUGCODE(fC2Span = nullptr);
caryclark45fa4472015-01-16 07:04:10 -08002057 SkDEBUGCODE(fC1Index = fC2Index = -1);
2058 }
2059
2060 void update(const SkClosestRecord& mate) {
2061 fC1StartT = SkTMin(fC1StartT, mate.fC1StartT);
2062 fC1EndT = SkTMax(fC1EndT, mate.fC1EndT);
2063 fC2StartT = SkTMin(fC2StartT, mate.fC2StartT);
2064 fC2EndT = SkTMax(fC2EndT, mate.fC2EndT);
2065 }
2066
caryclark1049f122015-04-20 08:31:59 -07002067 const SkTSpan<TCurve, OppCurve>* fC1Span;
2068 const SkTSpan<OppCurve, TCurve>* fC2Span;
caryclark45fa4472015-01-16 07:04:10 -08002069 double fC1StartT;
2070 double fC1EndT;
2071 double fC2StartT;
2072 double fC2EndT;
2073 double fClosest;
2074 int fC1Index;
2075 int fC2Index;
2076};
2077
caryclark1049f122015-04-20 08:31:59 -07002078template<typename TCurve, typename OppCurve>
caryclark45fa4472015-01-16 07:04:10 -08002079struct SkClosestSect {
2080 SkClosestSect()
2081 : fUsed(0) {
2082 fClosest.push_back().reset();
2083 }
2084
caryclarkcdeff812016-07-22 03:34:19 -07002085 bool find(const SkTSpan<TCurve, OppCurve>* span1, const SkTSpan<OppCurve, TCurve>* span2
2086 SkDEBUGPARAMS(SkIntersections* i)) {
caryclark1049f122015-04-20 08:31:59 -07002087 SkClosestRecord<TCurve, OppCurve>* record = &fClosest[fUsed];
caryclark45fa4472015-01-16 07:04:10 -08002088 record->findEnd(span1, span2, 0, 0);
caryclark1049f122015-04-20 08:31:59 -07002089 record->findEnd(span1, span2, 0, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002090 record->findEnd(span1, span2, TCurve::kPointLast, 0);
caryclark1049f122015-04-20 08:31:59 -07002091 record->findEnd(span1, span2, TCurve::kPointLast, OppCurve::kPointLast);
caryclark45fa4472015-01-16 07:04:10 -08002092 if (record->fClosest == FLT_MAX) {
caryclark54359292015-03-26 07:52:43 -07002093 return false;
caryclark45fa4472015-01-16 07:04:10 -08002094 }
2095 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002096 SkClosestRecord<TCurve, OppCurve>* test = &fClosest[index];
caryclarkcdeff812016-07-22 03:34:19 -07002097 if (test->matesWith(*record SkDEBUGPARAMS(i))) {
caryclark45fa4472015-01-16 07:04:10 -08002098 if (test->fClosest > record->fClosest) {
2099 test->merge(*record);
2100 }
2101 test->update(*record);
2102 record->reset();
caryclark54359292015-03-26 07:52:43 -07002103 return false;
caryclark45fa4472015-01-16 07:04:10 -08002104 }
2105 }
2106 ++fUsed;
2107 fClosest.push_back().reset();
caryclark54359292015-03-26 07:52:43 -07002108 return true;
caryclark45fa4472015-01-16 07:04:10 -08002109 }
2110
2111 void finish(SkIntersections* intersections) const {
caryclark1049f122015-04-20 08:31:59 -07002112 SkSTArray<TCurve::kMaxIntersections * 3,
2113 const SkClosestRecord<TCurve, OppCurve>*, true> closestPtrs;
caryclark45fa4472015-01-16 07:04:10 -08002114 for (int index = 0; index < fUsed; ++index) {
caryclark54359292015-03-26 07:52:43 -07002115 closestPtrs.push_back(&fClosest[index]);
2116 }
caryclark1049f122015-04-20 08:31:59 -07002117 SkTQSort<const SkClosestRecord<TCurve, OppCurve> >(closestPtrs.begin(), closestPtrs.end()
2118 - 1);
caryclark54359292015-03-26 07:52:43 -07002119 for (int index = 0; index < fUsed; ++index) {
caryclark1049f122015-04-20 08:31:59 -07002120 const SkClosestRecord<TCurve, OppCurve>* test = closestPtrs[index];
caryclark54359292015-03-26 07:52:43 -07002121 test->addIntersection(intersections);
caryclark45fa4472015-01-16 07:04:10 -08002122 }
2123 }
2124
caryclark54359292015-03-26 07:52:43 -07002125 // this is oversized so that an extra records can merge into final one
caryclark1049f122015-04-20 08:31:59 -07002126 SkSTArray<TCurve::kMaxIntersections * 2, SkClosestRecord<TCurve, OppCurve>, true> fClosest;
caryclark45fa4472015-01-16 07:04:10 -08002127 int fUsed;
2128};
2129
2130// returns true if the rect is too small to consider
caryclark1049f122015-04-20 08:31:59 -07002131template<typename TCurve, typename OppCurve>
2132void SkTSect<TCurve, OppCurve>::BinarySearch(SkTSect<TCurve, OppCurve>* sect1,
2133 SkTSect<OppCurve, TCurve>* sect2, SkIntersections* intersections) {
caryclark54359292015-03-26 07:52:43 -07002134#if DEBUG_T_SECT_DUMP > 1
2135 gDumpTSectNum = 0;
2136#endif
caryclark1049f122015-04-20 08:31:59 -07002137 SkDEBUGCODE(sect1->fOppSect = sect2);
2138 SkDEBUGCODE(sect2->fOppSect = sect1);
caryclark45fa4472015-01-16 07:04:10 -08002139 intersections->reset();
caryclarka35ab3e2016-10-20 08:32:18 -07002140 intersections->setMax(TCurve::kMaxIntersections + 4); // give extra for slop
caryclark1049f122015-04-20 08:31:59 -07002141 SkTSpan<TCurve, OppCurve>* span1 = sect1->fHead;
2142 SkTSpan<OppCurve, TCurve>* span2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002143 int oppSect, sect = sect1->intersects(span1, sect2, span2, &oppSect);
2144// SkASSERT(between(0, sect, 2));
2145 if (!sect) {
caryclark45fa4472015-01-16 07:04:10 -08002146 return;
2147 }
caryclark54359292015-03-26 07:52:43 -07002148 if (sect == 2 && oppSect == 2) {
caryclark45fa4472015-01-16 07:04:10 -08002149 (void) EndsEqual(sect1, sect2, intersections);
2150 return;
2151 }
caryclark54359292015-03-26 07:52:43 -07002152 span1->addBounded(span2, &sect1->fHeap);
2153 span2->addBounded(span1, &sect2->fHeap);
caryclark26ad22a2015-10-16 09:03:38 -07002154 const int kMaxCoinLoopCount = 8;
2155 int coinLoopCount = kMaxCoinLoopCount;
2156 double start1s SK_INIT_TO_AVOID_WARNING;
2157 double start1e SK_INIT_TO_AVOID_WARNING;
caryclark45fa4472015-01-16 07:04:10 -08002158 do {
2159 // find the largest bounds
caryclark1049f122015-04-20 08:31:59 -07002160 SkTSpan<TCurve, OppCurve>* largest1 = sect1->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002161 if (!largest1) {
2162 break;
2163 }
caryclark1049f122015-04-20 08:31:59 -07002164 SkTSpan<OppCurve, TCurve>* largest2 = sect2->boundsMax();
caryclark45fa4472015-01-16 07:04:10 -08002165 // split it
caryclark1049f122015-04-20 08:31:59 -07002166 if (!largest2 || (largest1 && (largest1->fBoundsMax > largest2->fBoundsMax
2167 || (!largest1->fCollapsed && largest2->fCollapsed)))) {
2168 if (largest1->fCollapsed) {
2169 break;
2170 }
caryclark34efb702016-10-24 08:19:06 -07002171 sect1->resetRemovedEnds();
2172 sect2->resetRemovedEnds();
caryclark1049f122015-04-20 08:31:59 -07002173 // trim parts that don't intersect the opposite
2174 SkTSpan<TCurve, OppCurve>* half1 = sect1->addOne();
caryclark643ede62016-08-08 14:27:45 -07002175 SkDEBUGCODE(half1->debugSetGlobalState(sect1->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002176 if (!half1->split(largest1, &sect1->fHeap)) {
2177 break;
2178 }
caryclarka35ab3e2016-10-20 08:32:18 -07002179 if (!sect1->trim(largest1, sect2)) {
2180 SkOPOBJASSERT(intersections, 0);
2181 return;
2182 }
2183 if (!sect1->trim(half1, sect2)) {
2184 SkOPOBJASSERT(intersections, 0);
2185 return;
2186 }
caryclark1049f122015-04-20 08:31:59 -07002187 } else {
2188 if (largest2->fCollapsed) {
2189 break;
2190 }
caryclark34efb702016-10-24 08:19:06 -07002191 sect1->resetRemovedEnds();
2192 sect2->resetRemovedEnds();
caryclark1049f122015-04-20 08:31:59 -07002193 // trim parts that don't intersect the opposite
2194 SkTSpan<OppCurve, TCurve>* half2 = sect2->addOne();
caryclark643ede62016-08-08 14:27:45 -07002195 SkDEBUGCODE(half2->debugSetGlobalState(sect2->globalState()));
caryclark1049f122015-04-20 08:31:59 -07002196 if (!half2->split(largest2, &sect2->fHeap)) {
2197 break;
2198 }
caryclarka35ab3e2016-10-20 08:32:18 -07002199 if (!sect2->trim(largest2, sect1)) {
2200 SkOPOBJASSERT(intersections, 0);
2201 return;
2202 }
2203 if (!sect2->trim(half2, sect1)) {
2204 SkOPOBJASSERT(intersections, 0);
2205 return;
2206 }
caryclark45fa4472015-01-16 07:04:10 -08002207 }
caryclark54359292015-03-26 07:52:43 -07002208 sect1->validate();
2209 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002210#if DEBUG_T_SECT_LOOP_COUNT
2211 intersections->debugBumpLoopCount(SkIntersections::kIterations_DebugLoop);
2212#endif
caryclark45fa4472015-01-16 07:04:10 -08002213 // if there are 9 or more continuous spans on both sects, suspect coincidence
2214 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2215 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
caryclark26ad22a2015-10-16 09:03:38 -07002216 if (coinLoopCount == kMaxCoinLoopCount) {
2217 start1s = sect1->fHead->fStartT;
2218 start1e = sect1->tail()->fEndT;
2219 }
caryclarkef7cee42016-09-06 09:05:54 -07002220 if (!sect1->coincidentCheck(sect2)) {
2221 return;
2222 }
caryclark54359292015-03-26 07:52:43 -07002223 sect1->validate();
2224 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002225#if DEBUG_T_SECT_LOOP_COUNT
2226 intersections->debugBumpLoopCount(SkIntersections::kCoinCheck_DebugLoop);
2227#endif
caryclarkef784fb2015-10-30 12:03:06 -07002228 if (!--coinLoopCount && sect1->fHead && sect2->fHead) {
caryclark26ad22a2015-10-16 09:03:38 -07002229 /* All known working cases resolve in two tries. Sadly, cubicConicTests[0]
2230 gets stuck in a loop. It adds an extension to allow a coincident end
2231 perpendicular to track its intersection in the opposite curve. However,
2232 the bounding box of the extension does not intersect the original curve,
caryclark55888e42016-07-18 10:01:36 -07002233 so the extension is discarded, only to be added again the next time around. */
caryclark26ad22a2015-10-16 09:03:38 -07002234 sect1->coincidentForce(sect2, start1s, start1e);
2235 sect1->validate();
2236 sect2->validate();
2237 }
caryclark45fa4472015-01-16 07:04:10 -08002238 }
caryclark54359292015-03-26 07:52:43 -07002239 if (sect1->fActiveCount >= COINCIDENT_SPAN_COUNT
2240 && sect2->fActiveCount >= COINCIDENT_SPAN_COUNT) {
Cary Clark59ed4822016-12-08 16:17:56 -05002241 if (!sect1->fHead) {
2242 return;
2243 }
caryclark54359292015-03-26 07:52:43 -07002244 sect1->computePerpendiculars(sect2, sect1->fHead, sect1->tail());
Cary Clark59ed4822016-12-08 16:17:56 -05002245 if (!sect2->fHead) {
2246 return;
2247 }
caryclark54359292015-03-26 07:52:43 -07002248 sect2->computePerpendiculars(sect1, sect2->fHead, sect2->tail());
2249 sect1->removeByPerpendicular(sect2);
2250 sect1->validate();
2251 sect2->validate();
caryclark26ad22a2015-10-16 09:03:38 -07002252#if DEBUG_T_SECT_LOOP_COUNT
2253 intersections->debugBumpLoopCount(SkIntersections::kComputePerp_DebugLoop);
2254#endif
caryclark1049f122015-04-20 08:31:59 -07002255 if (sect1->collapsed() > TCurve::kMaxIntersections) {
2256 break;
2257 }
caryclark54359292015-03-26 07:52:43 -07002258 }
2259#if DEBUG_T_SECT_DUMP
2260 sect1->dumpBoth(sect2);
caryclark45fa4472015-01-16 07:04:10 -08002261#endif
2262 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002263 break;
caryclark45fa4472015-01-16 07:04:10 -08002264 }
2265 } while (true);
caryclark1049f122015-04-20 08:31:59 -07002266 SkTSpan<TCurve, OppCurve>* coincident = sect1->fCoincident;
caryclark54359292015-03-26 07:52:43 -07002267 if (coincident) {
2268 // if there is more than one coincident span, check loosely to see if they should be joined
2269 if (coincident->fNext) {
2270 sect1->mergeCoincidence(sect2);
2271 coincident = sect1->fCoincident;
2272 }
2273 SkASSERT(sect2->fCoincident); // courtesy check : coincidence only looks at sect 1
caryclark45fa4472015-01-16 07:04:10 -08002274 do {
caryclark221a4bb2016-10-07 11:15:15 -07002275 if (!coincident) {
2276 return;
2277 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002278 if (!coincident->fCoinStart.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002279 continue;
2280 }
caryclark6c3b9cd2016-09-26 05:36:58 -07002281 if (!coincident->fCoinEnd.isMatch()) {
caryclarkef784fb2015-10-30 12:03:06 -07002282 continue;
2283 }
caryclark54359292015-03-26 07:52:43 -07002284 int index = intersections->insertCoincident(coincident->fStartT,
2285 coincident->fCoinStart.perpT(), coincident->fPart[0]);
2286 if ((intersections->insertCoincident(coincident->fEndT,
2287 coincident->fCoinEnd.perpT(),
2288 coincident->fPart[TCurve::kPointLast]) < 0) && index >= 0) {
caryclark45fa4472015-01-16 07:04:10 -08002289 intersections->clearCoincidence(index);
2290 }
caryclark54359292015-03-26 07:52:43 -07002291 } while ((coincident = coincident->fNext));
2292 }
caryclark1049f122015-04-20 08:31:59 -07002293 int zeroOneSet = EndsEqual(sect1, sect2, intersections);
caryclark34efb702016-10-24 08:19:06 -07002294// if (!sect1->fHead || !sect2->fHead) {
caryclark6c3b9cd2016-09-26 05:36:58 -07002295 // if the final iteration contains an end (0 or 1),
2296 if (sect1->fRemovedStartT && !(zeroOneSet & kZeroS1Set)) {
2297 SkTCoincident<TCurve, OppCurve> perp; // intersect perpendicular with opposite curve
caryclarka35ab3e2016-10-20 08:32:18 -07002298 perp.setPerp(sect1->fCurve, 0, sect1->fCurve[0], sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002299 if (perp.isMatch()) {
2300 intersections->insert(0, perp.perpT(), perp.perpPt());
2301 }
2302 }
2303 if (sect1->fRemovedEndT && !(zeroOneSet & kOneS1Set)) {
2304 SkTCoincident<TCurve, OppCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002305 perp.setPerp(sect1->fCurve, 1, sect1->fCurve[TCurve::kPointLast], sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002306 if (perp.isMatch()) {
2307 intersections->insert(1, perp.perpT(), perp.perpPt());
2308 }
2309 }
2310 if (sect2->fRemovedStartT && !(zeroOneSet & kZeroS2Set)) {
2311 SkTCoincident<OppCurve, TCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002312 perp.setPerp(sect2->fCurve, 0, sect2->fCurve[0], sect1->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002313 if (perp.isMatch()) {
2314 intersections->insert(perp.perpT(), 0, perp.perpPt());
2315 }
2316 }
2317 if (sect2->fRemovedEndT && !(zeroOneSet & kOneS2Set)) {
2318 SkTCoincident<OppCurve, TCurve> perp;
caryclarka35ab3e2016-10-20 08:32:18 -07002319 perp.setPerp(sect2->fCurve, 1, sect2->fCurve[OppCurve::kPointLast], sect1->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002320 if (perp.isMatch()) {
2321 intersections->insert(perp.perpT(), 1, perp.perpPt());
2322 }
2323 }
caryclark34efb702016-10-24 08:19:06 -07002324// }
2325 if (!sect1->fHead || !sect2->fHead) {
caryclark54359292015-03-26 07:52:43 -07002326 return;
caryclark45fa4472015-01-16 07:04:10 -08002327 }
caryclark45fa4472015-01-16 07:04:10 -08002328 sect1->recoverCollapsed();
2329 sect2->recoverCollapsed();
caryclark1049f122015-04-20 08:31:59 -07002330 SkTSpan<TCurve, OppCurve>* result1 = sect1->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002331 // check heads and tails for zero and ones and insert them if we haven't already done so
caryclark1049f122015-04-20 08:31:59 -07002332 const SkTSpan<TCurve, OppCurve>* head1 = result1;
caryclark45fa4472015-01-16 07:04:10 -08002333 if (!(zeroOneSet & kZeroS1Set) && approximately_less_than_zero(head1->fStartT)) {
2334 const SkDPoint& start1 = sect1->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002335 if (head1->isBounded()) {
2336 double t = head1->closestBoundedT(start1);
2337 if (sect2->fCurve.ptAtT(t).approximatelyEqual(start1)) {
2338 intersections->insert(0, t, start1);
2339 }
caryclark45fa4472015-01-16 07:04:10 -08002340 }
2341 }
caryclark1049f122015-04-20 08:31:59 -07002342 const SkTSpan<OppCurve, TCurve>* head2 = sect2->fHead;
caryclark45fa4472015-01-16 07:04:10 -08002343 if (!(zeroOneSet & kZeroS2Set) && approximately_less_than_zero(head2->fStartT)) {
2344 const SkDPoint& start2 = sect2->fCurve[0];
caryclark54359292015-03-26 07:52:43 -07002345 if (head2->isBounded()) {
2346 double t = head2->closestBoundedT(start2);
2347 if (sect1->fCurve.ptAtT(t).approximatelyEqual(start2)) {
2348 intersections->insert(t, 0, start2);
2349 }
caryclark45fa4472015-01-16 07:04:10 -08002350 }
2351 }
caryclark1049f122015-04-20 08:31:59 -07002352 const SkTSpan<TCurve, OppCurve>* tail1 = sect1->tail();
caryclark45fa4472015-01-16 07:04:10 -08002353 if (!(zeroOneSet & kOneS1Set) && approximately_greater_than_one(tail1->fEndT)) {
2354 const SkDPoint& end1 = sect1->fCurve[TCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002355 if (tail1->isBounded()) {
2356 double t = tail1->closestBoundedT(end1);
2357 if (sect2->fCurve.ptAtT(t).approximatelyEqual(end1)) {
2358 intersections->insert(1, t, end1);
2359 }
caryclark45fa4472015-01-16 07:04:10 -08002360 }
2361 }
caryclark1049f122015-04-20 08:31:59 -07002362 const SkTSpan<OppCurve, TCurve>* tail2 = sect2->tail();
caryclark45fa4472015-01-16 07:04:10 -08002363 if (!(zeroOneSet & kOneS2Set) && approximately_greater_than_one(tail2->fEndT)) {
caryclark1049f122015-04-20 08:31:59 -07002364 const SkDPoint& end2 = sect2->fCurve[OppCurve::kPointLast];
caryclark54359292015-03-26 07:52:43 -07002365 if (tail2->isBounded()) {
2366 double t = tail2->closestBoundedT(end2);
2367 if (sect1->fCurve.ptAtT(t).approximatelyEqual(end2)) {
2368 intersections->insert(t, 1, end2);
2369 }
caryclark45fa4472015-01-16 07:04:10 -08002370 }
2371 }
caryclark1049f122015-04-20 08:31:59 -07002372 SkClosestSect<TCurve, OppCurve> closest;
caryclark45fa4472015-01-16 07:04:10 -08002373 do {
caryclark6c3b9cd2016-09-26 05:36:58 -07002374 while (result1 && result1->fCoinStart.isMatch() && result1->fCoinEnd.isMatch()) {
caryclark45fa4472015-01-16 07:04:10 -08002375 result1 = result1->fNext;
2376 }
2377 if (!result1) {
2378 break;
2379 }
caryclark1049f122015-04-20 08:31:59 -07002380 SkTSpan<OppCurve, TCurve>* result2 = sect2->fHead;
caryclark54359292015-03-26 07:52:43 -07002381 bool found = false;
caryclark45fa4472015-01-16 07:04:10 -08002382 while (result2) {
caryclarkcdeff812016-07-22 03:34:19 -07002383 found |= closest.find(result1, result2 SkDEBUGPARAMS(intersections));
caryclark45fa4472015-01-16 07:04:10 -08002384 result2 = result2->fNext;
2385 }
caryclark45fa4472015-01-16 07:04:10 -08002386 } while ((result1 = result1->fNext));
2387 closest.finish(intersections);
caryclark54359292015-03-26 07:52:43 -07002388 // if there is more than one intersection and it isn't already coincident, check
2389 int last = intersections->used() - 1;
2390 for (int index = 0; index < last; ) {
2391 if (intersections->isCoincident(index) && intersections->isCoincident(index + 1)) {
2392 ++index;
2393 continue;
2394 }
2395 double midT = ((*intersections)[0][index] + (*intersections)[0][index + 1]) / 2;
2396 SkDPoint midPt = sect1->fCurve.ptAtT(midT);
2397 // intersect perpendicular with opposite curve
caryclark1049f122015-04-20 08:31:59 -07002398 SkTCoincident<TCurve, OppCurve> perp;
caryclark54359292015-03-26 07:52:43 -07002399 perp.setPerp(sect1->fCurve, midT, midPt, sect2->fCurve);
caryclark6c3b9cd2016-09-26 05:36:58 -07002400 if (!perp.isMatch()) {
caryclark54359292015-03-26 07:52:43 -07002401 ++index;
2402 continue;
2403 }
2404 if (intersections->isCoincident(index)) {
2405 intersections->removeOne(index);
2406 --last;
2407 } else if (intersections->isCoincident(index + 1)) {
2408 intersections->removeOne(index + 1);
2409 --last;
2410 } else {
2411 intersections->setCoincident(index++);
2412 }
2413 intersections->setCoincident(index);
2414 }
caryclarka35ab3e2016-10-20 08:32:18 -07002415 SkOPOBJASSERT(intersections, intersections->used() <= TCurve::kMaxIntersections);
caryclark45fa4472015-01-16 07:04:10 -08002416}
deanm12670eb2016-04-26 14:09:01 -07002417
2418#endif