Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: reject extrude holes that escape a concave outline, not just its bounding box (#3350)

[._.]/ Adam Eivy0550db9bee6e
diff --git a/server/lib/threejsModel.js b/server/lib/threejsModel.js
index dbece7be3e..b97f01c07f 100644
--- a/server/lib/threejsModel.js
+++ b/server/lib/threejsModel.js
@@ -80,12 +80,45 @@ const ringArea = (ring) => {
return Math.abs(doubled) / 2;
};
-const ringBounds = (ring) => ring.reduce((bounds, [x, y]) => ({
- minX: Math.min(bounds.minX, x),
- maxX: Math.max(bounds.maxX, x),
- minY: Math.min(bounds.minY, y),
- maxY: Math.max(bounds.maxY, y),
-}), { minX: Infinity, maxX: -Infinity, minY: Infinity, maxY: -Infinity });
+// Even-odd ray cast. A point exactly on an edge reads as outside, which is what
+// we want: a hole touching the outline is a malformed cutout, not a cutout.
+const pointInRing = (ring, [px, py]) => {
+ let inside = false;
+ for (let index = 0, previous = ring.length - 1; index < ring.length; previous = index, index += 1) {
+ const [xi, yi] = ring[index];
+ const [xj, yj] = ring[previous];
+ const straddles = (yi > py) !== (yj > py);
+ if (straddles && px < (((xj - xi) * (py - yi)) / (yj - yi)) + xi) inside = !inside;
+ }
+ return inside;
+};
+
+const turn = (a, b, c) => Math.sign(((b[0] - a[0]) * (c[1] - a[1])) - ((b[1] - a[1]) * (c[0] - a[0])));
+const onSegment = (a, b, p) => turn(a, b, p) === 0
+ && p[0] >= Math.min(a[0], b[0]) && p[0] <= Math.max(a[0], b[0])
+ && p[1] >= Math.min(a[1], b[1]) && p[1] <= Math.max(a[1], b[1]);
+const segmentsCross = (a, b, c, d) => {
+ if (turn(a, b, c) !== turn(a, b, d) && turn(c, d, a) !== turn(c, d, b)) return true;
+ return onSegment(a, b, c) || onSegment(a, b, d) || onSegment(c, d, a) || onSegment(c, d, b);
+};
+
+const ringsCross = (outer, inner) => {
+ for (let i = 0; i < outer.length; i += 1) {
+ const a = outer[i];
+ const b = outer[(i + 1) % outer.length];
+ for (let j = 0; j < inner.length; j += 1) {
+ if (segmentsCross(a, b, inner[j], inner[(j + 1) % inner.length])) return true;
+ }
+ }
+ return false;
+};
+
+// Vertex containment alone is not enough: a concave outline can hold every hole
+// vertex while an edge between two of them leaves through the notch. Bounded by
+// the ring caps (160 outline points × 12 holes × 160 hole points), so the O(n·m)
+// edge sweep only runs against provider output that already passed the caps.
+const ringContainsRing = (outer, inner) =>
+ inner.every((point) => pointInRing(outer, point)) && !ringsCross(outer, inner);
const outlineRingSchema = z.array(z.tuple([finite, finite])).min(3).max(160)
.refine((ring) => ringArea(ring) > MIN_RING_AREA, 'outline must enclose a non-zero area');
@@ -102,12 +135,10 @@ const extrudeGeometrySchema = z.object({
curveSegments: z.number().int().min(1).max(24).default(8),
steps: z.number().int().min(1).max(32).default(1),
}).superRefine((definition, ctx) => {
- const bounds = ringBounds(definition.outline);
- // A hole outside the outline is not a cutout — Three.js would silently emit a
- // second disjoint face, so treat it as a malformed outline instead.
+ // A hole that is not strictly inside the outline is not a cutout — Three.js
+ // silently emits a disjoint or self-intersecting face instead of failing.
definition.holes.forEach((hole, index) => {
- const outside = hole.some(([x, y]) => x < bounds.minX || x > bounds.maxX || y < bounds.minY || y > bounds.maxY);
- if (outside) {
+ if (!ringContainsRing(definition.outline, hole)) {
ctx.addIssue({ code: 'custom', message: `extrude hole ${index} falls outside the outline`, path: ['holes', index] });
}
});
diff --git a/server/lib/threejsModel.test.js b/server/lib/threejsModel.test.js
index c5fb521fd6..118b5bcc6e 100644
--- a/server/lib/threejsModel.test.js
+++ b/server/lib/threejsModel.test.js
@@ -138,6 +138,39 @@ describe('threejsGeometrySchema extrude/tube validation', () => {
expect(result.error.issues.some((issue) => issue.message.includes('falls outside the outline'))).toBe(true);
});
+ // An L-shape: the bounding box covers the notch, so a bounds-only containment
+ // check would accept a hole floating in empty space outside the outline.
+ const lShapedOutline = [[0, 0], [3, 0], [3, 1], [1, 1], [1, 3], [0, 3]];
+
+ it('rejects a hole sitting in the notch of a concave outline', () => {
+ const result = threejsGeometrySchema.safeParse({
+ ...validExtrude(),
+ outline: lShapedOutline,
+ holes: [[[1.5, 1.5], [2.5, 1.5], [2.5, 2.5], [1.5, 2.5]]],
+ });
+ expect(result.success).toBe(false);
+ expect(result.error.issues.some((issue) => issue.message.includes('falls outside the outline'))).toBe(true);
+ });
+
+ it('rejects a hole whose vertices are inside but whose edge leaves the outline', () => {
+ const result = threejsGeometrySchema.safeParse({
+ ...validExtrude(),
+ outline: lShapedOutline,
+ holes: [[[0.5, 0.5], [2.5, 0.5], [0.5, 2.5]]],
+ });
+ expect(result.success).toBe(false);
+ expect(result.error.issues.some((issue) => issue.message.includes('falls outside the outline'))).toBe(true);
+ });
+
+ it('accepts a hole fully inside a concave outline', () => {
+ const result = threejsGeometrySchema.safeParse({
+ ...validExtrude(),
+ outline: lShapedOutline,
+ holes: [[[0.2, 0.2], [0.8, 0.2], [0.8, 0.8], [0.2, 0.8]]],
+ });
+ expect(result.success).toBe(true);
+ });
+
it('rejects a tube path that repeats a point consecutively', () => {
const result = threejsGeometrySchema.safeParse({
...validTube(),