perf: OBJ decode 1.65x: plain v / vt / vn / f lines matched in place from the text, faces fanned without temporary tables
This commit is contained in:
1 parent
4c3159d02d
commit
b7e96cbf34
1 file changed
+118
-44
+118
-44
@@ -89,6 +89,11 @@ type Prim = {
|
|||||||
}
|
}
|
||||||
type Section = { object: string?, group: string?, prims: { Prim }, byKey: { [string]: Prim }, smoothing: string? }
|
type Section = { object: string?, group: string?, prims: { Prim }, byKey: { [string]: Prim }, smoothing: string? }
|
||||||
|
|
||||||
|
local TOK = "[ \t]+([^%s#\\]+)"
|
||||||
|
local V2 = "^" .. TOK .. TOK .. "[ \t]*\r?\n()"
|
||||||
|
local V3 = "^" .. TOK .. TOK .. TOK .. "[ \t]*\r?\n()"
|
||||||
|
local V4 = "^" .. TOK .. TOK .. TOK .. TOK .. "[ \t]*\r?\n()"
|
||||||
|
|
||||||
function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||||
local triangulate = not (opts and opts.triangulate == false)
|
local triangulate = not (opts and opts.triangulate == false)
|
||||||
local flipV = not (opts and opts.flipV == false)
|
local flipV = not (opts and opts.flipV == false)
|
||||||
@@ -164,7 +169,117 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
|||||||
return vi, ti, ni
|
return vi, ti, ni
|
||||||
end
|
end
|
||||||
local fs: { string } = {}
|
local fs: { string } = {}
|
||||||
|
local SV, ST, SN = {}, {}, {}
|
||||||
|
local function element(key: string, k: number): Err.Err?
|
||||||
|
local mode = if key == "f" then (if triangulate then "triangles" else "polygons") elseif key == "l" then "lines" else "points"
|
||||||
|
if (key == "f" and k < 3) or (key == "l" and k < 2) or k < 1 then
|
||||||
|
return Err.new(Err.CORRUPT, "an element with too few corners on line " .. lineNo)
|
||||||
|
end
|
||||||
|
corners += k
|
||||||
|
if corners > maxV * 4 then
|
||||||
|
return Err.new(Err.LIMIT, "too many face corners")
|
||||||
|
end
|
||||||
|
local p = prim(mode)
|
||||||
|
for i = 1, k do
|
||||||
|
local tok = fs[i]
|
||||||
|
local vi, ti, ni = corner(tok)
|
||||||
|
if not vi then
|
||||||
|
return Err.new(Err.CORRUPT, "bad index '" .. tok .. "' on line " .. lineNo)
|
||||||
|
end
|
||||||
|
SV[i], ST[i], SN[i] = vi, ti, ni
|
||||||
|
if ti >= 0 then
|
||||||
|
p.hasT = true
|
||||||
|
end
|
||||||
|
if ni >= 0 then
|
||||||
|
p.hasN = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local cv, ct, cn = p.cv, p.ct, p.cn
|
||||||
|
local o = #cv
|
||||||
|
if mode == "triangles" then
|
||||||
|
for a = 2, k - 1 do
|
||||||
|
cv[o + 1], ct[o + 1], cn[o + 1] = SV[1], ST[1], SN[1]
|
||||||
|
cv[o + 2], ct[o + 2], cn[o + 2] = SV[a], ST[a], SN[a]
|
||||||
|
cv[o + 3], ct[o + 3], cn[o + 3] = SV[a + 1], ST[a + 1], SN[a + 1]
|
||||||
|
o += 3
|
||||||
|
end
|
||||||
|
elseif mode == "lines" then
|
||||||
|
for a = 1, k - 1 do
|
||||||
|
cv[o + 1], ct[o + 1], cn[o + 1] = SV[a], ST[a], SN[a]
|
||||||
|
cv[o + 2], ct[o + 2], cn[o + 2] = SV[a + 1], ST[a + 1], SN[a + 1]
|
||||||
|
o += 2
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for i = 1, k do
|
||||||
|
cv[o + i], ct[o + i], cn[o + i] = SV[i], ST[i], SN[i]
|
||||||
|
end
|
||||||
|
if mode == "polygons" then
|
||||||
|
table.insert(p.faces, k)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
while pos <= len do
|
while pos <= len do
|
||||||
|
-- plain "v x y z", "vt u v", "vn x y z" and three- / four-corner "f" lines; anything else takes the general path
|
||||||
|
local c0 = string.byte(text, pos)
|
||||||
|
if c0 == 118 then -- v
|
||||||
|
local c1 = string.byte(text, pos + 1)
|
||||||
|
if c1 == 32 or c1 == 9 then
|
||||||
|
local x, y, z, ne = string.match(text, V3, pos + 1)
|
||||||
|
if x then
|
||||||
|
local fx, fy, fz = tonumber(x), tonumber(y), tonumber(z)
|
||||||
|
if fx and fy and fz and np < maxV then
|
||||||
|
P[np * 3 + 1], P[np * 3 + 2], P[np * 3 + 3] = fx, fy, fz
|
||||||
|
np += 1
|
||||||
|
lineNo += 1
|
||||||
|
pos = ne
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif c1 == 110 then -- vn
|
||||||
|
local x, y, z, ne = string.match(text, V3, pos + 2)
|
||||||
|
if x then
|
||||||
|
local fx, fy, fz = tonumber(x), tonumber(y), tonumber(z)
|
||||||
|
if fx and fy and fz and nn < maxV then
|
||||||
|
N[nn * 3 + 1], N[nn * 3 + 2], N[nn * 3 + 3] = fx, fy, fz
|
||||||
|
nn += 1
|
||||||
|
lineNo += 1
|
||||||
|
pos = ne
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif c1 == 116 then -- vt
|
||||||
|
local u, v, ne = string.match(text, V2, pos + 2)
|
||||||
|
if u then
|
||||||
|
local fu, fv = tonumber(u), tonumber(v)
|
||||||
|
if fu and fv and nt < maxV then
|
||||||
|
UV[nt * 2 + 1], UV[nt * 2 + 2] = fu, if flipV then 1 - fv else fv
|
||||||
|
nt += 1
|
||||||
|
lineNo += 1
|
||||||
|
pos = ne
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif c0 == 102 then -- f
|
||||||
|
local a, b, c, ne = string.match(text, V3, pos + 1)
|
||||||
|
local k = 3
|
||||||
|
local d = nil
|
||||||
|
if not a then
|
||||||
|
a, b, c, d, ne = string.match(text, V4, pos + 1)
|
||||||
|
k = 4
|
||||||
|
end
|
||||||
|
if a then
|
||||||
|
fs[1], fs[2], fs[3], fs[4] = a, b, c, d
|
||||||
|
lineNo += 1
|
||||||
|
local err = element("f", k)
|
||||||
|
if err then
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
pos = ne
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
end
|
||||||
local e = string.find(text, "\n", pos, true) or (len + 1)
|
local e = string.find(text, "\n", pos, true) or (len + 1)
|
||||||
local line = string.sub(text, pos, e - 1)
|
local line = string.sub(text, pos, e - 1)
|
||||||
pos = e + 1
|
pos = e + 1
|
||||||
@@ -229,50 +344,9 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
|||||||
for tok in string.gmatch(rest, "%S+") do
|
for tok in string.gmatch(rest, "%S+") do
|
||||||
table.insert(fs, tok)
|
table.insert(fs, tok)
|
||||||
end
|
end
|
||||||
local k = #fs
|
local err = element(key, #fs)
|
||||||
local mode = if key == "f" then (if triangulate then "triangles" else "polygons") elseif key == "l" then "lines" else "points"
|
if err then
|
||||||
if (key == "f" and k < 3) or (key == "l" and k < 2) or k < 1 then
|
return nil, err
|
||||||
return nil, Err.new(Err.CORRUPT, "an element with too few corners on line " .. lineNo)
|
|
||||||
end
|
|
||||||
corners += k
|
|
||||||
if corners > maxV * 4 then
|
|
||||||
return nil, Err.new(Err.LIMIT, "too many face corners")
|
|
||||||
end
|
|
||||||
local p = prim(mode)
|
|
||||||
local base = #p.cv
|
|
||||||
for i, tok in fs do
|
|
||||||
local vi, ti, ni = corner(tok)
|
|
||||||
if not vi then
|
|
||||||
return nil, Err.new(Err.CORRUPT, "bad index '" .. tok .. "' on line " .. lineNo)
|
|
||||||
end
|
|
||||||
p.cv[base + i], p.ct[base + i], p.cn[base + i] = vi, ti, ni
|
|
||||||
if ti >= 0 then
|
|
||||||
p.hasT = true
|
|
||||||
end
|
|
||||||
if ni >= 0 then
|
|
||||||
p.hasN = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if mode == "triangles" and k > 3 then
|
|
||||||
local cv, ct, cn = table.move(p.cv, base + 1, base + k, 1, {}), table.move(p.ct, base + 1, base + k, 1, {}), table.move(p.cn, base + 1, base + k, 1, {})
|
|
||||||
local o = base
|
|
||||||
for a = 2, k - 1 do
|
|
||||||
for _, j in { 1, a, a + 1 } do
|
|
||||||
o += 1
|
|
||||||
p.cv[o], p.ct[o], p.cn[o] = cv[j], ct[j], cn[j]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
elseif mode == "lines" then
|
|
||||||
local cv, ct, cn = table.move(p.cv, base + 1, base + k, 1, {}), table.move(p.ct, base + 1, base + k, 1, {}), table.move(p.cn, base + 1, base + k, 1, {})
|
|
||||||
local o = base
|
|
||||||
for a = 1, k - 1 do
|
|
||||||
for _, j in { a, a + 1 } do
|
|
||||||
o += 1
|
|
||||||
p.cv[o], p.ct[o], p.cn[o] = cv[j], ct[j], cn[j]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
elseif mode == "polygons" then
|
|
||||||
table.insert(p.faces, k)
|
|
||||||
end
|
end
|
||||||
elseif key == "o" then
|
elseif key == "o" then
|
||||||
object = rest
|
object = rest
|
||||||
|
|||||||
Reference in new issue
Block a user