dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / overflow_tests.luc
## A fillet larger than a face it runs onto (the SMLib study's item 8, its
## F6): a plate 4 thick, its top edge rounded 5 and 4.5. The fillet runs off
## the side face over its convex bottom edge into the bottom, which trims it
## (the study's rail classification, its same-convexity case): the side
## face goes, the round meets the bottom at an angle. Refused before ("it
## must be less than 4"); SMLib fails it too. The volumes are the plate's
## less 20 times the section taken away: the corner rectangle r by 4 less
## the disk of r about (10 - r, 2 - r) inside it, integrated: 1493.36981 and
## 1513.17902. A side face ending in a concave edge (a ledge below it, a
## cliff) was refused: the tool reached into the ledge. It now stops at the
## ledge's plane, which covers the fillet's end there: the ledge plate (a
## 20 x 4 x 20 plate on a 10 x 4 x 20 ledge, 2400) rounded 5 and 4.5 is the
## section integral's 2293.36981 and 2313.17902 to 2e-3. A side face split
## along a line (two faces in one plane) lets the ball roll on past the
## line: rounded 3, 1561.37167 (1600 less 20 r² (1 - π/4)); refused before
## (only 2 of the side above the line).
## Choosing among the behaviours (Parasolid's): with Cliff, the plate's and
## the ledge's top edge rounded 5 is a cliff-edge blend, its circle tangent
## to the top and through the side face's bottom edge, the side face gone:
## the section taken away is the triangle between the edge, the top contact
## and the cliff edge less the circle's segment on that chord. Rolling on
## onto a curved tangent face (the study's item 8, smooth): the plate's
## bottom side edge rounded 3 first leaves 1 of side face; its top edge
## rounded 1.5 takes the ball onto that round, touching the top and the
## cylinder (the polygon of the contacts and the side's ends, less the
## fillet's segment, with the cylinder's); notch alone refuses it.
## Inside edges and ends (Fusion 2705, probe 14 of 2026-10-13): a wall 2
## high, its top inner edge rounded 1.5, its foot filleted 3 rolls the ball
## in the air onto that round (Fusion's 1764.688847, 9 faces: the circle
## tangent to the floor and to the round, outside it); a wall's foot whose
## floor drops away in a step ends flush with the step's face (a cliff
## end: 1948.9712479 and 1923.2190275 at 3 and 1, the strips' own areas);
## a cube's top edge rounded 4 where its side is notched below one end
## runs on, the notch's faces trimming it (Fusion's 7761.5377437, 10
## faces); and with Parasolid's overlap limit instead it stops square
## where the notch begins (7830 less 15 of its section, 7778.495559).
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 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, 1.0)) and not (id in found):
found.append(id)
return found
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)
pub func overflow_tests() -> unit!:
let plate = CadModeling.tagged(CadSolids.box(20.0, 4.0, 20.0), "p")
let top_edge = func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.x - 10.0) < 1e-9 and math.abs(b.x - 10.0) < 1e-9 and math.abs(a.y - 2.0) < 1e-9 and math.abs(b.y - 2.0) < 1e-9
let edge = CadModeling.edge_references(plate, edges_where(plate, top_edge))
for (radius, expected) in [(5.0, 1493.36981), (4.5, 1513.17902)]:
let made = CadModeling.filleted(plate, edge, radius, "f", 1, overflow = 5)
assert(made.face_count() == 6)
let coarse = volume(made.tessellate(32))
let mesh = made.tessellate(64)
closed(mesh)
near(volume(mesh) + (volume(mesh) - coarse) / 3.0, expected, 1e-3, f"plate rounded {radius}")
# A ledge below the side face: its edge there is concave (a cliff).
let ledge = CadModeling.combined(plate, CadModeling.tagged(CadSolids.box(10.0, 4.0, 20.0), "l").transformed(Vector3(10.0, -4.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0)), 0)
for (radius, expected) in [(5.0, 2293.36981), (4.5, 2313.17902)]:
let made = CadModeling.filleted(ledge, CadModeling.edge_references(ledge, edges_where(ledge, top_edge)), radius, "f", 1, overflow = 5)
let coarse = volume(made.tessellate(32))
let mesh = made.tessellate(64)
closed(mesh)
near(volume(mesh) + (volume(mesh) - coarse) / 3.0, expected, 2e-3, f"ledge plate rounded {radius}")
# The side face split along y = 0: the ball rolls over the line.
var side = ""
for face in 0..<plate.face_count():
let name = CadModeling.face_name_of(plate, face)
let at = CadModeling.face_point(plate, name)
if at.found and at.normal.x > 0.999: side = name
let split = CadDirect.split_faces_by_plane(plate, side, Vector3(), Vector3(0.0, 1.0, 0.0), "s")
let rolled = CadModeling.filleted(split, CadModeling.edge_references(split, edges_where(split, top_edge)), 3.0, "f", 1)
let coarse = volume(rolled.tessellate(32))
let mesh = rolled.tessellate(64)
closed(mesh)
near(volume(mesh) + (volume(mesh) - coarse) / 3.0, 1600.0 - 20.0 * 9.0 * (1.0 - math.pi / 4.0), 1e-3, "split side rounded 3")
cliff_edges(plate, ledge, top_edge)
inside_cliff(plate)
rolled_on(plate, top_edge)
inside_ends(plate)
print("PASS fillet overflow: a plate 4 thick, its top edge rounded 5 and 4.5, trimmed by its bottom (the side face gone; the volumes to 1e-3); over a cliff onto a ledge, stopped at its plane (to 2e-3); rolling on over a line splitting the side face; cliff-edge blends on the plate and the ledge; rolling on onto a round below the side face; an inside foot rolling onto its wall's rounded top, ending flush over a step (cliff end), a notch below one end trimming a fillet (Fusion's volumes), and an overlap limit stopping it there")
## A circle's segment of radius `r` on a chord `chord` long.
func segment(r: float, chord: float) -> float:
let angle = 2.0 * math.asin(chord / (2.0 * r))
return r * r * 0.5 * (angle - math.sin(angle))
## The mesh volume of `made` extrapolated from 32 and 64 divisions, closed.
func extrapolated(made: CadModel) -> float!:
let coarse = volume(made.tessellate(32))
let mesh = made.tessellate(64)
closed(mesh)
return volume(mesh) + (volume(mesh) - coarse) / 3.0
func cliff_edges(plate: CadModel, ledge: CadModel, top_edge: func(Vector3, Vector3) -> bool) -> unit!:
# Radius 5 over a side 4 high: the circle tangent to the top at sc from
# the edge, through the side's bottom edge.
let r = 5.0
let sc = math.sqrt(r * r - (4.0 - r) * (4.0 - r))
let taken = sc * 4.0 * 0.5 - segment(r, math.sqrt(sc * sc + 16.0))
let made = CadModeling.filleted(plate, CadModeling.edge_references(plate, edges_where(plate, top_edge)), r, "f", 1, overflow = 3)
assert(made.face_count() == 6, f"cliff: {made.face_count()} faces")
near(extrapolated(made), 1600.0 - 20.0 * taken, 1e-3, "plate's cliff-edge blend")
# Fusion's own (probe 12): 1501.45145 and the ledge 2301.45145, its
# default; and ours by default.
near(1600.0 - 20.0 * taken, 1501.45145, 1e-5, "Fusion's cliff-edge blend")
let fusion = CadModeling.filleted(plate, CadModeling.edge_references(plate, edges_where(plate, top_edge)), r, "f", 1)
near(extrapolated(fusion), 1501.45145, 1e-3, "the default overflow, Fusion's cliff")
let on_ledge = CadModeling.filleted(ledge, CadModeling.edge_references(ledge, edges_where(ledge, top_edge)), r, "f", 1, overflow = 3)
near(extrapolated(on_ledge), 2400.0 - 20.0 * taken, 2e-3, "ledge's cliff-edge blend")
# G2 takes no cliff-edge blend: said, not silently notched.
var said = ""
_ = CadModeling.filleted(plate, CadModeling.edge_references(plate, edges_where(plate, top_edge)), r, "f", 2, overflow = 2) catch failure:
said = failure.message
recover plate
assert(said.contains("cliff-edge"), f"G2 cliff: {said}")
func rolled_on(plate: CadModel, top_edge: func(Vector3, Vector3) -> bool) -> unit!:
let bottom_edge = func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.x - 10.0) < 1e-9 and math.abs(b.x - 10.0) < 1e-9 and math.abs(a.y + 2.0) < 1e-9 and math.abs(b.y + 2.0) < 1e-9
let big = 3.0
let rounded = CadModeling.filleted(plate, CadModeling.edge_references(plate, edges_where(plate, bottom_edge)), big, "b", 1)
let edge = CadModeling.edge_references(rounded, edges_where(rounded, top_edge))
# The ball of r at (cx, 2 - r) touches the cylinder about (7, 1) inside.
let r = 1.5
let cx = 7.0 + math.sqrt((big - r) * (big - r) - (1.0 - r) * (1.0 - r))
let k = big / (big - r)
let tbx = 7.0 + (cx - 7.0) * k
let tby = 1.0 + (2.0 - r - 1.0) * k
# The polygon (cx, 2), (10, 2), (10, 1), tb by the shoelace formula.
let xs = [cx, 10.0, 10.0, tbx]
let ys = [2.0, 2.0, 1.0, tby]
var twice = 0.0
for i in 0..<4:
twice += xs[i] * ys[(i + 1) % 4] - xs[(i + 1) % 4] * ys[i]
let polygon = math.abs(twice) * 0.5
let fillet_chord = math.sqrt((tbx - cx) * (tbx - cx) + (tby - 2.0) * (tby - 2.0))
let round_chord = math.sqrt((tbx - 10.0) * (tbx - 10.0) + (tby - 1.0) * (tby - 1.0))
let taken = polygon - segment(r, fillet_chord) + segment(big, round_chord)
let made = CadModeling.filleted(rounded, edge, r, "f", 1)
let rolled = extrapolated(made)
near(rolled, 1600.0 - 20.0 * big * big * (1.0 - math.pi / 4.0) - 20.0 * taken, 1e-3, "rolled on onto the round")
# Fusion rolls on too (probe 12): 1549.13093.
near(rolled, 1549.13093, 2e-3, "Fusion's rolled-on plate")
# G2 rolls on too, its section following the round's curvature at its
# contact: closed, and fuller than the circle's.
let g2 = extrapolated(CadModeling.filleted(rounded, edge, r, "f", 2))
assert(g2 > rolled + 0.1 and g2 < 1600.0, f"G2 rolled on: {g2} against G1's {rolled}")
var said = ""
_ = CadModeling.filleted(rounded, edge, r, "f", 1, overflow = 4) catch failure:
said = failure.message
recover rounded
assert(said.contains("must be less than"), f"notch alone: {said}")
## An inside corner whose wall (2 high) is lower than its fillet (3): the
## cliff-edge blend, tangent to the floor and through the wall's top edge,
## adds the triangle between the corner, the floor contact and that edge
## less the circle's segment on that chord.
func inside_cliff(plate: CadModel) -> unit!:
let wall = CadModeling.tagged(CadSolids.box(4.0, 2.0, 20.0), "w").transformed(Vector3(-8.0, 3.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
let body = CadModeling.combined(plate, wall, 0)
let inside = CadModeling.edge_references(body, edges_where(body, func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.x + 6.0) < 1e-9 and math.abs(b.x + 6.0) < 1e-9 and math.abs(a.y - 2.0) < 1e-9 and math.abs(b.y - 2.0) < 1e-9))
let r = 3.0
let h = 2.0
let sc = math.sqrt(r * r - (h - r) * (h - r))
let added = sc * h * 0.5 - segment(r, math.sqrt(sc * sc + h * h))
let made = CadModeling.filleted(body, inside, r, "f", 1)
near(extrapolated(made), 1760.0 + 20.0 * added, 1e-3, "an inside corner's cliff-edge blend")
# Fusion's (probe 13): 1790.6350087.
near(1760.0 + 20.0 * added, 1790.6350087, 1e-6, "Fusion's inside cliff")
var said = ""
_ = CadModeling.filleted(body, inside, r, "f", 1, overflow = 5) catch failure:
said = failure.message
recover body
assert(said.contains("must be less than"), f"inside corner, no cliff: {said}")
## A body's box `w` by `h` by `d` about `at`, tagged.
func box_at(w: float, h: float, d: float, at: Vector3, tag: str) -> CadModel!:
return CadModeling.moved_body(CadModeling.tagged(CadSolids.box(w, h, d), tag), at)
## Fusion's inside-edge overflow and ends (the file's header).
func inside_ends(plate: CadModel) -> unit!:
let foot = func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.x + 6.0) < 1e-9 and math.abs(b.x + 6.0) < 1e-9 and math.abs(a.y - 2.0) < 1e-9 and math.abs(b.y - 2.0) < 1e-9
let walled = CadModeling.combined(plate, box_at(4.0, 2.0, 20.0, Vector3(-8.0, 3.0, 0.0), "w"), 0)
let top = CadModeling.filleted(walled, CadModeling.edge_references(walled, edges_where(walled, func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.x + 6.0) < 1e-9 and math.abs(b.x + 6.0) < 1e-9 and math.abs(a.y - 4.0) < 1e-9 and math.abs(b.y - 4.0) < 1e-9)), 1.5, "t", 1)
let rolled = CadModeling.filleted(top, CadModeling.edge_references(top, edges_where(top, foot)), 3.0, "f", 1)
assert(rolled.face_count() == 9, f"{rolled.face_count()} faces")
near(extrapolated(rolled), 1764.688847, 1e-3, "an inside foot rolled onto its wall's round")
# The floor dropping 2 in a step at z = 5 beside a wall 6 high.
let high = CadModeling.combined(plate, box_at(4.0, 6.0, 20.0, Vector3(-8.0, 5.0, 0.0), "w"), 0)
let stepped = CadModeling.combined(high, box_at(16.0, 2.0, 5.0, Vector3(2.0, 1.0, 7.5), "s"), 1)
for (r, fusion) in [(3.0, 1948.9712479), (1.0, 1923.2190275)]:
let made = CadModeling.filleted(stepped, CadModeling.edge_references(stepped, edges_where(stepped, foot)), r, "f", 1)
assert(made.face_count() == 11)
near(extrapolated(made), fusion, 1e-3, f"a foot ending over a step, {r}")
near(1920.0 + 15.0 * r * r * (1.0 - math.pi / 4.0), fusion, 1e-6, "Fusion's step ends")
# A 20 cube's side notched below one end of its top edge.
let cube = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "c")
let notched = CadModeling.combined(cube, box_at(2.0, 17.0, 5.0, Vector3(9.0, -1.5, 7.5), "n"), 1)
let edge = CadModeling.edge_references(notched, edges_where(notched, func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.x - 10.0) < 1e-9 and math.abs(b.x - 10.0) < 1e-9 and math.abs(a.y - 10.0) < 1e-9 and math.abs(b.y - 10.0) < 1e-9))
let trimmed = CadModeling.filleted(notched, edge, 4.0, "f", 1)
assert(trimmed.face_count() == 10)
near(extrapolated(trimmed), 7761.5377437, 1e-3, "a notch trimming a fillet's end")
let limited = CadModeling.filleted(notched, edge, 4.0, "f", 1, overflow = 0, limit_overlap = true)
near(extrapolated(limited), 7830.0 - 15.0 * 16.0 * (1.0 - math.pi / 4.0), 1e-3, "an overlap limit")