93 lines
2.7 KiB
Luau
93 lines
2.7 KiB
Luau
-- tests/model/_soup: triangle soups of a primitive and a tolerant comparison against assimp's soup.
|
|
local Types = require("@rosetta/Types")
|
|
|
|
local Soup = {}
|
|
|
|
-- positions(prim, target?) -> f32 x3 buffer of the corners in index order (target k adds morph target k's deltas)
|
|
function Soup.positions(prim: any, target: number?): buffer
|
|
local pos = Types.Scene.reader(prim.attributes.POSITION) :: any
|
|
local d = if target then Types.Scene.reader(prim.targets[target].POSITION) :: any else nil
|
|
local idx = prim.indices
|
|
local n = idx.count
|
|
local out = buffer.create(n * 12)
|
|
for k = 0, n - 1 do
|
|
local v = buffer.readu32(idx.data, k * 4)
|
|
for c = 0, 2 do
|
|
buffer.writef32(out, k * 12 + c * 4, pos(v, c) + (if d then d(v, c) else 0))
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- flip: v -> 1 - v (assimp's convention); "neg": v -> -v (assimp's Half-Life loader)
|
|
function Soup.uvs(prim: any, set: number?, flip: (boolean | string)?): { number }
|
|
local tc = Types.Scene.reader(prim.attributes["TEXCOORD_" .. (set or 0)]) :: any
|
|
local idx = prim.indices
|
|
local out = {}
|
|
for k = 0, idx.count - 1 do
|
|
local v = buffer.readu32(idx.data, k * 4)
|
|
table.insert(out, tc(v, 0))
|
|
table.insert(out, if flip == "neg" then -tc(v, 1) elseif flip then 1 - tc(v, 1) else tc(v, 1))
|
|
end
|
|
return out
|
|
end
|
|
|
|
function Soup.close(a: any, b: { number }, tol: number): (boolean, number?)
|
|
local n = if type(a) == "buffer" then buffer.len(a) // 4 else #a
|
|
if n ~= #b then
|
|
return false, -1
|
|
end
|
|
for i = 1, n do
|
|
local x = if type(a) == "buffer" then buffer.readf32(a, (i - 1) * 4) else a[i]
|
|
if math.abs(x - b[i]) > tol * math.max(1, math.abs(b[i])) then
|
|
return false, i
|
|
end
|
|
end
|
|
return true, nil
|
|
end
|
|
|
|
-- triangles compare up to a cyclic rotation of their corners (assimp rotates them)
|
|
function Soup.triangles(a: { number }, b: { number }, tol: number, stride: number?): (boolean, number?)
|
|
local s = stride or 3
|
|
if #a ~= #b then
|
|
return false, -1
|
|
end
|
|
local per = 3 * s
|
|
for t = 0, #a // per - 1 do
|
|
local found = false
|
|
for rot = 0, 2 do
|
|
local same = true
|
|
for c = 0, 2 do
|
|
local ca, cb = t * per + c * s, t * per + ((c + rot) % 3) * s
|
|
for k = 1, s do
|
|
if math.abs(a[ca + k] - b[cb + k]) > tol * math.max(1, math.abs(b[cb + k])) then
|
|
same = false
|
|
end
|
|
end
|
|
end
|
|
if same then
|
|
found = true
|
|
break
|
|
end
|
|
end
|
|
if not found then
|
|
return false, t
|
|
end
|
|
end
|
|
return true, nil
|
|
end
|
|
|
|
function Soup.list(b: buffer): { number }
|
|
local out = table.create(buffer.len(b) // 4)
|
|
for i = 0, buffer.len(b) // 4 - 1 do
|
|
out[i + 1] = buffer.readf32(b, i * 4)
|
|
end
|
|
return out
|
|
end
|
|
|
|
function Soup.slice(list: { number }, from: number, n: number): { number }
|
|
return table.move(list, from, from + n - 1, 1, table.create(n))
|
|
end
|
|
|
|
return Soup
|