Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.9

0.7.9 / 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, splats) 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
import strings
from core.growing import Growing
from core.shared import Column
from core.bits import Bits, BitsBuilder
from core.attributes import Domain, Attribute, AttributeStore, Storage, DataType
from math.vector import Vector3
from math.matrix import Matrix4
from geometries.triangle_index import TriangleIndex
from geometries.polygon import Mesh, MeshPart, concatenated, mesh_share, mesh_dispose, mesh_adopt
from core.float3 import Float3
from core.strings import TableBuilder
from geometry_set import GeometrySet, ComponentType, PointCloud, points_component, cloud_origin, cloud_positions, cloud_store, cloud_with_store, cloud_subset, cloud_mesh, cloud_assembled, cloud_half_turned, cloud_of_mesh, set_cloud, set_mesh, empty_storage, dispose_set, adopt_set, geometry_set_type, mesh_component, set_put_detail
from splats import BakeOptions, ColorSpace, SplatNaming, baked_store, splat_naming, with_sh_degree, is_splat_store, color_space_name, color_space_text, up_axis_name, y_down_text, CropRegion, crop_flags, CleanOptions, clean_flags, ReduceOptions, reduced_cloud, Floats, float_attribute, float32_values, capture_voxel, to_linear, scale_name, color_name, SurfaceMesh, SurfaceColor, SurfaceSplatOptions, surface_splats, NormalOptions, splat_normals
from splats import ScreenView, ScreenRegion, RegionShape, FootprintCloud, OccluderCloud, Occluding, select_footprints, transformed_store, recolored_store, opacity_store, orient_name, opacity_name
from groups import GroupReport, GroupType, group_bits, guess_domain, is_blank, Selection, selection_bits, adopt_selection
from core.verb_specs import ParmSpec, ParmKind, Parms, filled_parms
from verbs import Verbs, verb_at, verb_count, ComponentTransform
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, GridClass, box_grid, share_grid, 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, VolumeLook, restyled, grid_in_group, SplatDensityOptions, splat_density

## 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
    ## The texts of the verb's text rows, in order (fewer read "").
    pub let texts: const str[]

    ## Text row `index`'s text, trimmed; "" past those given.
    pub func text(index: usize) -> str:
        return strings.trim(self.texts[index]) if index < self.texts.length else ""

## 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!
    ## Text rows the node shows in place of Group (a verb has them or Group,
    ## not both; none when left out); their texts reach the cook as `texts`.
    pub let text_rows: const TextRow[]

## A text row of a set verb's node: a name it reads (the Volume node's grid
## name), or with `ramp` a color ramp, whose text is luce-std ramp's numbers
## comma apart (the node's ramp editor writes them; empty takes `initial`).
pub struct TextRow:
    pub let label: str
    pub let doc: str
    pub let ramp: bool = false
    pub let initial: str = ""

## Run `verb` on `set` (and `second`) with the texts of its text rows,
## 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[], texts: const str[]) -> SetVerbOutput!:
    if verb.inputs > 1 and verb.second_required and second == none: error(errors.invalid, "connect the second geometry input")
    var values = try filled_parms(verb.parms, numbers)
    defer values.close()
    var output = SetVerbOutput()
    errdefer output.close()
    let input = SetVerbInput(set, second, group, group_type, Parms(values.view()), texts)
    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!:
    let named = try GroupType.of(input.group_type)
    if named == GroupType.edges: error(errors.invalid, "curves have point and primitive groups, not edge groups")
    var domain = named.domain(curve_domain)
    if named == GroupType.guess: domain = try guess_curve_domain(source, input.group, curve_domain)
    # A curve's points are its vertices too.
    if domain == Domain.corner: domain = Domain.point
    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