dymokomi/luce-geocore
Source of release 0.7.6
0.7.6 / src / geometries / polygon / attributes.lucb
## Attribute access and attribute-only edits. Every edit shares the topology
## columns, the query index and every other attribute's column: adding one
## costs that column alone.
extend Mesh:
pub func attribute_count() -> i64:
return (i64)self.attributes.count()
## The index of attribute `name` on domain `domain` (0..4), or -1.
pub func find_attribute(name: str, domain: i64) -> i64:
if domain < 0 or domain > 4: return -1
return self.attributes.find(name, Domain((u8)domain))
## `find_attribute` for Base callers.
pub func attribute_on(name: str, domain: Domain) -> i64:
return self.attributes.find(name, domain)
pub func attribute_name(index: i64) -> str!:
try checked(index, self.attributes.count())
return self.attributes.entry((usize)index).name()
pub func attribute_domain(index: i64) -> i64!:
try checked(index, self.attributes.count())
return (i64)(u8)self.attributes.entry((usize)index).domain
pub func attribute_width(index: i64) -> i64!:
try checked(index, self.attributes.count())
return (i64)self.attributes.entry((usize)index).width
pub func attribute_integer(index: i64) -> bool!:
try checked(index, self.attributes.count())
return self.attributes.entry((usize)index).integer()
## The scalar type (core.attributes.DataType) and storage (0 array, 1 single).
pub func attribute_type(index: i64) -> i64!:
try checked(index, self.attributes.count())
return (i64)(u8)self.attributes.entry((usize)index).kind
pub func attribute_storage(index: i64) -> i64!:
try checked(index, self.attributes.count())
return (i64)(u8)self.attributes.entry((usize)index).storage
## The change id of attribute `index`'s column.
pub func attribute_id(index: i64) -> u64!:
try checked(index, self.attributes.count())
return self.attributes.entry((usize)index).data.id()
pub func attribute_value(index: i64, element: i64, component: i64 = 0) -> f64!:
try checked(index, self.attributes.count())
let entry = self.attributes.entry((usize)index)
try checked(component, entry.width)
try checked(element, (try self.domain_size(entry.domain)))
return entry.number((usize)element, (usize)component)
## `float32` stores the values as f32 instead of f64.
pub func with_attribute(name: str, domain: i64, width: i64, values: const f64[], integer: bool = false, float32: bool = false) -> interop.Reference[Mesh]!:
let count = try self.domain_count(domain)
if width < 1 or width > 4 or values.length != (usize)(count * width):
error(errors.invalid, "attribute length must match its geometry domain")
return try self.with_attribute_entry(try Attribute.from_numbers(name, Domain((u8)domain), (usize)width, values, integer, Storage.array, float32))
## One value for the whole domain: O(1) whatever the domain's size.
pub func with_single_attribute(name: str, domain: i64, values: const f64[], integer: bool = false) -> interop.Reference[Mesh]!:
let count = try self.domain_count(domain)
if values.length < 1 or values.length > 4:
error(errors.invalid, "a single attribute value has 1..4 components")
var attribute = try Attribute.from_numbers(name, Domain((u8)domain), values.length, values, integer, Storage.single)
attribute.elements = (usize)count
return try self.with_attribute_entry(attribute)
## A snapshot with `attribute` added or replacing its (name, domain), for
## Base kernels. Takes the attribute's share.
pub func with_attribute_entry(attribute: Attribute) -> interop.Reference[Mesh]!:
var owned = attribute
let result = self.snapshot() catch failure:
owned.release()
error(failure.code, failure.message)
errdefer result.release()
try result.value().attributes.put(owned)
return result
pub func without_attribute(name: str, domain: i64) -> interop.Reference[Mesh]!:
let result = try self.snapshot()
let found = result.value().find_attribute(name, domain)
if found >= 0: result.value().attributes.remove((usize)found)
return result
pub func renamed_attribute(name: str, replacement: str, domain: i64) -> interop.Reference[Mesh]!:
let index = self.find_attribute(name, domain)
if index < 0:
error(errors.invalid, "attribute to rename does not exist")
let renamed = try self.attributes.entry((usize)index).renamed(replacement)
let result = try self.with_attribute_entry(renamed)
if replacement != name:
let old = result.value().find_attribute(name, domain)
result.value().attributes.remove((usize)old)
return result
## Numeric domain conversion: continuous values average incident source
## elements, integers keep the first contributor. Isolated targets get zero.
pub func promoted_attribute(name: str, domain: i64, target: i64) -> interop.Reference[Mesh]!:
let index = self.find_attribute(name, domain)
if index < 0:
error(errors.invalid, "attribute to promote does not exist")
if domain == 4 or target == 4:
error(errors.invalid, "edge attributes are not promoted yet")
let entry = self.attributes.entry((usize)index)
let count = (usize)(try self.domain_count(target))
let source_count = (usize)(try self.domain_count(domain))
if entry.storage == Storage.ragged:
return try self.with_attribute_entry(try self.promoted_array(entry, domain, target, count, source_count))
let width = entry.width
let values = try new f64[count * width] --- in memory.heap
defer free(values) in memory.heap
let weights = try new usize[count] --- in memory.heap
defer free(weights) in memory.heap
for at in 0..<values.length:
values[at] = 0.0
for at in 0..<count:
weights[at] = 0
let corners = self.corners.view()
let corner_faces = try self.corner_faces()
let contributions = count if domain == target or domain == 3 else (source_count if target == 3 else corners.length)
for at in 0..<contributions:
let source = at if domain == target or target == 3 else (0 if domain == 3 else ((usize)corners[at] if domain == 0 else ((usize)corner_faces[at] if domain == 2 else at)))
let destination = at if domain == target or domain == 3 else (0 if target == 3 else ((usize)corners[at] if target == 0 else ((usize)corner_faces[at] if target == 2 else at)))
for component in 0..<width:
if not entry.integer() or weights[destination] == 0:
values[destination * width + component] += entry.number(source, component)
weights[destination] += 1
if not entry.integer():
for at in 0..<count:
if weights[at] > 0:
for component in 0..<width:
values[at * width + component] /= (f64)weights[at]
return try self.with_attribute(name, target, (i64)width, values, entry.integer())
## An array attribute promoted from `domain` to `target`: each target element's array
## is the arrays of the source elements that reach it, in source order, joined
## (promoting to the detail joins them all; from the detail, every element copies it).
func promoted_array(entry: const Attribute*, domain: i64, target: i64, count: usize, source_count: usize) -> Attribute!:
let corners = self.corners.view()
let corner_faces = try self.corner_faces()
let contributions = count if domain == target or domain == 3 else (source_count if target == 3 else corners.length)
let item = entry.width * 4
# The contribution pairs (source, destination), then each destination's size.
let sources = try new u32[contributions] --- in memory.heap
defer free(sources) in memory.heap
let destinations = try new u32[contributions] --- in memory.heap
defer free(destinations) in memory.heap
var offsets = try Column[u8].allocate((count + 1) * 4)
errdefer offsets.release()
let bounds = u32[]((u32*)offsets.span().data, count + 1)
for at in 0..<count + 1: bounds[at] = 0
for at in 0..<contributions:
let source = at if domain == target or target == 3 else (0 if domain == 3 else ((usize)corners[at] if domain == 0 else ((usize)corner_faces[at] if domain == 2 else at)))
let destination = at if domain == target or domain == 3 else (0 if target == 3 else ((usize)corners[at] if target == 0 else ((usize)corner_faces[at] if target == 2 else at)))
sources[at] = (u32)source
destinations[at] = (u32)destination
bounds[destination + 1] += (u32)entry.item_count(source)
for at in 0..<count: bounds[at + 1] += bounds[at]
var values = try Column[u8].allocate((usize)bounds[count] * item)
errdefer values.release()
let filled = try new u32[count] in memory.heap
defer free(filled) in memory.heap
let output = values.span()
let input = entry.data.view()
for at in 0..<contributions:
let source = (usize)sources[at]
let destination = (usize)destinations[at]
let items = entry.item_count(source)
let from_item = entry.first_item(source)
let to_item = (usize)bounds[destination] + (usize)filled[destination]
memory.copy(output[to_item * item..<(to_item + items) * item], input[from_item * item..<(from_item + items) * item], items * item)
filled[destination] += (u32)items
return try Attribute.ragged(entry.name(), Domain((u8)target), entry.kind, entry.width, offsets, values)
## A snapshot with array attribute `name` on `domain`: element i has `counts[i]` items
## of `width` (1, or 3 for vectors) components, `values` holding them all in order;
## i32 items when `integer`, else f32.
pub func with_array_attribute(name: str, domain: i64, integer: bool, width: i64, counts: const i64[], values: const f64[]) -> interop.Reference[Mesh]!:
let elements = (usize)(try self.domain_count(domain))
if counts.length != elements: error(errors.invalid, "an array attribute needs one count per element")
if width != 1 and width != 3: error(errors.invalid, "array attribute items have 1 or 3 components")
var offsets = try Column[u8].allocate((elements + 1) * 4)
errdefer offsets.release()
let spans = u32[]((u32*)offsets.span().data, elements + 1)
var total: usize = 0
for at in 0..<elements:
if counts[at] < 0: error(errors.invalid, "an array attribute count is negative")
spans[at] = (u32)total
total += (usize)counts[at]
spans[elements] = (u32)total
if values.length != total * (usize)width: error(errors.invalid, "array attribute values do not match their counts")
var column = try Column[u8].allocate(values.length * 4)
errdefer column.release()
let bytes = column.span()
for (at, value) in values.indexed():
if integer: memory.write[i32]((void*)&bytes[at * 4], (i32)value)
else: memory.write[f32]((void*)&bytes[at * 4], (f32)value)
return try self.with_attribute_entry(try Attribute.ragged(name, Domain((u8)domain), DataType.int32 if integer else DataType.float32, (usize)width, offsets, column))
## Whether attribute `index` is an array attribute (Houdini's f[]@, i[]@, v[]@).
pub func attribute_is_array(index: i64) -> bool!:
try checked(index, self.attributes.count())
return self.attributes.entry((usize)index).storage == Storage.ragged
## How many items element `element` of array attribute `index` holds (0 for others).
pub func attribute_item_count(index: i64, element: i64) -> i64!:
try checked(index, self.attributes.count())
let entry = self.attributes.entry((usize)index)
try checked(element, (try self.domain_size(entry.domain)))
return (i64)entry.item_count((usize)element)
## Component `component` of item `item` of element `element` of array attribute `index`.
pub func attribute_item(index: i64, element: i64, item: i64, component: i64 = 0) -> f64!:
try checked(index, self.attributes.count())
let entry = self.attributes.entry((usize)index)
try checked(element, (try self.domain_size(entry.domain)))
try checked(item, entry.item_count((usize)element))
try checked(component, entry.width)
return entry.item((usize)element, (usize)item, (usize)component)
## Element `element` of array attribute `index` as text for a spreadsheet: "[1, 2, 3]",
## vectors as "[(1, 0, 0.5), …]"; "" for an attribute that is not an array.
pub func attribute_array_text(index: i64, element: i64) -> interop.Owned[str]!:
try checked(index, self.attributes.count())
let entry = self.attributes.entry((usize)index)
try checked(element, (try self.domain_size(entry.domain)))
var text = Growing[u8]()
defer text.close()
if entry.storage == Storage.ragged:
try append_bytes(&text, "[")
for at in 0..<entry.item_count((usize)element):
if at > 0: try append_bytes(&text, ", ")
if entry.width > 1: try append_bytes(&text, "(")
for component in 0..<entry.width:
if component > 0: try append_bytes(&text, ", ")
let value = entry.item((usize)element, at, component)
var room: u8[48]
let shown = (strings.format(room, f"{(i64)value}") else "") if entry.kind == DataType.int32 else (strings.format(room, f"{(f32)value}") else "")
try append_bytes(&text, shown)
if entry.width > 1: try append_bytes(&text, ")")
try append_bytes(&text, "]")
return try interop.copy_text((str)text.view())
## Construct topology and its imported corner data once ## Construct topology and its imported corner data once, without rebuilding
## triangulation, edge tables and BVH for each attribute. Empty spans omit it.
pub static func from_corner_data(points: const Vector3[], sizes: const i64[], corners: const i64[], uv: const f64[], normals: const f64[]) -> interop.Reference[Mesh]!:
if (uv.length != 0 and uv.length != corners.length * 2) or (normals.length != 0 and normals.length != corners.length * 3):
error(errors.invalid, "imported corner attribute size mismatch")
let result = try make_mesh(points, sizes, corners)
errdefer result.release()
if uv.length > 0: try result.value().attributes.put(try Attribute.from_numbers("uv", Domain.corner, 2, uv, false))
if normals.length > 0: try result.value().attributes.put(try Attribute.from_numbers("N", Domain.corner, 3, normals, false))
return result
## Bytes of the columns this mesh holds, shared ones included (lazy
## topology caches are not).
pub func column_bytes() -> i64:
let columns = self.positions.bytes() + self.offsets.bytes() + self.corners.bytes() + self.normals.bytes() + self.triangles.bytes() + self.precise.bytes()
return (i64)(columns + self.attributes.bytes())
func append_bytes(out: Growing[u8]*, text: str) -> !:
for byte in text.bytes: try out.push(byte)