-- model/ldraw tests: hand-written and real LDraw files against the generator's own expansion and LeoCAD's OBJ export. -- Fixtures: tools/fixtures/ldraw.py. local T = require("@tests/T") local Ldraw = require("@rosetta/model/ldraw") local MeshOps = require("@rosetta/ops/MeshOps") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Cases = require("@tests/gen/ldraw_cases") local Config = require("@tests/gen/ldraw_config") local t = T.suite("ldraw") local function decode(data: any, opts: any?): (any, any) local b = if type(data) == "string" then buffer.fromstring(data) else data return Ldraw.decode(Bytes.reader(b), opts) end 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 t:eq(Ldraw.name, "ldraw", "name") t:eq(Ldraw.category, "model", "category") t:check(Ldraw.caps.decode and not Ldraw.caps.encode, "caps") t:check(Ldraw.sniff(Bytes.reader(buffer.fromstring("0 FILE main.ldr\n1 16 0 0 0 1 0 0 0 1 0 0 0 1 a.dat\n"))) >= 0.9, "sniff MPD") t:check(Ldraw.sniff(Bytes.reader(buffer.fromstring("0 x\n1 16 0 0 0 1 0 0 0 1 0 0 0 1 3001.dat\n"))) >= 0.8, "sniff LDR") t:eq(Ldraw.sniff(Bytes.reader(buffer.fromstring("solid cube\n facet normal 0 0 1\n"))), 0, "sniff other") do local tbl = Ldraw.colors() local bad = 0 for _, c in Config.colours do local e = tbl[c[1]] if not e or e.rgb ~= c[2] or e.edge ~= c[3] or e.alpha ~= c[4] then bad += 1 end end t:eq(bad, 0, "built-in colours = LDConfig.ldr (" .. #Config.colours .. " colours)") local over = Ldraw.colors("0 !COLOUR Mine CODE 4 VALUE #010203 EDGE 0\n") t:check(over[4].rgb == 0x010203 and over[4].edge == 0 and over[4].name == "Mine", "ldconfig overrides") end type Stats = { triangles: number, lines: number, optional: number, ds: number, area: number, volume: number, colours: { [number]: number } } local function det3(m: { number }): number return m[1] * (m[6] * m[11] - m[10] * m[7]) - m[5] * (m[2] * m[11] - m[10] * m[3]) + m[9] * (m[2] * m[7] - m[6] * m[3]) end local function measure(scene: any): Stats local s: Stats = { triangles = 0, lines = 0, optional = 0, ds = 0, area = 0, volume = 0, colours = {} } for _, pl in Types.Scene.flatten(scene) do local p = pl.primitive local n = p.attributes.POSITION.count if p.mode == "lines" then if p.extras and p.extras.ldraw and p.extras.ldraw.optional then s.optional += n // 2 else s.lines += n // 2 end else local mat = scene.materials[p.material] local code = mat.extras.ldraw.code s.colours[code] = (s.colours[code] or 0) + n // 3 s.triangles += n // 3 if mat.doubleSided then s.ds += n // 3 end local m = pl.matrix local flip = det3(m) < 0 local get = assert(Types.Scene.reader(p.attributes.POSITION)) for tri = 0, n // 3 - 1 do local q = {} for k = 0, 2 do local x, y, z = get(tri * 3 + k, 0), get(tri * 3 + k, 1), get(tri * 3 + k, 2) q[k + 1] = { m[1] * x + m[5] * y + m[9] * z + m[13], m[2] * x + m[6] * y + m[10] * z + m[14], m[3] * x + m[7] * y + m[11] * z + m[15] } end local a, b, c = q[1], q[2], q[3] if flip then a, c = c, a end local ux, uy, uz, vx, vy, vz = b[1] - a[1], b[2] - a[2], b[3] - a[3], c[1] - a[1], c[2] - a[2], c[3] - a[3] local x, y, z = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx s.area += math.sqrt(x * x + y * y + z * z) / 2 if not mat.doubleSided then s.volume += (a[1] * (b[2] * c[3] - b[3] * c[2]) - a[2] * (b[1] * c[3] - b[3] * c[1]) + a[3] * (b[1] * c[2] - b[2] * c[1])) / 6 end end end end return s end local function compare(label: string, s: Stats, w: any) t:eq(s.triangles, w.triangles, label .. ": triangles") t:eq(s.ds, w.doubleSided, label .. ": double-sided triangles") t:eq(s.lines, w.lines, label .. ": lines") local bad = {} for _, pair in w.colours do if s.colours[pair[1]] ~= pair[2] then table.insert(bad, string.format("%d: %s / %d", pair[1], tostring(s.colours[pair[1]]), pair[2])) end end t:check(#bad == 0 and #w.colours == #(function() local keys = {} for k in s.colours do table.insert(keys, k) end return keys end)(), label .. ": triangles per colour " .. table.concat(bad, ", ")) t:check(near(s.area, w.area, 1e-5), string.format("%s: area %.6f / %.6f", label, s.area, w.area)) t:check(near(s.volume, w.volume, 1e-5), string.format("%s: signed volume %.6f / %.6f", label, s.volume, w.volume)) end local samples = {} local lib: any = nil for _, c in Cases do local opts = table.clone(c.opts or {}) opts.files = c.files local scene, err = decode(c.data, opts) t:check(scene, c.name .. ": decode: " .. tostring(err)) local w = c.want compare(c.name, measure(scene), w) local lo, hi = MeshOps.bounds(assert(MeshOps.merge(scene))) local ok = lo ~= nil and w.min ~= nil for k = 1, 3 do ok = ok and near((lo :: any)[k], w.min[k], 1e-5) and near((hi :: any)[k], w.max[k], 1e-5) end t:check(ok, string.format("%s: bounds %s / %s", c.name, table.concat(lo or {}, " "), table.concat(hi or {}, " "))) t:eq(table.concat(scene.meta.source.missing, ","), table.concat(w.missing, ","), c.name .. ": missing files") t:check(scene.meta.up == "y" and scene.meta.unit == 0.0004, c.name .. ": meta.up / unit") -- LeoCAD: its axes are (x, -z, y) of ours, 2 decimals local lc = c.leocad if lc then local lmin = { lo[1], -hi[3], lo[2] } local lmax = { hi[1], -lo[3], hi[2] } local good = true for k = 1, 3 do good = good and math.abs(lmin[k] - lc.min[k]) < 0.02 and math.abs(lmax[k] - lc.max[k]) < 0.02 end t:check(good, c.name .. ": bounds (LeoCAD)") local s = measure(scene) t:check(near(s.area, lc.area, 2e-4), string.format("%s: area %.3f (LeoCAD %.3f)", c.name, s.area, lc.area)) end -- the same model flattened into one mesh, and without the root flip local flat = assert(decode(c.data, { files = c.files, flatten = true })) t:eq(#flat.meshes <= 1, true, c.name .. ": flatten: one mesh") compare(c.name .. " (flatten)", measure(flat), w) local raw = assert(decode(c.data, { files = c.files, upright = false })) local rlo, rhi = MeshOps.bounds(assert(MeshOps.merge(raw))) t:check(rlo and near(rlo[2], -hi[2], 1e-6) and near(rhi[3], -lo[3], 1e-6) and raw.meta.source.up == "-y", c.name .. ": upright = false") if #c.data < 4000 then table.insert(samples, c.data) end if c.name == "two.ldr" then lib = c.files -- one mesh per (part, colour): four colours, the missing part has no node t:eq(#scene.meshes, 4, "two.ldr: meshes per colour") local root = scene.nodes[scene.roots[1]] t:eq(#root.children, 4, "two.ldr: one node per placed part") local steps = {} for _, ci in root.children do table.insert(steps, scene.nodes[ci].extras.ldraw.step) end t:eq(table.concat(steps, ","), "1,1,1,2", "two.ldr: steps") local viaResolve = assert(decode(c.data, { resolve = function(uri: string): any return c.files[uri] end })) t:eq(measure(viaResolve).triangles, w.triangles, "two.ldr: resolve") local withOpt = assert(decode(c.data, { files = c.files, optionalLines = true, lines = false })) local so = measure(withOpt) t:check(so.optional == w.optional and so.lines == 0, "two.ldr: optional lines only") elseif c.name == "model.mpd" then t:check(scene.meta.source.mpd, "model.mpd: mpd") local meshNodes = 0 for _, n in scene.nodes do if n.mesh then meshNodes += 1 end end t:check(#scene.meshes < meshNodes, string.format("model.mpd: meshes shared (%d meshes, %d mesh nodes)", #scene.meshes, meshNodes)) local purple for _, m in scene.materials do if m.extras.ldraw.code == 1000 then purple = m end end t:check(purple and purple.name == "Test_Purple" and purple.alphaMode == "blend", "model.mpd: local !COLOUR") elseif c.name == "badrefs.ldr" then t:check(scene.meta.source.cycles > 0 and scene.meta.source.badLines == 2, "badrefs.ldr: cycle and bad lines counted") end end do local _, e = decode("") t:check(e and e.code == Err.CORRUPT, "empty: CORRUPT") _, e = decode("hello world\nthis is not ldraw\n") t:check(e and e.code == Err.CORRUPT, "text: CORRUPT") _, e = decode(Cases[1].data, { files = Cases[1].files, maxTriangles = 10 }) t:check(e and e.code == Err.LIMIT, "maxTriangles: LIMIT") _, e = decode(Cases[1].data, { files = Cases[1].files, maxDepth = 0 }) t:check(e and e.code == Err.LIMIT, "maxDepth: LIMIT") end t:fuzz("decode", function(b: buffer) local s = Ldraw.decode(Bytes.reader(b), { files = lib, optionalLines = true }) if s then MeshOps.merge(s) end return s end, samples, { mutations = 24 }) t:done()