Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.7

0.7.7 / src / verbs / face_verbs.lucb

## Verbs over faces that keep the rest of the mesh as it is: Delete (Blast),
## Reverse, Duplicate and Split (Triangulate is triangulate.lucb's). Faces
## outside the group are copied with their triangles; the output selection
## is what the verb made (the kept, reversed, new or inner faces).

let delete_parms: ParmSpec[1] = [ParmSpec("Keep group", ParmKind.toggle, 0.0, 0.0, 1.0, 1, "", "On: keep only the group. Off: delete it.")]

func delete_verb() -> Verb:
    return Verb("Delete", "Modeling", "Remove the group's faces (points delete every face using them) and the points left unused", Domain.face, true, delete_parms, true, cook_delete, true, section = "Modify", family = "Delete", icon = "trash")

## Verbs without parameters take an empty slice of this.
let placeholder_parms: ParmSpec[1] = [ParmSpec("", ParmKind.number, 0.0, 0.0, 0.0)]

func reverse_verb() -> Verb:
    return Verb("Reverse", "Modeling", "Flip the winding of the group's faces", Domain.face, true, placeholder_parms[..<0], true, cook_reverse, section = "Modify", family = "Normals", icon = "reverse-normals", picks = pick_pieces)

let duplicate_parms: ParmSpec[1] = [ParmSpec("Offset", ParmKind.number, 0.2, -100000.0, 100000.0, 1, "", "Distance of the copies along their face normals.", 2.0)]

func duplicate_verb() -> Verb:
    return Verb("Duplicate", "Modeling", "Copy the group's faces, offset along their normals", Domain.face, true, duplicate_parms, true, cook_duplicate, section = "Modify", family = "Extrude", icon = "duplicate-faces", picks = pick_pieces, drag = 0, drag_mode = "normal")

func split_verb() -> Verb:
    return Verb("Split", "Modeling", "Detach the group's faces: they get their own points", Domain.face, true, placeholder_parms[..<0], true, cook_split, section = "Modify", family = "Split", icon = "split-mesh", picks = pick_faces)

func cook_delete(input: const VerbInput*, output: VerbOutput*) -> !:
    var keep = try BitsBuilder.copy_of(input.group)
    defer keep.close()
    if input.parms.at(0) == 0.0: keep.invert()
    let result = try kept_faces(input.mesh, keep.view())
    defer result.release()
    output.mesh = try mesh_share(result.value())
    output.selection_domain = Domain.face

## Copy every face; the group's go through `edit` instead.
func each_face(input: const VerbInput*, output: VerbOutput*, edit: func(const VerbInput*, TopologyBuilder*, usize, BitsBuilder*) -> unit!) -> !:
    try output.builder.begin(input.mesh)
    try output.builder.copy_points(input.mesh)
    output.built = true
    let faces = (usize)input.mesh.face_count()
    # Runs of faces outside the group are copied whole.
    var run: usize = 0
    for face in 0..<faces:
        if not input.group.get(face): continue
        try output.builder.copy_faces(input.mesh, run, face)
        try edit(input, &output.builder, face, &output.selection)
        run = face + 1
    try output.builder.copy_faces(input.mesh, run, faces)

## Start the output selection over the faces the builder will have: every
## face of the input plus `extra`.
func begin_selection(output: VerbOutput*, faces: usize) -> !:
    output.selection = try BitsBuilder.zeros(faces)
    output.selection_domain = Domain.face

func cook_reverse(input: const VerbInput*, output: VerbOutput*) -> !:
    try begin_selection(output, (usize)input.mesh.face_count())
    try each_face(input, output, reverse_face)

func reverse_face(input: const VerbInput*, builder: TopologyBuilder*, face: usize, selection: BitsBuilder*) -> !:
    let offsets = input.mesh.offset_span()
    let corners = input.mesh.corner_span()
    let start = (usize)offsets[face]
    let end = (usize)offsets[face + 1]
    selection.set(builder.face_count())
    try builder.face((i32)face, false, true)
    for at in 0..<end - start:
        let corner = end - 1 - at
        try builder.corner(corners[corner], (i32)corner)
    try builder.end_face()

func cook_duplicate(input: const VerbInput*, output: VerbOutput*) -> !:
    try begin_selection(output, (usize)input.mesh.face_count() + input.group.population())
    try output.builder.begin(input.mesh)
    try output.builder.copy_points(input.mesh)
    output.built = true
    let faces = (usize)input.mesh.face_count()
    try output.builder.copy_faces(input.mesh, 0, faces)
    let offsets = input.mesh.offset_span()
    let corners = input.mesh.corner_span()
    let normals = input.mesh.face_normal_span()
    let distance = input.parms.at(0)
    for face in 0..<faces:
        if not input.group.get(face): continue
        let offset = normals[face].vector().multiply_scalar(distance)
        output.selection.set(output.builder.face_count())
        let start = (usize)offsets[face]
        let size = (usize)offsets[face + 1] - start
        # Small faces on the stack, larger ones in heap scratch.
        var stack: i32[256]
        let points = try face_scratch.take[i32](stack, size, size)
        defer face_scratch.release[i32](stack, points)
        for at in 0..<size:
            let point = corners[start + at]
            points[at] = try output.builder.point(output.builder.position(point).add(offset), point)
        try output.builder.face((i32)face, true)
        for at in 0..<size: try output.builder.corner(points[at], (i32)(start + at))
        try output.builder.end_face()

## The group's faces get their own points, so they separate from the rest.
func cook_split(input: const VerbInput*, output: VerbOutput*) -> !:
    try begin_selection(output, (usize)input.mesh.face_count())
    try each_face(input, output, split_face)

func split_face(input: const VerbInput*, builder: TopologyBuilder*, face: usize, selection: BitsBuilder*) -> !:
    let offsets = input.mesh.offset_span()
    let corners = input.mesh.corner_span()
    let start = (usize)offsets[face]
    let size = (usize)offsets[face + 1] - start
    # Small faces on the stack, larger ones in heap scratch.
    var stack: i32[256]
    let points = try face_scratch.take[i32](stack, size, size)
    defer face_scratch.release[i32](stack, points)
    for at in 0..<size:
        let point = corners[start + at]
        points[at] = try builder.point(builder.position(point), point)
    selection.set(builder.face_count())
    try builder.face((i32)face, true)
    for at in 0..<size: try builder.corner(points[at], (i32)(start + at))
    try builder.end_face()