Skip to content

Mesh.remesh: even out meshes with large faces and slivers, and close and orient the result - #2296

Merged
peterdsharpe merged 17 commits into
NVIDIA:mainfrom
peterdsharpe:pr/mesh-remesh-large-faces
Nov 26, 2026
Merged

peterdsharpe merged 17 commits into
NVIDIA:mainfrom
peterdsharpe:pr/mesh-remesh-large-faces

Conversation

@peterdsharpe

@peterdsharpe peterdsharpe commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Mesh.remesh clusters 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, remesh returns its input.

box = _fillet_box()  # defined in test/mesh/remeshing/test_remesh_api.py
out = box.remesh(box.n_points)
# main:    face-area p99/p1 714, the input unchanged
# this PR: face-area p99/p1 9.6, closed, consistently wound, outward

remesh now 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_clusters faces. 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 and resolution_field still interpolate barycentrically on the original faces.

For a watertight input, remesh repairs 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, remesh clusters the input's own vertices, as main does. The new keyword refine=False always does. Every result, the fallback and refine=False included, is then wound consistently and like the source, so some of main'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.

Mesh (points), n_clusters Area CV Area p99/p1 Volume change, this PR CPU s GPU s GPU peak MiB
Fillet box (514), 250 5.27 → 0.28 902 → 2.7 -1.3% 0.03 → 0.14 0.019 → 0.074 0 → 17
Fillet box (514), 514 7.64 → 0.35 714 → 9.6 -1.1% 0.03 → 0.27 0.016 → 0.110 9 → 34
Fillet box, 1000-point fillets (8,002), 4,000 21.5 → 2.78 7.3 → 364 0.0% 0.13 → 2.65 0.015 → 0.089 12 → 145
Fillet box, 1000-point fillets (8,002), 8,002 30.4 → 1.83 2.0 → 200 0.0% 0.41 → 4.93 0.019 → 0.355 15 → 408
Text3D "PhysicsNeMo" (858), 858 1.79 → 1.79 13,840 → 13,840 0.0% 0.02 → 0.18 0.023 → 0.185 9 → 14
PyVista ant (486), 486 1.99 → 1.99 130,857 → 130,857 0.0% 0.02 → 0.21 0.017 → 0.293 9 → 11
Icosphere (40,962), 19,000 0.34 → 0.22 4.6 → 2.8 0.0% 0.28 → 2.22 0.025 → 0.089 28 → 119

Where n_clusters equals the point count, main returns 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 on main there.

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 whenever n_clusters exceeds 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 and resolution_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=False and the thin-strip fallback. test/mesh passes on the CPU, and test/mesh/remeshing on an RTX 4090.

Checklist

Dependencies

None. #2195 also edits physicsnemo/mesh/remeshing/_remeshing.py, so whichever of the two merges second takes a merge of main.

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.

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>
@copy-pr-bot

copy-pr-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions

Copy link
Copy Markdown
Contributor

CODEOWNERS review map

Current for commit 498e95c3018f. An approval covers every file listed for that owner; one owner is sufficient for shared files.

⏳ @peterdsharpe — 2 file(s)
  • physicsnemo/mesh/remeshing/_remeshing.py
  • test/mesh/remeshing/test_remesh_api.py

No CODEOWNER

  • CHANGELOG.md

Comment /codeowners-info to refresh.

@peterdsharpe
peterdsharpe marked this pull request as ready for review November 19, 2026 14:31
@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in Greptile

[Medium impact] The changes affect callers of the mesh-remeshing API.

Fix midpoint arithmetic before merging so valid translated meshes still remesh successfully.

Findings

  1. P1 Finite coordinates overflow during splitting ▶

Summary

Mesh.remesh now splits long source edges before clustering, then repairs holes and matches the source's winding.

  • Mesh.remesh splits long edges before clustering.
  • Remeshed outputs get consistent winding and repair when the source is watertight.

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>

@zhenping-nv zhenping-nv left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

2fd6870f-a26c-4c72-b13c-2c3ea097440b

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 for split_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. main never returns an empty mesh. Splitting those edges instead of cutting their faces, and raising the documented RuntimeError when no cells remain, would fix both.
  • Open output: a zero-area slit that fill_holes skips 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 in repair_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]) / 2 overflows near the dtype's maximum: point data of 1e308 becomes NaN. Use x[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_holes passes float16 points to fill_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 < 0 before source.cells[source_faces].

5. Docs and tests.

  • docs/api/mesh/remeshing.rst still 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>
@peterdsharpe

Copy link
Copy Markdown
Collaborator Author

/ok to test 926d55c

@peterdsharpe

Copy link
Copy Markdown
Collaborator Author

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.

  1. remesh now keeps the refined result only if it keeps the input's area within 5%. A closed input must also keep its volume within 5% and come out closed and consistently wound. Otherwise, and always with refine=False, it clusters the input's own vertices as main does. Either way it then winds the result like the source, which can re-wind some of main's cells. Your plates, cube and open cylinder now match main on both devices, and a new test pins them. Kept results in the PR body change area by up to 2.8% and volume by up to 1.3%. The closest rejection, the cylinder at 250 clusters, loses 9.3–9.6% of its area. Near 5% the path can still flip. At 150 clusters the fillet box's area changes by -4.3% on the CPU and -5.1% on CUDA, so only the CPU keeps it. The docstring now says so.

  2. For your cases, both devices now take the same path. The triangle at remesh(3) returns area 0.5. The 2% plate at 250 clusters keeps 100% on the CPU and in 3 CUDA runs. The one exception is the 8,002- and 40,002-point fillet boxes at their point counts. There CUDA can leave a slit open and falls back to the input. I'd leave a dedicated exception type for a follow-up, since it needs a Warp backend change.

  3. The fold loop now cuts out only faces on edges that two faces run the same way. split_nonmanifold_edges handles edges of more than two faces. An empty result fails the check and falls back instead of raising. The 1e-6 box at remesh(4) returns 2 cells. sphere.remesh(3) returns one triangle, because the check rejects the doubled one. A doubled pair inside a large mesh would pass, so I can add a duplicate filter if you'd like. stitch_t_junctions can't close the slit, since no vertex lies on its edges, and a second fill_holes pass didn't either. The check rejects the open result instead.

  4. All five are in. Midpoints are x[a] * 0.5 + x[b] * 0.5, since x[b] - x[a] can overflow when the signs differ.

  5. The docstring now covers vertex counts, hole-fill vertices, the check and refine. The float16 test remeshes a box at x = 40000 whose refined result is kept, within 5% of its area and volume. The thin-strip test guards the new collapse fallback, so it can't fail on main. remeshing.rst now describes the new pipeline too.

Clarify conditions for clustering vertices in remesh.
@peterdsharpe

Copy link
Copy Markdown
Collaborator Author

/ok to test ae801c2

@peterdsharpe
peterdsharpe added this pull request to the merge queue Nov 26, 2026
Merged via the queue into NVIDIA:main with commit cc242c5 Nov 26, 2026
13 checks passed
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.

3 participants