Optimize fat-line clipping code a bit further.

We don't need to calculate v1Clip and tDiff if oldTDiff > 0.5 && tDiff > 0.5.
This commit is contained in:
Jürg Lehni 2015-12-30 23:19:58 +01:00
parent f19bdf9834
commit df24de0fdf

View file

@ -1465,21 +1465,17 @@ new function() { // Scope for intersection using bezier fat-line clipping
(tMaxClip = clipConvexHull(top.reverse(), bottom.reverse(), (tMaxClip = clipConvexHull(top.reverse(), bottom.reverse(),
dMin, dMax)) == null) dMin, dMax)) == null)
return; return;
// Clip P with the fat-line for Q
var v1New = Curve.getPart(v1, tMinClip, tMaxClip),
tDiff = tMaxClip - tMinClip,
// tMin and tMax are within the range (0, 1). We need to project it // tMin and tMax are within the range (0, 1). We need to project it
// to the original parameter range for v2. // to the original parameter range for v2.
tMinNew = tMin + (tMax - tMin) * tMinClip, var tMinNew = tMin + (tMax - tMin) * tMinClip,
tMaxNew = tMin + (tMax - tMin) * tMaxClip; tMaxNew = tMin + (tMax - tMin) * tMaxClip;
if (Math.max(uMax - uMin, tMaxNew - tMinNew) if (Math.max(uMax - uMin, tMaxNew - tMinNew)
< /*#=*/Numerical.CLIPPING_EPSILON) { < /*#=*/Numerical.CLIPPING_EPSILON) {
// We have isolated the intersection with sufficient precision // We have isolated the intersection with sufficient precision
var t = (tMinNew + tMaxNew) / 2, var t = (tMinNew + tMaxNew) / 2,
u = (uMin + uMax) / 2; u = (uMin + uMax) / 2;
// Since we've been chopping up v1 and v2, we need to pass on the // As we've been clipping v1 and v2, we need to pass on the original
// original full curves here again to match the parameter space of // curves here again to match the parameter space of t1 and t2.
// t1 and t2.
// TODO: Add two more arguments to addCurveIntersections after param // TODO: Add two more arguments to addCurveIntersections after param
// to pass on the sub-curves. // to pass on the sub-curves.
v1 = c1.getValues(); v1 = c1.getValues();
@ -1487,10 +1483,14 @@ new function() { // Scope for intersection using bezier fat-line clipping
addLocation(locations, param, addLocation(locations, param,
reverse ? v2 : v1, reverse ? c2 : c1, reverse ? u : t, null, reverse ? v2 : v1, reverse ? c2 : c1, reverse ? u : t, null,
reverse ? v1 : v2, reverse ? c1 : c2, reverse ? t : u, null); reverse ? v1 : v2, reverse ? c1 : c2, reverse ? t : u, null);
} else if (oldTDiff > 0.5 && tDiff > 0.5) { } else {
// Clip P with the fat-line for Q
var v1Clip = Curve.getPart(v1, tMinClip, tMaxClip),
tDiff = tMaxClip - tMinClip;
if (oldTDiff > 0.5 && tDiff > 0.5) {
// Subdivide the curve which has converged the least. // Subdivide the curve which has converged the least.
if (tMaxNew - tMinNew > uMax - uMin) { if (tMaxNew - tMinNew > uMax - uMin) {
var parts = Curve.subdivide(v1New, 0.5), var parts = Curve.subdivide(v1Clip, 0.5),
t = (tMinNew + tMaxNew) / 2; t = (tMinNew + tMaxNew) / 2;
addCurveIntersections( addCurveIntersections(
v2, parts[0], c2, c1, locations, param, v2, parts[0], c2, c1, locations, param,
@ -1502,22 +1502,24 @@ new function() { // Scope for intersection using bezier fat-line clipping
var parts = Curve.subdivide(v2, 0.5), var parts = Curve.subdivide(v2, 0.5),
u = (uMin + uMax) / 2; u = (uMin + uMax) / 2;
addCurveIntersections( addCurveIntersections(
parts[0], v1New, c2, c1, locations, param, parts[0], v1Clip, c2, c1, locations, param,
uMin, u, tMinNew, tMaxNew, tDiff, !reverse, recursion); uMin, u, tMinNew, tMaxNew, tDiff, !reverse, recursion);
addCurveIntersections( addCurveIntersections(
parts[1], v1New, c2, c1, locations, param, parts[1], v1Clip, c2, c1, locations, param,
u, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); u, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion);
} }
} else if (tDiff > 0) { // Iterate } else if (tDiff > 0) { // Iterate
addCurveIntersections(v2, v1New, c2, c1, locations, param, addCurveIntersections(v2, v1Clip, c2, c1, locations, param,
uMin, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); uMin, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion);
} else { } else {
// curve 1 has converged to a point. Since we cannot construct a fat-line from // P has converged to a point. Since we cannot construct a fat-
// a point, we dismiss this clipping so we can continue clipping curve 2. // line from a point, we dismiss this clipping so we can
// continue with clipping Q.
addCurveIntersections(v2, v1, c2, c1, locations, param, addCurveIntersections(v2, v1, c2, c1, locations, param,
uMin, uMax, tMin, tMax, tDiff, !reverse, recursion); uMin, uMax, tMin, tMax, tDiff, !reverse, recursion);
} }
} }
}
/** /**
* Calculate the convex hull for the non-parametric bezier curve D(ti, di(t)) * Calculate the convex hull for the non-parametric bezier curve D(ti, di(t))