More work on PathItem#__splitPath()

This commit is contained in:
Jürg Lehni 2013-05-03 18:52:03 -07:00
parent f054f4a7e0
commit b07e52d361

View file

@ -91,44 +91,47 @@ PathItem.inject({
function sortIx(a, b) { function sortIx(a, b) {
return b.parameter - a.parameter; return b.parameter - a.parameter;
} }
var paths = {}; var pathIds = {},
paths = [];
for (var i = 0, l = _ixs.length; i < l; i++) { for (var i = 0, l = _ixs.length; i < l; i++) {
var ix = other ? _ixs[i].getIntersection() : _ixs[i]; var ix = other ? _ixs[i].getIntersection() : _ixs[i],
if (!paths[ix.path.id]) path = ix.getPath(),
paths[ix.path.id] = ix.path; curve = ix.getCurve();
if (!ix.curve._ixParams) // Add each path only once to the array
ix.curve._ixParams = []; if (!pathIds[path._id]) {
ix.curve._ixParams.push({ paths.push(path);
parameter: ix.parameter, pathIds[path._id] = true;
}
if (!curve._ixParams)
curve._ixParams = [];
curve._ixParams.push({
parameter: ix.getParameter(),
pair: ix.getIntersection() pair: ix.getIntersection()
}); });
} }
for (var k in paths) { for (var i = 0, l = paths.length; i < l; i++) {
if (!paths.hasOwnProperty(k)) var path = paths[i],
continue; segments = path._segments;
var path = paths[k], for (var j = segments.length - 1; j >= 0; j--) {
lastNode = path.lastSegment, var nextNode = segments[j],
firstNode = path.firstSegment, curve = nextNode.getCurve(),
nextNode = null; ixs = curve._ixParams,
while (nextNode !== firstNode) { amount = ixs && ixs.length;
nextNode = (nextNode)? nextNode.previous: lastNode; if (amount > 0) {
if (nextNode.curve._ixParams) { ixs.sort(sortIx);
var ixs = nextNode.curve._ixParams, var isLinear = curve.isLinear(),
crv = nextNode.getCurve(),
isLinear = crv.isLinear(),
vals = null, vals = null,
segment; segment;
ixs.sort(sortIx); for (var k = 0; k < amount; k++) {
for (var i = 0, l = ixs.length; i < l; i++) { var ix = ixs[k],
var ix = ixs[i],
t = ix.parameter; t = ix.parameter;
if (!vals) if (!vals)
vals = crv.getValues(); vals = curve.getValues();
if (t === 0 || t === 1) { if (t === 0 || t === 1) {
// Intersection is on an existing node: No need to // Intersection is on an existing node: No need to
// create a new segment, we just link the // create a new segment, we just link the
// corresponding intersections together // corresponding intersections together
segment = t === 0 ? crv.segment1 : crv.segment2; segment = t === 0 ? curve.segment1 : curve.segment2;
} else { } else {
var parts = Curve.subdivide(vals, t), var parts = Curve.subdivide(vals, t),
left = parts[0], left = parts[0],
@ -136,10 +139,10 @@ PathItem.inject({
segment = new Segment( segment = new Segment(
new Point(right[0], right[1])); new Point(right[0], right[1]));
if (!isLinear) { if (!isLinear) {
crv.segment1.handleOut = new Point( curve.segment1.handleOut = new Point(
left[2] - left[0], left[2] - left[0],
left[3] - left[1]); left[3] - left[1]);
crv.segment2.handleIn = new Point( curve.segment2.handleIn = new Point(
right[4] - right[6], right[4] - right[6],
right[5] - right[7]); right[5] - right[7]);
segment.handleIn = new Point( segment.handleIn = new Point(
@ -149,15 +152,15 @@ PathItem.inject({
right[2] - left[6], right[2] - left[6],
right[3] - left[7]); right[3] - left[7]);
} }
path.insert(nextNode.index + 1, segment); path.insert(j + 1, segment);
crv = nextNode.getCurve(); curve = nextNode.getCurve();
vals = left; vals = left;
} }
segment._ixPair = ix.pair; segment._ixPair = ix.pair;
segment._ixPair._segment = segment; segment._ixPair._segment = segment;
// Readjust parameters after splitting // Readjust parameters after splitting
for (var j = i + 1; j < l; j++) for (var n = k + 1; n < amount; n++)
ixs[j].parameter /= t; ixs[n].parameter /= t;
} }
} }
} }