Skip to content

armgit / forma

Public Imported from GitHub
0

A 1.2 MB pure-Rust rewrite of headless Blender built for AI agents — 3D B-rep modeling engine, CLI, and MCP server

feat: add axis to pattern_radial, document array/radial in SPEC_PIPE, raise seg/array/remesh/render caps

wrong-nebula159c078a7f9d
diff --git a/SKILL.md b/SKILL.md
index 6b29cef..d7603b7 100644
--- a/SKILL.md
+++ b/SKILL.md
@@ -82,7 +82,7 @@ EOF
- `shell(t, open_top=true)` — hollow solid with wall thickness `t`.
- `subsurf(levels=1, crease=false)` — Catmull-Clark subdivision (`crease=true` preserves sharp rims).
- `extrude(faces, d)` | `inset(faces, amt, depth=0)` — face selectors: `top`, `bottom`, `sides`, `all`.
- - `mirror(x|y|z)` | `pattern_linear(n, step=(dx,dy,dz))` | `pattern_radial(n, r=)` | `decimate(ratio=0.5, faces=)` (QEM LOD reduction preserving watertightness)
+ - `mirror(x|y|z)` | `pattern_linear(n, step=(dx,dy,dz))` / `array(n, step=)` | `pattern_radial(n, r=, axis=z)` / `radial(n, r=, axis=z)` | `decimate(ratio=0.5, faces=)` (QEM LOD reduction preserving watertightness)
- **Transforms & Relational Placement:**
- `move(x, y, z)` | `rot(rx, ry, rz)` (degrees) | `scale(sx, sy, sz)`
- `on(base)` (rests cleanly on top of `base`) | `align(x|y|z, min|center|max, val)` | `align_z0(z)`
diff --git a/crates/forma-core/src/boolean.rs b/crates/forma-core/src/boolean.rs
index 47596f7..482bb65 100644
--- a/crates/forma-core/src/boolean.rs
+++ b/crates/forma-core/src/boolean.rs
@@ -1340,7 +1340,7 @@ fn eliminate_t_junctions(mesh: &mut PolyMesh, seg_tol: f64) {
let seg_tol_sq = seg_tol * seg_tol;
let end_tol_sq = (seg_tol * 0.25).max(1e-6).powi(2);
let pad = seg_tol.max(1e-4);
- let max_faces = (mesh.faces.len() * 4 + 2048).min(50_000);
+ let max_faces = (mesh.faces.len() * 4 + 2048).min(250_000);
for _pass in 0..8 {
if mesh.faces.len() >= max_faces {
return;
diff --git a/crates/forma-core/src/ops.rs b/crates/forma-core/src/ops.rs
index b7dcd54..92d1c9f 100644
--- a/crates/forma-core/src/ops.rs
+++ b/crates/forma-core/src/ops.rs
@@ -952,6 +952,7 @@ pub fn mirror_mesh(mesh: &PolyMesh, axis: char, union_with_orig: bool, op_id: u3
/// Repeat a mesh linearly (defaulting to union for overlapping instances; pairwise-disjoint
/// instances use an O(1) AABB fast-path with zero CSG overhead).
+/// Allows up to 256 total instances while bounding overlapping CSG unions at 64.
pub fn array_linear(
mesh: &PolyMesh,
count: usize,
@@ -962,18 +963,26 @@ pub fn array_linear(
if count == 0 {
return Err("array count must be >= 1".into());
}
- let max_cnt = if union_all { 32 } else { 64 };
- if count > max_cnt {
- return Err(format!("array count {count} exceeds limit ({max_cnt})"));
+ const MAX_TOTAL: usize = 256;
+ const MAX_CSG_UNIONS: usize = 64;
+ if count > MAX_TOTAL {
+ return Err(format!("array count {count} exceeds limit ({MAX_TOTAL})"));
}
let mut acc = mesh.clone();
let mut inst_bbs: Vec<crate::math::Aabb> = Vec::with_capacity(count);
inst_bbs.push(mesh.aabb());
+ let mut csg_unions = 0usize;
for i in 1..count {
let mut inst = mesh.clone();
inst.translate(step * (i as f64));
let ibb = inst.aabb();
if union_all && inst_bbs.iter().any(|b| b.overlaps(&ibb, 1e-5)) {
+ csg_unions += 1;
+ if csg_unions > MAX_CSG_UNIONS {
+ return Err(format!(
+ "overlapping array CSG unions exceed limit ({MAX_CSG_UNIONS}); separate instances or reduce count"
+ ));
+ }
acc = boolean_op(&acc, &inst, BoolOp::Union, None, op_id)?;
} else {
acc.append_mesh(&inst);
@@ -983,12 +992,15 @@ pub fn array_linear(
Ok(acc)
}
-/// Repeat a mesh radially around the Z axis, optionally offset radially by `radius` along +X before rotating.
-/// Pairwise-disjoint instances use an O(1) AABB fast-path with zero CSG overhead.
-pub fn array_radial_with_radius(
+/// Repeat a mesh radially around the specified axis (`'x'`, `'y'`, or `'z'`), optionally
+/// offset radially by `radius` before rotating.
+/// Pairwise-disjoint instances use an O(1) AABB fast-path with zero CSG overhead (up to 256
+/// total instances; overlapping CSG unions bounded at 64).
+pub fn array_radial_ex(
mesh: &PolyMesh,
count: usize,
radius: f64,
+ axis: char,
union_all: bool,
op_id: u32,
) -> Result<PolyMesh, String> {
@@ -998,22 +1010,44 @@ pub fn array_radial_with_radius(
if !radius.is_finite() {
return Err("radial array radius must be finite".into());
}
- let max_cnt = if union_all { 32 } else { 64 };
- if count > max_cnt {
- return Err(format!("radial array count {count} exceeds limit ({max_cnt})"));
+ let ax = axis.to_ascii_lowercase();
+ if !matches!(ax, 'x' | 'y' | 'z') {
+ return Err(format!("invalid radial array axis '{axis}' (use x, y, or z)"));
+ }
+ const MAX_TOTAL: usize = 256;
+ const MAX_CSG_UNIONS: usize = 64;
+ if count > MAX_TOTAL {
+ return Err(format!("radial array count {count} exceeds limit ({MAX_TOTAL})"));
}
let mut base = mesh.clone();
if radius.abs() > 1e-12 {
- base.translate(Vec3::new(radius, 0.0, 0.0));
+ let offset = match ax {
+ 'x' => Vec3::new(0.0, radius, 0.0),
+ 'y' => Vec3::new(radius, 0.0, 0.0),
+ _ => Vec3::new(radius, 0.0, 0.0),
+ };
+ base.translate(offset);
}
let mut acc = base.clone();
let mut inst_bbs: Vec<crate::math::Aabb> = Vec::with_capacity(count);
inst_bbs.push(base.aabb());
+ let mut csg_unions = 0usize;
for i in 1..count {
let mut inst = base.clone();
- inst.rotate_deg(0.0, 0.0, 360.0 * (i as f64) / (count as f64));
+ let ang = 360.0 * (i as f64) / (count as f64);
+ match ax {
+ 'x' => inst.rotate_deg(ang, 0.0, 0.0),
+ 'y' => inst.rotate_deg(0.0, ang, 0.0),
+ _ => inst.rotate_deg(0.0, 0.0, ang),
+ }
let ibb = inst.aabb();
if union_all && inst_bbs.iter().any(|b| b.overlaps(&ibb, 1e-5)) {
+ csg_unions += 1;
+ if csg_unions > MAX_CSG_UNIONS {
+ return Err(format!(
+ "overlapping radial array CSG unions exceed limit ({MAX_CSG_UNIONS}); increase r or reduce count"
+ ));
+ }
acc = boolean_op(&acc, &inst, BoolOp::Union, None, op_id)?;
} else {
acc.append_mesh(&inst);
@@ -1023,6 +1057,17 @@ pub fn array_radial_with_radius(
Ok(acc)
}
+/// Repeat a mesh radially around the Z axis, optionally offset radially by `radius` along +X before rotating.
+pub fn array_radial_with_radius(
+ mesh: &PolyMesh,
+ count: usize,
+ radius: f64,
+ union_all: bool,
+ op_id: u32,
+) -> Result<PolyMesh, String> {
+ array_radial_ex(mesh, count, radius, 'z', union_all, op_id)
+}
+
/// Repeat a mesh radially around the Z axis (zero radial offset).
pub fn array_radial(
mesh: &PolyMesh,
@@ -1030,7 +1075,7 @@ pub fn array_radial(
union_all: bool,
op_id: u32,
) -> Result<PolyMesh, String> {
- array_radial_with_radius(mesh, count, 0.0, union_all, op_id)
+ array_radial_ex(mesh, count, 0.0, 'z', union_all, op_id)
}
/// Topology-preserving Quadric Error Metric (QEM) mesh decimation with Edelsbrunner Link-Condition.
diff --git a/crates/forma-core/src/primitives.rs b/crates/forma-core/src/primitives.rs
index df3097e..74b4d93 100644
--- a/crates/forma-core/src/primitives.rs
+++ b/crates/forma-core/src/primitives.rs
@@ -42,7 +42,7 @@ pub fn make_cylinder(radius: f64, height: f64, segs: usize, op_id: u32) -> PolyM
/// Cone or frustum aligned with Z, centered at origin (z in [-h/2, +h/2]).
pub fn make_cone(r_bot: f64, r_top: f64, height: f64, segs: usize, op_id: u32) -> PolyMesh {
- let segs = segs.clamp(3, 48);
+ let segs = segs.clamp(3, 128);
let hz = height * 0.5;
let mut m = PolyMesh::new();
@@ -117,8 +117,8 @@ pub fn make_sphere(radius: f64, u_segs: usize, v_segs: usize, op_id: u32) -> Pol
if !radius.is_finite() || radius <= 1e-9 {
return PolyMesh::new();
}
- let u_segs = u_segs.clamp(4, 36);
- let v_segs = v_segs.clamp(3, 18);
+ let u_segs = u_segs.clamp(4, 96);
+ let v_segs = v_segs.clamp(3, 48);
let mut m = PolyMesh::new();
let south = m.add_vert(Vec3::new(0.0, 0.0, -radius));
let north = m.add_vert(Vec3::new(0.0, 0.0, radius));
@@ -164,8 +164,8 @@ pub fn make_torus(major_r: f64, minor_r: f64, major_segs: usize, minor_segs: usi
if !major_r.is_finite() || !minor_r.is_finite() || major_r <= 1e-9 || minor_r <= 1e-9 {
return PolyMesh::new();
}
- let major_segs = major_segs.clamp(6, 48);
- let minor_segs = minor_segs.clamp(4, 32);
+ let major_segs = major_segs.clamp(6, 96);
+ let minor_segs = minor_segs.clamp(4, 48);
let mut m = PolyMesh::new();
let mut rings = Vec::with_capacity(major_segs);
for i in 0..major_segs {
@@ -201,7 +201,7 @@ pub fn make_revolve(profile: &[(f64, f64)], segs: usize, op_id: u32) -> Result<P
if profile.len() < 2 {
return Err("revolve profile requires at least 2 (r, z) points".into());
}
- let segs = segs.clamp(6, 48);
+ let segs = segs.clamp(6, 128);
// Clean consecutive duplicates and build explicit list starting/ending at r=0 if needed
let mut pts: Vec<(f64, f64)> = Vec::new();
for &(r, z) in profile {
@@ -289,7 +289,7 @@ pub fn make_tube(points: &[Vec3], radius: f64, ring_segs: usize, closed: bool, o
if points.len() > 256 {
return Err("tube path exceeds maximum of 256 points".into());
}
- let ring_segs = ring_segs.clamp(6, 36);
+ let ring_segs = ring_segs.clamp(6, 64);
let n = points.len();
let mut tangents = Vec::with_capacity(n);
for i in 0..n {
@@ -389,7 +389,7 @@ pub fn make_arc_tube(
if !(end_deg - start_deg).is_finite() || (end_deg - start_deg).abs() <= 1e-6 {
return Err("arc_tube requires non-zero angular span".into());
}
- let arc_segs = arc_segs.clamp(4, 64);
+ let arc_segs = arc_segs.clamp(4, 128);
let mut pts = Vec::with_capacity(arc_segs + 1);
for i in 0..=arc_segs {
let t = (start_deg + (end_deg - start_deg) * (i as f64) / (arc_segs as f64)).to_radians();
@@ -451,7 +451,7 @@ pub fn make_pipe(r_out: f64, r_in: f64, height: f64, segs: usize, op_id: u32) ->
if height <= 1e-6 {
return Err("pipe height must be positive".into());
}
- let segs = segs.clamp(6, 48);
+ let segs = segs.clamp(6, 128);
let hz = height * 0.5;
let mut m = PolyMesh::new();
let mut ob = Vec::with_capacity(segs);
@@ -551,7 +551,7 @@ pub fn make_loft(sections: &[(f64, Vec<(f64, f64)>)], segs: usize, op_id: u32) -
if sections.len() < 2 {
return Err("loft requires at least 2 (z, profile) sections".into());
}
- let segs = segs.clamp(8, 48);
+ let segs = segs.clamp(8, 128);
let mut ordered = sections.to_vec();
ordered.sort_by(|a, b| a.0.total_cmp(&b.0));
@@ -599,7 +599,7 @@ pub fn make_thread(
if pitch <= 1e-5 || turns <= 0.25 {
return Err("thread requires pitch > 0 and turns > 0.25".into());
}
- let segs = segs_per_turn.clamp(12, 48);
+ let segs = segs_per_turn.clamp(12, 96);
let height = pitch * turns;
let rings_per_pitch = 8usize;
let n_rings = ((turns * rings_per_pitch as f64).ceil() as usize).clamp(8, 128);
diff --git a/crates/forma-core/src/remesh.rs b/crates/forma-core/src/remesh.rs
index 627eccc..75e7d7b 100644
--- a/crates/forma-core/src/remesh.rs
+++ b/crates/forma-core/src/remesh.rs
@@ -18,8 +18,8 @@ pub fn remesh_voxel(mesh: &PolyMesh, voxel_size: f64, op_id: u32) -> Result<Poly
let diag = sz.len().max(1e-4);
let mut h = if voxel_size > 1e-5 { voxel_size } else { diag / 28.0 };
let max_axis = sz.x.max(sz.y).max(sz.z);
- if max_axis / h > 56.0 {
- h = max_axis / 56.0;
+ if max_axis / h > 128.0 {
+ h = max_axis / 128.0;
}
let pad = h * 2.0;
diff --git a/crates/forma-core/src/render.rs b/crates/forma-core/src/render.rs
index aa6c5ae..a2397e9 100644
--- a/crates/forma-core/src/render.rs
+++ b/crates/forma-core/src/render.rs
@@ -223,7 +223,7 @@ pub fn render_quad_png_bytes(mesh: &PolyMesh, size: usize) -> Result<Vec<u8>, St
if mesh.faces.is_empty() || mesh.verts.is_empty() {
return Err("cannot render empty mesh".into());
}
- let size = size.clamp(128, 1024);
+ let size = size.clamp(128, 2048);
let half = size / 2;
let mut fb = Framebuffer::new(size, size);
diff --git a/crates/forma-core/tests/kernel_tests.rs b/crates/forma-core/tests/kernel_tests.rs
index d34a88b..af57d86 100644
--- a/crates/forma-core/tests/kernel_tests.rs
+++ b/crates/forma-core/tests/kernel_tests.rs
@@ -594,3 +594,31 @@ fn test_glb_yup_roundtrip_legacy_compat_and_import_fixes() {
let n = open_mesh.face_normal(0);
assert!(n.z > 0.99, "open quad +Z normal must not be inverted at negative Z, got {n:?}");
}
+
+#[test]
+fn test_ergonomics_radial_axis_disjoint_array_caps_and_high_segs() {
+ // 1. High-resolution wheel rim (segs=96, previously clamped at 48)
+ let rim = forma_core::primitives::make_pipe(1.0, 0.88, 0.14, 96, 1).unwrap();
+ let rinv = MeshInvariants::compute(&rim);
+ assert!(rinv.is_valid_solid() && rinv.genus == 1 && rinv.faces == 96 * 4, "rim={rinv:?}");
+
+ // 2. Vertical wheel spoke/rivet ring via radial(axis='x') & radial(axis='y')
+ let rivet = forma_core::primitives::make_box(0.06, 0.04, 0.04, 2);
+ let ring_x = forma_core::ops::array_radial_ex(&rivet, 80, 0.95, 'x', true, 3).unwrap();
+ let xinv = MeshInvariants::compute(&ring_x);
+ assert!(xinv.is_valid_solid() && xinv.components == 80, "ring_x={xinv:?}");
+ let xbb = ring_x.aabb();
+ assert!((xbb.size().x - 0.06).abs() < 1e-4 && xbb.size().y > 1.8 && xbb.size().z > 1.8, "axis=x ring should lie in YZ plane: {xbb:?}");
+
+ let ring_y = forma_core::ops::array_radial_ex(&rivet, 12, 1.0, 'y', true, 4).unwrap();
+ let yinv = MeshInvariants::compute(&ring_y);
+ assert!(yinv.is_valid_solid() && yinv.components == 12);
+ let ybb = ring_y.aabb();
+ assert!((ybb.size().y - 0.04).abs() < 1e-4 && ybb.size().x > 1.8 && ybb.size().z > 1.8, "axis=y ring should lie in XZ plane: {ybb:?}");
+
+ // 3. 100-instance disjoint linear array passes via O(1) AABB fast-path (previously capped at 32)
+ let plank = forma_core::primitives::make_box(0.05, 1.0, 0.02, 5);
+ let deck = forma_core::ops::array_linear(&plank, 100, Vec3::new(0.1, 0.0, 0.0), true, 6).unwrap();
+ let dinv = MeshInvariants::compute(&deck);
+ assert!(dinv.is_valid_solid() && dinv.components == 100, "deck={dinv:?}");
+}
diff --git a/crates/forma-lang/src/lib.rs b/crates/forma-lang/src/lib.rs
index 2f45b74..66b46fa 100644
--- a/crates/forma-lang/src/lib.rs
+++ b/crates/forma-lang/src/lib.rs
@@ -77,7 +77,7 @@ Primitives (`at=(x,y,z)` or `[(x,y,z),...]`; `z0=Z` sets bottom):
- `arc_tube(center=(x,y,z), rx=, rz=, start=-90, end=90, r=0.08)` | `tube([(x,y,z),...], r=0.08)`
- `extrude2d([(x,y),...], z0, z1, holes=)` | `pipe(ro,ri,h)` | `loft([(z,[(x,y),...]),...])` | `thread(r0,r1,pitch,turns)`
Ops (`|` or `op(m,...)`; `add`/`cut` take `at=(x,y,z)` or `[...]`):
-- `add(b, at=)` | `cut(b, at=)` | `intersect(b)` | `mirror(x|y|z)` | `shell(t, open_top=true)` | `subsurf(levels=1, crease=false)`
+- `add(b, at=)` | `cut(b, at=)` | `intersect(b)` | `mirror(x|y|z)` | `array(n,step)` | `radial(n,r=,axis=z)` | `shell(t, open_top=true)` | `subsurf(levels=1, crease=false)`
- `bevel(edges, w, segs=1)` (vertical|top_rim|bottom_rim|outer_rim|sharp|all) | `extrude(faces, d)` | `inset(faces, amt, depth=0)`
- `move(x,y,z)` | `rot(rx,ry,rz)` | `scale(sx,sy,sz)` | `align(x|y|z, min|center|max, val)` | `on(base)` | `align_z0(z)`
- `mat(name, color=(r,g,b), metallic=0.1, roughness=0.5)` | `color(r,g,b)` | `clearance(a,b)` | `section(p,z)`
diff --git a/crates/forma-lang/src/pipeline.rs b/crates/forma-lang/src/pipeline.rs
index 6ede451..22283a5 100644
--- a/crates/forma-lang/src/pipeline.rs
+++ b/crates/forma-lang/src/pipeline.rs
@@ -1102,8 +1102,13 @@ impl<'a> Parser<'a> {
None => (None, None),
};
let radius = kw.get("r").or_else(|| kw.get("radius")).or(pos_r).map(|v| v.as_num(&ctx)).transpose()?.unwrap_or(0.0);
+ let axis = kw
+ .get("axis")
+ .and_then(|v| v.as_str(&ctx).ok())
+ .and_then(|s| s.chars().next())
+ .unwrap_or('z');
let u = kw.get("union").or(pos_u).map(|v| v.as_bool(&ctx)).transpose()?.unwrap_or(true);
- forma_core::ops::array_radial_with_radius(&mesh, cnt, radius, u, op_id).map_err(|e| format!("{ctx}: {e}"))
+ forma_core::ops::array_radial_ex(&mesh, cnt, radius, axis, u, op_id).map_err(|e| format!("{ctx}: {e}"))
}
"decimate" | "lod" => {
let ratio = kw.get("ratio").or_else(|| pos.first()).map(|v| v.as_num(&ctx)).transpose()?.unwrap_or(0.5);
diff --git a/wasm/forma.wasm b/wasm/forma.wasm
index ff86d20..e512b0c 100755
Binary files a/wasm/forma.wasm and b/wasm/forma.wasm differ