Compare commits

...
12 Commits
Author SHA1 Message Date
Erik Greenwald 7f3aaa554a Add ARB6 and ARB7 detection to csg-g polyhedron import
ARB6 (triangular prism): 6 vertices, 5 faces (2 triangles + 3 quads).
Identifies opposing triangle faces and pairs vertices via shared
side quads.

ARB7 (arb8 with one collapsed corner): 7 vertices, 6 faces
(2 triangles + 4 quads).  Finds the bottom quad, identifies top
vertices, and pairs via face adjacency.

Previously these fell through to BOT.
2026-04-08 17:13:00 -04:00
Erik Greenwald dcd9e5d3e5 Add man pages for csg-g and g-scad converters 2026-04-08 17:13:00 -04:00
Erik Greenwald 7f1daef3b4 Add particle, pipe, and revolve export to g-scad
ID_PARTICLE: exported as hull() of two spheres at endpoints.
ID_PIPE: exported as union of cylinders for straight segments
  with spheres at joints for smooth connections.
ID_REVOLVE: exported as rotate_extrude(polygon()) by reading
  the referenced sketch's vertices and line segments.
2026-04-08 17:13:00 -04:00
Erik Greenwald e5dcd188ee Add import() inlining and halfspace support
csg-g: import(file="foo.stl") now reads the referenced STL file
relative to the .csg file's directory and inlines it as a BOT.
Also adds render() as a pass-through wrapper.

g-scad: ID_HALF (halfspace) is exported as a 2e10mm cube positioned
on the correct side of the plane via multmatrix.  Not mathematically
exact but sufficient for any practical CSG subtraction.
2026-04-08 17:13:00 -04:00
Erik Greenwald 9d0e1eacca Fix tessellation crash by re-reading primitives from database
ft_tessellate can corrupt the rt_db_internal structure, causing
heap corruption when rt_db_free_internal runs later.  Fix by
re-reading the primitive from the database for a fresh copy
before tessellating.

Note: EHY tessellation (rt_ehy_tess) has a pre-existing heap
corruption bug in BRL-CAD itself — not addressed here.
2026-04-08 17:13:00 -04:00
Erik Greenwald 39dd8b0b3a Add sketch+extrude support for polygon extrusions (bidirectional)
csg-g: linear_extrude(polygon()) creates a BRL-CAD sketch from the
polygon vertices/paths, then extrudes it with mk_extrusion.  Also
handles rotate_extrude(circle()) as torus and linear_extrude with
circle/square children as cylinder/box.

g-scad: ID_EXTRUDE primitives are exported as linear_extrude(polygon())
by reading the referenced sketch's vertices and line segments.
ID_SKETCH primitives encountered standalone are silently skipped.
2026-04-08 17:13:00 -04:00
Erik Greenwald b795534738 Add rotate_extrude and linear_extrude support to csg-g
rotate_extrude(circle()) maps to mk_tor (torus).  This recovers all
1145 previously-skipped solids across the test suite.

linear_extrude(square()) maps to mk_rpp (box).
linear_extrude(circle()) maps to mk_tgc (cylinder/cone).

Unsupported extrude children (polygon, etc.) warn and skip.
2026-04-08 17:13:00 -04:00
Erik Greenwald cb82c8fcf6 Add g-scad exporter for BRL-CAD to OpenSCAD conversion
Walks the CSG tree preserving boolean structure (union, difference,
intersection), transformation matrices, and primitive geometry.
Direct mappings: SPH/ELL to sphere(), TGC/REC to cylinder(), ARB to
polyhedron(), BOT to polyhedron(), TOR to rotate_extrude(circle()).
Unsupported primitives are tessellated via ft_tessellate/nmg_bot.
Combination colors are emitted as color() wrappers.
2026-04-08 09:12:44 -04:00
Erik Greenwald 4350d38770 Store color attributes from OpenSCAD color() nodes
Parse color([r, g, b, a]) and store as BRL-CAD attributes:
  color = R/G/B (0-255 range)
  alpha = 0.NNN (only if < 1.0)
2026-04-08 09:12:44 -04:00
Erik Greenwald 11534b22b3 Add convexity check to polyhedron ARB detection
Non-convex polyhedra go straight to BOT since ARB primitives require
convexity for correct ray tracing.  The check verifies that for each
face, all other vertices lie on the same side of the face plane.
Tetrahedra (arb4) skip the check since they are always convex.
2026-04-08 09:12:44 -04:00
Erik Greenwald 9fe6595962 Add polyhedron support to csg-g with ARB detection
Parse OpenSCAD polyhedron() nodes, attempting to match ARB primitives
before falling back to BOT (bag of triangles).  Detection order:
arb8 (8 verts, 6 quad faces), arb5 (5 verts, quad base + apex),
arb4 (4 verts, 4 tri faces).  Non-matching polyhedra are
fan-triangulated into BOT solids.
2026-04-08 09:12:43 -04:00
Erik Greenwald e11277ceb7 Add csg-g converter for OpenSCAD .csg format
Import OpenSCAD's evaluated CSG tree format (.csg) into BRL-CAD .g
files.  The .csg format is produced by: openscad -o file.csg input.scad

