-- model/obj tests: OBJ / MTL files against assimp and trimesh; ear clipping against the exact polygon area. -- Fixtures: tools/fixtures/obj.py. local T = require("@tests/T") local Obj = require("@rosetta/model/obj") local MeshOps = require("@rosetta/ops/MeshOps") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Cases = require("@tests/gen/obj_cases") local Xcheck = require("@tests/gen/obj_xcheck") local t = T.suite("obj") local function decode(data: any, opts: any?): (any, any) local b = if type(data) == "string" then buffer.fromstring(data) else data return Obj.decode(Bytes.reader(b), opts) end t:eq(Obj.name, "obj", "name") t:eq(Obj.category, "model", "category") t:check(Obj.caps.decode and Obj.caps.encode, "caps") local function near(a: number, b: number, eps: number): boolean return math.abs(a - b) <= eps * math.max(1, math.abs(a), math.abs(b)) end local function triArea(prim: any): number local pos = prim.attributes.POSITION local ind = prim.indices local area = 0 local function P(i: number, k: number): number return buffer.readf32(pos.data, (i * 3 + k) * 4) end for tri = 0, ind.count // 3 - 1 do local a, b, c = buffer.readu32(ind.data, tri * 12), buffer.readu32(ind.data, tri * 12 + 4), buffer.readu32(ind.data, tri * 12 + 8) local e1 = { P(b, 0) - P(a, 0), P(b, 1) - P(a, 1), P(b, 2) - P(a, 2) } local e2 = { P(c, 0) - P(a, 0), P(c, 1) - P(a, 1), P(c, 2) - P(a, 2) } local x, y, z = e1[2] * e2[3] - e1[3] * e2[2], e1[3] * e2[1] - e1[1] * e2[3], e1[1] * e2[2] - e1[2] * e2[1] area += math.sqrt(x * x + y * y + z * z) / 2 end return area end -- the counts assimp reports: meshes (= our primitives), vertices, faces (triangles + line segments + points) local function counts(scene: any): (number, number, number) local prims, verts, faces = 0, 0, 0 for _, m in scene.meshes do for _, p in m.primitives do prims += 1 verts += p.attributes.POSITION.count local n = if p.indices then p.indices.count else p.attributes.POSITION.count faces += if p.mode == "triangles" then n // 3 elseif p.mode == "lines" then n // 2 else n end end return prims, verts, faces end local function checkAgainst(scene: any, assimp: any, trimesh: any?, label: string, vertices: boolean, fanArea: number) local prims, verts, faces = counts(scene) t:eq(prims, assimp.meshes, label .. ": meshes (assimp)") if vertices then t:eq(verts, assimp.vertices, label .. ": vertices (assimp)") end t:eq(faces, assimp.faces, label .. ": faces (assimp)") local lo, hi = MeshOps.bounds(scene) t:check(lo and hi, label .. ": bounds") for k = 1, 3 do t:check(near(lo[k], assimp.min[k], 1e-5) and near(hi[k], assimp.max[k], 1e-5), label .. ": bounds = assimp's") end local all = MeshOps.merge(scene) t:check(near(triArea(all), fanArea, 1e-5), string.format("%s: area %g, exact fan area %g", label, triArea(all), fanArea)) if trimesh then local merged = MeshOps.merge(scene) t:eq(merged.indices.count // 3, trimesh.faces, label .. ": triangles (trimesh)") t:check(near(triArea(merged), trimesh.area, 1e-5), string.format("%s: area %g, trimesh %g", label, triArea(merged), trimesh.area)) end end local byName = {} for _, c in Cases do byName[c.name] = c local scene, err = decode(c.data, { files = c.files }) t:check(scene, c.name .. ": decode: " .. tostring(err)) checkAgainst(scene, c.assimp, c.trimesh, c.name, not c.noVertexCount, c.fanArea) if #scene.materials > 0 then t:eq(#scene.materials, c.assimp.materials, c.name .. ": materials (assimp)") end local files = {} local out, eerr = Obj.encode(scene, { name = "rt.obj", files = files }) t:check(out, c.name .. ": encode: " .. tostring(eerr)) local back, berr = decode(out, { files = files }) t:check(back, c.name .. ": decode our OBJ: " .. tostring(berr)) local p1, v1, f1 = counts(scene) local p2, v2, f2 = counts(back) t:check(p1 == p2 and v1 == v2 and f1 == f2, c.name .. ": round trip counts") local m1, m2 = MeshOps.merge(scene), MeshOps.merge(back) for _, attr in { "POSITION", "TEXCOORD_0", "NORMAL", "COLOR_0" } do local a, b = m1.attributes[attr], m2.attributes[attr] t:check((a == nil) == (b == nil), c.name .. ": round trip has " .. attr) if a and b then t:check(buffer.tostring(a.data) == buffer.tostring(b.data), c.name .. ": round trip " .. attr .. " exact") end end if m1.indices then t:check(buffer.tostring(m1.indices.data) == buffer.tostring(m2.indices.data), c.name .. ": round trip indices") end t:eq(#back.materials, #scene.materials, c.name .. ": round trip materials") end for _, x in Xcheck do local orig = byName[string.sub(x.name, 4)] t:check(orig ~= nil, x.name .. ": a case") t:eq(x.assimp.faces, orig.assimp.faces, x.name .. ": assimp faces of our file") if not orig.noVertexCount then t:eq(x.assimp.vertices, orig.assimp.vertices, x.name .. ": assimp vertices of our file") end t:eq(x.assimp.materials, orig.assimp.materials, x.name .. ": assimp materials of our file") for k = 1, 3 do t:check(near(x.assimp.min[k], orig.assimp.min[k], 1e-5) and near(x.assimp.max[k], orig.assimp.max[k], 1e-5), x.name .. ": assimp bounds of our file") end if orig.trimesh then t:check(near(x.trimesh.area, orig.trimesh.area, 1e-5) and x.trimesh.faces == orig.trimesh.faces, x.name .. ": trimesh area of our file") end end t:note("%d files match assimp / trimesh; %d of our files read back by assimp / trimesh", #Cases, #Xcheck) do local c = byName["cube.obj"] local s = decode(c.data, { files = c.files }) t:eq(#s.roots, 1, "one object") local root = s.nodes[s.roots[1]] t:check(root.name == "Cube" and #root.children == 2 and root.mesh == nil, "the object holds two groups") t:check(s.nodes[root.children[1]].name == "top" and s.nodes[root.children[2]].name == "bottom", "group names") local red, blue = s.materials[1], s.materials[2] t:check(red.name == "red" and red.baseColor[1] == 1 and red.baseColor[2] == 0 and red.metallic == 0, "red: Kd, not metallic") t:check(near(red.roughness, math.sqrt(2 / 52), 1e-9), "red: roughness from Ns") local tex = s.textures[red.baseColorTexture.texture] t:check(tex.uri == "textures/tex.png" and tex.mime == "image/png" and tex.data and buffer.len(tex.data) > 30, "map_Kd: uri, mime, bytes through opts.files") t:check(red.baseColorTexture.transform.scale[1] == 2 and red.baseColorTexture.transform.offset[1] == 0.5, "map_Kd -s / -o") t:check(red.normalTexture and red.normalTexture.scale == 0.5, "map_Bump -bm") t:check(red.extras.obj.values.Ns[1] == 50 and red.extras.obj.values.illum[1] == 2, "Phong values in extras") t:check(blue.baseColor[4] == 0.5 and blue.alphaMode == "blend" and blue.roughness == 0.25 and blue.metallic == 1 and blue.emissive[3] == 0.3, "blue: d, Pr, Pm, Ke") t:check(s.nodes[root.children[1]].extras.obj.smoothing == "1", "smoothing group kept") local top = s.meshes[s.nodes[root.children[1]].mesh].primitives[1] t:check(top.attributes.TEXCOORD_0 ~= nil and buffer.readf32(top.attributes.TEXCOORD_0.data, 4) == 1, "vt 0 0 -> (0, 1) (top-left origin)") local s2 = decode(c.data, { files = c.files, flipV = false }) local top2 = s2.meshes[s2.nodes[s2.nodes[s2.roots[1]].children[1]].mesh].primitives[1] t:check(buffer.readf32(top2.attributes.TEXCOORD_0.data, 4) == 0, "flipV = false") -- without the MTL: named default materials, the library listed as missing local s3 = decode(c.data) t:check(#s3.materials == 2 and s3.meta.source.missingMtl[1] == "cube.mtl", "missing mtllib") local s4 = decode(c.data, { mtl = c.files["cube.mtl"] }) t:check(s4.materials[1].baseColor[1] == 1 and s4.textures[1].data == nil, "opts.mtl (texture bytes not found)") local asked = {} local s5 = decode(c.data, { resolve = function(uri: string): any table.insert(asked, uri) return c.files[uri] end }) t:check(s5.materials[2].baseColor[4] == 0.5 and table.find(asked, "cube.mtl") and table.find(asked, "textures/tex.png"), "opts.resolve") local files = {} Obj.encode(s, { name = "x.obj", files = files }) local mtl = files["x.mtl"] t:check(type(mtl) == "string" and string.find(mtl, "map_Kd -o 0.5 0 -s 2 2 textures/tex.png", 1, true) ~= nil, "MTL map_Kd written: " .. tostring(mtl)) t:check(string.find(mtl, "newmtl blue", 1, true) and string.find(mtl, "\nd 0.5", 1, true), "MTL blue written") t:check(files["textures/tex.png"] ~= nil, "texture bytes written to opts.files") end -- polygons: triangulate = false keeps them; ear clipping gets the concave area right, the fan does not do local c = byName["concave.obj"] local s = decode(c.data, { triangulate = false }) local p = s.meshes[1].primitives[1] t:check(p.mode == "polygons" and p.faces.count == 2 and p.indices.count == 14, "polygons kept") local tri = MeshOps.triangulate(p) :: any t:check(near(triArea(tri), c.polygonArea, 1e-9), "ear clipping area " .. triArea(tri) .. " = " .. c.polygonArea) local fan = MeshOps.triangulate(p, { method = "fan" }) :: any t:check(near(triArea(fan), c.trimesh.area, 1e-9), "fan area = trimesh's (fan) area") local out = Obj.encode(s) :: buffer t:check(string.find(buffer.tostring(out), "\nf 7 8 9 10 11 12 13 14", 1, true) ~= nil, "an octagon written as one face") local back = decode(out, { triangulate = false }) t:eq(back.meshes[1].primitives[1].faces.count, 2, "polygons round trip") end do local s = decode(byName["wire.obj"].data) local modes = {} for _, p in s.meshes[1].primitives do modes[p.mode] = p end t:check(modes.lines and modes.lines.indices.count == 8 and modes.points and modes.triangles, "l (polyline -> 4 segments), p, f") end do local _, e = decode("v 0 0 0\nf 1 2 3\n") t:check(e and e.code == Err.CORRUPT, "index out of range: " .. tostring(e)) local _, e2 = decode("v 0 0\n") t:check(e2 and e2.code == Err.CORRUPT, "short vertex: " .. tostring(e2)) local _, e3 = decode(byName["random.obj"].data, { maxVertices = 10 }) t:check(e3 and e3.code == Err.LIMIT, "maxVertices: " .. tostring(e3)) local _, e4 = Obj.encode({} :: any) t:check(e4 and e4.code == Err.ARG, "encode junk") t:check(Obj.sniff(Bytes.reader(buffer.fromstring(byName["cube.obj"].data))) > 0, "sniff an OBJ") t:eq(Obj.sniff(Bytes.reader(buffer.fromstring("\137PNG\r\n\26\n"))), 0, "sniff a PNG") local empty = decode("# nothing\n") t:check(empty and #empty.nodes == 0, "an empty file is an empty scene") end do local samples = {} for _, c in Cases do table.insert(samples, c.data) end t:fuzz("decode", function(b: buffer) local s = Obj.decode(Bytes.reader(b), { triangulate = false }) if s then Obj.encode(s) MeshOps.merge(s) end return s end, samples, { mutations = 40 }) end t:done()