Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / edit_mesh.luc

## Edit Mesh: the Edit framework's polygon kind. Its levels are Object
## (whole pieces: faces sharing a `path`, else a connected component),
## Vertices (points), Edges and Polygons; its tools are the registry's
## (tool_registry.luc: the luce-geocore modeling verbs), its primitives
## among them (Box, Sphere, …: verbs whose step adds a piece of its own
## `path` to the edited mesh).
from luce_geocore.geocore import Mesh, Groups, Domain, GroupType, Vector3
from geometry_data import GeometryData
from edit_operations import EditStep
from edit_types import EditKind, EditLevel, EditTool, from_tool
from tool_record import ToolPlace
import tool_registry
import verb_tools
from luce_ui import ui
from luce_cad.model import CadTolerances, MeshQuality

pub let kind_name = "Edit Mesh"

## The step a selection set is written by (selection_sets.luc): its group
## the selection, its text the set's name, its number how it writes it.
pub let set_step = "Selection Set"

pub func kind() -> EditKind:
    let tools = tool_registry.of_kind(kind_name)
    let result: list[EditTool] = []
    let placing: list[str] = []
    for tool in tools:
        result.append(from_tool(tool))
        if tool.gesture == "place":
            placing.append(tool.label)
    return EditKind(kind_name, levels(), result, [], start, pick, run, create, draw_tools = placing, drawn = drawn, steps = [set_step], holds = true, run_over = run_over, background = background)

## Object, Vertices, Edges, Polygons (edit_types' level order; any
## displayed mesh selects on these).
pub func levels() -> list[EditLevel]:
    return [EditLevel("Object", "cube", Domain.face, GroupType.faces, piece),
            EditLevel("Vertices", "vertex-select", Domain.point, GroupType.points),
            EditLevel("Edges", "edge-select", Domain.edge, GroupType.edges),
            EditLevel("Polygons", "face-select", Domain.face, GroupType.faces)]

## A placing tool's corners (the `place` gesture: clicks on points, the
## surface or the ground) as its one step, in PolyDraw's numbers: a snap
## distance, the corner count, then the corners. Corners placed on existing
## points carry their exact positions, so a tiny snap distance joins them.
func drawn(label: str, corners: list[Vector3]) -> EditStep?!:
    let tool = tool_registry.find(label) else return none
    if tool.gesture != "place" or corners.length < 3:
        return none
    var numbers = [0.00001, float(corners.length)]
    for corner in corners:
        numbers = numbers + [corner.x, corner.y, corner.z]
    return EditStep(tool.name, "", 0, numbers, 0)

## The piece around face `face`: the faces sharing its `path`, or its
## connected component when it has none.
func piece(mesh: Mesh, face: int) -> str!:
    let index = mesh.find_attribute("path", 2)
    if index >= 0:
        let path = mesh.attribute_text(index, face)
        if path != "":
            return f"@path=\"{path}\""
    return f"connected({face})"

## Steps edit the input's polygons (GeometryData.operand: its mesh, or its
## instances realized; CAD alone must be tessellated first) and put them
## back beside its other components, which pass through.
func start(data: GeometryData, tag: str) -> GeometryData!:
    _ = data.operand()
    return data

## Every level picks on the polygons (an empty mesh before there are any).
func pick(data: GeometryData, level: int, quality: MeshQuality) -> Mesh?!:
    return mesh_of(data)

func mesh_of(data: GeometryData) -> Mesh!:
    return data.operand() else Mesh.empty()

## A step: its verb run as every mesh tool's cook runs it (verb_tools.run:
## a tool's verb, a primitive's, or a move), a verb of a reference surface
## (Shrinkwrap, Relax) reading the node's Reference input.
func run(step: EditStep, at: int, data: GeometryData, tag: str, reference: GeometryData?, tolerances: CadTolerances, quality: MeshQuality) -> GeometryData!:
    let mesh = mesh_of(data)
    if step.verb == set_step:
        return data.with_operand(with_set(step, mesh))
    return verb_tools.ran(step.invocation(reference, tolerances), data, mesh)

## Whether `step` runs on the compute worker: all but a gizmo's move and a
## sculpt stroke, which follow the pointer on the UI thread and cost only
## the points they move (a Boolean of large pieces, Corrective Smooth or
## Edge Transport on a 1M-face mesh would hold the UI for many frames).
func background(step: EditStep) -> bool!:
    return not (step.verb in ["Transform Components", "Sculpt"])

## A step run again made over `over`, its last result, where only
## positions change (verb_tools.run's `over`).
func run_over(step: EditStep, data: GeometryData, reference: GeometryData?, over: GeometryData) -> GeometryData!:
    if step.verb == set_step:
        return run(step, 0, data, "", reference, CadTolerances(), MeshQuality.medium())
    return verb_tools.ran(step.invocation(reference), data, mesh_of(data), over.mesh)

## `mesh` with selection set `step.text(0)` (a named group) holding the
## step's group (number 0: 0), its members and the group (1), or its
## members less the group (2).
func with_set(step: EditStep, mesh: Mesh) -> Mesh!:
    let name = step.text(0)
    if name == "" or name.starts_with("."):
        error(ui.invalid, "A selection set needs a name")
    if step.topology != 0 and step.topology != mesh.topology_hash():
        error(ui.invalid, "The components picked are gone: pick them again")
    let domain = [0, 0, 4, 2, 1][step.group_type] if step.group_type > 0 else 2
    let mode = int(step.numbers[0])
    var members = step.group
    if mode == 1:
        members = f"{name} {step.group}"
    elif mode == 2:
        members = f"{name} {removed(step.group)}"
    return Groups.with_group(mesh, name, domain, members)

## A group expression whose every term is taken away (`^`), so that after a
## set's name it removes what `group` names.
pub func removed(group: str) -> str:
    let terms: list[str] = []
    for term in group.split(" "):
        if term != "":
            terms.append("^" + term)
    return terms.join(" ")

## The step adding primitive `name` (a registry tool made from nothing:
## Box, Sphere, …) at its starting numbers: standing on the origin, a piece
## of its own.
func create(name: str) -> EditStep?!:
    let tool = tool_registry.find(name) else return none
    let place = tool.place else return none
    return EditStep(tool.name, "", 0, named(tool.starting(), place), 0)

## Primitive numbers `numbers` with Name piece on: a step's piece.
pub func named(numbers: list[float], place: ToolPlace) -> list[float]:
    let result = numbers.copy()
    while result.length <= place.piece:
        result.append(0.0)
    result[place.piece] = 1.0
    return result