codecs/model: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:04:29 -06:00
1 parent 0bd7b87d2b
commit 9b6a205844
26 files changed
+134 -571

No files matched your search

+9 -21
View File
@@ -1,11 +1,7 @@
--!strict
-- codecs/model/Draco/Attributes: Draco attribute decoding. The attributes decoders (one per connectivity: linear
-- sequencer for sequential meshes and point clouds, traversal sequencers for Edgebreaker), their attribute
-- descriptors, and the sequential attribute decoders: generic (raw values), integer, quantization (dequantized to
-- f32 exactly like the C++) and octahedral normals; prediction through codecs/model/Draco/Predict. Bitstream
-- versions 1.0 - 2.2. Ported from Google's Draco 1.5.7 (compression/attributes/*, compression/point_cloud/
-- point_cloud_decoder.cc, attributes/attribute_quantization_transform.cc, attribute_octahedron_transform.cc;
-- Apache-2.0, licenses/draco-LICENSE.txt).
-- codecs/model/Draco/Attributes: Draco attribute decoders and sequential attribute decoding (bitstreams 1.0 - 2.2).
-- Port of Draco 1.5.7 compression/attributes/*, point_cloud_decoder.cc, attribute_quantization_transform.cc and
-- attribute_octahedron_transform.cc (Apache-2.0, licenses/draco-LICENSE.txt).
local Reader = require("@rosetta/codecs/model/Draco/Reader")
local Connectivity = require("@rosetta/codecs/model/Draco/Connectivity")
local Predict = require("@rosetta/codecs/model/Draco/Predict")
@@ -20,18 +16,18 @@ local DT_LENGTH = { [1] = 1, [2] = 1, [3] = 2, [4] = 2, [5] = 4, [6] = 4, [7] =
Attributes.DT_LENGTH = DT_LENGTH
export type Attr = {
id: number, -- 0-based attribute id
id: number,
type: number, -- GeometryAttribute::Type (0 POSITION, 1 NORMAL, 2 COLOR, 3 TEX_COORD, 4 GENERIC, 5.. transcoder types)
dataType: number,
comps: number,
normalized: boolean,
uniqueId: number,
seqType: number, -- 0 generic, 1 integer, 2 quantization, 3 normals
map: { number }?, -- point -> value (0-based values at [point + 1]); nil = identity
map: { number }?,
numValues: number,
portable: { number }?, -- the integer (portable) values
values: { number }?, -- the final values (numbers), flat
raw: buffer?, -- generic attributes: the raw values
portable: { number }?,
values: { number }?,
raw: buffer?,
quant: { min: { number }, range: number, bits: number }?,
octBits: number?,
scheme: Predict.Scheme?,
@@ -44,7 +40,6 @@ type Decoder = {
points: { number }?,
}
-- the geometry the attributes decode against
export type Geom = { numPoints: number, mesh: Connectivity.Mesh?, edgebreaker: boolean, kdTree: boolean }
local function zigzag(v: number): number
@@ -54,8 +49,7 @@ local function zigzag(v: number): number
return v // 2
end
-- the parent (position) getter for a scheme: portable integers from 2.0 on, the final values before (truncated to
-- integers unless the scheme works in floats)
-- parent getter: portable integers from 2.0 on, the final values before (truncated unless the scheme uses floats)
local function parentGetter(ver: number, pos: Attr, floats: boolean): Predict.Parent?
local old = ver < 0x0200
local vals = if old then pos.values else pos.portable
@@ -228,7 +222,6 @@ local function storeValues(a: Attr): (boolean, string?)
return true, nil
end
-- decodes every attribute; returns them in attribute id order
function Attributes.decode(d: D, g: Geom): ({ Attr }?, string?)
local ver = d.ver
local numDecoders = Reader.u8(d)
@@ -283,13 +276,11 @@ function Attributes.decode(d: D, g: Geom): ({ Attr }?, string?)
enc = cad.enc
dec.traversal = { ct = cad.ct :: Connectivity.CT, enc = enc, method = 0 }
end
-- 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 }
end
decoders[i + 1] = dec
end
-- the attribute descriptors
local all: { Attr } = {}
for _, dec in decoders do
local n = Reader.count(d, 0x0200)
@@ -350,7 +341,6 @@ function Attributes.decode(d: D, g: Geom): ({ Attr }?, string?)
end
return nil
end
-- decode
for _, dec in decoders do
if g.kdTree then
local ok, err = KdTree.decode(d, dec.attrs, g.numPoints)
@@ -383,7 +373,6 @@ function Attributes.decode(d: D, g: Geom): ({ Attr }?, string?)
end
end
dec.points = points
-- portable values
for _, a in dec.attrs do
if a.seqType == 0 then
local stride = DT_LENGTH[a.dataType] * a.comps
@@ -449,7 +438,6 @@ function Attributes.decode(d: D, g: Geom): ({ Attr }?, string?)
end
end
end
-- data needed by the transforms
if ver >= 0x0200 then
for _, a in dec.attrs do
if a.seqType == 2 then
@@ -1,27 +1,21 @@
--!strict
-- codecs/model/Draco/Connectivity: Draco mesh connectivity. The sequential decoder (raw or rANS delta-coded indices)
-- and the Edgebreaker decoder (standard, predictive and valence traversals, topology splits, start faces, attribute
-- seams), the corner tables they build (the mesh's and one per attribute seam set), and the traversal sequencers
-- (depth first and max prediction degree) that order attribute values. Ported from Google's Draco 1.5.7
-- (compression/mesh/mesh_sequential_decoder.cc, mesh_edgebreaker_decoder_impl.cc, mesh_edgebreaker_traversal_*,
-- mesh/corner_table, mesh/mesh_attribute_corner_table, compression/mesh/traverser/*; Apache-2.0,
-- licenses/draco-LICENSE.txt). Corners, vertices and faces are 0-based values stored at [i + 1]; -1 is invalid.
-- codecs/model/Draco/Connectivity: Draco mesh connectivity (sequential and Edgebreaker) and traversal sequencers.
-- Port of Draco 1.5.7 mesh_sequential_decoder.cc, mesh_edgebreaker_decoder_impl.cc, mesh_edgebreaker_traversal_*,
-- corner_table, mesh_attribute_corner_table, traverser/* (Apache-2.0, licenses/draco-LICENSE.txt).
local Reader = require("@rosetta/codecs/model/Draco/Reader")
local Connectivity = {}
type D = Reader.D
-- a corner table: corner -> vertex, corner -> opposite corner, vertex -> left-most corner
export type CT = { c2v: { number }, opp: { number }, lmc: { number }, nv: number, nf: number }
-- encoding data of one attribute connectivity: vertex -> value index, value index -> corner
export type Enc = { v2e: { number }, e2c: { number }, numValues: number }
export type AttrData = { decoderId: number, used: boolean, seams: { number }, ct: CT?, enc: Enc }
export type Mesh = {
faces: { number }, -- corner -> point
faces: { number },
numFaces: number,
numPoints: number,
ct: CT?, -- Edgebreaker only
ct: CT?,
attrData: { AttrData },
posEnc: Enc?,
posDecoderId: number,
@@ -49,7 +43,6 @@ local function newEnc(nv: number): Enc
return { v2e = table.create(nv, 0), e2c = {}, numValues = 0 }
end
-- ------------------------------------------------------------------------------------------------ sequential
function Connectivity.sequential(d: D): (Mesh?, string?)
local numFaces, numPoints
if d.ver < 0x0202 then
@@ -124,8 +117,6 @@ function Connectivity.sequential(d: D): (Mesh?, string?)
return { faces = faces, numFaces = numFaces, numPoints = numPoints, ct = nil, attrData = {}, posEnc = nil, posDecoderId = -1 }, nil
end
-- ------------------------------------------------------------------------------------------------ edgebreaker
-- the traversal decoders: symbols, start face configurations, attribute seam bits
type Trav = {
decodeSymbol: () -> number,
newActiveCorner: (corner: number) -> (),
@@ -134,8 +125,7 @@ type Trav = {
seam: (i: number) -> boolean,
}
-- MeshEdgebreakerTraversalDecoder::Start (the standard parts): symbols (bits), start faces, seam decoders.
-- Returns the decoder state and the buffer after them.
-- MeshEdgebreakerTraversalDecoder::Start
local function standardStart(buf: D, numAttributeData: number, withSymbols: boolean): (any?, D)
local st: any = {}
if withSymbols then
@@ -195,7 +185,7 @@ local function standardSymbol(sym: D): number
return s + Reader.bits(sym, 2) * 2
end
-- builds the traversal decoder for `kind` (0 standard, 1 predictive, 2 valence); returns it and the buffer after its data
-- traversal decoder kind: 0 standard, 1 predictive, 2 valence
local function makeTraversal(kind: number, buf: D, numAttributeData: number, numVertices: number, numFaces: number, ct: CT): (Trav?, D)
local ver = buf.ver
local st, out = standardStart(buf, numAttributeData, kind ~= 2 or ver < 0x0202)
@@ -372,7 +362,6 @@ local function makeTraversal(kind: number, buf: D, numAttributeData: number, num
}, out
end
-- hole and topology split events; returns the split stack (source / split symbol ids, edges) or nil
local function splitEvents(eb: D, numFaces: number): { any }?
local ver = eb.ver
local n = Reader.count(eb, 0x0200)
@@ -559,7 +548,6 @@ function Connectivity.edgebreaker(d: D, traversalKind: number): (Mesh?, string?)
end
local maxVertices = numEncodedVertices + numSplitSymbols
-- the corner table (filled lazily: nil = invalid)
local c2v: { number } = {}
local opp: { number } = {}
local lmc: { number } = {}
@@ -630,7 +618,6 @@ function Connectivity.edgebreaker(d: D, traversalKind: number): (Mesh?, string?)
return prevC(O(prevC(c)))
end
-- the symbols
local stack: { number } = {}
local sp = 0
local splitActive: { [number]: number } = {}
@@ -869,7 +856,6 @@ function Connectivity.edgebreaker(d: D, traversalKind: number): (Mesh?, string?)
d.pos += splitBytes
end
-- attribute seams
local attrData: { AttrData } = {}
for i = 1, numAttributeData do
attrData[i] = { decoderId = -1, used = true, seams = {}, ct = nil, enc = newEnc(0) }
@@ -990,9 +976,7 @@ function Connectivity.edgebreaker(d: D, traversalKind: number): (Mesh?, string?)
}, nil
end
-- ------------------------------------------------------------------------------------------------ traversal
-- MeshTraversalSequencer::GenerateSequence: the point ids in value order; fills enc (v2e, e2c, numValues).
-- method 0 = depth first, 1 = max prediction degree.
-- MeshTraversalSequencer::GenerateSequence: method 0 depth first, 1 max prediction degree
function Connectivity.traverse(ct: CT, faces: { number }, enc: Enc, method: number): { number }?
local c2v, opp, lmc = ct.c2v, ct.opp, ct.lmc
local nf, nvt = ct.nf, ct.nv
@@ -1093,7 +1077,6 @@ function Connectivity.traverse(ct: CT, faces: { number }, enc: Enc, method: numb
end
return points
end
-- max prediction degree
if nvt == 0 then
return points
end
@@ -1182,7 +1165,7 @@ function Connectivity.traverse(ct: CT, faces: { number }, enc: Enc, method: numb
return points
end
-- MeshTraversalSequencer::UpdatePointToAttributeIndexMapping: point -> value index (1-based table of 0-based values)
-- MeshTraversalSequencer::UpdatePointToAttributeIndexMapping
function Connectivity.mapping(ct: CT, faces: { number }, enc: Enc, numPoints: number): { number }?
local map = table.create(numPoints, 0)
local c2v, v2e = ct.c2v, enc.v2e
+2 -11
View File
@@ -1,7 +1,6 @@
--!strict
-- codecs/model/Draco/Int64: the 64-bit integer arithmetic Draco's portable tex coord predictor relies on (int64 /
-- uint64 with wrap-around, truncating division, IntSqrt), on (hi, lo) pairs of 32-bit halves held in numbers (both
-- runtimes: no native 64-bit integers). Two's complement: hi >= 2^31 is negative when read as signed.
-- codecs/model/Draco/Int64: wrap-around int64 / uint64 for Draco's portable tex coord predictor.
-- Values are (hi, lo) 32-bit halves, two's complement (hi >= 2^31 is negative).
local Int64 = {}
local P32 = 4294967296
@@ -37,7 +36,6 @@ function Int64.sub(ah: number, al: number, bh: number, bl: number): (number, num
return Int64.add(ah, al, nh, nl)
end
-- the product mod 2^64 (the same bits for signed and unsigned operands)
function Int64.mul(ah: number, al: number, bh: number, bl: number): (number, number)
local a0, a1, a2, a3 = al % 65536, al // 65536, ah % 65536, ah // 65536
local b0, b1, b2, b3 = bl % 65536, bl // 65536, bh % 65536, bh // 65536
@@ -56,7 +54,6 @@ function Int64.isNeg(h: number): boolean
return h >= P31
end
-- the signed value as a number (exact below 2^53 in magnitude)
function Int64.signed(h: number, l: number): number
if h >= P31 then
return (h - P32) * P32 + l
@@ -64,12 +61,10 @@ function Int64.signed(h: number, l: number): number
return h * P32 + l
end
-- the unsigned value as a number (rounded past 2^53)
function Int64.unsigned(h: number, l: number): number
return h * P32 + l
end
-- unsigned compare: -1, 0, 1
function Int64.ucmp(ah: number, al: number, bh: number, bl: number): number
if ah ~= bh then
return if ah < bh then -1 else 1
@@ -80,7 +75,6 @@ function Int64.ucmp(ah: number, al: number, bh: number, bl: number): number
return 0
end
-- signed compare: -1, 0, 1
function Int64.cmp(ah: number, al: number, bh: number, bl: number): number
local sa = if ah >= P31 then ah - P32 else ah
local sb = if bh >= P31 then bh - P32 else bh
@@ -90,14 +84,12 @@ function Int64.cmp(ah: number, al: number, bh: number, bl: number): number
return Int64.ucmp(0, al, 0, bl)
end
-- unsigned division (b ~= 0): quotient, remainder
function Int64.udivmod(ah: number, al: number, bh: number, bl: number): (number, number, number, number)
if ah == 0 and bh == 0 then
return 0, al // bl, 0, al % bl
end
local qh, ql, rh, rl = 0, 0, 0, 0
for i = 63, 0, -1 do
-- r = r * 2 + bit i of a
local bit = if i >= 32 then (ah // 2 ^ (i - 32)) % 2 else (al // 2 ^ i) % 2
local over = rh >= P31
rh = (rh * 2 + rl // P31) % P32
@@ -114,7 +106,6 @@ function Int64.udivmod(ah: number, al: number, bh: number, bl: number): (number,
return qh, ql, rh, rl
end
-- signed division truncating toward zero (b ~= 0)
function Int64.div(ah: number, al: number, bh: number, bl: number): (number, number)
local na, nb = ah >= P31, bh >= P31
if na then
+5 -11
View File
@@ -1,10 +1,7 @@
--!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.
-- codecs/model/Draco/KdTree: kd-tree coded point cloud attributes (bitstream 2.3, compression levels 0 - 6).
-- Port of Draco 1.5.7 kd_tree_attributes_decoder.cc, dynamic_integer_points_kd_tree_decoder.h and the direct /
-- folded_integer bit decoders (Apache-2.0, licenses/draco-LICENSE.txt). Pre-2.3 kd-tree layouts are not supported.
local Reader = require("@rosetta/codecs/model/Draco/Reader")
local Predict = require("@rosetta/codecs/model/Draco/Predict")
@@ -12,7 +9,7 @@ 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
-- bits(n) reads MSB first
type Src = { next: () -> number, bits: (n: number) -> number? }
local function direct(d: D): Src?
@@ -122,7 +119,7 @@ local function msb(n: number): number
return k
end
-- DynamicIntegerPointsKdTreeDecoder::DecodePoints -> points (each a table of `dim` u32 values), or nil
-- DynamicIntegerPointsKdTreeDecoder::DecodePoints
local function decodePoints(d: D, level: number, dim: number, expected: number): ({ { number } }?, string?)
local bitLength = Reader.u32(d)
local numPoints = Reader.u32(d)
@@ -250,7 +247,6 @@ 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"
@@ -293,7 +289,6 @@ function KdTree.decode(d: D, attrs: { any }, numPoints: number): (boolean, strin
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)
@@ -321,7 +316,6 @@ function KdTree.decode(d: D, attrs: { any }, numPoints: number): (boolean, strin
a.signedMin = mins
end
end
-- back to the original values
local fround = Predict.fround
for _, a in attrs do
local vals = a.portable :: { number }
+6 -23
View File
@@ -1,11 +1,7 @@
--!strict
-- codecs/model/Draco/Predict: Draco attribute prediction schemes and their decoding transforms. Schemes: difference
-- (delta), parallelogram, multi-parallelogram, constrained multi-parallelogram, tex coords (deprecated, float) and
-- portable tex coords, geometric normal; transforms: wrap, normal octahedron and canonicalized normal octahedron.
-- Integer semantics follow the C++ (int32 wrap, truncating division, exact 64-bit products where they can pass 2^53;
-- float math rounded to f32 at every step). Ported from Google's Draco 1.5.7
-- (compression/attributes/prediction_schemes/*, compression/attributes/normal_compression_utils.h; Apache-2.0,
-- licenses/draco-LICENSE.txt).
-- codecs/model/Draco/Predict: Draco attribute prediction schemes and their decoding transforms.
-- C++ integer semantics (int32 wrap, truncating division, exact 64-bit products) and f32 rounding at every step.
-- Port of Draco 1.5.7 prediction_schemes/*, normal_compression_utils.h (Apache-2.0, licenses/draco-LICENSE.txt).
local Reader = require("@rosetta/codecs/model/Draco/Reader")
local Connectivity = require("@rosetta/codecs/model/Draco/Connectivity")
local Int64 = require("@rosetta/codecs/model/Draco/Int64")
@@ -87,7 +83,7 @@ local function isqrtProd(a: number, b: number): number
end
local s = math.floor(math.sqrt(a) * math.sqrt(b))
local ph, pl = twoProd(a, b)
local function le(x: number): boolean -- x * x <= a * b
local function le(x: number): boolean
local xh, xl = twoProd(x, x)
if xh ~= ph then
return xh < ph
@@ -103,7 +99,6 @@ local function isqrtProd(a: number, b: number): number
return s
end
-- ------------------------------------------------------------------------------------------------ octahedron toolbox
export type Oct = { bits: number, maxQ: number, maxV: number, center: number, scale: number }
function Predict.octahedron(bits: number): Oct?
@@ -207,8 +202,6 @@ function Predict.octToVector(o: Oct, s: number, t: number): (number, number, num
return fround(x * dd), fround(y * dd), fround(z * dd)
end
-- ------------------------------------------------------------------------------------------------ transforms
-- tr.apply(pred, data, off): data[off + 1 ..] holds the corrections and receives the original values
export type Transform = {
kind: number, -- 1 wrap, 2 octahedron, 3 octahedron canonicalized
positive: boolean, -- corrections are stored without the zig-zag sign mapping
@@ -254,7 +247,6 @@ function Predict.decodeTransform(tr: Transform, d: D): boolean
return true
end
-- the original values from a prediction (pred[1 .. comps]) and the corrections at data[off + 1 ..]
local function apply(tr: Transform, pred: { number }, data: { number }, off: number, comps: number)
if tr.kind == 1 then
local mn, mx, dif = tr.min, tr.max, tr.maxDif
@@ -324,10 +316,7 @@ local function apply(tr: Transform, pred: { number }, data: { number }, off: num
end
Predict.apply = apply
-- ------------------------------------------------------------------------------------------------ schemes
-- the mesh data of a scheme: corner table, value -> corner, vertex -> value
export type MeshData = { ct: CT, e2c: { number }, v2e: { number } }
-- the parent position attribute: pos(point) -> x, y, z (portable integers, or floats before 2.0)
export type Parent = (point: number) -> (number, number, number)
export type Scheme = {
@@ -335,7 +324,6 @@ export type Scheme = {
tr: Transform,
md: MeshData?,
parent: Parent?,
-- per-scheme decoded data
crease: { { boolean } }?,
orient: { boolean }?,
flips: Reader.Bits?,
@@ -346,7 +334,6 @@ export type Scheme = {
local DELTA, PARALLELOGRAM, MULTI, TEXDEP, CONSTRAINED, TEXPORT, GEONORMAL = 0, 1, 2, 3, 4, 5, 6
Predict.GEOMETRIC_NORMAL = GEONORMAL
-- whether the scheme needs the position attribute
function Predict.needsParent(s: Scheme): boolean
return s.md ~= nil and (s.method == TEXDEP or s.method == TEXPORT or s.method == GEONORMAL)
end
@@ -489,9 +476,7 @@ local function swingLeft(ct: CT, c: number): number
return nextC(o)
end
-- the portable tex coord prediction when both neighbours are decoded (MeshPredictionSchemeTexCoordsPortablePredictor
-- ::ComputePredictedValue), in int64 / uint64 arithmetic like the C++. Returns "ok", u, v; "fallback" (degenerate
-- edge: predict from a neighbour); or "fail", message.
-- MeshPredictionSchemeTexCoordsPortablePredictor::ComputePredictedValue in int64 / uint64 like the C++
local function texPortable(nu: number, nv: number, pu: number, pv: number, tx: number, ty: number, tz: number, nx: number, ny: number, nz: number, qx: number, qy: number, qz: number, pop: () -> boolean?): (string, any, any)
local pnx, pny, pnz = qx - nx, qy - ny, qz - nz
local cnx, cny, cnz = tx - nx, ty - ny, tz - nz
@@ -524,7 +509,6 @@ local function texPortable(nu: number, nv: number, pu: number, pv: number, tx: n
end
end
end
-- the general case: int64
local from, add, sub, mul, div = Int64.from, Int64.add, Int64.sub, Int64.mul, Int64.div
local function dot3(ax: number, ay: number, az: number, bx: number, by: number, bz: number): (number, number)
local xh, xl = from(ax)
@@ -636,7 +620,7 @@ local function texPortable(nu: number, nv: number, pu: number, pv: number, tx: n
return "ok", i32(ul), i32(vl)
end
-- ComputeOriginalValues over data (corrections in, values out, in place); points = the entry -> point map
-- ComputeOriginalValues (in place); points = the entry -> point map
function Predict.compute(s: Scheme, data: { number }, numEntries: number, comps: number, points: { number }): (boolean, string?)
local tr = s.tr
local m = s.method
@@ -912,7 +896,6 @@ function Predict.compute(s: Scheme, data: { number }, numEntries: number, comps:
end
return true, nil
end
-- geometric normal
local o = tr.oct
local flips = s.flips
if not o or not flips or comps ~= 2 then
+11 -19
View File
@@ -1,10 +1,7 @@
--!strict
-- codecs/model/Draco/Reader: the Draco decoder buffer (little-endian fields, LEB128 varints, LSB-first bit runs) and
-- its entropy decoders: rANS symbol coding (tagged and raw schemes, DecodeSymbols) and the binary rANS / rABS bit
-- decoder. Ported from Google's Draco 1.5.7 (core/decoder_buffer, core/varint_decoding, compression/entropy/ans.h,
-- rans_symbol_decoder.h, symbol_decoding.cc, compression/bit_coders/rans_bit_decoder.cc; Apache-2.0,
-- licenses/draco-LICENSE.txt). Bitstream versions 1.0 - 2.2 (fixed-width counts before 2.0 / 2.2).
-- A failed read sets the sticky d.bad message and returns 0; callers check d.bad at their boundaries.
-- codecs/model/Draco/Reader: Draco's decoder buffer (LE fields, LEB128, LSB-first bit runs) and rANS / rABS decoders.
-- Port of Draco 1.5.7 core/decoder_buffer, core/varint_decoding, entropy/ans.h, rans_symbol_decoder.h, symbol_decoding.cc
-- and rans_bit_decoder.cc (Apache-2.0, licenses/draco-LICENSE.txt).
local Reader = {}
local readu8, readu16, readu32, readi32, readf32 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readi32, buffer.readf32
@@ -12,13 +9,13 @@ local band, rshift = bit32.band, bit32.rshift
export type D = {
b: buffer,
pos: number, -- absolute offset in b
limit: number, -- absolute end
pos: number,
limit: number,
ver: number, -- major * 256 + minor
bad: string?,
bitMode: boolean,
bitStart: number, -- absolute byte where the bit run starts
bitOff: number, -- bits consumed
bitStart: number,
bitOff: number,
}
function Reader.new(b: buffer, pos: number, limit: number, ver: number): D
@@ -82,7 +79,6 @@ function Reader.i32(d: D): number
return readi32(d.b, p)
end
-- u64 as a number (exact below 2^53; sizes beyond that fail the bounds checks anyway)
function Reader.u64(d: D): number
local p = d.pos
if p + 8 > d.limit then
@@ -132,7 +128,6 @@ function Reader.count(d: D, fixedBefore: number): number
return Reader.varint(d)
end
-- ------------------------------------------------------------------------------------------------ bit runs
-- StartBitDecoding(decodeSize): an optional size (u64 before 2.2, varint64 after), then LSB-first bits up to the end
-- of the buffer; EndBitDecoding moves pos past the whole bytes consumed.
function Reader.startBits(d: D, decodeSize: boolean): number
@@ -179,7 +174,6 @@ function Reader.endBits(d: D)
d.pos = d.bitStart + (d.bitOff + 7) // 8
end
-- ------------------------------------------------------------------------------------------------ rANS symbols
-- the precision for a symbol alphabet of `bitLength`-bit symbols: (3 * bits) / 2 clamped to 12..20
local function precisionBits(bitLength: number): number
local p = (3 * bitLength) // 2
@@ -188,12 +182,12 @@ end
export type Rans = {
b: buffer,
base: number, -- absolute start of the coded bytes
off: number, -- bytes left (read backwards from base + off)
base: number,
off: number,
state: number,
lBase: number,
precBits: number,
lut: { number }, -- rem + 1 -> symbol + 1
lut: { number },
prob: { number },
cum: { number },
numSymbols: number,
@@ -337,7 +331,6 @@ local function ransStart(d: D, r: Rans): boolean
return true
end
-- decodes n symbols into out[first .. first + n - 1] (0-based symbol values)
local function ransRead(r: Rans, n: number, out: { number }, first: number)
local b, base, off, state = r.b, r.base, r.off, r.state
local lBase, bits = r.lBase, r.precBits
@@ -412,7 +405,7 @@ local function decodeRaw(d: D, n: number, out: { number }): boolean
return true
end
-- DecodeSymbols(n, components) -> { u32 } (1-based) | nil (d.bad set)
-- DecodeSymbols
function Reader.symbols(d: D, n: number, comps: number): { number }?
local out = table.create(n, 0)
if n == 0 then
@@ -434,7 +427,6 @@ function Reader.symbols(d: D, n: number, comps: number): { number }?
return if ok then out else nil
end
-- ------------------------------------------------------------------------------------------------ binary rANS
export type Bits = { b: buffer, base: number, off: number, state: number, p0: number, p: number }
-- RAnsBitDecoder::StartDecoding: prob_zero, the size (u32 before 2.2), the bytes
+2 -25
View File
@@ -1,25 +1,7 @@
--!strict
-- codecs/model/Draco: Google Draco geometry decompression (.drc; glTF KHR_draco_mesh_compression; Roblox mesh v6 / v7).
-- 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), 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("@rosetta/codecs/model/Draco")
-- local g, err = Draco.decode(buf, off?, len?, opts?) -- buffer | string
-- g.kind ("mesh" | "points"), g.numPoints, g.numFaces, g.indices (u32 x 3 per face, or nil for points)
-- g.attributes[i] = { name = "POSITION" | "NORMAL" | "COLOR" | "TEX_COORD" | "GENERIC" | ..., uniqueId,
-- components, type = "f32" | "u8" | "i16" | ..., normalized, data (per point, packed) }
-- Draco.attribute(g, uniqueId) -> the attribute with that unique id (glTF's attribute map)
-- g.metadata = { entries = { [name] = buffer }, children = { [name] = Metadata }, attributes = { [uniqueId] = Metadata } }
--
-- Options: maxPoints (default 2^24), maxFaces (default 2^24): larger geometry returns Err.LIMIT.
-- Never throws: bad data returns nil, Err (TRUNCATED / CORRUPT / UNSUPPORTED / LIMIT).
-- Port of Draco 1.5.7's decoder (Apache-2.0, licenses/draco-LICENSE.txt): bitstreams 1.0 - 2.2 meshes, 2.3 kd-tree
-- point clouds; values match draco_decoder bit for bit (f32 rounding at every dequantization step).
local Err = require("@rosetta/core/Err")
local Bytes = require("@rosetta/core/Bytes")
local Reader = require("@rosetta/codecs/model/Draco/Reader")
@@ -55,7 +37,6 @@ export type Geometry = {
}
export type Header = { major: number, minor: number, kind: "mesh" | "points", method: number, flags: number }
-- sniff(buf, off?) -> whether the bytes start with "DRACO"
function Draco.sniff(buf: buffer, off: number?): boolean
local o = off or 0
return buffer.len(buf) >= o + 5 and buffer.readstring(buf, o, 5) == "DRACO"
@@ -77,7 +58,6 @@ local function header(d: Reader.D): (Header?, string?)
return { major = major, minor = minor, kind = if kind == 0 then "points" else "mesh", method = method, flags = flags }, nil
end
-- info(buf, off?, len?) -> the header
function Draco.info(src: buffer | string, off: number?, len: number?): (Header?, Err.Err?)
local buf, o, l, aerr = Bytes.span("draco", src, off, len)
if not buf then
@@ -169,7 +149,6 @@ local WRITE: { [number]: (buffer, number, number) -> () } = {
[11] = buffer.writeu8,
}
-- per point output buffer of an attribute
local function expand(a: Attributes.Attr, numPoints: number): (buffer?, string?)
local size = Attributes.DT_LENGTH[a.dataType]
local comps = a.comps
@@ -324,7 +303,6 @@ local function decode(buf: buffer, o: number, l: number, opts: any): (Geometry?,
return g, nil, nil
end
-- decode(src, off?, len?, opts?) -> Geometry | nil, Err
function Draco.decode(src: buffer | string, off: number?, len: number?, opts: any?): (Geometry?, Err.Err?)
local buf, o, l, aerr = Bytes.span("draco", src, off, len)
if not buf then
@@ -340,7 +318,6 @@ function Draco.decode(src: buffer | string, off: number?, len: number?, opts: an
return g, nil
end
-- attribute(g, uniqueId) -> the attribute with that unique id
function Draco.attribute(g: Geometry, uniqueId: number): Attribute?
for _, a in g.attributes do
if a.uniqueId == uniqueId then