Move the shape cleanup functionality out.

This commit is contained in:
be5invis 2020-06-17 03:35:21 -07:00
parent 5a0546270c
commit 9543e23a95
5 changed files with 135 additions and 500 deletions

View file

@ -37,87 +37,6 @@ exports.curveToContour = function (curve, segments) {
return [Point.cornerFrom(z0), ...offPoints, Point.cornerFrom(z1)];
};
function removeMids(contour) {
for (let rounds = 0; rounds < 255; rounds++) {
const n0 = contour.length;
let last = contour.length - 1;
for (let j = 0; j < contour.length - 1; j++) {
if (
Math.abs(contour[j].x - contour[j + 1].x) < 1 &&
Math.abs(contour[j].y - contour[j + 1].y) < 1
) {
contour[j + 1].rem = true;
contour[j].on = true;
}
}
while (
last > 0 &&
Math.abs(contour[0].x - contour[last].x) < 1 &&
Math.abs(contour[0].y - contour[last].y) < 1
) {
contour[last].rem = true;
contour[0].on = true;
last -= 1;
}
contour = contour.filter(x => !x.rem);
last = contour.length - 1;
let zPre, z, zPost;
for (let j = 1; j < contour.length; j++) {
if (j < contour.length - 1) {
zPre = contour[j - 1];
z = contour[j];
zPost = contour[j + 1];
} else {
zPre = contour[last];
z = contour[0];
zPost = contour[1];
}
// if (!zPre || !zPost) continue;
const mx = zPre.x + zPost.x;
const my = zPre.y + zPost.y;
const dx = zPre.x - zPost.x;
const dy = zPre.y - zPost.y;
if (!zPre.on && z.on && !zPost.on) {
if (Math.abs(dy) >= 1 && Math.abs(z.x * 2 - mx) < 1 && Math.abs(z.y * 2 - my) < 1) {
z.rem = true;
}
} else if (!zPre.rem && zPre.on && z.on && !zPost.rem && zPost.on) {
if (Math.abs(dy) >= 1 && Math.abs(z.x * 2 - mx) < 1 && Math.abs(dx) < 1) {
z.rem = true;
} else if (Math.abs(dx) >= 1 && Math.abs(z.y * 2 - my) < 1 && Math.abs(dy) < 1) {
z.rem = true;
}
}
}
contour = contour.filter(x => !x.rem);
const n = contour.length;
if (n >= n0) break;
}
return contour;
}
function extPrior(a, b) {
return a.y < b.y || (a.y === b.y && ((a.on && !b.on) || (a.on === b.on && a.x < b.x)));
}
function canonicalStart(_points) {
const points = _points.reverse();
let jm = 0;
for (let j = 0; j < points.length * 2; j++) {
if (extPrior(points[j % points.length], points[jm])) {
jm = j % points.length;
}
}
return points.slice(jm).concat(points.slice(0, jm));
}
function cleanupQuadContour(c) {
return canonicalStart(removeMids(c));
}
function convertContourToCubic(contour) {
if (!contour || !contour.length) return [];
@ -271,7 +190,6 @@ function convertShapeToArcs(shape) {
return shape.map(convertContourToArcs);
}
exports.cleanupQuadContour = cleanupQuadContour;
exports.convertContourToCubic = convertContourToCubic;
exports.convertContourToCubicRev = convertContourToCubicRev;
exports.autoCubify = autoCubify;