fixes #327 cleaning up svg to poly/extrusion legacy code

This commit is contained in:
Stewart Allen 2025-01-26 11:21:51 -05:00
commit 561911ee8e

View file

@ -22,11 +22,11 @@ function parse(text, opt = { }) {
const rez = (opt.resolution || 1);
const dpi = (opt.dpi || 0);
const segmin = Math.max(1, opt.segmin || 10);
const objs = [];
const data = new THREE.SVGLoader().parse(text);
const paths = data.paths;
const xmlat = data.xml.attributes;
const polys = fromSoup ? [] : undefined;
const objs = [];
const polys = [];
const isinch = xmlat.width?.value.endsWith('in');
const scale = isinch ? 25.4 : (dpi ? 1 / (dpi / 25.4) : 1);
const depth = parseFloat(opt.depth || xmlat['data-km-extrude']?.value
@ -39,76 +39,50 @@ function parse(text, opt = { }) {
let type = path.userData?.node?.nodeName;
let width = path.userData?.style?.strokeWidth;
let miter = path.userData?.style?.strokeMiterLimit;
if (fromSoup) {
for (let sub of path.subPaths) {
let points = sub.curves.map(curve => {
let length = curve.getLength();
let segs = curve.type === 'LineCurve' ?
1 : Math.max(Math.ceil(length * rez), segmin);
return curve.getPoints(segs);
}).flat();
if (points.length < 3) {
// console.log({ sub, length, points });
continue;
}
let poly = base.newPolygon().addPoints(points.map(p => base.newPoint(p.x, -p.y, 0)));
if (poly.appearsClosed()) poly.points.pop();
if (type === 'polyline') poly.setOpen(true);
poly._svg = { width, miter };
polys.push(poly);
if (scale !== 1) {
poly.scale({ x: scale, y: scale, z: 1 });
}
for (let sub of path.subPaths) {
let points = sub.curves.map(curve => {
let length = curve.getLength();
let segs = curve.type === 'LineCurve' ?
1 : Math.max(Math.ceil(length * rez), segmin);
return curve.getPoints(segs);
}).flat();
if (points.length < 3) {
// console.log({ sub, length, points });
continue;
}
continue;
}
if (justPoly) {
continue;
}
let geom = new THREE.ExtrudeGeometry(shapes, {
depth,
steps: 1,
bevelEnabled: false
});
let array = geom.attributes.position.array;
// invert y
for (let i=1; i<array.length; i+=3) {
array[i] = -array[i];
}
// invert vertex order to compensate for inverted y
for (let i=0; i<array.length; i+=9) {
let tmp = array.slice(i,i+3);
for (let j=0; j<3; j++) {
array[i+j] = array[i+j+3];
array[i+j+3] = tmp[j];
let poly = base.newPolygon().addPoints(points.map(p => base.newPoint(p.x, -p.y, 0)));
if (poly.appearsClosed()) poly.points.pop();
if (type === 'polyline') poly.setOpen(true);
poly._svg = { width, miter };
polys.push(poly);
if (scale !== 1) {
poly.scale({ x: scale, y: scale, z: 1 });
}
}
objs.push([ ...array ]);
}
if (fromSoup) {
const nest = base.polygons.nest(polys.filter(p => {
// filter duplicates
for (let pc of polys) {
if (pc === p) {
return true;
} else {
return !pc.isEquivalent(p);
}
}
}));
if (justPoly) {
return nest;
}
for (let poly of nest) {
let obj = poly.extrude(depth);
objs.push(obj);
}
}
return justPoly ? polys : objs;
const sub = fromSoup ? base.polygons.nest(polys) : polys;
const nest = sub.filter(p => {
// filter duplicates
for (let pc of polys) {
if (pc === p) {
return true;
} else {
return !pc.isEquivalent(p);
}
}
});
if (justPoly) {
return nest;
}
for (let poly of nest) {
let obj = poly.extrude(depth);
objs.push(obj);
}
return objs;
}
});