Skip to content

fix: dissolve coplanar boolean edges when faces share tags - #46

Closed
zalzdarkent wants to merge 4 commits into
Sandflow-ai:mainfrom
zalzdarkent:fix/dissolve-coplanar-boolean-edges
Closed

zalzdarkent wants to merge 4 commits into
Sandflow-ai:mainfrom
zalzdarkent:fix/dissolve-coplanar-boolean-edges

Conversation

@zalzdarkent

@zalzdarkent zalzdarkent commented Nov 22, 2026 •

Copy link
Copy Markdown

Problem

Blender's EXACT boolean solver frequently leaves redundant coplanar split edges on flat faces (for example, when overlapping faces are fused in join() or cut()). While _flatten_names() already untags flat named edges and marks them smooth, those interior edges and split faces remain in the mesh.

Solution

  • Added _dissolve_coplanar(bm) in src/sand3d/edit.py, called right after _flatten_names(bm) in _boolean().
  • Dissolves interior edges (use_verts=True, use_face_split=False so 2-valent collinear vertices are also cleaned up and tessellation does not produce duplicate triangles) only when:
    1. Both adjacent faces are strictly coplanar (dot > 1.0 - 1e-6, avoiding curved smooth-loft facets where adjacent normals can differ by < 0.5°),
    2. Both faces share identical FACE_TAG layers (preserving face provenance), and
    3. The edge carries no EDGE_TAG (not a real/named edge).
  • Added test_coplanar_edges_dissolved_on_join in tests/test_edits.py verifying that two overlapping boxes of equal height merge their coplanar top/bottom splits cleanly into 12 faces (instead of 14).

Golden Digests (tests/golden/bpy-5.2.json)

Updated to match Linux CI (bpy 5.2). Dissolving redundant coplanar edges and 2-valent collinear vertices after booleans reduces face/edge/vertex counts on boolean-built geometry, moving the following 10 digests:

  • Cases: case:holes_and_pockets, case:distributors, case:export_trace
  • Programs: program:weight_stack.weight_stack, program:cutlery.spoon, program:cutlery.fork, program:washing_machine.washer, program:washing_machine.washer_open, program:lamp.lamp, program:rack.rack

Problem:
Blender's EXACT boolean solver leaves redundant coplanar split/triangulation edges on flat faces (e.g. overlapping faces in join/cut). Even after _flatten_names() untags flat named edges and marks them smooth, the extra edges and split faces remain in the mesh.

Solution:
- Add _dissolve_coplanar(bm) in src/sand3d/edit.py, called after _flatten_names(bm) in _boolean().
- Only dissolve interior edges whose two adjacent faces are strictly coplanar (dot > 1.0 - 1e-6, avoiding curved smooth-loft facets), share identical FACE_TAG layers, and have no EDGE_TAG on the separating edge.
- Add test_coplanar_edges_dissolved_on_join in tests/test_edits.py to verify overlapping flush boxes merge coplanar top/bottom splits cleanly.
@zalzdarkent
zalzdarkent requested a review from a team as a code owner November 22, 2026 00:37
@zalzdarkent

Copy link
Copy Markdown
Author

Hi! I've synced this branch with the latest main (merge conflicts resolved), updated _dissolve_coplanar() to use use_verts=True so tessellation doesn't leave collinear 2-valent vertices, and rewritten the 27 affected golden digests in tests/golden/bpy-5.2.json via tests/run_golden.py --write.

Ready for CI workflow approval and review whenever you have a moment—thanks!

@tneveu-chat3d

Copy link
Copy Markdown
Contributor

Hey, thanks for this PR, I delayed the review since we had a lot of changes with the 1.7.0 & organic support.

Before we dig in, I'd like to understand what got you there. What were you building when those leftover seams got in your way? Was it how the mesh looked, a later step like a fillet or a shell that misbehaved, an export, or something else?

If you still have the small program where you hit it, could you share it? Knowing the actual pain point will help us find the right fix.

@zalzdarkent

Copy link
Copy Markdown
Author

Thanks for taking a look!

The main pain point was mesh / wireframe cleanliness on generated models. When having an AI agent build 3D objects composed of multiple overlapping box/planar parts using join() / cut(), the shaded result looked fine, but inspecting the mesh in wireframe showed a lot of unnecessary coplanar split edges and extra vertices across flat surfaces.

Even a minimal case like joining two boxes of equal height:

import sand3d as s3d

a = s3d.box("a", 10, 10, 10)
s3d.join(a, s3d.at(s3d.box("b", 5, 5, 10), x=4, y=4))

leaves the flat top and bottom split into extra faces (14 polygons instead of 12), and in larger agent-generated models with several boolean steps, the wireframe quickly gets cluttered with redundant interior edges on flat faces.

That's why I added _dissolve_coplanar(bm) right after _flatten_names(bm)—to clean up those redundant coplanar splits when the adjacent faces share the same FACE_TAG layers and no named EDGE_TAG separates them.

(Also just pushed an update to tests/golden/bpy-5.2.json aligning the 10 affected digests with the Linux CI output from the last run.)

@zalzdarkent

Copy link
Copy Markdown
Author

Here is a side-by-side comparison in Blender for the same sand3d model built on upstream/main vs. this PR:

Before (upstream/main) After (PR #46)
Before After

Mesh Comparison

Metric upstream/main PR #46 Change
Vertices 76 44 -42.1%
Edges 119 69 -42.0%
Faces 45 27 -40.0%
Triangles 148 84 -43.2%
Volume (mm³) 83,136.0000 83,136.0000 0% (identical)
Script used
import sand3d as s3d

s3d.units("mm")
body = s3d.box("bracket", 80, 50, 12)
s3d.join(body, s3d.at(s3d.box("rib_left", 14, 50, 12), x=-33))
s3d.join(body, s3d.at(s3d.box("rib_right", 14, 50, 12), x=33))
s3d.join(body, s3d.at(s3d.box("boss_center", 36, 30, 12), y=10))
s3d.join(body, s3d.at(s3d.box("back_wall", 80, 12, 36), y=19, z=12))
s3d.join(body, s3d.at(s3d.box("gusset_l", 12, 24, 24), x=-34, y=7, z=12))
s3d.join(body, s3d.at(s3d.box("gusset_r", 12, 24, 24), x=34, y=7, z=12))
s3d.cut(body, s3d.at(s3d.box("slot_front", 30, 16, 12), y=-17))
s3d.cut(body, s3d.at(s3d.box("notch_top", 24, 12, 14), y=19, z=34))

@tneveu-chat3d

Copy link
Copy Markdown
Contributor

Thanks for the details and the bracket comparison,

I'm a bit sceptical though, The pass doesn't only clean the seams the boolean just made: it touches every coplanar edge on the part, including ones that were there on purpose.

3-bent-plate 6-loft-u-coordinate 4-named-edges

In sand3d, while a model is being built, faces and edges carry meaning: names that later verbs look up, colour and u sampled per face, slices that hold a curve. So the mesh mid-build is working state, not the final product, and merging faces there loses information the next verbs still rely on.

Cleaning up topology feels like something that belongs at the very end, on the exported file, where nothing reads those names anymore. If we tackle topology optimisation, I think export is the place for it as a first step, ideally once we support textures and UVs, so that detail removed from the geometry can be baked into normal maps.

So I'd rather not merge this one for now, but thanks a lot for digging into it.

@tneveu-chat3d

Copy link
Copy Markdown
Contributor

As a followup : #58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants