blob: 216e31f285bc63ea1553032f1b35c5c0cfaf8f02 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
2 * Copyright 2012 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 */
7#include "SkIntersections.h"
8#include "SkPathOpsLine.h"
9#include "SkPathOpsQuad.h"
10
11/*
12Find the interection of a line and quadratic by solving for valid t values.
13
14From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve
15
16"A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three
17control points) can be expressed as: F(t) = A(1 - t)^2 + B(1 - t)t + Ct^2 where
18A, B and C are points and t goes from zero to one.
19
20This will give you two equations:
21
22 x = a(1 - t)^2 + b(1 - t)t + ct^2
23 y = d(1 - t)^2 + e(1 - t)t + ft^2
24
25If you add for instance the line equation (y = kx + m) to that, you'll end up
26with three equations and three unknowns (x, y and t)."
27
28Similar to above, the quadratic is represented as
29 x = a(1-t)^2 + 2b(1-t)t + ct^2
30 y = d(1-t)^2 + 2e(1-t)t + ft^2
31and the line as
32 y = g*x + h
33
34Using Mathematica, solve for the values of t where the quadratic intersects the
35line:
36
37 (in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x,
38 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x]
39 (out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 +
40 g (a - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2)
41 (in) Solve[t1 == 0, t]
42 (out) {
43 {t -> (-2 d + 2 e + 2 a g - 2 b g -
44 Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
45 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) /
46 (2 (-d + 2 e - f + a g - 2 b g + c g))
47 },
48 {t -> (-2 d + 2 e + 2 a g - 2 b g +
49 Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
50 4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) /
51 (2 (-d + 2 e - f + a g - 2 b g + c g))
52 }
53 }
54
55Using the results above (when the line tends towards horizontal)
56 A = (-(d - 2*e + f) + g*(a - 2*b + c) )
57 B = 2*( (d - e ) - g*(a - b ) )
58 C = (-(d ) + g*(a ) + h )
59
60If g goes to infinity, we can rewrite the line in terms of x.
61 x = g'*y + h'
62
63And solve accordingly in Mathematica:
64
65 (in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h',
66 d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y]
67 (out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 -
68 g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2)
69 (in) Solve[t2 == 0, t]
70 (out) {
71 {t -> (2 a - 2 b - 2 d g' + 2 e g' -
72 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
73 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) /
74 (2 (a - 2 b + c - d g' + 2 e g' - f g'))
75 },
76 {t -> (2 a - 2 b - 2 d g' + 2 e g' +
77 Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
78 4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/
79 (2 (a - 2 b + c - d g' + 2 e g' - f g'))
80 }
81 }
82
83Thus, if the slope of the line tends towards vertical, we use:
84 A = ( (a - 2*b + c) - g'*(d - 2*e + f) )
85 B = 2*(-(a - b ) + g'*(d - e ) )
86 C = ( (a ) - g'*(d ) - h' )
87 */
88
89
90class LineQuadraticIntersections {
91public:
92 LineQuadraticIntersections(const SkDQuad& q, const SkDLine& l, SkIntersections* i)
93 : quad(q)
94 , line(l)
95 , intersections(i) {
96 }
97
98 int intersectRay(double roots[2]) {
99 /*
100 solve by rotating line+quad so line is horizontal, then finding the roots
101 set up matrix to rotate quad to x-axis
102 |cos(a) -sin(a)|
103 |sin(a) cos(a)|
104 note that cos(a) = A(djacent) / Hypoteneuse
105 sin(a) = O(pposite) / Hypoteneuse
106 since we are computing Ts, we can ignore hypoteneuse, the scale factor:
107 | A -O |
108 | O A |
109 A = line[1].fX - line[0].fX (adjacent side of the right triangle)
110 O = line[1].fY - line[0].fY (opposite side of the right triangle)
111 for each of the three points (e.g. n = 0 to 2)
112 quad[n].fY' = (quad[n].fY - line[0].fY) * A - (quad[n].fX - line[0].fX) * O
113 */
114 double adj = line[1].fX - line[0].fX;
115 double opp = line[1].fY - line[0].fY;
116 double r[3];
117 for (int n = 0; n < 3; ++n) {
118 r[n] = (quad[n].fY - line[0].fY) * adj - (quad[n].fX - line[0].fX) * opp;
119 }
120 double A = r[2];
121 double B = r[1];
122 double C = r[0];
123 A += C - 2 * B; // A = a - 2*b + c
124 B -= C; // B = -(b - c)
125 return SkDQuad::RootsValidT(A, 2 * B, C, roots);
126 }
127
128 int intersect() {
129 addEndPoints();
130 double rootVals[2];
131 int roots = intersectRay(rootVals);
132 for (int index = 0; index < roots; ++index) {
133 double quadT = rootVals[index];
134 double lineT = findLineT(quadT);
135 if (PinTs(&quadT, &lineT)) {
136 SkDPoint pt = line.xyAtT(lineT);
137 intersections->insert(quadT, lineT, pt);
138 }
139 }
140 return intersections->used();
141 }
142
143 int horizontalIntersect(double axisIntercept, double roots[2]) {
144 double D = quad[2].fY; // f
145 double E = quad[1].fY; // e
146 double F = quad[0].fY; // d
147 D += F - 2 * E; // D = d - 2*e + f
148 E -= F; // E = -(d - e)
149 F -= axisIntercept;
150 return SkDQuad::RootsValidT(D, 2 * E, F, roots);
151 }
152
153 int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) {
154 addHorizontalEndPoints(left, right, axisIntercept);
155 double rootVals[2];
156 int roots = horizontalIntersect(axisIntercept, rootVals);
157 for (int index = 0; index < roots; ++index) {
158 double quadT = rootVals[index];
159 SkDPoint pt = quad.xyAtT(quadT);
160 double lineT = (pt.fX - left) / (right - left);
161 if (PinTs(&quadT, &lineT)) {
162 intersections->insert(quadT, lineT, pt);
163 }
164 }
165 if (flipped) {
166 intersections->flip();
167 }
168 return intersections->used();
169 }
170
171 int verticalIntersect(double axisIntercept, double roots[2]) {
172 double D = quad[2].fX; // f
173 double E = quad[1].fX; // e
174 double F = quad[0].fX; // d
175 D += F - 2 * E; // D = d - 2*e + f
176 E -= F; // E = -(d - e)
177 F -= axisIntercept;
178 return SkDQuad::RootsValidT(D, 2 * E, F, roots);
179 }
180
181 int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) {
182 addVerticalEndPoints(top, bottom, axisIntercept);
183 double rootVals[2];
184 int roots = verticalIntersect(axisIntercept, rootVals);
185 for (int index = 0; index < roots; ++index) {
186 double quadT = rootVals[index];
187 SkDPoint pt = quad.xyAtT(quadT);
188 double lineT = (pt.fY - top) / (bottom - top);
189 if (PinTs(&quadT, &lineT)) {
190 intersections->insert(quadT, lineT, pt);
191 }
192 }
193 if (flipped) {
194 intersections->flip();
195 }
196 return intersections->used();
197 }
198
199protected:
200 // add endpoints first to get zero and one t values exactly
201 void addEndPoints() {
202 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000203 bool foundEnd = false;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000204 for (int lIndex = 0; lIndex < 2; lIndex++) {
205 if (quad[qIndex] == line[lIndex]) {
206 intersections->insert(qIndex >> 1, lIndex, line[lIndex]);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000207 foundEnd = true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000208 }
209 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000210 if (foundEnd) {
211 continue;
212 }
213 // See if the quad end touches the line.
214 double dist = line.isLeft(quad[qIndex]); // this distance isn't cartesian
215 SkDVector lineLen = line[1] - line[0]; // the x/y magnitudes of the line
216 // compute the ULPS of the larger of the x/y deltas
217 double larger = SkTMax(SkTAbs(lineLen.fX), SkTAbs(lineLen.fY));
218 if (!RoughlyEqualUlps(larger, larger + dist)) { // is the dist within ULPS tolerance?
219 continue;
220 }
221 double lineT = findLineT(qIndex >> 1);
222 if (!between(0, lineT, 1)) {
223 continue;
224 }
225 SkDPoint linePt = line.xyAtT(lineT);
226 if (linePt.approximatelyEqual(quad[qIndex])) {
227 intersections->insert(qIndex >> 1, lineT, quad[qIndex]);
228 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000229 }
230 }
231
232 void addHorizontalEndPoints(double left, double right, double y) {
233 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000234 if (!AlmostEqualUlps(quad[qIndex].fY, y)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000235 continue;
236 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000237 double x = quad[qIndex].fX;
238 if (between(left, x, right)) {
239 double t = (x - left) / (right - left);
240 intersections->insert(qIndex >> 1, t, quad[qIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000241 }
242 }
243 }
244
245 void addVerticalEndPoints(double top, double bottom, double x) {
246 for (int qIndex = 0; qIndex < 3; qIndex += 2) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000247 if (!AlmostEqualUlps(quad[qIndex].fX, x)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000248 continue;
249 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000250 double y = quad[qIndex].fY;
251 if (between(top, y, bottom)) {
252 double t = (y - top) / (bottom - top);
253 intersections->insert(qIndex >> 1, t, quad[qIndex]);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000254 }
255 }
256 }
257
258 double findLineT(double t) {
259 SkDPoint xy = quad.xyAtT(t);
260 double dx = line[1].fX - line[0].fX;
261 double dy = line[1].fY - line[0].fY;
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000262#if 0
caryclark@google.com07393ca2013-04-08 11:47:37 +0000263 if (fabs(dx) > fabs(dy)) {
264 return (xy.fX - line[0].fX) / dx;
265 }
266 return (xy.fY - line[0].fY) / dy;
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000267#else
268 double dxT = (xy.fX - line[0].fX) / dx;
269 double dyT = (xy.fY - line[0].fY) / dy;
270 if (!between(FLT_EPSILON, dxT, 1 - FLT_EPSILON) && between(0, dyT, 1)) {
271 return dyT;
272 }
273 if (!between(FLT_EPSILON, dyT, 1 - FLT_EPSILON) && between(0, dxT, 1)) {
274 return dxT;
275 }
276 return fabs(dx) > fabs(dy) ? dxT : dyT;
277#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000278 }
279
280 static bool PinTs(double* quadT, double* lineT) {
281 if (!approximately_one_or_less(*lineT)) {
282 return false;
283 }
284 if (!approximately_zero_or_more(*lineT)) {
285 return false;
286 }
287 if (precisely_less_than_zero(*quadT)) {
288 *quadT = 0;
289 } else if (precisely_greater_than_one(*quadT)) {
290 *quadT = 1;
291 }
292 if (precisely_less_than_zero(*lineT)) {
293 *lineT = 0;
294 } else if (precisely_greater_than_one(*lineT)) {
295 *lineT = 1;
296 }
297 return true;
298 }
299
300private:
301 const SkDQuad& quad;
302 const SkDLine& line;
303 SkIntersections* intersections;
304};
305
306// utility for pairs of coincident quads
307static double horizontalIntersect(const SkDQuad& quad, const SkDPoint& pt) {
308 LineQuadraticIntersections q(quad, *(static_cast<SkDLine*>(0)),
309 static_cast<SkIntersections*>(0));
310 double rootVals[2];
311 int roots = q.horizontalIntersect(pt.fY, rootVals);
312 for (int index = 0; index < roots; ++index) {
313 double t = rootVals[index];
314 SkDPoint qPt = quad.xyAtT(t);
315 if (AlmostEqualUlps(qPt.fX, pt.fX)) {
316 return t;
317 }
318 }
319 return -1;
320}
321
322static double verticalIntersect(const SkDQuad& quad, const SkDPoint& pt) {
323 LineQuadraticIntersections q(quad, *(static_cast<SkDLine*>(0)),
324 static_cast<SkIntersections*>(0));
325 double rootVals[2];
326 int roots = q.verticalIntersect(pt.fX, rootVals);
327 for (int index = 0; index < roots; ++index) {
328 double t = rootVals[index];
329 SkDPoint qPt = quad.xyAtT(t);
330 if (AlmostEqualUlps(qPt.fY, pt.fY)) {
331 return t;
332 }
333 }
334 return -1;
335}
336
337double SkIntersections::Axial(const SkDQuad& q1, const SkDPoint& p, bool vertical) {
338 if (vertical) {
339 return verticalIntersect(q1, p);
340 }
341 return horizontalIntersect(q1, p);
342}
343
344int SkIntersections::horizontal(const SkDQuad& quad, double left, double right, double y,
345 bool flipped) {
346 LineQuadraticIntersections q(quad, *(static_cast<SkDLine*>(0)), this);
347 return q.horizontalIntersect(y, left, right, flipped);
348}
349
350int SkIntersections::vertical(const SkDQuad& quad, double top, double bottom, double x,
351 bool flipped) {
352 LineQuadraticIntersections q(quad, *(static_cast<SkDLine*>(0)), this);
353 return q.verticalIntersect(x, top, bottom, flipped);
354}
355
356int SkIntersections::intersect(const SkDQuad& quad, const SkDLine& line) {
357 LineQuadraticIntersections q(quad, line, this);
358 return q.intersect();
359}
360
361int SkIntersections::intersectRay(const SkDQuad& quad, const SkDLine& line) {
362 LineQuadraticIntersections q(quad, line, this);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000363 fUsed = q.intersectRay(fT[0]);
364 for (int index = 0; index < fUsed; ++index) {
365 fPt[index] = quad.xyAtT(fT[0][index]);
366 }
367 return fUsed;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000368}