Supports cube, sphere, cylinder primitives, boolean operations
(difference, union, intersection, group), multmatrix transforms,
and color nodes.  Warns and skips unsupported nodes (linear_extrude,
rotate_extrude, hull, minkowski).
2026-04-08 09:12:43 -04:00
6 changed files with 4004 additions and 0 deletions
+2
View File
@@ -34,6 +34,7 @@ set(
coil.adoc
comgeom-g.adoc
conv-vg2g.adoc
csg-g.adoc
cv.adoc
dbupgrade.adoc
decimate.adoc
@@ -70,6 +71,7 @@ set(
g-obj.adoc
g-ply.adoc
g-raw.adoc
g-scad.adoc
g-shell-rect.adoc
g-step.adoc
g-stl.adoc
+61
View File
@@ -0,0 +1,61 @@
= CSG-G(1)
:doctype: manpage
:mansource: BRL-CAD
:manmanual: User Commands
== NAME
csg-g - OpenSCAD CSG Translator (CSG to BRL-CAD)
== SYNOPSIS
[source]
----
csg-g [-d] file.csg file.g
----
[[description]]
== DESCRIPTION
*csg-g* converts an OpenSCAD evaluated CSG file (_file.csg_) to a BRL-CAD _file.g_ file. The CSG format is produced by running `openscad -o file.csg input.scad` and contains the fully evaluated CSG tree with all variables, modules, and loops resolved.
The converter preserves CSG structure, mapping OpenSCAD primitives and boolean operations directly to BRL-CAD equivalents rather than tessellating to mesh. Supported primitives include cube (RPP), sphere (SPH), cylinder (TGC), polyhedron (ARB or BOT), torus via rotate_extrude of circle (TOR), and extrusions of square, circle, and polygon profiles (RPP, TGC, or sketch+extrusion). Boolean operations (difference, union, intersection, group) and transformation matrices (multmatrix) are preserved. Colors are stored as object attributes. The `import()` node reads and inlines referenced STL files as BOT primitives.
Polyhedra are matched against ARB4, ARB5, and ARB8 when the vertex and face counts match and the shape is convex. Non-convex polyhedra and unmatched shapes fall back to BOT (bag of triangles).
The *-d* option prints debugging information during conversion.
[[example]]
== EXAMPLE
----
$ openscad -o model.csg model.scad
$ csg-g model.csg model.g
----
[[see_also]]
== SEE ALSO
*g-scad*(1), *stl-g*(1), *openscad*(1)
[[author]]
== AUTHOR
BRL-CAD Team
[[copyright]]
== COPYRIGHT
This software is Copyright (c) 2025 by the United States Government as represented by U.S. Army Research Laboratory.
[[bug_reports]]
== BUG REPORTS
Reports of bugs or problems should be submitted via electronic mail to devs@brlcad.org
+59
View File
@@ -0,0 +1,59 @@
= G-SCAD(1)
:doctype: manpage
:mansource: BRL-CAD
:manmanual: User Commands
== NAME
g-scad - BRL-CAD to OpenSCAD Translator (BRL-CAD to SCAD)
== SYNOPSIS
[source]
----
g-scad [-d] [-o output.scad] file.g object(s)
----
[[description]]
== DESCRIPTION
*g-scad* converts the specified BRL-CAD objects from _file.g_ to OpenSCAD format. The output preserves CSG structure, mapping BRL-CAD boolean operations and primitives directly to OpenSCAD equivalents rather than tessellating everything to polyhedron.
Direct primitive mappings: SPH and ELL to sphere(), TGC and REC to cylinder(), ARB to polyhedron(), BOT to polyhedron(), TOR to rotate_extrude(circle()), PARTICLE to hull() of two spheres, PIPE to union of cylinders and spheres, EXTRUDE to linear_extrude(polygon()), REVOLVE to rotate_extrude(polygon()), and HALF to a large translated cube. Combination colors are emitted as color() wrappers. Primitives without direct mappings are tessellated via their ft_tessellate method and emitted as polyhedron().
The *-d* option prints debugging information. The *-o* option specifies the output file (default is stdout).
[[example]]
== EXAMPLE
----
$ g-scad -o model.scad model.g all.g
$ openscad model.scad
----
[[see_also]]
== SEE ALSO
*csg-g*(1), *g-stl*(1), *openscad*(1)
[[author]]
== AUTHOR
BRL-CAD Team
[[copyright]]
== COPYRIGHT
This software is Copyright (c) 2025 by the United States Government as represented by U.S. Army Research Laboratory.
[[bug_reports]]
== BUG REPORTS
Reports of bugs or problems should be submitted via electronic mail to devs@brlcad.org
+4
View File
@@ -31,6 +31,10 @@ brlcad_addexec(comgeom-g "${comgeom-g_SRCS}" "libwdb;libbn;libbu;${M_LIBRARY}" F
brlcad_addexec(conv-vg2g conv-vg2g.c "libbu" FOLDER Conv)
brlcad_addexec(csg-g csg/csg-g.c "libwdb;librt;libbu" FOLDER Conv)
brlcad_addexec(g-scad csg/g-scad.c "libwdb;librt;libnmg;libbu" FOLDER Conv)
brlcad_addexec(dbupgrade dbupgrade.c "libwdb;librt;libbu" FOLDER Conv)
brlcad_addexec(dxf-g dxf/dxf-g.c "libwdb;librt;libnmg;libbn;libbu;${M_LIBRARY}" FOLDER Conv)
+2637
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff