ws-3d3: codecs/model/Draco: kd-tree point clouds (bitstream 2.3, levels 0 - 6)
Byte-exact against libdraco on all 206 test files, kd-tree point clouds included.
This commit is contained in:
1 parent
de2695b87d
commit
4f2a388a6d
4 files changed
+388
-19
No files matched your search
@@ -9,6 +9,7 @@
|
||||
local Reader = require("@formats/codecs/model/Draco/Reader")
|
||||
local Connectivity = require("@formats/codecs/model/Draco/Connectivity")
|
||||
local Predict = require("@formats/codecs/model/Draco/Predict")
|
||||
local KdTree = require("@formats/codecs/model/Draco/KdTree")
|
||||
|
||||
local Attributes = {}
|
||||
|
||||
@@ -285,8 +286,6 @@ function Attributes.decode(d: D, g: Geom): ({ Attr }?, string?)
|
||||
-- prediction data: the attribute's own corner table when its seams are in use
|
||||
local pct = if ad and ad.used then ad.ct :: Connectivity.CT else ct
|
||||
dec.md = { ct = pct, e2c = enc.e2c, v2e = enc.v2e }
|
||||
elseif g.kdTree then
|
||||
return nil, "unsupported: kd-tree point clouds"
|
||||
end
|
||||
decoders[i + 1] = dec
|
||||
end
|
||||
@@ -323,7 +322,7 @@ function Attributes.decode(d: D, g: Geom): ({ Attr }?, string?)
|
||||
table.insert(all, a)
|
||||
table.insert(dec.attrs, a)
|
||||
end
|
||||
for _, a in dec.attrs do
|
||||
for _, a in (if g.kdTree then {} else dec.attrs) do
|
||||
local t = Reader.u8(d)
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
@@ -353,6 +352,13 @@ function Attributes.decode(d: D, g: Geom): ({ Attr }?, string?)
|
||||
end
|
||||
-- decode
|
||||
for _, dec in decoders do
|
||||
if g.kdTree then
|
||||
local ok, err = KdTree.decode(d, dec.attrs, g.numPoints)
|
||||
if not ok then
|
||||
return nil, err
|
||||
end
|
||||
continue
|
||||
end
|
||||
local points: { number }
|
||||
local trav = dec.traversal
|
||||
if trav then
|
||||
|
||||
@@ -0,0 +1,363 @@
|
||||
--!strict
|
||||
-- codecs/model/Draco/KdTree: kd-tree coded point cloud attributes (bitstream 2.3): DynamicIntegerPointsKdTreeDecoder
|
||||
-- at compression levels 0 - 6 (direct bits, rANS bits, folded 32-bit rANS numbers, selected axes) over all the
|
||||
-- attributes of a decoder at once, then the signed offsets and the dequantization of f32 attributes. Ported from
|
||||
-- Google's Draco 1.5.7 (compression/attributes/kd_tree_attributes_decoder.cc, compression/point_cloud/algorithms/
|
||||
-- dynamic_integer_points_kd_tree_decoder.h, compression/bit_coders/{direct,folded_integer}_bit_decoder; Apache-2.0,
|
||||
-- licenses/draco-LICENSE.txt). The pre-2.3 kd-tree layouts are not supported.
|
||||
local Reader = require("@formats/codecs/model/Draco/Reader")
|
||||
local Predict = require("@formats/codecs/model/Draco/Predict")
|
||||
|
||||
local KdTree = {}
|
||||
|
||||
type D = Reader.D
|
||||
|
||||
-- a bit source: next() -> 0 / 1, bits(n) -> value (MSB first); bits returns nil when a direct decoder runs out
|
||||
type Src = { next: () -> number, bits: (n: number) -> number? }
|
||||
|
||||
local function direct(d: D): Src?
|
||||
local size = Reader.u32(d)
|
||||
if d.bad or size == 0 or size % 4 ~= 0 or size > Reader.remaining(d) then
|
||||
Reader.fail(d, "kd-tree: bad direct bits")
|
||||
return nil
|
||||
end
|
||||
local b, base, words = d.b, d.pos, size // 4
|
||||
d.pos += size
|
||||
local pos, used = 0, 0
|
||||
return {
|
||||
next = function(): number
|
||||
if pos >= words then
|
||||
return 0
|
||||
end
|
||||
local w = buffer.readu32(b, base + pos * 4)
|
||||
local bit = bit32.extract(w, 31 - used)
|
||||
used += 1
|
||||
if used == 32 then
|
||||
pos += 1
|
||||
used = 0
|
||||
end
|
||||
return bit
|
||||
end,
|
||||
bits = function(n: number): number?
|
||||
local remaining = 32 - used
|
||||
if n <= remaining then
|
||||
if pos >= words then
|
||||
return nil
|
||||
end
|
||||
local w = buffer.readu32(b, base + pos * 4)
|
||||
local v = bit32.extract(w, 32 - used - n, n)
|
||||
used += n
|
||||
if used == 32 then
|
||||
pos += 1
|
||||
used = 0
|
||||
end
|
||||
return v
|
||||
end
|
||||
if pos + 1 >= words then
|
||||
return nil
|
||||
end
|
||||
local hiBits = bit32.extract(buffer.readu32(b, base + pos * 4), 0, remaining)
|
||||
local lo = n - remaining
|
||||
pos += 1
|
||||
used = lo
|
||||
local loBits = bit32.extract(buffer.readu32(b, base + pos * 4), 32 - lo, lo)
|
||||
return hiBits * 2 ^ lo + loBits
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
local function rans(d: D): Src?
|
||||
local bd = Reader.bitDecoder(d)
|
||||
if not bd then
|
||||
return nil
|
||||
end
|
||||
local function nextBit(): number
|
||||
return if Reader.bit(bd) then 1 else 0
|
||||
end
|
||||
return {
|
||||
next = nextBit,
|
||||
bits = function(n: number): number?
|
||||
local v = 0
|
||||
for _ = 1, n do
|
||||
v = v * 2 + nextBit()
|
||||
end
|
||||
return v
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
local function folded(d: D): Src?
|
||||
local parts: { Reader.Bits } = {}
|
||||
for i = 1, 32 do
|
||||
local bd = Reader.bitDecoder(d)
|
||||
if not bd then
|
||||
return nil
|
||||
end
|
||||
parts[i] = bd
|
||||
end
|
||||
local main = Reader.bitDecoder(d)
|
||||
if not main then
|
||||
return nil
|
||||
end
|
||||
return {
|
||||
next = function(): number
|
||||
return if Reader.bit(main :: Reader.Bits) then 1 else 0
|
||||
end,
|
||||
bits = function(n: number): number?
|
||||
local v = 0
|
||||
for i = 1, n do
|
||||
v = v * 2 + (if Reader.bit(parts[i]) then 1 else 0)
|
||||
end
|
||||
return v
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
local function msb(n: number): number
|
||||
local k = -1
|
||||
while n >= 1 do
|
||||
n = n // 2
|
||||
k += 1
|
||||
end
|
||||
return k
|
||||
end
|
||||
|
||||
-- DynamicIntegerPointsKdTreeDecoder::DecodePoints -> points (each a table of `dim` u32 values), or nil
|
||||
local function decodePoints(d: D, level: number, dim: number, expected: number): ({ { number } }?, string?)
|
||||
local bitLength = Reader.u32(d)
|
||||
local numPoints = Reader.u32(d)
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
end
|
||||
if bitLength > 32 then
|
||||
return nil, "kd-tree: bad bit length"
|
||||
end
|
||||
if numPoints ~= expected then
|
||||
return nil, "kd-tree: point count mismatch"
|
||||
end
|
||||
local out: { { number } } = {}
|
||||
if numPoints == 0 then
|
||||
return out, nil
|
||||
end
|
||||
local numbers = if level >= 4 then folded(d) elseif level >= 2 then rans(d) else direct(d)
|
||||
local remainingBits = numbers and direct(d)
|
||||
local axisDec = remainingBits and direct(d)
|
||||
local half = axisDec and direct(d)
|
||||
if not numbers or not remainingBits or not axisDec or not half then
|
||||
return nil, d.bad or "kd-tree: bad bit decoders"
|
||||
end
|
||||
local selectAxis = level >= 6
|
||||
local zero = table.create(dim, 0)
|
||||
local baseStack: { { number } } = { table.clone(zero) }
|
||||
local levelStack: { { number } } = { table.clone(zero) }
|
||||
local stack = { { numPoints, 0, 0 } }
|
||||
local decoded = 0
|
||||
local p = table.create(dim, 0)
|
||||
while #stack > 0 do
|
||||
local st = stack[#stack]
|
||||
stack[#stack] = nil
|
||||
local n, lastAxis, sp = st[1], st[2], st[3]
|
||||
local oldBase, levels = baseStack[sp + 1], levelStack[sp + 1]
|
||||
if n > numPoints then
|
||||
return nil, "kd-tree: bad point count"
|
||||
end
|
||||
local axis
|
||||
if not selectAxis then
|
||||
axis = if lastAxis == dim - 1 then 0 else lastAxis + 1
|
||||
elseif n < 64 then
|
||||
axis = 0
|
||||
for a = 1, dim - 1 do
|
||||
if levels[axis + 1] > levels[a + 1] then
|
||||
axis = a
|
||||
end
|
||||
end
|
||||
else
|
||||
axis = (axisDec :: Src).bits(4) or 0
|
||||
end
|
||||
if axis >= dim then
|
||||
return nil, "kd-tree: bad axis"
|
||||
end
|
||||
local lvl = levels[axis + 1]
|
||||
if bitLength - lvl == 0 then
|
||||
for _ = 1, n do
|
||||
table.insert(out, table.clone(oldBase))
|
||||
decoded += 1
|
||||
end
|
||||
if decoded > numPoints then
|
||||
return nil, "kd-tree: too many points"
|
||||
end
|
||||
continue
|
||||
end
|
||||
if n <= 2 then
|
||||
for _ = 1, n do
|
||||
local a = axis
|
||||
for _ = 1, dim do
|
||||
local rb = bitLength - levels[a + 1]
|
||||
local v = 0
|
||||
if rb > 0 then
|
||||
local got = remainingBits.bits(rb)
|
||||
if got == nil then
|
||||
return nil, "kd-tree: remaining bits run out"
|
||||
end
|
||||
v = got
|
||||
end
|
||||
p[a + 1] = bit32.bor(oldBase[a + 1], v)
|
||||
a = if a == dim - 1 then 0 else a + 1
|
||||
end
|
||||
table.insert(out, table.clone(p))
|
||||
decoded += 1
|
||||
end
|
||||
if decoded > numPoints then
|
||||
return nil, "kd-tree: too many points"
|
||||
end
|
||||
continue
|
||||
end
|
||||
if decoded > numPoints then
|
||||
return nil, "kd-tree: too many points"
|
||||
end
|
||||
local rbits = bitLength - lvl
|
||||
local modifier = 2 ^ (rbits - 1)
|
||||
local nb = table.clone(oldBase)
|
||||
nb[axis + 1] = (nb[axis + 1] + modifier) % 4294967296
|
||||
baseStack[sp + 2] = nb
|
||||
local number = numbers.bits(msb(n)) or 0
|
||||
local first = n // 2
|
||||
if first < number then
|
||||
return nil, "kd-tree: bad split"
|
||||
end
|
||||
first -= number
|
||||
local second = n - first
|
||||
if first ~= second and half.next() == 0 then
|
||||
first, second = second, first
|
||||
end
|
||||
levels[axis + 1] += 1
|
||||
levelStack[sp + 2] = table.clone(levels)
|
||||
if sp + 2 > 32 * dim + 1 then
|
||||
return nil, "kd-tree: too deep"
|
||||
end
|
||||
if first > 0 then
|
||||
table.insert(stack, { first, axis, sp })
|
||||
end
|
||||
if second > 0 then
|
||||
table.insert(stack, { second, axis, sp + 1 })
|
||||
end
|
||||
end
|
||||
if decoded ~= expected then
|
||||
return nil, "kd-tree: point count mismatch"
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
local SIGNED = { [1] = 8, [3] = 16, [5] = 32 }
|
||||
|
||||
-- decodes the attributes of one kd-tree decoder (Attributes.Attr records: dataType, comps; sets values / numValues)
|
||||
function KdTree.decode(d: D, attrs: { any }, numPoints: number): (boolean, string?)
|
||||
if d.ver < 0x0203 then
|
||||
return false, "unsupported: kd-tree point clouds before bitstream 2.3"
|
||||
end
|
||||
local level = Reader.u8(d)
|
||||
if d.bad then
|
||||
return false, d.bad
|
||||
end
|
||||
if level > 6 then
|
||||
return false, "kd-tree: bad compression level"
|
||||
end
|
||||
local dim = 0
|
||||
for _, a in attrs do
|
||||
local dt = a.dataType
|
||||
if dt ~= 2 and dt ~= 4 and dt ~= 6 and dt ~= 1 and dt ~= 3 and dt ~= 5 and dt ~= 9 then
|
||||
return false, "unsupported: kd-tree attribute of data type " .. dt
|
||||
end
|
||||
dim += a.comps
|
||||
end
|
||||
local points, err = decodePoints(d, level, dim, numPoints)
|
||||
if not points then
|
||||
return false, err
|
||||
end
|
||||
-- the portable values per attribute: the low bytes of each u32 for 8 / 16-bit types
|
||||
local off = 0
|
||||
for _, a in attrs do
|
||||
local comps = a.comps
|
||||
local vals = table.create(numPoints * comps, 0)
|
||||
local mod = if a.dataType == 1 or a.dataType == 2 then 256 elseif a.dataType == 3 or a.dataType == 4 then 65536 else 4294967296
|
||||
local raw = a.dataType == 9
|
||||
for i, pt in points do
|
||||
for c = 1, comps do
|
||||
local v = pt[off + c]
|
||||
vals[(i - 1) * comps + c] = if raw then v else v % mod
|
||||
end
|
||||
end
|
||||
a.portable = vals
|
||||
a.values = vals
|
||||
a.numValues = numPoints
|
||||
a.map = nil
|
||||
off += comps
|
||||
end
|
||||
-- the data needed by the transforms: f32 quantization, then the signed minima
|
||||
for _, a in attrs do
|
||||
if a.dataType == 9 then
|
||||
local mins = table.create(a.comps, 0)
|
||||
for c = 1, a.comps do
|
||||
mins[c] = Reader.f32(d)
|
||||
end
|
||||
local range = Reader.f32(d)
|
||||
local bits = Reader.u8(d)
|
||||
if d.bad or bits < 1 or bits > 30 then
|
||||
return false, d.bad or "kd-tree: bad quantization"
|
||||
end
|
||||
a.quant = { min = mins, range = range, bits = bits }
|
||||
end
|
||||
end
|
||||
for _, a in attrs do
|
||||
if SIGNED[a.dataType] then
|
||||
local mins = table.create(a.comps, 0)
|
||||
for c = 1, a.comps do
|
||||
local v = Reader.varint(d)
|
||||
mins[c] = if v % 2 == 1 then -(v // 2) - 1 else v // 2
|
||||
end
|
||||
if d.bad then
|
||||
return false, d.bad
|
||||
end
|
||||
a.signedMin = mins
|
||||
end
|
||||
end
|
||||
-- back to the original values
|
||||
local fround = Predict.fround
|
||||
for _, a in attrs do
|
||||
local vals = a.portable :: { number }
|
||||
local comps = a.comps
|
||||
if a.dataType == 9 then
|
||||
local q = a.quant
|
||||
local delta = fround(q.range / fround(2 ^ q.bits - 1))
|
||||
local out = table.create(#vals, 0)
|
||||
for i = 1, #vals do
|
||||
local c = (i - 1) % comps + 1
|
||||
local v = vals[i]
|
||||
if v >= 2147483648 then
|
||||
v -= 4294967296
|
||||
end
|
||||
out[i] = fround(fround(fround(v) * delta) + q.min[c])
|
||||
end
|
||||
a.values = out
|
||||
elseif SIGNED[a.dataType] then
|
||||
-- the C++ reads the stored (already narrowed) unsigned values back
|
||||
local bits = SIGNED[a.dataType]
|
||||
local mins = a.signedMin
|
||||
local out = table.create(#vals, 0)
|
||||
for i = 1, #vals do
|
||||
local c = (i - 1) % comps + 1
|
||||
local u = vals[i]
|
||||
if u > 2147483647 then
|
||||
return false, "kd-tree: signed value out of range"
|
||||
end
|
||||
local s = Predict.i32(u + mins[c])
|
||||
local m = 2 ^ bits
|
||||
out[i] = (s + m / 2) % m - m / 2
|
||||
end
|
||||
a.values = out
|
||||
end
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
return KdTree
|
||||
@@ -3,11 +3,12 @@
|
||||
-- A codec, not a format: model/gltf (and any format carrying Draco data) calls it on a byte range.
|
||||
-- Ported from Google's Draco 1.5.7 (Apache-2.0, licenses/draco-LICENSE.txt): the decoder only, bitstream versions
|
||||
-- 1.0 - 2.2 for meshes (sequential and Edgebreaker connectivity: standard, predictive and valence traversals; topology
|
||||
-- splits, attribute seams) and sequential point clouds; attribute decoders generic / integer / quantization /
|
||||
-- octahedral normals; prediction schemes difference, parallelogram, multi-parallelogram, constrained
|
||||
-- multi-parallelogram, tex coords (deprecated and portable), geometric normal, with the wrap and normal octahedron
|
||||
-- transforms; metadata. Values match draco_decoder bit for bit (f32 rounding at every step of the dequantization).
|
||||
-- Not supported: kd-tree point clouds (Err.UNSUPPORTED).
|
||||
-- splits, attribute seams), sequential point clouds and kd-tree point clouds (2.3); attribute decoders generic /
|
||||
-- integer / quantization / octahedral normals; prediction schemes difference, parallelogram, multi-parallelogram,
|
||||
-- constrained multi-parallelogram, tex coords (deprecated and portable), geometric normal, with the wrap and normal
|
||||
-- octahedron transforms; metadata. Values match draco_decoder bit for bit (f32 rounding at every step of the
|
||||
-- dequantization).
|
||||
-- Not supported: the pre-2.3 kd-tree point cloud layouts (Err.UNSUPPORTED).
|
||||
--
|
||||
-- local Draco = require("@formats/codecs/model/Draco")
|
||||
-- local g, err = Draco.decode(buf, off?, len?, opts?) -- buffer | string
|
||||
@@ -262,12 +263,10 @@ local function decode(buf: buffer, o: number, l: number, opts: any): (Geometry?,
|
||||
end
|
||||
numPoints = m.numPoints
|
||||
else
|
||||
if h.method == 1 then
|
||||
return nil, "unsupported: kd-tree point clouds", nil
|
||||
elseif h.method ~= 0 then
|
||||
if h.method > 1 then
|
||||
return nil, "unsupported: point cloud encoding method " .. h.method, nil
|
||||
end
|
||||
method = "sequential"
|
||||
method = if h.method == 1 then "kdtree" else "sequential"
|
||||
numPoints = Reader.i32(d)
|
||||
if d.bad then
|
||||
return nil, d.bad, nil
|
||||
@@ -279,7 +278,7 @@ local function decode(buf: buffer, o: number, l: number, opts: any): (Geometry?,
|
||||
if numPoints > maxPoints then
|
||||
return nil, "too many points", "limit"
|
||||
end
|
||||
local attrs, aerr = Attributes.decode(d, { numPoints = numPoints, mesh = mesh, edgebreaker = method == "edgebreaker", kdTree = false })
|
||||
local attrs, aerr = Attributes.decode(d, { numPoints = numPoints, mesh = mesh, edgebreaker = method == "edgebreaker", kdTree = method == "kdtree" })
|
||||
if not attrs then
|
||||
return nil, aerr, nil
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
-- codecs/model/Draco tests: Google's Draco test data (bitstreams 0.9 - 2.2) and files encoded by draco_encoder 1.5.7
|
||||
-- at every compression level, quantization and as point clouds, decoded and compared byte for byte (points, faces,
|
||||
-- every attribute's values per point, as CRCs) with libdraco's own decoder (tools/fixtures/_draco_dump.cc); kd-tree
|
||||
-- point clouds report UNSUPPORTED; argument errors; the fuzz no-throw pass (which also fails on any internal error the
|
||||
-- every attribute's values per point, as CRCs) with libdraco's own decoder (tools/fixtures/_draco_dump.cc), kd-tree
|
||||
-- point clouds included; argument errors; the fuzz no-throw pass (which also fails on any internal error the
|
||||
-- decoder's safety net caught). Fixtures: tools/fixtures/draco.py.
|
||||
local T = require("@tests/T")
|
||||
local Draco = require("@formats/codecs/model/Draco")
|
||||
@@ -11,15 +11,13 @@ local Cases = require("@tests/gen/draco_cases")
|
||||
|
||||
local t = T.suite("draco")
|
||||
|
||||
local counts = { exact = 0, unsupported = 0, rejected = 0 }
|
||||
local counts = { exact = 0, rejected = 0, kd = 0 }
|
||||
local samples = {}
|
||||
for _, c in Cases do
|
||||
local g, err = Draco.decode(c.data)
|
||||
if c.error then
|
||||
t:check(g == nil and err ~= nil, c.name .. ": libdraco rejects it, so do we")
|
||||
counts.rejected += 1
|
||||
elseif not g and err and err.code == Err.UNSUPPORTED and string.find(err.message, "kd-tree", 1, true) then
|
||||
counts.unsupported += 1
|
||||
else
|
||||
t:check(g, c.name .. ": decode: " .. tostring(err))
|
||||
if g then
|
||||
@@ -41,13 +39,16 @@ for _, c in Cases do
|
||||
end
|
||||
end
|
||||
counts.exact += 1
|
||||
if g.method == "kdtree" then
|
||||
counts.kd += 1
|
||||
end
|
||||
if #c.data < 4000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:note("%d files decoded exactly like libdraco, %d kd-tree point clouds unsupported, %d rejected by both", counts.exact, counts.unsupported, counts.rejected)
|
||||
t:note("%d files decoded exactly like libdraco (%d kd-tree point clouds), %d rejected by both", counts.exact, counts.kd, counts.rejected)
|
||||
|
||||
-- the attribute lookup and the header
|
||||
local first = Cases[1]
|
||||
|
||||
Reference in new issue
Block a user