2013-05-03 19:16:52 -04:00
|
|
|
/*
|
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* - paperjs Path and CompoundPath objects
|
|
|
|
* - 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
|
|
|
|
*/
|
|
|
|
|
2013-05-04 00:21:53 -04:00
|
|
|
PathItem.inject(new function() {
|
2013-05-03 19:16:52 -04:00
|
|
|
|
2013-05-04 00:21:53 -04:00
|
|
|
function splitPath(intersections, collectOthers) {
|
2013-05-04 00:05:44 -04:00
|
|
|
// Sort intersections by paths ids, curve index and parameter, so we
|
|
|
|
// can loop through all intersections, divide paths and never need to
|
|
|
|
// readjust indices.
|
|
|
|
intersections.sort(function(loc1, loc2) {
|
|
|
|
var path1 = loc1.getPath(),
|
|
|
|
path2 = loc2.getPath();
|
|
|
|
return path1 === path2
|
|
|
|
// We can add parameter (0 <= t <= 1) to index (a integer)
|
|
|
|
// to compare both at the same time
|
|
|
|
? (loc1.getIndex() + loc1.getParameter())
|
|
|
|
- (loc2.getIndex() + loc2.getParameter())
|
|
|
|
: path1._id - path2._id;
|
|
|
|
});
|
|
|
|
var others = collectOthers && [];
|
|
|
|
for (var i = intersections.length - 1; i >= 0; i--) {
|
|
|
|
var loc = intersections[i],
|
|
|
|
other = loc.getIntersection(),
|
|
|
|
curve = loc.divide(),
|
|
|
|
// When the curve doesn't need to be divided since t = 0, 1,
|
|
|
|
// #divide() returns null and we can use the existing segment.
|
|
|
|
segment = curve && curve.getSegment1() || loc.getSegment();
|
|
|
|
if (others)
|
|
|
|
others.push(other);
|
|
|
|
other.__segment = segment;
|
|
|
|
segment._ixPair = other;
|
2013-05-03 19:21:44 -04:00
|
|
|
}
|
2013-05-04 00:05:44 -04:00
|
|
|
return others;
|
2013-05-04 00:21:53 -04:00
|
|
|
}
|
2013-05-03 19:16:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* To deal with a HTML 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
|
2013-05-03 19:21:44 -04:00
|
|
|
* the holes have opposit winding direction (already handled by paperjs)
|
|
|
|
* islands has to have same winding direction (as the first child of the path)
|
2013-05-03 19:16:52 -04:00
|
|
|
*
|
|
|
|
* Does NOT handle selfIntersecting CompoundPaths.
|
|
|
|
*
|
|
|
|
* @param {CompoundPath} path - Input CompoundPath, Note: This path could be modified if need be.
|
2013-05-03 19:21:44 -04:00
|
|
|
* @return {boolean} the winding direction of the base contour(true if clockwise)
|
2013-05-03 19:16:52 -04:00
|
|
|
*/
|
2013-05-04 00:21:53 -04:00
|
|
|
function reorientCompoundPath(path) {
|
2013-05-03 19:21:44 -04:00
|
|
|
if (!(path instanceof CompoundPath)) {
|
|
|
|
path.closed = true;
|
|
|
|
return path.clockwise;
|
|
|
|
}
|
|
|
|
var children = path.children, len = children.length, baseWinding;
|
|
|
|
var bounds = new Array(len);
|
|
|
|
var tmparray = new Array(len);
|
|
|
|
baseWinding = children[0].clockwise;
|
|
|
|
// Omit the first path
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
children[i].closed = true;
|
|
|
|
bounds[i] = children[i].bounds;
|
|
|
|
tmparray[i] = 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
var p1 = children[i];
|
|
|
|
for (j = 0; j < len; j++) {
|
|
|
|
var p2 = children[j];
|
|
|
|
if (i !== j && bounds[i].contains(bounds[j])) {
|
|
|
|
tmparray[j]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 1; i < len; i++) {
|
|
|
|
if (tmparray[i] % 2 === 0) {
|
|
|
|
children[i].clockwise = baseWinding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return baseWinding;
|
2013-05-04 00:21:53 -04:00
|
|
|
}
|
2013-05-03 19:16:52 -04:00
|
|
|
|
2013-05-04 00:21:53 -04:00
|
|
|
function reversePath(path) {
|
2013-05-03 19:21:44 -04:00
|
|
|
var baseWinding;
|
|
|
|
if (path instanceof CompoundPath) {
|
|
|
|
var children = path.children, i, len;
|
|
|
|
for (i = 0, len = children.length; i < len; i++) {
|
|
|
|
children[i].reverse();
|
|
|
|
}
|
|
|
|
baseWinding = children[0].clockwise;
|
|
|
|
} else {
|
|
|
|
path.reverse();
|
|
|
|
baseWinding = path.clockwise;
|
|
|
|
}
|
|
|
|
return baseWinding;
|
2013-05-04 00:21:53 -04:00
|
|
|
}
|
2013-05-03 19:16:52 -04:00
|
|
|
|
2013-05-04 00:21:53 -04:00
|
|
|
function computeBoolean(path1, path2, operator, subtract, _cache) {
|
2013-05-03 19:21:44 -04:00
|
|
|
var _path1, _path2, path1Clockwise, path2Clockwise;
|
|
|
|
var ixs, path1Id, path2Id;
|
|
|
|
// 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.
|
|
|
|
_path1 = path1.clone();
|
|
|
|
_path2 = path2.clone();
|
|
|
|
_path1.style = _path2.style = null;
|
|
|
|
_path1.selected = _path2.selected = false;
|
2013-05-04 00:21:53 -04:00
|
|
|
path1Clockwise = reorientCompoundPath(_path1);
|
|
|
|
path2Clockwise = reorientCompoundPath(_path2);
|
2013-05-03 19:21:44 -04:00
|
|
|
path1Id = _path1.id;
|
|
|
|
path2Id = _path2.id;
|
|
|
|
// Calculate all the intersections
|
2013-05-04 00:16:36 -04:00
|
|
|
ixs = _cache && _cache.intersections || _path1.getIntersections(_path2);
|
|
|
|
// if we have a empty _cache object as an operand,
|
2013-05-03 19:21:44 -04:00
|
|
|
// skip calculating boolean and cache the intersections
|
2013-05-04 00:16:36 -04:00
|
|
|
if (_cache && !_cache.intersections)
|
|
|
|
return _cache.intersections = ixs;
|
2013-05-04 00:21:53 -04:00
|
|
|
splitPath(splitPath(ixs, true));
|
2013-05-03 19:21:44 -04:00
|
|
|
path1Id = _path1.id;
|
|
|
|
path2Id = _path2.id;
|
|
|
|
// Do operator specific calculations before we begin
|
2013-05-04 00:21:53 -04:00
|
|
|
if (subtract)
|
|
|
|
path2Clockwise = reversePath(_path2);
|
2013-05-03 19:16:52 -04:00
|
|
|
|
2013-05-03 19:21:44 -04:00
|
|
|
var i, j, len, path, crv;
|
|
|
|
var paths = [];
|
|
|
|
if (_path1 instanceof CompoundPath) {
|
|
|
|
paths = paths.concat(_path1.children);
|
|
|
|
} else {
|
|
|
|
paths = [ _path1 ];
|
|
|
|
}
|
|
|
|
if (_path2 instanceof CompoundPath) {
|
|
|
|
paths = paths.concat(_path2.children);
|
|
|
|
} else {
|
|
|
|
paths.push(_path2);
|
|
|
|
}
|
|
|
|
// step 1: discard invalid links according to the boolean operator
|
|
|
|
var lastNode, firstNode, nextNode, midPoint, insidePath1, insidePath2;
|
2013-05-04 00:16:36 -04:00
|
|
|
var thisId, thisWinding, contains;
|
2013-05-03 19:21:44 -04:00
|
|
|
for (i = 0, len = paths.length; i < len; i++) {
|
|
|
|
insidePath1 = insidePath2 = false;
|
|
|
|
path = paths[i];
|
|
|
|
thisId = (path.parent instanceof CompoundPath)? path.parent.id : path.id;
|
|
|
|
thisWinding = path.clockwise;
|
|
|
|
lastNode = path.lastSegment;
|
|
|
|
firstNode = path.firstSegment;
|
|
|
|
nextNode = null;
|
|
|
|
while (nextNode !== firstNode) {
|
|
|
|
nextNode = (nextNode)? nextNode.previous: lastNode;
|
|
|
|
crv = nextNode.curve;
|
|
|
|
midPoint = crv.getPoint(0.5);
|
|
|
|
if (thisId !== path1Id) {
|
|
|
|
contains = _path1.
|
|
|
|
contains(midPoint);
|
2013-05-04 00:21:53 -04:00
|
|
|
insidePath1 = thisWinding === path1Clockwise || subtract
|
2013-05-04 00:16:36 -04:00
|
|
|
? contains
|
2013-05-04 00:21:53 -04:00
|
|
|
: contains && !testOnCurve(_path1, midPoint);
|
2013-05-03 19:21:44 -04:00
|
|
|
}
|
|
|
|
if (thisId !== path2Id) {
|
|
|
|
contains = _path2.contains(midPoint);
|
2013-05-04 00:16:36 -04:00
|
|
|
insidePath2 = thisWinding === path2Clockwise
|
|
|
|
? contains
|
2013-05-04 00:21:53 -04:00
|
|
|
: contains && !testOnCurve(_path2, midPoint);
|
2013-05-03 19:21:44 -04:00
|
|
|
}
|
2013-05-03 19:52:37 -04:00
|
|
|
if (operator(thisId === path1Id, insidePath1, insidePath2)) {
|
2013-05-03 19:21:44 -04:00
|
|
|
crv._INVALID = true;
|
|
|
|
// markPoint(midPoint, '+');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-03 19:16:52 -04:00
|
|
|
|
2013-05-03 19:21:44 -04:00
|
|
|
// Final step: Retrieve the resulting paths from the graph
|
|
|
|
var boolResult = new CompoundPath();
|
|
|
|
var node, nuNode, nuPath, nodeList = [], handle;
|
|
|
|
for (i = 0, len = paths.length; i < len; i++) {
|
|
|
|
nodeList = nodeList.concat(paths[i].segments);
|
|
|
|
}
|
|
|
|
for (i = 0, len = nodeList.length; i < len; i++) {
|
|
|
|
node = nodeList[i];
|
|
|
|
if (node.curve._INVALID || node._visited) { continue; }
|
|
|
|
path = node.path;
|
|
|
|
thisId = (path.parent instanceof CompoundPath)? path.parent.id : path.id;
|
|
|
|
thisWinding = path.clockwise;
|
|
|
|
nuPath = new Path();
|
|
|
|
firstNode = null;
|
|
|
|
firstNode_ix = null;
|
|
|
|
if (node.previous.curve._INVALID) {
|
|
|
|
node.handleIn = (node._ixPair)?
|
2013-05-04 00:05:44 -04:00
|
|
|
node._ixPair.getIntersection().__segment.handleIn : [ 0, 0 ];
|
2013-05-03 19:21:44 -04:00
|
|
|
}
|
|
|
|
while (node && !node._visited && (node !== firstNode && node !== firstNode_ix)) {
|
|
|
|
node._visited = true;
|
|
|
|
firstNode = (firstNode)? firstNode: node;
|
|
|
|
firstNode_ix = (!firstNode_ix && firstNode._ixPair)?
|
2013-05-04 00:05:44 -04:00
|
|
|
firstNode._ixPair.getIntersection().__segment: firstNode_ix;
|
2013-05-03 19:21:44 -04:00
|
|
|
// node._ixPair is this node's intersection CurveLocation object
|
|
|
|
// node._ixPair.getIntersection() is the other CurveLocation object this node intersects with
|
2013-05-04 00:05:44 -04:00
|
|
|
nextNode = (node._ixPair && node.curve._INVALID)? node._ixPair.getIntersection().__segment : node;
|
2013-05-03 19:21:44 -04:00
|
|
|
if (node._ixPair) {
|
|
|
|
nextNode._visited = true;
|
|
|
|
nuNode = new Segment(node.point, node.handleIn, nextNode.handleOut);
|
|
|
|
nuPath.add(nuNode);
|
|
|
|
node = nextNode;
|
|
|
|
path = node.path;
|
|
|
|
thisWinding = path.clockwise;
|
|
|
|
} else {
|
|
|
|
nuPath.add(node);
|
|
|
|
}
|
|
|
|
node = node.next;
|
|
|
|
}
|
|
|
|
if (nuPath.segments.length > 1) {
|
|
|
|
// avoid stray segments and incomplete paths
|
|
|
|
if (nuPath.segments.length > 2 || !nuPath.curves[0].isLinear()) {
|
|
|
|
nuPath.closed = true;
|
|
|
|
boolResult.addChild(nuPath, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Delete the proxies
|
|
|
|
_path1.remove();
|
|
|
|
_path2.remove();
|
|
|
|
// And then, we are done.
|
|
|
|
return boolResult.reduce();
|
2013-05-04 00:21:53 -04:00
|
|
|
}
|
2013-05-03 19:16:52 -04:00
|
|
|
|
2013-05-04 00:21:53 -04:00
|
|
|
function testOnCurve(path, point) {
|
2013-05-04 00:24:02 -04:00
|
|
|
var curves = path.getCurves(),
|
|
|
|
bounds = path.getBounds();
|
|
|
|
if (bounds.contains(point)) {
|
|
|
|
for (var i = 0, l = curves.length; i < l; i++) {
|
|
|
|
var curve = curves[i];
|
|
|
|
if (curve.getBounds().contains(point)
|
|
|
|
&& curve.getParameterOf(point))
|
|
|
|
return true;
|
2013-05-03 19:21:44 -04:00
|
|
|
}
|
|
|
|
}
|
2013-05-04 00:24:02 -04:00
|
|
|
return false;
|
2013-05-03 19:16:52 -04:00
|
|
|
}
|
2013-05-04 00:21:53 -04:00
|
|
|
|
|
|
|
// A boolean operator is a binary operator function of the form
|
|
|
|
// function(isPath1, isInPath1, isInPath2)
|
|
|
|
//
|
|
|
|
// Operators return true if a curve in the operands is to be removed,
|
|
|
|
// and they aare called for each curve segment in the graph after all the
|
|
|
|
// intersections between the operands are calculated and curves in the
|
|
|
|
// operands were split at intersections.
|
|
|
|
//
|
|
|
|
// The boolean operator return a Boolean value indicating whether to
|
|
|
|
// keep the curve or not.
|
|
|
|
// return true - discard the curve
|
|
|
|
// return false - keep the curve
|
|
|
|
|
|
|
|
return {
|
|
|
|
unite: function(path, _cache) {
|
|
|
|
return computeBoolean(this, path,
|
|
|
|
function(isPath1, isInPath1, isInPath2) {
|
|
|
|
return isInPath1 || isInPath2;
|
|
|
|
}, false, _cache);
|
|
|
|
},
|
|
|
|
|
|
|
|
intersect: function(path, _cache) {
|
|
|
|
return computeBoolean(this, path,
|
|
|
|
function(isPath1, isInPath1, isInPath2) {
|
|
|
|
return !(isInPath1 || isInPath2);
|
|
|
|
}, false, _cache);
|
|
|
|
},
|
|
|
|
|
|
|
|
subtract: function(path, _cache) {
|
|
|
|
return computeBoolean(this, path,
|
|
|
|
function(isPath1, isInPath1, isInPath2) {
|
|
|
|
return isPath1 && isInPath2 || !isPath1 && !isInPath1;
|
|
|
|
}, true, _cache);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Compound boolean operators combine the basic boolean operations such
|
|
|
|
// as union, intersection, subtract etc.
|
|
|
|
// TODO: cache the split objects and find a way to properly clone them!
|
|
|
|
// a.k.a. eXclusiveOR
|
|
|
|
exclude: function(path) {
|
|
|
|
return new Group([this.subtract(path), path.subtract(this)]);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Divide path1 by path2
|
|
|
|
divide: function(path) {
|
|
|
|
return new Group([this.subtract(path), this.intersect(path)]);
|
|
|
|
}
|
|
|
|
};
|
2013-05-03 19:16:52 -04:00
|
|
|
});
|