diff --git a/bower.json b/bower.json index f051d996..ae64abd8 100644 --- a/bower.json +++ b/bower.json @@ -6,7 +6,6 @@ "build", "components", "dist/paper-node.js", - "fatline", "projects", "node_modules", "package.json", diff --git a/fatline/COPYING b/fatline/COPYING deleted file mode 100644 index dccb91dc..00000000 --- a/fatline/COPYING +++ /dev/null @@ -1,9 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2013 Harikrishnan Gopalakrishnan - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/fatline/Intersect.js b/fatline/Intersect.js deleted file mode 100644 index 6706df75..00000000 --- a/fatline/Intersect.js +++ /dev/null @@ -1,424 +0,0 @@ -new function() { - -var MAX_RECURSION = 20; -var MAX_ITERATION = 20; - -/** - * This method is analogous to paperjs#PathItem.getIntersections, but calls - * Curve.getIntersections2 instead. - */ - PathItem.prototype.getIntersections2 = function(path) { - // First check the bounds of the two paths. If they don't intersect, - // we don't need to iterate through their curves. - if (!this.getBounds().touches(path.getBounds())) - return []; - var locations = [], - curves1 = this.getCurves(), - curves2 = path.getCurves(), - length2 = curves2.length, - values2 = []; - for (var i = 0; i < length2; i++) - values2[i] = curves2[i].getValues(); - for (var i = 0, l = curves1.length; i < l; i++) { - var curve1 = curves1[i], - values1 = curve1.getValues(); - for (var j = 0; j < length2; j++) - Curve.getIntersections2(values1, values2[j], curve1, curves2[j], - locations); - } - return locations; -}; - -/** - * This method is analogous to paperjs#Curve.getIntersections - */ -Curve.getIntersections2 = function(v1, v2, curve1, curve2, locations) { - var linear1 = Curve.isLinear(v1), - linear2 = Curve.isLinear(v2); - // Determine the correct intersection method based on values of linear1 & 2: - (linear1 && linear2 - ? getLineLineIntersection - : linear1 || linear2 - ? getCurveLineIntersections - : getCurveIntersections)(v1, v2, curve1, curve2, locations); - return locations; -}; - -function addLocation(locations, curve1, parameter, point, curve2) { - // Avoid duplicates when hitting segments (closed paths too) - var first = locations[0], - last = locations[locations.length - 1]; - if ((!first || !point.equals(first._point)) - && (!last || !point.equals(last._point))) - locations.push(new CurveLocation(curve1, parameter, point, curve2)); -} - -function getCurveIntersections(v1, v2, curve1, curve2, locations, - range1, range2, recursion) { - // NOTE: range1 and range1 are only used for recusion - recursion = (recursion || 0) + 1; - // Avoid endless recursion. - // Perhaps we should fall back to a more expensive method after this, but - // so far endless recursion happens only when there is no real intersection - // and the infinite fatline continue to intersect with the other curve - // outside its bounds! - if (recursion > MAX_RECURSION) - return; - // Set up the parameter ranges. - range1 = range1 || [ 0, 1 ]; - range2 = range2 || [ 0, 1 ]; - // Get the clipped parts from the original curve, to avoid cumulative errors - var part1 = Curve.getPart(v1, range1[0], range1[1]), - part2 = Curve.getPart(v2, range2[0], range2[1]), - iteration = 0; - // markCurve(part1, '#f0f', true); - // markCurve(part2, '#0ff', false); - // Loop until both parameter range converge. We have to handle the - // degenerate case seperately, where fat-line clipping can become - // numerically unstable when one of the curves has converged to a point and - // the other hasn't. - while (iteration++ < MAX_ITERATION - && (Math.abs(range1[1] - range1[0]) > /*#=*/ Numerical.TOLERANCE - || Math.abs(range2[1] - range2[0]) > /*#=*/ Numerical.TOLERANCE)) { - // First we clip v2 with v1's fat-line - var range, - intersects1 = clipFatLine(part1, part2, range = range2.slice()), - intersects2 = 0; - // Stop if there are no possible intersections - if (intersects1 === 0) - break; - if (intersects1 > 0) { - // Get the clipped parts from the original v2, to avoid cumulative - // errors ...and reuse some objects. - range2 = range; - part2 = Curve.getPart(v2, range2[0], range2[1]); - // markCurve(part2, '#0ff', false); - // Next we clip v1 with nuv2's fat-line - intersects2 = clipFatLine(part2, part1, range = range1.slice()); - // Stop if there are no possible intersections - if (intersects2 === 0) - break; - if (intersects1 > 0) { - // Get the clipped parts from the original v2, to avoid - // cumulative errors - range1 = range; - part1 = Curve.getPart(v1, range1[0], range1[1]); - } - // markCurve(part1, '#f0f', true); - } - // Get the clipped parts from the original v1 - // Check if there could be multiple intersections - if (intersects1 < 0 || intersects2 < 0) { - // Subdivide the curve which has converged the least from the - // original range [0,1], which would be the curve with the largest - // parameter range after clipping - if (range1[1] - range1[0] > range2[1] - range2[0]) { - // subdivide v1 and recurse - var t = (range1[0] + range1[1]) / 2; - getCurveIntersections(v1, v2, curve1, curve2, locations, - [ range1[0], t ], range2, recursion); - getCurveIntersections(v1, v2, curve1, curve2, locations, - [ t, range1[1] ], range2, recursion); - break; - } else { - // subdivide v2 and recurse - var t = (range2[0] + range2[1]) / 2; - getCurveIntersections(v1, v2, curve1, curve2, locations, range1, - [ range2[0], t ], recursion); - getCurveIntersections(v1, v2, curve1, curve2, locations, range1, - [ t, range2[1] ], recursion); - break; - } - } - // We need to bailout of clipping and try a numerically stable method if - // any of the following are true. - // 1. One of the parameter ranges is converged to a point. - // 2. Both of the parameter ranges have converged reasonably well - // (according to Numerical.TOLERANCE). - // 3. One of the parameter range is converged enough so that it is - // *flat enough* to calculate line curve intersection implicitly. - // - // Check if one of the parameter range has converged completely to a - // point. Now things could get only worse if we iterate more for the - // other curve to converge if it hasn't yet happened so. - var converged1 = Math.abs(range1[1] - range1[0]) < /*#=*/ Numerical.TOLERANCE, - converged2 = Math.abs(range2[1] - range2[0]) < /*#=*/ Numerical.TOLERANCE; - if (converged1 || converged2) { - addLocation(locations, curve1, null, converged1 - ? curve1.getPointAt(range1[0], true) - : curve2.getPointAt(range2[0], true), curve2); - break; - } - // see if either or both of the curves are flat enough to be treated - // as lines. - var flat1 = Curve.isFlatEnough(part1, /*#=*/ Numerical.TOLERANCE), - flat2 = Curve.isFlatEnough(part2, /*#=*/ Numerical.TOLERANCE); - if (flat1 || flat2) { - (flat1 && flat2 - ? getLineLineIntersection - // Use curve line intersection method while specifying - // which curve to be treated as line - : getCurveLineIntersections)(part1, part2, - curve1, curve2, locations, flat1); - break; - } - } -} - -/** - * Clip curve V2 with fat-line of v1 - * @param {Array} v1 section of the first curve, for which we will make a - * fat-line - * @param {Array} v2 section of the second curve; we will clip this curve with - * the fat-line of v1 - * @param {Array} range2 the parameter range of v2 - * @return {Number} 0: no Intersection, 1: one intersection, -1: more than one - * ntersection - */ -function clipFatLine(v1, v2, range2) { - // P = first curve, Q = second curve - var p0x = v1[0], p0y = v1[1], p1x = v1[2], p1y = v1[3], - p2x = v1[4], p2y = v1[5], p3x = v1[6], p3y = v1[7], - q0x = v2[0], q0y = v2[1], q1x = v2[2], q1y = v2[3], - q2x = v2[4], q2y = v2[5], q3x = v2[6], q3y = v2[7], - // Calculate the fat-line L for P is the baseline l and two - // offsets which completely encloses the curve P. - d1 = getSignedDistance(p0x, p0y, p3x, p3y, p1x, p1y) || 0, - d2 = getSignedDistance(p0x, p0y, p3x, p3y, p2x, p2y) || 0, - factor = d1 * d2 > 0 ? 3 / 4 : 4 / 9, - dmin = factor * Math.min(0, d1, d2), - dmax = factor * Math.max(0, d1, d2), - // Calculate non-parametric bezier curve D(ti, di(t)) - di(t) is the - // distance of Q from the baseline l of the fat-line, ti is equally - // spaced in [0, 1] - dq0 = getSignedDistance(p0x, p0y, p3x, p3y, q0x, q0y), - dq1 = getSignedDistance(p0x, p0y, p3x, p3y, q1x, q1y), - dq2 = getSignedDistance(p0x, p0y, p3x, p3y, q2x, q2y), - dq3 = getSignedDistance(p0x, p0y, p3x, p3y, q3x, q3y), - // Find the minimum and maximum distances from l, this is useful for - // checking whether the curves intersect with each other or not. - mindist = Math.min(dq0, dq1, dq2, dq3), - maxdist = Math.max(dq0, dq1, dq2, dq3); - // If the fatlines don't overlap, we have no intersections! - if (dmin > maxdist || dmax < mindist) - return 0; - var Dt = getConvexHull(dq0, dq1, dq2, dq3), - tmp; - if (dq3 < dq0) { - tmp = dmin; - dmin = dmax; - dmax = tmp; - } - // Calculate the convex hull for non-parametric bezier curve D(ti, di(t)) - // Now we clip the convex hulls for D(ti, di(t)) with dmin and dmax - // for the coorresponding t values (tmin, tmax): Portions of curve v2 before - // tmin and after tmax can safely be clipped away - var tmaxdmin = -Infinity, - tmin = Infinity, - tmax = -Infinity; - for (var i = 0, l = Dt.length; i < l; i++) { - var Dtl = Dt[i], - dtlx1 = Dtl[0], - dtly1 = Dtl[1], - dtlx2 = Dtl[2], - dtly2 = Dtl[3]; - if (dtly2 < dtly1) { - tmp = dtly2; - dtly2 = dtly1; - dtly1 = tmp; - tmp = dtlx2; - dtlx2 = dtlx1; - dtlx1 = tmp; - } - // We know that (dtlx2 - dtlx1) is never 0 - var inv = (dtly2 - dtly1) / (dtlx2 - dtlx1); - if (dmin >= dtly1 && dmin <= dtly2) { - var ixdx = dtlx1 + (dmin - dtly1) / inv; - if (ixdx < tmin) - tmin = ixdx; - if (ixdx > tmaxdmin) - tmaxdmin = ixdx; - } - if (dmax >= dtly1 && dmax <= dtly2) { - var ixdx = dtlx1 + (dmax - dtly1) / inv; - if (ixdx > tmax) - tmax = ixdx; - if (ixdx < tmin) - tmin = 0; - } - } - // Return the parameter values for v2 for which we can be sure that the - // intersection with v1 lies within. - if (tmin !== Infinity && tmax !== -Infinity) { - var mindmin = Math.min(dmin, dmax), - mindmax = Math.max(dmin, dmax); - if (dq3 > mindmin && dq3 < mindmax) - tmax = 1; - if (dq0 > mindmin && dq0 < mindmax) - tmin = 0; - if (tmaxdmin > tmax) - tmax = 1; - // tmin and tmax are within the range (0, 1). We need to project it to - // the original parameter range for v2. - var v2tmin = range2[0], - tdiff = range2[1] - v2tmin; - range2[0] = v2tmin + tmin * tdiff; - range2[1] = v2tmin + tmax * tdiff; - // If the new parameter range fails to converge by atleast 20% of the - // original range, possibly we have multiple intersections. We need to - // subdivide one of the curves. - if ((tdiff - (range2[1] - range2[0])) / tdiff >= 0.2) - return 1; - } - // TODO: Try checking with a perpendicular fatline to see if the curves - // overlap if it is any faster than this - if (Curve.getBounds(v1).touches(Curve.getBounds(v2))) - return -1; - return 0; -} - -/** - * Calculate the convex hull for the non-paramertic bezier curve D(ti, di(t)). - * The ti is equally spaced across [0..1] — [0, 1/3, 2/3, 1] for - * di(t), [dq0, dq1, dq2, dq3] respectively. In other words our CVs for the - * curve are already sorted in the X axis in the increasing order. Calculating - * convex-hull is much easier than a set of arbitrary points. - */ -function getConvexHull(dq0, dq1, dq2, dq3) { - var distq1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1), - distq2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2); - // Check if [1/3, dq1] and [2/3, dq2] are on the same side of line - // [0,dq0, 1,dq3] - if (distq1 * distq2 < 0) { - // dq1 and dq2 lie on different sides on [0, q0, 1, q3]. The hull is a - // quadrilateral and line [0, q0, 1, q3] is NOT part of the hull so we - // are pretty much done here. - return [ - [ 0, dq0, 1 / 3, dq1 ], - [ 1 / 3, dq1, 1, dq3 ], - [ 2 / 3, dq2, 0, dq0 ], - [ 1, dq3, 2 / 3, dq2 ] - ]; - } - // dq1 and dq2 lie on the same sides on [0, q0, 1, q3]. The hull can be - // a triangle or a quadrilateral and line [0, q0, 1, q3] is part of the - // hull. Check if the hull is a triangle or a quadrilateral. - var dqMaxX, dqMaxY, vqa1a2X, vqa1a2Y, vqa1MaxX, vqa1MaxY, vqa1MinX, vqa1MinY; - if (Math.abs(distq1) > Math.abs(distq2)) { - dqMaxX = 1 / 3; - dqMaxY = dq1; - // apex is dq3 and the other apex point is dq0 vector - // dqapex->dqapex2 or base vector which is already part of the hull. - vqa1a2X = 1; - vqa1a2Y = dq3 - dq0; - // vector dqapex->dqMax - vqa1MaxX = 2 / 3; - vqa1MaxY = dq3 - dq1; - // vector dqapex->dqmin - vqa1MinX = 1 / 3; - vqa1MinY = dq3 - dq2; - } else { - dqMaxX = 2 / 3; - dqMaxY = dq2; - // apex is dq0 in this case, and the other apex point is dq3 vector - // dqapex->dqapex2 or base vector which is already part of the hull. - vqa1a2X = -1; - vqa1a2Y = dq0 - dq3; - // vector dqapex->dqMax - vqa1MaxX = -2 / 3; - vqa1MaxY = dq0 - dq2; - // vector dqapex->dqmin - vqa1MinX = -1 / 3; - vqa1MinY = dq0 - dq1; - } - // Compare cross products of these vectors to determine, if - // point is in triangles [ dq3, dqMax, dq0 ] or [ dq0, dqMax, dq3 ] - var a1a2_a1Min = vqa1a2X * vqa1MinY - vqa1a2Y * vqa1MinX, - a1Max_a1Min = vqa1MaxX * vqa1MinY - vqa1MaxY * vqa1MinX; - return a1a2_a1Min * a1Max_a1Min < 0 - // Point [2/3, dq2] is inside the triangle, the hull is a triangle. - ? [ - [ 0, dq0, dqMaxX, dqMaxY ], - [ dqMaxX, dqMaxY, 1, dq3 ], - [ 1, dq3, 0, dq0 ] - ] - // Convexhull is a quadrilateral and we need all lines in the - // correct order where line [0, q0, 1, q3] is part of the hull. - : [ - [ 0, dq0, 1 / 3, dq1 ], - [ 1 / 3, dq1, 2 / 3, dq2 ], - [ 2 / 3, dq2, 1, dq3 ], - [ 1, dq3, 0, dq0 ] - ]; -} - -// This is basically an "unrolled" version of #Line.getDistance() with sign -// May be a static method could be better! -function getSignedDistance(a1x, a1y, a2x, a2y, bx, by) { - var m = (a2y - a1y) / (a2x - a1x), - b = a1y - (m * a1x); - return (by - (m * bx) - b) / Math.sqrt(m * m + 1); -} - -/** - * Intersections between curve and line becomes rather simple here mostly - * because of Numerical class. We can rotate the curve and line so that the line - * is on X axis, and solve the implicit equations for X axis and the curve. - */ -function getCurveLineIntersections(v1, v2, curve1, curve2, locations, flip) { - if (flip === undefined) - flip = Curve.isLinear(v1); - var vc = flip ? v2 : v1, - vl = flip ? v1 : v2, - l1x = vl[0], l1y = vl[1], - l2x = vl[6], l2y = vl[7], - // Rotate both the curve and line around l1 so that line is on x axis - lvx = l2x - l1x, - lvy = l2y - l1y, - // Angle with x axis (1, 0) - angle = Math.atan2(-lvy, lvx), - sin = Math.sin(angle), - cos = Math.cos(angle), - // (rl1x, rl1y) = (0, 0) - rl2x = lvx * cos - lvy * sin, - rl2y = lvy * cos + lvx * sin, - vcr = []; - - for(var i = 0; i < 8; i += 2) { - var x = vc[i] - l1x, - y = vc[i + 1] - l1y; - vcr.push( - x * cos - y * sin, - y * cos + x * sin); - } - var roots = [], - count = Curve.solveCubic(vcr, 1, 0, roots); - // NOTE: count could theoretically be -1 for inifnite solutions, although - // that should only happen with lines, in which case we should not be here. - for (var i = 0; i < count; i++) { - var t = roots[i]; - if (t >= 0 && t <= 1) { - var point = Curve.evaluate(vcr, t, true, 0); - // We do have a point on the infinite line. Check if it falls on the - // line *segment*. - if (point.x >= 0 && point.x <= rl2x) - addLocation(locations, - flip ? curve2 : curve1, - // The actual intersection point - t, Curve.evaluate(vc, t, true, 0), - flip ? curve1 : curve2); - } - } -} - -function getLineLineIntersection(v1, v2, curve1, curve2, locations) { - var point = Line.intersect( - v1[0], v1[1], v1[6], v1[7], - v2[0], v2[1], v2[6], v2[7], false); - // Passing null for parameter leads to lazy determination of parameter - // values in CurveLocation#getParameter() only once they are requested. - if (point) - addLocation(locations, curve1, null, point, curve2); -} - -}; diff --git a/fatline/README.md b/fatline/README.md deleted file mode 100644 index 9526df04..00000000 --- a/fatline/README.md +++ /dev/null @@ -1,4 +0,0 @@ - - ------- -Harikrishnan Gopalakrishnan diff --git a/fatline/intersectStudy.html b/fatline/intersectStudy.html deleted file mode 100644 index 6146539e..00000000 --- a/fatline/intersectStudy.html +++ /dev/null @@ -1,470 +0,0 @@ - - - - - Poly-bézier path Intersection Study - - - - - - - -
-

Cubic bézier fat-line clipping study (using paperjs)

- -
- - - ​ - ​ - - - ​ - ​ - - - ​ - ​ - - - - - - - - - - - - - - - - - - - - diff --git a/fatline/intersectTests.js b/fatline/intersectTests.js deleted file mode 100644 index 1b019e36..00000000 --- a/fatline/intersectTests.js +++ /dev/null @@ -1,848 +0,0 @@ -// Uncomment this to turn on fatline clipping in paper.js too -options.fatline = true; -paper.install(window); - -/** - * http://stackoverflow.com/questions/6875625/does-javascript-provide-a-high-resolution-timer - */ -if (window.performance && window.performance.now) { - console.log("Using high performance timer"); - getTimestamp = function() { return window.performance.now(); }; -} else { - if (window.performance && window.performance.webkitNow) { - console.log("Using webkit high performance timer"); - getTimestamp = function() { return window.performance.webkitNow(); }; - } else { - console.log("Using low performance timer"); - getTimestamp = function() { return new Date().getTime(); }; - } -} - -function runTests() { - var caption, pathA, pathB, group, testdata = [], randomtestdata = [], testQueued = 0, testExecuted = 0; - - var container = document.getElementById('container'); - - function runTest(testName, handler) { - var caption = document.createElement('h3'); - var canvas = document.createElement('canvas'); - caption.appendChild(document.createTextNode(testName)); - container.appendChild(caption); - container.appendChild(canvas); - ++testQueued; - setTimeout(function() { - console.log('\n' + testName); - paper.setup(canvas); - var paths = handler(); - var success = testIntersections(paths[0], paths[1], caption, testName, testdata); - testExecuted++; - if (testExecuted === testQueued) { - plotData(); - } - }, 0); - return caption; - } - - var caption = document.createElement('h3'); - caption.appendChild(document.createTextNode("Randomised tests (may take a while...)")); - container.appendChild(caption); - var canvas = document.createElement('CANVAS'); - container.appendChild(canvas); - paper.setup(canvas); - doRandomTests(randomtestdata); - window.d = randomtestdata; - container.removeChild(canvas); - - runTest('random', function() { - pathA = getRandomPath(20); - pathB = getRandomPath(20); - return [pathA, pathB]; - }); - - runTest('failcase 1', function() { - group = paper.project.importSVG(document.getElementById('svgrandom1')); - pathA = group.children[0]; - pathB = group.children[1]; - pathA.style = pathB.style = null; - return [pathA, pathB]; - }); - - runTest('failcase 2', function() { - group = paper.project.importSVG(document.getElementById('svgrandom2')); - pathA = group.children[0]; - pathB = group.children[1]; - pathA.style = pathB.style = null; - return [pathA, pathB]; - }); - - runTest('failcase 3', function() { - group = paper.project.importSVG(document.getElementById('svgrandom3')); - pathA = group.children[0]; - pathB = group.children[1]; - pathA.style = pathB.style = null; - return [pathA, pathB]; - }); - - runTest('Overlapping circles', function() { - pathA = new Path.Circle(new Point(80, 110), 50); - pathB = new Path.Circle(new Point(150, 110), 70); - return [pathA, pathB]; - }); - - runTest('Polygon and square', function() { - pathA = new Path.RegularPolygon(new Point(80, 110), 12, 80); - pathB = new Path.Rectangle(new Point(100, 80), [80, 80]); - return [pathA, pathB]; - }); - - runTest('Circle and square (overlaps exactly on existing segments)', function() { - pathA = new Path.Circle(new Point(110, 110), 80); - pathB = new Path.Rectangle(new Point(110, 110), [80, 80]); - return [pathA, pathB]; - }); - - runTest('Circle and square (existing segments overlaps on curves)', function() { - pathA = new Path.Circle(new Point(110, 110), 80); - pathB = new Path.Rectangle(new Point(110, 110), [100, 100]); - return [pathA, pathB]; - }); - - runTest('Square and square (one segment overlaps on a line)', function() { - pathA = new Path.Rectangle(new Point(80, 125), [50, 50]); - pathA.rotate(45); - pathB = new Path.Rectangle(new Point(pathA.segments[2].point.x, 110), [80, 80]); - return [pathA, pathB]; - }); - - runTest('Rectangle and rectangle (overlaps exactly on existing curves)', function() { - pathA = new Path.Rectangle(new Point(30.5, 50.5), [100, 150]); - pathB = new Path.Rectangle(new Point(130.5, 60.5), [100, 150]); - return [pathA, pathB]; - }); - - runTest('Overlapping stars 1', function() { - pathA = new Path.Star(new Point(80, 110), 10, 20, 80); - pathB = new Path.Star(new Point(120, 110), 10, 30, 100); - return [pathA, pathB]; - }); - - runTest('Overlapping stars 2', function() { - pathA = new Path.Star(new Point(110, 110), 20, 20, 80); - pathB = new Path.Star(new Point(110, 110), 6, 30, 100); - return [pathA, pathB]; - }); - - runTest('Circle and banana (multiple intersections within same curve segment)', function() { - pathA = new Path.Circle(new Point(80, 110), 80); - pathB = new Path.Circle(new Point(130, 110), 80); - pathB.segments[3].point = pathB.segments[3].point.add([ 0, -120 ]); - return [pathA, pathB]; - }); - - runTest('Maximum possible intersections between 2 cubic bezier curve segments - 9', function() { - pathA = new Path(); - pathA.add(new Segment([173, 44], [-281, 268], [-86, 152])); - pathA.add(new Segment([47, 93], [-89, 100], [240, -239])); - pathA.closed = true; - pathB = pathA.clone(); - pathB.rotate(-90); - pathA.translate([-10,0]); - pathB.translate([10,0]); - return [pathA, pathB]; - }); - - runTest('SVG gears', function() { - group = paper.project.importSVG(document.getElementById('svggears')); - pathA = group.children[0]; - pathB = group.children[1]; - return [pathA, pathB]; - }); - - runTest('Glyphs imported from SVG', function() { - group = paper.project.importSVG(document.getElementById('glyphsys')); - pathA = group.children[0]; - pathB = group.children[1]; - return [pathA, pathB]; - }); - - runTest('CompoundPaths 1', function() { - group = paper.project.importSVG(document.getElementById('glyphsacirc')); - pathA = group.children[0]; - pathB = group.children[1]; - return [pathA, pathB]; - }); - - runTest('CompoundPaths 2 - holes', function() { - group = paper.project.importSVG(document.getElementById('glyphsacirc')); - pathA = group.children[0]; - pathB = new CompoundPath(); - group.children[1].clockwise = true; - pathB.addChild(group.children[1]); - var npath = new Path.Circle([110, 110], 30); - pathB.addChild(npath); - return [pathA, pathB]; - }); - - runTest('CompoundPaths 3 !', function() { - group = paper.project.importSVG(document.getElementById('svggreenland')); - pathA = group.children[0]; - pathB = group.children[1]; - pathB.scale(0.5, 1).translate([25.5, 0]); - // pathA.scale(2); - // pathB.scale(2); - return [pathA, pathB]; - }); - - runTest('CompoundPaths 4 - holes and islands 1', function() { - group = paper.project.importSVG(document.getElementById('glyphsacirc')); - pathA = group.children[0]; - pathB = new CompoundPath(); - group.children[1].clockwise = true; - pathB.addChild(group.children[1]); - var npath = new Path.Circle([40, 80], 20); - pathB.addChild(npath); - return [pathA, pathB]; - }); - - runTest('CompoundPaths 5 - holes and islands 2', function() { - group = paper.project.importSVG(document.getElementById('glyphsacirc')); - pathA = group.children[0]; - pathB = new CompoundPath(); - group.children[1].clockwise = true; - pathB.addChild(group.children[1]); - var npath = new Path.Circle([40, 80], 20); - pathB.addChild(npath); - npath = new Path.Circle([120, 110], 30); - pathB.addChild(npath); - return [pathA, pathB]; - }); - - runTest('CompoundPaths 6 - holes and islands 3', function() { - group = paper.project.importSVG(document.getElementById('glyphsacirc')); - pathA = group.children[0]; - pathB = new CompoundPath(); - var npath = new Path.Circle([110, 110], 100); - pathB.addChild(npath); - npath = new Path.Circle([110, 110], 60); - pathB.addChild(npath); - npath = new Path.Circle([110, 110], 30); - pathB.addChild(npath); - return [pathA, pathB]; - }); - - runTest('CompoundPaths 6 - holes and islands 4 (curves overlap exactly on existing curves)', function() { - pathA = new Path.Rectangle(new Point(50.5, 50.5), [100, 120]); - pathB = new CompoundPath(); - pathB.addChild(new Path.Rectangle(new Point(140.5, 30.5), [100, 150])); - pathB.addChild(new Path.Rectangle(new Point(150.5, 65.5), [50, 100])); - // pathB = new Path.Rectangle(new Point(150.5, 80.5), [80, 80]); - return [pathA, pathB]; - }); - - - // Plot the run times - function plotData() { - prepareTest('Results - Random tests (Intersections/Curve vs Time)', container, 'big2'); - plotDataRandom(randomtestdata); - - prepareTest('Results - Boolean tests (Time taken per test)', container, 'big'); - var x = 80.5, y = 15.5, width = 500, height = 190, i, txt, ny, - yy = y + height, xx = x + width; - var ppaper = new Path(), - pfat = new Path(); - var max = testdata.reduce(function(a, b) { - return Math.max(a, b.paperTime + b.fatTime); - }, 0) + 20; - var vscale = height / max, hscale = width / testdata.length; - var caxes = '#999', ctxt = '#222', ctxt2 = '#555', cpaper = '#268BD2', cpaperfill ='#B5E1FF', - cfat = '#D33682', cfatfill = '#FFADD4'; - new Path.Line(x, yy, xx, yy).strokeColor = caxes; - new Path.Line(x, yy, x, y).strokeColor = caxes; - for (i = 0; i < 10 ; i++) { - ny = yy - vscale * max * i / 10; - new Path.Line(x, ny, x-5, ny).strokeColor = caxes; - txt = new PointText([x-10, ny]); - txt.justification = 'right'; - txt.fillColor = (i%2)? ctxt: ctxt2; - txt.content = (max * i / 10).toFixed(1) + ((!i)? ' ms' : ''); - } - ppaper.add(x, yy); - pfat.add(x, yy); - var vx = x, clr = ctxt; - var coords = [], avgPaper = 0, avgFat = 0, - maxSpeedup = -Infinity, minSpeedup = Infinity, avgSpeedup = 0; - testdata.map(function(data) { - avgPaper += data.paperTime; - ny = yy - (data.paperTime /*+ data.fatTime */) * vscale; - ppaper.add(vx, ny); - var np = new Point(vx, ny); - np._data = data; - np._datatype = 'paper'; - coords.push(np); - avgFat += data.fatTime; - ny = yy - (data.fatTime) * vscale; - pfat.add(vx, ny); - np = new Point(vx, ny); - np._data = data; - np._datatype = 'fat'; - coords.push(np); - - var speedup = data.paperTime / data.fatTime; - if (speedup > maxSpeedup) maxSpeedup = speedup; - if (speedup < minSpeedup) minSpeedup = speedup; - avgSpeedup += speedup; - - new Path.Line(vx, yy, vx, yy + 5).strokeColor = caxes; - txt = new PointText([vx, yy+18]); - txt.justification = 'left'; - txt.fillColor = clr; - txt.content = data.name; - txt.rotate(30, new Point(vx, yy+10)); - - if (!data.success) { - var p = new Path.Line(vx, y, vx, yy); - p.strokeWidth = 5; - p.strokeColor = '#f00'; - } - - clr = (clr === ctxt)? ctxt2 : ctxt; - vx += hscale; - }); - ppaper.strokeWidth = 2; - ppaper.strokeColor = cpaper; - ppaper.add(vx-hscale, yy); - ppaper.closed = true; - ppaper.fillColor = cpaperfill; - ppaper.opacity = 0.75; - pfat.strokeWidth = 2; - pfat.strokeColor = cfat; - pfat.add(vx-hscale, yy); - pfat.closed = true; - pfat.fillColor = cfatfill; - pfat.opacity = 0.75; - - avgPaper/= testdata.length; - avgFat/= testdata.length; - avgSpeedup = Math.round(avgSpeedup / testdata.length); - maxSpeedup = Math.round(maxSpeedup); - minSpeedup = Math.round(minSpeedup); - ny = Math.round(yy - avgPaper * vscale) + 0.5; - new Path.Line(x, ny, xx, ny).strokeColor = cpaper; - txt = new PointText([xx, ny]); - txt.justification = 'right'; - txt.fillColor = cpaper; - txt.content = avgPaper.toFixed(1); - ny = Math.round(yy - avgFat * vscale) + 0.5; - new Path.Line(x, ny, xx, ny).strokeColor = cfat; - txt = new PointText([xx, ny]); - txt.justification = 'right'; - txt.fillColor = cfat; - txt.content = avgFat.toFixed(1); - - txt = new PointText([610, 75]); - txt.justification = 'center'; - txt.fillColor = '#000'; - txt.content = 'fatline vs subdiv'; - new Path.Rectangle([600, 90], [20, 100]).style = { fillColor: '#ccc', strokeColor: '#000' }; - ny = 190 - (avgSpeedup - minSpeedup) * 100.0 / (maxSpeedup - minSpeedup); - new Path.Line([600, ny], [620, ny]).style = { strokeWidth: 2, strokeColor: '#000' }; - txt = new PointText([630, 95]); - txt.fillColor = '#000'; - txt.content = maxSpeedup; - txt = new PointText([630, 195]); - txt.fillColor = '#000'; - txt.content = minSpeedup; - txt = new PointText([630, ny+5]); - txt.fillColor = '#000'; - txt.content = avgSpeedup + ' times'; - view.draw(); - - var tool = new Tool(); - tool.onMouseMove = function(e) { - var len = coords.length; - var data = null, dist = Infinity, dst, pnt = null, type = 'paper'; - while (len--) { - dst = e.point.getDistance(coords[len], true); - if (dst < dist) { - pnt = coords[len]; - data = coords[len]._data; - type = coords[len]._datatype; - dist = dst; - } - } - if (dist > 500) { return; } - if (pnt && data) { - var p = new Path.Line(pnt.x+0.5, y, pnt.x+0.5, yy); - p.strokeColor = '#000'; - p.removeOnMove(); - p = new Path.Circle(pnt, 3); - p.fillColor = (type === 'fat')? '#D33682' :'#268BD2'; - p.removeOnMove(); - var txt = new PointText([ 500, 20 ]); - txt.content = 'subdiv : ' + data.paperTime.toFixed(1) + ' ms'; - txt.fillColor = '#222'; - txt.removeOnMove(); - txt = new PointText([ 500, 36 ]); - txt.content = 'fatline : ' + data.fatTime.toFixed(1) + ' ms'; - txt.fillColor = '#222'; - txt.removeOnMove(); - } - }; - } - - - function prepareTest(testName, parentNode, _big) { - console.log('\n' + testName); - var caption = document.createElement('h3'); - caption.appendChild(document.createTextNode(testName)); - var canvas = document.createElement('CANVAS'); - canvas.className += ' ' + _big; - parentNode.appendChild(caption); - parentNode.appendChild(canvas); - paper.setup(canvas); - return caption; - } -} - -var pathStyleIx = { - fillColor: new Color(0.8, 0, 0), - strokeColor: new Color(0, 0, 0) -}; - -var pathStyleNormal = { - strokeColor: new Color(0, 0, 0), - // fillColor: new Color(0, 0, 0, 0.1), - strokeWidth: 1 -}; - -var pathStyleBoolean = { - strokeColor: new Color(0,0,0,0.4), - fillColor: new Color(0, 0, 0, 0.0), - strokeWidth: 1 -}; - - -// Better if path1 and path2 fit nicely inside a 200x200 pixels rect -function testIntersections(path1, path2, caption, testname, testdata, nomark) { - var i, l, maxCount = 10, count = maxCount, st, t1, t2, - ixsPaper, ixsFatline, success = false, maxdiff = -Infinity; - try { - path1.style = path2.style = pathStyleNormal; - - if (!nomark) console.time('paperjs x ' + maxCount); - st = getTimestamp(); - while (count--) { - ixsPaper = path1.getIntersections(path2); - } - t1 = (getTimestamp() - st) / maxCount; - if (!nomark) console.timeEnd('paperjs x ' + maxCount); - - count = maxCount; - if (!nomark) console.time('fatline x ' + maxCount); - st = getTimestamp(); - while (count--) { - ixsFatline = path1.getIntersections2(path2); - } - t2 = (getTimestamp() - st) / maxCount; - if (!nomark) console.timeEnd('fatline x ' + maxCount); - - var found = 0, tol = 0.1; - if (ixsFatline.length === ixsPaper.length) { - for (i=0, l=ixsFatline.length; i= 0; i--) { - annotateCurve(crvs[i], t, c, tc, remove); - } - var segs = path.segments; - for (i = segs.length - 1; i >= 0; i--) { - annotateSegment(segs[i], t, c, tc, remove, true); - } -} - -function annotateSegment(s, t, c, tc, remove, skipCurves) { - if (!s) return; - c = c || '#000'; - tc = tc || '#ccc'; - t = t || s.index; - if (remove === undefined) { remove = true; } - var crv = s.curve; - var t1 = crv.getNormal(0).normalize(10); - var p = s.point.clone().add(t1); - var cir = new Path.Circle(s.point, 2); - cir.fillColor = c; - cir.strokeColor = tc; - var text = new PointText(p); - text.justification = 'center'; - text.fillColor = c; - text.content = t; - if (remove) { - cir.removeOnMove(); - text.removeOnMove(); - } - if (!skipCurves) { - annotateCurve(s.curveIn, null, c, tc, remove); - annotateCurve(s.curveOut, null, c, tc, remove); - } -} - -function annotateCurve(crv, t, c, tc, remove) { - if (!crv) return; - c = c || '#000'; - tc = tc || '#ccc'; - t = t || crv.index; - if (remove === undefined) { remove = true; } - var p = crv.getPoint(0.57); - var t1 = crv.getTangent(0.57).normalize(-10); - var p2 = p.clone().add(t1); - var l = new Path.Line(p, p2).rotate(30, p); - var l2 = new Path.Line(p, p2).rotate(-30, p); - p = crv.getPoint(0.43); - var cir = new Path.Circle(p, 8); - var text = new PointText(p.subtract([0, -4])); - text.justification = 'center'; - text.fillColor = tc; - text.content = t; - l.strokeColor = l2.strokeColor = c; - cir.fillColor = c; - if (remove) { - l.removeOnMove(); - l2.removeOnMove(); - cir.removeOnMove(); - text.removeOnMove(); - } -} - - -// Plot the run times -function plotDataRandom(testdata) { - var x = 80.5, y = 15.5, width = 500, height = 190, i, txt, ny, - yy = y + height, xx = x + width; - var ppaper = new Path(), pfat = new Path(); - var max = testdata.reduce(function(a, b) { return Math.max(a, b.paperTime, b.fatTime); }, 0) + 20; - testdata.sort(function(a,b) { return a.ratio - b.ratio; }); - var vscale = height / max, hscale = width / testdata.length; - var caxes = '#999', ctxt = '#222', cpaper = '#268BD2', cfat = '#D33682'; - new Path.Line(x, yy, xx, yy).strokeColor = caxes; - new Path.Line(x, yy, x, y).strokeColor = caxes; - for (i = 0; i < 10 ; i++) { - ny = yy - vscale * max * i / 10; - new Path.Line(x, ny, x-5, ny).strokeColor = caxes; - txt = new PointText([x-10, ny]); - txt.justification = 'right'; - txt.fillColor = ctxt; - txt.content = (max * i / 10).toFixed(1) + ((!i)? ' ms' : ''); - } - txt = new PointText([xx + 20, yy + 18 ]); - txt.justification = 'left'; - txt.fillColor = ctxt; - txt.content = 'ixs / curve'; - txt = new PointText([xx + 20, yy + 40]); - txt.justification = 'left'; - txt.fillColor = '#999'; - txt.content = '(Total Curves)'; - var vx = x, step = 15, count = 0; - var avgPaper = 0, avgFat = 0; - testdata.map(function(data) { - avgPaper += data.paperTime; - ny = yy - (data.paperTime /* + data.fatTime*/) * vscale; - ppaper.add(vx, ny); - avgFat += data.fatTime; - ny = yy - (data.fatTime) * vscale; - pfat.add(vx, ny); - - new Path.Line(vx, yy, vx, yy + 5 + ((count%2)? step:0)).strokeColor = caxes; - txt = new PointText([vx, yy+18 + ((count%2)? step:0) ]); - txt.justification = 'center'; - txt.fillColor = ctxt; - txt.content = data.ratio.toFixed(1); - txt = new PointText([vx -5, yy+40 ]); - txt.justification = 'left'; - txt.fillColor = '#999'; - txt.content = data.curves; - txt.rotate(90, [vx-5, yy+40 ]); - - if (!data.success) { - var p = new Path.Line(vx, y, vx, yy); - p.strokeWidth = 5; - p.strokeColor = '#f00'; - } - ++count; - vx += hscale; - }); -// ppaper.smooth(); - ppaper.strokeWidth = 2; - ppaper.strokeColor = cpaper; - ppaper.opacity = 0.75; -// pfat.smooth(); - pfat.strokeWidth = 2; - pfat.strokeColor = cfat; - pfat.opacity = 0.75; - - avgPaper/= testdata.length; - avgFat/= testdata.length; - ny = Math.round(yy - avgPaper * vscale) + 0.5; - new Path.Line(x, ny, xx, ny).strokeColor = cpaper; - txt = new PointText([xx, ny]); - txt.justification = 'right'; - txt.fillColor = cpaper; - txt.content = avgPaper.toFixed(1); - ny = Math.round(yy - avgFat * vscale) + 0.5; - new Path.Line(x, ny, xx, ny).strokeColor = cfat; - txt = new PointText([xx, ny]); - txt.justification = 'right'; - txt.fillColor = cfat; - txt.content = avgFat.toFixed(1); - - view.draw(); -} - -function drawFatline(v1) { - function signum(num) { - return (num > 0)? 1 : (num < 0)? -1 : 0; - } - var l = new Line([v1[0], v1[1]], [v1[6], v1[7]], false); - var p1 = new Point(v1[2], v1[3]), p2 = new Point(v1[4], v1[5]); - var d1 = l.getSide(p1) * l.getDistance(p1) || 0; - var d2 = l.getSide(p2) * l.getDistance(p2) || 0; - var dmin, dmax; - if (d1 * d2 > 0) { - // 3/4 * min{0, d1, d2} - dmin = 0.75 * Math.min(0, d1, d2); - dmax = 0.75 * Math.max(0, d1, d2); - } else { - // 4/9 * min{0, d1, d2} - dmin = 4 * Math.min(0, d1, d2) / 9.0; - dmax = 4 * Math.max(0, d1, d2) / 9.0; - } - var ll = new Path.Line(v1[0], v1[1], v1[6], v1[7]); - window.__p3.push(ll); - window.__p3[window.__p3.length-1].strokeColor = new Color(0,0,0.9, 0.8); - var lp1 = ll.segments[0].point; - var lp2 = ll.segments[1].point; - var pm = l.vector, pm1 = pm.rotate(signum(dmin) * -90), pm2 = pm.rotate(signum(dmax) * -90); - var p11 = lp1.add(pm1.normalize(Math.abs(dmin))); - var p12 = lp2.add(pm1.normalize(Math.abs(dmin))); - var p21 = lp1.add(pm2.normalize(Math.abs(dmax))); - var p22 = lp2.add(pm2.normalize(Math.abs(dmax))); - window.__p3.push(new Path.Line(p11, p12)); - window.__p3[window.__p3.length-1].strokeColor = new Color(0,0,0.9); - window.__p3.push(new Path.Line(p21, p22)); - window.__p3[window.__p3.length-1].strokeColor = new Color(0,0,0.9); -} - -function plotD_vs_t(x, y, arr, arr2, v, dmin, dmax, tmin, tmax, yscale, tvalue) { - yscale = yscale || 1; - new Path.Line(x, y-100, x, y+100).strokeColor = '#aaa'; - new Path.Line(x, y, x + 200, y).strokeColor = '#aaa'; - - var clr = (tvalue)? '#a00' : '#00a'; - if (window.__p3) window.__p3.map(function(a) {a.remove();}); - - window.__p3 = []; - - drawFatline(v); - - window.__p3.push(new Path.Line(x, y + dmin * yscale, x + 200, y + dmin * yscale)); - window.__p3[window.__p3.length-1].strokeColor = '#000'; - window.__p3.push(new Path.Line(x, y + dmax * yscale, x + 200, y + dmax * yscale)); - window.__p3[window.__p3.length-1].strokeColor = '#000'; - window.__p3.push(new Path.Line(x + tmin * 190, y-100, x + tmin * 190, y+100)); - window.__p3[window.__p3.length-1].strokeColor = clr; - window.__p3.push(new Path.Line(x + tmax * 190, y-100, x + tmax * 190, y+100)); - window.__p3[window.__p3.length-1].strokeColor = clr; - - for (var i = 0; i < arr.length; i++) { - window.__p3.push(new Path.Line(new Point(x + arr[i][0] * 190, y + arr[i][1] * yscale), - new Point(x + arr[i][2] * 190, y + arr[i][3] * yscale))); - window.__p3[window.__p3.length-1].strokeColor = '#999'; - } - var pnt = []; - var arr2x = [ 0.0, 0.333333333, 0.6666666666, 1.0 ]; - for (var i = 0; i < arr2.length; i++) { - pnt.push(new Point(x + arr2x[i] * 190, y + arr2[i] * yscale)); - window.__p3.push(new Path.Circle(pnt[pnt.length-1], 2)); - window.__p3[window.__p3.length-1].fillColor = '#000'; - } - // var pth = new Path(pnt[0], pnt[1], pnt[2], pnt[3]); - // pth.closed = true; - window.__p3.push(new Path( - new Segment(pnt[0], null, pnt[1].subtract(pnt[0])), - new Segment(pnt[3], pnt[2].subtract(pnt[3]), null))); - window.__p3[window.__p3.length-1].strokeColor = clr; - view.draw(); -}