Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-ply

Source of release 0.1.0

0.1.0 / tests / ply / format_checks.lucb

## Generic PLY: one mesh fixture in ASCII, binary little- and big-endian;
## a point cloud with a list property; round trips through every format.
import memory
import strings
from luce_std import math
from luce_geocore.geocore import Mesh
from luce_geocore.kernel import GeometrySet, Attribute, Domain, DataType, PointCloud, set_mesh, set_detail, dispose_set, points_component, cloud_store, cloud_positions
from luce_ply.kernel import Format, Warnings, decode, encode, invalid
from build import Bytes

## The mesh fixture in `format`: five points with normals, uchar colors, a
## double and a ushort property; a quad and a triangle with an int
## material; an `edge` element; and a comment.
pub func mesh_fixture(format: Format, out: Bytes*) -> !:
    out.big = format == Format.big_endian
    try out.text("ply\n")
    match format:
        .ascii: try out.text("format ascii 1.0\n")
        .little_endian: try out.text("format binary_little_endian 1.0\n")
        _: try out.text("format binary_big_endian 1.0\n")
    try out.text("comment made by the luce-ply checks\nelement vertex 5\nproperty float x\nproperty float y\nproperty float z\n")
    try out.text("property float nx\nproperty float ny\nproperty float nz\nproperty uchar red\nproperty uchar green\nproperty uchar blue\n")
    try out.text("property double quality\nproperty ushort tag\nelement face 2\nproperty list uchar int vertex_indices\nproperty int material\n")
    try out.text("element edge 1\nproperty int vertex1\nproperty int vertex2\nend_header\n")
    let xs: f32[5] = [0.0, 1.0, 1.0, 0.0, 2.0]
    let ys: f32[5] = [0.0, 0.0, 1.0, 1.0, 0.0]
    for at in 0..<(usize)5:
        if format == Format.ascii:
            var line: u8[160]
            try out.text(try strings.format(line, f"{xs[at]} {ys[at]} 0 0 0 1 255 0 51 {f64(at) * 0.5} {1000 + at}\n"))
            continue
        try out.put_f32(xs[at])
        try out.put_f32(ys[at])
        try out.put_f32(0.0)
        try out.put_f32(0.0)
        try out.put_f32(0.0)
        try out.put_f32(1.0)
        try out.put_u8(255)
        try out.put_u8(0)
        try out.put_u8(51)
        try out.put_f64((f64)at * 0.5)
        try out.put_u16((u16)(1000 + at))
    if format == Format.ascii:
        try out.text("4 0 1 2 3 7\n3 1 4 2 9\n0 4\n")
        return
    try out.put_u8(4)
    for index in [0, 1, 2, 3]: try out.put_i32((i32)index)
    try out.put_i32(7)
    try out.put_u8(3)
    for index in [1, 4, 2]: try out.put_i32((i32)index)
    try out.put_i32(9)
    try out.put_i32(0)
    try out.put_i32(4)

func attribute_of(mesh: const Mesh*, name: str, domain: Domain) -> const Attribute*!:
    let index = mesh.find_attribute(name, (i64)(u8)domain)
    if index < 0:
        print(f"missing attribute {name}")
        error(invalid, "a missing attribute")
    return try mesh.attribute_entry(index)

## What every format of the mesh fixture must read as.
pub func check_mesh(geometry: const GeometrySet*) -> !:
    let mesh = set_mesh(geometry) else error(invalid, "no mesh")
    assert(mesh.point_count() == 5 and mesh.face_count() == 2 and mesh.vertex_count() == 7)
    let far = try mesh.point(4)
    assert(far.x == 2.0 and far.y == 0.0 and far.z == 0.0)
    assert(try mesh.face_point(1, 1) == 4)
    let normals = try attribute_of(mesh, "N", Domain.point)
    assert(normals.width == 3 and normals.number(3, 2) == 1.0)
    let colors = try attribute_of(mesh, "Cd", Domain.point)
    assert(colors.kind == DataType.float32 and colors.number(2, 0) == 1.0 and colors.number(2, 1) == 0.0)
    assert(math.abs(colors.number(2, 2) - 0.2) < 0.000001)
    let quality = try attribute_of(mesh, "quality", Domain.point)
    assert(quality.kind == DataType.float64 and quality.number(3, 0) == 1.5)
    let tag = try attribute_of(mesh, "tag", Domain.point)
    assert(tag.kind == DataType.int32 and tag.number(3, 0) == 1003.0)
    let material = try attribute_of(mesh, "material", Domain.face)
    assert(material.number(0, 0) == 7.0 and material.number(1, 0) == 9.0)
    let detail = set_detail(geometry)
    let edge = detail.find("edge.vertex2", Domain.detail)
    assert(edge >= 0)
    let entry = detail.entry((usize)edge)
    assert(entry.is_ragged() and entry.item_count(0) == 1 and entry.item(0, 0, 0) == 4.0)

