diff --git a/src/kernel/engine.ts b/src/kernel/engine.ts index e83d348..d9e9cf2 100644 --- a/src/kernel/engine.ts +++ b/src/kernel/engine.ts @@ -1354,33 +1354,30 @@ function trimSweepEnd(body: Shape3D, F: Face, by: number, ctx: Ctx): Shape3D | n const c = F.center.toTuple() as Vec3; for (const spine of ctx.sweeps.values()) { const all = wirePoints(spine); + // Every place the path passes through F's plane heading out through F + // (the end itself, or where an earlier push left it): the one at F. On an + // S-bend the plane cuts the path elsewhere too. + const side = (p: Vec3) => dot(sub(p, origin), normal); + const tolPlane = size * 0.05 + 1e-6; + let best: { pts: Vec3[]; out: Vec3; d: number } | null = null; for (const way of [all, [...all].reverse()]) { - // Walking in from this end of the path: where it crosses F's plane (the - // end itself, or where an earlier push left the end), heading out - // through F there, close to it. - const side = (p: Vec3) => dot(sub(p, origin), normal); - const tolPlane = size * 0.05 + 1e-6; - let k = -1; - for (let j = 0; j < way.length - 1; j++) { - if (side(way[j]) >= -tolPlane && side(way[j + 1]) < side(way[j])) { - if (side(way[j + 1]) <= tolPlane) { - k = j; - break; - } - } + for (let k = 0; k < way.length - 1; k++) { + const a0 = side(way[k]); + const a1 = side(way[k + 1]); + if (!(a0 >= -tolPlane && a1 < a0 && a1 <= tolPlane)) continue; + const out = norm(sub(way[k], way[k + 1])); + if (Math.abs(dot(out, normal)) < 0.98) continue; + // (The sweep turns its profile square to the path: a profile drawn a + // little off square ends on a plane a hair off the path's end.) + const t0 = a0 <= tolPlane && k === 0 ? 0 : Math.min(1, Math.max(0, a0 / (a0 - a1))); + const q = add(way[k], scale(sub(way[k + 1], way[k]), t0)); + const d = dist(q, c); + if (d <= size && (!best || d < best.d)) best = { pts: [q, ...way.slice(k + 1)], out, d }; } - if (k < 0) continue; - const a0 = side(way[k]); - const a1 = side(way[k + 1]); - // (The sweep turns its profile square to the path: a profile drawn a - // little off square ends on a plane a hair off the path's end.) - const t0 = a0 <= tolPlane && a0 >= -tolPlane && k === 0 ? 0 : Math.min(1, Math.max(0, a0 / (a0 - a1))); - const q = add(way[k], scale(sub(way[k + 1], way[k]), t0)); - const pts = [q, ...way.slice(k + 1)]; + } + if (best) { + const { pts, out } = best; const p0 = pts[0]; - const out = norm(sub(way[k], way[k + 1])); - if (Math.abs(dot(out, normal)) < 0.98) continue; - if (dist(p0, c) > size) continue; // The piece of path to take off: from the end, `by` along it. const piece: Vec3[] = [p0]; let run = 0; @@ -1399,11 +1396,13 @@ function trimSweepEnd(body: Shape3D, F: Face, by: number, ctx: Ctx): Shape3D | n if (run < by - 1e-9 || i >= pts.length - 1) throw new Error("That's as long as the sweep's whole path or more - it would remove it"); // Straight line pieces stay a line; a curve is fitted through its points. const straight = piece.every((p) => len(sub(sub(p, p0), scale(norm(sub(piece[piece.length - 1], p0)), dot(sub(p, p0), norm(sub(piece[piece.length - 1], p0)))))) < size * 1e-6); - const reach = size * 0.05 + 1e-3; - const start = add(p0, scale(out, reach)); - const curve = straight || piece.length < 4 ? makeLine(p0, piece[piece.length - 1]) : makeBSplineApproximation(piece, { tolerance: size * 1e-6 + 1e-7, degMax: 5, degMin: 2 }); + const lead = size * 0.05 + 1e-3; + const start = add(p0, scale(out, lead)); + const curve = straight || piece.length < 4 ? makeLine(p0, piece[piece.length - 1]) : makeBSplineApproximation(piece, { tolerance: size * 1e-4 + 1e-6, degMax: 5, degMin: 2 }); const path = assembleWire([makeLine(start, p0), curve]); - const r = dist(p0, c) + size; + let reach0 = 0; + for (const e of F.edges) for (let j = 0; j <= 16; j++) reach0 = Math.max(reach0, dist(e.pointAt(j / 16).toTuple() as Vec3, p0)); + const r = reach0 * 1.1 + size * 0.02; const disc = assembleWire([makeCircle(r, start, scale(out, -1))]); const cutter = genericSweep(disc, path, { frenet: false, transitionMode: "round" }) as Shape3D; const result = fuzzyBoolean("cut", body, cutter); diff --git a/src/kernel/faceEdit.ts b/src/kernel/faceEdit.ts index e049b8d..f3bedc7 100644 --- a/src/kernel/faceEdit.ts +++ b/src/kernel/faceEdit.ts @@ -6,7 +6,7 @@ // the body is on (planes → half-spaces, cylinders → infinite cylinders). Changing // F then only means adding/removing the right slab of E. -import { assembleWire, basicFaceExtrusion, getOC, loft, makeBSplineApproximation, makeCylinder, makeLine, makePolygon, makeSphere, measureArea, measureVolume, cast, Vector, type Face, type Shape3D } from "replicad"; +import { assembleWire, basicFaceExtrusion, getOC, loft, makeFace, makeBSplineApproximation, makeCylinder, makeLine, makePolygon, makeSphere, measureArea, measureVolume, cast, Vector, type Face, type Shape3D } from "replicad"; import type { PlaneDef, Vec3 } from "../doc/types"; import { add, cross, dist, dot, len, norm, scale, sub, xDirFor } from "../geom"; import { subShapes } from "./health"; @@ -127,6 +127,22 @@ function infiniteCylinder(cy: Cyl, r: number, L0: number): Shape3D { return makeCylinder(r, 2 * L, sub(cy.c, scale(cy.a, L)), cy.a) as Shape3D; } +/** + * g, a round face next to flat face F, is a convex fillet that ends on F + * (its axis runs through F, and it rounds the edge between two flat faces it + * is tangent to): those two faces' planes, outward. Null otherwise. + */ +function filletEnding(body: Shape3D, F: Face, g: Face, cy: Cyl): [PlaneDef, PlaneDef] | null { + if (Math.abs(dot(cy.a, planeOf(F).normal)) < 0.05) return null; + const tol = sizeOf(body) * 1e-7; + const sup = neighbours(body, g).filter((h) => h.geomType === "PLANE" && !h.isSame(F) && tangentTo(cy, planeOf(h), tol)); + if (sup.length !== 2) return null; + const pls = sup.map((h) => planeOf(h)) as [PlaneDef, PlaneDef]; + // Convex: the axis lies inside the material, behind both faces. + if (!pls.every((pl) => dot(sub(cy.c, pl.origin), pl.normal) < 0)) return null; + return pls; +} + /** Faces sharing an edge with F. */ function neighbours(body: Shape3D, F: Face): Face[] { const edges = F.edges; @@ -138,12 +154,16 @@ function neighbours(body: Shape3D, F: Face): Face[] { * that contains `q` (a point just inside the body under F). Bounded by a box. */ export function extendedRegion(body: Shape3D, F: Face, q: Vec3, L: number): Shape3D { + return regionAround(body, F, neighbours(body, F), q, L); +} + +/** As extendedRegion, bounded by the given faces round F (those along one of its outlines). */ +function regionAround(body: Shape3D, F: Face, nb: Face[], q: Vec3, L: number): Shape3D { const c = body.boundingBox.center as Vec3; // Bounding box, bigger than the other tools so no faces coincide with theirs. const big = () => halfSpace({ origin: sub(c, [0, 0, 1.5 * L]), xDir: [1, 0, 0], normal: [0, 0, 1] }, 3 * L); const keep: Shape3D[] = []; const remove: Shape3D[] = []; - const nb = neighbours(body, F); for (const g of nb) { if (g.geomType === "PLANE") { const { origin: p, normal: n } = planeOf(g); @@ -153,6 +173,19 @@ export function extendedRegion(body: Shape3D, F: Face, q: Vec3, L: number): Shap } else if (g.geomType === "CYLINDRE") { const cy = cylinderOf(g); if (!cy) continue; + // A rounded edge (a fillet) that ends on F: it carries on along itself, + // between the two faces it rounds - not as an endless cylinder, which + // carved a groove into the body and left a scrap of the old face standing. + const corner = F.geomType === "PLANE" ? filletEnding(body, F, g, cy) : null; + if (corner) { + for (const pl of corner) { + if (nb.some((x) => x.geomType === "PLANE" && Math.abs(dot(planeOf(x).normal, pl.normal)) > 1 - 1e-9 && Math.abs(dot(sub(planeOf(x).origin, pl.origin), pl.normal)) < 1e-6)) continue; + keep.push(halfSpace({ origin: pl.origin, normal: scale(pl.normal, -1), xDir: xDirFor(scale(pl.normal, -1)) }, L)); + } + const wedge = halfSpace({ origin: cy.c, normal: corner[0].normal, xDir: xDirFor(corner[0].normal) }, L).intersect(halfSpace({ origin: cy.c, normal: corner[1].normal, xDir: xDirFor(corner[1].normal) }, L)); + remove.push(wedge.cut(infiniteCylinder(cy, cy.r, L))); + continue; + } const rel = sub(q, cy.c); const radial = len(sub(rel, scale(cy.a, dot(rel, cy.a)))); (radial < cy.r ? keep : remove).push(infiniteCylinder(cy, cy.r, L)); @@ -338,6 +371,8 @@ function faceColumn(body: Shape3D, F: Face, L: number): Shape3D { const lift = sizeOf(body) * 1e-3; const q0 = sub(p, scale(n, lift)); if (F.geomType === "PLANE" && convexFace(F)) return extendedRegion(body, F, q0, L); + const holed = F.geomType === "PLANE" ? framedColumn(body, F, n, lift, L) : null; + if (holed) return holed; const tol = sizeOf(body) * 1e-6; const sides = neighbours(body, F).map((g): ((q: Vec3) => number) | null => { if (g.geomType === "PLANE") { @@ -386,11 +421,79 @@ function faceColumn(body: Shape3D, F: Face, L: number): Shape3D { let E: Shape3D | null = null; for (const q of cells.values()) { const c = extendedRegion(body, F, q, L); - E = E ? E.fuse(c) : c; + E = E ? fuseCells(E, c) : c; } return E!; } +/** + * A flat face with holes whose outline and holes are each convex (a rim left + * round an opening, a plate with a window): the space its outline's + * neighbours enclose, less the space each hole's neighbours enclose. Null for + * any other face. (Cutting it into cells by every neighbour's surface and + * joining them again failed on such a rim.) + */ +function framedColumn(body: Shape3D, F: Face, n: Vec3, lift: number, L: number): Shape3D | null { + const holes = F.clone().innerWires(); + if (!holes.length) return null; + const outer = F.clone().outerWire(); + const wireFace = (w: typeof outer): Face | null => { + try { + return makeFace(w); + } catch { + return null; + } + }; + const loops = [outer, ...holes].map((w) => ({ w, f: wireFace(w) })); + if (loops.some((l) => !l.f || !convexFace(l.f))) return null; + const all = neighbours(body, F); + const along = (w: typeof outer) => all.filter((g) => g.edges.some((e) => w.edges.some((we) => we.isSame(e)))); + // A point inside a convex loop, just under F. + const inside = (w: typeof outer): Vec3 => { + const pts = w.edges.flatMap((e) => [0, 0.5].map((t) => tup(e.pointAt(t)))); + const m = scale(pts.reduce((a, b) => add(a, b), [0, 0, 0] as Vec3), 1 / pts.length); + return sub(m, scale(n, lift)); + }; + try { + let E = regionAround(body, F, along(outer), inside(outer), L); + for (const h of holes) E = E.cut(regionAround(body, F, along(h), inside(h), L)); + return isEmpty(E) ? null : E; + } catch { + return null; + } +} + +/** + * Two of a face's cells joined. They meet face to face, and the plain join + * can fail on that (a U-shaped rim across a shell's walls): then once more + * with a small tolerance, which lets the shared faces be one. + */ +function fuseCells(a: Shape3D, b: Shape3D): Shape3D { + try { + return a.fuse(b); + } catch (err) { + const oc = getOC(); + const algo = new oc.BRepAlgoAPI_Fuse(); + const args = new oc.NCollection_List_TopoDS_Shape(); + const tools = new oc.NCollection_List_TopoDS_Shape(); + try { + args.Append(a.wrapped); + tools.Append(b.wrapped); + algo.SetArguments(args); + algo.SetTools(tools); + algo.SetFuzzyValue(Math.max(1e-7, Math.max(sizeOf(a), sizeOf(b)) * 1e-6)); + algo.SetNonDestructive(true); + algo.Build(new oc.Message_ProgressRange()); + if (!algo.IsDone()) throw err; + return cast(algo.Shape()) as Shape3D; + } finally { + algo.delete(); + args.delete(); + tools.delete(); + } + } +} + /** Does a flat face's outline turn the same way at every corner (no inward corners, no holes)? */ export function convexFace(F: Face): boolean { if (F.clone().innerWires().length) return false; diff --git a/src/ui/about.ts b/src/ui/about.ts index 61b5fd9..df0a333 100644 --- a/src/ui/about.ts +++ b/src/ui/about.ts @@ -111,15 +111,11 @@ export function showAbout(): void { const box = el("div", "dialog about"); const head = el("div", "library-head"); head.append(el("div", "panel-title", "About supergeometry.app")); - const gh = el("a", "text-btn", "On GitHub"); - gh.href = REPO; - gh.target = "_blank"; - gh.rel = "noopener"; const closeBtn = el("button", "icon-btn library-close"); closeBtn.type = "button"; closeBtn.title = "Close"; closeBtn.innerHTML = icons.close; - head.append(gh, closeBtn); + head.append(closeBtn); const body = el("div", "about-body"); body.innerHTML = render(aboutText()); // The Contents links scroll within the window. diff --git a/tests/fixtures/movefillet.json b/tests/fixtures/movefillet.json new file mode 100644 index 0000000..5977986 --- /dev/null +++ b/tests/fixtures/movefillet.json @@ -0,0 +1 @@ +{"format":"super","version":2,"name":"Untitled","features":[{"id":"sketch-mw569tes-9eqj","name":"Sketch 01","type":"sketch","plane":{"origin":[0,0,0],"xDir":[1,0,0],"normal":[0,-1,0]},"entities":[{"id":"5ludb8xt","kind":"line","a":[-10,0],"b":[-9.999999999999996,50]},{"id":"mk28wv3v","kind":"line","a":[-9.999999999999996,50],"b":[-60,50.00000000000001]},{"id":"qjpky46f","kind":"line","a":[-60,50.00000000000001],"b":[-60,0]},{"id":"87e4ry1k","kind":"line","a":[-60,0],"b":[-10,0]}],"constraints":[{"id":"u8afcwye","type":"vertical","curves":["5ludb8xt"]},{"id":"pnmx28tq","type":"coincident","pts":[{"e":"5ludb8xt","p":"b"},{"e":"mk28wv3v","p":"a"}]},{"id":"laf2pdi3","type":"horizontal","curves":["mk28wv3v"]},{"id":"tzgx1ui7","type":"coincident","pts":[{"e":"mk28wv3v","p":"b"},{"e":"qjpky46f","p":"a"}]},{"id":"ng4u1dih","type":"vertical","curves":["qjpky46f"]},{"id":"9w5fe9ad","type":"coincident","pts":[{"e":"qjpky46f","p":"b"},{"e":"87e4ry1k","p":"a"}]},{"id":"muii5bt2","type":"coincident","pts":[{"e":"87e4ry1k","p":"b"},{"e":"5ludb8xt","p":"a"}]},{"id":"d0rnci4s","type":"horizontal","curves":["87e4ry1k"]}],"log":[{"op":"Line","n":4}]},{"id":"extrude-mw56abf8-v8l2","name":"Extrude 01","type":"extrude","source":{"kind":"profile","sketch":"sketch-mw569tes-9eqj","regions":[{"c":[-35,25.000000000000004],"a":2500.0000000000005}]},"distance":33.5,"mode":"auto"},{"id":"moveFace-mw56ait0-ryuw","name":"Move Face 01","type":"moveFace","face":{"body":"extrude-mw56abf8-v8l2","center":[-9.999999999999998,-16.75,25],"normal":[1,0,-7.105427357601002e-17],"area":1675},"translate":[0,0,0],"rotate":{"origin":[-9.999999999999998,-16.75,25],"axis":[0,1,0],"angle":-30}},{"id":"moveFace-mw56apcg-u2z5","name":"Move Face 02","type":"moveFace","face":{"body":"extrude-mw56abf8-v8l2","center":[-34.30555555555555,-33.5,22.594373878376565],"normal":[0,-1,0],"area":2500.000000000001},"translate":[0,0,0],"rotate":{"origin":[-34.30555555555555,-33.5,22.594373878376565],"axis":[1,0,0],"angle":30}},{"id":"moveFace-mw56ay0f-5jhf","name":"Move Edge 01","type":"moveFace","edge":{"body":"extrude-mw56abf8-v8l2","mid":[-42.21687836487031,0,50.00000000000001],"dir":[-1,0,1.4210854715202004e-16],"length":35.56624327025938},"translate":[0,18.5,0]},{"id":"fillet-mw56b1ly-gl90","name":"Fillet 01","type":"fillet","edges":[{"body":"extrude-mw56abf8-v8l2","mid":[-24.43375672974063,-15.411322809314754,50.00000000000001],"dir":[-2.6191205359177573e-16,-1,2.0952964287342059e-16],"length":67.82264561862951}],"radius":5},{"id":"moveFace-mw56bndt-p9vl","name":"Move Face 03","type":"moveFace","face":{"body":"extrude-mw56abf8-v8l2","center":[-34.31070177796434,8.354569335452299,22.579917122844055],"normal":[0,0.9378618426812279,-0.3470088817920542],"area":2664.2050022174662},"translate":[0,0,0],"rotate":{"origin":[-34.31070177796434,8.354569335452299,22.579917122844055],"axis":[-1,0,0],"angle":12}},{"id":"fillet-mw56bw0o-4v3o","name":"Fillet 02","type":"fillet","edges":[{"body":"extrude-mw56abf8-v8l2","mid":[-9.27831216351296,9.094392904224886,23.749999999999996],"dir":[-0.43855660750047387,0.48028367458274124,0.7596023261858635],"length":62.532720559859705},{"body":"extrude-mw56abf8-v8l2","mid":[-24.82050807568875,25.26827360128725,49.33012701892219],"dir":[-0.8257432937451457,0.3014365888760406,0.4767431129252887],"length":5.529472582131915},{"body":"extrude-mw56abf8-v8l2","mid":[-43.66025403784439,25.6918229319893,50],"dir":[-1,0,0],"length":32.67949192431128}],"radius":3},{"id":"moveFace-mw56c27p-5oqy","name":"Move Face 04","type":"moveFace","face":{"body":"extrude-mw56abf8-v8l2","center":[-35.92141430595347,7.092617452310803,20.584051686023404],"normal":[0,0.8452201078962631,-0.5344183466235317],"area":2520.8994263543664},"translate":[0,0,0],"rotate":{"origin":[-35.92141430595347,7.092617452310803,20.584051686023404],"axis":[-1,0,0],"angle":-48}},{"id":"moveFace-mw6q8oo5-balt","name":"Move Face 05","type":"moveFace","face":{"body":"extrude-mw56abf8-v8l2","center":[-59.99999999999999,-12.45599336973517,30.573464678026944],"normal":[-1,0,0],"area":2231.9488897382917},"translate":[0,0,0],"rotate":{"origin":[-59.99999999999999,-12.45599336973517,30.573464678026944],"axis":[0,-1,0],"angle":-18}}],"variables":[],"views":[],"units":"mm","hidden":["sketch-mw569tes-9eqj"],"pins":[],"names":{}} \ No newline at end of file diff --git a/tests/fixtures/sweeppushes.json b/tests/fixtures/sweeppushes.json new file mode 100644 index 0000000..b1c9fb5 --- /dev/null +++ b/tests/fixtures/sweeppushes.json @@ -0,0 +1 @@ +{"format":"super","version":2,"name":"Sweep pushes","features":[{"id":"sketch-mw6p62j2-zkxr","name":"Sketch 02","type":"sketch","plane":{"origin":[0,0,0],"xDir":[0,1,0],"normal":[1,0,0]},"entities":[{"id":"x8j9jgav","kind":"spline","points":[[88,45],[61,76],[87.39225610881964,113.40026703073036],[58.00019999681631,160.00015555382723]],"closed":false,"mode":"fit"}],"log":[{"op":"Spline","n":1},{"op":"Edit","n":4}]},{"id":"plane-mw6p6vgj-kpa4","name":"Plane on Curve 01","type":"plane","kind":"curve","plane":{"origin":[0,88,45],"normal":[0,-0.6793415449133355,0.7338222300767145],"xDir":[1,0,0]},"offset":0},{"id":"sketch-mw6p7ecz-g926","name":"Sketch 03","type":"sketch","plane":{"origin":[0,88,45],"normal":[0,-0.6793415449133355,0.7338222300767145],"xDir":[1,0,0]},"entities":[{"id":"5sk0gblt","kind":"circle","center":[0,-2.34],"radius":2.5}],"log":[{"op":"Circle","n":1}]},{"id":"sweep-mw6p7ni3-sy2n","name":"Sweep 01","type":"sweep","source":{"kind":"profile","sketch":"sketch-mw6p7ecz-g926","regions":[{"c":[-5.2811344478160123e-17,-2.34],"a":19.62093876904292}]},"path":{"kind":"sketch","sketch":"sketch-mw6p62j2-zkxr","entities":["x8j9jgav"]},"mode":"auto"},{"id":"extrude-mw6pjvwz-ijh8","name":"Offset Face 03","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-4.917177776064822e-15,86.28285598162046,43.4103407849028],"normal":[1.7134265996619277e-14,0.6567807448042061,-0.7540815958863021],"area":19.634954081988045}},"distance":-4,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6qg03t-3hcm","name":"Offset Face 04","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-1.4155343563970829e-15,56.05975781906323,158.6923772259762],"normal":[-6.762003933128444e-14,-0.5334810012630209,0.845812048443036],"area":19.634954081987942}},"distance":-10.9,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6qh5l7-edec","name":"Offset Face 05","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-1.0499729169851643e-9,83.62489596282964,46.22370327243816],"normal":[1.5637167615485535e-16,0.7186785482251644,-0.6953424654952192],"area":19.635653021480103}},"distance":-18.400000000000002,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6qhgew-cw31","name":"Offset Face 06","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-2.1588229866933183e-9,70.1765438185329,58.78579783500034],"normal":[4.05203504840482e-15,0.7186785482251604,-0.6953424654952234],"area":14.791006276862927}},"distance":-6.4,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6qik27-jff1","name":"Offset Face 07","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[1.5363720589622422e-9,62.56723772213059,149.70505799101187],"normal":[-4.073926954848786e-15,-0.6280549996812033,0.7781689516907262],"area":19.63547000902378}},"distance":-10.5,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6r9w6c-rsag","name":"Offset Face 08","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-6.093350279759103e-10,69.25358467715617,141.56075796340758],"normal":[-3.057313129744463e-15,-0.6404968893783877,0.7679607637741719],"area":19.63481076329073}},"distance":-20.5,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6ra16n-ydmb","name":"Offset Face 09","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-5.780823033823249e-10,81.22958750430324,125.45050365203369],"normal":[-1.5887174746623638e-15,-0.4994939362202074,0.8663173827641019],"area":19.63477284891142}},"distance":-6.5,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6ra2dp-xqrk","name":"Offset Face 10","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[3.012906750679475e-12,83.84312183698555,119.99032837486163],"normal":[-1.1026772174136642e-14,-0.32899684588525624,0.9443310200335225],"area":19.63718957106897}},"distance":-8.5,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6ra3mq-hnwc","name":"Offset Face 11","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-6.35640471318342e-10,83.97889483210909,119.60990384649041],"normal":[2.863970521722694e-16,-0.33470278389924424,0.9423237482150686],"area":19.63517181293865}},"distance":-4,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6ra55l-dva3","name":"Offset Face 12","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[2.6643879830602964e-10,85.01527134873632,112.85870668942809],"normal":[1.5858447534010204e-14,0.1373979821644283,0.9905159233940378],"area":19.647999482766583}},"distance":-7.5,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6ra6lk-mcpn","name":"Offset Face 13","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[2.6643879830602964e-10,85.01527134873632,112.85870668942809],"normal":[1.5858447534010204e-14,0.1373979821644283,0.9905159233940378],"area":19.647999482766583}},"distance":-3.5,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6ra9sm-8fv2","name":"Offset Face 14","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[1.4615649971555142e-12,84.38346560028593,110.3151436950435],"normal":[3.5493395713495472e-15,0.37407855846926363,0.9273970196704094],"area":19.63742450103932}},"distance":-9,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6racmt-w9i0","name":"Offset Face 15","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-4.483953820412249e-10,79.91047382514044,103.49014067396655],"normal":[9.4081730236539e-15,0.6634384743697905,0.7482308405337785],"area":19.635637554241384}},"distance":-10.5,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6raeh4-zbyd","name":"Offset Face 16","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-3.481094965408405e-10,72.82445999440901,96.34900113706071],"normal":[6.609837189796811e-15,0.7224285220213498,0.6914456092636991],"area":19.63563805317719}},"distance":-10,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6rajue-9mzg","name":"Offset Face 17","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-6.28500571448134e-10,65.19340962108421,63.018096535109045],"normal":[-4.76544106369694e-15,0.6733495657740495,-0.7393242605730582],"area":19.634954546087272}},"distance":-12,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6ram82-x4bw","name":"Offset Face 18","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[5.309956665900157e-12,58.79022105744781,74.6083662175401],"normal":[-3.174174050462372e-15,0.12388938221879059,-0.9922960349479617],"area":19.63953914700263}},"distance":-6.4,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6rap55-whgp","name":"Offset Face 19","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[5.309956665900157e-12,58.79022105744781,74.6083662175401],"normal":[-3.174174050462372e-15,0.12388938221879059,-0.9922960349479617],"area":19.63953914700263}},"distance":-1.5,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6rari7-tg5z","name":"Offset Face 20","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-4.3845575914058766e-10,65.56056897197313,89.28313752827536],"normal":[2.7557145660923668e-15,0.6812689061607174,0.732033248902384],"area":19.635107500291117}},"distance":-4.5,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"},{"id":"extrude-mw6rat2p-b5bq","name":"Offset Face 21","type":"extrude","source":{"kind":"face","face":{"body":"sweep-mw6p7ni3-sy2n","center":[-5.627551849532562e-10,62.759967384895944,86.05959332721542],"normal":[4.206729507437087e-15,0.6187812668004853,0.7855633289918683],"area":19.634805978737123}},"distance":-3.6,"mode":"auto","target":"sweep-mw6p7ni3-sy2n"}],"variables":[],"views":[],"units":"mm","hidden":[],"pins":[],"names":{}} diff --git a/tests/movefillet.test.ts b/tests/movefillet.test.ts new file mode 100644 index 0000000..01adb8c --- /dev/null +++ b/tests/movefillet.test.ts @@ -0,0 +1,36 @@ +// Run: npx tsx tests/movefillet.test.ts - a user's part (tests/fixtures/ +// movefillet.json): a box with faces tilted, an edge moved, a fillet and a +// chain of R3 fillets, then the left wall tilted 18° (Move Face 05). The R3 +// fillet ends on that wall and can't be taken off alone (it's one link of a +// chain), so the wall's region was made with the fillet's cylinder as an +// endless one: it carved a groove and left a scrap of the old wall standing +// (12 faces: an inside-out cylinder and a 28 mm² patch at x = -60). Now the +// fillet carries on along its own corner and simply ends on the tilted wall. +import opencascade from "replicad-opencascadejs"; +import { measureArea, setOC } from "replicad"; +import { readFileSync } from "node:fs"; +import { debugBodies, regenerate } from "../src/kernel/engine"; +import { healthOf, isHealthy } from "../src/kernel/repair"; + +setOC(await (opencascade as unknown as () => Promise)()); +let failed = 0; +const check = (name: string, ok: boolean, info = "") => { + if (!ok) failed++; + console.log(ok ? "ok " : "FAIL", name, info); +}; +const doc = JSON.parse(readFileSync(new URL("./fixtures/movefillet.json", import.meta.url), "utf8")); +const B = "extrude-mw56abf8-v8l2"; +const r: any = await regenerate(doc.features); +const name = (id: string) => doc.features.find((f: any) => f.id === id)?.name; +check("only Move Face 04 is refused (a fillet chain runs along that face), with a clear reason", r.errors.length === 1 && name(r.errors[0].featureId) === "Move Face 04" && /fillet/.test(r.errors[0].message), r.errors.map((e: any) => `${name(e.featureId)}: ${e.message}`).join("; ")); +const s = debugBodies().get(B)!; +check("one clean solid", isHealthy(healthOf(s))); +const n = (f: any) => f.normalAt().toTuple(); +const oldWall = s.faces.filter((f) => f.geomType === "PLANE" && Math.abs(n(f)[0] + 1) < 1e-6); +check("no scrap of the old left wall left standing", oldWall.length === 0, oldWall.map((f) => measureArea(f).toFixed(1)).join(", ")); +const wall = s.faces.filter((f) => f.geomType === "PLANE" && Math.abs(n(f)[0] + Math.cos(Math.PI / 10)) < 1e-6 && Math.abs(n(f)[2] - Math.sin(Math.PI / 10)) < 1e-6); +check("the left wall is one face, tilted 18°", wall.length === 1, wall.map((f) => measureArea(f).toFixed(1)).join(", ")); +const cyl = s.faces.filter((f) => f.geomType === "CYLINDRE"); +check("the same three rounded faces as before the tilt (no groove)", s.faces.length === 10 && cyl.length === 3, `${s.faces.length} faces, round: ${cyl.map((f) => measureArea(f).toFixed(1)).join(", ")}`); +console.log(failed ? `${failed} FAILED` : "all passed"); +process.exit(failed ? 1 : 0); diff --git a/tests/rimtilt.test.ts b/tests/rimtilt.test.ts new file mode 100644 index 0000000..a35859f --- /dev/null +++ b/tests/rimtilt.test.ts @@ -0,0 +1,45 @@ +// Run: npx tsx tests/rimtilt.test.ts - Move Face tilting the rim a Shell +// leaves round its opening (a user's box: filleted, chamfered, shelled open on +// the chamfer, the rim pushed in twice). The rim is a flat face with a hole; +// tilting it failed at any angle (its region was cut into 11 cells by every +// neighbour's surface, and joining them failed). Now: its outline's region +// less its hole's. +import opencascade from "replicad-opencascadejs"; +import { measureArea, measureVolume, setOC } from "replicad"; +import { debugBodies, regenerate } from "../src/kernel/engine"; +import { healthOf, isHealthy } from "../src/kernel/repair"; + +setOC(await (opencascade as unknown as () => Promise)()); +let failed = 0; +const check = (name: string, ok: boolean, info = "") => { + if (!ok) failed++; + console.log(ok ? "ok " : "FAIL", name, info); +}; +const rect = { id: "sk", name: "Sketch 01", type: "sketch", plane: { origin: [0, 0, 0], xDir: [0, 1, 0], normal: [1, 0, 0] }, entities: [{ id: "r", kind: "rect", center: [20, 25], w: 40, h: 50, rotation: 0 }] }; +const B = "box"; +const s2 = Math.SQRT1_2; +const base: any[] = [ + rect, + { id: B, name: "Extrude 01", type: "extrude", source: { kind: "profile", sketch: "sk", regions: [{ c: [20, 25], a: 2000 }] }, distance: 28, mode: "auto" }, + { id: "f1", name: "Fillet 01", type: "fillet", edges: [{ body: B, mid: [14, 40, 50], dir: [1, 0, 0], length: 28 }], radius: 20 }, + { id: "c1", name: "Chamfer 01", type: "chamfer", edges: [{ body: B, mid: [14, 0, 50], dir: [1, 0, 0], length: 28 }], radius: 14 }, + { id: "s1", name: "Shell 01", type: "shell", body: B, faces: [{ body: B, center: [14, 7, 43], normal: [0, -s2, s2], area: 554.37 }], thickness: 5 }, + { id: "o1", name: "Offset Face 01", type: "extrude", source: { kind: "face", face: { body: B, center: [14, 7, 43], normal: [0, -s2, s2], area: 452.548 } }, distance: -3, mode: "auto", target: B }, + { id: "o2", name: "Offset Face 02", type: "extrude", source: { kind: "face", face: { body: B, center: [14, 9.1213, 40.8787], normal: [0, -s2, s2], area: 512.548 } }, distance: -9, mode: "auto", target: B }, +]; +const rim = { body: B, center: [14, 13.960626616153542, 32.99006386767639], normal: [0, -s2, s2], area: 632.652 }; +await regenerate(base); +const v0 = Math.abs(measureVolume(debugBodies().get(B)!)); +for (const angle of [14, -10, 3]) { + const r: any = await regenerate([...base, { id: "m1", name: "Move Face 01", type: "moveFace", face: rim, translate: [0, 0, 0], rotate: { origin: rim.center, axis: [1, 0, 0], angle } }]); + const s = debugBodies().get(B)!; + const a = (angle * Math.PI) / 180; + const want = [0, -s2 * Math.cos(a) - s2 * Math.sin(a), -s2 * Math.sin(a) + s2 * Math.cos(a)]; + const tilted = s.faces.filter((f) => f.geomType === "PLANE" && f.normalAt().toTuple().every((x, i) => Math.abs(x - want[i]) < 1e-6)); + const v = Math.abs(measureVolume(s)); + check(`rim tilted ${angle}°: builds, one clean solid`, !r.errors.length && isHealthy(healthOf(s)), r.errors.map((e: any) => e.message).join("; ")); + check("…the rim is one face at the new angle, still with its opening", tilted.length === 1 && tilted[0].clone().innerWires().length === 1, tilted.map((f) => measureArea(f).toFixed(1)).join(", ")); + check("…the walls just follow (volume within 10%)", Math.abs(v - v0) < v0 * 0.1, `${v0.toFixed(0)} → ${v.toFixed(0)}`); +} +console.log(failed ? `${failed} FAILED` : "all passed"); +process.exit(failed ? 1 : 0); diff --git a/tests/sweeppushes.test.ts b/tests/sweeppushes.test.ts new file mode 100644 index 0000000..fbaeb54 --- /dev/null +++ b/tests/sweeppushes.test.ts @@ -0,0 +1,54 @@ +// Run: npx tsx tests/sweeppushes.test.ts - a user's S-curve tube whose ends +// were pushed in again and again (Offset Face on a sweep's end, 19 times; +// tests/fixtures/sweeppushes.json). Pushes on the bend failed: the end's plane +// cuts an S-shaped path in more than one place and only the first was tried, +// and the cutting disc was far wider than the tube (on a tight bend it folded +// over itself, or bit into the tube's next turn: "break into pieces"). +import opencascade from "replicad-opencascadejs"; +import { measureArea, measureVolume, setOC } from "replicad"; +import { readFileSync } from "node:fs"; +import { debugBodies, regenerate } from "../src/kernel/engine"; +import { healthOf, isHealthy } from "../src/kernel/repair"; + +setOC(await (opencascade as unknown as () => Promise)()); +let failed = 0; +const check = (name: string, ok: boolean, info = "") => { + if (!ok) failed++; + console.log(ok ? "ok " : "FAIL", name, info); +}; +const doc = JSON.parse(readFileSync(new URL("./fixtures/sweeppushes.json", import.meta.url), "utf8")); +const BODY = "sweep-mw6p7ni3-sy2n"; +const A = Math.PI * 2.5 * 2.5; +const name = (id: string) => doc.features.find((f: any) => f.id === id)?.name; +const tube = () => { + const s = debugBodies().get(BODY)!; + const ends = s.faces.filter((f) => f.geomType === "PLANE"); + return { s, length: Math.abs(measureVolume(s)) / A, round: ends.length === 2 && ends.every((f) => Math.abs(measureArea(f) - A) < A * 2e-3), ends: ends.map((f) => measureArea(f).toFixed(2)).join(", ") }; +}; + +// As the user saw it: the pushes that failed then (11, 12, 18, 20) left out. +{ + const skip = new Set(["Offset Face 11", "Offset Face 12", "Offset Face 18", "Offset Face 20"]); + const r: any = await regenerate(doc.features.filter((f: any) => !skip.has(f.name))); + const t = tube(); + check("as the user built it: every push builds", !r.errors.length, r.errors.map((e: any) => `${name(e.featureId)}: ${e.message}`).join("; ")); + check("…one clean tube, both ends round", isHealthy(healthOf(t.s)) && t.round, `${t.length.toFixed(2)} long; ends ${t.ends}`); +} + +// Everything: 11 and 12 build now; by 18 the tube is shorter than the push (an honest error). +{ + const t0 = performance.now(); + const r: any = await regenerate(doc.features); + const ms = performance.now() - t0; + const failedNames = r.errors.map((e: any) => name(e.featureId)); + check("all on: Offset Face 11 and 12 build (they're on the bend)", !failedNames.includes("Offset Face 11") && !failedNames.includes("Offset Face 12"), failedNames.join(", ")); + check("…no kernel failures, only pushes past what's left", r.errors.every((e: any) => /remove/.test(e.message)), r.errors.map((e: any) => `${name(e.featureId)}: ${e.message}`).join("; ")); + check("…19 pushes rebuild in reasonable time", ms < 12000, `${Math.round(ms)} ms`); + const upto = doc.features.slice(0, doc.features.findIndex((f: any) => f.name === "Offset Face 17") + 1); + await regenerate(upto); + const t = tube(); + check("…up to 17 the tube is 3 mm long (18 pushes 6.4)", t.length < 6.4 && t.round, `${t.length.toFixed(2)}; ends ${t.ends}`); +} + +console.log(failed ? `${failed} FAILED` : "all passed"); +process.exit(failed ? 1 : 0);