Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.4

0.9.4 / src / modeling / handles.lucb

## Where a feature's handles stand on a body (luced-3d's viewport arrows):
## the middle of an edge with the way out of its corner, for a fillet's or
## chamfer's radius arrow; the middle of a face with its outward normal, for
## Press Pull's arrow on a face.

## An edge's handle (CadModeling.edge_frame): its middle, its direction
## there, and the way out of the corner between its two faces (their
## outward normals' sum, square to the edge); `found` false when the edge
## is gone.
pub struct EdgeFrame:
    pub var found: bool
    pub var point: Vector3
    pub var tangent: Vector3
    pub var outward: Vector3

## A face's handle (CadModeling.face_point): a point on it near the middle
## of its boundary and its outward normal there; `found` false when no face
## goes by the name.
pub struct FacePoint:
    pub var found: bool
    pub var point: Vector3
    pub var normal: Vector3

## The handle of the first edge `reference` names on `w`.
func edge_frame_of(w: const Shape*, reference: str) -> EdgeFrame!:
    var edges = Growing[usize]()
    defer edges.close()
    try resolve_edges(w, reference, &edges)
    if edges.count == 0: return EdgeFrame(false, Vector3(), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0))
    let e = edges.items[0]
    let p = try w.edge_at(e, 0.5)
    let edge = w.edges.items[e]
    let tangent = try w.tangent(edge.carrier, edge.t0 + (edge.t1 - edge.t0) * 0.5).normalized()
    let (fa, fb) = try edge_faces(w, e)
    let na = try w.face_normal(fa, try nearest_on_surface(w, fa, p))
    let nb = try w.face_normal(fb, try nearest_on_surface(w, fb, p))
    var out = na.add(nb)
    out = out.subtract(tangent.multiply_scalar(out.dot(tangent)))
    # Faces meeting flat (a tangent edge): out along their shared normal.
    let way = out.normalized() catch failure:
        recover na
    return EdgeFrame(true, p, tangent, way)

## The handle of the face of `w` named `name` (any alias), the first.
func face_point_of(w: const Shape*, name: str) -> FacePoint!:
    for f in 0..<w.faces.count:
        if not (try names_meet(w.face_name(f), name)): continue
        let on = try point_on_face(w, f)
        return FacePoint(true, on, try w.face_normal(f, on))
    return FacePoint(false, Vector3(), Vector3(0.0, 1.0, 0.0))