dymokomi/luce-geocore
Source of release 0.7.6
0.7.6 / tests / mesh / codec_checks.lucb
## The geometry codec: a set with a mesh (attributes of every type, groups,
## text, single storage, corner uv), a point cloud, curves (handles, custom
## knots) and nested instances saves and loads back byte for byte, also
## with every column chunked; the root key reads alone; bad files fail.
from luce_geocore import geocore, kernel as geocore_kernel
import interop
from luce_std import math
import memory
import strings
from luce_std import files
import c
from io import path
from luce_geocore.geocore import Vector3, Mesh, MeshPrimitives, GeometrySet, AttributeKernels
from luce_geocore.kernel import Curves, CurvesBuilder, CurveType, Attribute, AttributeStore, Domain, DataType, set_curves, curves_type, empty_set, dispose_set, adopt_set, save_geometry, load_geometry, geometry_key, arc_curves, set_mesh, points_component, instances_component, Instances, PointCloud, cloud_positions, cloud_store, curves_knots, evaluated_positions, curves_dispose
from luce_geocore.core.growing import Growing
from luce_geocore.kernel import GeometryWriter, CrateReader, dtype_int32
pub func exercise() -> !:
let set = try sample_set()
defer set.release()
var file = Growing[u8]()
defer file.close()
try temporary(&file, "luce-geocore-codec.prism")
let saved = try save_geometry(set.value(), (str)file.view(), 42, 268435456)
assert(saved > 0 and (try geometry_key((str)file.view())) == 42)
let storage = try load_geometry((str)file.view())
let loaded = try adopt_set(storage)
defer loaded.release()
try same_sets(set.value(), loaded.value())
# Every column in chunks of at most 64 bytes.
let chunked = try save_geometry(set.value(), (str)file.view(), 7, 64)
assert(chunked > saved and (try geometry_key((str)file.view())) == 7)
let again = try adopt_set(try load_geometry((str)file.view()))
defer again.release()
try same_sets(set.value(), again.value())
# A file that is not a crate fails; a missing one has no key.
var bad = Growing[u8]()
defer bad.close()
try temporary(&bad, "luce-geocore-codec-bad.prism")
let native = try strings.copy((str)bad.view())
defer strings.release(native)
try files.write((c.str)native, "not a crate at all".bytes)
assert(not (try loads((str)bad.view())) and (try geometry_key((str)bad.view())) == 0)
try temporaries(&file)
## A column written from a temporary freed before the file is: `column`
## copies, so the file holds the values, not whatever reused the memory.
func temporaries(file: Growing[u8]*) -> !:
var writer = GeometryWriter()
defer writer.close()
try writer.crate.element("/geometry", "geometry")
try writer.crate.integer("version", 1)
try writer.crate.element("/geometry/probe", "probe")
var values = Growing[i32]()
for at in 0..<(usize)1000: try values.push((i32)(at * 7))
try writer.column("values", dtype_int32, values.count, 1, u8[]((u8*)values.items.data, values.count * 4))
values.close()
var noise = Growing[i32]()
defer noise.close()
for at in 0..<(usize)1000: try noise.push(-1)
_ = try writer.crate.save((str)file.view())
var reader = try CrateReader.open((str)file.view())
defer reader.close()
_ = try reader.next_element()
_ = try reader.next_element()
let header = (try reader.next_property()) else trap("probe")
assert(header.name == "values" and header.bytes == 4000)
var read = try new i32[1000] --- in memory.heap
defer free(read) in memory.heap
try reader.read_payload(u8[]((u8*)read.data, 4000))
for at in 0..<(usize)1000: assert(read[at] == (i32)(at * 7))
## Whether the file at `name` loads as geometry.
func loads(name: str) -> bool!:
let storage = load_geometry(name) catch failure:
if failure.code == memory.exhausted: error(failure.code, failure.message)
return false
dispose_set(storage)
return true
## A file name in the temporary directory.
func temporary(out: Growing[u8]*, name: str) -> !:
try out.assign(path.temp().bytes)
try out.push(47)
for byte in name.bytes: try out.push(byte)
## Mesh with attributes of every kind, a cloud of its points, curves and
## nested instances.
func sample_set() -> interop.Reference[GeometrySet]!:
let grid = try MeshPrimitives.grid(2.0, 3)
defer grid.release()
let weights = try grid.value().with_attribute("weight", 0, 1, [0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5], false, true)
defer weights.release()
let material = try weights.value().with_attribute("material", 2, 1, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], true)
defer material.release()
let wide = try material.value().with_attribute("big", 2, 1, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 10000000000.0], true)
defer wide.release()
let grouped = try wide.value().with_group("top", 2, [0, 4, 8])
defer grouped.release()
let named = try grouped.value().with_text_values("path", 2, "a\nb/c", [0, 1, 0, 1, 0, 1, 0, 1, -1])
defer named.release()
let single = try named.value().with_single_attribute("scale", 3, [2.0, 3.0])
defer single.release()
let mapped = try AttributeKernels.uv_project(single, 0.5)
defer mapped.release()
let base = try GeometrySet.of_mesh(mapped)
defer base.release()
let clouded = try base.value().with_cloud_of(mapped)
defer clouded.release()
# Curves with handles and an exact NURBS circle (custom knots).
var builder = CurvesBuilder()
defer builder.close()
try builder.begin(CurveType.bezier, true, 6)
try builder.point(Vector3(0.0, 0.0, 0.0))
builder.handles(Vector3(-0.2, 0.0, 0.0), Vector3(0.2, 0.1, 0.0))
try builder.point(Vector3(1.0, 1.0, 0.0))
builder.handles(Vector3(0.8, 1.0, 0.0), Vector3(1.2, 1.0, 0.3))
try builder.point(Vector3(2.0, 0.0, 0.5))
let bezier = try set_of(try builder.finish())
defer bezier.release()
let circle = try set_of(try arc_curves(1.0, 0.0, 360.0, 4, 2, 0, 8))
defer circle.release()
let curvy = try bezier.value().merged(circle)
defer curvy.release()
let full = try clouded.value().merged(curvy)
defer full.release()
let child = try GeometrySet.of_mesh(grid)
defer child.release()
let nested = try child.value().with_instance(child, Vector3(1.0, 2.0, 3.0), Vector3(0.1, 0.2, 0.3), Vector3(1.0, 2.0, 1.0), false)
defer nested.release()
return try full.value().with_instance(nested, Vector3(5.0, 0.0, 0.0), Vector3(), Vector3(1.0, 1.0, 1.0))
## A set of `curves` (taken, even on failure).
func set_of(curves: Curves*) -> interop.Reference[GeometrySet]!:
let storage = empty_set() catch failure:
curves_dispose(curves)
error(failure.code, failure.message)
storage.put(curves_type(), (void*)curves) catch failure:
dispose_set(storage)
error(failure.code, failure.message)
return try adopt_set(storage)
# mark: Comparison
func same_bytes(a: const u8[], b: const u8[]):
assert(a.length == b.length)
for at in 0..<a.length: assert(a[at] == b[at])
func bytes_of[T](values: const T[]) -> const u8[]:
return u8[]((u8*)values.data, values.length * memory.size_of(T))
func same_attribute(a: const Attribute*, b: const Attribute*):
assert(a.name() == b.name() and a.domain == b.domain and a.kind == b.kind and a.width == b.width)
assert(a.storage == b.storage and a.flags == b.flags and a.role == b.role and a.elements == b.elements)
same_bytes(a.data.view(), b.data.view())
same_bytes(a.table.view(), b.table.view())
func same_store(a: const AttributeStore*, b: const AttributeStore*):
assert(a.count() == b.count())
for index in 0..<a.count(): same_attribute(a.entry(index), b.entry(index))
func same_sets(a: const GeometrySet*, b: const GeometrySet*) -> !:
assert(a.component_count() == b.component_count())
for index in 0..<a.component_count():
assert(a.component(index).kind.name == b.component(index).kind.name)
if let x = set_mesh(a):
let y = set_mesh(b) else trap("mesh")
same_bytes(bytes_of(x.point_span()), bytes_of(y.point_span()))
same_bytes(bytes_of(x.offset_span()), bytes_of(y.offset_span()))
same_bytes(bytes_of(x.corner_span()), bytes_of(y.corner_span()))
same_bytes(bytes_of(x.triangle_span()), bytes_of(y.triangle_span()))
same_bytes(bytes_of(x.face_normal_span()), bytes_of(y.face_normal_span()))
assert(x.position_origin() == y.position_origin() and x.attribute_count() == y.attribute_count())
for index in 0..<x.attribute_count(): same_attribute(try x.attribute_entry(index), try y.attribute_entry(index))
if let x = set_curves(a):
let y = set_curves(b) else trap("curves")
same_bytes(bytes_of(x.position_span()), bytes_of(y.position_span()))
same_bytes(bytes_of(x.offset_span()), bytes_of(y.offset_span()))
same_bytes(bytes_of(curves_knots(x)), bytes_of(curves_knots(y)))
same_store(x.store(), y.store())
same_bytes(bytes_of(try evaluated_positions(x)), bytes_of(try evaluated_positions(y)))
if let found = a.find(points_component()):
let other = b.find(points_component()) else trap("points")
same_bytes(bytes_of(cloud_positions((const PointCloud*)found)), bytes_of(cloud_positions((const PointCloud*)other)))
same_store(cloud_store((const PointCloud*)found), cloud_store((const PointCloud*)other))
if let found = a.find(instances_component()):
let x = (const Instances*)found
let y = (const Instances*)(b.find(instances_component()) else trap("instances"))
assert(x.count() == y.count())
for index in 0..<x.count():
let p = x.item(index)
let q = y.item(index)
assert(p.translation == q.translation and p.rotation == q.rotation and p.scale == q.scale and p.visible == q.visible)
try same_sets(p.set, q.set)