Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-obj

Source of release 0.10.1

0.10.1 / tests / obj / obj_tests.luc

## Wavefront decoding (indices, UVs, normals, limits) and writing round trips.
from luce_obj.obj import Obj
from luce_geocore.geocore import Mesh, Vector3
from luce_std import math

func near(a: float, b: float):
    if math.abs(a - b) >= 0.000001: print(f"near failed: actual={a}, expected={b}")
    assert(math.abs(a - b) < 0.000001)

## Positions and polygons match exactly; corner attributes match when both have them.
func same(a: Mesh, b: Mesh) -> unit!:
    assert(a.point_count() == b.point_count() and a.face_count() == b.face_count())
    for index in 0..<a.point_count():
        assert(a.point(index).subtract(b.point(index)).length() == 0.0)
    for face in 0..<a.face_count():
        assert(a.face_size(face) == b.face_size(face))
        for corner in 0..<a.face_size(face):
            assert(a.face_point(face, corner) == b.face_point(face, corner))
    for name in ["uv", "N"]:
        let left = a.find_attribute(name, 1)
        let right = b.find_attribute(name, 1)
        assert((left >= 0) == (right >= 0))
        if left >= 0:
            for corner in 0..<a.vertex_count():
                for component in 0..<(2 if name == "uv" else 3):
                    assert(a.attribute_value(left, corner, component) == b.attribute_value(right, corner, component))

pub func obj_tests(scratch: str) -> unit!:
    let large = Obj.load(f"{scratch}/large.obj")
    assert(large.face_count() == 22000 and large.vertex_count() == 66000)
    near(large.attribute_value(large.find_attribute("N", 1), 65999, 2), 1.0)
    let mesh = Obj.decode("v 0 0 0\nv 1 0 0\nv 1 1 0\nv 0 1 0\nvt 0 0\nvt 1 0\nvt 1 1\nvt 0 1\nf -4/1 -3/2 -2/3 -1/4\n")
    assert(mesh.point_count() == 4 and mesh.face_count() == 1)
    near(mesh.attribute_value(mesh.find_attribute("uv", 1), 2, 1), 1.0)
    var rejected = false
    _ = Obj.decode("v 0 0 0\nf 0 1 2\n") catch failure:
        rejected = true
    assert(rejected)
    # Writing: positions round-trip bit for bit, polygons keep their sizes.
    let cube = Mesh.cube(2.0)
    let text = Obj.encode(cube, attributes = false)
    assert(text.starts_with("v ") and not text.contains("vn ") and not text.contains("vt "))
    same(cube, Obj.decode(text))
    let awkward = Mesh([Vector3(0.1 + 0.2, 1.0 / 3.0, -1e-300), Vector3(999999.99999999988, 2.0 / 7.0, 3.0), Vector3(4.0, 5.0, 6.0)], [3], [0, 1, 2])
    same(awkward, Obj.decode(Obj.encode(awkward)))
    # Corner UVs and normals survive a write and a read.
    let textured = Obj.decode("v 0 0 0\nv 1 0 0\nv 1 1 0\nv 0 1 0\nv 2 0 0\nvt 0 0\nvt 1 0\nvt 1 1\nvt 0 1\nvn 0 0 1\nvn 0.6 0 0.8\n" +
                              "f 1/1/1 2/2/1 3/3/2 4/4/1\nf 2/2/2 5/1/1 3/3/1\n")
    let written = Obj.encode(textured)
    assert(written.contains("vt ") and written.contains("vn ") and written.contains("f 1/1/1 2/2/2 3/3/3 4/4/4"))
    same(textured, Obj.decode(written))
    let normals_only = Obj.decode("v 0 0 0\nv 1 0 0\nv 0 1 0\nvn 0 0 1\nf 1//1 2//1 3//1\n")
    assert(Obj.encode(normals_only).contains("f 1//1 2//2 3//3"))
    same(normals_only, Obj.decode(Obj.encode(normals_only)))
    # A 1000-corner n-gon (a concave star) writes and reads as one face.
    var star_points: list[Vector3] = []
    var star_corners: list[int] = []
    for at in 0..<1000:
        let radius = 1.0 if at % 2 == 0 else 0.8
        let angle = 2.0 * math.pi * float(at) / 1000.0
        star_points.append(Vector3(radius * math.cos(angle), radius * math.sin(angle), 0.0))
        star_corners.append(at)
    let star = Mesh(star_points, [1000], star_corners)
    let star_again = Obj.decode(Obj.encode(star))
    assert(star_again.face_count() == 1 and star_again.face_size(0) == 1000)
    same(star, star_again)
    Obj.write(f"{scratch}/written.obj", textured)
    same(textured, Obj.load(f"{scratch}/written.obj"))
    print("PASS OBJ indices, UVs, normals and limits; writing round-trips positions, polygons, UVs and normals")