Some code-cleanup for a328f5b04b

This commit is contained in:
Jürg Lehni 2016-06-16 11:48:29 +02:00
parent 85c680820e
commit e80b2ff043
2 changed files with 69 additions and 58 deletions

View file

@ -825,20 +825,23 @@ statics: /** @lends Curve */{
}, },
/** /**
* Splits the specified curve values into segments of curves that are monotone in the specified * Splits the specified curve values into segments of curves that are
* coordinate direction (0: monotone in x-direction, 1: monotone in y-direction. If the curve is * monotone in the specified coordinate direction (0: monotone in
* already monotone, an array only containing the original values will be returned. * x-direction, 1: monotone in y-direction. If the curve is already
* monotone, an array only containing the original values will be returned.
*/ */
splitToMonoCurves: function(v, coord) { splitToMonoCurves: function(v, coord) {
var vMono = []; var vMono = [];
// getLength is a rather expensive operation, therefore we test two cheap preconditions first // #getLength() is a rather expensive operation, therefore we test two
// cheap preconditions first.
if (v[0] === v[6] && v[1] === v[7] && Curve.getLength(v) === 0) if (v[0] === v[6] && v[1] === v[7] && Curve.getLength(v) === 0)
return vMono; return vMono;
var o0 = v[1 - coord], var o0 = v[1 - coord],
o1 = v[3 - coord], o1 = v[3 - coord],
o2 = v[5 - coord], o2 = v[5 - coord],
o3 = v[7 - coord]; o3 = v[7 - coord];
if (o0 >= o1 === o1 >= o2 && o1 >= o2 === o2 >= o3 || Curve.isStraight(v)) { if ((o0 >= o1) === (o1 >= o2) && (o1 >= o2) === (o2 >= o3)
|| Curve.isStraight(v)) {
// Straight curves and curves with points and control points ordered // Straight curves and curves with points and control points ordered
// in coordinate direction are guaranteed to be monotone. // in coordinate direction are guaranteed to be monotone.
vMono.push(v); vMono.push(v);

View file

@ -295,58 +295,64 @@ PathItem.inject(new function() {
return results || locations; return results || locations;
} }
/** /**
* Adds the winding contribution of a curve to the already found windings. The curve does not have * Adds the winding contribution of a curve to the already found windings.
* to be a monotone curve. * The curve does not have to be a monotone curve.
* *
* @param v the values of the curve * @param v the values of the curve
* @param vPrev * @param vPrev the values of the previous curve
* @param px x coordinate of the point to be examined * @param px x coordinate of the point to be examined
* @param py y coordinate of the point to be examined * @param py y coordinate of the point to be examined
* @param windings an array of length 2. Index 0 is the windings to the left, index 1 to the right. * @param windings an array of length 2. Index 0 is the windings to the
* left, index 1 to the right.
* @param onCurveWinding * @param onCurveWinding
* @param coord The coordinate direction of the cast ray (0 = x, 1 = y) * @param coord The coordinate direction of the cast ray (0 = x, 1 = y)
*/ */
function addWindingContribution(v, vPrev, px, py, windings, onCurveWinding, coord) { function addWinding(v, vPrev, px, py, windings, onCurveWinding, coord) {
var epsilon = 2e-7; var epsilon = /*#=*/Numerical.WINDING_EPSILON,
var pa = coord ? py : px; // point's abscissa pa = coord ? py : px, // point's abscissa
var po = coord ? px : py; // point's ordinate po = coord ? px : py, // point's ordinate
var vo0 = v[1 - coord], vo0 = v[1 - coord],
vo3 = v[7 - coord]; vo3 = v[7 - coord];
if ((vo0 > po && vo3 > po) || if ((vo0 > po && vo3 > po) ||
(vo0 < po && vo3 < po)) { (vo0 < po && vo3 < po)) {
// if curve is outside the ordinates' range, no intersection with the ray is possible // If curve is outside the ordinates' range, no intersection with
// the ray is possible.
return v; return v;
} }
var aBefore = pa - epsilon; var aBefore = pa - epsilon,
var aAfter = pa + epsilon; aAfter = pa + epsilon,
var va0 = v[coord], va0 = v[coord],
va1 = v[2 + coord], va1 = v[2 + coord],
va2 = v[4 + coord], va2 = v[4 + coord],
va3 = v[6 + coord]; va3 = v[6 + coord];
if (vo0 === vo3) { if (vo0 === vo3) {
if (aAfter > va1 && aBefore < va3 || aAfter > va3 && aBefore < va1) if ( aAfter > va1 && aBefore < va3 ||
onCurveWinding[0] = (onCurveWinding[0] || 0); aAfter > va3 && aBefore < va1)
// if curve does not change in ordinate direction, windings will be added by adjacent curves onCurveWinding[0] = onCurveWinding[0] || 0;
// If curve does not change in ordinate direction, windings will be
// added by adjacent curves.
return vPrev; return vPrev;
} }
var roots = []; var roots = [],
var a = (va0 < aBefore && va1 < aBefore && va2 < aBefore && va3 < aBefore) || a = ( va0 < aBefore && va1 < aBefore &&
(va0 > aAfter && va1 > aAfter && va2 > aAfter && va3 > aAfter) va2 < aBefore && va3 < aBefore) ||
? (va0 + va3) / 2 ( va0 > aAfter && va1 > aAfter &&
: po === vo0 ? va0 va2 > aAfter && va3 > aAfter)
: po === vo3 ? va3 ? (va0 + va3) / 2
: Curve.solveCubic(v, coord ? 0 : 1, po, roots, 0, 1) === 1 : po === vo0 ? va0
? Curve.getPoint(v, roots[0])[coord ? 'y' : 'x'] : po === vo3 ? va3
: (va0 + va3) / 2; : Curve.solveCubic(v, coord ? 0 : 1, po, roots, 0, 1) === 1
var winding = vo0 < vo3 ? 1 : -1; ? Curve.getPoint(v, roots[0])[coord ? 'y' : 'x']
var prevWinding = vPrev[1 - coord] < vPrev[7 - coord] ? 1 : -1; : (va0 + va3) / 2;
var prevAEnd = vPrev[6 + coord]; var winding = vo0 < vo3 ? 1 : -1,
var prevAStart = vPrev[coord]; prevWinding = vPrev[1 - coord] < vPrev[7 - coord] ? 1 : -1,
prevAEnd = vPrev[6 + coord],
prevAStart = vPrev[coord];
if (a != null) { if (a != null) {
if (po !== vo0) { if (po !== vo0) {
// standard case, the ray crosses the curve, but not at the start point // Standard case, the ray crosses the curve, but not at the
// start point.
if (a < aBefore) { if (a < aBefore) {
windings[0] += winding; windings[0] += winding;
} else if (a > aAfter) { } else if (a > aAfter) {
@ -360,7 +366,8 @@ PathItem.inject(new function() {
if (prevAEnd >= aBefore && prevAEnd <= aAfter) { if (prevAEnd >= aBefore && prevAEnd <= aAfter) {
if (winding !== prevWinding) { if (winding !== prevWinding) {
onCurveWinding[0] = (onCurveWinding[0] || 0) + winding; onCurveWinding[0] = (onCurveWinding[0] || 0) + winding;
if (prevAStart < v[6]) { // ToDo: This should be done with comparing tangens // TODO: This should be done with comparing tangents:
if (prevAStart < v[6]) {
windings[1] += winding; windings[1] += winding;
} else if (prevAStart > v[6]) { } else if (prevAStart > v[6]) {
windings[0] += winding; windings[0] += winding;
@ -404,17 +411,16 @@ PathItem.inject(new function() {
return v; return v;
} }
function getWinding(point, curves, horizontal) { function getWinding(point, curves, horizontal) {
var epsilon = /*#=*/Numerical.WINDING_EPSILON, var epsilon = /*#=*/Numerical.WINDING_EPSILON,
abs = Math.abs,
windings = [0, 0], // left, right winding windings = [0, 0], // left, right winding
isOnPath = [null], isOnPath = [null],
onPathWinding = 0, onPathWinding = 0,
length = curves.length, vPrev,
vPrev; pathPrev = null,
var pathPrev = null; coord = horizontal ? 1 : 0;
var coord = horizontal ? 1 : 0; for (var i = 0, l = curves.length; i < l; i++) {
for (var i = 0; i < length; i++) {
var curve = curves[i]; var curve = curves[i];
if (pathPrev !== curve.getPath()) { if (pathPrev !== curve.getPath()) {
if (isOnPath[0] != null) { if (isOnPath[0] != null) {
@ -436,24 +442,28 @@ PathItem.inject(new function() {
vPrev = curve.getValues(); vPrev = curve.getValues();
} }
// get mono curves // get mono curves
var pa = horizontal ? point.y : point.x; var pa = horizontal ? point.y : point.x,
var aBefore = pa - epsilon; aBefore = pa - epsilon,
var aAfter = pa + epsilon; aAfter = pa + epsilon,
var v = curve.getValues(); v = curve.getValues(),
var monoCurves = (v[coord] < aBefore && v[2 + coord] < aBefore && v[4 + coord] < aBefore && v[6 + coord] < aBefore) || monoCurves =
(v[coord] > aAfter && v[2 + coord] > aAfter && v[4 + coord] > aAfter && v[6 + coord] > aAfter) ( v[coord] < aBefore && v[2 + coord] < aBefore &&
? [v] v[4 + coord] < aBefore && v[6 + coord] < aBefore) ||
: Curve.splitToMonoCurves(v, coord); ( v[coord] > aAfter && v[2 + coord] > aAfter &&
for (var j = 0; j < monoCurves.length; j++) { v[4 + coord] > aAfter && v[6 + coord] > aAfter)
vPrev = addWindingContribution(monoCurves[j], vPrev, point.x, point.y, windings, isOnPath, coord); ? [v]
: Curve.splitToMonoCurves(v, coord);
for (var j = 0, m = monoCurves.length; j < m; j++) {
vPrev = addWinding(monoCurves[j], vPrev, point.x, point.y,
windings, isOnPath, coord);
} }
pathPrev = curve.getPath(); pathPrev = curve.getPath();
} }
if (isOnPath[0] != null) { if (isOnPath[0] != null) {
onPathWinding++; onPathWinding++;
} }
var windLeft = windings[0] && (2 - Math.abs(windings[0]) % 2); var windLeft = windings[0] && (2 - abs(windings[0]) % 2),
var windRight = windings[1] && (2 - Math.abs(windings[1]) % 2); windRight = windings[1] && (2 - abs(windings[1]) % 2);
// Use the on-curve windings if no other intersections were found or // Use the on-curve windings if no other intersections were found or
// if they canceled each other. On single paths this ensures that // if they canceled each other. On single paths this ensures that
// the overall winding is 1 if the point was on a monotonic curve. // the overall winding is 1 if the point was on a monotonic curve.
@ -465,13 +475,11 @@ PathItem.inject(new function() {
// This is required when handling unite operations, where a winding // This is required when handling unite operations, where a winding
// contribution of 2 is not part of the result unless it's the contour: // contribution of 2 is not part of the result unless it's the contour:
return { return {
winding: Math.max(Math.abs(windLeft), Math.abs(windRight)), winding: Math.max(abs(windLeft), abs(windRight)),
contour: !windLeft ^ !windRight contour: !windLeft ^ !windRight
}; };
} }
function propagateWinding(segment, path1, path2, curves, operator) { function propagateWinding(segment, path1, path2, curves, operator) {
// Here we try to determine the most likely winding number contribution // Here we try to determine the most likely winding number contribution
// for the curve-chain starting with this segment. Once we have enough // for the curve-chain starting with this segment. Once we have enough