model formats (mca..x3d): comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:54:32 -05:00
1 parent 0bd7b87d2b
commit b952c10222
49 files changed
+310 -936

No files matched your search

+2 -15
View File
@@ -1,11 +1,8 @@
--!strict
--!native
--!optimize 2
-- model/ply/Binary: PLY element data loops. Value read / write by type code (1 i8, 2 u8, 3 i16, 4 u16, 5 i32, 6 u32,
-- 7 f32, 8 f64) in either byte order, the column copy of fixed-size rows (scalar properties only) and the row walk of
-- rows with lists (a scan for the list totals, then the fill). The caller (init) sizes every destination from the
-- header counts and the scan, and checks the rows fit in the data first, so every index here is in range; a bad list
-- count or an out-of-range vertex index returns false / -1 with a message. Nothing here throws.
-- model/ply/Binary: PLY element data loops; type codes 1 i8, 2 u8, 3 i16, 4 u16, 5 i32, 6 u32, 7 f32, 8 f64, either
-- byte order. Never throws: the caller bounds every row first.
local readu8, readi8, readu16, readi16, readu32, readi32 = buffer.readu8, buffer.readi8, buffer.readu16, buffer.readi16, buffer.readu32, buffer.readi32
local readf32, readf64 = buffer.readf32, buffer.readf64
local writeu8, writei8, writeu16, writei16, writeu32, writei32 = buffer.writeu8, buffer.writei8, buffer.writeu16, buffer.writei16, buffer.writeu32, buffer.writei32
@@ -22,7 +19,6 @@ local function swap16(v: number): number
return rshift(v, 8) + band(v, 255) * 256
end
-- rd(b, q, code, be) -> the value at q
local function rd(b: buffer, q: number, code: number, be: boolean): number
if not be then
if code == 7 then
@@ -63,7 +59,6 @@ local function rd(b: buffer, q: number, code: number, be: boolean): number
end
Binary.rd = rd
-- wr(d, o, code, v): v as code, little endian
local function wr(d: buffer, o: number, code: number, v: number)
if code == 7 then
writef32(d, o, v)
@@ -85,7 +80,6 @@ local function wr(d: buffer, o: number, code: number, v: number)
end
Binary.wr = wr
-- put(d, o, code, v, be): v as code in either byte order (the writer)
function Binary.put(d: buffer, o: number, code: number, v: number, be: boolean)
if not be or code <= 2 then
wr(d, o, code, v)
@@ -102,7 +96,6 @@ function Binary.put(d: buffer, o: number, code: number, v: number, be: boolean)
end
end
-- fixed(b, p, n, rowSize, np, srcOff, code, be, dst, dOff, dStride, dCode): column copies of n fixed-size rows at p
function Binary.fixed(b: buffer, p: number, n: number, rowSize: number, np: number, srcOff: { number }, code: { number }, be: boolean, dst: { buffer }, dOff: { number }, dStride: { number }, dCode: { number })
for k = 1, np do
local sc, dc, d = code[k], dCode[k], dst[k]
@@ -135,8 +128,6 @@ function Binary.fixed(b: buffer, p: number, n: number, rowSize: number, np: numb
end
end
-- scan(b, p, limit, n, np, code, isList, countCode, be) -> the end offset and the item total of each list property,
-- or -1, message (truncated rows, a negative / fractional count)
function Binary.scan(b: buffer, p: number, limit: number, n: number, np: number, code: { number }, isList: { boolean }, countCode: { number }, be: boolean): (number, any)
local totals = table.create(np, 0)
local q = p
@@ -168,10 +159,6 @@ function Binary.scan(b: buffer, p: number, limit: number, n: number, np: number,
return q, totals
end
-- rows(b, p, n, np, code, isList, countCode, be, dst, dOff, dStride, dCode, sizes, bound) -> true | false, message.
-- Scalars go to dst[k] at dOff + row * dStride as dCode; lists write their count to sizes[k] (u32 per row) and their
-- items one after the other into dst[k] as dCode; bound[k] >= 0 checks items are integers in 0 .. bound - 1. The rows
-- must have passed scan() with the same arguments.
function Binary.rows(b: buffer, p: number, n: number, np: number, code: { number }, isList: { boolean }, countCode: { number }, be: boolean, dst: { buffer }, dOff: { number }, dStride: { number }, dCode: { number }, sizes: { buffer }, bound: { number }): (boolean, string?)
local at = table.create(np, 0)
local q = p
+5 -50
View File
@@ -1,24 +1,7 @@
--!strict
-- model/ply: PLY (Stanford polygon file format, Greg Turk 1994), ascii / binary_little_endian / binary_big_endian,
-- read + write. Written from the format description (paulbourke.net/dataformats/ply, the rply / plyfile behaviour).
-- Decode: the vertex element maps x y z -> POSITION f32 x3, nx ny nz -> NORMAL f32 x3, red green blue [alpha] ->
-- COLOR_0 (uchar: unorm8 x4, ushort: unorm16 x4, float / double: f32 x4; alpha full when absent), u v / s t / texture_u
-- texture_v / texture_s texture_t -> TEXCOORD_0 f32 x2, every other property -> "_" .. name in its own type (char ->
-- i8, uchar -> u8, ..., double -> f64). Doubles in x / y / z / normals / texcoords become f32. The face element's
-- vertex_indices / vertex_index list gives the faces: triangles stay triangles (u32 indices), n-gons are
-- fan-triangulated (opts.triangulate, default true: right for convex faces; faces of fewer than 3 vertices are
-- dropped) or kept as mode "polygons" (prim.faces = vertex count per face). No faces: a point cloud (mode "points",
-- no indices). Other face properties: prim.extras.faceAttributes[name] (one value per file face, its own type), face
-- lists: prim.extras.faceLists[name] = { countType, itemType, sizes (u32 accessor), items (accessor) }; after
-- triangulating n-gons prim.extras.faceOfTriangle (u32, 0-based) maps triangles to file faces. Other elements (edge,
-- material, ...) are kept raw in scene.meta.ply.elements (in their types) and written back, as are comments
-- (meta.ply.comments), obj_info lines (meta.ply.objInfo), the element order and the vertex / face property order and
-- types. meta.source = { format, encoding, vertices, faces }.
-- Encode: every primitive in world space (node transforms applied) into one vertex list: POSITION, NORMAL, COLOR_0,
-- TEXCOORD_0 and "_" attributes (under their name, in their type; several components: name_0, name_1, ...), faces
-- (triangles and polygons natively as `property list uchar int vertex_indices`, a wider count / uint index type when
-- needed), point clouds without a face element; lines are skipped. Floats print as %.9g (f32-exact), doubles %.17g.
-- Not supported: list properties in the vertex element (UNSUPPORTED).
-- model/ply: Stanford PLY (Greg Turk 1994; paulbourke.net/dataformats/ply, rply / plyfile behaviour), ascii and
-- binary either endian, read + write. Supports: every property type, faces (fan-triangulated or "polygons"), point
-- clouds; other elements, comments, obj_info and property order round-trip. Not supported: vertex list properties.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -39,13 +22,13 @@ local Ply = {
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
triangulate: boolean?, -- fan-triangulate n-gons (default true); false keeps them as mode "polygons"
triangulate: boolean?, -- default true
maxVertices: number?, -- default 2^24
maxFaces: number?, -- faces, and rows of any other element (default 2^25)
}
export type EncodeOptions = {
format: ("binary_little_endian" | "binary_big_endian" | "ascii")?, -- default "binary_little_endian"
comments: { string }?, -- extra comment lines (after those in scene.meta.ply.comments)
comments: { string }?,
}
type Prop = { name: string, ply: string, code: number, list: boolean, countPly: string?, countCode: number? }
@@ -115,7 +98,6 @@ local function parseHeader(r: Bytes.Reader): (Header?, Err.Err?)
if first then
first = false -- "ply"
elseif kw == nil then
-- blank line
elseif kw == "end_header" then
h.dataStart = base + at - 1
break
@@ -175,7 +157,6 @@ local function parseHeader(r: Bytes.Reader): (Header?, Err.Err?)
return h, nil
end
-- the reading plan of one element: per property, where its values go
type Plan = {
code: { number },
isList: { boolean },
@@ -213,7 +194,6 @@ local function newPlan(e: Elem): Plan
return pl
end
-- unset scalar destinations get their own native buffer
local function ownBuffers(e: Elem, pl: Plan)
for k, p in e.props do
if not p.list and pl.dst[k] == nil then
@@ -222,7 +202,6 @@ local function ownBuffers(e: Elem, pl: Plan)
end
end
-- read one element's rows (binary) into the plan; returns the next offset or nil, Err
local function readBinary(r: Bytes.Reader, p: number, e: Elem, pl: Plan, be: boolean): (number?, Err.Err?)
local b, limit, n, np = r.buf, r.limit, e.count, #e.props
local hasList, rowSize = false, 0
@@ -267,7 +246,6 @@ local function readBinary(r: Bytes.Reader, p: number, e: Elem, pl: Plan, be: boo
return q, nil
end
-- a token as a value of type code, or nil
local function value(tok: string?, code: number): number?
if tok == nil then
return nil
@@ -283,7 +261,6 @@ local function value(tok: string?, code: number): number?
return v
end
-- read one element's rows (ascii) from the token stream
local function readAscii(nextTok: () -> string?, e: Elem, pl: Plan): Err.Err?
ownBuffers(e, pl)
local n = e.count
@@ -352,7 +329,6 @@ local function propIndex(e: Elem, name: string): number?
return nil
end
-- vertex element -> attributes: sets the plan's destinations, returns the attribute table
local function planVertex(e: Elem, pl: Plan): ({ [string]: Types.Accessor }?, Err.Err?)
local nv = e.count
local attrs: { [string]: Types.Accessor } = {}
@@ -362,7 +338,6 @@ local function planVertex(e: Elem, pl: Plan): ({ [string]: Types.Accessor }?, Er
return nil, Err.new(Err.UNSUPPORTED, "list property " .. p.name .. " in the vertex element")
end
end
-- group(attr, names, required, ok, comps, fill): the first `required` names must exist, the others are optional
local function group(attr: string, names: { string }, required: number, ok: (codes: { number }) -> number?, comps: number, fillValue: number?)
local ks: { [number]: number } = {}
local codes = {}
@@ -408,7 +383,6 @@ local function planVertex(e: Elem, pl: Plan): ({ [string]: Types.Accessor }?, Er
end
group("POSITION", { "x", "y", "z" }, 3, anyNumber, 3, nil)
group("NORMAL", { "nx", "ny", "nz" }, 3, floats, 3, nil)
-- colours: one type class for all channels
do
local rk, gk, bk, ak = propIndex(e, "red"), propIndex(e, "green"), propIndex(e, "blue"), propIndex(e, "alpha")
if rk and gk and bk then
@@ -447,7 +421,6 @@ local function planVertex(e: Elem, pl: Plan): ({ [string]: Types.Accessor }?, Er
return attrs, nil
end
-- the fan triangulation of faces with sizes / corners; returns indices (u32), triangle count, faceOfTriangle
local function fan(sizes: buffer, corners: buffer, nf: number): (buffer, number, buffer)
local nt = 0
for f = 0, nf - 1 do
@@ -513,7 +486,6 @@ function Ply.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
end
if ascii then
local body = buffer.readstring(r.buf, h.dataStart, r.limit - h.dataStart)
-- each value takes at least 2 bytes (a digit and a separator): bound the counts before allocating
local need = 0
for _, e in h.elems do
need += e.count * #e.props
@@ -527,7 +499,6 @@ function Ply.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
local attrs: { [string]: Types.Accessor } = {}
local p = h.dataStart
if not ascii then
-- every row takes at least its scalar sizes and list counts: bound the counts before allocating
local need = 0
for _, e in h.elems do
local row = 0
@@ -566,7 +537,6 @@ function Ply.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
p = q
end
end
-- the primitive
local prim: Types.Primitive = { attributes = attrs, mode = "points" }
local nf = 0
local extras: { [string]: any } = {}
@@ -630,7 +600,6 @@ function Ply.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
if next(extras) then
prim.extras = extras
end
-- raw elements, element order, property order
local elements: { any } = {}
local order: { string } = {}
for i, e in h.elems do
@@ -662,7 +631,6 @@ function Ply.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
end
-- ------------------------------------------------------------------------------------------------ encode
-- a column to write: ply type code + spelling, the values (one per row, or per row * stride at offset)
type Column = { name: string, ply: string, code: number, values: { number }, stride: number, offset: number }
type ListColumn = { name: string, ply: string, code: number, countPly: string, countCode: number, sizes: { number }, items: { number } }
@@ -670,7 +638,6 @@ local function keep(name: string): boolean
return name == "NORMAL" or name == "COLOR_0" or name == "TEXCOORD_0" or string.sub(name, 1, 1) == "_"
end
-- the spelling `want` from the source file when it means the same type as code, else the classic name
local function spell(want: string?, code: number): string
if want and CODES[want] == code then
return want
@@ -689,7 +656,6 @@ local function toInt(v: number, code: number): number
return v
end
-- write rows of columns (+ list columns, in `order`) for n rows: ascii lines into out, binary into the writer
type Row = { kind: "s", col: Column } | { kind: "l", col: ListColumn }
local function rowsAscii(out: { string }, n: number, cols: { Row })
@@ -762,7 +728,6 @@ local function rowsBinary(w: Bytes.Writer, n: number, cols: { Row }, be: boolean
w.pos = o
end
-- numbers of a raw element column (buffer of a ply type)
local function bufNumbers(b: any, code: number, count: number): { number }?
if type(b) ~= "buffer" or buffer.len(b) < count * SIZE[code] then
return nil
@@ -774,7 +739,6 @@ local function bufNumbers(b: any, code: number, count: number): { number }?
return out
end
-- the list count type for a maximum count
local function countCodeFor(maxCount: number): number
return if maxCount <= 255 then 2 elseif maxCount <= 65535 then 4 else 6
end
@@ -791,7 +755,6 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
end
local meta: any = if type(scene) == "table" and type(scene.meta) == "table" and type(scene.meta.ply) == "table" then scene.meta.ply else {}
local nv = m.count
-- source property spellings / order
local srcType: { [string]: string } = {}
local srcOrder: { [string]: number } = {}
if type(meta.vertexProperties) == "table" then
@@ -822,7 +785,6 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
if C then
local code = if C.raw and C.type == "unorm8" then 2 elseif C.raw and C.type == "unorm16" then 4 else 7
if C.raw and code == 7 then
-- another normalized type: as floats
local vals = table.create(#C.values)
local scale = if C.type == "snorm8" then 127 else 32767
for i, v in C.values do
@@ -888,7 +850,6 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
end
end
end
-- the source order first (stable for the rest)
local rank: { [Column]: number } = {}
for i, c in cols do
rank[c] = (srcOrder[c.name] or (1000000 + i))
@@ -896,7 +857,6 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
table.sort(cols, function(a: Column, b: Column): boolean
return rank[a] < rank[b]
end)
-- faces
local faceRows: { Row }? = nil
local nf = 0
if m.sizes then
@@ -929,7 +889,6 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
local rows: { Row } = {
{ kind = "l", col = { name = idxName, ply = spell(idxType, ic), code = ic, countPly = spell(idxCount, cc), countCode = cc, sizes = sizes, items = m.corners } },
}
-- per-face data of a single primitive
if #m.parts == 1 then
local ex = m.parts[1].prim.extras
local fot: { number }? = nil
@@ -984,7 +943,6 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
end
end
end
-- the source order, then the rest by name
local placed: { [string]: boolean } = { [idxName] = true }
local ordered: { Row } = {}
for _, fp in fprops do
@@ -1014,7 +972,6 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
end
faceRows = rows
end
-- raw elements
type Raw = { name: string, count: number, rows: { Row } }
local raws: { [string]: Raw } = {}
local rawOrder: { string } = {}
@@ -1057,7 +1014,6 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
end
end
end
-- element order: the source's, then vertex, face, the rest
local order: { string } = {}
local seen: { [string]: boolean } = {}
local function want(name: string)
@@ -1081,7 +1037,6 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
for _, name in rawOrder do
want(name)
end
-- header
local vrows: { Row } = {}
for _, c in cols do
table.insert(vrows, { kind = "s" :: "s", col = c })