dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / spline_boolean_tests.luc
## Booleans of B-spline faces at any angle (march.lucb traces where their
## surfaces meet): a box with G2-rounded uprights cut by a tilted slab and
## drilled by a tilted pin, and two such boxes turned against each other,
## joined and intersected. Each result is closed, and the pieces add up:
## A less B and A with B make A; A or B and A with B make A and B.
##
## B-spline faces lying on each other (spline_coincidence.lucb; the SMLib
## study's B9): the rounded box and its copy moved half its height, its
## strips on the copy's over their common half: union, common and
## difference are 1.5, 0.5 and 0.5 of the box (SMLib: within 2e-7 of
## those). And a box flush with the rounded box's side, its faces on the
## side's plane and across two strips: with it, the side's corners back
## (half of what the four strips took off), and in common half the box.
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 closed, volume
from luce_std import math
func uprights(model: CadModel) -> 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)
let a = model.face_edge_point(face, use, 0.0)
let b = model.face_edge_point(face, use, 1.0)
if 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 found
## `model`'s volume, its tessellation checked closed.
func sized(model: CadModel) -> float!:
let mesh = model.tessellate(32)
closed(mesh)
return volume(mesh)
func adds_up(parts: float, whole: float, what: str) -> unit!:
if math.abs(parts - whole) > 0.001: print(f"{what}: {parts} against {whole}")
assert(math.abs(parts - whole) <= 0.001)
pub func spline_boolean_tests() -> unit!:
let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "#b")
let rounded = CadModeling.filleted(box, CadModeling.edge_references(box, uprights(box)), 0.5, "#f", 2)
let whole = sized(rounded)
# A slab tilted about X and Z: planes across the strips at an angle.
let slab = CadModeling.tagged(CadSolids.box(4.0, 1.0, 4.0).transformed(Vector3(0.0, 1.0, 0.0), Vector3(0.3, 0.0, 0.2), Vector3(1.0, 1.0, 1.0)), "#s")
adds_up(sized(CadModeling.combined(rounded, slab, 1)) + sized(CadModeling.combined(rounded, slab, 2)), whole, "slab")
# A pin tilted through a strip: a cylinder meeting the B-spline.
let pin = CadModeling.tagged(CadSolids.cylinder(0.3, 4.0).transformed(Vector3(0.9, 0.0, 0.9), Vector3(0.5, 0.0, -0.5), Vector3(1.0, 1.0, 1.0)), "#p")
adds_up(sized(CadModeling.combined(rounded, pin, 1)) + sized(CadModeling.combined(rounded, pin, 2)), whole, "pin")
# A second rounded box turned about X and Y: strips crossing strips.
let other = CadModeling.prefixed(rounded.transformed(Vector3(1.2, 0.3, 0.9), Vector3(0.4, 0.5, 0.2), Vector3(1.0, 1.0, 1.0)), "o:")
let union = sized(CadModeling.combined(rounded, other, 0))
let common = sized(CadModeling.combined(rounded, other, 2))
adds_up(union + common, whole + sized(other), "two rounded boxes")
assert(common > 0.5)
coincident_tests()
print("PASS B-spline booleans: a G2-rounded box cut by a tilted slab and drilled by a tilted pin, two such boxes turned against each other joined and intersected; closed, and their pieces add up; B-spline faces on each other (a rounded box and its copy moved half its height: 1.5, 0.5, 0.5 of it; a box flush with its side)")
## The volume of `model`, extrapolated from 32 and 64 divisions, its
## meshes closed.
func refined(model: CadModel) -> float!:
let coarse = sized(model)
let mesh = model.tessellate(64)
closed(mesh)
let fine = volume(mesh)
return fine + (fine - coarse) / 3.0
func near(value: float, expected: float, within: float, what: str) -> unit!:
if math.abs(value - expected) > within: print(f"{what}: {value} against {expected}")
assert(math.abs(value - expected) <= within)
func coincident_tests() -> unit!:
let box = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "r")
let rounded = CadModeling.filleted(box, CadModeling.edge_references(box, uprights(box)), 5.0, "g", 2)
let whole = refined(rounded)
let copy = rounded.transformed(Vector3(0.0, 10.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
for (op, share, prefix) in [(0, 1.5, "s:"), (2, 0.5, "t:"), (1, 0.5, "u:")]:
let made = CadModeling.combined(rounded, CadModeling.prefixed(copy, prefix), op)
near(refined(made), whole * share, 0.002, f"rounded box and its copy, operation {op}")
# The box over x from 0 to 10: its faces at z = ±10 and y = ±10 on
# the rounded box's, its face at x = 10 on the side's.
let half = CadModeling.tagged(CadSolids.box(10.0, 20.0, 20.0).transformed(Vector3(5.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0)), "h")
near(refined(CadModeling.combined(rounded, half, 0)), (8000.0 + whole) * 0.5, 0.002, "flush box joined")
near(refined(CadModeling.combined(rounded, half, 2)), whole * 0.5, 0.002, "flush box in common")