--!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