## Decode, check, and check again after writing in each format.
func mesh_round_trips(format: Format) -> !:
    var bytes = Bytes()
    defer bytes.close()
    try mesh_fixture(format, &bytes)
    var warnings = Warnings()
    defer warnings.close()
    let geometry = try decode(bytes.view(), &warnings)
    defer dispose_set(geometry)
    try check_mesh(geometry)
    let formats: Format[3] = [Format.ascii, Format.little_endian, Format.big_endian]
    for written in formats:
        let data = try encode(geometry, written, &warnings)
        defer free(data) in memory.heap
        let again = try decode(data, &warnings)
        defer dispose_set(again)
        try check_mesh(again)

## The mesh fixture in every format, and written back in every format.
pub func meshes() -> !:
    try mesh_round_trips(Format.ascii)
    try mesh_round_trips(Format.little_endian)
    try mesh_round_trips(Format.big_endian)

## A cloud: no faces, a list property per point, CRLF header lines.
pub func clouds() -> !:
    let text = "ply\r\nformat ascii 1.0\r\nelement vertex 3\r\nproperty float x\r\nproperty float y\r\nproperty float z\r\nproperty list uchar float weights\r\nend_header\r\n1 2 3 1 0.5\n4 5 6 0\n7 8 9 2 0.25 0.125\n"
    var warnings = Warnings()
    defer warnings.close()
    let geometry = try decode(text.bytes, &warnings)
    defer dispose_set(geometry)
    try check_cloud(geometry)
    let data = try encode(geometry, Format.little_endian, &warnings)
    defer free(data) in memory.heap
    let again = try decode(data, &warnings)
    defer dispose_set(again)
    try check_cloud(again)

func check_cloud(geometry: const GeometrySet*) -> !:
    assert(set_mesh(geometry) == none)
    let data = geometry.find(points_component()) else error(invalid, "no cloud")
    let cloud = (const PointCloud*)data
    assert(cloud.count() == 3 and cloud.point(2).z == 9.0)
    let attributes = cloud_store(cloud)
    let index = attributes.find("weights", Domain.point)
    assert(index >= 0)
    let weights = attributes.entry((usize)index)
    assert(weights.is_ragged() and weights.item_count(0) == 1 and weights.item_count(1) == 0 and weights.item_count(2) == 2)
    assert(weights.item(2, 1, 0) == 0.125)
    _ = cloud_positions(cloud)

## Malformed files fail with `invalid`, never trap.
pub func malformed() -> !:
    let cases: str[6] = [
        "plx\nformat ascii 1.0\nend_header\n",
        "ply\nformat ascii 1.0\nelement vertex 2\nproperty float x\nproperty float y\nproperty float z\nend_header\n1 2 3\n",
        "ply\nformat binary_little_endian 1.0\nelement vertex 2\nproperty float x\nproperty float y\nproperty float z\nend_header\nabcdefghijkl",
        "ply\nformat ascii 1.0\nelement vertex 3\nproperty float x\nproperty float y\nproperty float z\nelement face 1\nproperty list uchar int vertex_indices\nend_header\n0 0 0 1 0 0 0 1 0\n3 0 1 7\n",
        "ply\nformat ascii 1.0\nelement vertex 1\nproperty float x\nend_header\n1\n",
        "ply\nformat ascii 1.0\nelement vertex 1\nproperty float x\nproperty float y\nproperty float z\n",
    ]
    var warnings = Warnings()
    defer warnings.close()
    for text in cases:
        var failed = false
        decode_and_dispose(text, &warnings) catch failure:
            if failure.code != invalid: print(f"unexpected failure: {failure.message}")
            assert(failure.code == invalid)
            failed = true
        assert(failed)

func decode_and_dispose(text: str, warnings: Warnings*) -> !:
    dispose_set(try decode(text.bytes, warnings))