Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.9

0.7.9 / src / set_verbs / api.lucb

## The set verb registry, Luce's view of it (GeometryVerbs), and the one
## catalog of every node verb (VerbCatalog): mesh verbs first, in their
## registry's order, then set verbs. luced-3d generates its node catalog from
## VerbCatalog alone, so both kinds get the same rows, Group handling,
## categories and descriptions.

## Every set verb's constructor, in menu order: the array's length is the
## registry's size, so a set verb added here is counted.
let set_makers: (func() -> SetVerb)[35] = [
    line_verb,
    circle_verb,
    arc_verb,
    spiral_verb,
    resample_verb,
    trim_verb,
    fillet_verb,
    reverse_curve_verb,
    set_type_verb,
    curve_to_mesh_verb,
    mesh_to_curve_verb,
    sdf_sphere_verb,
    sdf_box_verb,
    sdf_torus_verb,
    sdf_capsule_verb,
    sdf_cylinder_verb,
    sdf_boolean_verb,
    sdf_modify_verb,
    sdf_to_volume_verb,
    mesh_to_sdf_verb,
    volume_from_points_verb,
    convert_to_mesh_verb,
    volume_slice_verb,
    volume_verb,
    volume_visualization_verb,
    bake_gsplats_verb,
    gsplats_sh_degree_verb,
    crop_gsplats_verb,
    clean_gsplats_verb,
    reduce_gsplats_verb,
    gsplats_from_polygons_verb,
    gsplats_to_volume_verb,
    normals_from_gsplats_verb,
    delight_gsplats_verb,
    relight_gsplats_verb]

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

## How many set verbs the registry holds.
pub func set_verb_count() -> usize:
    return set_makers.length

## The set verb called `name`, if any.
pub func find_set_verb(name: str) -> SetVerb?:
    for index in 0..<set_verb_count():
        let verb = set_verb_at(index)
        if verb.name == name: return verb
    return none

## Luce's view of the set verbs: running one.
pub struct GeometryVerbs:
    let reserved: u8 = 0

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

    ## Run set verb `name` on `input` (and `second`): `group` read on
    ## `group_type`'s domain, the parameter values in order, and the texts
    ## of its text rows.
    pub static func run(name: str, input: interop.Reference[GeometrySet], second: interop.Reference[GeometrySet]?, group: str, group_type: i64, numbers: const f64[], texts: const str[]) -> interop.Reference[SetVerbResult]!:
        let verb = find_set_verb(name) else error(errors.invalid, "unknown operation")
        var other: const GeometrySet*? = none
        if let given = second: other = try given.get()
        var output = try run_set_verb(&verb, try input.get(), other, group, group_type, numbers, texts)
        let made = output.set else trap("a set verb made no set")
        output.set = none
        let storage = new SetVerbResult[1] --- in memory.heap catch failure:
            dispose_set(made)
            error(failure.code, failure.message)
        storage[0] = SetVerbResult(made, output.warning, output.misses)
        return try interop.Reference[SetVerbResult].adopt(storage.data, set_verb_result_type, memory.heap)

## What a set verb run made.
pub struct SetVerbResult:
    var set: GeometrySet*
    var text: str
    var missed: usize

    ## The output set.
    pub func geometry() -> interop.Reference[GeometrySet]!:
        return try adopt_set(try self.set.storage())

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

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

    ## Release the output set.
    pub func close():
        dispose_set(self.set)

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

## Every verb a node can run, mesh verbs first: what the node catalog is
## generated from. Indices past the mesh verbs name set verbs.
pub struct VerbCatalog:
    let reserved: u8 = 0

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

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

    ## Whether verb `index` runs on whole sets (else on the realized mesh).
    pub static func is_set(index: i64) -> bool:
        return checked(index) >= verb_count()

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

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

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

    ## Inputs of its node: 0 for generators, 2 when it reads a second set.
    pub static func inputs(index: i64) -> i64:
        return (i64)entry(checked(index)).inputs

    ## Whether its second input must be connected.
    pub static func second_required(index: i64) -> bool:
        return entry(checked(index)).second_required

    ## Whether its node has the Group and Group Type rows.
    pub static func grouped(index: i64) -> bool:
        return entry(checked(index)).grouped

    ## How many text rows its node shows in place of Group (the Volume
    ## node's Name): their texts go to the run as its `texts`, in order.
    pub static func text_row_count(index: i64) -> i64:
        return (i64)entry(checked(index)).text_rows.length

    ## Text row `row`'s label.
    pub static func text_row_label(index: i64, row: i64) -> str:
        return text_row_of(index, row).label

    ## Text row `row`'s tooltip.
    pub static func text_row_doc(index: i64, row: i64) -> str:
        return text_row_of(index, row).doc

    ## Whether text row `row` is a color ramp (its text luce-std ramp's
    ## numbers, comma apart).
    pub static func text_row_ramp(index: i64, row: i64) -> bool:
        return text_row_of(index, row).ramp

    ## The text row `row` reads when its text is empty.
    pub static func text_row_initial(index: i64, row: i64) -> str:
        return text_row_of(index, row).initial

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

    ## How many numbers its parameters take (a vector one per component): a
    ## run takes at most these, unless `trailing`.
    pub static func number_count(index: i64) -> i64:
        var total: usize = 0
        for spec in entry(checked(index)).parms: total += spec.components
        return (i64)total

    ## Whether numbers may follow its parameters (PolyDraw's drawn corners).
    pub static func trailing(index: i64) -> bool:
        return entry(checked(index)).trailing

    ## 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, 5 ramp.
    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 initial value for component `component` (a ramp's
    ## default points).
    pub static func parm_initial_at(index: i64, parm: i64, component: i64) -> f64:
        return parm_of(index, parm).initial_at((usize)component)

    ## 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

## What the catalog needs of either kind of verb.
struct Entry:
    let name: str
    let category: str
    let description: str
    let inputs: usize
    let second_required: bool
    let grouped: bool
    let parms: const ParmSpec[]
    let trailing: bool
    let text_rows: const TextRow[]

func entry(index: usize) -> Entry:
    if index < verb_count():
        let verb = verb_at(index)
        var rows: const TextRow[]
        return Entry(verb.name, verb.category, verb.description, verb.inputs, verb.second_required, verb.inputs > 0, verb.parms, verb.trailing, rows)
    let verb = set_verb_at(index - verb_count())
    return Entry(verb.name, verb.category, verb.description, verb.inputs, verb.second_required, verb.grouped, verb.parms, false, verb.text_rows)

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

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

func text_row_of(index: i64, row: i64) -> TextRow:
    let rows = entry(checked(index)).text_rows
    if row < 0 or (usize)row >= rows.length: trap("verb text row out of range")
    return rows[(usize)row]