dymokomi/luce-cad
Source of release 0.9.2
0.9.2 / tests / regressions / modeling_tests.luc
## CAD modeling: sketch profiles extruded into closed solids, and booleans,
## checked by their tessellations: closed and outward, with the volumes
## geometry gives.
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 luce_geocore.geocore import Mesh, GeometrySet, Sketches
from checks import manifold, volume
from luce_std import math
func solid(model: CadModel, faces: int, expected: float, tolerance: float) -> Mesh!:
if model.face_count() != faces: print(f"faces {model.face_count()}, expected {faces}")
assert(model.face_count() == faces)
let mesh = model.tessellate(16)
manifold(mesh)
let v = volume(mesh)
if math.abs(v - expected) > tolerance: print(f"volume {v}, expected {expected}")
assert(math.abs(v - expected) <= tolerance)
return mesh
pub func modeling_tests() -> unit!:
let empty = GeometrySet.of_mesh(Mesh.empty())
# A 4 x 2 rectangle with a hole of radius 0.5 (shape 4 is a rectangle,
# 3 a circle), on the ground plane.
let plate = Sketches.with_shape(Sketches.with_shape(empty, 4, [4.0, 2.0], 0.0, 0.0), 3, [0.5], 1.0, 0.0)
assert(CadModeling.profile_count(plate) == 1)
_ = solid(CadModeling.extruded(plate, 1.0, 0.0, "e"), 7, 8.0 - math.pi * 0.25, 0.02)
# Downward, and a lone circle: a cylinder.
_ = solid(CadModeling.extruded(plate, -0.5, 0.0, "e"), 7, (8.0 - math.pi * 0.25) * 0.5, 0.02)
# Symmetric: from -0.5 to 0.5 along the normal (Y on the ground).
let both_ways = CadModeling.extruded(plate, 1.0, -0.5, "e")
let swept = solid(both_ways, 7, 8.0 - math.pi * 0.25, 0.02)
assert(math.abs(swept.bounds().minimum.y + 0.5) < 1e-6 and math.abs(swept.bounds().maximum.y - 0.5) < 1e-6)
let disc = Sketches.with_shape(empty, 3, [1.0], 0.0, 0.0)
_ = solid(CadModeling.extruded(disc, 2.0, 0.0, "e"), 3, math.pi * 2.0, 0.05)
# An arc closed by a line: a D shape (half disc).
let arc = Sketches.with_shape(empty, 2, [1.0, 0.0, 180.0], 0.0, 0.0)
let d_shape = Sketches.with_shape(arc, 0, [2.0], 0.0, 0.0)
_ = solid(CadModeling.extruded(d_shape, 1.0, 0.0, "e"), 4, math.pi * 0.5, 0.02)
# An open curve is no profile.
var rejected = false
_ = CadModeling.extruded(arc, 1.0, 0.0, "e") catch failure:
rejected = failure.message.contains("open")
assert(rejected)
revolves()
booleans()
blends()
marched()
slotted_block()
rims()
mirrored()
print("PASS modeling: sketch profiles extruded and revolved (all the way and part way) into closed solids; booleans (union, subtract, intersect) of boxes, cylinders and spheres with coplanar faces, holes across and at an angle, a cut sphere and a lens; chamfers and fillets of box edges, a slotted, drilled block chamfered; cylinders crossing at right angles (traced); hole rims chamfered and filleted; a mirrored body joined; all closed and of the right volume")
## Booleans of solids, checked by volume: a box drilled through by a
## cylinder, two boxes overlapping (coplanar faces) joined and intersected.
func booleans() -> unit!:
let box = CadSolids.box(2.0, 2.0, 2.0)
let pin = CadSolids.cylinder(0.5, 3.0)
_ = solid(CadModeling.combined(box, pin, 1), 7, 8.0 - math.pi * 0.25 * 2.0, 0.02)
let moved = box.transformed(Vector3(1.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
# Flush faces merge: the union and the intersection are plain boxes.
_ = solid(CadModeling.combined(box, moved, 0), 6, 12.0, 0.001)
_ = solid(CadModeling.combined(box, moved, 2), 6, 4.0, 0.001)
let r = 0.5
# A boss: a cylinder standing out of the top (from y 0.5 to 2.5).
let boss = pin.transformed(Vector3(0.0, 1.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
_ = solid(CadModeling.combined(box, boss, 0), 8, 8.0 + math.pi * r * r * 1.5, 0.02)
# A pocket: a smaller box cut from a corner of the top.
let cutter = CadSolids.box(1.0, 1.0, 1.0).transformed(Vector3(0.5, 0.75, 0.5), Vector3(), Vector3(1.0, 1.0, 1.0))
let pocket = CadModeling.combined(box, cutter, 1)
# The cutter overlaps the box by 1 x 0.75 x 1.
_ = solid(pocket, pocket.face_count(), 8.0 - 0.75, 0.001)
# A horizontal hole along Z: the cylinder's side meets the end faces in
# circles.
let across = pin.transformed(Vector3(), Vector3(math.pi * 0.5, 0.0, 0.0), Vector3(1.0, 1.0, 1.0))
_ = solid(CadModeling.combined(box, across, 1), 7, 8.0 - math.pi * r * r * 2.0, 0.02)
# A tilted hole: ellipses on the top and bottom.
let tilt = 0.3
let slanted = CadSolids.cylinder(0.4, 4.0).transformed(Vector3(), Vector3(0.0, 0.0, tilt), Vector3(1.0, 1.0, 1.0))
_ = solid(CadModeling.combined(box, slanted, 1), 7, 8.0 - math.pi * 0.16 * 2.0 / math.cos(tilt), 0.03)
# A sphere cut flat: a plane meets it in a circle.
let ball = CadSolids.sphere(1.0)
let slab = CadSolids.box(4.0, 2.0, 4.0).transformed(Vector3(0.0, 1.5, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let h = 0.5
_ = solid(CadModeling.combined(ball, slab, 1), 3, 4.0 / 3.0 * math.pi - math.pi * h * h * (3.0 - h) / 3.0, 0.03)
# Two parallel cylinders overlapping: their lens.
let other = pin.transformed(Vector3(0.5, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let lens = CadModeling.combined(pin, other, 2)
let area = 2.0 * r * r * math.acos(0.5 / (2.0 * r)) - 0.5 * 0.5 * math.sqrt(4.0 * r * r - 0.25)
_ = solid(lens, lens.face_count(), area * 3.0, 0.02)
## References to `model`'s edges `edges`.
func refs(model: CadModel, edges: list[int]) -> str!:
return CadModeling.edge_references(model, edges)
## The B-rep edges of `model` along Y.
func upright_edges(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 model.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 found
## Chamfers and fillets of a box's edges, by volume: the four upright ones
## chamfered and filleted, and every edge chamfered (a^3 - 6ad^2 + 6d^3:
## the corners where three chamfers meet, from the booleans).
func blends() -> unit!:
let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "box")
let d = 0.2
let uprights = upright_edges(box)
assert(uprights.length == 4)
_ = solid(CadModeling.chamfered(box, refs(box, uprights), d, "c"), 10, 8.0 - 4.0 * d * d * 0.5 * 2.0, 0.002)
let r = 0.3
_ = solid(CadModeling.filleted(box, refs(box, uprights), r, "f"), 10, 8.0 - 8.0 * (r * r - math.pi * r * r * 0.25), 0.01)
# Every edge filleted: a rounded box (faces, quarter cylinders, sphere
# corners): (a - 2r)^3 + 6 (a - 2r)^2 r + 3 pi r^2 (a - 2r) + 4/3 pi r^3.
let inner = 2.0 - 2.0 * r
let rounded_volume = inner * inner * inner + 6.0 * inner * inner * r + 3.0 * math.pi * r * r * inner + 4.0 / 3.0 * math.pi * r * r * r
_ = solid(CadModeling.filleted(box, "", r, "f"), 26, rounded_volume, 0.01)
# Fillets meeting at corners: fillet_tests.luc.
# Each corner is where three chamfer planes meet (mitered).
_ = solid(CadModeling.chamfered(box, "", d, "c"), 18, 8.0 - 6.0 * 2.0 * d * d + 6.0 * d * d * d, 0.002)
## A part: a block with a slot across its top (the slot's sides flush with
## the block's), a hole down through it, then every straight convex edge
## chamfered.
func slotted_block() -> unit!:
let block = CadSolids.box(3.0, 2.0, 2.0)
let slot = CadSolids.box(1.0, 2.0, 2.0).transformed(Vector3(0.0, 1.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let hole = CadSolids.cylinder(0.35, 4.0).transformed(Vector3(1.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let slotted = CadModeling.combined(block, slot, 1)
let drilled = CadModeling.combined(slotted, hole, 1)
let solid_volume = 12.0 - 2.0 - math.pi * 0.35 * 0.35 * 2.0
_ = solid(drilled, drilled.face_count(), solid_volume, 0.02)
let chamfered = CadModeling.chamfered(drilled, "", 0.12, "c")
let m = chamfered.tessellate(16)
manifold(m)
assert(volume(m) < solid_volume and volume(m) > solid_volume - 0.3)
## Revolutions about the sketch plane's u axis (Z on the ground plane): a
## rectangle beside it a tube, a circle a torus, a half disc on it a ball.
func revolves() -> unit!:
let empty = GeometrySet.of_mesh(Mesh.empty())
# Plane coordinates (a, b) are (z, x) on the ground.
let rectangle = Sketches.with_shape(empty, 4, [2.0, 1.0], 0.0, 1.5)
_ = solid(CadModeling.revolved(rectangle, 0, "r"), 4, math.pi * 3.0 * 2.0, 0.05)
let ring = Sketches.with_shape(empty, 3, [0.5], 0.0, 2.0)
_ = solid(CadModeling.revolved(ring, 0, "r"), 1, 2.0 * math.pi * math.pi * 2.0 * 0.25, 0.05)
let half = Sketches.with_shape(Sketches.with_shape(empty, 2, [1.0, 0.0, 180.0], 0.0, 0.0), 0, [2.0], 0.0, 0.0)
_ = solid(CadModeling.revolved(half, 0, "r"), 2, 4.0 / 3.0 * math.pi, 0.05)
# Part way: a quarter of the ring of rectangles (4 sides, 2 caps), a
# quarter torus (1 side, 2 caps), half a ball (its 2 halves of a side
# and 2 caps: the half disc's line lies on the axis).
_ = solid(CadModeling.revolved(rectangle, 0, "r", math.pi * 0.5), 6, math.pi * 3.0 * 2.0 * 0.25, 0.02)
_ = solid(CadModeling.revolved(ring, 0, "r", math.pi * 0.5), 3, 2.0 * math.pi * math.pi * 2.0 * 0.25 * 0.25, 0.02)
_ = solid(CadModeling.revolved(half, 0, "r", math.pi), 4, 2.0 / 3.0 * math.pi, 0.02)
## Where no closed form meets: cylinders crossing at right angles (a cross
## of pipes, and one drilled across another), a frustum tilted through a
## post and a ring tilted around a rod, traced and fitted.
func marched() -> unit!:
tilted(CadSolids.cylinder(0.5, 3.0), CadSolids.cone(0.6, 3.0, 0.2).transformed(Vector3(), Vector3(math.pi / 3.0, 0.0, 0.0), Vector3(1.0, 1.0, 1.0)), math.pi * 0.25 * 3.0, math.pi * (0.36 + 0.12 + 0.04))
tilted(CadSolids.cylinder(0.15, 3.0).transformed(Vector3(1.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0)), CadSolids.torus(1.0, 0.25).transformed(Vector3(), Vector3(math.pi / 6.0, 0.0, 0.0), Vector3(1.0, 1.0, 1.0)), math.pi * 0.0225 * 3.0, 2.0 * math.pi * math.pi * 0.0625)
let upright = CadSolids.cylinder(0.5, 3.0)
let lying = CadSolids.cylinder(0.3, 3.0).transformed(Vector3(), Vector3(math.pi * 0.5, 0.0, 0.0), Vector3(1.0, 1.0, 1.0))
# The union and the intersection together hold both parts' volumes.
let both = CadModeling.combined(upright, lying, 0)
let shared = CadModeling.combined(upright, lying, 2)
let a = math.pi * 0.25 * 3.0
let b = math.pi * 0.09 * 3.0
let joined = solid(both, both.face_count(), 0.0, 1000.0)
let common = solid(shared, shared.face_count(), 0.0, 1000.0)
let total = volume(joined) + volume(common)
if math.abs(total - (a + b)) > 0.02: print(f"union + intersection {total}, parts {a + b}")
assert(math.abs(total - (a + b)) < 0.02)
# The shared part of a rod through a thicker post: less than the rod
# across the post's width, more than the square in the rod's section
# along the shortest chord through the post.
assert(volume(common) < math.pi * 0.09 * 1.0 and volume(common) > 0.18 * 0.8)
_ = solid(CadModeling.combined(upright, lying, 1), CadModeling.combined(upright, lying, 1).face_count(), a - volume(common), 0.02)
## `a` (of volume `va`) and `b` (of `vb`) crossing at an angle: their union
## and intersection, both closed, together hold both volumes.
func tilted(a: CadModel, b: CadModel, va: float, vb: float) -> unit!:
let both = CadModeling.combined(a, b, 0)
let shared = CadModeling.combined(a, b, 2)
let total = volume(solid(both, both.face_count(), 0.0, 1000.0)) + volume(solid(shared, shared.face_count(), 0.0, 1000.0))
if math.abs(total - (va + vb)) > 0.02: print(f"union + intersection {total}, parts {va + vb}")
assert(math.abs(total - (va + vb)) < 0.02)
## A plate's hole: its rims (circles between the faces and the hole)
## chamfered (cones) and filleted (tori), by Pappus: the corner region's
## area turned about the axis at its centroid's radius.
func rims() -> unit!:
let plate = CadModeling.tagged(CadSolids.box(3.0, 1.0, 3.0), "plate")
let hole = CadModeling.tagged(CadSolids.cylinder(0.5, 2.0), "hole")
let drilled = CadModeling.combined(plate, hole, 1)
let plain = 9.0 - math.pi * 0.25 * 1.0
let edges: list[int] = []
for face in 0..<drilled.face_count():
for use in 0..<drilled.face_edge_count(face):
let id = drilled.face_edge_id(face, use)
if not drilled.face_edge_linear(face, use) and not (id in edges): edges.append(id)
assert(edges.length == 2)
let d = 0.1
let chamfer = 2.0 * math.pi * (0.5 + d / 3.0) * d * d * 0.5
_ = solid(CadModeling.chamfered(drilled, refs(drilled, edges), d, "c"), 9, plain - 2.0 * chamfer, 0.005)
let r = 0.15
let area = r * r * (1.0 - math.pi * 0.25)
let reach = r * (10.0 - 3.0 * math.pi) / (3.0 * (4.0 - math.pi))
_ = solid(CadModeling.filleted(drilled, refs(drilled, edges), r, "f"), 9, plain - 2.0 * 2.0 * math.pi * (0.5 + reach) * area, 0.005)
## A body mirrored (a reflection, its loops turned) and joined to itself.
func mirrored() -> unit!:
# A wedge-ish body off the YZ plane: a box from x 0.5 to 1.5.
let box = CadSolids.box(1.0, 1.0, 1.0).transformed(Vector3(1.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let mirror = box.transformed(Vector3(), Vector3(), Vector3(-1.0, 1.0, 1.0))
_ = solid(CadModeling.combined(mirror, CadSolids.box(0.5, 0.5, 0.5), 0), 12, 1.125, 0.001)
_ = solid(CadModeling.combined(box, mirror, 0), 12, 2.0, 0.001)