dymokomi/luce-geocore
Source of release 0.7.9
0.7.9 / src / geometry_set / instances.lucb
## The instances family: other sets placed by a transform, without copying
## them (Houdini's packed primitives, Blender's instances). Groups and
## assemblies produce them; the viewport draws each prototype under its
## instances' transforms, and `realized` bakes them into one mesh only when
## a node needs polygons.
##
## Prototypes are the distinct sets instanced; each instance is a row of one
## shared column (its prototype and placement). A share of the component
## shares that column (O(1), the same ids) and makes its own share of every
## prototype (O(prototypes)): each owner then traces exactly the prototypes'
## Luce objects it owns (see ComponentType.share).
##
## Rows may carry attributes, as Houdini's packed primitives' points do: an
## AttributeStore on the point domain, one element per row (`path`, the
## instance's own place in a scene, is the common one). Shares, joins,
## filters and the codec keep them; rows added after them take zero or "".
## One instance, as `item` reads it: its prototype set and placement.
pub struct Instance:
pub let set: GeometrySet*
pub let translation: Vector3
pub let rotation: Vector3
pub let scale: Vector3
## Hidden instances still cook; only the viewport skips them.
pub let visible: bool
pub func matrix() -> Matrix4:
return Matrix4.compose(self.translation, self.rotation, self.scale)
## One instance as stored: its prototype and placement.
pub struct InstanceRow:
pub let prototype: i32
pub let visible: bool
pub let translation: Vector3
pub let rotation: Vector3
pub let scale: Vector3
pub struct Instances:
var prototypes: Growing[GeometrySet*]
## Rows 0..<count are instances; the column may hold spare rows while a
## builder appends.
var rows: Column[InstanceRow]
var size: usize
## Per-row attributes on the point domain, `size` elements each.
var attributes: AttributeStore
pub func count() -> usize:
return self.size
## Drop the prototypes' components that trace objects (GeometrySet.plain_storage).
pub func drop_traced():
for prototype in self.prototypes.view(): prototype.drop_traced()
pub func prototype_count() -> usize:
return self.prototypes.count
pub func prototype(index: usize) -> const GeometrySet*:
return self.prototypes.items[index]
pub func row(index: usize) -> InstanceRow:
return self.rows.view()[index]
## The change id of the instance rows (a share keeps it).
pub func rows_id() -> u64:
return self.rows.id()
## The rows' attributes (point domain, one element per row).
pub func store() -> const AttributeStore*:
return &self.attributes
## Row `index`'s text attribute `name` ("" without one).
pub func row_text(name: str, index: usize) -> str:
let found = self.attributes.find(name, Domain.point)
if found < 0 or index >= self.size: return ""
return self.attributes.entry((usize)found).text_at(index)
## Add a row attribute (taken, even on failure): on the point domain, one
## element per row; it replaces one of the same name.
pub func put_attribute(attribute: Attribute) -> !:
var owned = attribute
if owned.domain != Domain.point or owned.elements != self.size:
owned.release()
error(errors.invalid, "an instance attribute needs one point-domain element per row")
try self.attributes.put(owned)
pub func item(index: usize) -> Instance:
let row = self.rows.view()[index]
return Instance(self.prototypes.items[(usize)row.prototype], row.translation, row.rotation, row.scale, row.visible)
## Add prototype `set` (taken, even on failure); its index.
pub func add_prototype(set: GeometrySet*) -> usize!:
self.prototypes.push(set) catch failure:
dispose_set(set)
error(failure.code, failure.message)
return self.prototypes.count - 1
## Add an instance of prototype `prototype`.
pub func add_instance(prototype: usize, placement: const Placement*, visible: bool) -> !:
if prototype >= self.prototypes.count: error(errors.invalid, "instance prototype out of range")
if self.size == self.rows.count():
# Grow by doubling: appends are amortized O(1).
var grown = try Column[InstanceRow].allocate(self.size * 2 if self.size > 0 else 4)
memory.copy(grown.span()[..<self.size], self.rows.view()[..<self.size], self.size)
self.rows.release()
self.rows = grown.take()
let out = try self.rows.writable()
out[self.size] = InstanceRow((i32)prototype, visible, placement.translation, placement.rotation, placement.scale)
# Row attributes grow with the rows: zero or "" for the new one.
if self.attributes.count() > 0:
var grown = try joined_store(&self.attributes, self.size, none, 1)
self.attributes.close()
self.attributes = grown
self.size += 1
## Add an instance of its own prototype `set` (taken, even on failure).
pub func add(set: GeometrySet*, placement: const Placement*, visible: bool) -> !:
let prototype = try self.add_prototype(set)
try self.add_instance(prototype, placement, visible)
## Add every instance of `other`, sharing its prototypes; into an empty
## component its rows are shared as they are.
pub func add_all(other: const Instances*) -> !:
let base = self.prototypes.count
for at in 0..<other.prototypes.count: _ = try self.add_prototype(try other.prototypes.items[at].storage())
if self.size == 0 and base == 0:
self.rows.release()
self.rows = other.rows.share()
self.size = other.size
var shared = try other.attributes.shared()
self.attributes.close()
self.attributes = shared
return
# The rows' attributes joined once, not grown row by row.
let before = self.size
var joined = AttributeStore()
if self.attributes.count() > 0 or other.attributes.count() > 0:
joined = try joined_store(&self.attributes, before, &other.attributes, other.size)
errdefer joined.close()
var held = self.attributes
self.attributes = AttributeStore()
defer held.close()
for at in 0..<other.size:
let row = other.rows.view()[at]
let placement = Placement(row.translation, row.rotation, row.scale, Matrix4.compose(row.translation, row.rotation, row.scale))
try self.add_instance(base + (usize)row.prototype, &placement, row.visible)
self.attributes.close()
self.attributes = joined
## A new, empty instances component.
pub func new_instances() -> Instances*!:
let storage = try new Instances[1] --- in memory.heap
storage[0] = Instances(Growing[GeometrySet*](), Column[InstanceRow](), 0, AttributeStore())
return storage.data
## The row attributes of `first` (`first_rows` rows) followed by those of
## `second` (`second_rows`; none: rows without attributes): every name of
## either, zero or "" where one lacks it.
func joined_store(first: const AttributeStore*, first_rows: usize, second: const AttributeStore*?, second_rows: usize) -> AttributeStore!:
var result = AttributeStore()
errdefer result.close()
let point = Domain.point
for pass in 0..<(usize)2:
var from_store: const AttributeStore*? = none
if pass == 0: from_store = first
else: from_store = second
let store = from_store else continue
for index in 0..<store.count():
let name = store.entry(index).name()
if result.find(name, point) >= 0: continue
let a = first.find(name, point)
var b: i64 = -1
if let other = second: b = other.find(name, point)
var pieces: attribute_ops.Piece[2] = [attribute_ops.Piece(first.entry((usize)a) if a >= 0 else none, first_rows), attribute_ops.Piece(none, second_rows)]
if let other = second:
if b >= 0: pieces[1] = attribute_ops.Piece(other.entry((usize)b), second_rows)
try result.put(try attribute_ops.concatenated(pieces))
return result
pub let instances_component: ComponentType = ComponentType("instances", share_instances, close_instances, instances_size, instances_bytes, instances_bounds, place_instances, join_instances, instance_paths, filter_instances, describe_instances, "", trace_instances)
## Another owner: the rows shared, each prototype shared anew (see the top).
func share_instances(data: const void*) -> void*!:
let copy = try new_instances()
copy.add_all((const Instances*)data) catch failure:
close_instances((void*)copy)
error(failure.code, failure.message)
return (void*)copy
func close_instances(data: void*):
let instances = (Instances*)data
for set in instances.prototypes.view(): dispose_set(set)
instances.prototypes.close()
instances.rows.release()
instances.attributes.close()
free(Instances[](instances, 1)) in memory.heap
func instances_size(data: const void*) -> usize:
return ((const Instances*)data).count()
func instances_bytes(data: const void*, ids: IdSet*) -> usize!:
let instances = (const Instances*)data
# The rows in use, not a builder's spare capacity.
var total: usize = 64 + (try ids.charge(instances.rows.id(), instances.size * memory.size_of(InstanceRow))) + instances.attributes.bytes()
for set in instances.prototypes.view(): total += try set_bytes(set, ids)
return total
## Each prototype's own box is found once and placed per row (a prototype
## instanced many times is not measured again for each).
func instances_bounds(data: const void*, matrix: Matrix4, out: Bounds*) -> bool!:
let instances = (const Instances*)data
let count = instances.prototype_count()
if count == 0: return false
let boxes = try new Bounds[count] --- in memory.heap
defer free(boxes) in memory.heap
let known = try new u8[count] in memory.heap
defer free(known) in memory.heap
var found = false
for at in 0..<instances.count():
let row = instances.row(at)
let prototype = (usize)row.prototype
if known[prototype] == 0:
var own = Bounds(Vector3(), Vector3())
known[prototype] = 1 if (try set_bounds(instances.prototype(prototype), Matrix4(), &own)) else 2
boxes[prototype] = own
if known[prototype] != 1: continue
let box = placed_box(boxes[prototype], matrix.multiply(Matrix4.compose(row.translation, row.rotation, row.scale)))
*out = union_box(*out, box) if found else box
found = true
return found
## Placing instances wraps them in one instance under the placement, which
## keeps any composition of rotations and nonuniform scales exact.
func place_instances(data: const void*, placement: const Placement*) -> void*!:
let inner = try empty_storage()
inner.put(&instances_component, try share_instances(data)) catch failure:
dispose_set(inner)
error(failure.code, failure.message)
let wrapped = new_instances() catch failure:
dispose_set(inner)
error(failure.code, failure.message)
wrapped.add(inner, placement, true) catch failure:
close_instances((void*)wrapped)
error(failure.code, failure.message)
return (void*)wrapped
func join_instances(first: const void*, second: const void*) -> void*!:
let joined = try new_instances()
errdefer close_instances((void*)joined)
try joined.add_all((const Instances*)first)
try joined.add_all((const Instances*)second)
return (void*)joined
func instance_paths(data: const void*, out: TableBuilder*) -> !:
for set in ((const Instances*)data).prototypes.view(): try set_paths(set, out)
## Each prototype filtered once; instances keep their rows, and those of
## prototypes left empty stay, empty.
func filter_instances(data: const void*, filter: const PathFilter*, keep: bool) -> void*!:
let source = (const Instances*)data
let result = try new_instances()
errdefer close_instances((void*)result)
for set in source.prototypes.view(): _ = try result.add_prototype(try set_filtered(set, filter, keep))
result.rows.release()
result.rows = source.rows.share()
result.size = source.size
var shared = try source.attributes.shared()
result.attributes.close()
result.attributes = shared
return (void*)result
func describe_instances(data: const void*, out: Growing[u8]*) -> !:
try append_number(out, ((const Instances*)data).count())
try append_text(out, " scene branches")
## Each prototype once: the Luce objects this owner holds.
func trace_instances(data: const void*, visit: func(ownership.Object*, void*) -> unit, context: void*):
for set in ((const Instances*)data).prototypes.view(): set.trace(visit, context)