Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / src / modeling / chord_length.lucb

## Fusion's Fillet Radius Type Chord Length: the fillet sized by the
## distance between its contacts, where it leaves the faces, rather than by
## its radius. Faces whose ways from the edge are θ apart take contacts
## r cot(θ/2) from the edge, 2 r cos(θ/2) apart: the radius is
## chord / (2 cos(θ/2)) (r = chord / √2 at a square corner). G2 and G3
## fillets touch where the circle does, so the same radius gives them the
## same chord. Each edge's θ is taken at its middle (a curved face's
## tangent plane there); edges whose faces meet at different angles would
## need different radii, which one fillet does not take: refused.

## The radius that gives the edges `references` names (as blended's, with
## their tangent chains when `chain`) fillets `chord` between contacts.
func chord_radius_of(model: const CadModel*, references: str, chord: f64, chain: bool) -> f64!:
    if not math.is_finite(chord) or chord <= 0.0: error(invalid, "a chord length is positive")
    var w = try shape_of(model)
    defer w.close()
    var chosen = Growing[usize]()
    defer chosen.close()
    try choose_edges(&w, references, chain, &chosen)
    var radius = 0.0
    for e in chosen.view():
        let (fa, fb) = try edge_faces(&w, e)
        let along = try along_edge(&w, e)
        let angle = math.acos(math.clamp((try into_face(&w, fa, e, along)).dot(try into_face(&w, fb, e, along)), -1.0, 1.0))
        let half = math.cos(angle * 0.5)
        if half < 1e-9: error(invalid, "an edge between faces that run on flat has no chord length")
        let r = chord / (2.0 * half)
        if radius > 0.0 and math.abs(r - radius) > radius * 1e-6: error(invalid, "Chord length takes edges whose faces meet at one angle: these meet at several, so fillet them apart")
        radius = r
    return radius