dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / division_range_tests.luc
## Every face meshes at every division count, or the tessellation names it:
## no face drops out silently. Wavy strips of variable fillets rolled round
## loops (a dome's rim, a rounded box's top) once dropped out at 48
## divisions and more: their closed B-spline strip's seam started its
## chart walk on the wrong copy, so the mapped mesher refused it and the
## clipped grid outgrew its budget; the dome's sphere cap (its rim wavy,
## not a latitude) outgrew the lattice budget. Open ends' strips beside
## flat end faces and a ball drilled off its axis and at a slant (sphere
## trims that are not caps; at a slant each half sphere is its equator and
## a hole, which no chart took) are checked too, each at 4 to 64
## divisions: closed, no face skipped, and every face present.
##
## A ball with eight blind holes all round and a T of pipes with its saddle
## filleted (the SMLib study's T2 and F7) once lost faces at 64 divisions:
## shared edges were sampled more coarsely than their curved faces' interior
## targets, the refinement chased a sag it could not reduce, and Lawson
## flips stopped by a pass budget left fans of slivers to split. Their
## volumes are checked against SMLib's exact B-rep integrals.
##
## A ball with six blind holes along ±X, ±Y and ±Z (through both poles of
## its own chart) splits into faces no chart of the sphere's own takes:
## each is meshed about an axis of its own (sphere_axis.lucb), closed at
## every division and converging to the exact volume.
from luce_cad.modeling import CadModeling
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_cad.cad_geometry import CadGeometry
from luce_geocore.geocore import Vector3
from checks import closed, volume
from luce_std import math
func edges_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, 0.5)) and not (id in found):
found.append(id)
return found
## `model` meshed at every division count: closed, nothing skipped, and
## every one of its faces holding polygons.
func every_division(name: str, model: CadModel) -> unit!:
for d in [4, 8, 16, 24, 32, 40, 44, 48, 52, 56, 60, 64]:
let mesh = model.tessellate(d)
let skipped = CadGeometry.skipped_faces(mesh)
if skipped != "": print(f"{name} at {d}: {skipped}")
assert(skipped == "" and CadGeometry.dropped_faces(mesh) == 0)
let parent = mesh.find_attribute("cad_face", 2)
let seen: list[bool] = []
for face in 0..<model.face_count(): seen.append(false)
for face in 0..<mesh.face_count(): seen[int(mesh.attribute_value(parent, face, 0))] = true
for face in 0..<model.face_count():
if not seen[face]: print(f"{name} at {d}: face {face} has no polygons")
assert(seen[face])
closed(mesh)
## The volume `model`'s meshes at 32 and 64 divisions extrapolate to.
func refined(model: CadModel) -> float!:
let coarse = volume(model.tessellate(32))
let fine = volume(model.tessellate(64))
return fine + (fine - coarse) / 3.0
pub func division_range_tests() -> unit!:
# A dome's rim, rolled G1 at weights 1 and 0.5, and G2.
let ball = CadModeling.tagged(CadSolids.sphere(10.0), "ball")
let below = CadModeling.tagged(CadSolids.box(30.0, 30.0, 30.0), "cut").transformed(Vector3(0.0, -15.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let dome = CadModeling.combined(ball, below, 1)
let on_rim = func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.y) < 1e-9 and math.abs(b.y) < 1e-9 and math.abs(math.hypot(a.x, a.z) - 10.0) < 1e-6
let rim = CadModeling.edge_references(dome, edges_where(dome, on_rim))
for (continuity, weight) in [(1, 1.0), (1, 0.5), (2, 1.0)]:
every_division(f"dome G{continuity} weight {weight}", CadModeling.variable_filleted(dome, rim, [0.0, 0.25, 0.75, 1.0], [2.0, 1.5, 2.5, 2.0], "v", false, continuity, true, weight))
# A rounded box's top loop, rolled.
let box = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "b")
let upright = func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.x - b.x) < 1e-9 and math.abs(a.y - b.y) < 1e-9
let rounded = CadModeling.filleted(box, CadModeling.edge_references(box, edges_where(box, upright)), 4.0, "u", 1, false)
let top = func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.z - 10.0) < 1e-9 and math.abs(b.z - 10.0) < 1e-9
let loop = edges_where(rounded, top)
for (continuity, weight) in [(1, 1.0), (2, 1.0)]:
every_division(f"box loop G{continuity}", CadModeling.variable_filleted(rounded, CadModeling.edge_references(rounded, [loop[0]]), [0.0, 0.25, 0.75, 1.0], [2.0, 1.5, 2.5, 2.0], "v", false, continuity, true, weight))
# An upright's arc alone on the top: its strip open, ending in flat
# faces (strips facing either way).
for continuity in 1..<4:
var curve = -1
for face in 0..<rounded.face_count():
for use in 0..<rounded.face_edge_count(face):
let id = rounded.face_edge_id(face, use)
if (id in loop) and not rounded.face_edge_linear(face, use): curve = id
every_division(f"arc alone G{continuity}", CadModeling.filleted(rounded, CadModeling.edge_references(rounded, [curve]), 2.0, "g", continuity, false))
# A ball drilled off its axis: sphere faces trimmed by loops that are
# not latitudes; and at a slant: each half sphere its equator round the
# pole and a hole (sphere_disk), the same volume either way.
let pin = CadModeling.tagged(CadSolids.cylinder(3.0, 30.0), "pin")
let straight = CadModeling.combined(ball, pin.transformed(Vector3(2.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0)), 1)
let slanted = CadModeling.combined(ball, pin.transformed(Vector3(2.0, 0.0, 0.0), Vector3(0.4, 0.3, 0.0), Vector3(1.0, 1.0, 1.0)), 1)
every_division("drilled ball", straight)
every_division("ball drilled at a slant", slanted)
let a = refined(straight)
let b = refined(slanted)
if math.abs(a - b) > 0.1: print(f"drilled balls: {a} straight, {b} at a slant")
assert(math.abs(a - b) <= 0.1)
# Eight blind holes of 2, 8 deep from 4 off the center, all round a
# ball of 10 (six round z at 45 degrees, two along z; the six overlap).
var holed = ball
var k = 0
for theta in [math.pi / 2.0, math.pi / 4.0, 3.0 * math.pi / 4.0, -math.pi / 2.0, -math.pi / 4.0, -3.0 * math.pi / 4.0]:
let axis = Vector3(-math.sin(theta), math.cos(theta), 0.0)
holed = CadModeling.combined(holed, CadModeling.tagged(CadSolids.cylinder(2.0, 8.0), f"h{k}").transformed(axis.multiply_scalar(8.0), Vector3(0.0, 0.0, theta), Vector3(1.0, 1.0, 1.0)), 1)
k += 1
for phi in [math.pi / 2.0, -math.pi / 2.0]:
let axis = Vector3(0.0, math.cos(phi), math.sin(phi))
holed = CadModeling.combined(holed, CadModeling.tagged(CadSolids.cylinder(2.0, 8.0), f"h{k}").transformed(axis.multiply_scalar(8.0), Vector3(phi, 0.0, 0.0), Vector3(1.0, 1.0, 1.0)), 1)
k += 1
every_division("ball with eight holes", holed)
near_smlib("ball with eight holes", refined(holed), 3597.119970, 0.05)
# Six blind holes of 2, 8 deep from 4 off the center, along the axes:
# each takes (2π/3)(1000 - 96^1.5) - 16π from the ball.
var six = ball
var n = 0
for axis in [Vector3(1.0, 0.0, 0.0), Vector3(-1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0), Vector3(0.0, -1.0, 0.0), Vector3(0.0, 0.0, 1.0), Vector3(0.0, 0.0, -1.0)]:
let turn = Vector3(math.pi / 2.0 if axis.z != 0.0 else 0.0, 0.0, math.pi / 2.0 if axis.x != 0.0 else 0.0)
six = CadModeling.combined(six, CadModeling.tagged(CadSolids.cylinder(2.0, 8.0), f"x{n}").transformed(axis.multiply_scalar(8.0), turn, Vector3(1.0, 1.0, 1.0)), 1)
n += 1
every_division("ball with six holes", six)
let hole = 2.0 * math.pi / 3.0 * (1000.0 - math.pow(96.0, 1.5)) - 16.0 * math.pi
near_smlib("ball with six holes (exact)", refined(six), 4000.0 * math.pi / 3.0 - 6.0 * hole, 0.08)
# A T of pipes of 5 and 3, and its saddle loop filleted 1.
let pipe = CadModeling.tagged(CadSolids.cylinder(5.0, 40.0), "m").transformed(Vector3(), Vector3(0.0, 0.0, math.pi / 2.0), Vector3(1.0, 1.0, 1.0))
let branch = CadModeling.tagged(CadSolids.cylinder(3.0, 15.0), "n").transformed(Vector3(0.0, 7.5, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let tee = CadModeling.combined(pipe, branch, 0)
let saddle = func (p: Vector3, m: Vector3) -> bool:
return math.abs(math.hypot(p.x, p.z) - 3.0) < 1e-6 and math.abs(math.hypot(m.x, m.z) - 3.0) < 1e-6 and p.y > 1.0 and p.y < 5.5 and m.y < 5.5
let filleted = CadModeling.filleted(tee, CadModeling.edge_references(tee, edges_where(tee, saddle)), 1.0, "f", 1, false)
every_division("T of pipes", tee)
every_division("T of pipes filleted", filleted)
# At 64 divisions the long pipe's face takes the trimmed chart, whose
# target is relative to the whole face (40 long), so these converge
# less closely than the ball. SMLib's fillet is a cubic approximation
# of the rolled circle (ours is exact).
near_smlib("T of pipes", refined(tee), 3431.022107, 0.5)
near_smlib("T of pipes filleted", refined(filleted), 3433.167125, 0.5)
print("PASS every face meshes at 4 to 64 divisions, or is named: rolled variable fillets round a dome's rim and a box's top loop, open strips ending in flat faces, balls drilled off their axis, straight and at a slant, a ball with eight holes and a T of pipes filleted at its saddle (SMLib's T2 and F7), a ball with six holes through its poles meshed about an axis of its own")
func near_smlib(name: str, ours: float, smlib: float, within: float) -> unit:
if math.abs(ours - smlib) > within: print(f"{name}: {ours}, SMLib {smlib}")
assert(math.abs(ours - smlib) <= within)