cad formats: comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:51:17 -05:00
1 parent 0bd7b87d2b
commit 8bd1a9327c
4 files changed
+22 -91

No files matched your search

+6 -21
View File
@@ -1,16 +1,7 @@
--!strict
-- cad/step/Brep: the injected B-rep plug-in (Rosetta.plugins.brep; rosetta never requires a kernel). Normalises
-- the plug-in table, calls it under pcall (kernels throw on bad geometry) and turns its tessellated meshes into a
-- Scene. Interpreted on purpose: the plug-in's errors unwind through here.
--
-- The interface (every function may throw; results are checked):
-- read(text: string, opts) -> { bodies = { Body }, stats = { ... }? } STEP text -> B-rep bodies (body.name?,
-- body.color? = Color3 | { r, g, b } | { 1, 2, 3 })
-- write(bodies: { Body }, opts) -> string bodies -> STEP text
-- tessellate(body: Body, opts) -> { positions = { vector | {x, y, z} }, normals = { ... }?, indices = { 1-based
-- triples }, faceOf = { [triangle] = Face (face.color?) }? }
-- or brep-luau's modules as they are: { Step = , StepBrep = , StepWrite = , Tess = } (adapted here: read =
-- StepBrep.read(Step.parse(text)), write = StepWrite.write with options.colors from body.color, tessellate = Tess.body).
-- 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")
@@ -30,7 +21,7 @@ local function rgbOf(c: any): (number?, number?, number?)
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 -- Color3
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
@@ -38,7 +29,6 @@ local function rgbOf(c: any): (number?, number?, number?)
return nil, nil, nil
end
-- plugin(p) -> the normalised interface, or nil + the reason
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 })"
@@ -82,7 +72,6 @@ function Brep.plugin(p: any): (Plugin?, string?)
return nil, "the B-rep plug-in has neither read() nor brep-luau's Step + StepBrep modules"
end
-- find(opts) -> the plug-in from opts.brep or opts.plugins.brep
function Brep.find(opts: any): (Plugin?, Err.Err?)
local p = nil
if type(opts) == "table" then
@@ -98,7 +87,6 @@ function Brep.find(opts: any): (Plugin?, Err.Err?)
return plug, nil
end
-- read(plug, text, opts) -> bodies, result | nil, Err
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
@@ -110,7 +98,6 @@ function Brep.read(plug: Plugin, text: string, opts: any): (({ any })?, any, Err
return res.bodies, res, nil
end
-- write(plug, bodies, opts) -> text | nil, Err
function Brep.write(plug: Plugin, bodies: { any }, opts: any): (string?, Err.Err?)
local w = plug.write
if not w then
@@ -130,7 +117,7 @@ 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 -- vector / Vector3
return (p :: any).X, (p :: any).Y, (p :: any).Z
end
local function linear(c: number): number
@@ -138,8 +125,7 @@ local function linear(c: number): number
return if c <= 0.04045 then c / 12.92 else ((c + 0.055) / 1.055) ^ 2.4
end
-- scene(plug, bodies, opts) -> Scene: one node + mesh per body, one primitive per face colour (materials shared by
-- colour; baseColor = the STEP colour converted from sRGB to linear, extras.color = the colour as written)
-- 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
@@ -196,7 +182,6 @@ function Brep.scene(plug: Plugin, bodies: { any }, opts: any): (Types.Scene?, Er
end
end
local br, bg, bb = rgbOf(type(body) == "table" and body.color or nil)
-- triangles grouped by colour, in first-seen order
local groups: { { mat: number, tris: { number } } } = {}
local byMat: { [number]: { mat: number, tris: { number } } } = {}
local faceOf = if type(mesh.faceOf) == "table" then mesh.faceOf else {}