dymokomi/luce-geocore
Source of release 0.7.7
0.7.7 / src / geometry_set / points.lucb
## The points family: a point cloud with point attributes and no faces
## (scatter results, copy targets, particle-like data). Positions are f32
## over an f64 origin, like a mesh's; every column is shared.
pub struct PointCloud:
var positions: Column[Float3]
var origin: Vector3
var attributes: AttributeStore
pub func count() -> usize:
return self.positions.count()
pub func point(index: usize) -> Vector3:
return self.positions.view()[index].vector().add(self.origin)
## Another owner of the same columns.
func shared_storage() -> PointCloud*!:
var attributes = try self.attributes.shared()
errdefer attributes.close()
let storage = try new PointCloud[1] --- in memory.heap
storage[0] = PointCloud(self.positions.share(), self.origin, attributes)
return storage.data
pub func close():
self.positions.release()
self.attributes.close()
## A cloud's columns, for Base readers (the codec): its positions
## (relative to its origin), its origin and its attributes.
pub func cloud_positions(cloud: const PointCloud*) -> const Float3[]:
return cloud.positions.view()
pub func cloud_origin(cloud: const PointCloud*) -> Vector3:
return cloud.origin
pub func cloud_store(cloud: const PointCloud*) -> const AttributeStore*:
return &cloud.attributes
## The change id of a cloud's positions (luce-geocore's column ids): equal
## ids mean equal positions, so a renderer keys its GPU copy on it.
pub func cloud_positions_id(cloud: const PointCloud*) -> u64:
return cloud.positions.id()
## `cloud` with `attributes` (taken, even on failure) in place of its
## own; the positions are shared.
pub func cloud_with_store(cloud: const PointCloud*, attributes: AttributeStore) -> PointCloud*!:
return try cloud_assembled(cloud.positions.share(), cloud.origin, attributes)
## A heap cloud owning these columns (taken, even on failure).
pub func cloud_assembled(positions: Column[Float3], origin: Vector3, attributes: AttributeStore) -> PointCloud*!:
var owned_positions = positions
var owned_attributes = attributes
let storage = new PointCloud[1] --- in memory.heap catch failure:
owned_positions.release()
owned_attributes.close()
error(failure.code, failure.message)
storage[0] = PointCloud(owned_positions, origin, owned_attributes)
return storage.data
## A point cloud of `mesh`'s points: its positions and point attributes,
## shared, and `like`'s other attributes (its detail ones), if given.
pub func cloud_of_mesh(mesh: const Mesh*, like: const PointCloud*? = none) -> PointCloud*!:
var attributes = AttributeStore()
errdefer attributes.close()
for index in 0..<(usize)mesh.attribute_count():
let entry = try mesh.attribute_entry((i64)index)
if entry.domain == Domain.point: try attributes.put(entry.share())
if let other = like:
for index in 0..<other.attributes.count():
let entry = other.attributes.entry(index)
if entry.domain != Domain.point: try attributes.put(entry.share())
let storage = try new PointCloud[1] --- in memory.heap
storage[0] = PointCloud(mesh_positions(mesh), mesh.position_origin(), attributes)
return storage.data
## The points of `cloud` whose bits `keep` holds (one per point), in order,
## with their attributes; detail attributes are shared. What Blast, Delete and
## the GSplats filters make.
pub func cloud_subset(cloud: const PointCloud*, keep: Bits) -> PointCloud*!:
let count = keep.population()
let parents = try new i32[count + 1] --- in memory.heap
defer free(parents) in memory.heap
var at: usize = 0
for point in 0..<cloud.count():
if keep.get(point):
parents[at] = (i32)point
at += 1
var positions = try Column[Float3].allocate(count)
defer positions.release()
let out = positions.span()
let source = cloud.positions.view()
for index in 0..<count: out[index] = source[(usize)parents[index]]
var attributes = AttributeStore()
defer attributes.close()
for index in 0..<cloud.attributes.count():
let entry = cloud.attributes.entry(index)
if entry.domain != Domain.point:
try attributes.put(entry.share())
continue
try attributes.put(try attribute_ops.remapped(entry, parents[..<count], count))
let handed = attributes
attributes = AttributeStore()
return try cloud_assembled(positions.take(), cloud.origin, handed)
## A mesh of `cloud`'s points and no faces, sharing its positions and point
## attributes: how the group language and the Code node read a cloud. A
## mesh without faces comes back as a cloud through `cloud_of_mesh`.
pub func cloud_mesh(cloud: const PointCloud*) -> Mesh*!:
let offsets = try Column[i32].filled(1, 0)
let made = try assembled(cloud.positions.share(), cloud.origin, offsets, Column[i32](), Column[i32](), Column[Float3](), Column[Vector3]())
errdefer mesh_dispose(made)
for index in 0..<cloud.attributes.count():
let entry = cloud.attributes.entry(index)
if entry.domain == Domain.point: try put_attribute(made, entry.share())
return made
## The set's point cloud, splats or plain points, for renderers.
pub func set_cloud(set: const GeometrySet*) -> const PointCloud*?:
let data = set.find(&points_component) else return none
return (const PointCloud*)data
## The set's point cloud when it is a splat cloud (its points carry orient,
## scale and opacity: splats/conventions.lucb), for renderers and exporters.
pub func set_splats(set: const GeometrySet*) -> const PointCloud*?:
let data = set.find(&points_component) else return none
let cloud = (const PointCloud*)data
if not is_splat_store(&cloud.attributes): return none
return cloud
## A heap copy of `set` whose splat cloud carries the raw 3DGS PLY names
## again (f_dc_*, opacity and scale_* as logit and logs, rot_* w, x, y, z, and
## `f_rest`; splats/unbake.lucb), its colors encoded back into the set's
## `gsplat_color_space`, which it drops: what a PLY writer writes. A set
## without splats is shared as it is.
pub func unbaked_set(set: const GeometrySet*) -> GeometrySet*!:
let storage = try set.storage()
errdefer dispose_set(storage)
let found = set_splats(set) else return storage
var cloud = found
# A cloud stood up from a Y-down file goes back down (the turn is its own inverse).
var turned: PointCloud*? = none
defer:
if let made_turn = turned: close_cloud((void*)made_turn)
let up = set.detail.find(up_axis_name, Domain.detail)
if up >= 0 and set.detail.entry((usize)up).text_at(0) == y_down_text:
let made_turn = try cloud_half_turned(found, true)
turned = made_turn
cloud = made_turn
let made = try cloud_with_store(cloud, try unbaked_store(&cloud.attributes, cloud.count(), detail_color_space(&set.detail)))
try storage.put(&points_component, (void*)made)
let index = storage.detail.find(color_space_name, Domain.detail)
if index >= 0: storage.detail.remove((usize)index)
let up_index = storage.detail.find(up_axis_name, Domain.detail)
if up_index >= 0: storage.detail.remove((usize)up_index)
return storage
## `cloud` turned a half turn about X, (x, y, z) -> (x, -y, -z), exactly:
## positions, origin and attributes (splats/axes.lucb); `back` undoes an
## earlier turn bit for bit.
pub func cloud_half_turned(cloud: const PointCloud*, back: bool = false) -> PointCloud*!:
var positions = try Column[Float3].copy_of(cloud.positions.view())
defer positions.release()
for at in 0..<positions.count():
let p = positions.view()[at]
positions.span()[at] = Float3(p.x, -p.y, -p.z)
let attributes = try half_turned_store(&cloud.attributes, cloud.count(), back)
return try cloud_assembled(positions.take(), Vector3(cloud.origin.x, -cloud.origin.y, -cloud.origin.z), attributes)
extend GeometrySet:
## How many Gaussian splats the set holds: its point cloud's points when
## they are splats, else 0.
pub func splat_count() -> i64:
let cloud = set_splats(&self) else return 0
return (i64)cloud.count()
## This set with its splats back in the raw 3DGS PLY names, for export
## (see unbaked_set); Bake GSplats is the way in.
pub func unbaked_splats() -> interop.Reference[GeometrySet]!:
return try adopt_set(try unbaked_set(&self))
pub let points_component: ComponentType = ComponentType("points", share_cloud, close_cloud, cloud_size, cloud_bytes, cloud_bounds, place_cloud, join_clouds, no_paths, keep_cloud, describe_cloud, "", no_trace)
func share_cloud(data: const void*) -> void*!:
return (void*)(try ((const PointCloud*)data).shared_storage())
func close_cloud(data: void*):
let cloud = (PointCloud*)data
cloud.close()
free(PointCloud[](cloud, 1)) in memory.heap
func cloud_size(data: const void*) -> usize:
return ((const PointCloud*)data).count()
func cloud_bytes(data: const void*, ids: IdSet*) -> usize!:
let cloud = (const PointCloud*)data
var total = try ids.charge(cloud.positions.id(), cloud.positions.bytes())
for index in 0..<cloud.attributes.count():
let entry = cloud.attributes.entry(index)
total += try ids.charge(entry.data.id(), entry.data.bytes())
return total + 64
func cloud_bounds(data: const void*, matrix: Matrix4, out: Bounds*) -> bool!:
let cloud = (const PointCloud*)data
if cloud.count() == 0: return false
*out = placed_box(positions_bounds(cloud.positions.view(), cloud.origin), matrix)
return true
## Positions move with the placement; attributes by role, and a splat
## cloud's orient, scale, restorient and SH exactly (splats/placement.lucb).
func place_cloud(data: const void*, placement: const Placement*) -> void*!:
let cloud = (const PointCloud*)data
var positions = try Column[Float3].allocate(cloud.count())
defer positions.release()
let out = positions.span()
let origin = placement.matrix.transform_point(cloud.origin)
for at in 0..<out.length:
out[at] = Float3.of(placement.matrix.transform_point(cloud.point(at)).subtract(origin))
var attributes = try placed_point_store(&cloud.attributes, cloud.count(), placement.matrix)
errdefer attributes.close()
let storage = try new PointCloud[1] --- in memory.heap
storage[0] = PointCloud(positions.take(), origin, attributes)
return (void*)storage.data
## Both clouds' points, the first's origin kept. Splat clouds agree first:
## SH padded to the larger degree, orient and restorient where either has
## them (splats/merging.lucb).
func join_clouds(first: const void*, second: const void*) -> void*!:
let a = (const PointCloud*)first
let b = (const PointCloud*)second
var positions = try Column[Float3].allocate(a.count() + b.count())
defer positions.release()
let out = positions.span()
for at in 0..<a.count(): out[at] = Float3.of(a.point(at).subtract(a.origin))
for at in 0..<b.count(): out[a.count() + at] = Float3.of(b.point(at).subtract(a.origin))
var left = try conformed_for_join(&a.attributes, a.count(), &b.attributes)
defer left.close()
var right = try conformed_for_join(&b.attributes, b.count(), &a.attributes)
defer right.close()
var attributes = AttributeStore()
errdefer attributes.close()
for side in 0..<(usize)2:
let store = &left if side == 0 else &right
for index in 0..<store.count():
let entry = store.entry(index)
if side == 1 and left.find(entry.name(), entry.domain) >= 0: continue
let first_index = left.find(entry.name(), entry.domain)
let second_index = right.find(entry.name(), entry.domain)
let pieces: attribute_ops.Piece[2] = [attribute_ops.Piece(left.entry((usize)first_index) if first_index >= 0 else none, a.count()),
attribute_ops.Piece(right.entry((usize)second_index) if second_index >= 0 else none, b.count())]
try attributes.put(try attribute_ops.concatenated(pieces))
let storage = try new PointCloud[1] --- in memory.heap
storage[0] = PointCloud(positions.take(), a.origin, attributes)
return (void*)storage.data
func no_paths(data: const void*, out: TableBuilder*) -> !:
_ = data
_ = out
## Points carry no paths: a filter keeps them all or none.
func keep_cloud(data: const void*, filter: const PathFilter*, keep: bool) -> void*!:
if filter.is_empty() == keep:
return (void*)(try ((const PointCloud*)data).shared_storage())
let storage = try new PointCloud[1] --- in memory.heap
storage[0] = PointCloud(Column[Float3](), Vector3(), AttributeStore())
return (void*)storage.data
func describe_cloud(data: const void*, out: Growing[u8]*) -> !:
try append_number(out, ((const PointCloud*)data).count())
try append_text(out, " cloud points")