Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / src / edit_sdf.luc

## Edit SDF: the Edit framework's kind for signed distance fields. Its one
## level, Object, picks whole primitives of the SDF program on their proxies
## (each primitive's own shape, placed: luce-geocore SdfEdits), so moves keep
## the selection and subtracted shapes stay pickable. Primitives (sphere,
## box, torus, capsule, cylinder) are recipe steps added to the program;
## the tools set a primitive's boolean (Add, Subtract, Intersect), its blend
## radius (Blend), or delete it; the gizmo moves,
## rotates and scales primitives. Other components pass through.
from luce_geocore.geocore import Mesh, Groups, Domain, GroupType, SdfEdits, Vector3
from geometry_data import GeometryData
from edit_operations import EditStep
from edit_types import EditKind, EditLevel, EditTool, own_data, missing
from parameter_rows import Parameter, number
from luce_ui import ui

pub let kind_name = "Edit SDF"

pub func primitive_names() -> list[str]:
    return ["Sphere", "Box", "Torus", "Capsule", "Cylinder"]

pub func kind() -> EditKind:
    return EditKind(kind_name, levels(), tool_list(), primitive_names(), own_data, pick, run, create, see_through = true, smart = no_smart())

## Smart has nothing to pick among: the Object level alone.
func no_smart() -> list[int]:
    let result: list[int] = []
    return result

func levels() -> list[EditLevel]:
    return [EditLevel("Object", "sphere", Domain.face, GroupType.faces, piece), missing("Point"), missing("Edge"), missing("Face")]

## Add, Subtract and Intersect set the boolean combining a primitive with
## the shapes before it; Blend rounds that join (its radius on the tool bar;
## 0 is sharp); Delete removes primitives.
func tool_list() -> list[EditTool]:
    return [EditTool("Add", "SDF Mode", [], [0.0], none_rows(), icon = "boolean-union"),
            EditTool("Subtract", "SDF Mode", [], [1.0], none_rows(), icon = "boolean-subtract"),
            EditTool("Intersect", "SDF Mode", [], [2.0], none_rows(), icon = "boolean-intersect"),
            EditTool("Blend", "SDF Blend", [], [0.5], [number("Blend radius", 0, 0.5, 0.0, 100000.0, 0.05, "How far the join is rounded; 0 is sharp.", soft = 2.0)], icon = "sdf-blend"),
            EditTool("Delete", "SDF Delete", [], none_numbers(), none_rows(), icon = "trash")]

func none_rows() -> list[Parameter]:
    let result: list[Parameter] = []
    return result

func none_numbers() -> list[float]:
    let result: list[float] = []
    return result

## The primitive under a picked proxy face.
func piece(mesh: Mesh, face: int) -> str!:
    return f"@sdf_prim={int(mesh.attribute_value(mesh.find_attribute("sdf_prim", 2), face))}"


## The proxies of the SDF's primitives (made once per result), with what
## the last step created as `.selection`. Steps read their groups on this
## same mesh (a selection may be written as `.selection`).
func pick(data: GeometryData, level: int) -> Mesh?!:
    return picked(data)

func picked(data: GeometryData) -> Mesh!:
    let shown = proxies(data)
    if data.created != "":
        return Groups.with_group(shown, ".selection", 2, data.created)
    return shown

func proxies(data: GeometryData) -> Mesh!:
    if let cached = data.pick_cache:
        return cached
    let made = SdfEdits.proxies(data.geometry)
    data.pick_cache = made
    return made

func run(step: EditStep, at: int, data: GeometryData, tag: str, reference: GeometryData?) -> GeometryData!:
    if step.verb.starts_with("Create "):
        return created(step, data)
    if SdfEdits.count(data.geometry) == 0:
        error(ui.invalid, "Edit SDF has no primitives to edit yet: create one, or connect an SDF")
    let shown = picked(data)
    if step.topology != 0 and step.topology != shown.topology_hash():
        error(ui.invalid, "The SDF input changed. Undo the upstream change or clear this node's edits.")
    if step.verb == "Transform Components":
        return GeometryData(geometry = SdfEdits.transformed(data.geometry, shown, step.group, step.numbers))
    if step.verb == "SDF Mode":
        return GeometryData(geometry = SdfEdits.with_boolean(data.geometry, shown, step.group, int(step.numbers[0]), -1.0))
    if step.verb == "SDF Blend":
        return GeometryData(geometry = SdfEdits.with_boolean(data.geometry, shown, step.group, -1, step.numbers[0]))
    if step.verb == "SDF Delete":
        return GeometryData(geometry = SdfEdits.without(data.geometry, shown, step.group))
    error(ui.invalid, f"{step.verb} does not apply to SDF primitives")

## The step adding primitive `name` at the origin, combined by Add. Its
## numbers: three sizes (sphere radius; box width, height, depth; torus ring
## and tube radii; capsule and cylinder radius and height), the position,
## the boolean (0 add, 1 subtract, 2 intersect) and the blend radius.
func create(name: str) -> EditStep?!:
    let shape = shape_of(name)
    if shape < 0:
        return none
    let sizes = [[1.0, 0.0, 0.0], [2.0, 2.0, 2.0], [1.0, 0.3, 0.0], [0.5, 2.0, 0.0], [1.0, 2.0, 0.0]][shape]
    return EditStep("Create " + name, "", 0, sizes + [0.0, 0.0, 0.0, 0.0, 0.0], 0)

## Primitive `name`'s shape number (SdfEdits.with_primitive), or -1.
func shape_of(name: str) -> int:
    return primitive_names().index_of(name) else -1

## `data` with the primitive of `step` added, selected as what it created.
func created(step: EditStep, data: GeometryData) -> GeometryData!:
    let v = step.numbers
    if v.length < 8:
        error(ui.invalid, f"{step.verb} needs eight numbers")
    let shape = shape_of(step.verb[7..])
    if shape < 0:
        error(ui.invalid, f"Edit SDF cannot create {step.verb[7..]}")
    let index = SdfEdits.count(data.geometry)
    let result = GeometryData(geometry = SdfEdits.with_primitive(data.geometry, shape, v[..<3], Vector3(v[3], v[4], v[5]), int(v[6]), v[7]))
    result.created = f"@sdf_prim={index}"
    return result