merge ws/quake (codecs/model/Brush, codecs/model/Patch, model/map) for model/d3map
This commit is contained in:
commit
4bd59a30c8
25 files changed
+8721
-1316
No files matched your search
@@ -0,0 +1,224 @@
|
||||
-- codecs/model/Brush tests: random overlapping brushes in Quake standard / Valve 220 / brush-primitive projections
|
||||
-- against ericw-tools qbsp -nofill (face area per plane after CSG, texture coordinates at every compiled corner);
|
||||
-- merging, the primitive builder, the fuzz pass. Fixtures: tools/fixtures/brush.py.
|
||||
local T = require("@tests/T")
|
||||
local Brush = require("@rosetta/codecs/model/Brush")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Fx = require("@tests/gen/brush")
|
||||
|
||||
local t = T.suite("brush")
|
||||
|
||||
local function faceOf(rec: any): Brush.Face?
|
||||
local p = rec.pts
|
||||
local plane = Brush.planeFromPoints({ p[1], p[2], p[3] }, { p[4], p[5], p[6] }, { p[7], p[8], p[9] })
|
||||
if not plane then
|
||||
return nil
|
||||
end
|
||||
local a = rec.args
|
||||
local f: Brush.Face = { plane = plane, texture = rec.tex }
|
||||
if rec.kind == "standard" then
|
||||
f.vecs = Brush.quakeVecs(plane, a[1], a[2], a[3], a[4], a[5])
|
||||
elseif rec.kind == "valve" then
|
||||
f.vecs = Brush.valveVecs({ a[1], a[2], a[3], a[4] }, { a[5], a[6], a[7], a[8] }, a[10], a[11])
|
||||
else
|
||||
f.bp = { { a[1], a[2], a[3] }, { a[4], a[5], a[6] } }
|
||||
end
|
||||
return f
|
||||
end
|
||||
|
||||
local function key(n: { number }): string
|
||||
local function r(x: number, k: number): number
|
||||
return math.floor(x * k + 0.5)
|
||||
end
|
||||
return string.format("%d %d %d %d", r(n[1], 1000), r(n[2], 1000), r(n[3], 1000), r(n[4], 100))
|
||||
end
|
||||
|
||||
local function inside(pts: { number }, n: { number }, x: number, y: number, z: number, tol: number): boolean
|
||||
local k = #pts // 3
|
||||
for i = 0, k - 1 do
|
||||
local j = (i + 1) % k
|
||||
local ex, ey, ez = pts[j * 3 + 1] - pts[i * 3 + 1], pts[j * 3 + 2] - pts[i * 3 + 2], pts[j * 3 + 3] - pts[i * 3 + 3]
|
||||
-- inward edge normal = n x e (counter-clockwise polygons)
|
||||
local ix, iy, iz = n[2] * ez - n[3] * ey, n[3] * ex - n[1] * ez, n[1] * ey - n[2] * ex
|
||||
local l = math.sqrt(ix * ix + iy * iy + iz * iz)
|
||||
if l > 0 and ((x - pts[i * 3 + 1]) * ix + (y - pts[i * 3 + 2]) * iy + (z - pts[i * 3 + 3]) * iz) / l < -tol then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
for kind, m in Fx do
|
||||
local brushes: { { Brush.Face } } = {}
|
||||
for _, b in m.brushes do
|
||||
local faces = {}
|
||||
for _, rec in b do
|
||||
table.insert(faces, assert(faceOf(rec)))
|
||||
end
|
||||
table.insert(brushes, faces)
|
||||
end
|
||||
local polys = assert(Brush.polygons(brushes, { hull = true }))
|
||||
-- area per plane: ours after CSG = qbsp's faces (split by its BSP, merged, subdivided)
|
||||
local ours, theirs = {}, {}
|
||||
for _, p in polys do
|
||||
local k = key(brushes[p.brush][p.face].plane)
|
||||
ours[k] = (ours[k] or 0) + Brush.area(p.points)
|
||||
end
|
||||
for _, f in m.faces do
|
||||
local k = key(f.plane)
|
||||
theirs[k] = (theirs[k] or 0) + Brush.area(f.pts)
|
||||
end
|
||||
local bad = 0
|
||||
for k, a in theirs do
|
||||
if math.abs((ours[k] or 0) - a) > 0.05 then
|
||||
bad += 1
|
||||
t:note("%s: plane %s area %s, qbsp %s", kind, k, tostring(ours[k]), tostring(a))
|
||||
end
|
||||
end
|
||||
for k in ours do
|
||||
if not theirs[k] then
|
||||
bad += 1
|
||||
t:note("%s: plane %s only in ours", kind, k)
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, kind .. ": face area per plane matches qbsp")
|
||||
-- every compiled corner lies on one of our polygons with the same texture, and maps to the same texels
|
||||
local miss, off, worst = 0, 0, 0
|
||||
local corners = 0
|
||||
for _, f in m.faces do
|
||||
local pk = key(f.plane)
|
||||
for c = 0, #f.pts // 3 - 1 do
|
||||
local x, y, z = f.pts[c * 3 + 1], f.pts[c * 3 + 2], f.pts[c * 3 + 3]
|
||||
corners += 1
|
||||
local s = f.vecs[1] * x + f.vecs[2] * y + f.vecs[3] * z + f.vecs[4]
|
||||
local tt = f.vecs[5] * x + f.vecs[6] * y + f.vecs[7] * z + f.vecs[8]
|
||||
local found = false
|
||||
local best = math.huge
|
||||
for _, p in polys do
|
||||
local face = brushes[p.brush][p.face]
|
||||
if face.texture == f.tex and key(face.plane) == pk and inside(p.points, p.normal, x, y, z, 0.02) then
|
||||
found = true
|
||||
local size = m.sizes[f.tex]
|
||||
local u, v = Brush.uv(face, x, y, z, size[1], size[2])
|
||||
best = math.min(best, math.max(math.abs(u * size[1] - s), math.abs(v * size[2] - tt)))
|
||||
end
|
||||
end
|
||||
if not found then
|
||||
miss += 1
|
||||
local any = {}
|
||||
for _, p in polys do
|
||||
local face = brushes[p.brush][p.face]
|
||||
if key(face.plane) == pk and inside(p.points, p.normal, x, y, z, 0.02) then
|
||||
table.insert(any, face.texture .. "#" .. p.brush)
|
||||
end
|
||||
end
|
||||
t:note("%s miss %s %s %s on %s tex %s: ours %s", kind, x, y, z, pk, f.tex, table.concat(any, ","))
|
||||
elseif best > 0.02 then
|
||||
off += 1
|
||||
t:note("%s off %s %s %s on %s tex %s best %s vecs %s", kind, x, y, z, pk, f.tex, best, table.concat(f.vecs, " "))
|
||||
worst = math.max(worst, best)
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(miss, 0, kind .. ": every qbsp corner is on one of our faces (" .. corners .. " corners)")
|
||||
t:eq(off, 0, kind .. ": texture coordinates match qbsp's texinfo (worst " .. worst .. " texels)")
|
||||
-- primitives: counter-clockwise about the face normals, one per texture
|
||||
local built = Brush.primitives(polys, brushes, function(name: string)
|
||||
local s = m.sizes[name]
|
||||
return s[1], s[2]
|
||||
end)
|
||||
local seen, wrong = {}, 0
|
||||
for _, b in built do
|
||||
t:check(not seen[b.texture], kind .. ": one primitive per texture " .. b.texture)
|
||||
seen[b.texture] = true
|
||||
local prim = b.primitive
|
||||
local pos = Types.Scene.reader(prim.attributes.POSITION) :: any
|
||||
local nrm = Types.Scene.reader(prim.attributes.NORMAL) :: any
|
||||
local idx = prim.indices :: Types.Accessor
|
||||
for k = 0, idx.count - 1, 3 do
|
||||
local i0, i1, i2 = buffer.readu32(idx.data, k * 4), buffer.readu32(idx.data, k * 4 + 4), buffer.readu32(idx.data, k * 4 + 8)
|
||||
local ux, uy, uz = pos(i1, 0) - pos(i0, 0), pos(i1, 1) - pos(i0, 1), pos(i1, 2) - pos(i0, 2)
|
||||
local vx, vy, vz = pos(i2, 0) - pos(i0, 0), pos(i2, 1) - pos(i0, 1), pos(i2, 2) - pos(i0, 2)
|
||||
local cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
||||
if cx * nrm(i0, 0) + cy * nrm(i0, 1) + cz * nrm(i0, 2) <= 0 then
|
||||
wrong += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(wrong, 0, kind .. ": triangles counter-clockwise about their normals")
|
||||
-- merging keeps the area per plane
|
||||
local merged = assert(Brush.polygons(brushes, { hull = true, merge = true }))
|
||||
t:check(#merged <= #polys, kind .. ": merge does not add polygons")
|
||||
local after = {}
|
||||
for _, p in merged do
|
||||
local k = key(brushes[p.brush][p.face].plane)
|
||||
after[k] = (after[k] or 0) + Brush.area(p.points)
|
||||
end
|
||||
local drift = 0
|
||||
for k, a in ours do
|
||||
drift = math.max(drift, math.abs((after[k] or 0) - a))
|
||||
end
|
||||
t:check(drift < 1e-6, kind .. ": merged area per plane unchanged")
|
||||
end
|
||||
|
||||
-- two touching cubes merge into one box: 6 faces, no inner faces
|
||||
do
|
||||
local function cube(x0: number, x1: number): { Brush.Face }
|
||||
local f = {}
|
||||
for _, p in { { 1, 0, 0, x1 }, { -1, 0, 0, -x0 }, { 0, 1, 0, 64 }, { 0, -1, 0, 0 }, { 0, 0, 1, 64 }, { 0, 0, -1, 0 } } do
|
||||
table.insert(f, { plane = p, texture = "t", vecs = Brush.quakeVecs(p, 0, 0, 0, 1, 1) })
|
||||
end
|
||||
return f
|
||||
end
|
||||
local polys = assert(Brush.polygons({ cube(0, 64), cube(64, 128) }, { hull = true, merge = true }))
|
||||
t:eq(#polys, 6, "touching cubes: 6 merged faces")
|
||||
local area = 0
|
||||
for _, p in polys do
|
||||
area += Brush.area(p.points)
|
||||
end
|
||||
t:eq(area, 2 * 128 * 64 * 2 + 2 * 64 * 64, "touching cubes: surface area")
|
||||
local plain = assert(Brush.polygons({ cube(0, 64), cube(64, 128) }))
|
||||
t:eq(#plain, 12, "without hull / merge: every face")
|
||||
t:check(Brush.planeFromPoints({ 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 }) == nil, "colinear points: no plane")
|
||||
-- Quake standard with exact rotations, against TextureAxisFromPlane by hand: a floor, rotated 90 degrees
|
||||
local v = Brush.quakeVecs({ 0, 0, 1, 0 }, 8, 4, 90, 2, 1)
|
||||
t:eq(v[1][1] .. " " .. v[1][2] .. " " .. v[1][4], "0 0.5 8", "rotated floor s axis")
|
||||
t:eq(v[2][1] .. " " .. v[2][2] .. " " .. v[2][4], "1 0 4", "rotated floor t axis")
|
||||
-- brush primitives <-> texel vectors
|
||||
local n = { 0.6, 0, 0.8, 10 }
|
||||
local bp = { { 0.01, 0.02, 0.5 }, { -0.03, 0.004, 0.25 } }
|
||||
local back = Brush.vecsToBp(n, Brush.bpVecs(n, bp, 64, 32), 64, 32)
|
||||
local err = 0
|
||||
for i = 1, 2 do
|
||||
for j = 1, 3 do
|
||||
err = math.max(err, math.abs(back[i][j] - bp[i][j]))
|
||||
end
|
||||
end
|
||||
t:check(err < 1e-12, "bpVecs / vecsToBp round trip")
|
||||
end
|
||||
|
||||
-- fuzz: random planes never throw
|
||||
do
|
||||
local rand = T.rng(5)
|
||||
local threw = 0
|
||||
for _ = 1, 200 do
|
||||
local faces = {}
|
||||
for _ = 1, 1 + rand(10) do
|
||||
local p = { rand(2001) / 1000 - 1, rand(2001) / 1000 - 1, rand(2001) / 1000 - 1, rand(200) - 100 }
|
||||
table.insert(faces, { plane = p, texture = "x", vecs = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 } } })
|
||||
end
|
||||
local ok = pcall(function()
|
||||
local polys = Brush.polygons({ faces, faces }, { hull = true, merge = true })
|
||||
if polys then
|
||||
Brush.primitives(polys, { faces, faces }, function()
|
||||
return 64, 64
|
||||
end)
|
||||
end
|
||||
end)
|
||||
if not ok then
|
||||
threw += 1
|
||||
end
|
||||
end
|
||||
t:eq(threw, 0, "random planes never throw")
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,85 @@
|
||||
-- codecs/model/Patch tests: biquadratic Bezier evaluation against the closed form, shared sub-patch edges, triangle
|
||||
-- winding and normals, explicit (patchDef3-style) subdivisions per axis, the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local Patch = require("@rosetta/codecs/model/Patch")
|
||||
|
||||
local t = T.suite("patch")
|
||||
|
||||
-- a 5 x 3 grid (two sub-patches along u): x = column * 8, y = row * 8, z = a bump on the middle control row
|
||||
local ctrl = {}
|
||||
for j = 0, 2 do
|
||||
for i = 0, 4 do
|
||||
table.insert(ctrl, i * 8)
|
||||
table.insert(ctrl, j * 8)
|
||||
table.insert(ctrl, if j == 1 then 16 else 0)
|
||||
end
|
||||
end
|
||||
local pts, gw, gh = Patch.grid(ctrl, 5, 3, 3, 4, 2)
|
||||
t:check(pts ~= nil, "grid")
|
||||
t:eq(gw, 9, "width: 2 sub-patches x 4 segments + 1")
|
||||
t:eq(gh, 3, "height: 2 segments + 1")
|
||||
local p = pts :: { number }
|
||||
-- z(v) = 2 v (1 - v) * 16 on every column (the bump is constant along u); x is linear in u
|
||||
local worst = 0
|
||||
for gy = 0, gh - 1 do
|
||||
local v = gy / 2
|
||||
for gx = 0, gw - 1 do
|
||||
local o = (gy * gw + gx) * 3
|
||||
worst = math.max(worst, math.abs(p[o + 1] - gx * 4), math.abs(p[o + 2] - v * 16), math.abs(p[o + 3] - 32 * v * (1 - v)))
|
||||
end
|
||||
end
|
||||
t:check(worst < 1e-12, "closed form (worst " .. worst .. ")")
|
||||
-- corners interpolate the control points
|
||||
t:eq(p[1] .. " " .. p[2] .. " " .. p[3], "0 0 0", "first corner")
|
||||
local last = (gw * gh - 1) * 3
|
||||
t:eq(p[last + 1] .. " " .. p[last + 2] .. " " .. p[last + 3], "32 16 0", "last corner")
|
||||
|
||||
local tris = Patch.triangles(gw, gh)
|
||||
t:eq(#tris, (gw - 1) * (gh - 1) * 6, "two triangles per cell")
|
||||
local nrm = Patch.normals(p, 3, gw, gh)
|
||||
local bad = 0
|
||||
for k = 1, #tris, 3 do
|
||||
local a, b, c = tris[k] * 3, tris[k + 1] * 3, tris[k + 2] * 3
|
||||
local ux, uy, uz = p[b + 1] - p[a + 1], p[b + 2] - p[a + 2], p[b + 3] - p[a + 3]
|
||||
local vx, vy, vz = p[c + 1] - p[a + 1], p[c + 2] - p[a + 2], p[c + 3] - p[a + 3]
|
||||
local cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
||||
if cx * nrm[a + 1] + cy * nrm[a + 2] + cz * nrm[a + 3] <= 0 then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, "normals face the front of the triangles")
|
||||
|
||||
-- extra components (texture coordinates) interpolate too; normalize renormalizes a vec3
|
||||
local flat = {}
|
||||
for j = 0, 2 do
|
||||
for i = 0, 2 do
|
||||
for _, v in { i, j, 0, i / 2, j / 2, 0, 0, 2 } do
|
||||
table.insert(flat, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
local q, qw, qh = Patch.grid(flat, 3, 3, 8, 3, 5)
|
||||
t:eq(qw * qh, 4 * 6, "explicit subdivisions per axis")
|
||||
Patch.normalize(q :: { number }, 8, 5)
|
||||
t:eq((q :: { number })[8], 1, "renormalized normal")
|
||||
t:eq((q :: { number })[(qw * qh - 1) * 8 + 4], 1, "texture coordinate at the far corner")
|
||||
|
||||
t:check(Patch.grid({ 1, 2, 3 }, 2, 2, 3, 4, 4) == nil, "even grid: nil")
|
||||
t:check(Patch.grid(ctrl, 5, 3, 3, 1000, 1000, 1000) == nil, "point limit: nil")
|
||||
|
||||
t:fuzz("grid", function(b: buffer)
|
||||
local n = buffer.len(b)
|
||||
local w = 1 + 2 * (if n > 0 then buffer.readu8(b, 0) % 4 else 1)
|
||||
local h = 1 + 2 * (if n > 1 then buffer.readu8(b, 1) % 4 else 1)
|
||||
local c = {}
|
||||
for k = 0, n - 1 do
|
||||
table.insert(c, buffer.readu8(b, k) - 128)
|
||||
end
|
||||
local g, gw2, gh2 = Patch.grid(c, w, h, 3, 1 + n % 5, 1 + n % 3)
|
||||
if g then
|
||||
Patch.normals(g, 3, gw2, gh2)
|
||||
Patch.triangles(gw2, gh2)
|
||||
end
|
||||
return g
|
||||
end, { buffer.create(27), buffer.create(45), buffer.create(75) })
|
||||
t:done()
|
||||
Reference in new issue
Block a user