2012-08-27 14:11:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2012-02-03 22:07:47 +00:00
|
|
|
#include "CurveIntersection.h"
|
2012-01-25 18:57:23 +00:00
|
|
|
#include "Intersections.h"
|
|
|
|
#include "LineUtilities.h"
|
|
|
|
#include "QuadraticUtilities.h"
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
/*
|
2012-01-25 18:57:23 +00:00
|
|
|
Find the interection of a line and quadratic by solving for valid t values.
|
|
|
|
|
|
|
|
From http://stackoverflow.com/questions/1853637/how-to-find-the-mathematical-function-defining-a-bezier-curve
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
"A Bezier curve is a parametric function. A quadratic Bezier curve (i.e. three
|
|
|
|
control points) can be expressed as: F(t) = A(1 - t)^2 + B(1 - t)t + Ct^2 where
|
2012-01-25 18:57:23 +00:00
|
|
|
A, B and C are points and t goes from zero to one.
|
|
|
|
|
|
|
|
This will give you two equations:
|
|
|
|
|
|
|
|
x = a(1 - t)^2 + b(1 - t)t + ct^2
|
|
|
|
y = d(1 - t)^2 + e(1 - t)t + ft^2
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
If you add for instance the line equation (y = kx + m) to that, you'll end up
|
2012-01-25 18:57:23 +00:00
|
|
|
with three equations and three unknowns (x, y and t)."
|
|
|
|
|
|
|
|
Similar to above, the quadratic is represented as
|
|
|
|
x = a(1-t)^2 + 2b(1-t)t + ct^2
|
|
|
|
y = d(1-t)^2 + 2e(1-t)t + ft^2
|
|
|
|
and the line as
|
|
|
|
y = g*x + h
|
|
|
|
|
|
|
|
Using Mathematica, solve for the values of t where the quadratic intersects the
|
|
|
|
line:
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
(in) t1 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - x,
|
2012-01-25 18:57:23 +00:00
|
|
|
d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - g*x - h, x]
|
2012-08-23 18:14:13 +00:00
|
|
|
(out) -d + h + 2 d t - 2 e t - d t^2 + 2 e t^2 - f t^2 +
|
2012-01-25 18:57:23 +00:00
|
|
|
g (a - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2)
|
|
|
|
(in) Solve[t1 == 0, t]
|
|
|
|
(out) {
|
|
|
|
{t -> (-2 d + 2 e + 2 a g - 2 b g -
|
2012-08-23 18:14:13 +00:00
|
|
|
Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
|
2012-01-25 18:57:23 +00:00
|
|
|
4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) /
|
|
|
|
(2 (-d + 2 e - f + a g - 2 b g + c g))
|
|
|
|
},
|
|
|
|
{t -> (-2 d + 2 e + 2 a g - 2 b g +
|
2012-08-23 18:14:13 +00:00
|
|
|
Sqrt[(2 d - 2 e - 2 a g + 2 b g)^2 -
|
2012-01-25 18:57:23 +00:00
|
|
|
4 (-d + 2 e - f + a g - 2 b g + c g) (-d + a g + h)]) /
|
|
|
|
(2 (-d + 2 e - f + a g - 2 b g + c g))
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-01-25 18:57:23 +00:00
|
|
|
Using the results above (when the line tends towards horizontal)
|
|
|
|
A = (-(d - 2*e + f) + g*(a - 2*b + c) )
|
|
|
|
B = 2*( (d - e ) - g*(a - b ) )
|
|
|
|
C = (-(d ) + g*(a ) + h )
|
|
|
|
|
|
|
|
If g goes to infinity, we can rewrite the line in terms of x.
|
|
|
|
x = g'*y + h'
|
|
|
|
|
|
|
|
And solve accordingly in Mathematica:
|
|
|
|
|
2012-08-23 18:14:13 +00:00
|
|
|
(in) t2 = Resultant[a*(1 - t)^2 + 2*b*(1 - t)*t + c*t^2 - g'*y - h',
|
2012-01-25 18:57:23 +00:00
|
|
|
d*(1 - t)^2 + 2*e*(1 - t)*t + f*t^2 - y, y]
|
2012-08-23 18:14:13 +00:00
|
|
|
(out) a - h' - 2 a t + 2 b t + a t^2 - 2 b t^2 + c t^2 -
|
2012-01-25 18:57:23 +00:00
|
|
|
g' (d - 2 d t + 2 e t + d t^2 - 2 e t^2 + f t^2)
|
|
|
|
(in) Solve[t2 == 0, t]
|
|
|
|
(out) {
|
|
|
|
{t -> (2 a - 2 b - 2 d g' + 2 e g' -
|
2012-08-23 18:14:13 +00:00
|
|
|
Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
|
2012-01-25 18:57:23 +00:00
|
|
|
4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')]) /
|
|
|
|
(2 (a - 2 b + c - d g' + 2 e g' - f g'))
|
|
|
|
},
|
|
|
|
{t -> (2 a - 2 b - 2 d g' + 2 e g' +
|
2012-08-23 18:14:13 +00:00
|
|
|
Sqrt[(-2 a + 2 b + 2 d g' - 2 e g')^2 -
|
2012-01-25 18:57:23 +00:00
|
|
|
4 (a - 2 b + c - d g' + 2 e g' - f g') (a - d g' - h')])/
|
|
|
|
(2 (a - 2 b + c - d g' + 2 e g' - f g'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Thus, if the slope of the line tends towards vertical, we use:
|
|
|
|
A = ( (a - 2*b + c) - g'*(d - 2*e + f) )
|
|
|
|
B = 2*(-(a - b ) + g'*(d - e ) )
|
|
|
|
C = ( (a ) - g'*(d ) - h' )
|
|
|
|
*/
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-01-25 18:57:23 +00:00
|
|
|
|
2012-10-19 15:54:16 +00:00
|
|
|
class LineQuadraticIntersections {
|
2012-01-25 18:57:23 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
LineQuadraticIntersections(const Quadratic& q, const _Line& l, Intersections& i)
|
|
|
|
: quad(q)
|
|
|
|
, line(l)
|
|
|
|
, intersections(i) {
|
|
|
|
}
|
|
|
|
|
2012-10-19 15:54:16 +00:00
|
|
|
int intersectRay(double roots[2]) {
|
2012-08-24 15:24:36 +00:00
|
|
|
/*
|
|
|
|
solve by rotating line+quad so line is horizontal, then finding the roots
|
|
|
|
set up matrix to rotate quad to x-axis
|
|
|
|
|cos(a) -sin(a)|
|
|
|
|
|sin(a) cos(a)|
|
|
|
|
note that cos(a) = A(djacent) / Hypoteneuse
|
|
|
|
sin(a) = O(pposite) / Hypoteneuse
|
|
|
|
since we are computing Ts, we can ignore hypoteneuse, the scale factor:
|
|
|
|
| A -O |
|
|
|
|
| O A |
|
|
|
|
A = line[1].x - line[0].x (adjacent side of the right triangle)
|
|
|
|
O = line[1].y - line[0].y (opposite side of the right triangle)
|
|
|
|
for each of the three points (e.g. n = 0 to 2)
|
|
|
|
quad[n].y' = (quad[n].y - line[0].y) * A - (quad[n].x - line[0].x) * O
|
|
|
|
*/
|
|
|
|
double adj = line[1].x - line[0].x;
|
|
|
|
double opp = line[1].y - line[0].y;
|
|
|
|
double r[3];
|
|
|
|
for (int n = 0; n < 3; ++n) {
|
|
|
|
r[n] = (quad[n].y - line[0].y) * adj - (quad[n].x - line[0].x) * opp;
|
2012-01-25 18:57:23 +00:00
|
|
|
}
|
2012-08-24 15:24:36 +00:00
|
|
|
double A = r[2];
|
|
|
|
double B = r[1];
|
|
|
|
double C = r[0];
|
|
|
|
A += C - 2 * B; // A = a - 2*b + c
|
|
|
|
B -= C; // B = -(b - c)
|
2012-10-19 15:54:16 +00:00
|
|
|
return quadraticRoots(A, B, C, roots);
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int intersect() {
|
2012-10-19 15:54:16 +00:00
|
|
|
addEndPoints();
|
|
|
|
double rootVals[2];
|
|
|
|
int roots = intersectRay(rootVals);
|
|
|
|
for (int index = 0; index < roots; ++index) {
|
|
|
|
double quadT = rootVals[index];
|
|
|
|
double lineT = findLineT(quadT);
|
|
|
|
if (pinTs(quadT, lineT)) {
|
|
|
|
intersections.insert(quadT, lineT);
|
2012-08-20 12:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-19 15:54:16 +00:00
|
|
|
return intersections.fUsed;
|
2012-01-25 18:57:23 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 15:54:16 +00:00
|
|
|
int horizontalIntersect(double axisIntercept, double roots[2]) {
|
2012-03-30 18:47:02 +00:00
|
|
|
double D = quad[2].y; // f
|
|
|
|
double E = quad[1].y; // e
|
|
|
|
double F = quad[0].y; // d
|
|
|
|
D += F - 2 * E; // D = d - 2*e + f
|
|
|
|
E -= F; // E = -(d - e)
|
|
|
|
F -= axisIntercept;
|
2012-10-19 15:54:16 +00:00
|
|
|
return quadraticRoots(D, E, F, roots);
|
2012-03-30 18:47:02 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 15:54:16 +00:00
|
|
|
int horizontalIntersect(double axisIntercept, double left, double right, bool flipped) {
|
|
|
|
addHorizontalEndPoints(left, right, axisIntercept);
|
|
|
|
double rootVals[2];
|
|
|
|
int roots = horizontalIntersect(axisIntercept, rootVals);
|
|
|
|
for (int index = 0; index < roots; ++index) {
|
2012-08-24 15:24:36 +00:00
|
|
|
double x;
|
2012-10-19 15:54:16 +00:00
|
|
|
double quadT = rootVals[index];
|
|
|
|
xy_at_t(quad, quadT, x, *(double*) NULL);
|
2012-08-24 15:24:36 +00:00
|
|
|
double lineT = (x - left) / (right - left);
|
2012-10-19 15:54:16 +00:00
|
|
|
if (pinTs(quadT, lineT)) {
|
|
|
|
intersections.insert(quadT, lineT);
|
2012-08-24 15:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-19 15:54:16 +00:00
|
|
|
if (flipped) {
|
|
|
|
flip();
|
|
|
|
}
|
|
|
|
return intersections.fUsed;
|
2012-08-24 15:24:36 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 15:54:16 +00:00
|
|
|
int verticalIntersect(double axisIntercept, double roots[2]) {
|
2012-04-26 21:01:06 +00:00
|
|
|
double D = quad[2].x; // f
|
|
|
|
double E = quad[1].x; // e
|
|
|
|
double F = quad[0].x; // d
|
|
|
|
D += F - 2 * E; // D = d - 2*e + f
|
|
|
|
E -= F; // E = -(d - e)
|
|
|
|
F -= axisIntercept;
|
2012-10-19 15:54:16 +00:00
|
|
|
return quadraticRoots(D, E, F, roots);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 15:54:16 +00:00
|
|
|
int verticalIntersect(double axisIntercept, double top, double bottom, bool flipped) {
|
|
|
|
addVerticalEndPoints(top, bottom, axisIntercept);
|
|
|
|
double rootVals[2];
|
|
|
|
int roots = verticalIntersect(axisIntercept, rootVals);
|
|
|
|
for (int index = 0; index < roots; ++index) {
|
2012-08-24 15:24:36 +00:00
|
|
|
double y;
|
2012-10-19 15:54:16 +00:00
|
|
|
double quadT = rootVals[index];
|
|
|
|
xy_at_t(quad, quadT, *(double*) NULL, y);
|
2012-08-24 15:24:36 +00:00
|
|
|
double lineT = (y - top) / (bottom - top);
|
2012-10-19 15:54:16 +00:00
|
|
|
if (pinTs(quadT, lineT)) {
|
|
|
|
intersections.insert(quadT, lineT);
|
2012-08-24 15:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-19 15:54:16 +00:00
|
|
|
if (flipped) {
|
|
|
|
flip();
|
|
|
|
}
|
|
|
|
return intersections.fUsed;
|
2012-08-24 15:24:36 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 18:57:23 +00:00
|
|
|
protected:
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2012-10-19 15:54:16 +00:00
|
|
|
// add endpoints first to get zero and one t values exactly
|
|
|
|
void addEndPoints()
|
|
|
|
{
|
|
|
|
for (int qIndex = 0; qIndex < 3; qIndex += 2) {
|
|
|
|
for (int lIndex = 0; lIndex < 2; lIndex++) {
|
|
|
|
if (quad[qIndex] == line[lIndex]) {
|
|
|
|
intersections.insert(qIndex >> 1, lIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addHorizontalEndPoints(double left, double right, double y)
|
|
|
|
{
|
|
|
|
for (int qIndex = 0; qIndex < 3; qIndex += 2) {
|
|
|
|
if (quad[qIndex].y != y) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (quad[qIndex].x == left) {
|
|
|
|
intersections.insert(qIndex >> 1, 0);
|
|
|
|
}
|
|
|
|
if (quad[qIndex].x == right) {
|
|
|
|
intersections.insert(qIndex >> 1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addVerticalEndPoints(double top, double bottom, double x)
|
|
|
|
{
|
|
|
|
for (int qIndex = 0; qIndex < 3; qIndex += 2) {
|
|
|
|
if (quad[qIndex].x != x) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (quad[qIndex].y == top) {
|
|
|
|
intersections.insert(qIndex >> 1, 0);
|
|
|
|
}
|
|
|
|
if (quad[qIndex].y == bottom) {
|
|
|
|
intersections.insert(qIndex >> 1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-25 18:57:23 +00:00
|
|
|
double findLineT(double t) {
|
2012-09-14 14:19:30 +00:00
|
|
|
double x, y;
|
|
|
|
xy_at_t(quad, t, x, y);
|
|
|
|
double dx = line[1].x - line[0].x;
|
|
|
|
double dy = line[1].y - line[0].y;
|
|
|
|
if (fabs(dx) > fabs(dy)) {
|
|
|
|
return (x - line[0].x) / dx;
|
|
|
|
}
|
|
|
|
return (y - line[0].y) / dy;
|
2012-01-25 18:57:23 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 15:54:16 +00:00
|
|
|
void flip() {
|
|
|
|
// OPTIMIZATION: instead of swapping, pass original line, use [1].y - [0].y
|
|
|
|
int roots = intersections.fUsed;
|
|
|
|
for (int index = 0; index < roots; ++index) {
|
|
|
|
intersections.fT[1][index] = 1 - intersections.fT[1][index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pinTs(double& quadT, double& lineT) {
|
|
|
|
if (!approximately_one_or_less(lineT)) {
|
2012-08-24 15:24:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-19 15:54:16 +00:00
|
|
|
if (!approximately_zero_or_more(lineT)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (quadT < 0) {
|
|
|
|
quadT = 0;
|
|
|
|
} else if (quadT > 1) {
|
|
|
|
quadT = 1;
|
|
|
|
}
|
|
|
|
if (lineT < 0) {
|
2012-08-24 15:24:36 +00:00
|
|
|
lineT = 0;
|
2012-10-19 15:54:16 +00:00
|
|
|
} else if (lineT > 1) {
|
2012-08-24 15:24:36 +00:00
|
|
|
lineT = 1;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-25 18:57:23 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
const Quadratic& quad;
|
|
|
|
const _Line& line;
|
|
|
|
Intersections& intersections;
|
|
|
|
};
|
2012-03-30 18:47:02 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
// utility for pairs of coincident quads
|
|
|
|
static double horizontalIntersect(const Quadratic& quad, const _Point& pt) {
|
2012-10-19 15:54:16 +00:00
|
|
|
LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0));
|
|
|
|
double rootVals[2];
|
|
|
|
int roots = q.horizontalIntersect(pt.y, rootVals);
|
2012-08-31 20:55:07 +00:00
|
|
|
for (int index = 0; index < roots; ++index) {
|
|
|
|
double x;
|
2012-10-19 15:54:16 +00:00
|
|
|
double t = rootVals[index];
|
2012-08-31 20:55:07 +00:00
|
|
|
xy_at_t(quad, t, x, *(double*) 0);
|
|
|
|
if (approximately_equal(x, pt.x)) {
|
|
|
|
return t;
|
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double verticalIntersect(const Quadratic& quad, const _Point& pt) {
|
2012-10-19 15:54:16 +00:00
|
|
|
LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0));
|
|
|
|
double rootVals[2];
|
|
|
|
int roots = q.verticalIntersect(pt.x, rootVals);
|
2012-08-31 20:55:07 +00:00
|
|
|
for (int index = 0; index < roots; ++index) {
|
|
|
|
double y;
|
2012-10-19 15:54:16 +00:00
|
|
|
double t = rootVals[index];
|
2012-08-31 20:55:07 +00:00
|
|
|
xy_at_t(quad, t, *(double*) 0, y);
|
|
|
|
if (approximately_equal(y, pt.y)) {
|
|
|
|
return t;
|
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
double axialIntersect(const Quadratic& q1, const _Point& p, bool vertical) {
|
|
|
|
if (vertical) {
|
|
|
|
return verticalIntersect(q1, p);
|
|
|
|
}
|
|
|
|
return horizontalIntersect(q1, p);
|
|
|
|
}
|
|
|
|
|
2012-03-30 18:47:02 +00:00
|
|
|
int horizontalIntersect(const Quadratic& quad, double left, double right,
|
|
|
|
double y, double tRange[2]) {
|
2012-10-19 15:54:16 +00:00
|
|
|
LineQuadraticIntersections q(quad, *((_Line*) 0), *((Intersections*) 0));
|
|
|
|
double rootVals[2];
|
|
|
|
int result = q.horizontalIntersect(y, rootVals);
|
2012-03-30 18:47:02 +00:00
|
|
|
int tCount = 0;
|
|
|
|
for (int index = 0; index < result; ++index) {
|
|
|
|
double x, y;
|
2012-10-19 15:54:16 +00:00
|
|
|
xy_at_t(quad, rootVals[index], x, y);
|
2012-03-30 18:47:02 +00:00
|
|
|
if (x < left || x > right) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-19 15:54:16 +00:00
|
|
|
tRange[tCount++] = rootVals[index];
|
2012-03-30 18:47:02 +00:00
|
|
|
}
|
|
|
|
return tCount;
|
|
|
|
}
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
int horizontalIntersect(const Quadratic& quad, double left, double right, double y,
|
|
|
|
bool flipped, Intersections& intersections) {
|
|
|
|
LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
|
2012-10-19 15:54:16 +00:00
|
|
|
return q.horizontalIntersect(y, left, right, flipped);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int verticalIntersect(const Quadratic& quad, double top, double bottom, double x,
|
|
|
|
bool flipped, Intersections& intersections) {
|
|
|
|
LineQuadraticIntersections q(quad, *((_Line*) 0), intersections);
|
2012-10-19 15:54:16 +00:00
|
|
|
return q.verticalIntersect(x, top, bottom, flipped);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 15:24:36 +00:00
|
|
|
int intersect(const Quadratic& quad, const _Line& line, Intersections& i) {
|
2012-01-25 18:57:23 +00:00
|
|
|
LineQuadraticIntersections q(quad, line, i);
|
|
|
|
return q.intersect();
|
|
|
|
}
|
2012-09-14 14:19:30 +00:00
|
|
|
|
|
|
|
int intersectRay(const Quadratic& quad, const _Line& line, Intersections& i) {
|
|
|
|
LineQuadraticIntersections q(quad, line, i);
|
2012-10-19 15:54:16 +00:00
|
|
|
return q.intersectRay(i.fT[0]);
|
2012-09-14 14:19:30 +00:00
|
|
|
}
|