Fixup more broken geometries. (#2276)

This commit is contained in:
Belleve 2024-04-02 18:14:21 -10:00 committed by GitHub
parent 6923d74c0f
commit dd7d055302
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 136 additions and 101 deletions

View file

@ -136,17 +136,28 @@ export class BezToContoursSink {
this.lastContour = [];
}
moveTo(x, y) {
if (!isFinite(x) || !isFinite(y)) throw new Error("Invalid coordinates detected in moveTo");
this.endShape();
this.lastContour.push(Point.transformedXY(this.gizmo, Point.Type.Corner, x, y));
}
lineTo(x, y) {
if (!isFinite(x) || !isFinite(y)) throw new Error("Invalid coordinates detected in lineTo");
this.lastContour.push(Point.transformedXY(this.gizmo, Point.Type.Corner, x, y));
}
curveTo(xc, yc, x, y) {
if (!isFinite(xc) || !isFinite(yc) || !isFinite(x) || !isFinite(y))
throw new Error("Invalid coordinates detected in curveTo");
this.lastContour.push(Point.transformedXY(this.gizmo, Point.Type.Quadratic, xc, yc));
this.lastContour.push(Point.transformedXY(this.gizmo, Point.Type.Corner, x, y));
}
cubicTo(x1, y1, x2, y2, x, y) {
if (!isFinite(x1) || !isFinite(y1))
throw new Error("Invalid coordinates detected in cubicTo");
if (!isFinite(x2) || !isFinite(y2))
throw new Error("Invalid coordinates detected in cubicTo");
if (!isFinite(x) || !isFinite(y))
throw new Error("Invalid coordinates detected in cubicTo");
this.lastContour.push(Point.transformedXY(this.gizmo, Point.Type.CubicStart, x1, y1));
this.lastContour.push(Point.transformedXY(this.gizmo, Point.Type.CubicEnd, x2, y2));
this.lastContour.push(Point.transformedXY(this.gizmo, Point.Type.Corner, x, y));

View file

@ -50,9 +50,11 @@ class SpiroSimplifier {
flushArcs() {
if (!this.m_ongoingArcs.length) return;
if (this.m_ongoingArcs.length === 1) {
this.combinedArcs.push(this.m_ongoingArcs[0]);
const arc = this.m_ongoingArcs[0];
if (arc.arcLength > 1e-6) this.combinedArcs.push(arc);
} else {
this.combinedArcs.push(new SpiroSequenceArc(this.m_ongoingArcs));
const combined = new SpiroSequenceArc(this.m_ongoingArcs);
if (combined.totalLength > 1e-6) this.combinedArcs.push(combined);
}
this.m_ongoingArcs = [];
}
@ -60,6 +62,16 @@ class SpiroSimplifier {
class SpiroSequenceArc {
constructor(segments) {
// Filter out zero-length segments
let rear = 0;
for (let j = 0; j < segments.length; j++) {
if (segments[j].arcLength > 1e-6) {
segments[rear++] = segments[j];
}
}
segments.length = rear;
// Compute total length and stops
let totalLength = 0;
let stops = [];
for (let j = 0; j < segments.length; j++) {
@ -69,6 +81,7 @@ class SpiroSequenceArc {
for (let j = 0; j < segments.length; j++) {
stops[j] = stops[j] / totalLength;
}
this.totalLength = totalLength;
this.m_segments = segments;
this.m_stops = stops;
}