dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / face_parity_tests.luc
## Fusion's direct modeling beyond one face at a time (CadDirect): Tangent
## chain, Shell's Tangent chain, copies of faces as a surface body, Split
## Face, Replace Face onto several targets and Delete healing several
## strips and corners at once; each checked closed and of the volume
## geometry gives, or refused with why.
from luce_cad.modeling import CadModeling, CadDirect
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_cad.sketch import CadSketch
from luce_geocore.geocore import Vector3
from checks import manifold, 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 whose outward normal is `normal`.
func flat(model: CadModel, normal: Vector3) -> 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).dot(normal) > 0.999:
return name
assert(false, "no such face")
return ""
## References to `box`'s straight edges whose ends both satisfy `keep`
## (by their coordinates).
func edges_where(box: CadModel, keep: func(Vector3, Vector3) -> bool) -> 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)
if keep(box.face_edge_point(face, use, 0.0), box.face_edge_point(face, use, 1.0)) and not (id in found):
found.append(id)
return CadModeling.edge_references(box, found)
## The names of `model`'s faces whose name contains `part`, apart by `;`.
func named(model: CadModel, part: str) -> str!:
let found: list[str] = []
for f in 0..<model.face_count():
let name = CadModeling.face_name_of(model, f)
if name.contains(part) and not (name in found):
found.append(name)
return found.join(";")
pub func face_parity_tests() -> unit!:
chains()
copies()
splits()
deletes()
replaces()
print("PASS direct modeling parity: a box's top replaced by a taller box's split top (two faces, one plane) and by a tilted block's two under faces (a valley); a can's wall onto a wider one with its tangent chain; a box's rounded top chain deleted whole, two chamfers at once, an inside fillet and an outside chamfer of an L-bracket together, all healed back; a top and a bottom refused; a side's tangent chain over a rounded upright, a shell opened along it; faces copied as a surface body; a box's top split by a plane and by a pin's wall into pieces named apart, split again; a tool that misses or lies on the face refused")
## A 2-box with one upright (x = 1, z = 1) rounded 0.3: the +X side's
## tangent chain is it, the strip and the +Z side. Shell with Tangent chain
## opens all three, as naming them does;
## without it the side alone cannot open.
func chains() -> unit!:
let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "box")
let corner = edges_where(box, func (a: Vector3, b: Vector3) -> bool: return math.abs(a.x - 1.0) < 1e-9 and math.abs(b.x - 1.0) < 1e-9 and math.abs(a.z - 1.0) < 1e-9 and math.abs(b.z - 1.0) < 1e-9)
let rounded = CadModeling.filleted(box, corner, 0.3, "f")
let side = flat(rounded, Vector3(1.0, 0.0, 0.0))
let chain = CadDirect.tangent_chain(rounded, side)
assert(chain.split(";").length == 3)
assert(chain.contains(named(rounded, "f/blend")) and chain.contains(flat(rounded, Vector3(0.0, 0.0, 1.0))))
# Alone, a flat top meets nothing tangentially.
assert(CadDirect.tangent_chain(rounded, flat(rounded, Vector3(0.0, 1.0, 0.0))).split(";").length == 1)
let by_chain = CadModeling.shelled(rounded, side, 0.1, "s", chain = true)
let by_names = CadModeling.shelled(rounded, chain, 0.1, "s")
let expected = volume(by_names.tessellate(32))
solid(by_chain, expected, 1e-6, "shell with tangent chain")
assert(by_chain.face_count() == by_names.face_count())
# Without the chain the strip would have to leave the side it rounds
# onto: refused.
var refused = false
_ = CadModeling.shelled(rounded, side, 0.1, "s") catch failure:
refused = failure.message.contains("too large")
assert(refused)
## A box's top and +X side copied: an open body of two faces named apart,
## which meshes; the box is unchanged.
func copies() -> unit!:
let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "box")
let top = flat(box, Vector3(0.0, 1.0, 0.0))
let side = flat(box, Vector3(1.0, 0.0, 0.0))
let copy = CadDirect.copied_faces(box, f"{top};{side}", "c:")
assert(copy.face_count() == 2)
for f in 0..<copy.face_count():
assert(CadModeling.face_name_of(copy, f).starts_with("c:"))
let mesh = copy.tessellate(32)
assert(mesh.face_count() > 0)
assert(box.face_count() == 6)
var refused = false
_ = CadDirect.copied_faces(box, "nothing", "c:") catch failure:
refused = failure.message.contains("gone")
assert(refused)
## The names of `model`'s faces, in order.
func names_of(model: CadModel) -> list[str]!:
let found: list[str] = []
for f in 0..<model.face_count():
found.append(CadModeling.face_name_of(model, f))
return found
## Split Face: a 2-box's top by the plane x = 0 (two halves, the +X one
## named apart) and by a pin's wall (a disc and a ring round it, the ring
## named apart); one closed solid of the box's volume each time. A half
## split again by z = 0. Planes that miss the top or lie on it: refused.
func splits() -> unit!:
let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "box")
let top = flat(box, Vector3(0.0, 1.0, 0.0))
let halves = CadDirect.split_faces_by_plane(box, top, Vector3(), Vector3(1.0, 0.0, 0.0), "k")
solid(halves, 8.0, 1e-6, "top split in halves")
assert(halves.face_count() == 7)
let piece = f"k/split[{top}]"
assert(piece in names_of(halves) and top in names_of(halves))
let at = CadModeling.face_point(halves, piece)
assert(at.found and at.point.x > 0.0 and at.normal.y > 0.999)
# The neighbors keep their faces; the +X half splits again across Z.
let quarters = CadDirect.split_faces_by_plane(halves, piece, Vector3(), Vector3(0.0, 0.0, 1.0), "q")
solid(quarters, 8.0, 1e-6, "a half split again")
assert(quarters.face_count() == 8)
let pin = CadModeling.tagged(CadSolids.cylinder(0.5, 3.0), "pin")
var wall = ""
for f in 0..<pin.face_count():
if pin.face_surface_kind(f) == 1:
wall = CadModeling.face_name_of(pin, f)
let ringed = CadDirect.split_faces(box, top, pin, wall, "r")
solid(ringed, 8.0, 1e-6, "top split by a pin")
assert(ringed.face_count() == 7 and f"r/split[{top}]" in names_of(ringed))
var missed = false
_ = CadDirect.split_faces_by_plane(box, top, Vector3(5.0, 0.0, 0.0), Vector3(1.0, 0.0, 0.0), "k") catch failure:
missed = failure.message.contains("misses")
assert(missed)
var lying = false
_ = CadDirect.split_faces_by_plane(box, top, Vector3(0.0, 1.0, 0.0), Vector3(0.0, 1.0, 0.0), "k") catch failure:
lying = failure.message.contains("lies on")
assert(lying)
## Delete of several faces at once: a 2-box's four top edges rounded 0.3
## (the strips and where they meet) deleted together heal back to the box;
## two of its chamfered uprights (0.2) at once; an L-bracket's inside
## fillet (material added) with a chamfered outside edge. A box's top and
## bottom together have nothing to close them: refused.
func deletes() -> unit!:
let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "box")
let tops = edges_where(box, func (a: Vector3, b: Vector3) -> bool: return math.abs(a.y - 1.0) < 1e-9 and math.abs(b.y - 1.0) < 1e-9)
let rounded = CadModeling.filleted(box, tops, 0.3, "t")
let chain = named(rounded, "t/")
assert(chain.split(";").length >= 4)
let healed = CadDirect.deleted_faces(rounded, chain)
solid(healed, 8.0, 1e-5, "rounded top chain deleted")
assert(healed.face_count() == 6)
let uprights = edges_where(box, func (a: Vector3, b: Vector3) -> bool: return math.abs(a.x - b.x) < 1e-9 and math.abs(a.z - b.z) < 1e-9 and math.abs(a.y - b.y) > 1.0)
let d = 0.2
let chamfered = CadModeling.chamfered(box, uprights, d, "c")
let strips = named(chamfered, "c/blend").split(";")
assert(strips.length == 4)
solid(CadDirect.deleted_faces(chamfered, f"{strips[0]};{strips[2]}"), 8.0 - 2.0 * d * d, 1e-5, "two chamfers deleted")
# An L-bracket (legs 2 thick, 10 by 8, 5 deep): its inside upright edge
# filleted 0.5, its outside corner at the origin chamfered 0.5.
let s = CadSketch.parse("")
let corners = [[0.0, 0.0], [10.0, 0.0], [10.0, 2.0], [2.0, 2.0], [2.0, 8.0], [0.0, 8.0]]
let points: list[int] = []
for c in corners:
points.append(s.add_point(c[0], c[1]))
for at in 0..<6:
_ = s.add_line(points[at], points[(at + 1) % 6])
let bracket = CadModeling.extruded(s.curves(Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0)), 5.0, 0.0, "l")
let inner = edges_where(bracket, func (a: Vector3, b: Vector3) -> bool: return math.abs(a.x - 2.0) < 1e-9 and math.abs(b.x - 2.0) < 1e-9 and math.abs(a.y - 2.0) < 1e-9 and math.abs(b.y - 2.0) < 1e-9)
let outer = edges_where(bracket, func (a: Vector3, b: Vector3) -> bool: return math.abs(a.x) < 1e-9 and math.abs(b.x) < 1e-9 and math.abs(a.y) < 1e-9 and math.abs(b.y) < 1e-9)
let filleted = CadModeling.filleted(bracket, inner, 0.5, "fi")
let both = CadModeling.chamfered(filleted, CadModeling.edge_references(filleted, ids_where(filleted, func (a: Vector3, b: Vector3) -> bool: return math.abs(a.x) < 1e-9 and math.abs(b.x) < 1e-9 and math.abs(a.y) < 1e-9 and math.abs(b.y) < 1e-9)), 0.5, "ch")
assert(outer != "")
let pair = f"{named(both, "fi/blend")};{named(both, "ch/blend")}"
solid(CadDirect.deleted_faces(both, pair), 160.0, 1e-5, "inside fillet and outside chamfer deleted")
var refused = false
_ = CadDirect.deleted_faces(box, f"{flat(box, Vector3(0.0, 1.0, 0.0))};{flat(box, Vector3(0.0, -1.0, 0.0))}") catch failure:
refused = failure.message.contains("cannot")
assert(refused)
## The B-rep numbers of `model`'s edges whose ends both satisfy `keep`.
func ids_where(model: CadModel, keep: func(Vector3, Vector3) -> bool) -> list[int]!:
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 found
## Replace Face onto several targets: a 2-box's top onto both halves of a
## taller box's split top (one plane: the local move), 3 high; onto the
## two under faces of a 4-block turned 45° whose lowest edge is 0.5 over
## the top (a valley: 1.5 + |x| over the middle), 12 in all; a can's wall
## (and what meets it tangentially) onto a wider can's.
func replaces() -> unit!:
let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "box")
let top = flat(box, Vector3(0.0, 1.0, 0.0))
let tall = CadModeling.tagged(CadSolids.box(2.0, 4.0, 2.0), "tall")
let tall_top = flat(tall, Vector3(0.0, 1.0, 0.0))
let halves = CadDirect.split_faces_by_plane(tall, tall_top, Vector3(), Vector3(1.0, 0.0, 0.0), "k")
solid(CadDirect.replaced_faces(box, top, halves, f"{tall_top};k/split[{tall_top}]"), 12.0, 1e-5, "top onto two coplanar faces")
let block = CadModeling.tagged(CadSolids.box(4.0, 4.0, 4.0).transformed(Vector3(0.0, 1.5 + 2.0 * math.sqrt(2.0), 0.0), Vector3(0.0, 0.0, math.pi * 0.25), Vector3(1.0, 1.0, 1.0)), "block")
let under: list[str] = []
for f in 0..<block.face_count():
let name = CadModeling.face_name_of(block, f)
let frame = CadModeling.face_frame(block, name)
if frame.found and frame.u.cross(frame.v).y < -0.5:
under.append(name)
assert(under.length == 2)
let valley = CadDirect.replaced_faces(box, top, block, under.join(";"))
solid(valley, 12.0, 1e-4, "top onto a valley")
let can = CadModeling.tagged(CadSolids.cylinder(1.0, 2.0), "can")
let wide = CadModeling.tagged(CadSolids.cylinder(1.5, 4.0), "wide")
var wall = ""
var wide_wall = ""
for f in 0..<can.face_count():
if can.face_surface_kind(f) == 1:
wall = CadModeling.face_name_of(can, f)
for f in 0..<wide.face_count():
if wide.face_surface_kind(f) == 1:
wide_wall = CadModeling.face_name_of(wide, f)
solid(CadDirect.replaced_faces(can, wall, wide, wide_wall, chain = true), math.pi * 2.25 * 2.0, 0.02, "wall onto a wider one")
# A quilt the body would have to pass beside: a block too narrow.
let narrow = CadModeling.tagged(CadSolids.box(1.0, 1.0, 1.0).transformed(Vector3(0.0, 3.0, 0.0), Vector3(0.0, 0.0, math.pi * 0.25), Vector3(1.0, 1.0, 1.0)), "narrow")
let narrow_under: list[str] = []
for f in 0..<narrow.face_count():
let name = CadModeling.face_name_of(narrow, f)
let frame = CadModeling.face_frame(narrow, name)
if frame.found and frame.u.cross(frame.v).y < -0.5:
narrow_under.append(name)
var refused = false
_ = CadDirect.replaced_faces(box, top, narrow, narrow_under.join(";")) catch failure:
refused = failure.message.contains("reach")
assert(refused)