Repository navigation
Mesh.remesh: even out meshes with large faces and slivers, and close and orient the result - #2296
Conversation
Warp's clustering integrates only at source vertices, so no cluster can land inside a large source triangle. A CAD-like mesh with large faces and slivers stayed uneven, and asking for about as many clusters as source points returned the input unchanged. remesh now splits the source's long edges at their midpoints, the longest first, down to half the output's target edge and until there are four source points per cluster, within 256 source faces per cluster. Selected point data and the resolution field are averaged at the midpoints, so the transfer still interpolates the original surface linearly. After reconstruction, a watertight source's remesh has its holes filled, with the new centroid points taking the mean point data of their hole's boundary. fix_orientation then winds each component consistently, and a component whose normals mostly oppose the source faces its vertices project onto is reversed. Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
…ollapses A strip thinner than the cluster spacing, such as two triangles 1e-7 wide remeshed to their own four corners, has no refined face that spans three clusters, so every face collapsed. remesh then retries the input as given, which keeps the existing thin-face test passing. Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
The face areas' sum was taken from the cross products, which are twice the areas, so the split target was 0.71 instead of 0.5 times the edge of the output's equilateral faces. Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
On an RTX 4090, the remesh of a fillet box whose fanned caps exhaust the refinement budget had a fold: one component with 144 mis-wound edges that no winding can fix. For a watertight source, the faces on mis-wound or non-manifold edges are now cut out and the holes filled again, up to three times. An edge that two sheets still share then gets one copy per sheet from split_nonmanifold_edges. Orientation votes now use per-vertex source normals, so they also cover the faces that hole filling adds. The test helper encodes directed edges without an integer matmul, which CUDA does not support. Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
Refinement now stops at 256 source faces per cluster or 2**22 faces (about 1 GiB at about 250 bytes per face), whichever is lower, unless the input already has more. The unrefined fallback catches only Warp's errors for a remesh with no faces left, so out-of-memory and provenance errors still raise. The docstring and CHANGELOG now say what the even spacing costs: features thinner than the cluster spacing are welded shut and lose volume, which more clusters or a resolution_field avoid. The repair closes holes and folds where it can, rather than always. A CPU test runs the repair on a sphere with a missing vertex fan and a doubled face: it fills the hole, cuts out and refills the faces on the non-manifold edges, and checks the filled centroid's point data. Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
… that more clusters or resolution only reduce thin features' volume loss, and that hole filling can add vertices beyond n_clusters; note the collapse messages' source Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
CODEOWNERS review mapCurrent for commit ⏳ @peterdsharpe — 2 file(s)
No CODEOWNER
Comment |
|
[Medium impact] The changes affect callers of the mesh-remeshing API. Fix midpoint arithmetic before merging so valid translated meshes still remesh successfully. Findings
Summary
peterdsharpe explicitly notes thin-feature volume loss and higher runtime and memory use as trade-offs. One additional issue needs fixing: midpoint arithmetic can overflow on valid reduced-precision coordinates. Reviews (1) · Last reviewed commit: "Mesh.remesh: document that an input over..." · Reviewed by Greptile |
…m the origin don't overflow (a square at x = 40000 gave inf coordinates); the refined points return to the source's dtype Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
There was a problem hiding this comment.
Thanks @peterdsharpe , the uneven-CAD case is a real gap. The change works at the test cases. There are some things to address before merging.
1. Splitting is always on, and coarse, thin or open inputs that main handled well now shrink.
The left panel shows what remesh keeps with default arguments: main keeps everything, while this PR keeps only 3–17% of a thin plate, 45% of a unit cube at 8 clusters, and 90% of an open cylinder's area (its rims move in from z = ±1 to about ±0.9). The right panel shows why, on the 2% plate at 250 clusters. Once the faces are split, the clusters are about 0.09 wide, 4.6× the plate's thickness. So each cluster averages points from the top and bottom faces, its vertex lands inside the plate (red dots), and the triangles cut through it. On main, the plate's only vertices are on its rims, so it comes back unchanged.
Across plates 0.5–5% thick at 100, 250 and 514 clusters, the PR keeps 3–78% of the volume on CPU and CUDA. Disabling only _split_long_edges restores 100% in every case, so the cause is the splitting, not the repair. Sheet-metal parts are common CAD exports, and there is currently no way to get main's behaviour back. An opt-out keyword, e.g. refine: bool, would give users one. A check that falls back to the input when the refined result loses area or volume would protect the default. I prototyped the check; see the follow-up comment. A test that bounds the volume of a thin or coarse closed solid, and the area of an open surface, would pin it.
2. CPU and CUDA can take different paths.
The fallback to the input only runs when Warp raises one of _COLLAPSE_ERRORS, and whether it raises depends on the device. For a single right triangle at remesh(3), CPU clusters the refined mesh and returns a triangle of area 0.087. CUDA raises a collapse error, falls back to the input, and returns the original area of 0.5. Both outcomes repeat every run. On refined inputs, results also differ between devices, and on CUDA from run to run: the 2% plate at 250 clusters keeps 4.9% on CPU and 30.6% ± 2.9 over three CUDA runs, while main gives 100% every time. The result check from concern 1 would make both devices take the same path. A dedicated exception type would also be safer than matching error strings.
3. The repair can return an empty or open mesh without raising.
- Empty mesh: a closed box 1e-6 thick at
remesh(4)comes back with 0 points and 0 cells, and no error. The fold loop cuts every face on an edge shared by more than two faces, so nothing is left forsplit_nonmanifold_edges. It happens at 4 and 5 clusters for thicknesses from 1e-6 to 1e-4, on CPU and CUDA, and also for two coincident copies of a sphere.mainnever returns an empty mesh. Splitting those edges instead of cutting their faces, and raising the documentedRuntimeErrorwhen no cells remain, would fix both. - Open output: a zero-area slit that
fill_holesskips leaves a closed input open. Remeshing the 40,002-point fillet box at its own point count leaves 11 open edges on its bottom face. This one is narrow: I only hit it on CPU, for that box.stitch_t_junctions, or the slit handling inrepair_mesh, might close it. - Duplicate cells:
sphere.remesh(3)returns the same triangle twice, once in each winding ([[2, 0, 1], [0, 2, 1]]), on CPU and CUDA. Skipping fill faces that duplicate an existing cell would avoid this.
4. Small hardening (each about a line):
(x[a] + x[b]) / 2overflows near the dtype's maximum: point data of 1e308 becomes NaN. Usex[a] + 0.5 * (x[b] - x[a])..float()on edge vectors and normals disables refinement and the orientation vote at extreme scales (1e±20 or beyond) or at offsets around 1e10. Normal map coordinates are fine; I checked 5e6._fill_holespasses float16 points tofill_holes, whose area check overflows: a float16 mesh about 500 units across stays open. Widen the points first.- The face budget counts 3 new faces per split edge, but an edge shared by many faces adds more. Count face–edge incidences instead.
- Mask
source_faces < 0beforesource.cells[source_faces].
5. Docs and tests.
docs/api/mesh/remeshing.rststill describes the old pipeline, with "slightly fewer vertices" and every vertex projected.- Output vertex counts can be well off
n_clusters: 16.5% over on a thin plate, and as few as half on others. Hole-fill vertices sit off the surface and carry boundary means. The docs should say so. - The float16 test only checks finiteness: the quad's area drops from 16,384 to 3,900 and it still passes. The thin-strip test also passes on
main.
Requesting changes for 1–3, mainly 1; 4 and 5 can go in alongside.
Splitting spaces clusters evenly by area, so a plate thinner than the cluster spacing was welded shut: a 2 % plate at 250 clusters kept 5 % of its volume on the CPU, and a unit cube at 8 clusters 45 %. remesh now checks the refined result against the input: the area and, for a closed input, the volume within 5 %, and a closed input must come back closed and consistently wound. Otherwise it clusters the input's own vertices, as main does, so the plates, the cube and the open cylinder come back as on main, on both devices. refine=False skips the refinement, the repair and the check. The repair now runs only on the refined result. It cuts out faces on mis-wound edges only, and leaves edges of more than two faces to split_nonmanifold_edges, so it no longer empties a mesh. Midpoints are averaged as halves, edge lengths, normals and the orientation vote use float64, hole filling runs on float32 points, the face budget counts face-edge incidences, and source_faces < 0 gives no normal. Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
…slits On CUDA, the fillet box at 8,002 and 40,002 clusters kept one 5-edge loop open, which fill_holes skips as a slit of zero area, so the result check fell back to the unrefined input there while the CPU kept the refined one. A second fill without the area test closes such loops. stitch_t_junctions did not, since no vertex lies on the open edges. Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
…nd cylinder Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
The fallback and refine=False cluster the input's own vertices, as main does, but then wind the result consistently and like the source, which can re-wind some of main's cells. The docstring and CHANGELOG now say so, and that a result near the 5 % limit can take either path, on CUDA from run to run. The second fill_holes pass of fc52eca goes. Its claim did not hold: tolerance=0.0 still skips loops of exactly zero area, no test reached it, and the 40,002-point fillet box still kept 4 open edges on CUDA, so the result check fell back there. The float16 test now remeshes a closed fillet box 16384 across at x = 40000, whose refined result the check keeps: it checks the area and volume to within 5 % and a far lower face-area p99/p1 than refine=False gives. Before, the refined quad was rejected and the test passed only through the fallback. A _face_normals helper replaces three cross products, the fields are widened with one apply, _fill_holes returns early, and one comment on the refinement limits remains. Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
…p "as before"; the float16 test's comment says what it guards Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
…ts in remeshing.rst The remeshing page still said that cleanup can produce slightly fewer vertices than n_clusters, and that Warp projects every output vertex. It now says that hole filling can add vertices, how refinement, the repair and the result check work, what refine=False does, and that a vertex added by hole filling sits at its hole's centroid with its boundary's mean point data. Signed-off-by: Peter Sharpe <peterdsharpe@gmail.com>
|
/ok to test 926d55c |
|
Thanks @zhenping-nv for the careful review. The new commits address all five points, checked on the CPU and an RTX 4090. Your prototype isn't posted yet, so I wrote my own check. I'm happy to switch to yours.
|
Clarify conditions for clustering vertices in remesh.
|
/ok to test ae801c2 |
Description
Mesh.remeshclusters only the source's vertices. CAD exports often have large faces and slivers, which are long thin triangles. No cluster can land inside a large face, so the output stays uneven. Asked for as many clusters as the mesh has points,remeshreturns its input.remeshnow splits long source edges at their midpoints, the longest first. The target edge is the side of an equilateral triangle when the surface is cut into 2 ×n_clustersfaces. Splitting stops once every edge is at most half the target edge and the source has 4 points per cluster. It also stops before a round exceeds 256 faces per cluster or 2^22 faces (about 1 GiB). Midpoints lie on the original edges, so point data andresolution_fieldstill interpolate barycentrically on the original faces.For a watertight input,
remeshrepairs the refined result. It fills holes, and each new centroid takes its boundary's mean point data. Faces on mis-wound edges, which two faces run the same way, are cut out and refilled. An edge still shared by two sheets gets one copy per sheet.A check guards the default. The refined result must keep the input's area within 5%. A closed input must also keep its volume within 5% and come out closed and consistently wound. Otherwise, or if clustering leaves no faces,
remeshclusters the input's own vertices, asmaindoes. The new keywordrefine=Falsealways does. Every result, the fallback andrefine=Falseincluded, is then wound consistently and like the source, so some ofmain's cells can come out re-wound.Before and after
Each cell reads
main→ this PR. Quality columns are from the CPU, and times are on a CPU and an RTX 4090. Area CV is the standard deviation of face area over its mean. Area p99/p1 is the 99th over the 1st percentile of face area. Every output is closed and consistently wound.n_clustersWhere
n_clustersequals the point count,mainreturns its input. On the finer fillet box, fanned slivers use up the face budget before they are short. Its CV falls from 30 to 1.8, but p99/p1 rises to 200–364. At 8,002 clusters on CUDA, one slit stays open, so that row comes back as onmainthere.Even spacing welds features thinner than the cluster spacing. Refined, Text3D lost 20% of its area and 33% of its volume. A 2% thick plate at 250 clusters lost 75–100% of its volume, and an open cylinder 9–16% of its area. The check rejects all of these, so they come back as on
main. The check looks at the whole mesh, so a small thin feature in a large mesh can still be welded.In the table, kept results change area by at most 2.8% and volume by at most 1.3%. That margin holds only on these cases. On the CPU, the PR's own fillet box keeps its refined result at 30 clusters (volume -4.7%) and at 75–200. It falls back at 40–60. At 150 clusters its area change is -4.3% on the CPU and -5.1% on CUDA, so the two devices take different paths there. A result within about a point of 5% can take either path, and on CUDA from run to run.
Hole filling can add vertices beyond
n_clusters, such as 8,006 for 8,002. Refinement also runs on even meshes whenevern_clustersexceeds about a quarter of their points. The icosphere at 19,000 clusters then takes 2.2 s instead of 0.28 s on the CPU, for CV 0.34 → 0.22. Rejected attempts cost time too, such as 0.18 s instead of 0.02 s on Text3D.Tests
New tests remesh the fillet box at 250 clusters and at its point count, on the CPU and CUDA. They fail on
main. They check p99/p1, that the output is closed and consistently wound, and its volume. At the point count they also check point-data transfer andresolution_field. A parametrized test pins a 2% plate's volume, the unit cube's and the open cylinder's area. A float16 fillet box at x = 40000 must keep its refined result, with area and volume within 5%. Others cover the repair,refine=Falseand the thin-strip fallback.test/meshpasses on the CPU, andtest/mesh/remeshingon an RTX 4090.Checklist
Dependencies
None. #2195 also edits
physicsnemo/mesh/remeshing/_remeshing.py, so whichever of the two merges second takes a merge ofmain.Review Process
All PRs are reviewed by the PhysicsNeMo team before merging.
Depending on which files are changed, GitHub may automatically assign a maintainer for review.
We are also testing AI-based code review tools (e.g., Greptile), which may add automated comments with a confidence score.
This score reflects the AI’s assessment of merge readiness and is not a qualitative judgment of your work, nor is
it an indication that the PR will be accepted / rejected.
AI-generated feedback should be reviewed critically for usefulness.
You are not required to respond to every AI comment, but they are intended to help both authors and reviewers.
Please react to Greptile comments with 👍 or 👎 to provide feedback on their accuracy.