Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.4

0.7.4 / src / verbs / api.lucb

## The verb registry and how Luce runs verbs. The node catalog is generated
## from it: each verb is a node kind whose parameters are the verb's, plus
## Group and Group Type.

## Every verb's constructor, in menu order: the array's length is the
## registry's size, so a verb added here is counted.
let makers: (func() -> Verb)[30] = [
    transform_verb,
    smooth_verb,
    mountain_verb,
    peak_verb,
    flatten_verb,
    snap_verb,
    delete_verb,
    reverse_verb,
    triangulate_verb,
    duplicate_verb,
    split_verb,
    inset_verb,
    extrude_verb,
    subdivide_verb,
    fuse_verb,
    clean_verb,
    bevel_verb,
    fill_verb,
    dissolve_verb,
    loop_cut_verb,
    bridge_verb,
    merge_verb,
    subdivision_verb,
    crease_verb,
    clip_verb,
    connect_verb,
    edge_slide_verb,
    mirror_verb,
    spin_verb,
    polydraw_verb]

## Verb `index` in menu order.
pub func verb_at(index: usize) -> Verb:
    if index >= makers.length: trap("verb index out of range")
    let make = makers[index]
    return make()

## How many mesh verbs the registry holds.
pub func verb_count() -> usize:
    return makers.length

## The mesh verb called `name`, if any.
pub func find_verb(name: str) -> Verb?:
    for index in 0..<verb_count():
        let verb = verb_at(index)
        if verb.name == name: return verb
    return none

## Luce's view of the verbs: their descriptions (for the node catalog) and
## running one.
pub struct Verbs:
    let reserved: u8 = 0

    ## How many mesh verbs there are.
    pub static func count() -> i64:
        return (i64)verb_count()

    ## The index of verb `name`, or -1.
    pub static func index_of(name: str) -> i64:
        for index in 0..<verb_count():
            if verb_at(index).name == name: return (i64)index
        return -1

    ## Verb `index`'s node kind and recipe name.
    pub static func name(index: i64) -> str:
        return verb_at(checked(index)).name

    ## Verb `index`'s menu category.
    pub static func category(index: i64) -> str:
        return verb_at(checked(index)).category

    ## Verb `index`'s one-line description.
    pub static func description(index: i64) -> str:
        return verb_at(checked(index)).description

    ## The domain its kernel works on (0 points, 1 corners, 2 faces, 4 edges).
    pub static func domain(index: i64) -> i64:
        return (i64)(u8)verb_at(checked(index)).domain

    ## Whether verb `index` changes the topology (else positions and
    ## attributes only).
    pub static func changes_topology(index: i64) -> bool:
        return verb_at(checked(index)).changes_topology

    ## How many parameters verb `index` has.
    pub static func parm_count(index: i64) -> i64:
        return (i64)verb_at(checked(index)).parms.length

    ## Parameter `parm`'s label.
    pub static func parm_label(index: i64, parm: i64) -> str:
        return parm_of(index, parm).label

    ## 0 number, 1 integer, 2 toggle, 3 vector, 4 menu.
    pub static func parm_kind(index: i64, parm: i64) -> i64:
        return (i64)(u8)parm_of(index, parm).kind

    ## Parameter `parm`'s initial value (each component's, for a vector).
    pub static func parm_initial(index: i64, parm: i64) -> f64:
        return parm_of(index, parm).initial

    ## Parameter `parm`'s lower bound.
    pub static func parm_minimum(index: i64, parm: i64) -> f64:
        return parm_of(index, parm).minimum

    ## Parameter `parm`'s upper bound.
    pub static func parm_maximum(index: i64, parm: i64) -> f64:
        return parm_of(index, parm).maximum

    ## Parameter `parm`'s slider maximum (0: its maximum).
    pub static func parm_soft(index: i64, parm: i64) -> f64:
        return parm_of(index, parm).soft

    ## How many numbers parameter `parm` takes (3 for a vector).
    pub static func parm_components(index: i64, parm: i64) -> i64:
        return (i64)parm_of(index, parm).components

    ## A menu parameter's choices, `Label|Label`.
    pub static func parm_choices(index: i64, parm: i64) -> str:
        return parm_of(index, parm).choices

    ## Parameter `parm`'s tooltip.
    pub static func parm_doc(index: i64, parm: i64) -> str:
        return parm_of(index, parm).doc

    ## Run verb `name` on `mesh`: `group` (empty: everything) read on
    ## `group_type`'s domain (0 guess, 1 points, 2 edges, 3 faces,
    ## 4 corners), the parameter values in order.
    ## `symmetry` mirrors the group across X, Y or Z (1..3); `keep` selects
    ## the group carried through the verb instead of what it made.
    pub static func run(name: str, mesh: interop.Reference[Mesh], group: str, group_type: i64, numbers: const f64[], symmetry: i64 = 0, keep: bool = false) -> interop.Reference[VerbResult]!:
        let verb = find_verb(name) else error(errors.invalid, "unknown operation")
        var product = try run_verb(&verb, try mesh.get(), group, group_type, numbers, symmetry, keep)
        let storage = new VerbResult[1] --- in memory.heap catch failure:
            product.close()
            error(failure.code, failure.message)
        storage[0] = VerbResult(product)
        return try interop.Reference[VerbResult].adopt(storage.data, verb_result_type, memory.heap)

func checked(index: i64) -> usize:
    if index < 0 or (usize)index >= verb_count(): trap("verb index out of range")
    return (usize)index

func parm_of(index: i64, parm: i64) -> ParmSpec:
    let verb = verb_at(checked(index))
    if parm < 0 or (usize)parm >= verb.parms.length: trap("verb parameter out of range")
    return verb.parms[(usize)parm]

## What a run made: its mesh, its output selection and its warning.
pub struct VerbResult:
    var run: VerbRun

    ## The output mesh.
    pub func mesh() -> interop.Reference[Mesh]!:
        return try mesh_adopt(try mesh_share(self.run.mesh))

    ## The components the verb made or moved, on the output mesh.
    pub func selection() -> interop.Reference[Selection]!:
        let guard = (u64)self.run.mesh.topology_hash()
        return try adopt_selection(self.run.selection.share(), self.run.selection_count, self.run.selection_domain, guard, GroupReport())

    ## The warning for the node's info line, or "".
    pub func warning() -> str:
        return self.run.warning

    ## Numbers in the group that named no element.
    pub func misses() -> i64:
        return (i64)self.run.misses

    ## Release the run's mesh and selection.
    pub func close():
        self.run.close()

## VerbResult's interop type.
pub let verb_result_type: interop.Type[VerbResult] = interop.Type[VerbResult]("VerbResult", VerbResult.close, closeable = true)