dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / heal_replace_tests.luc
## Replace Face onto a closed body that overlaps the one changed, and
## Delete healing beside curved faces beyond one face losing a whole outline
## with a ring of several faces round it: an inside fillet or a chamfer at
## a boss's foot, a hole's rounded rim, and groups of faces apart from each
## other at once. What Fusion does, checked closed and of the volume
## geometry gives, or refused with why. An inside corner between a boss and
## a curved wall: a pipe with a box boss on its side, the boss's upright
## inside edges filleted 1, those fillets deleted heal to the sharp body
## exactly (refused before: the arc on the pipe from the boss's corner to a
## fillet's contact must shrink to the corner, where the strip's end meets
## the corner's vertex).
from luce_cad.modeling import CadModeling, CadDirect
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_geocore.geocore import Vector3
from checks import manifold, volume
from luce_std import math
pub func heal_replace_tests() -> unit!:
overlapping_replace()
curved_rings()
boss_on_pipe()
print("PASS inside corners of a boss on a pipe healed exactly; Replace Face of a top and its fillet onto an overlapping copy's top and fillet (a plane and a cylinder): the body 0.5 taller; Delete of a boss's foot fillet and chamfer, a hole's rounded rim, and a foot fillet with a rounded upright apart from it at once, each healed to the sharp body; the fillet with the plate's top refused")
func solid(model: CadModel, expected: float, tolerance: float, what: str) -> unit!:
let mesh = model.tessellate(32)
manifold(mesh)
let v = volume(mesh)
assert(math.abs(v - expected) <= tolerance, f"{what}: volume {v}, expected {expected}")
## 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 ""
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)
## The names of `model`'s faces on surfaces of `kind` (1 a cylinder).
func of_kind(model: CadModel, kind: int) -> list[str]!:
let found: list[str] = []
for f in 0..<model.face_count():
if model.face_surface_kind(f) == kind:
found.append(CadModeling.face_name_of(model, f))
return found
## A 2-box's +X top edge rounded 0.3 and a copy raised 0.5 (overlapping
## it): the box's top with its Tangent chain (the fillet and the +X side
## it rounds onto) replaced onto the copy's top, fillet and +X side, face
## for face (the copy reaching into the box's bottom, its solid could not
## be the target): the rounded box 0.5 taller.
func overlapping_replace() -> unit!:
let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "box")
let rounded = CadModeling.filleted(box, edges_where(box, func (a: Vector3, b: Vector3) -> bool: return a.x > 0.99 and b.x > 0.99 and a.y > 0.99 and b.y > 0.99), 0.3, "r")
let copy = CadModeling.moved_body(rounded, Vector3(0.0, 0.5, 0.0), prefix = "c:")
let quilt = [flat(copy, Vector3(0.0, 1.0, 0.0))] + of_kind(copy, 1) + [flat(copy, Vector3(1.0, 0.0, 0.0))]
assert(quilt.length == 3)
let expected = 8.0 - 2.0 * (0.09 - 0.09 * math.pi / 4.0) + 2.0
solid(CadDirect.replaced_faces(rounded, flat(rounded, Vector3(0.0, 1.0, 0.0)), copy, quilt.join(";"), chain = true), expected, 1e-3, "top and fillet onto an overlapping copy's")
## The names of `model`'s faces whose names contain `part`.
func named(model: CadModel, part: str) -> list[str]!:
let found: list[str] = []
for f in 0..<model.face_count():
let name = CadModeling.face_name_of(model, f)
if name.contains(part):
found.append(name)
return found
## Delete beside a curved face whose gap closes on itself: a 4 × 1 × 4
## plate with a pin of radius 0.5 standing on it, the pin's foot filleted
## (an inside corner) or chamfered 0.2, deleted: the pin meets the plate
## again along a circle, the plate and pin's volume. A hole of radius 0.5
## through the plate, its rim rounded 0.2, deleted: the drilled plate. The
## foot fillet and a rounded upright of the plate (apart from it) deleted
## together: each healed in turn. The fillet with the plate's top: refused.
func curved_rings() -> unit!:
let plate = CadModeling.tagged(CadSolids.box(4.0, 1.0, 4.0), "plate")
let pinned = CadModeling.combined(plate, CadModeling.moved_body(CadModeling.tagged(CadSolids.cylinder(0.5, 2.0), "pin"), Vector3(0.0, 0.5, 0.0)), 0)
let sharp = volume(pinned.tessellate(32))
let foot = edges_where(pinned, func (a: Vector3, b: Vector3) -> bool: return math.abs(a.y - 0.5) < 1e-6 and math.abs(b.y - 0.5) < 1e-6 and a.length() < 1.0)
for chamfer in [false, true]:
let blended = CadModeling.chamfered(pinned, foot, 0.2, "f", true, 0) if chamfer else CadModeling.filleted(pinned, foot, 0.2, "f")
let gone = named(blended, "f/")
assert(gone.length == 1)
solid(CadDirect.deleted_faces(blended, gone[0]), sharp, 1e-4, "a boss's foot healed")
let drilled = CadModeling.combined(plate, CadModeling.tagged(CadSolids.cylinder(0.5, 3.0), "hole"), 1)
let rim = edges_where(drilled, func (a: Vector3, b: Vector3) -> bool: return math.abs(a.y - 0.5) < 1e-6 and math.abs(b.y - 0.5) < 1e-6 and a.length() < 1.0)
let rounded = CadModeling.filleted(drilled, rim, 0.2, "f")
solid(CadDirect.deleted_faces(rounded, named(rounded, "f/").join(";")), volume(drilled.tessellate(32)), 1e-4, "a hole's rim healed")
# The foot fillet and the plate's upright at +X +Z rounded 0.3: apart.
let footed = CadModeling.filleted(pinned, foot, 0.2, "f")
let upright = edges_where(footed, func (a: Vector3, b: Vector3) -> bool: return a.x > 1.99 and b.x > 1.99 and a.z > 1.99 and b.z > 1.99)
let both = CadModeling.filleted(footed, upright, 0.3, "u")
let groups = named(both, "f/") + named(both, "u/")
assert(groups.length == 2)
solid(CadDirect.deleted_faces(both, groups.join(";")), sharp, 1e-4, "a foot fillet and an upright apart healed")
var refused = ""
_ = CadDirect.deleted_faces(footed, named(footed, "f/")[0] + ";" + flat(footed, Vector3(0.0, 1.0, 0.0))) catch failure:
refused = failure.message
assert(refused.contains("cannot"), refused)
## A pipe (r 10, 20 tall) with a 10 x 8 x 8 boss on its side, its upright
## inside edges rounded 1, the rounds deleted.
func boss_on_pipe() -> unit!:
let pipe = CadModeling.tagged(CadSolids.cylinder(10.0, 20.0), "c")
let boss = CadModeling.tagged(CadSolids.box(10.0, 8.0, 8.0), "b").transformed(Vector3(12.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let body = CadModeling.combined(pipe, boss, 0)
let found: list[int] = []
for face in 0..<body.face_count():
for use in 0..<body.face_edge_count(face):
let a = body.face_edge_point(face, use, 0.0)
let m = body.face_edge_point(face, use, 0.5)
let id = body.face_edge_id(face, use)
if math.abs(a.x - m.x) < 1e-9 and math.abs(a.z - m.z) < 1e-9 and math.abs(a.y - m.y) > 1e-6 and a.x > 9.0 and a.x < 10.0 and not (id in found): found.append(id)
assert(found.length == 2)
let rounded = CadModeling.filleted(body, CadModeling.edge_references(body, found), 1.0, "f", 1)
var names = ""
for f in 0..<rounded.face_count():
let n = CadModeling.face_name_of(rounded, f)
if n.starts_with("f/"): names = n if names.length == 0 else names + ";" + n
let healed = CadDirect.deleted_faces(rounded, names)
assert(healed.face_count() == body.face_count())
solid(healed, volume(body.tessellate(32)), 1e-6, "a boss's inside rounds on a pipe deleted")