1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkIntersections.h"
#include "SkPathOpsConic.h"
#include "SkPathOpsCurve.h"
#include "SkPathOpsLine.h"
class LineConicIntersections {
public:
enum PinTPoint {
kPointUninitialized,
kPointInitialized
};
LineConicIntersections(const SkDConic& c, const SkDLine& l, SkIntersections* i)
: fConic(c)
, fLine(&l)
, fIntersections(i)
, fAllowNear(true) {
i->setMax(4); // allow short partial coincidence plus discrete intersection
}
LineConicIntersections(const SkDConic& c)
: fConic(c)
SkDEBUGPARAMS(fLine(nullptr))
SkDEBUGPARAMS(fIntersections(nullptr))
SkDEBUGPARAMS(fAllowNear(false)) {
}
void allowNear(bool allow) {
fAllowNear = allow;
}
void checkCoincident() {
int last = fIntersections->used() - 1;
for (int index = 0; index < last; ) {
double conicMidT = ((*fIntersections)[0][index] + (*fIntersections)[0][index + 1]) / 2;
SkDPoint conicMidPt = fConic.ptAtT(conicMidT);
double t = fLine->nearPoint(conicMidPt, nullptr);
if (t < 0) {
++index;
continue;
}
if (fIntersections->isCoincident(index)) {
fIntersections->removeOne(index);
--last;
} else if (fIntersections->isCoincident(index + 1)) {
fIntersections->removeOne(index + 1);
--last;
} else {
fIntersections->setCoincident(index++);
}
fIntersections->setCoincident(index);
}
}
#ifdef SK_DEBUG
static bool close_to(double a, double b, const double c[3]) {
double max = SkTMax(-SkTMin(SkTMin(c[0], c[1]), c[2]), SkTMax(SkTMax(c[0], c[1]), c[2]));
return approximately_zero_when_compared_to(a - b, max);
}
#endif
int horizontalIntersect(double axisIntercept, double roots[2]) {
double conicVals[] = { fConic[0].fY, fConic[1].fY, fConic[2].fY };
return this->validT(conicVals, axisIntercept, roots);
}
int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) {
this->addExactHorizontalEndPoints(left, right, axisIntercept);
if (fAllowNear) {
this->addNearHorizontalEndPoints(left, right, axisIntercept);
}
double roots[2];
int count = this->horizontalIntersect(axisIntercept, roots);
for (int index = 0; index < count; ++index) {
double conicT = roots[index];
SkDPoint pt = fConic.ptAtT(conicT);
SkDEBUGCODE(double conicVals[] = { fConic[0].fY, fConic[1].fY, fConic[2].fY });
SkOPOBJASSERT(fIntersections, close_to(pt.fY, axisIntercept, conicVals));
double lineT = (pt.fX - left) / (right - left);
if (this->pinTs(&conicT, &lineT, &pt, kPointInitialized)
&& this->uniqueAnswer(conicT, pt)) {
fIntersections->insert(conicT, lineT, pt);
}
}
if (flipped) {
fIntersections->flip();
}
this->checkCoincident();
return fIntersections->used();
}
int intersect() {
this->addExactEndPoints();
if (fAllowNear) {
this->addNearEndPoints();
}
double rootVals[2];
int roots = this->intersectRay(rootVals);
for (int index = 0; index < roots; ++index) {
double conicT = rootVals[index];
double lineT = this->findLineT(conicT);
#ifdef SK_DEBUG
if (!fIntersections->globalState()
|| !fIntersections->globalState()->debugSkipAssert()) {
SkDEBUGCODE(SkDPoint conicPt = fConic.ptAtT(conicT));
SkDEBUGCODE(SkDPoint linePt = fLine->ptAtT(lineT));
SkASSERT(conicPt.approximatelyDEqual(linePt));
}
#endif
SkDPoint pt;
if (this->pinTs(&conicT, &lineT, &pt, kPointUninitialized)
&& this->uniqueAnswer(conicT, pt)) {
fIntersections->insert(conicT, lineT, pt);
}
}
this->checkCoincident();
return fIntersections->used();
}
int intersectRay(double roots[2]) {
double adj = (*fLine)[1].fX - (*fLine)[0].fX;
double opp = (*fLine)[1].fY - (*fLine)[0].fY;
double r[3];
for (int n = 0; n < 3; ++n) {
r[n] = (fConic[n].fY - (*fLine)[0].fY) * adj - (fConic[n].fX - (*fLine)[0].fX) * opp;
}
return this->validT(r, 0, roots);
}
int validT(double r[3], double axisIntercept, double roots[2]) {
double A = r[2];
double B = r[1] * fConic.fWeight - axisIntercept * fConic.fWeight + axisIntercept;
double C = r[0];
A += C - 2 * B; // A = a + c - 2*(b*w - xCept*w + xCept)
B -= C; // B = b*w - w * xCept + xCept - a
C -= axisIntercept;
return SkDQuad::RootsValidT(A, 2 * B, C, roots);
}
int verticalIntersect(double axisIntercept, double roots[2]) {
double conicVals[] = { fConic[0].fX, fConic[1].fX, fConic[2].fX };
return this->validT(conicVals, axisIntercept, roots);
}
int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) {
this->addExactVerticalEndPoints(top, bottom, axisIntercept);
if (fAllowNear) {
this->addNearVerticalEndPoints(top, bottom, axisIntercept);
}
double roots[2];
int count = this->verticalIntersect(axisIntercept, roots);
for (int index = 0; index < count; ++index) {
double conicT = roots[index];
SkDPoint pt = fConic.ptAtT(conicT);
SkDEBUGCODE(double conicVals[] = { fConic[0].fX, fConic[1].fX, fConic[2].fX });
SkOPOBJASSERT(fIntersections, close_to(pt.fX, axisIntercept, conicVals));
double lineT = (pt.fY - top) / (bottom - top);
if (this->pinTs(&conicT, &lineT, &pt, kPointInitialized)
&& this->uniqueAnswer(conicT, pt)) {
fIntersections->insert(conicT, lineT, pt);
}
}
if (flipped) {
fIntersections->flip();
}
this->checkCoincident();
return fIntersections->used();
}
protected:
// OPTIMIZE: Functions of the form add .. points are indentical to the conic routines.
// add endpoints first to get zero and one t values exactly
void addExactEndPoints() {
for (int cIndex = 0; cIndex < SkDConic::kPointCount; cIndex += SkDConic::kPointLast) {
double lineT = fLine->exactPoint(fConic[cIndex]);
if (lineT < 0) {
continue;
}
double conicT = (double) (cIndex >> 1);
fIntersections->insert(conicT, lineT, fConic[cIndex]);
}
}
void addNearEndPoints() {
for (int cIndex = 0; cIndex < SkDConic::kPointCount; cIndex += SkDConic::kPointLast) {
double conicT = (double) (cIndex >> 1);
if (fIntersections->hasT(conicT)) {
continue;
}
double lineT = fLine->nearPoint(fConic[cIndex], nullptr);
if (lineT < 0) {
continue;
}
fIntersections->insert(conicT, lineT, fConic[cIndex]);
}
this->addLineNearEndPoints();
}
void addLineNearEndPoints() {
for (int lIndex = 0; lIndex < 2; ++lIndex) {
double lineT = (double) lIndex;
if (fIntersections->hasOppT(lineT)) {
continue;
}
double conicT = ((SkDCurve*) &fConic)->nearPoint(SkPath::kConic_Verb,
(*fLine)[lIndex], (*fLine)[!lIndex]);
if (conicT < 0) {
continue;
}
fIntersections->insert(conicT, lineT, (*fLine)[lIndex]);
}
}
void addExactHorizontalEndPoints(double left, double right, double y) {
for (int cIndex = 0; cIndex < SkDConic::kPointCount; cIndex += SkDConic::kPointLast) {
double lineT = SkDLine::ExactPointH(fConic[cIndex], left, right, y);
if (lineT < 0) {
continue;
}
double conicT = (double) (cIndex >> 1);
fIntersections->insert(conicT, lineT, fConic[cIndex]);
}
}
void addNearHorizontalEndPoints(double left, double right, double y) {
for (int cIndex = 0; cIndex < SkDConic::kPointCount; cIndex += SkDConic::kPointLast) {
double conicT = (double) (cIndex >> 1);
if (fIntersections->hasT(conicT)) {
continue;
}
double lineT = SkDLine::NearPointH(fConic[cIndex], left, right, y);
if (lineT < 0) {
continue;
}
fIntersections->insert(conicT, lineT, fConic[cIndex]);
}
this->addLineNearEndPoints();
}
void addExactVerticalEndPoints(double top, double bottom, double x) {
for (int cIndex = 0; cIndex < SkDConic::kPointCount; cIndex += SkDConic::kPointLast) {
double lineT = SkDLine::ExactPointV(fConic[cIndex], top, bottom, x);
if (lineT < 0) {
continue;
}
double conicT = (double) (cIndex >> 1);
fIntersections->insert(conicT, lineT, fConic[cIndex]);
}
}
void addNearVerticalEndPoints(double top, double bottom, double x) {
for (int cIndex = 0; cIndex < SkDConic::kPointCount; cIndex += SkDConic::kPointLast) {
double conicT = (double) (cIndex >> 1);
if (fIntersections->hasT(conicT)) {
continue;
}
double lineT = SkDLine::NearPointV(fConic[cIndex], top, bottom, x);
if (lineT < 0) {
continue;
}
fIntersections->insert(conicT, lineT, fConic[cIndex]);
}
this->addLineNearEndPoints();
}
double findLineT(double t) {
SkDPoint xy = fConic.ptAtT(t);
double dx = (*fLine)[1].fX - (*fLine)[0].fX;
double dy = (*fLine)[1].fY - (*fLine)[0].fY;
if (fabs(dx) > fabs(dy)) {
return (xy.fX - (*fLine)[0].fX) / dx;
}
return (xy.fY - (*fLine)[0].fY) / dy;
}
bool pinTs(double* conicT, double* lineT, SkDPoint* pt, PinTPoint ptSet) {
if (!approximately_one_or_less_double(*lineT)) {
return false;
}
if (!approximately_zero_or_more_double(*lineT)) {
return false;
}
double qT = *conicT = SkPinT(*conicT);
double lT = *lineT = SkPinT(*lineT);
if (lT == 0 || lT == 1 || (ptSet == kPointUninitialized && qT != 0 && qT != 1)) {
*pt = (*fLine).ptAtT(lT);
} else if (ptSet == kPointUninitialized) {
*pt = fConic.ptAtT(qT);
}
SkPoint gridPt = pt->asSkPoint();
if (SkDPoint::ApproximatelyEqual(gridPt, (*fLine)[0].asSkPoint())) {
*pt = (*fLine)[0];
*lineT = 0;
} else if (SkDPoint::ApproximatelyEqual(gridPt, (*fLine)[1].asSkPoint())) {
*pt = (*fLine)[1];
*lineT = 1;
}
if (fIntersections->used() > 0 && approximately_equal((*fIntersections)[1][0], *lineT)) {
return false;
}
if (gridPt == fConic[0].asSkPoint()) {
*pt = fConic[0];
*conicT = 0;
} else if (gridPt == fConic[2].asSkPoint()) {
*pt = fConic[2];
*conicT = 1;
}
return true;
}
bool uniqueAnswer(double conicT, const SkDPoint& pt) {
for (int inner = 0; inner < fIntersections->used(); ++inner) {
if (fIntersections->pt(inner) != pt) {
continue;
}
double existingConicT = (*fIntersections)[0][inner];
if (conicT == existingConicT) {
return false;
}
// check if midway on conic is also same point. If so, discard this
double conicMidT = (existingConicT + conicT) / 2;
SkDPoint conicMidPt = fConic.ptAtT(conicMidT);
if (conicMidPt.approximatelyEqual(pt)) {
return false;
}
}
#if ONE_OFF_DEBUG
SkDPoint qPt = fConic.ptAtT(conicT);
SkDebugf("%s pt=(%1.9g,%1.9g) cPt=(%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY,
qPt.fX, qPt.fY);
#endif
return true;
}
private:
const SkDConic& fConic;
const SkDLine* fLine;
SkIntersections* fIntersections;
bool fAllowNear;
};
int SkIntersections::horizontal(const SkDConic& conic, double left, double right, double y,
bool flipped) {
SkDLine line = {{{ left, y }, { right, y }}};
LineConicIntersections c(conic, line, this);
return c.horizontalIntersect(y, left, right, flipped);
}
int SkIntersections::vertical(const SkDConic& conic, double top, double bottom, double x,
bool flipped) {
SkDLine line = {{{ x, top }, { x, bottom }}};
LineConicIntersections c(conic, line, this);
return c.verticalIntersect(x, top, bottom, flipped);
}
int SkIntersections::intersect(const SkDConic& conic, const SkDLine& line) {
LineConicIntersections c(conic, line, this);
c.allowNear(fAllowNear);
return c.intersect();
}
int SkIntersections::intersectRay(const SkDConic& conic, const SkDLine& line) {
LineConicIntersections c(conic, line, this);
fUsed = c.intersectRay(fT[0]);
for (int index = 0; index < fUsed; ++index) {
fPt[index] = conic.ptAtT(fT[0][index]);
}
return fUsed;
}
int SkIntersections::HorizontalIntercept(const SkDConic& conic, SkScalar y, double* roots) {
LineConicIntersections c(conic);
return c.horizontalIntersect(y, roots);
}
int SkIntersections::VerticalIntercept(const SkDConic& conic, SkScalar x, double* roots) {
LineConicIntersections c(conic);
return c.verticalIntersect(x, roots);
}
|