Skip to content

saveMeshAsStep: one STEP face per mesh segment given by boundary edges - #7610

Open
Fedr wants to merge 11 commits into
masterfrom
segmented-mesh-to-step
Open

Fedr wants to merge 11 commits into
masterfrom
segmented-mesh-to-step

Conversation

@Fedr

@Fedr Fedr commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

MR::saveMeshAsStep( mesh, faceBoundaries, out|path, MeshToStepSettings ) writes a mesh as a STEP (AP214) boundary representation with one face per mesh segment, where the segments are the connected parts of the mesh separated by given edges (e.g. selected by the user). Unlike #7581, the subdivision on faces is an input, so there is no region growing (Grower) or cleaning (Cleaner).

The surfaces of the segments are found by fitSmoothSurfaces (#7621, in master). Taken from #7581: the BRep/STEP writer (MRMeshToStepBRep.cpp).

New:

  • Surface of every segment: fitSmoothSurfaces with MeshToStepSettings::tolerance as its bsplineThreshold (estimated from the noise of the mesh if not given), and every segment keeps its SmoothSurface. The boundary snapping measures the distance to a B-spline surface along its normal, which extends the surface beyond its border by its tangent planes, so that the boundary vertices can be moved to where it meets the neighbour surfaces.
  • Boundary vertices are moved to where the surfaces of all adjacent segments meet (Gauss-Newton steps with SymMatrix3::pseudoinverse( 0.01 ): no move along the surfaces, and only along the common normal of nearly tangent surfaces; it needs SymMatrix3::pseudoinverse: no direct inverse of a rank-1 matrix, tolerance floor cbrt(eps) #7611, which fixed the pseudoinverse of rank-1 matrices, i.e. of a vertex on one surface). The chains between two segments are smoothed between the snaps: the selected mesh edges zigzag around the true curve, and where the surfaces are nearly tangent the snap alone keeps that zigzag. The curve tolerance in the BRep writer is per chain (twice the snap residual of its vertices), and circles must pass through their end vertices.
  • Surfaces fitted together: surfaces fitted separately do not meet where they are nearly tangent (pieces of one smooth surface, nearly coplanar planes), so a boundary vertex there stays at some distance from them, and the curves between the faces are not on their surfaces. The surfaces of the segments with such vertices are fitted again (same kind, same number of B-spline control points), with these vertices taken in their common positions with weight 100, and all boundary vertices are snapped again; it is repeated up to 5 times while the largest distance from a vertex to its surfaces decreases. The other surfaces do not change.
  • MeshToStepStats::maxDeviation: the maximum distance from a mesh vertex to the surface of its face.

Scanned housing (37201 vertices, 50 segments from 2694 selected edges)

time 3.0 s
faces 28 planes, 12 cylinders, 7 B-splines, and 80 triangle faces of 3 bump segments
STEP read back by OpenCASCADE, area / volume 15799 / 70000 (mesh: 15687 / 70189)
mesh vertices to STEP, mean / max 0.050 / 1.19 mm
read-back surface to mesh, area-weighted mean / max 0.041 / 0.74 mm

The whole automatic conversion of the same mesh (#7581) produced thousands of single-triangle planes or broken surfaces, since that mesh is not a CAD tessellation.

Segments enclosed by one other segment whose surfaces do not meet along their boundary (three bumps of the scan, 4 to 9 mm², about 1 mm high, fitted by planes meeting the enclosing plane along a line) become one plane face per triangle; before, their faces came out empty.

OpenCASCADE reports the whole shape and all its 127 faces as valid (BRepCheck_Analyzer). Without fitting the surfaces together 2 faces were invalid (a self-intersecting wire, a wire of bad orientation): pieces of the large bore fitted separately disagreed by about 0.5 mm, so at their corners with the counterbore floor the three surfaces had no common point. The largest distance from a boundary vertex to its surfaces (the uncertainty written in the file) is now 0.05 mm instead of 0.21 mm, and 9 vertices instead of 226 are farther than 0.02 mm.

Test MRMesh.MeshToStep: cube, cylinder, UV sphere, torus with sharp edges as the boundaries, plus a subdivided cube and a UV sphere with noisy vertices.

🤖 Generated with Claude Code

Fedr and others added 2 commits November 27, 2026 14:00
The subdivision of the mesh on faces is an input (e.g. edges selected by
the user), so there is no region growing or cleaning. Every segment gets
the simplest of plane, cylinder, cone, sphere, torus whose RMS distance is
within 1.5x of any more complex type, or a B-spline surface if that distance
exceeds the tolerance (by default 2.5x the median over the segments, the
noise level). Boundary vertices are moved to where the surfaces of adjacent
segments meet; boundary chains are smoothed between the snaps, which
removes the zigzag of mesh edges where surfaces are nearly tangent.
Surfaces, the B-spline fit and the BRep writer are taken from #7581.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Point

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fedr added a commit that referenced this pull request Nov 27, 2026
…rance floor cbrt(eps) (#7611)

SymMatrix3::pseudoinverse took the direct-inverse fast path (added in #7178) for some rank-1 matrices: their determinant and 2x2 minors are rounding errors (about eps * trace^3) that could pass its check, and the result was garbage (up to 2e6 times too large, rank reported as 3). Found in #7610.

- the direct path also requires det > tol^2 * (trace/3)^3, which every matrix with lambdaMin > tol * lambdaMax satisfies;
- its tolerance is at least cbrt(eps) of the type (compile-time detail::cCbrtEpsilon<T>) instead of the fixed 1e-4, which left no margin for float;
- the direct inverse scales each element by 1 / d instead of res /= d after filling res: same results, 2x faster with MSVC x64 for double (MSVC reloaded just stored doubles as vectors, defeating store-to-load forwarding; reported as Developer Community 11188541).

Performance on 8 compiler/CPU combinations: well-conditioned matrices within 1.3 ns, rank-1 matrices 12-29% slower (now on eigensJacobi, master was only faster by being wrong), double with condition number 1e4..1e5 93-97% faster (now inverted directly).
Fedr and others added 3 commits November 27, 2026 16:54
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…enclosing one

A segment enclosed by one other segment needs a closed curve where the two
surfaces meet near its boundary; a bump of a scanned plane fitted by another
plane meets it along a line, and its face came out empty. Such segments (and
the segments inside them) become one plane face per triangle; the vertices
are snapped only to the other surfaces, and the triangle planes pass through
the moved vertices. The STEP uncertainty is the largest distance from a
boundary vertex to its surfaces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fedr and others added 6 commits November 29, 2026 21:47
…er, own fitting removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…shToStep.cpp

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…AllComponents, trackLeftBoundaryLoop, PointAccumulator

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…vertices

Surfaces fitted separately do not meet where they are nearly tangent, and the curves between
such faces were not on their surfaces (2 of 127 faces of the scanned housing were invalid in OpenCASCADE).
Now the surfaces of the regions with such vertices are fitted again with these vertices
in their common positions with a large weight, up to 5 times while the largest gap decreases.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

1 participant