Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.7

0.7.7 / src / geometry_set / set.lucb

## One geometry value: the result every node cooks, and what the viewport,
## the spreadsheet and the Edit engine read. A set holds typed components, at
## most one of each family: a mesh, points, instances, and families other
## packages register (luce-cad's CAD models). The container never downcasts:
## each family is a ComponentType, a table of functions over its data (after
## Blender's GeometryComponent and its attribute accessor tables). Copying a
## set shares every component (O(columns)); a component changes by being
## replaced, so a published set is immutable and any thread may read it.
## Detail attributes belong to the set itself (detail.lucb), as in Houdini:
## they exist with no components at all.
import interop
from luce_std import math
import memory
import ownership
import sync
import errors
from core.growing import Growing
from core.idset import IdSet
from core.strings import TableBuilder, table_text
from core.paths import PathFilter
from core.geometry import Bounds
from core.float3 import Float3
from core.shared import Column
from core.attributes import Attribute, AttributeStore, Domain, DataType, Role
import core.attribute_ops
from math.vector import Vector3
from math.matrix import Matrix4
from splats import placed_point_store, conformed_for_join, is_splat_store, LinearPart, turn_rotations, unbaked_store, detail_color_space, color_space_name, half_turned_store, up_axis_name, y_down_text
from geometries.polygon import Mesh, MeshPart, concatenated, mesh_share, mesh_dispose, mesh_adopt, mesh_unique_bytes, mesh_paths, mesh_filtered, positions_bounds, mesh_positions, assembled, put_attribute
from core.bits import Bits

## Where a component is placed: the matrix, and the translation, rotation
## (XYZ radians) and scale it was composed from, which instances keep.
pub struct Placement:
    pub let translation: Vector3
    pub let rotation: Vector3
    pub let scale: Vector3
    pub let matrix: Matrix4

    pub static func compose(translation: Vector3, rotation: Vector3, scale: Vector3) -> Placement:
        return Placement(translation, rotation, scale, Matrix4.compose(translation, rotation, scale))

## A component family: what a set may do with a component's data without
## knowing its layout. Data is an owned heap object; `share` makes another
## owner of the same contents and `close` gives one up.
##
## The tracing rule: `trace` visits the Luce objects this owner holds, and
## every strong reference it visits must be owned by this owner exactly
## once. A family whose data holds Luce objects (luce-cad's CAD models,
## instances' prototype sets) therefore cannot share that data by count:
## each share clones those references (sharing plain columns is fine), or
## the cycle collector counts one edge per owner against a single
## reference and traps.
pub struct ComponentType:
    pub let name: str
    pub let share: func(const void*) -> void*!
    pub let close: func(void*) -> unit
    ## Its elements (faces, points, instances, surfaces); 0 is empty.
    pub let size: func(const void*) -> usize
    ## Bytes of the arrays `ids` has not seen, which it then records.
    pub let bytes: func(const void*, IdSet*) -> usize!
    ## Its bounds under a matrix, into the Bounds; false when it has none.
    pub let bounds: func(const void*, Matrix4, Bounds*) -> bool!
    pub let placed: func(const void*, const Placement*) -> void*!
    ## Both inputs' elements, the first's first.
    pub let joined: func(const void*, const void*) -> void*!
    ## The object paths its elements carry, interned into the table.
    pub let paths: func(const void*, TableBuilder*) -> unit!
    ## The elements whose path the filter matches kept (or removed).
    pub let filtered: func(const void*, const PathFilter*, bool) -> void*!
    ## A short summary appended to the text ("12 faces").
    pub let describe: func(const void*, Growing[u8]*) -> unit!
    ## Why a set with this component cannot become one mesh ("" when it can:
    ## its faces join the mesh, or it has none).
    pub let unrealizable: str
    ## Luce-visible objects the data holds (see interop.Type's trace).
    pub let trace: func(const void*, func(ownership.Object*, void*) -> unit, void*) -> unit

func trace_nothing(data: const void*, visit: func(ownership.Object*, void*) -> unit, context: void*):
    _ = data
    _ = visit
    _ = context

pub let no_trace: func(const void*, func(ownership.Object*, void*) -> unit, void*) -> unit = trace_nothing

## Whether a component holds Luce-visible objects: its trace visits one.
func holds_objects(entry: const Component*) -> bool:
    var count: usize = 0
    let trace = entry.kind.trace
    trace(entry.data, count_object, (void*)&count)
    return count > 0

func count_object(object: ownership.Object*, context: void*):
    _ = object
    *(usize*)context += 1

# mark: Registry =====================================================================

var registry_lock: sync.Mutex
var registry: (const ComponentType*?)[64]
var registered: usize = 0

## Record a family. Putting a component registers its type, so a package
## need not run anything at startup.
pub func register(kind: const ComponentType*) -> !:
    registry_lock.lock()
    defer registry_lock.unlock()
    for at in 0..<registered:
        let known = registry[at] else continue
        if known == kind: return
        if known.name == kind.name: error(errors.invalid, "two component families share a name")
    if registered == registry.length: error(errors.limit, "at most 64 component families")
    registry[registered] = kind
    registered += 1

