Files
rosetta/tests/model/_vrml_common.luau

108 lines
3.9 KiB
Luau

-- tests/model/_vrml_common: VRML / X3D counts as the reference readers count them; bounds include lines and points.
local Types = require("@rosetta/Types")
local C = {}
export type Stats = { prims: number, unique: number, polys: number, lines: number, points: number, lo: { number }, hi: { number }, area: number }
function C.near(a: number, b: number, eps: number): boolean
return math.abs(a - b) <= eps * math.max(1, math.abs(a), math.abs(b))
end
function C.stats(scene: any): Stats
local s: Stats = { prims = 0, unique = 0, polys = 0, lines = 0, points = 0, lo = { math.huge, math.huge, math.huge }, hi = { -math.huge, -math.huge, -math.huge }, area = 0 }
local seen = {}
for _, pl in Types.Scene.flatten(scene) do
local p = pl.primitive
s.prims += 1
if not seen[p] then
seen[p] = true
s.unique += 1
end
local P = p.attributes.POSITION
local nf = 0
if p.mode == "polygons" then
nf = p.faces.count
elseif p.mode == "lines" then
s.lines += (if p.indices then p.indices.count else P.count) // 2
elseif p.mode == "points" then
s.points += P.count
elseif p.mode == "triangle_strip" or p.mode == "triangle_fan" then
nf = math.max(0, (if p.indices then p.indices.count else P.count) - 2)
elseif p.mode == "line_strip" or p.mode == "line_loop" then
local n = if p.indices then p.indices.count else P.count
s.lines += if p.mode == "line_loop" then n else math.max(0, n - 1)
else
nf = (if p.indices then p.indices.count else P.count) // 3
end
s.polys += nf
local m = pl.matrix
local get = assert(Types.Scene.reader(P))
local w = {}
for v = 0, P.count - 1 do
local x, y, z = get(v, 0), get(v, 1), get(v, 2)
local wx, wy, wz = 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]
w[v] = { wx, wy, wz }
s.lo[1], s.lo[2], s.lo[3] = math.min(s.lo[1], wx), math.min(s.lo[2], wy), math.min(s.lo[3], wz)
s.hi[1], s.hi[2], s.hi[3] = math.max(s.hi[1], wx), math.max(s.hi[2], wy), math.max(s.hi[3], wz)
end
-- world area of the faces (fan-triangulated polygons)
local mode = p.mode
if mode == "triangles" or mode == "polygons" or mode == "triangle_strip" or mode == "triangle_fan" then
local ir = if p.indices then Types.Scene.reader(p.indices) else nil
local ind = function(i: number, _c: number): number
return if ir then ir(i, 0) else i
end
local count = if p.indices then p.indices.count else P.count
local function tri(a: number, b: number, c: number)
local A, B, Cc = w[a], w[b], w[c]
if A and B and Cc then
local e1 = { B[1] - A[1], B[2] - A[2], B[3] - A[3] }
local e2 = { Cc[1] - A[1], Cc[2] - A[2], Cc[3] - A[3] }
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]
s.area += math.sqrt(x * x + y * y + z * z) / 2
end
end
if mode == "triangles" then
for t = 0, count // 3 - 1 do
tri(ind(t * 3, 0), ind(t * 3 + 1, 0), ind(t * 3 + 2, 0))
end
elseif mode == "triangle_strip" then
for t = 0, count - 3 do
tri(ind(t, 0), ind(t + 1, 0), ind(t + 2, 0))
end
elseif mode == "triangle_fan" then
for t = 1, count - 2 do
tri(ind(0, 0), ind(t, 0), ind(t + 1, 0))
end
else
local at = 0
local sizes = assert(Types.Scene.reader(p.faces))
for f = 0, p.faces.count - 1 do
local n = sizes(f, 0)
for k = 1, n - 2 do
tri(ind(at, 0), ind(at + k, 0), ind(at + k + 1, 0))
end
at += n
end
end
end
end
return s
end
function C.boundsNear(lo: { number }, hi: { number }, wlo: { number }, whi: { number }, eps: number): boolean
for k = 1, 3 do
if not C.near(lo[k], wlo[k], eps) or not C.near(hi[k], whi[k], eps) then
return false
end
end
return true
end
function C.fmt(lo: { number }, hi: { number }): string
return string.format("%.5g %.5g %.5g / %.5g %.5g %.5g", lo[1], lo[2], lo[3], hi[1], hi[2], hi[3])
end
return C