dymokomi/luce-geocore
Source of release 0.7.7
0.7.7 / src / geometries / polygon / pack.lucb
## Packing a mesh's arrays in the GPU's layout (f32 vectors, u32 indices) for
## renderers in other packages; each costs a plain pass over its array.
## Where a mesh's principal curvatures come from: the analytic CAD attributes
## curvature.k1/k2 (corner domain) when present, else the discrete estimate
## per point. `key` names the data for GPU caches.
pub struct CurvatureSource:
pub let domain: i64
pub let key: u64
let k1: i64
let k2: i64
extend Mesh:
## Positions as x, y, z f32 triples relative to position_origin(); `out` holds
## 3 per point. Positions are stored this way, so this is a copy.
pub func pack_positions(out: f32[]) -> !:
let points = self.point_span()
if out.length != points.length * 3: error(errors.invalid, "position output needs three values per point")
memory.copy(Float3[]((Float3*)out.data, points.length), points, points.length)
## The elements of attribute `index` that may differ from the array it was
## edited from (attribute_parent), as points_changed and smooth_changed are
## for the positions and smooth normals: a Move's neighborhood.
pub func attribute_changed(index: i64) -> (usize, usize)!:
let entry = try self.attribute_entry(index)
if entry.storage == Storage.ragged: return (0, entry.elements)
let bytes = entry.data.changed()
let tuple = entry.tuple_bytes()
return (bytes.0 // tuple, (bytes.1 + tuple - 1) // tuple)
## Positions first..<end as f32 triples (relative to position_origin()) into `out`.
pub func pack_positions_range(first: usize, end: usize, out: f32[]) -> !:
let points = self.point_span()
if end > points.length or first > end or out.length != (end - first) * 3: error(errors.invalid, "position range leaves the mesh")
memory.copy(Float3[]((Float3*)out.data, end - first), points[first..<end], end - first)
## Smooth normals first..<end (of their domain) into `out`.
pub func pack_smooth_range(first: usize, end: usize, out: f32[]) -> !:
let normals = try self.smooth_normal_span()
if end > normals.length or first > end or out.length != (end - first) * 3: error(errors.invalid, "normal range leaves the mesh")
memory.copy(Float3[]((Float3*)out.data, end - first), normals[first..<end], end - first)
## Tuples first..<end of a width-3 float attribute as f32 triples.
pub func pack_attribute3_range(index: i64, first: usize, end: usize, out: f32[]) -> !:
let entry = try self.attribute_entry(index)
if entry.width != 3 or (entry.kind != DataType.float32 and entry.kind != DataType.float64) or entry.storage != Storage.array: error(errors.invalid, "range packing needs a float attribute of three components")
if end > entry.elements or first > end or out.length != (end - first) * 3: error(errors.invalid, "attribute range leaves the mesh")
let bytes = entry.data.view()
if entry.kind == DataType.float32:
memory.copy(out, f32[]((f32*)bytes.data, entry.elements * 3)[first * 3..<end * 3], (end - first) * 3)
return
let values = f64[]((f64*)bytes.data, entry.elements * 3)
for at in 0..<(end - first) * 3: out[at] = (f32)values[first * 3 + at]
## The corner of each vertex of triangles first..<first + out.length / 3.
pub func pack_triangles(first: usize, out: u32[]) -> !:
let triangles = self.triangle_span()
if first * 3 + out.length > triangles.length: error(errors.invalid, "triangle range leaves the mesh")
memory.copy(i32[]((i32*)out.data, out.length), triangles[first * 3..<first * 3 + out.length], out.length)
## The point of every corner.
pub func pack_corner_points(out: u32[]) -> !:
let corners = self.corner_span()
if out.length != corners.length: error(errors.invalid, "corner output needs one value per corner")
memory.copy(i32[]((i32*)out.data, out.length), corners, corners.length)
## The face of each of triangles first..<first + out.length.
pub func pack_triangle_faces(first: usize, out: u32[]) -> !:
let triangles = self.triangle_span()
let faces = try self.corner_face_span()
if (first + out.length) * 3 > triangles.length: error(errors.invalid, "triangle range leaves the mesh")
for at in 0..<out.length:
out[at] = (u32)faces[(usize)triangles[(first + at) * 3]]
## Smooth normals (the shading of a mesh without N) as f32 triples, one per
## point or per corner (`smooth_domain`), computed on first use.
pub func pack_smooth_normals(out: f32[]) -> !:
let normals = try self.smooth_normal_span()
if out.length != normals.length * 3: error(errors.invalid, "normal output needs three values per normal")
memory.copy(Float3[]((Float3*)out.data, normals.length), normals, normals.length)
## The domain of the smooth normals: 1 (corner) or 0 (point).
pub func smooth_domain() -> i64!:
return 1 if (try self.smooth_normals_corner_domain()) else 0
## The smooth normals' column id and the id it was updated from (0: none).
pub func smooth_ids() -> (u64, u64)!:
return try self.smooth_normals_ids()
## The two points of every edge.
pub func pack_edges(out: u32[]) -> !:
let edges = try self.edge_span()
if out.length != edges.length: error(errors.invalid, "edge output needs two values per edge")
memory.copy(i32[]((i32*)out.data, out.length), edges, edges.length)
## The first three components of attribute `index` per stored tuple (one for
## single storage), as f32; `out` holds 3 per tuple.
pub func pack_attribute3(index: i64, out: f32[]) -> !:
let entry = try self.attribute_entry(index)
if entry.width < 3 or entry.kind == DataType.boolean: error(errors.invalid, "packing needs a numeric attribute of three components")
let tuples = out.length // 3
let bytes = entry.data.view()
let single = (u8)entry.storage == 1
if entry.width == 3 and entry.kind == DataType.float32 and not single:
memory.copy(out, f32[]((f32*)bytes.data, tuples * 3), tuples * 3)
return
if entry.width == 3 and entry.kind == DataType.float64 and not single:
let values = f64[]((f64*)bytes.data, tuples * 3)
for at in 0..<tuples * 3: out[at] = (f32)values[at]
return
for at in 0..<tuples:
for component in 0..<(usize)3:
out[at * 3 + component] = (f32)entry.number(at, component)
## The column attribute `index` was written from (a copy-on-write edit), or 0.
pub func attribute_parent(index: i64) -> u64!:
return (try self.attribute_entry(index)).data.parent()
## Tuples attribute `index` stores: its domain's size, or 1 for single storage.
pub func attribute_tuples(index: i64) -> usize!:
let entry = try self.attribute_entry(index)
return 1 if (u8)entry.storage == 1 else entry.elements
pub func curvature_source() -> CurvatureSource!:
let k1 = self.attribute_on("curvature.k1", Domain.corner)
let k2 = self.attribute_on("curvature.k2", Domain.corner)
if k1 >= 0 and k2 >= 0:
let key = mix(try self.attribute_id(k1), try self.attribute_id(k2))
return CurvatureSource(1, key, k1, k2)
return CurvatureSource(0, try self.curvature_id(), -1, -1)
## (k1, k2, 0) per element of the source's domain into `out`.
pub func pack_curvature(source: const CurvatureSource*, out: f32[]) -> !:
if source.k1 >= 0:
let k1 = try self.attribute_entry(source.k1)
let k2 = try self.attribute_entry(source.k2)
if out.length != k1.elements * 3: error(errors.invalid, "curvature output needs three values per corner")
for at in 0..<k1.elements:
out[at * 3] = (f32)k1.number(at, 0)
out[at * 3 + 1] = (f32)k2.number(at, 0)
out[at * 3 + 2] = 0.0
return
let values = try self.curvature_span()
if out.length != values.length // 2 * 3: error(errors.invalid, "curvature output needs three values per point")
for at in 0..<values.length // 2:
out[at * 3] = values[at * 2]
out[at * 3 + 1] = values[at * 2 + 1]
out[at * 3 + 2] = 0.0
## The 98th percentile of |value| over the source, where `kind` picks the
## value from (k1, k2): 0 mean, 1 Gaussian, 2 k1, 3 k2, 4 the larger |k|.
## The color map spans it, so a few extreme points do not wash it out.
pub func curvature_range(source: const CurvatureSource*, kind: i64) -> f64!:
var count: usize = 0
if source.k1 >= 0: count = (try self.attribute_entry(source.k1)).elements
else: count = (try self.curvature_span()).length // 2
if count == 0: return 1.0
let packed = try new f32[count * 3] --- in memory.heap
defer free(packed) in memory.heap
try self.pack_curvature(source, packed)
let values = try new f64[count] --- in memory.heap
defer free(values) in memory.heap
var largest: f64 = 0.0
for at in 0..<count:
let k1 = (f64)packed[at * 3]
let k2 = (f64)packed[at * 3 + 1]
let value = (k1 + k2) * 0.5 if kind == 0 else (k1 * k2 if kind == 1 else (k1 if kind == 2 else (k2 if kind == 3 else math.max(math.abs(k1), math.abs(k2)))))
values[at] = math.abs(value) if math.is_finite(value) else 0.0
largest = math.max(largest, values[at])
if largest <= 1e-12: return 1.0
# A 4096-bucket histogram of |value| finds the percentile in one pass.
var buckets: u32[4096]
for value in values: buckets[(usize)math.min(4095.0, value / largest * 4096.0)] += 1
let wanted = count * 98 // 100
var seen: usize = 0
for bucket in 0..<(usize)4096:
seen += (usize)buckets[bucket]
if seen > wanted: return largest * (f64)(bucket + 1) / 4096.0
return largest