## The registered family called `name`.
pub func component_type(name: str) -> const ComponentType*?:
    registry_lock.lock()
    defer registry_lock.unlock()
    for at in 0..<registered:
        let known = registry[at] else continue
        if known.name == name: return known
    return none

# mark: The container ================================================================

pub struct Component:
    pub let kind: const ComponentType*
    pub var data: void*

pub struct GeometrySet:
    var entries: Growing[Component]
    ## The set's own attributes (domain detail, one element each).
    var detail: AttributeStore

    pub func init():
        self.entries = Growing[Component]()
        self.detail = AttributeStore()

    ## The data of the `kind` component, if there is one (for other packages'
    ## component families, as the rest of this block).
    pub func find(kind: const ComponentType*) -> const void*?:
        for entry in self.entries.view():
            if entry.kind == kind: return entry.data
        return none

    ## Put `data` (taken, closed on failure) as the `kind` component,
    ## replacing any. Empty data is dropped instead.
    pub func put(kind: const ComponentType*, data: void*) -> !:
        let close = kind.close
        register(kind) catch failure:
            close(data)
            error(failure.code, failure.message)
        let size = kind.size
        let empty = size(data) == 0
        for index in 0..<self.entries.count:
            if self.entries.items[index].kind != kind: continue
            let old = self.entries.items[index].data
            if empty:
                close(data)
                self.remove_at(index)
            else:
                self.entries.items[index].data = data
            close(old)
            return
        if empty:
            close(data)
            return
        self.entries.push(Component(kind, data)) catch failure:
            close(data)
            error(failure.code, failure.message)

    func remove_at(index: usize):
        for at in index + 1..<self.entries.count: self.entries.items[at - 1] = self.entries.items[at]
        self.entries.count -= 1

    ## A storage of this set that another thread may own: its components but
    ## those holding Luce-visible objects (whose kind traces them: CAD models),
    ## which belong to the thread that made them, dropped from the instances'
    ## prototypes too. Arrays stay shared (their counts are atomic).
    pub func plain_storage() -> GeometrySet*!:
        let storage = try self.storage()
        storage.drop_traced()
        return storage

    ## Drop the components whose kind traces objects, through the instances.
    pub func drop_traced():
        var index: usize = 0
        while index < self.entries.count:
            let entry = self.entries.items[index]
            if entry.kind == &instances_component:
                ((Instances*)entry.data).drop_traced()
                index += 1
            elif holds_objects(&entry):
                self.remove_at(index)
                let close = entry.kind.close
                close(entry.data)
            else:
                index += 1

    ## Drop the `kind` component, if there is one.
    pub func remove(kind: const ComponentType*):
        for index in 0..<self.entries.count:
            if self.entries.items[index].kind == kind:
                let data = self.entries.items[index].data
                self.remove_at(index)
                let close = kind.close
                close(data)
                return

    ## A set sharing every component: O(columns).
    func shared() -> GeometrySet!:
        var copy = GeometrySet()
        errdefer copy.close()
        copy.detail.close()
        copy.detail = try self.detail.shared()
        try copy.entries.reserve(self.entries.count)
        for entry in self.entries.view():
            let share = entry.kind.share
            copy.entries.items[copy.entries.count] = Component(entry.kind, try share(entry.data))
            copy.entries.count += 1
        return copy

    ## A heap set sharing every component, for instances and adoption.
    pub func storage() -> GeometrySet*!:
        var copy = try self.shared()
        let storage = new GeometrySet[1] --- in memory.heap catch failure:
            copy.close()
            error(failure.code, failure.message)
        storage[0] = copy
        return storage.data

    ## Component `index`, for Base walkers (the codec), below component_count().
    pub func component(index: i64) -> const Component*:
        return &self.entries.items[(usize)index]

    pub func close():
        for entry in self.entries.view():
            let close = entry.kind.close
            close(entry.data)
        self.entries.close()
        self.detail.close()

    func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
        for entry in self.entries.view():
            let trace = entry.kind.trace
            trace(entry.data, visit, context)

pub let geometry_set_type: interop.Type[GeometrySet] = interop.Type[GeometrySet]("GeometrySet", GeometrySet.close, GeometrySet.trace, closeable = true)

## Adopt heap storage as a Luce-visible set (disposing it on failure).
pub func adopt_set(storage: GeometrySet*) -> interop.Reference[GeometrySet]!:
    return try interop.Reference[GeometrySet].adopt(storage, geometry_set_type, memory.heap)

pub func dispose_set(storage: GeometrySet*):
    storage.close()
    free(storage) in memory.heap

## A heap set holding nothing yet.
pub func empty_storage() -> GeometrySet*!:
    return try new GeometrySet() in memory.heap

## Append `text` to a growing byte buffer.
pub func append_text(out: Growing[u8]*, text: str) -> !:
    try out.reserve(out.count + text.length)
    if text.length > 0: memory.copy(out.items[out.count..], text.bytes, text.length)
    out.count += text.length

## Append a whole number in decimal.
pub func append_number(out: Growing[u8]*, value: usize) -> !:
    var digits: u8[24]
    var at: usize = 24
    var rest = value
    while true:
        at -= 1
        digits[at] = (u8)(48 + rest % 10)
        rest = rest // 10
        if rest == 0: break
    try append_text(out, (str)digits[at..])