Clean up intersection-fix code.

This commit is contained in:
Jürg Lehni 2013-10-29 10:16:20 +01:00
parent 8ae1105236
commit fb27f92ede

View file

@ -1429,54 +1429,58 @@ new function() { // Scope for methods that require numerical integration
vl = flip ? v1 : v2, vl = flip ? v1 : v2,
l1x = vl[0], l1y = vl[1], l1x = vl[0], l1y = vl[1],
l2x = vl[6], l2y = vl[7], l2x = vl[6], l2y = vl[7],
vc0 = vc[0], vc1 = vc[1], vc2 = vc[2], vc3 = vc[3], vc0 = vc[0], vc1 = vc[1],
vc2 = vc[2], vc3 = vc[3],
vc4 = vc[4], vc5 = vc[5], vc4 = vc[4], vc5 = vc[5],
// Equation of the line Ax + By + C = 0 // Equation of the line Ax + By + C = 0
A = l2y-l1y, //A=y2-y1 // A = y2 - y1
B = l1x-l2x, //B=x1-x2 // B = x1 - x2
C = l1x*(l1y-l2y) + l1y*(l2x-l1x), //C=x1*(y1-y2)+y1*(x2-x1) // C = x1 * (y1 - y2) + y1 * (x2 - x1)
A = l2y - l1y,
B = l1x - l2x,
C = l1x * (l1y - l2y) + l1y * (l2x - l1x),
// Bernstein coefficients for the curve // Bernstein coefficients for the curve
bx0 = -vc0 + 3*vc2 + -3*vc4 + vc[6], bx0 = -vc0 + 3 * vc2 + -3 * vc4 + vc[6],
bx1 = 3*vc0 - 6*vc2 + 3*vc4, bx1 = 3 * vc0 - 6 * vc2 + 3 * vc4,
bx2 = -3*vc0 + 3*vc2, bx2 = -3 * vc0 + 3 * vc2,
bx3 = vc0, bx3 = vc0,
by0 = -vc1 + 3*vc3 + -3*vc5 + vc[7], by0 = -vc1 + 3 * vc3 + -3 * vc5 + vc[7],
by1 = 3*vc1 - 6*vc3 + 3*vc5, by1 = 3 * vc1 - 6 * vc3 + 3 * vc5,
by2 = -3*vc1 + 3*vc3, by2 = -3 * vc1 + 3 * vc3,
by3 = vc1, by3 = vc1,
// Form the cubic equation // Form the cubic equation
// a*t^3 + b*t^2 + c*t + d = 0 // a * t^3 + b*t^2 + c*t + d = 0
a = A*bx0 + B*by0, /*t^3*/ a = A * bx0 + B * by0, // t^3
b = A*bx1 + B*by1, /*t^2*/ b = A * bx1 + B * by1, // t^2
c = A*bx2 + B*by2, /*t*/ c = A * bx2 + B * by2, // t
d = A*bx3 + B*by3 + C, /*1*/ d = A * bx3 + B * by3 + C, // t1
roots = [], count, x, y, t, tl; roots = [],
// Solve the cubic equation, interested only in results in [0 .. 1]
// Solve the cubic equation count = Numerical.solveCubic(a, b, c, d, roots, 0, 1);
count = Numerical.solveCubic(a, b, c, d, roots);
// NOTE: count could be -1 for inifnite solutions, but that should only // NOTE: count could be -1 for inifnite solutions, but that should only
// happen with lines, in which case we should not be here. // happen with lines, in which case we should not be here.
for (var i=0;i<count;i++) { for (var i = 0; i < count; i++) {
t = roots[i]; var t = roots[i],
if(t >= 0 && t <= 1.0){ tt = t * t,
x = bx0*t*t*t + bx1*t*t + bx2*t + bx3; ttt = tt * t,
y = by0*t*t*t + by1*t*t + by2*t + by3; x = bx0 * ttt + bx1 * tt + bx2 * t + bx3,
// tl is the parameter of the intersection point in line segment. y = by0 * ttt + by1 * tt + by2 * t + by3;
// Special case to override the tight tolerence in // tl is the parameter of the intersection point in line segment.
// Curve.solveQuadratic when line is horizontal // Special case to override the tight tolerence in
if (l2y-l1y === 0) // Curve.solveQuadratic when line is horizontal
y = l1y; if (l2y === l1y)
tl = Curve.getParameterOf(vl, x, y); y = l1y;
// We do have a point on the infinite line. Check if it falls on var tl = Curve.getParameterOf(vl, x, y),
// the line *segment*. t2;
if(tl >= 0 && tl <= 1.0){ // We do have a point on the infinite line. Check if it falls on
// Interpolate the parameter for the intersection on line. // the line *segment*.
var t1 = flip ? tl : t, if (tl >= 0 && tl <= 1) {
t2 = flip ? t : tl; // Interpolate the parameter for the intersection on line.
addLocation(locations, t1 = flip ? tl : t;
curve1, t1, Curve.evaluate(v1, t1, 0), t2 = flip ? t : tl;
curve2, t2, Curve.evaluate(v2, t2, 0)); addLocation(locations,
} curve1, t1, Curve.evaluate(v1, t1, 0),
curve2, t2, Curve.evaluate(v2, t2, 0));
} }
} }
} }