Repository navigation
Conversation
Regions of the mesh lying on planes, cylinders, cones, spheres and tori are found by region growing, the remaining smooth regions are approximated by B-spline surfaces, and all of them are written as trimmed faces of a shell in STEP AP214. Optional edges can be given that must separate the faces. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
GCC 12 reports an impossible memcpy bound inside the inlined
"(" + std::string + ")"; build the string with fmt::format instead.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A mesh loaded from a text file with few decimal digits (e.g. 6 significant digits in OBJ) has vertices shifted from their surfaces by more than 2.5e-6 of the diagonal, and mostly came out as tiny planes. The default tolerance is now at least 7 maximum rounding errors of the coordinates, found from the number of digits in their shortest exact notation. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A closed chain of 3-4 points on mesh boundary always lies on a circle, so isolated triangles and rectangles were written as discs. Circles on mesh boundary now need at most 35 degrees between consecutive points. A fillet made of long needle triangles has vertices only at its two ends, and a torus with a very large tube radius passing through them could win over the true cylinder by taking a few more small triangles. With areas within 3%, the simpler surface now wins if it is much closer to the centers of the triangles. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2x2 and 3x3 fits accumulate SymMatrix2d/SymMatrix3d and use their eigens() and pseudoinverse(). The circle fit keeps Eigen's rank-revealing solver (pseudoinverse gave more fragmented faces on short arcs), and the two 4x4 fits stay in Eigen until SymMatrix4 gets an eigen-solver. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Grantim
reviewed
Nov 25, 2026
Grantim
left a comment
Contributor
There was a problem hiding this comment.
Review: reuse of existing MeshLib code
Most of the new code has no counterpart in MeshLib yet (torus, B-spline surfaces and curves, LSCM, normals-based axis fits, the STEP text writer). Several helpers, though, duplicate existing functions.
Drop-in replacements
- Segments by
faceBoundaries(flood fill inmakeData) andCleaner::splitOnComponents_->MeshComponents::getAllComponentsMap( mesh, FaceIncidence::PerEdge, faceBoundaries )(itsisCompBdparameter). numBoundaryLoops(std::set/std::mapof directed edges) ->findLeftBoundary( topology, ®ion ).size().- Union-find in
Cleaner(parent[]/find) ->MRUnionFind.h. frame()->Vector3d::perpendicular().centroid+fitPlane->PointAccumulator::getBestPlane().Data::tris/Data::nbrand theBRepWriter::bnd_/edgeOf_maps ->MeshTopology(edgeWithLeft,left/right); region borders ->findLeftBoundaryInsideMesh.Grower::patch_(k-ring) ->expand( topology, FaceId, hops ), limited to available faces.
Fitters: better promoted to the library than replaced
Cylinder3Approximation::solveSpecificAxisneeds at least 6 points and logs a warning below that, andsolveGeneralis a 180x90 hemisphere search, so neither suits per-seed use. The normals-based axes (fitCyl,fitConeByNormals,revolutionAxis) could become public functions or extra fitter types ofCylinder3Approximation/Cone3Approximation, so other callers get them too.- Sphere and circle fits exist only inside the
SphereObject/CircleObjectconstructors (float, uncentered). The centeredfitSphere/fitCircle2could become free functions inMRBestFit.h, used by both objects. Surfcould holdPlane3d/Cylinder3d/Cone3d/Sphere3dplus a newTorus3d.- Optional, a redesign rather than a drop-in: the merge steps of
Cleanercould be aMeshSegmenter<Policy>policy with the residual of the union fit as the merge penalty.
Keep as is
- The OCCT-free STEP writer:
MRIOExtrasdisables STEP under Emscripten, so writing STEP text from MRMesh is the only way to have this on the web.
Suggested split
- Public primitives and fits:
Torus3and its fit, sphere / circle free fits, normals-based axes. - B-spline surface and curve, LSCM parametrization.
saveMeshAsStepon top of them, using the topology helpers above.
I'm drafting (1) and (2) as a separate PR, so this one could build on it.
Generated with Claude Code
- segments and components of regions: MeshComponents::getAllComponentsMap - disk check of a B-spline patch: findLeftBoundary (plus no vertex passed twice) - union of same-surface regions: UnionFind - plane fit: PointAccumulator - orthogonal basis: Vector3d::perpendicular, except the reference direction of surface placements, where the previous choice leaves fewer cracks after reading the file back Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Contributor
Author
|
Thanks. Pushed the drop-in part in cc7a154. Applied
Face counts and deviations on the 11 NIST parts are the same as before the change. Holes after reading back: ftc_06 got one 3-vertex crack, ctc_04 lost one. Not in this commit
Generated with Claude Code |
This was referenced Nov 25, 2026
The sphere fit solves with SymMatrix4d::pseudoinverse, and the cone fit takes the eigenvector of the smallest eigenvalue from SymMatrix4d::eigens. The only fixed-size Eigen call left is the rank-revealing solve of the circle fit. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
MRMeshToStep.cpp had more than 3000 lines. The code is only moved: B-spline surface and its fit, analytic surfaces and their fits, mesh data, region growing, region clean-up and B-rep writing get a file each. Internal headers are .hpp, so the bindings do not see them. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The sphere, circle, cylinder, cone and torus fits and the torus refinement now call findBestSphere, findBestCircle, findBestCylinderByNormals, findRevolutionAxis, findBestConeWithAxis, findBestConeByTangentPlanes, findBestTorusWithAxis and refineTorus; only the Levenberg-Marquardt refinement of cylinders and cones stays here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Grantim
added a commit
that referenced
this pull request
Nov 26, 2026
BSplineSurface::project clamps the initial parameters to the domain: from outside it, the steps were halved toward the start, and the result could stay outside (on the skewed plane of the tests, (1.5, 0.65) instead of (1, 0.8)). The step is halved without a limit, and the iterations stop when the predicted decrease of the squared distance is below its rounding error, 8 eps |r| (|p| + |r|): once converged, a step that raised the computed distance by an ulp was rejected after 5 evaluations. In a simulation of the loop of #7581 (6x4 and 10x5 fits to 2000 points, 8 rounds of project( p, uv, 4 ) from the previous parameters, also translated by 1e3 and 1e5), the evaluations per call drop from 7.3-8.5 to 2.1-3.2 with the same final fits. Test: a projection from outside the domain. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Grantim
added a commit
that referenced
this pull request
Nov 26, 2026
A rejected step of BSplineSurface::project is shortened to the minimum of the parabola through the squared distances at both ends with the slope at the start, but 2 to 10 times, instead of halved. Halving scales u and v together, so far from the convex side, where the steps are 1 + distance / curvature radius times too long, the other parameter lagged: above an arch of curvature radius 1, the parameters were 0.019, 0.058 and 0.105 off after 12 iterations at distances 10, 25 and 100. Now distances 1 to 50 converge within 3 to 10 iterations (8.7e-9 at 10, 1.1e-8 at 25); 100 is still 0.006 off. The evaluations per projection in the simulation of the loop of #7581 stay at 2.1-3.2, at most 11 instead of 18. Test: a point at distance 25 above the arch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
refineTorus from master keeps the major radius non-negative, so a refined torus without major radius is taken as a sphere, and a sphere region with several weak sphere pieces around it joins them in a B-spline surface. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Fedr
pushed a commit
that referenced
this pull request
Nov 26, 2026
MRBSpline.h: BSplineCurve and BSplineSurface (evaluation, normal, projection of a point), interpolateBSplineCurve through given points, fitBSplineSurface by least squares with smoothing. MRConformalMap.h: leastSquaresConformalMap of a mesh part. All of them come from the private helpers of saveMeshAsStep (#7581), which can use them instead.
BSplineSurface, fitBSplineSurface, interpolateBSplineCurve and leastSquaresConformalMap from master replace the private basis, dense fits and parametrization. Left here: the grid of surface points to start projections from, the fit error, scaling of the parameters into the margin square, and the loop over the numbers of control points. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The best fits take MR::Vector since #7599, which broke the build here; all arrays of the converter are MR::Vector now. The output STEP files of 11 NIST parts are the same as before. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Fedr
added a commit
that referenced
this pull request
Nov 26, 2026
MRFittedBSplineSurface.h: FittedBSplineSurface is a BSplineSurface with its fit error and a grid of surface points to project on it without initial parameters; fitBSplineSurfaceToMesh fits it to a mesh part with disk topology (conformal parametrization, then least squares fits and projections with more and more control points until given tolerance). It comes from the private helpers of saveMeshAsStep (#7581), which can use it instead.
fitBSplineSurfaceToMesh from master replaces the private fit of a B-spline surface to a mesh region; MRMeshToStepBSpline files are gone. The faces, bad faces and holes of 11 NIST parts are the same. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The own Levenberg-Marquardt refinement of cylinders and cones (numeric derivatives, on Eigen) is replaced by the public ones. With them one part got 1500 bad faces: a B-spline surface replacing a group of regions was taken with any error. Now it must be within 50 tolerances of the vertices. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Fedr
added a commit
that referenced
this pull request
Nov 26, 2026
…efinement As in #7581 now: the own Levenberg-Marquardt with numeric derivatives on Eigen is gone. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
MR::saveMeshAsStep( mesh, stream or path, MeshToStepSettings )to MRMesh. It turns a mesh that is a tessellation of a CAD model back into a boundary representation and writes it as STEP (AP214). The STEP text is written directly, so nothing new is linked.How it works
faceBoundaries(one segment if none are given).MANIFOLD_SOLID_BREPif everything is closed).Code
MRMeshToStep.h/.cpp: the public function only.MRMeshToStepSurf,MRMeshToStepData: the surface type with its fits, the shared mesh data.MRMeshToStepGrower.cpp,MRMeshToStepCleaner.cpp,MRMeshToStepBRep.cpp: steps 2, 3 and 4.The internal headers are
.hppin namespaceMR::MeshToStep, so the bindings generator does not see them.Spheres, circles, cylinders, cones, tori and revolution axes are fitted by the public functions of
MRBestFit.h(#7589, #7595). B-spline surfaces and curves are public too:FittedBSplineSurfacewithfitBSplineSurfaceToMesh(#7598) for the surfaces of freeform regions andinterpolateBSplineCurve(#7590) for their borders; the converter adds only the (0,0,1) normal where a surface has none. Cylinders and cones are refined byrefineCylinderandrefineCone(#7600), tori byrefineTorus. Own fitting code is only the plane fit onPointAccumulator, and Eigen is not used here at all.Testing
MRMesh.MeshToStepin MRTest: cube -> 6 planes, cylinder -> 2 planes + 1 cylinder, UV sphere -> 1 sphere, torus -> 1 torus.Beyond that, 11 NIST PMI test parts were meshed from their STEP files, converted, read back through OpenCASCADE and compared with the mesh (no
faceBoundaries):"Clean" means watertight, with the deviation at the level of the two tessellations. A bad face deviates by more than 0.1 mm. Times are on Ryzen 9 3900X.
Known limitations (why it is a draft)
refineToruskeeps the major radius non-negative, so it cannot give a lemon-shaped surface of revolution. A refined torus without major radius is taken as a sphere, and a sphere region with several weak sphere pieces around it joins them in one B-spline surface (ftc_06 has such a region).🤖 Generated with Claude Code