Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / geometry_nodes.luc

## Nodes that work on the whole geometry set rather than its realized mesh:
## set verbs (curves, fields and conversions, through luce-geocore's
## GeometryVerbs), placement nodes (every component placed together),
## attribute nodes (mesh kernels on the mesh, SetAttributes on curves and
## point clouds) and Blast or Delete on point clouds.
from luce_geocore.geocore import GeometryVerbs, GeometrySet, SetAttributes, Mesh
from luce_3d.graphics import Vector3
from geometry_data import GeometryData
from node_evaluation import evaluate_operator
import node_catalog
from edit_operations import verb_numbers
from luce_std import math
from luce_ui import ui

## A set verb node's result, with a warning when the verb passed its input
## through or the group named elements that do not exist. A verb with text
## rows reads them from the node's texts, from slot 0.
pub func evaluate_set_verb(kind: str, v: list[float], text: list[str], data: GeometryData, second: GeometryData?) -> GeometryData!:
    var other: GeometrySet? = none
    if let input = second:
        other = input.geometry
    let result = GeometryVerbs.run(kind, data.geometry, other, text[0], int(v[node_catalog.group_type_slot]), verb_numbers(kind, v), text)
    let made = GeometryData(geometry = result.geometry())
    made.warning = result.warning()
    if made.warning == "" and result.misses() > 0:
        made.warning = f"{result.misses()} group numbers name no element"
    return made

# -- Placement nodes: the whole set is placed, every component with it.

## Whether node kind `kind` is a placement node.
pub func is_placement(kind: str) -> bool:
    return kind in ["Mirror", "CopyTransform", "CopyToPoints", "MatchSize"]

## Mirror, copies and Match Size on the whole set: meshes, curves, fields
## and CAD are placed together.
pub func evaluate_placement(kind: str, v: list[float], data: GeometryData, second: GeometryData?) -> GeometryData!:
    let same_size = Vector3(1.0, 1.0, 1.0)
    if kind == "Mirror":
        let axis = int(v[0])
        let mirrored = data.transformed(Vector3(), Vector3(), Vector3(-1.0 if axis == 0 else 1.0, -1.0 if axis == 1 else 1.0, -1.0 if axis == 2 else 1.0))
        return data.merged(mirrored) if v[1] != 0.0 else mirrored
    if kind == "MatchSize":
        if not data.geometry.has_bounds():
            return data
        let bounds = data.geometry.bounds()
        let size = bounds.maximum.subtract(bounds.minimum)
        let longest = math.max(size.x, math.max(size.y, size.z))
        if longest < 0.000001:
            return data
        let scale = v[0] / longest
        let center = bounds.minimum.add(bounds.maximum).multiply_scalar(0.5)
        return data.transformed(center.multiply_scalar(-scale), Vector3(), Vector3(scale, scale, scale))
    # Copies: the source set at each offset or target point, placed and
    # joined in one pass by luce-geocore.
    if kind == "CopyToPoints":
        let target = second else error(ui.invalid, "Connect target points")
        let targets = target.operand() else error(ui.invalid, "Connect target points")
        if targets.point_count() <= 0:
            return GeometryData()
        return data.with_copies(data.geometry.copies_at_points(targets, Vector3(), same_size), targets.point_count())
    let count = int(v[0])
    if count <= 0:
        return GeometryData()
    return data.with_copies(data.geometry.copies_along(count, Vector3(v[1], v[2], v[3]), Vector3(), same_size), count)

# -- Attribute nodes: the mesh kernels on the mesh, luce-geocore's
# SetAttributes on curves and point clouds.

## Blast or Delete on a point cloud: the points group expression
## `expression` names go (blank: all of them), or only they stay when `keep`.
pub func points_deleted(data: GeometryData, expression: str, keep: bool) -> GeometryData!:
    let made = GeometryData(geometry = SetAttributes.deleted(data.geometry, expression.trim(), keep))
    made.warning = data.warning
    return made

## An attribute node's result on every component its domain reaches;
## `operand` is the mesh the mesh kernels run on (none without polygons).
pub func evaluate_attributes(kind: str, v: list[float], text: list[str], data: GeometryData, operand: Mesh?) -> GeometryData!:
    var result = data
    if let mesh = operand:
        result = data.with_operand(evaluate_operator(kind, v, text, mesh, none))
    if not SetAttributes.present(result.geometry):
        if operand == none:
            return data.passed("The input has no elements this node works on; it passes through")
        return result
    if kind == "Name":
        if operand == none:
            return data.passed("The input has no polygons to name; it passes through")
        return result
    var geometry = result.geometry
    if kind == "Color":
        geometry = SetAttributes.filled(geometry, "Cd", 0, [v[0], v[1], v[2]], false, false, 0.0)
    elif kind in ["AttributeCreate", "AttributeRandomize"]:
        let values: list[float] = []
        for component in 0..<int(v[1]):
            values.append(v[3 + component])
        geometry = SetAttributes.filled(geometry, text[1], int(v[0]), values, v[2] != 0.0, kind == "AttributeRandomize", v[7])
    elif kind == "AttributeDelete":
        geometry = SetAttributes.removed(geometry, text[1], int(v[0]))
    elif kind == "AttributeRename":
        geometry = SetAttributes.renamed(geometry, text[1], text[2], int(v[0]))
    elif kind == "AttributePromote" and int(v[0]) in [0, 3] and int(v[1]) in [0, 3]:
        geometry = SetAttributes.promoted(geometry, text[1], int(v[0]), int(v[1]))
    elif kind == "Selection Group" and int(v[0]) != 4:
        geometry = SetAttributes.grouped(geometry, text[1], int(v[0]), text[0].trim())
    else:
        result.warning = f"{kind} works on meshes; curves and points pass through"
        return result
    let made = GeometryData(geometry = geometry)
    made.warning = result.warning
    return made