Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / src / modeling / face_split.lucb

## Split Face (Fusion's): faces of a body split where a surface crosses
## them (a plane, or a face's surface extended past the face). The body
## keeps its shape and stays one closed solid; it gains the edges along the
## cut, and the faces next to the split ones gain vertices where those
## edges end.
##
## The booleans' first steps do the work (boolean.lucb steps 1 to 4): the
## surface, as the faces of its solid lying on it, meets only the chosen
## faces; each chosen face is split along the new edges; every piece of
## the body is kept and none merged back (boolean_result.lucb's last steps
## without the selection and merging).
##
## Each piece needs a name of its own to be picked and moved apart: the
## pieces on the surface's outer side (a plane's normal side; outside a
## cylinder, cone, sphere or torus) are named `<tag>/split[<name>]`, the
## others keep the face's name. Refused when the surface crosses none of
## the faces, or lies on one of them.
##
## With Extend splitting tool off the tool face itself is the sheet, not
## its surface laid out past the body: it splits only where it reaches,
## and each face it meets must be cut right across (a cut ending inside a
## face is refused, naming the option).

## Why an unextended tool face is refused.
let reach_refusal = "the splitting face does not reach across the faces to split: turn Extend splitting tool on to split by its surface"

## `model` with its faces `chosen` (marked) split where surface `s` crosses
## them (see above).
func split_faces_by(model: const CadModel*, chosen: const bool[], s: const Surface*, tag: str) -> interop.Reference[CadModel]!:
    let (low, high) = try model_box(model)
    # The surface as faces: those of its solid lying on it, a plane's large
    # square or a curved surface's long wall.
    let span = high.subtract(low).length() * 4.0 + s.origin.subtract(low).length() * 2.0
    let (_, tool) = try surface_side(s, s.origin, 1.0, s.z, 0.0, span, tag)
    defer tool.release()
    var t = try shape_of(tool.value())
    defer t.close()
    var on = Growing[i64]()
    defer on.close()
    for f in 0..<t.faces.count:
        if t.surfaces.items[(usize)t.faces.items[f].surface].kind != s.kind: continue
        # Its boundary on the surface (a plane's square, not the box's other
        # sides; a wall, not its caps, by the kind).
        var lies = true
        for use in t.face_uses(f):
            let (gap, _) = try signed_gap(s, try t.edge_at(edge_index(use), 0.5), t.tolerance)
            if math.abs(gap) > span * 1e-9: lies = false
        if lies: try on.push((i64)f)
    if on.count == 0: error(invalid, "Split Face could not lay out the splitting surface")
    return try split_faces_by_sheet(model, chosen, s, tool.value(), on.view(), tag, true)

## `model` with its faces `chosen` split where faces `sheet` of `tool` (the
## tool face, unextended; `s` its surface, the sides' judge) cross them.
func split_faces_by_face(model: const CadModel*, chosen: const bool[], s: const Surface*, tool: const CadModel*, sheet: const i64[], tag: str) -> interop.Reference[CadModel]!:
    return try split_faces_by_sheet(model, chosen, s, tool, sheet, tag, false)

## The corners of the box round `model`'s faces.
func model_box(model: const CadModel*) -> (Vector3, Vector3)!:
    var w = try shape_of(model)
    defer w.close()
    var low = Vector3(1.0e300, 1.0e300, 1.0e300)
    var high = Vector3(-1.0e300, -1.0e300, -1.0e300)
    for f in 0..<w.faces.count:
        let (l, h) = try face_box(&w, f)
        low = lower(low, l)
        high = upper(high, h)
    return (low, high)

## The split itself: `tool`'s faces `on` laid into `model` (its faces
## `chosen` the ones cut), `s` telling the sides; `extended` when the sheet
## is the tool's whole surface (a cut then always runs across a face).
func split_faces_by_sheet(model: const CadModel*, chosen: const bool[], s: const Surface*, tool: const CadModel*, on: const i64[], tag: str, extended: bool) -> interop.Reference[CadModel]!:
    var first = try shape_of(model)
    let faces_a = first.faces.count
    let (low, high) = try model_box(model)
    let sheet = try tool.subset(on)
    defer sheet.release()
    var second = try shape_of(sheet.value())
    defer second.close()
    try append_shape(&first, &second)
    var work = Work(first, faces_a, 1.0, 1e-9, Growing[Vector3](), Growing[i64](), Growing[Cut](), Growing[Section](), Growing[Imprint](),
                    Growing[Coincident](), Growing[ShapeEdge](), Growing[i64](), Growing[usize](), Growing[i64](), Growing[bool]())
    defer work.close()
    for c in chosen: try work.chosen.push(c)
    work.size = math.max(1e-6, high.subtract(low).length())
    work.tol = math.max(work.size * 1e-7, 1e-10)
    for p in work.w.points.view(): try work.point_vertex.push(try work.vertex(p))
    try intersect_faces(&work)
    if work.coincident.count > 0: error(invalid, "the splitting surface lies on a face it would split: pick another tool")
    try vertex_cuts(&work)
    try cut_edges(&work)
    var traced = try final_shape(&work)
    defer traced.close()
    var pieces = try new_pieces()
    defer pieces.close()
    var piece_face = Growing[usize]()
    defer piece_face.close()
    var piece_name = Growing[i64]()
    defer piece_name.close()
    face_pieces(&work, &traced, faces_a, &pieces, &piece_face, &piece_name) catch failure:
        # Unextended, a cut ending inside a face leaves loose edges the
        # pieces cannot be told apart by.
        if extended: error(failure.code, failure.message)
        error(invalid, reach_refusal)
    if not extended:
        # Unextended, a face the tool face meets must be cut right across:
        # one cut ending inside it leaves it one piece.
        for section in work.sections.view():
            for side in [section.fa, section.fb]:
                if side < 0 or (usize)side >= faces_a or not chosen[(usize)side]: continue
                var count = 0
                for q in 0..<pieces.piece_count():
                    if piece_face.items[q] == (usize)side: count += 1
                if count < 2: error(invalid, reach_refusal)
    # Every piece of the body kept as it is.
    var keep = Growing[bool]()
    defer keep.close()
    var turn = Growing[bool]()
    defer turn.close()
    for _ in 0..<pieces.piece_count():
        try keep.push(true)
        try turn.push(false)
    # The pieces of each split face on the surface's outer side, named
    # apart.
    var split_any = false
    var name = Growing[u8]()
    defer name.close()
    for p in 0..<pieces.piece_count():
        let f = piece_face.items[p]
        if not chosen[f]: continue
        var count = 0
        for q in 0..<pieces.piece_count():
            if piece_face.items[q] == f: count += 1
        if count < 2: continue
        split_any = true
        let point = piece_point(&work, &traced, f, &pieces, p) catch failure:
            if extended: error(failure.code, failure.message)
            error(invalid, reach_refusal)
        let (gap, _) = try signed_gap(s, point, work.tol)
        if gap <= 0.0: continue
        name.clear()
        try role_name(&name, tag, "split[")
        for byte in work.w.face_name(f).bytes: try name.push(byte)
        try name.push(93)
        piece_name.items[p] = try work.w.add_name((str)name.view())
    if not split_any:
        if not extended: error(invalid, reach_refusal)
        error(invalid, "the splitting tool misses the faces to split")
    try seam_zones(&work, &traced, &pieces, piece_face.view(), keep.view())
    return try write_result(&work, &pieces, piece_face.view(), piece_name.view(), keep.view(), turn.view())