dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / shell_tests.luc
## Shell, checked against closed forms: a box opened at its top with the
## wall inside, outside and both ways (half each, and Inside and Outside thicknesses apart); a closed void; a cylinder cup; a box
## with rounded uprights (its inner fillets the radius less the wall); a
## wall thicker than the rounds, where they vanish from the hollow: it is
## the hollow of the body with them healed sharp (refused before); a cube
## rounded on every edge opened at its top with a wall thinner than the
## rounds (the closed shell opened square to the top); bodies that are not
## convex (an L prism, Fusion 2705's probe 15): sharp, its hollow the L
## moved in (Fusion's 3504 closed, 3192 open); its inside upright and an
## outer one rounded 1, shelled 2, the outer round swallowed and the inside
## one grown to 3 (6000 less the moved-in L and that round's region,
## 3473.0975; Fusion's 3473.0973); its top edges and inside upright rounded
## 1 (Fusion's 3460.7174); rounded 3 everywhere, shelled 1 open (Fusion's 1570.7683);
## rounded 1 everywhere (its rounds the wall swallows beside an inside one,
## which Delete cannot heal together) and one long top edge chamfered 1,
## shelled 3 (the chamfer swallowed): the cells of the L's planes, kept
## where the L is, moved in, the rounds made again (shell_cells.lucb):
## Fusion's 3423.3006 closed, 3107.4377 open (its hollow's top swept out
## through the wall), 3436.1456 at G2 (the inside round its strip's offset)
## and 4646.
from luce_cad.modeling import CadModeling
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_geocore.geocore import Vector3
from checks import manifold, closed, volume
from luce_std import math
func solid(model: CadModel, expected: float, tolerance: float, what: str) -> unit!:
let mesh = model.tessellate(32)
manifold(mesh)
let v = volume(mesh)
if math.abs(v - expected) > tolerance:
print(f"{what}: volume {v}, expected {expected}")
assert(math.abs(v - expected) <= tolerance)
## The name of `model`'s flat face facing +Y.
func top_name(model: CadModel) -> str!:
for f in 0..<model.face_count():
let name = CadModeling.face_name_of(model, f)
let frame = CadModeling.face_frame(model, name)
if frame.found and frame.u.cross(frame.v).y > 0.99:
return name
assert(false, "no top")
return ""
## References to `box`'s four upright edges.
func uprights(box: CadModel) -> str!:
let found: list[int] = []
for face in 0..<box.face_count():
for use in 0..<box.face_edge_count(face):
let id = box.face_edge_id(face, use)
let a = box.face_edge_point(face, use, 0.0)
let b = box.face_edge_point(face, use, 1.0)
if box.face_edge_linear(face, use) and math.abs(a.x - b.x) < 1e-9 and math.abs(a.z - b.z) < 1e-9 and not (id in found):
found.append(id)
return CadModeling.edge_references(box, found)
pub func shell_tests() -> unit!:
let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "box")
let top = top_name(box)
# Inside: the 1.8 x 1.9 x 1.8 hollow open at the top.
let inside = CadModeling.shelled(box, top, 0.1, "s")
solid(inside, 8.0 - 1.8 * 1.9 * 1.8, 1e-5, "inside")
assert(inside.face_count() == 11)
var inner_named = false
for f in 0..<inside.face_count():
if CadModeling.face_name_of(inside, f).starts_with("s/box/"):
inner_named = true
assert(inner_named)
# Outside: a 2.2 x 2.1 x 2.2 body less the box; both ways: 2.1 x 2.05
# x 2.1 less 1.9 x 1.95 x 1.9.
solid(CadModeling.shelled(box, top, 0.1, "s", 1), 2.2 * 2.1 * 2.2 - 8.0, 1e-5, "outside")
solid(CadModeling.shelled(box, top, 0.1, "s", 2), 2.1 * 2.05 * 2.1 - 1.9 * 1.95 * 1.9, 1e-5, "both")
# Both, Fusion's Inside 0.1 and Outside 0.2: 2.4 x 2.2 x 2.4 less 1.8 x
# 1.9 x 1.8.
solid(CadModeling.shelled(box, top, 0.1, "s", 2, outside_thickness = 0.2), 2.4 * 2.2 * 2.4 - 1.8 * 1.9 * 1.8, 1e-5, "both, inside and outside apart")
# Nothing removed: a closed void, two shells.
solid(CadModeling.shelled(box, "", 0.1, "s"), 8.0 - 1.8 * 1.8 * 1.8, 1e-5, "closed")
# A cylinder of radius 1, 2 high, opened at its top: a cup.
let can = CadModeling.tagged(CadSolids.cylinder(1.0, 2.0), "can")
solid(CadModeling.shelled(can, top_name(can), 0.1, "s"), math.pi * 2.0 - math.pi * 0.81 * 1.9, 0.01, "cup")
# Rounded uprights of 0.3: the hollow's are 0.2.
let rounded = CadModeling.filleted(box, uprights(box), 0.3, "f")
let corner_out = 0.09 - math.pi * 0.09 * 0.25
let corner_in = 0.04 - math.pi * 0.04 * 0.25
let outer = 8.0 - 8.0 * corner_out
let hollow = (1.8 * 1.8 - 4.0 * corner_in) * 1.9
solid(CadModeling.shelled(rounded, top_name(rounded), 0.1, "s"), outer - hollow, 0.01, "rounded box")
# A wall thicker than the fillets' radius: the rounds vanish from the
# hollow, which is the sharp box's (1 x 1.5 x 1 within the body).
solid(CadModeling.shelled(rounded, top_name(rounded), 0.5, "s"), outer - 1.5, 0.01, "rounded box, wall past its rounds")
# A 20 box rounded 1 on every edge (cylinders and corner balls), shelled
# 2: its hollow is the sharp 16 box, closed and opened at its top.
let cube = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "c")
let all_round = CadModeling.filleted(cube, "", 1.0, "f")
let core = 18.0
let whole = core * core * core + 6.0 * core * core + 3.0 * math.pi * core + 4.0 * math.pi / 3.0
for (faces, hollow_volume, what) in [("", 4096.0, "closed"), (top_name(all_round), 4608.0, "open")]:
let shelled = CadModeling.shelled(all_round, faces, 2.0, "s")
let coarse = volume(shelled.tessellate(32))
let mesh = shelled.tessellate(64)
manifold(mesh)
let v = volume(mesh) + (volume(mesh) - coarse) / 3.0
if math.abs(v - (whole - hollow_volume)) > 0.01: print(f"rounded cube shelled 2, {what}: {v} against {whole - hollow_volume}")
assert(math.abs(v - (whole - hollow_volume)) <= 0.01)
# Its top with Tangent chain takes every face (it is smooth all over):
# refused, not an empty body.
var refused = false
_ = CadModeling.shelled(all_round, top_name(all_round), 2.0, "s", 0, true) catch failure:
refused = failure.message.contains("Tangent chain")
recover all_round
assert(refused)
# A wall thinner than the rounds, opened at the top: the rounds' inner
# offsets (radius 2) never reach the top's plane, so the closed shell is
# opened square to the top (Limits 9; refused before): the cube rounded
# 3 less the 18 cube rounded 2 less the 14 x 14 slab of the top, 1 thick.
let thick_round = CadModeling.filleted(cube, "", 3.0, "f")
let steiner = func (a: float, r: float) -> float:
let c = a - 2.0 * r
return c * c * c + 6.0 * r * c * c + 3.0 * math.pi * r * r * c + 4.0 * math.pi * r * r * r / 3.0
let opened = CadModeling.shelled(thick_round, top_name(thick_round), 1.0, "s")
let coarse_open = volume(opened.tessellate(32))
let open_mesh = opened.tessellate(64)
manifold(open_mesh)
let open_volume = volume(open_mesh) + (volume(open_mesh) - coarse_open) / 3.0
let expected_open = steiner(20.0, 3.0) - steiner(18.0, 2.0) - 14.0 * 14.0
if math.abs(open_volume - expected_open) > 1e-3: print(f"rounded cube shelled 1, open: {open_volume} against {expected_open}")
assert(math.abs(open_volume - expected_open) <= 1e-3)
swallowed()
not_convex()
print("PASS shell: a box opened at its top, the wall inside, outside and both ways (half each, and Inside and Outside thicknesses apart); a closed void; a cylinder cup; a box with rounded uprights; walls thicker than the rounds (the rounds vanish from the hollow), a cube rounded all over shelled closed and open (its whole tangent chain refused); a cube rounded 3 shelled 1 open at its top, the rim square to it; G2 and G3 strips, G2 corners and a chamfer swallowed by the wall; an L prism sharp, with an inside and an outer round (one swallowed, one grown), rounded 3 opened (Fusion's); rounded 1 all over (G1 closed and open, G2 closed) and chamfered, shelled by the cells of its planes, Fusion's volumes")
## Faces an inset swallows beyond analytic rounds (the SMLib study's item
## 11): a 20 cube's uprights rounded 3 G2 and G3, shelled 4 closed and open
## at its top, its hollow the sharp 12 x 12 x 12 (and 12 x 16 x 12 open)
## box; the cube rounded 3 G2 on every edge (setback corners Delete cannot
## heal) shelled 4 closed, the convex cell of its planes; a cube chamfered 1
## on one edge shelled 3, the chamfer gone from the 14 cube hollow.
func swallowed() -> unit!:
let cube = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "c")
let upright_edges: list[int] = []
for f in 0..<cube.face_count():
for use in 0..<cube.face_edge_count(f):
let a = cube.face_edge_point(f, use, 0.0)
let b = cube.face_edge_point(f, use, 1.0)
let id = cube.face_edge_id(f, use)
if math.abs(a.x - b.x) < 1e-9 and math.abs(a.z - b.z) < 1e-9 and not (id in upright_edges): upright_edges.append(id)
let edges = CadModeling.edge_references(cube, upright_edges)
for continuity in [2, 3]:
let rounded = CadModeling.filleted(cube, edges, 3.0, "f", continuity)
let outer = volume(rounded.tessellate(32))
for (faces, hollow) in [("", 1728.0), (top_name(rounded), 2304.0)]:
let shelled = CadModeling.shelled(rounded, faces, 4.0, "s")
let mesh = shelled.tessellate(32)
manifold(mesh)
assert(math.abs(volume(mesh) - (outer - hollow)) < 1e-6, f"G{continuity} uprights shelled 4: {volume(mesh)} against {outer - hollow}")
let all_g2 = CadModeling.filleted(cube, "", 3.0, "f", 2)
let closed_g2 = CadModeling.shelled(all_g2, "", 4.0, "s")
let g2_mesh = closed_g2.tessellate(32)
manifold(g2_mesh)
assert(math.abs(volume(g2_mesh) - (volume(all_g2.tessellate(32)) - 1728.0)) < 1e-6, "every edge G2 shelled 4")
let chamfered = CadModeling.chamfered(cube, edges.split(";")[0], 1.0, "k")
solid(CadModeling.shelled(chamfered, "", 3.0, "s"), 7990.0 - 2744.0, 1e-6, "a chamfer swallowed")
# A wall thicker than half the body leaves no hollow: refused, never
# the body unchanged.
var refused = false
_ = CadModeling.shelled(all_g2, "", 11.0, "s") catch failure:
refused = failure.message.contains("too large")
recover all_g2
assert(refused, "a shell thicker than the body is refused")
## The L prism (a 20 cube less a 10 x 10 quarter along y) shelled (the
## file's header).
func not_convex() -> unit!:
let cube = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "c")
let notch = CadModeling.moved_body(CadModeling.tagged(CadSolids.box(10.0, 22.0, 10.0), "n"), Vector3(5.0, 0.0, 5.0))
let l = CadModeling.combined(cube, notch, 1)
solid(CadModeling.shelled(l, "", 2.0, "s"), 3504.0, 1e-6, "the L shelled 2 closed")
solid(CadModeling.shelled(l, top_name(l), 2.0, "s"), 3192.0, 1e-6, "the L shelled 2 open")
# Its inside upright (x 0, z 0) and the far outer one (x -10, z -10).
let uprights_both = edges_where(l, func (a: Vector3, b: Vector3) -> bool:
let inside = math.abs(a.x) < 1e-9 and math.abs(b.x) < 1e-9 and math.abs(a.z) < 1e-9 and math.abs(b.z) < 1e-9
let far = math.abs(a.x + 10.0) < 1e-9 and math.abs(b.x + 10.0) < 1e-9 and math.abs(a.z + 10.0) < 1e-9 and math.abs(b.z + 10.0) < 1e-9
return inside or far)
let two = CadModeling.filleted(l, uprights_both, 1.0, "f", 1, false)
let shelled_two = CadModeling.shelled(two, "", 2.0, "s")
let two_coarse = volume(shelled_two.tessellate(32))
let two_mesh = shelled_two.tessellate(64)
closed(two_mesh)
let two_volume = volume(two_mesh) + (volume(two_mesh) - two_coarse) / 3.0
let expected_two = 6000.0 - 2496.0 - 16.0 * 9.0 * (1.0 - math.pi / 4.0)
if math.abs(two_volume - expected_two) > 2e-3: print(f"the L's two uprights rounded 1, shelled 2: {two_volume}")
assert(math.abs(two_volume - expected_two) <= 2e-3)
# Its six top edges and inside upright rounded 1 (mixed corners where
# they meet), shelled 2: the top rounds swallowed, the inside one grown
# (Fusion's 3460.717389, 25 faces: probe 16).
let tops = edges_where(l, func (a: Vector3, b: Vector3) -> bool:
let top = math.abs(a.y - 10.0) < 1e-9 and math.abs(b.y - 10.0) < 1e-9
let inside = math.abs(a.x) < 1e-9 and math.abs(b.x) < 1e-9 and math.abs(a.z) < 1e-9 and math.abs(b.z) < 1e-9
return top or inside)
let topped = CadModeling.shelled(CadModeling.filleted(l, tops, 1.0, "f", 1, false), "", 2.0, "s")
assert(topped.face_count() == 25, f"{topped.face_count()} faces")
let tops_coarse = volume(topped.tessellate(32))
let tops_mesh = topped.tessellate(64)
closed(tops_mesh)
let tops_volume = volume(tops_mesh) + (volume(tops_mesh) - tops_coarse) / 3.0
if math.abs(tops_volume - 3460.717389) > 5e-3: print(f"the L's top edges and inside upright rounded 1, shelled 2: {tops_volume}")
assert(math.abs(tops_volume - 3460.717389) <= 5e-3)
let every = edges_where(l, func (a: Vector3, b: Vector3) -> bool:
return true)
let r3 = CadModeling.filleted(l, every, 3.0, "f", 1, false)
let open3 = CadModeling.shelled(r3, top_name(r3), 1.0, "s")
let coarse = volume(open3.tessellate(32))
# (Closed: the thin rounds' display slivers may face either way.)
let fine_mesh = open3.tessellate(64)
closed(fine_mesh)
let v = volume(fine_mesh) + (volume(fine_mesh) - coarse) / 3.0
if math.abs(v - 1570.7682821) > 1e-3: print(f"the L rounded 3 shelled 1 open: {v}")
assert(math.abs(v - 1570.7682821) <= 1e-3)
# Rounded 1 on every edge (G1 and G2), shelled 2, and one long top edge
# chamfered 1, shelled 3: the cells of their planes (shell_cells.lucb),
# Fusion's probe 15 (its faces 47, 46 and 17; the G2 one's corners are
# three faces each here, one in Fusion).
let r1 = CadModeling.filleted(l, every, 1.0, "f", 1, false)
fusion_shell(r1, "", 2.0, 47, 3423.3005606, 0.01, "the L rounded 1 shelled 2 closed")
fusion_shell(r1, top_name(r1), 2.0, 46, 3107.4377276, 0.01, "the L rounded 1 shelled 2 open")
let g2 = CadModeling.filleted(l, every, 1.0, "f", 2, false)
fusion_shell(g2, "", 2.0, 67, 3436.1455838, 0.25, "the L rounded 1 G2 shelled 2 closed")
let long_top = edges_where(l, func (a: Vector3, b: Vector3) -> bool:
return a.y > 9.99 and b.y > 9.99 and a.z < -9.99 and b.z < -9.99)
let chamfered = CadModeling.chamfered(l, long_top, 1.0, "ch")
fusion_shell(chamfered, "", 3.0, 17, 4646.0, 1e-6, "the L chamfered 1 shelled 3 closed")
## `body` shelled by `wall` (`top` opened, or closed), `faces` faces, its
## volume within `within` of Fusion's `fusion`.
func fusion_shell(body: CadModel, top: str, wall: float, faces: int, fusion: float, within: float, label: str) -> unit!:
let made = CadModeling.shelled(body, top, wall, "s")
assert(made.face_count() == faces, f"{label}: {made.face_count()} faces")
let coarse = volume(made.tessellate(32))
let mesh = made.tessellate(64)
closed(mesh)
let v = volume(mesh) + (volume(mesh) - coarse) / 3.0
assert(math.abs(v - fusion) <= within, f"{label}: {v} against Fusion's {fusion}")
## References to the edges of `model` whose ends `keep` takes.
func edges_where(model: CadModel, keep: func(Vector3, Vector3) -> bool) -> str!:
let found: list[int] = []
for face in 0..<model.face_count():
for use in 0..<model.face_edge_count(face):
let id = model.face_edge_id(face, use)
if keep(model.face_edge_point(face, use, 0.0), model.face_edge_point(face, use, 1.0)) and not (id in found):
found.append(id)
return CadModeling.edge_references(model, found)