Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / src / modeling / chamfer_corners.lucb

## Fusion's chamfer Corner Type, where three chamfered edges meet at a
## corner of three flat faces (each checked against Fusion 2705's own
## result on a 20 mm cube's corner, chamfers of 4):
## - Miter: the chamfers run on until they meet, three planes in a point.
##   Each edge's tool reaching past the corner (blend.lucb) gives it.
## - Chamfer (Fusion's default): a further flat face joins the chamfers.
##   Each face at the corner holds two of the chamfers' contact lines,
##   meeting at a point; the corner's face is the plane through those
##   three points, and the tip beyond it is taken away (a box on that
##   plane, as big as the corner, its inside toward the vertex). The
##   chamfers then end on that face in lines.
## - Blend: the chamfers set back from the corner and one flat face, a
##   hexagon, closing it: Fusion's (planar, not a curved patch, where the
##   faces are flat). The plane is the Chamfer face's turned the same way,
##   moved in until every contact line runs on past its crossing by its
##   chamfer's distance (2 d from the vertex at a square corner; the
##   hexagon's corners there are Fusion's to 0.0006 mm). Its sides
##   alternate: a chamfer's end, then a line across a face. Where the
##   faces are flat a plane always closes a convex corner, so no patch is
##   needed (MODELING.md: Chamfer corners, the rule for curved faces).

pub let corner_chamfer: i64 = 0
pub let corner_miter: i64 = 1
pub let corner_blend: i64 = 2

## The corners where three of `chosen` (straight, convex) meet at three
## flat faces, each as a vertex onto `out`.
func chamfer_corners(w: const Shape*, chosen: const usize[], out: Growing[i64]*) -> !:
    out.clear()
    for e in chosen:
        let ends: i64[2] = [w.edges.items[e].start, w.edges.items[e].end]
        for v in ends:
            if vertex_in(out.view(), v): continue
            var here = 0
            var all = 0
            for other in 0..<w.edges.count:
                let o = w.edges.items[other]
                if o.start != v and o.end != v: continue
                all += 1
                if edge_in(chosen, other) and w.carriers.items[(usize)o.carrier].kind == curve_line and try convex(w, other): here += 1
            if here != 3 or all != 3: continue
            var flat = true
            for f in 0..<w.faces.count:
                var at_corner = false
                for use in w.face_uses(f):
                    let o = w.edges.items[edge_index(use)]
                    if o.start == v or o.end == v: at_corner = true
                if at_corner and w.surfaces.items[(usize)w.faces.items[f].surface].kind != surface_plane: flat = false
            if flat: try out.push(v)

## The tool taking the tip off chamfer corner `v` (chamfers of `size`, with
## `sides`' legs), its faces named `name`: a box whose face toward the
## body lies on the plane through the three points where the chamfers'
## contact lines meet on each face; with `blend`, that plane moved in until
## each contact line runs on past its crossing by its chamfer's distance.
func chamfer_corner_tool(w: const Shape*, v: i64, chosen: const usize[], size: f64, sides: const ChamferSides*?, name: str, blend: bool) -> interop.Reference[CadModel]!:
    let apex = w.points.items[(usize)v]
    var edges: usize[3] = [0, 0, 0]
    var ways: Vector3[3] = [Vector3(), Vector3(), Vector3()]
    var count: usize = 0
    for e in chosen:
        let o = w.edges.items[e]
        if o.start != v and o.end != v: continue
        edges[count] = e
        ways[count] = try w.leaving(e, 0 if o.start == v else 1)
        count += 1
    if count != 3: error(invalid, "a chamfer corner needs three chamfered edges")
    # On each face, the two edges it holds: their contact lines meet at
    # apex + a (leg b / sin) + b (leg a / sin).
    var points: Vector3[3] = [Vector3(), Vector3(), Vector3()]
    # Each chamfer's distance: its longer leg.
    var reaches: f64[3] = [0.0, 0.0, 0.0]
    for i in 0..<(usize)3:
        let (leg_a, leg_b) = try chamfer_legs(w, edges[i], size, sides)
        reaches[i] = math.max(leg_a, leg_b)
    for i in 0..<(usize)3:
        let j = (i + 1) % 3
        let f = try shared_face(w, edges[i], edges[j])
        if f < 0: error(invalid, "a chamfer corner's edges must pair up on its faces")
        let leg_i = try leg_on(w, edges[i], (usize)f, size, sides)
        let leg_j = try leg_on(w, edges[j], (usize)f, size, sides)
        let sine = ways[i].cross(ways[j]).length()
        if sine < 1e-9: error(invalid, "a chamfer corner's edges run together")
        points[i] = apex.add(ways[i].multiply_scalar(leg_j / sine)).add(ways[j].multiply_scalar(leg_i / sine))
    var normal = try points[1].subtract(points[0]).cross(points[2].subtract(points[0])).normalized()
    if apex.subtract(points[0]).dot(normal) < 0.0: normal = normal.multiply_scalar(-1.0)
    # The plane's height along the normal: through the crossings, or for a
    # Blend the lowest of the points each contact line reaches past its
    # crossing (face i holds edges i and i + 1).
    var height_at = points[0].dot(normal)
    if blend:
        for i in 0..<(usize)3:
            let j = (i + 1) % 3
            height_at = math.min(height_at, points[i].add(ways[i].multiply_scalar(reaches[i])).dot(normal))
            height_at = math.min(height_at, points[i].add(ways[j].multiply_scalar(reaches[j])).dot(normal))
    let depth = apex.dot(normal) - height_at
    if depth <= w.tolerance: error(invalid, "the chamfers leave no corner to cut")
    # Where the plane meets each edge: the corner's face's corners.
    var center = Vector3()
    var meets: Vector3[3] = [Vector3(), Vector3(), Vector3()]
    for k in 0..<(usize)3:
        let t = -depth / ways[k].dot(normal)
        let o = w.edges.items[edges[k]]
        let length = w.points.items[(usize)o.end].subtract(w.points.items[(usize)o.start]).length()
        if t >= length - w.tolerance: error(invalid, "the chamfer corner reaches past the far end of an edge: make the chamfers smaller, or use a Miter corner")
        meets[k] = apex.add(ways[k].multiply_scalar(t))
        center = center.add(meets[k].multiply_scalar(1.0 / 3.0))
    var reach = 0.0
    for k in 0..<(usize)3: reach = math.max(reach, meets[k].subtract(center).length())
    let sideways = try meets[0].subtract(center).subtract(normal.multiply_scalar(meets[0].subtract(center).dot(normal))).normalized()
    let side = reach * 2.4
    let height = depth * 2.0 + w.tolerance * 100.0
    let box = try CadSolids.box(side, side, height)
    defer box.release()
    let placed = try box.value().placed(Matrix4(sideways, normal.cross(sideways), normal, center.add(normal.multiply_scalar(height * 0.5))))
    defer placed.release()
    var shape = try shape_of(placed.value())
    defer shape.close()
    let named = try shape.add_name(name)
    for f in 0..<shape.faces.count: shape.faces.items[f].name = named
    return try shape_model(&shape)

## Edge `e`'s chamfer leg along its face `f` (chamfer_legs').
func leg_on(w: const Shape*, e: usize, f: usize, size: f64, sides: const ChamferSides*?) -> f64!:
    let (fa, fb) = try edge_faces(w, e)
    let (leg_a, leg_b) = try chamfer_legs(w, e, size, sides)
    if f == fa: return leg_a
    if f == fb: return leg_b
    error(invalid, "a chamfer corner's face is not its edge's")