2013-05-03 19:16:52 -04:00
|
|
|
/*
|
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
2014-01-03 19:47:16 -05:00
|
|
|
* Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://scratchdisk.com/ & http://jonathanpuckey.com/
|
2013-05-03 19:16:52 -04:00
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-05-03 19:31:36 -04:00
|
|
|
* Boolean Geometric Path Operations
|
2013-05-03 19:16:52 -04:00
|
|
|
*
|
|
|
|
* This is mostly written for clarity and compatibility, not optimised for
|
|
|
|
* performance, and has to be tested heavily for stability.
|
|
|
|
*
|
|
|
|
* Supported
|
2013-05-05 19:38:18 -04:00
|
|
|
* - Path and CompoundPath items
|
2013-05-03 19:16:52 -04:00
|
|
|
* - Boolean Union
|
|
|
|
* - Boolean Intersection
|
|
|
|
* - Boolean Subtraction
|
|
|
|
* - Resolving a self-intersecting Path
|
|
|
|
*
|
|
|
|
* Not supported yet
|
|
|
|
* - Boolean operations on self-intersecting Paths
|
|
|
|
* - Paths are clones of each other that ovelap exactly on top of each other!
|
|
|
|
*
|
|
|
|
* @author Harikrishnan Gopalakrishnan
|
|
|
|
* http://hkrish.com/playground/paperjs/booleanStudy.html
|
|
|
|
*/
|
|
|
|
|
2014-02-20 14:24:16 -05:00
|
|
|
PathItem.inject(new function() {
|
2015-01-03 19:50:24 -05:00
|
|
|
var operators = {
|
|
|
|
unite: function(w) {
|
|
|
|
return w === 1 || w === 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
intersect: function(w) {
|
|
|
|
return w === 2;
|
|
|
|
},
|
|
|
|
|
|
|
|
subtract: function(w) {
|
|
|
|
return w === 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
exclude: function(w) {
|
|
|
|
return w === 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
// Boolean operators return true if a curve with the given winding
|
|
|
|
// contribution contributes to the final result or not. They are called
|
|
|
|
// for each curve in the graph after curves in the operands are
|
|
|
|
// split at intersections.
|
2015-01-03 19:50:24 -05:00
|
|
|
function computeBoolean(path1, path2, operation) {
|
|
|
|
var operator = operators[operation];
|
2015-01-02 09:33:23 -05:00
|
|
|
// Creates a cloned version of the path that we can modify freely, with
|
|
|
|
// its matrix applied to its geometry. Calls #reduce() to simplify
|
|
|
|
// compound paths and remove empty curves, and #reorient() to make sure
|
|
|
|
// all paths have correct winding direction.
|
|
|
|
function preparePath(path) {
|
2015-01-03 19:50:24 -05:00
|
|
|
return path.clone(false).reduce().reorient().transform(null, true,
|
|
|
|
true);
|
2015-01-02 09:33:23 -05:00
|
|
|
}
|
2014-03-17 05:04:09 -04:00
|
|
|
|
2015-01-03 19:50:24 -05:00
|
|
|
// We do not modify the operands themselves, but create copies instead,
|
|
|
|
// fas produced by the calls to preparePath().
|
|
|
|
// Note that the result paths might not belong to the same type
|
2015-01-02 09:33:23 -05:00
|
|
|
// i.e. subtraction(A:Path, B:Path):CompoundPath etc.
|
|
|
|
var _path1 = preparePath(path1),
|
|
|
|
_path2 = path2 && path1 !== path2 && preparePath(path2);
|
2015-01-03 19:51:27 -05:00
|
|
|
// Give both paths the same orientation except for subtraction
|
2015-01-03 19:50:24 -05:00
|
|
|
// and exclusion, where we need them at opposite orientation.
|
2015-01-03 19:51:27 -05:00
|
|
|
if (_path2 && /^(subtract|exclude)$/.test(operation)
|
|
|
|
^ (_path2.isClockwise() !== _path1.isClockwise()))
|
2015-01-02 09:33:23 -05:00
|
|
|
_path2.reverse();
|
|
|
|
// Split curves at intersections on both paths. Note that for self
|
|
|
|
// intersection, _path2 will be null and getIntersections() handles it.
|
|
|
|
splitPath(_path1.getIntersections(_path2, null, true));
|
2014-02-20 13:10:46 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
var chain = [],
|
|
|
|
segments = [],
|
|
|
|
// Aggregate of all curves in both operands, monotonic in y
|
2015-01-03 05:24:27 -05:00
|
|
|
monoCurves = [],
|
|
|
|
tolerance = /*#=*/Numerical.TOLERANCE;
|
2014-01-25 23:39:51 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
function collect(paths) {
|
|
|
|
for (var i = 0, l = paths.length; i < l; i++) {
|
|
|
|
var path = paths[i];
|
|
|
|
segments.push.apply(segments, path._segments);
|
|
|
|
monoCurves.push.apply(monoCurves, path._getMonoCurves());
|
|
|
|
}
|
|
|
|
}
|
2014-02-20 13:10:46 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
// Collect all segments and monotonic curves
|
|
|
|
collect(_path1._children || [_path1]);
|
|
|
|
if (_path2)
|
|
|
|
collect(_path2._children || [_path2]);
|
|
|
|
// Propagate the winding contribution. Winding contribution of curves
|
|
|
|
// does not change between two intersections.
|
|
|
|
// First, sort all segments with an intersection to the beginning.
|
|
|
|
segments.sort(function(a, b) {
|
|
|
|
var _a = a._intersection,
|
|
|
|
_b = b._intersection;
|
|
|
|
return !_a && !_b || _a && _b ? 0 : _a ? -1 : 1;
|
|
|
|
});
|
|
|
|
for (var i = 0, l = segments.length; i < l; i++) {
|
|
|
|
var segment = segments[i];
|
|
|
|
if (segment._winding != null)
|
|
|
|
continue;
|
|
|
|
// Here we try to determine the most probable winding number
|
|
|
|
// contribution for this curve-chain. Once we have enough confidence
|
|
|
|
// in the winding contribution, we can propagate it until the
|
|
|
|
// intersection or end of a curve chain.
|
2015-01-02 18:26:13 -05:00
|
|
|
chain.length = 0;
|
|
|
|
var startSeg = segment,
|
2015-01-03 05:25:10 -05:00
|
|
|
totalLength = 0,
|
|
|
|
windingSum = 0;
|
2015-01-02 09:33:23 -05:00
|
|
|
do {
|
2015-01-02 18:26:13 -05:00
|
|
|
var length = segment.getCurve().getLength();
|
|
|
|
chain.push({ segment: segment, length: length });
|
|
|
|
totalLength += length;
|
2015-01-02 09:33:23 -05:00
|
|
|
segment = segment.getNext();
|
|
|
|
} while (segment && !segment._intersection && segment !== startSeg);
|
2015-01-03 05:25:10 -05:00
|
|
|
// Calculate the average winding among three evenly distributed
|
|
|
|
// points along this curve chain as a representative winding number.
|
2015-01-02 18:32:06 -05:00
|
|
|
// This selection gives a better chance of returning a correct
|
|
|
|
// winding than equally dividing the curve chain, with the same
|
|
|
|
// (amortised) time.
|
2015-01-02 09:33:23 -05:00
|
|
|
for (var j = 0; j < 3; j++) {
|
2015-01-02 18:32:06 -05:00
|
|
|
// Try the points at 1/4, 2/4 and 3/4 of the total length:
|
2015-01-03 05:24:27 -05:00
|
|
|
var length = totalLength * (j + 1) / 4;
|
2015-01-02 18:26:13 -05:00
|
|
|
for (k = 0, m = chain.length; k < m; k++) {
|
2015-01-03 05:24:27 -05:00
|
|
|
var node = chain[k],
|
|
|
|
curveLength = node.length;
|
|
|
|
if (length <= curveLength) {
|
|
|
|
// If the selected location on the curve falls onto its
|
|
|
|
// beginning or end, use the curve's center instead.
|
2015-01-03 14:35:51 -05:00
|
|
|
if (length <= tolerance
|
|
|
|
|| curveLength - length <= tolerance)
|
2015-01-03 05:24:27 -05:00
|
|
|
length = curveLength / 2;
|
|
|
|
var curve = node.segment.getCurve(),
|
2015-01-02 18:26:13 -05:00
|
|
|
pt = curve.getPointAt(length),
|
2015-01-03 15:02:12 -05:00
|
|
|
// Determine if the curve is a horizontal linear
|
|
|
|
// curve by checking the slope of it's tangent.
|
|
|
|
hor = curve.isLinear() && Math.abs(curve
|
|
|
|
.getTangentAt(0.5, true).y) <= tolerance,
|
2015-01-02 18:26:13 -05:00
|
|
|
path = curve._path;
|
|
|
|
if (path._parent instanceof CompoundPath)
|
|
|
|
path = path._parent;
|
|
|
|
// While subtracting, we need to omit this curve if this
|
|
|
|
// curve is contributing to the second operand and is
|
|
|
|
// outside the first operand.
|
2015-01-03 19:50:24 -05:00
|
|
|
windingSum += operation === 'subtract' && _path2
|
2015-01-02 18:26:13 -05:00
|
|
|
&& (path === _path1 && _path2._getWinding(pt, hor)
|
|
|
|
|| path === _path2 && !_path1._getWinding(pt, hor))
|
|
|
|
? 0
|
|
|
|
: getWinding(pt, monoCurves, hor);
|
2015-01-02 09:33:23 -05:00
|
|
|
break;
|
|
|
|
}
|
2015-01-03 05:24:27 -05:00
|
|
|
length -= curveLength;
|
2015-01-02 18:26:13 -05:00
|
|
|
}
|
2015-01-02 09:33:23 -05:00
|
|
|
}
|
2015-01-03 05:25:10 -05:00
|
|
|
// Assign the average winding to the entire curve chain.
|
|
|
|
var winding = Math.round(windingSum / 3);
|
2015-01-02 09:33:23 -05:00
|
|
|
for (var j = chain.length - 1; j >= 0; j--)
|
2015-01-02 18:26:13 -05:00
|
|
|
chain[j].segment._winding = winding;
|
2015-01-02 09:33:23 -05:00
|
|
|
}
|
|
|
|
// Trace closed contours and insert them into the result.
|
2015-01-04 15:59:31 -05:00
|
|
|
var result = new CompoundPath(Item.NO_INSERT);
|
|
|
|
result.insertAbove(path1);
|
2015-01-02 09:33:23 -05:00
|
|
|
result.addChildren(tracePaths(segments, operator), true);
|
|
|
|
// See if the CompoundPath can be reduced to just a simple Path.
|
|
|
|
result = result.reduce();
|
|
|
|
// Copy over the left-hand item's style and we're done.
|
|
|
|
// TODO: Consider using Item#_clone() for this, but find a way to not
|
|
|
|
// clone children / name (content).
|
|
|
|
result.setStyle(path1._style);
|
|
|
|
return result;
|
|
|
|
}
|
2014-02-20 13:50:37 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
/**
|
|
|
|
* Private method for splitting a PathItem at the given intersections.
|
|
|
|
* The routine works for both self intersections and intersections
|
|
|
|
* between PathItems.
|
2015-06-16 11:50:37 -04:00
|
|
|
*
|
2015-01-02 09:33:23 -05:00
|
|
|
* @param {CurveLocation[]} intersections Array of CurveLocation objects
|
|
|
|
*/
|
|
|
|
function splitPath(intersections) {
|
2015-01-04 11:37:15 -05:00
|
|
|
var tMin = /*#=*/Numerical.TOLERANCE,
|
|
|
|
tMax = 1 - tMin,
|
2015-01-02 09:33:23 -05:00
|
|
|
linearHandles;
|
2014-02-20 13:50:37 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
function resetLinear() {
|
|
|
|
// Reset linear segments if they were part of a linear curve
|
|
|
|
// and if we are done with the entire curve.
|
|
|
|
for (var i = 0, l = linearHandles.length; i < l; i++)
|
|
|
|
linearHandles[i].set(0, 0);
|
|
|
|
}
|
2014-02-20 13:50:37 -05:00
|
|
|
|
2015-01-04 12:07:02 -05:00
|
|
|
for (var i = intersections.length - 1, curve, prev; i >= 0; i--) {
|
2015-01-02 09:33:23 -05:00
|
|
|
var loc = intersections[i],
|
|
|
|
t = loc._parameter;
|
2015-01-04 11:37:15 -05:00
|
|
|
// Check if we are splitting same curve multiple times, but avoid
|
|
|
|
// dividing with zero.
|
2015-01-04 12:07:02 -05:00
|
|
|
if (prev && prev._curve === loc._curve && prev._parameter > 0) {
|
2015-01-02 09:33:23 -05:00
|
|
|
// Scale parameter after previous split.
|
2015-01-04 12:07:02 -05:00
|
|
|
t /= prev._parameter;
|
2015-01-02 09:33:23 -05:00
|
|
|
} else {
|
|
|
|
curve = loc._curve;
|
|
|
|
if (linearHandles)
|
2014-08-16 13:24:54 -04:00
|
|
|
resetLinear();
|
2015-01-04 11:37:15 -05:00
|
|
|
linearHandles = curve.isLinear() ? [
|
|
|
|
curve._segment1._handleOut,
|
|
|
|
curve._segment2._handleIn
|
|
|
|
] : null;
|
2015-01-02 09:33:23 -05:00
|
|
|
}
|
|
|
|
var newCurve,
|
|
|
|
segment;
|
|
|
|
// Split the curve at t, while ignoring linearity of curves
|
|
|
|
if (newCurve = curve.divide(t, true, true)) {
|
|
|
|
segment = newCurve._segment1;
|
|
|
|
curve = newCurve.getPrevious();
|
|
|
|
if (linearHandles)
|
|
|
|
linearHandles.push(segment._handleOut, segment._handleIn);
|
|
|
|
} else {
|
2015-01-04 11:37:15 -05:00
|
|
|
segment = t < tMin
|
2015-01-02 09:33:23 -05:00
|
|
|
? curve._segment1
|
2015-01-04 11:37:15 -05:00
|
|
|
: t > tMax
|
2015-01-02 09:33:23 -05:00
|
|
|
? curve._segment2
|
|
|
|
: curve.getPartLength(0, t) < curve.getPartLength(t, 1)
|
|
|
|
? curve._segment1
|
|
|
|
: curve._segment2;
|
|
|
|
}
|
|
|
|
// Link the new segment with the intersection on the other curve
|
|
|
|
segment._intersection = loc.getIntersection();
|
|
|
|
loc._segment = segment;
|
2015-01-04 12:07:02 -05:00
|
|
|
prev = loc;
|
2015-01-02 09:33:23 -05:00
|
|
|
}
|
|
|
|
if (linearHandles)
|
|
|
|
resetLinear();
|
|
|
|
}
|
2014-02-20 13:50:37 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
/**
|
|
|
|
* Private method that returns the winding contribution of the given point
|
|
|
|
* with respect to a given set of monotone curves.
|
|
|
|
*/
|
|
|
|
function getWinding(point, curves, horizontal, testContains) {
|
2015-01-04 09:54:50 -05:00
|
|
|
var tolerance = /*#=*/Numerical.TOLERANCE,
|
2015-01-02 15:26:04 -05:00
|
|
|
tMin = tolerance,
|
2015-01-02 17:47:26 -05:00
|
|
|
tMax = 1 - tMin,
|
2015-01-04 18:13:30 -05:00
|
|
|
px = point.x,
|
|
|
|
py = point.y,
|
2015-01-02 09:33:23 -05:00
|
|
|
windLeft = 0,
|
|
|
|
windRight = 0,
|
|
|
|
roots = [],
|
2015-01-02 15:19:18 -05:00
|
|
|
abs = Math.abs;
|
2015-01-02 09:33:23 -05:00
|
|
|
// Absolutely horizontal curves may return wrong results, since
|
|
|
|
// the curves are monotonic in y direction and this is an
|
|
|
|
// indeterminate state.
|
|
|
|
if (horizontal) {
|
|
|
|
var yTop = -Infinity,
|
|
|
|
yBottom = Infinity,
|
2015-01-04 18:13:30 -05:00
|
|
|
yBefore = py - tolerance,
|
|
|
|
yAfter = py + tolerance;
|
2015-01-02 09:33:23 -05:00
|
|
|
// Find the closest top and bottom intercepts for the same vertical
|
|
|
|
// line.
|
|
|
|
for (var i = 0, l = curves.length; i < l; i++) {
|
|
|
|
var values = curves[i].values;
|
2015-01-04 18:13:30 -05:00
|
|
|
if (Curve.solveCubic(values, 0, px, roots, 0, 1) > 0) {
|
2015-01-02 09:33:23 -05:00
|
|
|
for (var j = roots.length - 1; j >= 0; j--) {
|
2015-01-04 18:13:30 -05:00
|
|
|
var y = Curve.evaluate(values, roots[j], 0).y;
|
|
|
|
if (y < yBefore && y > yTop) {
|
|
|
|
yTop = y;
|
|
|
|
} else if (y > yAfter && y < yBottom) {
|
|
|
|
yBottom = y;
|
2015-01-02 09:33:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Shift the point lying on the horizontal curves by
|
|
|
|
// half of closest top and bottom intercepts.
|
2015-01-04 18:13:30 -05:00
|
|
|
yTop = (yTop + py) / 2;
|
|
|
|
yBottom = (yBottom + py) / 2;
|
2015-01-02 09:33:23 -05:00
|
|
|
if (yTop > -Infinity)
|
2015-01-04 18:13:30 -05:00
|
|
|
windLeft = getWinding(new Point(px, yTop), curves);
|
2015-01-02 09:33:23 -05:00
|
|
|
if (yBottom < Infinity)
|
2015-01-04 18:13:30 -05:00
|
|
|
windRight = getWinding(new Point(px, yBottom), curves);
|
2015-01-02 09:33:23 -05:00
|
|
|
} else {
|
2015-01-04 18:13:30 -05:00
|
|
|
var xBefore = px - tolerance,
|
|
|
|
xAfter = px + tolerance;
|
2015-01-02 09:33:23 -05:00
|
|
|
// Find the winding number for right side of the curve, inclusive of
|
|
|
|
// the curve itself, while tracing along its +-x direction.
|
|
|
|
for (var i = 0, l = curves.length; i < l; i++) {
|
|
|
|
var curve = curves[i],
|
|
|
|
values = curve.values,
|
|
|
|
winding = curve.winding,
|
2015-01-04 18:09:34 -05:00
|
|
|
prevT,
|
2015-01-04 18:13:30 -05:00
|
|
|
prevX;
|
2015-01-04 17:59:25 -05:00
|
|
|
// Since the curves are monotone in y direction, we can just
|
|
|
|
// compare the endpoints of the curve to determine if the
|
|
|
|
// ray from query point along +-x direction will intersect
|
|
|
|
// the monotone curve. Results in quite significant speedup.
|
2015-01-02 09:33:23 -05:00
|
|
|
if (winding && (winding === 1
|
2015-01-04 18:13:30 -05:00
|
|
|
&& py >= values[1] && py <= values[7]
|
|
|
|
|| py >= values[7] && py <= values[1])
|
|
|
|
&& Curve.solveCubic(values, 1, py, roots, 0, 1) === 1) {
|
2015-01-04 17:28:39 -05:00
|
|
|
var t = roots[0],
|
2015-01-04 18:13:30 -05:00
|
|
|
x = Curve.evaluate(values, t, 0).x,
|
2015-01-04 17:28:39 -05:00
|
|
|
slope = Curve.evaluate(values, t, 1).y;
|
2015-01-02 09:33:23 -05:00
|
|
|
// Due to numerical precision issues, two consecutive curves
|
|
|
|
// may register an intercept twice, at t = 1 and 0, if y is
|
|
|
|
// almost equal to one of the endpoints of the curves.
|
2015-01-04 17:59:25 -05:00
|
|
|
// But since curves may contain more than one loop of curves
|
|
|
|
// and the end point on the last curve of a loop would not
|
|
|
|
// be registered as a double, we need to filter these cases:
|
|
|
|
if (!(t > tMax
|
|
|
|
// Detect and exclude intercepts at 'end' of loops:
|
|
|
|
&& (i === l - 1 || curve.next !== curves[i + 1])
|
2015-01-04 18:13:30 -05:00
|
|
|
&& abs(Curve.evaluate(curve.next.values, 0, 0).x -x)
|
|
|
|
<= tolerance
|
2015-01-04 17:59:25 -05:00
|
|
|
// Detect 2nd case of a consecutive intercept, but make
|
|
|
|
// sure we're still on the same loop
|
2015-01-04 18:09:34 -05:00
|
|
|
|| i > 0 && curve.previous === curves[i - 1]
|
2015-01-04 18:13:30 -05:00
|
|
|
&& abs(prevX - x) < tolerance
|
2015-01-04 18:09:34 -05:00
|
|
|
&& prevT > tMax && t < tMin)) {
|
2015-01-04 16:37:27 -05:00
|
|
|
// Take care of cases where the curve and the preceding
|
|
|
|
// curve merely touches the ray towards +-x direction,
|
|
|
|
// but proceeds to the same side of the ray.
|
|
|
|
// This essentially is not a crossing.
|
|
|
|
if (Numerical.isZero(slope) && !Curve.isLinear(values)
|
2015-01-04 17:28:39 -05:00
|
|
|
// Does the slope over curve beginning change?
|
2015-01-04 16:37:27 -05:00
|
|
|
|| t < tMin && slope * Curve.evaluate(
|
2015-01-04 17:28:39 -05:00
|
|
|
curve.previous.values, 1, 1).y < 0
|
|
|
|
// Does the slope over curve end change?
|
|
|
|
|| t > tMax && slope * Curve.evaluate(
|
|
|
|
curve.next.values, 0, 1).y < 0) {
|
2015-01-04 18:13:30 -05:00
|
|
|
if (testContains && x >= xBefore && x <= xAfter) {
|
2015-01-04 16:37:27 -05:00
|
|
|
++windLeft;
|
|
|
|
++windRight;
|
|
|
|
}
|
2015-01-04 18:13:30 -05:00
|
|
|
} else if (x <= xBefore) {
|
2015-01-04 16:37:27 -05:00
|
|
|
windLeft += winding;
|
2015-01-04 18:13:30 -05:00
|
|
|
} else if (x >= xAfter) {
|
2015-01-04 16:37:27 -05:00
|
|
|
windRight += winding;
|
2015-01-02 09:33:23 -05:00
|
|
|
}
|
|
|
|
}
|
2015-01-04 18:09:34 -05:00
|
|
|
prevT = t;
|
2015-01-04 18:13:30 -05:00
|
|
|
prevX = x;
|
2015-01-02 09:33:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Math.max(abs(windLeft), abs(windRight));
|
|
|
|
}
|
2014-02-20 13:50:37 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
/**
|
|
|
|
* Private method to trace closed contours from a set of segments according
|
|
|
|
* to a set of constraints-winding contribution and a custom operator.
|
|
|
|
*
|
|
|
|
* @param {Segment[]} segments Array of 'seed' segments for tracing closed
|
|
|
|
* contours
|
|
|
|
* @param {Function} the operator function that receives as argument the
|
|
|
|
* winding number contribution of a curve and returns a boolean value
|
|
|
|
* indicating whether the curve should be included in the final contour or
|
|
|
|
* not
|
|
|
|
* @return {Path[]} the contours traced
|
|
|
|
*/
|
|
|
|
function tracePaths(segments, operator, selfOp) {
|
|
|
|
var paths = [],
|
|
|
|
// Values for getTangentAt() that are almost 0 and 1.
|
|
|
|
// TODO: Correctly support getTangentAt(0) / (1)?
|
2015-01-03 14:59:20 -05:00
|
|
|
tMin = /*#=*/Numerical.TOLERANCE,
|
|
|
|
tMax = 1 - tMin;
|
2015-01-02 09:33:23 -05:00
|
|
|
for (var i = 0, seg, startSeg, l = segments.length; i < l; i++) {
|
|
|
|
seg = startSeg = segments[i];
|
|
|
|
if (seg._visited || !operator(seg._winding))
|
|
|
|
continue;
|
|
|
|
var path = new Path(Item.NO_INSERT),
|
|
|
|
inter = seg._intersection,
|
|
|
|
startInterSeg = inter && inter._segment,
|
|
|
|
added = false, // Whether a first segment as added already
|
|
|
|
dir = 1;
|
|
|
|
do {
|
|
|
|
var handleIn = dir > 0 ? seg._handleIn : seg._handleOut,
|
|
|
|
handleOut = dir > 0 ? seg._handleOut : seg._handleIn,
|
|
|
|
interSeg;
|
|
|
|
// If the intersection segment is valid, try switching to
|
|
|
|
// it, with an appropriate direction to continue traversal.
|
|
|
|
// Else, stay on the same contour.
|
|
|
|
if (added && (!operator(seg._winding) || selfOp)
|
|
|
|
&& (inter = seg._intersection)
|
|
|
|
&& (interSeg = inter._segment)
|
|
|
|
&& interSeg !== startSeg) {
|
|
|
|
if (selfOp) {
|
|
|
|
// Switch to the intersection segment, if we are
|
|
|
|
// resolving self-Intersections.
|
|
|
|
seg._visited = interSeg._visited;
|
|
|
|
seg = interSeg;
|
|
|
|
dir = 1;
|
|
|
|
} else {
|
|
|
|
var c1 = seg.getCurve();
|
|
|
|
if (dir > 0)
|
|
|
|
c1 = c1.getPrevious();
|
2015-01-03 14:59:20 -05:00
|
|
|
var t1 = c1.getTangentAt(dir < 1 ? tMin : tMax, true),
|
2015-01-02 09:33:23 -05:00
|
|
|
// Get both curves at the intersection (except the
|
|
|
|
// entry curves).
|
|
|
|
c4 = interSeg.getCurve(),
|
|
|
|
c3 = c4.getPrevious(),
|
|
|
|
// Calculate their winding values and tangents.
|
2015-01-03 14:59:20 -05:00
|
|
|
t3 = c3.getTangentAt(tMax, true),
|
|
|
|
t4 = c4.getTangentAt(tMin, true),
|
2015-01-02 09:33:23 -05:00
|
|
|
// Cross product of the entry and exit tangent
|
|
|
|
// vectors at the intersection, will let us select
|
|
|
|
// the correct contour to traverse next.
|
|
|
|
w3 = t1.cross(t3),
|
|
|
|
w4 = t1.cross(t4);
|
|
|
|
if (w3 * w4 !== 0) {
|
|
|
|
// Do not attempt to switch contours if we aren't
|
|
|
|
// sure that there is a possible candidate.
|
|
|
|
var curve = w3 < w4 ? c3 : c4,
|
|
|
|
nextCurve = operator(curve._segment1._winding)
|
|
|
|
? curve
|
|
|
|
: w3 < w4 ? c4 : c3,
|
|
|
|
nextSeg = nextCurve._segment1;
|
|
|
|
dir = nextCurve === c3 ? -1 : 1;
|
|
|
|
// If we didn't find a suitable direction for next
|
|
|
|
// contour to traverse, stay on the same contour.
|
|
|
|
if (nextSeg._visited && seg._path !== nextSeg._path
|
|
|
|
|| !operator(nextSeg._winding)) {
|
|
|
|
dir = 1;
|
|
|
|
} else {
|
|
|
|
// Switch to the intersection segment.
|
|
|
|
seg._visited = interSeg._visited;
|
|
|
|
seg = interSeg;
|
|
|
|
if (nextSeg._visited)
|
|
|
|
dir = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dir = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handleOut = dir > 0 ? seg._handleOut : seg._handleIn;
|
|
|
|
}
|
|
|
|
// Add the current segment to the path, and mark the added
|
|
|
|
// segment as visited.
|
|
|
|
path.add(new Segment(seg._point, added && handleIn, handleOut));
|
|
|
|
added = true;
|
|
|
|
seg._visited = true;
|
|
|
|
// Move to the next segment according to the traversal direction
|
|
|
|
seg = dir > 0 ? seg.getNext() : seg. getPrevious();
|
|
|
|
} while (seg && !seg._visited
|
|
|
|
&& seg !== startSeg && seg !== startInterSeg
|
|
|
|
&& (seg._intersection || operator(seg._winding)));
|
|
|
|
// Finish with closing the paths if necessary, correctly linking up
|
|
|
|
// curves etc.
|
|
|
|
if (seg && (seg === startSeg || seg === startInterSeg)) {
|
|
|
|
path.firstSegment.setHandleIn((seg === startInterSeg
|
|
|
|
? startInterSeg : seg)._handleIn);
|
|
|
|
path.setClosed(true);
|
|
|
|
} else {
|
|
|
|
path.lastSegment._handleOut.set(0, 0);
|
|
|
|
}
|
|
|
|
// Add the path to the result, while avoiding stray segments and
|
|
|
|
// incomplete paths. The amount of segments for valid paths depend
|
|
|
|
// on their geometry:
|
|
|
|
// - Closed paths with only straight lines (polygons) need more than
|
|
|
|
// two segments.
|
|
|
|
// - Closed paths with curves can consist of only one segment.
|
|
|
|
// - Open paths need at least two segments.
|
|
|
|
if (path._segments.length >
|
|
|
|
(path._closed ? path.isPolygon() ? 2 : 0 : 1))
|
|
|
|
paths.push(path);
|
|
|
|
}
|
|
|
|
return paths;
|
|
|
|
}
|
2014-02-20 14:24:16 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
return /** @lends PathItem# */{
|
|
|
|
/**
|
|
|
|
* Returns the winding contribution of the given point with respect to
|
|
|
|
* this PathItem.
|
|
|
|
*
|
|
|
|
* @param {Point} point the location for which to determine the winding
|
|
|
|
* direction
|
|
|
|
* @param {Boolean} horizontal whether we need to consider this point as
|
|
|
|
* part of a horizontal curve
|
|
|
|
* @param {Boolean} testContains whether we need to consider this point
|
|
|
|
* as part of stationary points on the curve itself, used when checking
|
2015-06-16 11:50:37 -04:00
|
|
|
* the winding about a point
|
2015-01-02 09:33:23 -05:00
|
|
|
* @return {Number} the winding number
|
|
|
|
*/
|
|
|
|
_getWinding: function(point, horizontal, testContains) {
|
|
|
|
return getWinding(point, this._getMonoCurves(),
|
|
|
|
horizontal, testContains);
|
|
|
|
},
|
2014-02-20 14:24:16 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
/**
|
|
|
|
* {@grouptitle Boolean Path Operations}
|
|
|
|
*
|
|
|
|
* Merges the geometry of the specified path from this path's
|
|
|
|
* geometry and returns the result as a new path item.
|
|
|
|
*
|
|
|
|
* @param {PathItem} path the path to unite with
|
|
|
|
* @return {PathItem} the resulting path item
|
|
|
|
*/
|
|
|
|
unite: function(path) {
|
2015-01-03 19:50:24 -05:00
|
|
|
return computeBoolean(this, path, 'unite');
|
2015-01-02 09:33:23 -05:00
|
|
|
},
|
2014-02-20 14:24:16 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
/**
|
|
|
|
* Intersects the geometry of the specified path with this path's
|
|
|
|
* geometry and returns the result as a new path item.
|
|
|
|
*
|
|
|
|
* @param {PathItem} path the path to intersect with
|
|
|
|
* @return {PathItem} the resulting path item
|
|
|
|
*/
|
|
|
|
intersect: function(path) {
|
2015-01-03 19:50:24 -05:00
|
|
|
return computeBoolean(this, path, 'intersect');
|
2015-01-02 09:33:23 -05:00
|
|
|
},
|
2014-02-20 14:24:16 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
/**
|
|
|
|
* Subtracts the geometry of the specified path from this path's
|
|
|
|
* geometry and returns the result as a new path item.
|
|
|
|
*
|
|
|
|
* @param {PathItem} path the path to subtract
|
|
|
|
* @return {PathItem} the resulting path item
|
|
|
|
*/
|
|
|
|
subtract: function(path) {
|
2015-01-03 19:50:24 -05:00
|
|
|
return computeBoolean(this, path, 'subtract');
|
2015-01-02 09:33:23 -05:00
|
|
|
},
|
2014-02-20 14:24:16 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
// Compound boolean operators combine the basic boolean operations such
|
|
|
|
// as union, intersection, subtract etc.
|
|
|
|
/**
|
|
|
|
* Excludes the intersection of the geometry of the specified path with
|
|
|
|
* this path's geometry and returns the result as a new group item.
|
|
|
|
*
|
|
|
|
* @param {PathItem} path the path to exclude the intersection of
|
|
|
|
* @return {Group} the resulting group item
|
|
|
|
*/
|
|
|
|
exclude: function(path) {
|
2015-01-03 19:50:24 -05:00
|
|
|
return computeBoolean(this, path, 'exclude');
|
2015-01-02 09:33:23 -05:00
|
|
|
},
|
2014-04-06 07:48:03 -04:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
/**
|
|
|
|
* Splits the geometry of this path along the geometry of the specified
|
|
|
|
* path returns the result as a new group item.
|
|
|
|
*
|
|
|
|
* @param {PathItem} path the path to divide by
|
|
|
|
* @return {Group} the resulting group item
|
|
|
|
*/
|
|
|
|
divide: function(path) {
|
|
|
|
return new Group([this.subtract(path), this.intersect(path)]);
|
|
|
|
}
|
|
|
|
};
|
2014-02-20 14:24:16 -05:00
|
|
|
});
|
2014-02-20 14:00:46 -05:00
|
|
|
|
|
|
|
Path.inject(/** @lends Path# */{
|
2015-01-02 09:33:23 -05:00
|
|
|
/**
|
|
|
|
* Private method that returns and caches all the curves in this Path, which
|
|
|
|
* are monotonically decreasing or increasing in the y-direction.
|
|
|
|
* Used by getWinding().
|
|
|
|
*/
|
|
|
|
_getMonoCurves: function() {
|
|
|
|
var monoCurves = this._monoCurves,
|
|
|
|
prevCurve;
|
2014-02-20 14:00:46 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
// Insert curve values into a cached array
|
|
|
|
function insertCurve(v) {
|
|
|
|
var y0 = v[1],
|
|
|
|
y1 = v[7],
|
|
|
|
curve = {
|
|
|
|
values: v,
|
|
|
|
winding: y0 === y1
|
|
|
|
? 0 // Horizontal
|
|
|
|
: y0 > y1
|
|
|
|
? -1 // Decreasing
|
|
|
|
: 1, // Increasing
|
|
|
|
// Add a reference to neighboring curves.
|
|
|
|
previous: prevCurve,
|
|
|
|
next: null // Always set it for hidden class optimization.
|
|
|
|
};
|
|
|
|
if (prevCurve)
|
|
|
|
prevCurve.next = curve;
|
|
|
|
monoCurves.push(curve);
|
|
|
|
prevCurve = curve;
|
|
|
|
}
|
2014-02-20 14:00:46 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
// Handle bezier curves. We need to chop them into smaller curves with
|
|
|
|
// defined orientation, by solving the derivative curve for y extrema.
|
|
|
|
function handleCurve(v) {
|
|
|
|
// Filter out curves of zero length.
|
|
|
|
// TODO: Do not filter this here.
|
|
|
|
if (Curve.getLength(v) === 0)
|
|
|
|
return;
|
|
|
|
var y0 = v[1],
|
|
|
|
y1 = v[3],
|
|
|
|
y2 = v[5],
|
|
|
|
y3 = v[7];
|
|
|
|
if (Curve.isLinear(v)) {
|
|
|
|
// Handling linear curves is easy.
|
|
|
|
insertCurve(v);
|
|
|
|
} else {
|
|
|
|
// Split the curve at y extrema, to get bezier curves with clear
|
|
|
|
// orientation: Calculate the derivative and find its roots.
|
|
|
|
var a = 3 * (y1 - y2) - y0 + y3,
|
|
|
|
b = 2 * (y0 + y2) - 4 * y1,
|
|
|
|
c = y1 - y0,
|
2015-01-02 15:19:18 -05:00
|
|
|
tolerance = /*#=*/Numerical.TOLERANCE,
|
2015-01-02 09:33:23 -05:00
|
|
|
roots = [];
|
|
|
|
// Keep then range to 0 .. 1 (excluding) in the search for y
|
|
|
|
// extrema.
|
2015-01-02 15:19:18 -05:00
|
|
|
var count = Numerical.solveQuadratic(a, b, c, roots, tolerance,
|
|
|
|
1 - tolerance);
|
2015-01-02 09:33:23 -05:00
|
|
|
if (count === 0) {
|
|
|
|
insertCurve(v);
|
|
|
|
} else {
|
|
|
|
roots.sort();
|
|
|
|
var t = roots[0],
|
|
|
|
parts = Curve.subdivide(v, t);
|
|
|
|
insertCurve(parts[0]);
|
|
|
|
if (count > 1) {
|
|
|
|
// If there are two extrema, renormalize t to the range
|
|
|
|
// of the second range and split again.
|
|
|
|
t = (roots[1] - t) / (1 - t);
|
|
|
|
// Since we already processed parts[0], we can override
|
|
|
|
// the parts array with the new pair now.
|
|
|
|
parts = Curve.subdivide(parts[1], t);
|
|
|
|
insertCurve(parts[0]);
|
|
|
|
}
|
|
|
|
insertCurve(parts[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-20 14:00:46 -05:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
if (!monoCurves) {
|
|
|
|
// Insert curves that are monotonic in y direction into cached array
|
|
|
|
monoCurves = this._monoCurves = [];
|
|
|
|
var curves = this.getCurves(),
|
|
|
|
segments = this._segments;
|
|
|
|
for (var i = 0, l = curves.length; i < l; i++)
|
|
|
|
handleCurve(curves[i].getValues());
|
|
|
|
// If the path is not closed, we need to join the end points with a
|
|
|
|
// straight line, just like how filling open paths works.
|
|
|
|
if (!this._closed && segments.length > 1) {
|
|
|
|
var p1 = segments[segments.length - 1]._point,
|
|
|
|
p2 = segments[0]._point,
|
|
|
|
p1x = p1._x, p1y = p1._y,
|
|
|
|
p2x = p2._x, p2y = p2._y;
|
|
|
|
handleCurve([p1x, p1y, p1x, p1y, p2x, p2y, p2x, p2y]);
|
|
|
|
}
|
|
|
|
if (monoCurves.length > 0) {
|
|
|
|
// Link first and last curves
|
|
|
|
var first = monoCurves[0],
|
|
|
|
last = monoCurves[monoCurves.length - 1];
|
|
|
|
first.previous = last;
|
|
|
|
last.next = first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return monoCurves;
|
|
|
|
},
|
2014-03-17 04:48:00 -04:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
/**
|
|
|
|
* Returns a point that is guaranteed to be inside the path.
|
|
|
|
*
|
|
|
|
* @type Point
|
|
|
|
* @bean
|
|
|
|
*/
|
|
|
|
getInteriorPoint: function() {
|
|
|
|
var bounds = this.getBounds(),
|
|
|
|
point = bounds.getCenter(true);
|
|
|
|
if (!this.contains(point)) {
|
|
|
|
// Since there is no guarantee that a poly-bezier path contains
|
|
|
|
// the center of its bounding rectangle, we shoot a ray in
|
|
|
|
// +x direction from the center and select a point between
|
|
|
|
// consecutive intersections of the ray
|
|
|
|
var curves = this._getMonoCurves(),
|
|
|
|
roots = [],
|
|
|
|
y = point.y,
|
|
|
|
xIntercepts = [];
|
|
|
|
for (var i = 0, l = curves.length; i < l; i++) {
|
|
|
|
var values = curves[i].values;
|
|
|
|
if ((curves[i].winding === 1
|
|
|
|
&& y >= values[1] && y <= values[7]
|
|
|
|
|| y >= values[7] && y <= values[1])
|
|
|
|
&& Curve.solveCubic(values, 1, y, roots, 0, 1) > 0) {
|
|
|
|
for (var j = roots.length - 1; j >= 0; j--)
|
|
|
|
xIntercepts.push(Curve.evaluate(values, roots[j], 0).x);
|
|
|
|
}
|
|
|
|
if (xIntercepts.length > 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
point.x = (xIntercepts[0] + xIntercepts[1]) / 2;
|
|
|
|
}
|
|
|
|
return point;
|
|
|
|
},
|
2014-03-17 05:04:09 -04:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
reorient: function() {
|
|
|
|
// Paths that are not part of compound paths should never be counter-
|
|
|
|
// clockwise for boolean operations.
|
|
|
|
this.setClockwise(true);
|
|
|
|
return this;
|
|
|
|
}
|
2014-02-20 14:00:46 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
CompoundPath.inject(/** @lends CompoundPath# */{
|
2015-01-02 09:33:23 -05:00
|
|
|
/**
|
|
|
|
* Private method that returns all the curves in this CompoundPath, which
|
|
|
|
* are monotonically decreasing or increasing in the 'y' direction.
|
|
|
|
* Used by getWinding().
|
|
|
|
*/
|
|
|
|
_getMonoCurves: function() {
|
|
|
|
var children = this._children,
|
|
|
|
monoCurves = [];
|
|
|
|
for (var i = 0, l = children.length; i < l; i++)
|
|
|
|
monoCurves.push.apply(monoCurves, children[i]._getMonoCurves());
|
|
|
|
return monoCurves;
|
|
|
|
},
|
2014-03-17 04:48:00 -04:00
|
|
|
|
2015-01-02 09:33:23 -05:00
|
|
|
/*
|
|
|
|
* Fixes the orientation of a CompoundPath's child paths by first ordering
|
|
|
|
* them according to their area, and then making sure that all children are
|
|
|
|
* of different winding direction than the first child, except for when
|
|
|
|
* some individual contours are disjoint, i.e. islands, they are reoriented
|
|
|
|
* so that:
|
|
|
|
* - The holes have opposite winding direction.
|
|
|
|
* - Islands have to have the same winding direction as the first child.
|
|
|
|
*/
|
|
|
|
// NOTE: Does NOT handle self-intersecting CompoundPaths.
|
|
|
|
reorient: function() {
|
|
|
|
var children = this.removeChildren().sort(function(a, b) {
|
|
|
|
return b.getBounds().getArea() - a.getBounds().getArea();
|
|
|
|
});
|
2015-01-02 18:46:24 -05:00
|
|
|
if (children.length > 0) {
|
|
|
|
this.addChildren(children);
|
|
|
|
var clockwise = children[0].isClockwise();
|
|
|
|
// Skip the first child
|
|
|
|
for (var i = 1, l = children.length; i < l; i++) {
|
|
|
|
var point = children[i].getInteriorPoint(),
|
|
|
|
counters = 0;
|
|
|
|
for (var j = i - 1; j >= 0; j--) {
|
|
|
|
if (children[j].contains(point))
|
|
|
|
counters++;
|
|
|
|
}
|
|
|
|
children[i].setClockwise(counters % 2 === 0 && clockwise);
|
2015-01-02 09:33:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2014-03-12 08:34:43 -04:00
|
|
|
});
|