dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / tests / regressions / fillet_option_tests.luc
## Fusion's Fillet options beyond a constant radius: Chord Length (the
## radius from the contacts' distance: c / √2 at a square corner, more
## at an obtuse one; edges at different angles refused).
from luce_cad.modeling import CadModeling
from luce_cad.model import CadModel
from luce_cad.solids import CadSolids
from luce_cad.sketch import CadSketch
from luce_geocore.geocore import Vector3
from checks import manifold, volume
from luce_std import math
## The ids of `model`'s edges whose ends `keep` takes.
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
pub func fillet_option_tests() -> unit!:
chord_length()
full_round()
variable_radius()
setback_g1()
print("PASS fillet options: chord length (c / √2 at a square corner, c / (2 cos 67.5°) on a 135° one, edges at mixed angles refused); full round of a square rib (its top gone, one round of half its width) and a tapered one (the round touching the top), G1 and G2; variable radius along a box's edge, straight (1 to 3) and smooth (2, 1 in the middle, 2), against the integral of r² along it; G1 setback corners (rational patches) at a box's corner and on every edge, closed, within a hair of the balls")
func chord_length() -> unit!:
let box = CadModeling.tagged(CadSolids.box(2.0, 2.0, 2.0), "box")
let front_top = func (a: Vector3, b: Vector3) -> bool:
return a.y > 0.99 and b.y > 0.99 and a.z > 0.99 and b.z > 0.99
let edge = CadModeling.edge_references(box, edges_where(box, front_top))
let r = CadModeling.chord_radius(box, edge, 0.5 * math.sqrt(2.0))
assert(math.abs(r - 0.5) < 1e-9, f"{r}")
# The fillet of that radius leaves the faces 0.5 from the edge: the
# contacts are the chord apart.
let made = CadModeling.filleted(box, edge, r, "f")
let mesh = made.tessellate(32)
manifold(mesh)
assert(math.abs(volume(mesh) - (8.0 - (0.25 - math.pi * 0.0625) * 2.0)) < 1e-3)
# A trapezoid prism: its top meets the slanted side at 135°, the
# left side at 90°.
let s = CadSketch.parse("")
let p = [s.add_point(0.0, 0.0), s.add_point(4.0, 0.0), s.add_point(3.0, 1.0), s.add_point(0.0, 1.0)]
for at in 0..<4:
_ = s.add_line(p[at], p[(at + 1) % 4])
let prism = CadModeling.extruded(s.curves(Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0)), 2.0, 0.0, "t")
let ridge = func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.x - 3.0) < 1e-9 and math.abs(b.x - 3.0) < 1e-9 and math.abs(a.y - 1.0) < 1e-9 and math.abs(b.y - 1.0) < 1e-9
let square = func (a: Vector3, b: Vector3) -> bool:
return math.abs(a.x) < 1e-9 and math.abs(b.x) < 1e-9 and math.abs(a.y - 1.0) < 1e-9 and math.abs(b.y - 1.0) < 1e-9
let at_slant = CadModeling.edge_references(prism, edges_where(prism, ridge))
let obtuse = CadModeling.chord_radius(prism, at_slant, 1.0, false)
assert(math.abs(obtuse - 1.0 / (2.0 * math.cos(math.pi * 0.375))) < 1e-6, f"{obtuse}")
# The 135° edge and the 90° one together: refused.
let both = CadModeling.edge_references(prism, edges_where(prism, ridge) + edges_where(prism, square))
var said = ""
_ = CadModeling.chord_radius(prism, both, 1.0, false) catch failure:
said = failure.message
assert(said.contains("one angle"), said)
## The name of the face of `model` facing `normal`.
func facing(model: CadModel, normal: Vector3) -> str!:
for f in 0..<model.face_count():
let name = CadModeling.face_name_of(model, f)
let at = CadModeling.face_point(model, name)
if at.found and at.normal.dot(normal) > 0.999:
return name
return ""
## Whether a flat face of `model` faces up (every face's point found,
## B-spline faces' too).
func flat_top(model: CadModel) -> bool!:
var up = false
for f in 0..<model.face_count():
let at = CadModeling.face_point(model, CadModeling.face_name_of(model, f))
assert(at.found)
up = up or (model.face_surface_kind(f) == 0 and at.normal.y > 0.999)
return up
## A G2 and a G3 full round: one face (its halves joined where the top
## was), its point inside it, halfway along the rib (z = 0, not on an
## end's section where a fallback would put it), on the round above its
## center, facing away from it (regression: a point off a B-spline surface
## was refused as "NURBS boundary projection exceeds CAD tolerance").
func strip_points(rib: CadModel, top: str) -> unit!:
for continuity in [2, 3]:
let round = CadModeling.full_round(rib, top, "r", continuity = continuity)
var strips = 0
for f in 0..<round.face_count():
if round.face_surface_kind(f) == 0:
continue
let at = CadModeling.face_point(round, CadModeling.face_name_of(round, f))
assert(at.found and math.abs(at.point.z) < 1e-6, f"{at.point.z}")
assert(at.point.y > 3.0 and at.point.y < 5.0 + 1e-9)
let out = Vector3(at.point.x, at.point.y - 3.0, 0.0).normalized()
assert(at.normal.dot(out) > 0.99)
strips += 1
assert(strips == 1)
## A 4 x 10 x 20 rib: its top rounded whole, radius 2: two quarter
## cylinders' worth taken, the top face gone. A tapered rib (6 wide at the
## foot, 4 at the top, 10 high): the round reaches the top's height.
func full_round() -> unit!:
let rib = CadModeling.tagged(CadSolids.box(4.0, 10.0, 20.0), "rib")
let top = facing(rib, Vector3(0.0, 1.0, 0.0))
for continuity in [1, 2]:
let round = CadModeling.full_round(rib, top, "r", continuity = continuity)
let mesh = round.tessellate(32)
manifold(mesh)
if continuity == 1:
assert(math.abs(volume(mesh) - (800.0 - 40.0 * 4.0 * (1.0 - math.pi * 0.25))) < 0.1)
assert(not flat_top(round) and math.abs(mesh.bounds().maximum.y - 5.0) < 1e-3)
strip_points(rib, top)
let s = CadSketch.parse("")
let p = [s.add_point(-3.0, 0.0), s.add_point(3.0, 0.0), s.add_point(2.0, 10.0), s.add_point(-2.0, 10.0)]
for at in 0..<4:
_ = s.add_line(p[at], p[(at + 1) % 4])
let tapered = CadModeling.extruded(s.curves(Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0)), 20.0, 0.0, "t")
let round = CadModeling.full_round(tapered, facing(tapered, Vector3(0.0, 1.0, 0.0)), "r")
let mesh = round.tessellate(32)
manifold(mesh)
assert(math.abs(mesh.bounds().maximum.y - 10.0) < 1e-3 and volume(mesh) < 100.0 * 20.0)
## A 20 box's front top edge, radius 1 at its start to 3 at its end,
## straight: a ball rolled (Fusion's G1 at weight 1), a cone whose sections
## square to the edge are conics of one middle weight w (rolling_weight:
## r cos²(θ/2) / (sin(θ/2) d) - 1 for its depth d at the bisector), so
## their corner area at unit radius times 20 × ∫ r² ds = 20 × 13/3 is
## taken; smooth with 1 in the middle and 2 at the ends: the circle's
## (1 − π/4) times that integral of the smooth r (its sections scaled).
func variable_radius() -> unit!:
let box = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "box")
let front_top = func (a: Vector3, b: Vector3) -> bool:
return a.y > 9.99 and b.y > 9.99 and a.z > 9.99 and b.z > 9.99
let edge = CadModeling.edge_references(box, edges_where(box, front_top))
let straight = CadModeling.variable_filleted(box, edge, [0.0, 1.0], [1.0, 3.0], "v", smooth = false)
let mesh = straight.tessellate(32)
manifold(mesh)
let corner = 1.0 - math.pi * 0.25
# The cone's slope 0.1 per length: its depth at the bisector and weight.
let half = math.sqrt(0.5)
let depth = (1.0 / half - math.sqrt(1.0 + 0.01 / 0.5 - 0.01)) / 0.99
let cone = conic_corner(0.5 / (half * depth) - 1.0)
let taken = 8000.0 - volume(mesh)
if math.abs(taken - cone * 20.0 * 13.0 / 3.0) > 0.05:
print(f"straight: taken {taken}, expected {cone * 20.0 * 13.0 / 3.0}")
assert(math.abs(taken - cone * 20.0 * 13.0 / 3.0) < 0.05)
assert(cone > 0.0 and cone < corner)
assert(straight.face_count() == 7)
let smooth = CadModeling.variable_filleted(box, edge, [0.0, 0.5, 1.0], [2.0, 1.0, 2.0], "v", smooth = true)
let smooth_mesh = smooth.tessellate(32)
manifold(smooth_mesh)
# ∫ r² ds by Simpson's rule on r = 2 − t²(3 − 2t) each half.
var integral = 0.0
let steps = 2000
for k in 0..<steps + 1:
let x = float(k) / float(steps)
let t = x * 2.0 if x <= 0.5 else (1.0 - x) * 2.0
let r = 2.0 - t * t * (3.0 - 2.0 * t)
let factor = 1.0 if k == 0 or k == steps else (4.0 if k % 2 == 1 else 2.0)
integral += factor * r * r
integral = integral / (3.0 * float(steps))
let smooth_taken = 8000.0 - volume(smooth_mesh)
if math.abs(smooth_taken - corner * 20.0 * integral) > 0.05:
print(f"smooth: taken {smooth_taken}, expected {corner * 20.0 * integral}")
assert(math.abs(smooth_taken - corner * 20.0 * integral) < 0.05)
## Three G1 fillets of 3 at a 20 box's corner with Fusion's Setback corner
## type: closed, the corner a patch (more faces than the ball's), taking a
## little more than the ball (the fillets stop 2 r short of the corner and
## the patch blends them; Fusion's at radius 4 takes 26.8 more, ours with
## its fairer, flatter patch 41: vertex_blend_tests); every edge of the box
## likewise.
func setback_g1() -> unit!:
let box = CadModeling.tagged(CadSolids.box(20.0, 20.0, 20.0), "box")
let corner = func (a: Vector3, b: Vector3) -> bool:
let c = Vector3(10.0, 10.0, 10.0)
return a.subtract(c).length() < 1e-9 or b.subtract(c).length() < 1e-9
let edges = CadModeling.edge_references(box, edges_where(box, corner))
let ball = CadModeling.filleted(box, edges, 3.0, "f")
let ball_mesh = ball.tessellate(32)
manifold(ball_mesh)
let patched = CadModeling.filleted(box, edges, 3.0, "f", setback = true)
let mesh = patched.tessellate(32)
manifold(mesh)
assert(patched.face_count() > ball.face_count())
assert(volume(mesh) < volume(ball_mesh) and volume(ball_mesh) - volume(mesh) < 0.004 * 8000.0, f"setback {volume(mesh)}, ball {volume(ball_mesh)}")
let every = CadModeling.filleted(box, "", 3.0, "f", setback = true)
let every_mesh = every.tessellate(32)
manifold(every_mesh)
## The corner region between unit legs along x and y and the rational
## quadratic from (1, 0) to (0, 1) whose middle control point is the corner,
## weighted `w` (a conic: the circle at √½): by the shoelace over its points.
func conic_corner(w: float) -> float:
var area = 0.0
var previous = Vector3(0.0, 0.0, 0.0)
let steps = 4000
for k in 0..<steps + 2:
var p = Vector3(0.0, 0.0, 0.0)
if k <= steps:
let t = float(k) / float(steps)
let a = (1.0 - t) * (1.0 - t)
let c = t * t
let total = a + 2.0 * t * (1.0 - t) * w + c
p = Vector3(a / total, c / total, 0.0)
area += previous.x * p.y - p.x * previous.y
previous = p
return area * 0.5