Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.1

0.7.1 / src / set_verbs / verb.lucb

## Set verbs: operations whose input and output are whole geometry sets, for
## the families mesh verbs do not cover (curves, SDFs, volumes) and for the
## conversions between families. A set verb is described like a mesh verb
## (the same ParmSpec rows, Group and Group Type), so the node catalog is one
## generator over both registries (api.lucb's VerbCatalog). Generators take no
## input; some verbs take a second set (a profile, a boolean's other operand).
import interop
from luce_std import math
import memory
import errors
from core.growing import Growing
from core.shared import Column
from core.bits import Bits, BitsBuilder
from core.attributes import Domain, Attribute, AttributeStore, Storage
from math.vector import Vector3
from math.matrix import Matrix4
from geometries.polygon import Mesh, MeshPart, concatenated, mesh_share, mesh_dispose
from geometry_set import GeometrySet, ComponentType, PointCloud, points_component, cloud_origin, cloud_positions, cloud_store, cloud_with_store, set_mesh, empty_storage, dispose_set, adopt_set, geometry_set_type, mesh_component
from groups import GroupReport, group_bits, guess_domain
from verbs import ParmSpec, ParmKind, Parms, Verbs, group_domain, verb_at, verb_count
import curves
from curves import Curves, curves_with_store, curve_domain, set_curves, with_curves, curves_dispose, curves_type, curve_group_bits, guess_curve_domain, convert_curve_bits
from fields import Sdf, SdfOp, SparseGrid, primitive_sdf, combined_sdf, modified_sdf, release_sdf, sdf_type, set_sdf, release_grid, new_volume, add_grid, release_volume, volume_type, set_volume, sampled_sdf, mesh_level_set, points_level_set, surface_mesh, slice_mesh, push_mesh

## What a set verb reads.
pub struct SetVerbInput:
    ## The first input (an empty set for generators) and the second, if any.
    pub let set: const GeometrySet*
    pub let second: const GeometrySet*?
    ## The Group text and Group Type (0 guess, 1 points, 2 edges,
    ## 3 primitives, 4 vertices).
    pub let group: str
    pub let group_type: i64
    pub let parms: Parms

## What a set verb makes: the output set (taken by the run) and what the
## node's info line should say.
pub struct SetVerbOutput:
    pub var set: GeometrySet*? = none
    pub var warning: str = ""
    pub var misses: usize = 0

    pub func close():
        if let made = self.set: dispose_set(made)
        self.set = none

pub struct SetVerb:
    pub let name: str
    pub let category: str
    pub let description: str
    ## Inputs the node has (0: a generator), and whether the second must be
    ## connected.
    pub let inputs: usize
    pub let second_required: bool
    ## Whether the node has Group and Group Type rows.
    pub let grouped: bool
    pub let parms: const ParmSpec[]
    pub let cook: func(const SetVerbInput*, SetVerbOutput*) -> unit!

## Run `verb` on `set` (and `second`), returning the output and its report.
pub func run_set_verb(verb: const SetVerb*, set: const GeometrySet*, second: const GeometrySet*?, group: str, group_type: i64, numbers: const f64[]) -> SetVerbOutput!:
    if verb.inputs > 1 and verb.second_required and second == none: error(errors.invalid, "connect the second geometry input")
    var output = SetVerbOutput()
    errdefer output.close()
    let input = SetVerbInput(set, second, group, group_type, Parms(numbers))
    let cook = verb.cook
    try cook(&input, &output)
    if output.set == none: output.set = try set.storage()
    return output

# mark: Helpers for verbs

## The input set with `curves` (taken) in place of its curves.
pub func replaced_curves(input: const SetVerbInput*, made: Curves*, output: SetVerbOutput*) -> !:
    output.set = try with_curves(input.set, made)

## A set holding only `curves` (taken): what generators make.
pub func only_curves(made: Curves*, output: SetVerbOutput*) -> !:
    let storage = empty_storage() catch failure:
        curves_dispose(made)
        error(failure.code, failure.message)
    storage.put(curves_type(), (void*)made) catch failure:
        dispose_set(storage)
        error(failure.code, failure.message)
    output.set = storage

## The curves the input's group selects, as bits over curves: the group is
## read on its Group Type's domain (guessed from the names it uses) and
## converted to curves (a curve when all its points are members).
pub func selected_curves(input: const SetVerbInput*, source: const Curves*, output: SetVerbOutput*) -> BitsBuilder!:
    var domain = curve_domain
    match input.group_type:
        0: domain = guess_curve_domain(source, input.group, curve_domain)
        1, 4: domain = Domain.point
        3: domain = curve_domain
        _: error(errors.invalid, "curves have point and primitive groups, not edge groups")
    var report = GroupReport()
    var bits = try curve_group_bits(source, input.group, domain, &report)
    output.misses += report.misses
    if domain == curve_domain: return bits
    defer bits.close()
    return try convert_curve_bits(source, bits.view(), domain, curve_domain)

## The input's curves, or a warning that there are none (the set passes).
pub func input_curves(input: const SetVerbInput*, output: SetVerbOutput*) -> const Curves*?:
    let found = set_curves(input.set)
    if found == none: output.warning = "The input has no curves; it passes through"
    return found

## The set with `mesh` (taken) joined to its mesh, and without its curves
## when `drop_curves`.
pub func with_added_mesh(set: const GeometrySet*, mesh: Mesh*, drop_curves: bool) -> GeometrySet*!:
    let storage = set.storage() catch failure:
        mesh_dispose(mesh)
        error(failure.code, failure.message)
    errdefer dispose_set(storage)
    var made = mesh
    if let existing = set_mesh(set):
        let parts: MeshPart[2] = [MeshPart(existing, Matrix4()), MeshPart(mesh, Matrix4())]
        let joined = concatenated(parts) catch failure:
            mesh_dispose(mesh)
            error(failure.code, failure.message)
        defer joined.release()
        mesh_dispose(mesh)
        made = try mesh_share(joined.value())
    try storage.put(&mesh_component, (void*)made)
    if drop_curves: storage.remove(curves_type())
    return storage