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-19 10:09:49 -05:00
|
|
|
PathItem.inject(new function() {
|
2014-01-05 09:59:21 -05:00
|
|
|
/**
|
|
|
|
* To deal with a HTML5 canvas requirement where CompoundPaths' child
|
|
|
|
* contours has to be of different winding direction for correctly filling
|
|
|
|
* holes. But if some individual countours are disjoint, i.e. islands, we
|
|
|
|
* have to reorient them so that:
|
|
|
|
* - the holes have opposit winding direction (already handled by paper.js)
|
|
|
|
* - islands have to have the same winding direction as the first child
|
|
|
|
*
|
|
|
|
* NOTE: Does NOT handle self-intersecting CompoundPaths.
|
|
|
|
*/
|
|
|
|
function reorientPath(path) {
|
2013-05-04 02:03:00 -04:00
|
|
|
if (path instanceof CompoundPath) {
|
2013-11-24 07:32:01 -05:00
|
|
|
var children = path.removeChildren(),
|
2014-01-05 09:59:21 -05:00
|
|
|
length = children.length,
|
|
|
|
bounds = new Array(length),
|
|
|
|
counters = new Array(length),
|
|
|
|
clockwise;
|
2013-12-03 15:54:36 -05:00
|
|
|
children.sort(function(a, b) {
|
|
|
|
return b.getBounds().getArea() - a.getBounds().getArea();
|
2013-11-24 07:32:01 -05:00
|
|
|
});
|
|
|
|
path.addChildren(children);
|
|
|
|
clockwise = children[0].isClockwise();
|
2013-05-04 02:03:00 -04:00
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
bounds[i] = children[i].getBounds();
|
|
|
|
counters[i] = 0;
|
2013-05-03 19:21:44 -04:00
|
|
|
}
|
2013-05-04 02:03:00 -04:00
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
for (var j = 1; j < length; j++) {
|
2013-12-29 07:36:43 -05:00
|
|
|
if (i !== j && bounds[i].intersects(bounds[j]))
|
2013-05-04 02:03:00 -04:00
|
|
|
counters[j]++;
|
|
|
|
}
|
2013-05-04 02:25:26 -04:00
|
|
|
// Omit the first child
|
|
|
|
if (i > 0 && counters[i] % 2 === 0)
|
2013-05-04 02:03:00 -04:00
|
|
|
children[i].setClockwise(clockwise);
|
2013-05-03 19:21:44 -04:00
|
|
|
}
|
|
|
|
}
|
2013-05-04 02:03:00 -04:00
|
|
|
return path;
|
2014-01-05 09:59:21 -05:00
|
|
|
}
|
2013-05-03 19:16:52 -04:00
|
|
|
|
2014-01-27 15:32:25 -05:00
|
|
|
function computeBoolean(path1, path2, operator, subtract) {
|
2013-05-03 19:21:44 -04:00
|
|
|
// We do not modify the operands themselves
|
|
|
|
// The result might not belong to the same type
|
|
|
|
// i.e. subtraction(A:Path, B:Path):CompoundPath etc.
|
2013-12-08 15:39:56 -05:00
|
|
|
// Also apply matrices to both paths in case they were transformed.
|
2014-02-16 13:02:07 -05:00
|
|
|
var singlePathOp = path1 === path2;
|
2013-12-08 15:39:56 -05:00
|
|
|
path1 = reorientPath(path1.clone(false).applyMatrix());
|
2014-02-16 13:02:07 -05:00
|
|
|
if (!singlePathOp)
|
|
|
|
path2 = reorientPath(path2.clone(false).applyMatrix());
|
2013-05-03 19:21:44 -04:00
|
|
|
// Do operator specific calculations before we begin
|
2013-12-29 07:38:04 -05:00
|
|
|
// Make both paths at clockwise orientation, except when @subtract = true
|
|
|
|
// We need both paths at opposit orientation for subtraction
|
|
|
|
if (!path1.isClockwise())
|
2013-09-22 21:18:22 -04:00
|
|
|
path1.reverse();
|
2014-02-16 13:02:07 -05:00
|
|
|
if (!singlePathOp && !(subtract ^ path2.isClockwise()))
|
2013-09-22 21:18:22 -04:00
|
|
|
path2.reverse();
|
2014-02-19 10:09:49 -05:00
|
|
|
var i, j, l, lj, segment, wind,
|
2014-02-17 14:59:38 -05:00
|
|
|
point, startSeg, crv, length, parent, v, horizontal,
|
|
|
|
curveChain = [],
|
|
|
|
windings = [],
|
|
|
|
lengths = [],
|
|
|
|
windMedian, lenCurves,
|
|
|
|
paths = [],
|
|
|
|
segments = [],
|
|
|
|
// Aggregate of all curves in both operands, monotonic in y
|
|
|
|
monoCurves = [],
|
|
|
|
result = new CompoundPath(),
|
|
|
|
tolerance = /*#=*/ Numerical.TOLERANCE,
|
|
|
|
intersections = singlePathOp ? path1.getSelfIntersections(true)
|
|
|
|
: path1.getIntersections(path2, true);
|
2014-02-19 10:09:49 -05:00
|
|
|
// Split curves at intersections on both paths.
|
|
|
|
PathItem._splitPath(intersections);
|
2013-12-29 07:38:04 -05:00
|
|
|
// Collect all sub paths and segments
|
|
|
|
paths.push.apply(paths, path1._children || [path1]);
|
2014-02-16 13:02:07 -05:00
|
|
|
if (!singlePathOp)
|
|
|
|
paths.push.apply(paths, path2._children || [path2]);
|
2014-01-25 23:39:51 -05:00
|
|
|
|
2014-02-17 14:59:38 -05:00
|
|
|
for (i = 0, l = paths.length; i < l; i++) {
|
2013-12-29 07:38:04 -05:00
|
|
|
segments.push.apply(segments, paths[i].getSegments());
|
|
|
|
monoCurves.push.apply(monoCurves, paths[i]._getMonotoneCurves());
|
2013-05-03 19:21:44 -04:00
|
|
|
}
|
2013-12-29 07:38:04 -05:00
|
|
|
// Propagate the winding contribution. Winding contribution of curves
|
|
|
|
// does not change between two intersections.
|
|
|
|
// First, sort all segments with an intersection to the begining.
|
|
|
|
segments.sort(function(a, b) {
|
|
|
|
var ixa = a._intersection,
|
2014-02-17 14:59:38 -05:00
|
|
|
ixb = b._intersection;
|
|
|
|
if (!ixa && !ixb || ixa && ixb)
|
2013-12-29 07:38:04 -05:00
|
|
|
return 0;
|
|
|
|
return ixa ? -1 : 1;
|
|
|
|
});
|
|
|
|
for (i = 0, l = segments.length; i < l; i++) {
|
|
|
|
segment = segments[i];
|
2014-02-17 14:59:38 -05:00
|
|
|
if (segment._winding != null)
|
2013-05-04 02:03:00 -04:00
|
|
|
continue;
|
2013-12-29 07:38:04 -05:00
|
|
|
// 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.
|
2014-01-05 08:26:09 -05:00
|
|
|
curveChain.length = lengths.length = 0;
|
|
|
|
lenCurves = 0;
|
2013-12-29 07:38:04 -05:00
|
|
|
startSeg = segment;
|
2013-05-04 05:50:18 -04:00
|
|
|
do {
|
2013-12-29 07:38:04 -05:00
|
|
|
curveChain.push(segment);
|
2014-01-05 08:26:09 -05:00
|
|
|
lenCurves += segment.getCurve().getLength();
|
|
|
|
lengths.push(lenCurves);
|
2013-12-29 07:38:04 -05:00
|
|
|
// Continue with next curve
|
2013-05-04 06:00:31 -04:00
|
|
|
segment = segment.getNext();
|
2014-02-17 14:59:38 -05:00
|
|
|
} while (segment && !segment._intersection && segment !== startSeg);
|
2014-01-05 08:26:09 -05:00
|
|
|
|
2014-01-25 23:39:51 -05:00
|
|
|
|
2014-01-05 08:26:09 -05:00
|
|
|
// Select the median winding of three random points along this
|
2014-01-05 09:59:21 -05:00
|
|
|
// curve chain, as a representative winding number. The
|
|
|
|
// random selection gives a better chance of returning a
|
|
|
|
// correct winding than equally dividing the curve chain, with
|
|
|
|
// the same (amortised) time.
|
2014-01-05 08:26:09 -05:00
|
|
|
windings.length = 0;
|
|
|
|
for (wind = 0; wind < 3; wind++) {
|
2014-02-19 10:09:49 -05:00
|
|
|
length = lenCurves * Math.random();
|
2014-01-05 08:26:09 -05:00
|
|
|
for (j = 0, lj = lengths.length ; j <= lj; j++)
|
2014-01-25 23:39:51 -05:00
|
|
|
if (lengths[j] >= length) {
|
2014-01-05 08:26:09 -05:00
|
|
|
length = j > 0 ? length - lengths[j-1] : length;
|
|
|
|
break;
|
2013-12-29 07:38:04 -05:00
|
|
|
}
|
2014-01-05 08:26:09 -05:00
|
|
|
crv = curveChain[j].getCurve();
|
|
|
|
point = crv.getPointAt(length);
|
2014-01-16 13:59:18 -05:00
|
|
|
v = crv.getValues();
|
2014-02-19 10:09:49 -05:00
|
|
|
horizontal = Curve.isLinear(v) && Math.abs(v[1] - v[7]) < tolerance;
|
|
|
|
windMedian = PathItem._getWinding(point, monoCurves, horizontal);
|
2014-01-05 08:26:09 -05:00
|
|
|
// While subtracting, we need to omit this curve if this
|
|
|
|
// curve is contributing to the second operand and is outside
|
|
|
|
// the first operand.
|
|
|
|
parent = crv._path;
|
|
|
|
if (parent._parent instanceof CompoundPath)
|
|
|
|
parent = parent._parent;
|
|
|
|
if (subtract && (parent._id === path2._id &&
|
2014-01-25 23:39:51 -05:00
|
|
|
!path1._getWinding(point, horizontal) ||
|
2014-01-05 09:59:21 -05:00
|
|
|
(parent._id === path1._id &&
|
2014-01-25 23:39:51 -05:00
|
|
|
path2._getWinding(point, horizontal)))) {
|
2014-01-05 08:26:09 -05:00
|
|
|
windMedian = 0;
|
2013-12-29 07:38:04 -05:00
|
|
|
}
|
2014-01-05 08:26:09 -05:00
|
|
|
windings[wind] = windMedian;
|
2013-05-03 19:21:44 -04:00
|
|
|
}
|
2014-01-05 08:26:09 -05:00
|
|
|
windings.sort();
|
|
|
|
windMedian = windings[1];
|
2013-12-29 07:38:04 -05:00
|
|
|
// Assign the winding to the entire curve chain
|
|
|
|
for (j = curveChain.length - 1; j >= 0; j--)
|
2014-01-05 08:26:09 -05:00
|
|
|
curveChain[j]._winding = windMedian;
|
2013-05-03 19:21:44 -04:00
|
|
|
}
|
2013-12-29 07:38:04 -05:00
|
|
|
// Trace closed contours and insert them into the result;
|
|
|
|
paths = PathItem._tracePaths(segments, operator);
|
|
|
|
for (i = 0, l = paths.length; i < l; i++)
|
|
|
|
result.addChild(paths[i], true);
|
2013-05-03 19:21:44 -04:00
|
|
|
// Delete the proxies
|
2013-05-04 06:38:19 -04:00
|
|
|
path1.remove();
|
2014-02-16 13:02:07 -05:00
|
|
|
if (!singlePathOp)
|
|
|
|
path2.remove();
|
2013-05-03 19:21:44 -04:00
|
|
|
// And then, we are done.
|
2013-05-04 01:38:29 -04:00
|
|
|
return result.reduce();
|
2014-01-05 09:59:21 -05:00
|
|
|
}
|
2013-05-03 19:16:52 -04:00
|
|
|
|
2014-01-05 09:59:21 -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.
|
|
|
|
return /** @lends Path# */{
|
2013-05-04 13:58:50 -04:00
|
|
|
/**
|
2013-12-28 14:56:30 -05:00
|
|
|
* {@grouptitle Boolean Path Operations}
|
|
|
|
*
|
2013-05-04 13:58:50 -04:00
|
|
|
* Merges the geometry of the specified path from this path's
|
|
|
|
* geometry and returns the result as a new path item.
|
2013-09-21 09:26:14 -04:00
|
|
|
*
|
2013-05-04 13:58:50 -04:00
|
|
|
* @param {PathItem} path the path to unite with
|
|
|
|
* @return {PathItem} the resulting path item
|
|
|
|
*/
|
2014-01-25 23:44:55 -05:00
|
|
|
unite: function(path) {
|
2013-05-04 00:21:53 -04:00
|
|
|
return computeBoolean(this, path,
|
2014-01-27 15:32:25 -05:00
|
|
|
function(w) { return w === 1 || w === 0; }, false);
|
2013-05-04 00:21:53 -04:00
|
|
|
},
|
|
|
|
|
2013-05-04 13:58:50 -04:00
|
|
|
/**
|
|
|
|
* Intersects the geometry of the specified path with this path's
|
|
|
|
* geometry and returns the result as a new path item.
|
2013-09-21 09:26:14 -04:00
|
|
|
*
|
2013-05-04 13:58:50 -04:00
|
|
|
* @param {PathItem} path the path to intersect with
|
|
|
|
* @return {PathItem} the resulting path item
|
|
|
|
*/
|
2014-01-25 23:44:55 -05:00
|
|
|
intersect: function(path) {
|
2013-05-04 00:21:53 -04:00
|
|
|
return computeBoolean(this, path,
|
2014-01-27 15:32:25 -05:00
|
|
|
function(w) { return w === 2; }, false);
|
2013-05-04 00:21:53 -04:00
|
|
|
},
|
|
|
|
|
2013-05-04 13:58:50 -04:00
|
|
|
/**
|
|
|
|
* Subtracts the geometry of the specified path from this path's
|
|
|
|
* geometry and returns the result as a new path item.
|
2013-09-21 09:26:14 -04:00
|
|
|
*
|
2013-05-04 13:58:50 -04:00
|
|
|
* @param {PathItem} path the path to subtract
|
|
|
|
* @return {PathItem} the resulting path item
|
|
|
|
*/
|
2014-01-25 23:44:55 -05:00
|
|
|
subtract: function(path) {
|
2013-05-04 00:21:53 -04:00
|
|
|
return computeBoolean(this, path,
|
2014-01-27 15:32:25 -05:00
|
|
|
function(w) { return w === 1; }, true);
|
2013-05-04 00:21:53 -04:00
|
|
|
},
|
|
|
|
|
2014-01-27 15:32:25 -05:00
|
|
|
// Compound boolean operators combine the basic boolean operations such
|
|
|
|
// as union, intersection, subtract etc.
|
2013-05-04 13:58:50 -04:00
|
|
|
/**
|
|
|
|
* Excludes the intersection of the geometry of the specified path with
|
|
|
|
* this path's geometry and returns the result as a new group item.
|
2013-09-21 09:26:14 -04:00
|
|
|
*
|
2013-05-04 13:58:50 -04:00
|
|
|
* @param {PathItem} path the path to exclude the intersection of
|
|
|
|
* @return {Group} the resulting group item
|
|
|
|
*/
|
2013-05-04 00:21:53 -04:00
|
|
|
exclude: function(path) {
|
2014-01-27 15:32:25 -05:00
|
|
|
return new Group([this.subtract(path), path.subtract(this)]);
|
2013-05-04 00:21:53 -04:00
|
|
|
},
|
2014-01-27 15:32:25 -05:00
|
|
|
|
2013-05-04 13:58:50 -04:00
|
|
|
/**
|
|
|
|
* Splits the geometry of this path along the geometry of the specified
|
|
|
|
* path returns the result as a new group item.
|
2013-09-21 09:26:14 -04:00
|
|
|
*
|
2013-05-04 13:58:50 -04:00
|
|
|
* @param {PathItem} path the path to divide by
|
|
|
|
* @return {Group} the resulting group item
|
|
|
|
*/
|
2013-05-04 00:21:53 -04:00
|
|
|
divide: function(path) {
|
|
|
|
return new Group([this.subtract(path), this.intersect(path)]);
|
|
|
|
}
|
2014-01-05 09:59:21 -05:00
|
|
|
};
|
2013-05-03 19:16:52 -04:00
|
|
|
});
|