--!strict -- cad/step/Brep: the injected B-rep plug-in (Rosetta.plugins.brep; never required by rosetta), called under pcall. -- Interface (each may throw): read(text, opts) -> { bodies, stats? }; write(bodies, opts) -> text; -- tessellate(body, opts) -> { positions, normals?, indices (1-based triples), faceOf? }; or brep-luau's modules as-is. local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Brep = {} export type Plugin = { read: (text: string, opts: any) -> any, write: ((bodies: { any }, opts: any) -> string)?, tessellate: ((body: any, opts: any) -> any)?, } local function rgbOf(c: any): (number?, number?, number?) if c == nil then return nil, nil, nil end local ok, r, g, b = pcall(function(): (any, any, any) if type(c) == "table" then return c.r or c.R or c[1], c.g or c.G or c[2], c.b or c.B or c[3] end return (c :: any).R, (c :: any).G, (c :: any).B end) if ok and type(r) == "number" and type(g) == "number" and type(b) == "number" then return r, g, b end return nil, nil, nil end function Brep.plugin(p: any): (Plugin?, string?) if type(p) ~= "table" then return nil, "needs the B-rep plug-in (Rosetta.plugins.brep = { read, write, tessellate }, e.g. brep-luau's { Step, StepBrep, StepWrite, Tess })" end if type(p.read) == "function" then return { read = p.read, write = if type(p.write) == "function" then p.write else nil, tessellate = if type(p.tessellate) == "function" then p.tessellate else nil, }, nil end local S, SB, SW, Tess = p.Step, p.StepBrep, p.StepWrite, p.Tess if type(S) == "table" and type(SB) == "table" and type(S.parse) == "function" and type(SB.read) == "function" then return { read = function(text: string, opts: any): any return SB.read(S.parse(text), opts) end, write = if type(SW) == "table" and type(SW.write) == "function" then function(bodies: { any }, opts: any): string local o = if type(opts) == "table" then table.clone(opts) else {} if o.colors == nil then local colors = {} for i, b in bodies do local r, g, bl = rgbOf(type(b) == "table" and b.color or nil) if r then colors[i] = { r, g, bl } end end o.colors = colors end return SW.write(bodies, o) end else nil, tessellate = if type(Tess) == "table" and type(Tess.body) == "function" then function(body: any, opts: any): any return Tess.body(body, opts) end else nil, }, nil end return nil, "the B-rep plug-in has neither read() nor brep-luau's Step + StepBrep modules" end function Brep.find(opts: any): (Plugin?, Err.Err?) local p = nil if type(opts) == "table" then p = opts.brep if p == nil and type(opts.plugins) == "table" then p = opts.plugins.brep end end local plug, why = Brep.plugin(p) if not plug then return nil, Err.new(Err.UNSUPPORTED, why :: string) end return plug, nil end function Brep.read(plug: Plugin, text: string, opts: any): (({ any })?, any, Err.Err?) local ok, res = pcall(plug.read, text, opts) if not ok then return nil, nil, Err.new(Err.CORRUPT, "the B-rep plug-in failed to read: " .. tostring(res)) end if type(res) ~= "table" or type(res.bodies) ~= "table" then return nil, nil, Err.new(Err.CORRUPT, "the B-rep plug-in's read returned no bodies") end return res.bodies, res, nil end function Brep.write(plug: Plugin, bodies: { any }, opts: any): (string?, Err.Err?) local w = plug.write if not w then return nil, Err.new(Err.UNSUPPORTED, "the B-rep plug-in has no write()") end local ok, res = pcall(w, bodies, opts) if not ok then return nil, Err.new(Err.ARG, "the B-rep plug-in failed to write: " .. tostring(res)) end if type(res) ~= "string" then return nil, Err.new(Err.ARG, "the B-rep plug-in's write returned no text") end return res, nil end local function xyz(p: any): (number, number, number) if type(p) == "table" then return tonumber(p.x or p.X or p[1]) or 0, tonumber(p.y or p.Y or p[2]) or 0, tonumber(p.z or p.Z or p[3]) or 0 end return (p :: any).X, (p :: any).Y, (p :: any).Z end local function linear(c: number): number c = math.clamp(c, 0, 1) return if c <= 0.04045 then c / 12.92 else ((c + 0.055) / 1.055) ^ 2.4 end -- baseColor = the STEP colour converted from sRGB to linear; extras.color = the colour as written function Brep.scene(plug: Plugin, bodies: { any }, opts: any): (Types.Scene?, Err.Err?) local tess = plug.tessellate if not tess then return nil, Err.new(Err.UNSUPPORTED, "the B-rep plug-in has no tessellate()") end local scene = Types.Scene.new() scene.meta.up = "z" scene.meta.unit = 0.001 local tessOpts = if type(opts) == "table" and type(opts.tess) == "table" then opts.tess else { chord = if type(opts) == "table" and type(opts.chord) == "number" then opts.chord else 0.05 } local matByKey: { [string]: number } = {} local function material(r: number?, g: number?, b: number?): number local key = if r then string.format("%.6f,%.6f,%.6f", r, g :: number, b :: number) else "default" local m = matByKey[key] if m then return m end local mat = Types.Scene.material(if r then "step_" .. #scene.materials + 1 else "default") if r then mat.baseColor = { linear(r), linear(g :: number), linear(b :: number), 1 } mat.extras = { color = { r, g, b } } else mat.baseColor = { 0.6, 0.6, 0.6, 1 } end mat.metallic, mat.roughness = 0, 0.5 table.insert(scene.materials, mat) matByKey[key] = #scene.materials return #scene.materials end for bi, body in bodies do local ok, mesh = pcall(tess, body, tessOpts) if not ok then return nil, Err.new(Err.CORRUPT, "the B-rep plug-in failed to tessellate body " .. bi .. ": " .. tostring(mesh)) end if type(mesh) ~= "table" or type(mesh.positions) ~= "table" or type(mesh.indices) ~= "table" then return nil, Err.new(Err.CORRUPT, "the B-rep plug-in's tessellate returned no mesh") end local cok, prims = pcall(function(): { Types.Primitive } local P, N, I = mesh.positions, mesh.normals, mesh.indices local nv = #P local pos = Types.Scene.accessor(nil, "f32", 3, nv) local nrm = if type(N) == "table" and #N == nv then Types.Scene.accessor(nil, "f32", 3, nv) else nil for i = 1, nv do local x, y, z = xyz(P[i]) buffer.writef32(pos.data, (i - 1) * 12, x) buffer.writef32(pos.data, (i - 1) * 12 + 4, y) buffer.writef32(pos.data, (i - 1) * 12 + 8, z) if nrm then local a, b, c = xyz(N[i]) buffer.writef32(nrm.data, (i - 1) * 12, a) buffer.writef32(nrm.data, (i - 1) * 12 + 4, b) buffer.writef32(nrm.data, (i - 1) * 12 + 8, c) end end local br, bg, bb = rgbOf(type(body) == "table" and body.color or nil) local groups: { { mat: number, tris: { number } } } = {} local byMat: { [number]: { mat: number, tris: { number } } } = {} local faceOf = if type(mesh.faceOf) == "table" then mesh.faceOf else {} local nt = #I // 3 for t = 1, nt do local f = faceOf[t] local r, g, b = rgbOf(type(f) == "table" and f.color or nil) if not r then r, g, b = br, bg, bb end local m = material(r, g, b) local grp = byMat[m] if not grp then grp = { mat = m, tris = {} } byMat[m] = grp table.insert(groups, grp) end table.insert(grp.tris, t) end local out: { Types.Primitive } = {} for _, grp in groups do local idx = Types.Scene.accessor(nil, "u32", 1, #grp.tris * 3) for k, t in grp.tris do for c = 0, 2 do local v = (tonumber(I[(t - 1) * 3 + c + 1]) or 1) - 1 buffer.writeu32(idx.data, ((k - 1) * 3 + c) * 4, math.clamp(v, 0, math.max(nv - 1, 0))) end end local attrs: { [string]: Types.Accessor } = { POSITION = pos } if nrm then attrs.NORMAL = nrm end table.insert(out, { attributes = attrs, indices = idx, mode = "triangles" :: any, material = grp.mat }) end return out end) if not cok then return nil, Err.new(Err.CORRUPT, "bad tessellation of body " .. bi .. ": " .. tostring(prims)) end local name = if type(body) == "table" and type(body.name) == "string" then body.name else "body_" .. bi table.insert(scene.meshes, { name = name, primitives = prims }) table.insert(scene.nodes, { name = name, children = {}, mesh = #scene.meshes }) table.insert(scene.roots, #scene.nodes) end scene.meta.source = { format = "step", tessellated = true, bodies = #bodies } return scene, nil end return Brep