Skip to content

Commit

Permalink
c++ marching cubes fix
Browse files Browse the repository at this point in the history
Summary:
Fixes #1641. The bug was caused by the mistaken downcasting of an int64_t into int, causing issues only on inputs large enough to have hashes that escaped the bounds of an int32.

Also added a test case for this issue.

Reviewed By: bottler

Differential Revision: D53505370

fbshipit-source-id: 0fdd0efc6d259cc3b0263e7ff3a4ab2c648ec521
  • Loading branch information
adaGrad1 authored and facebook-github-bot committed Feb 8, 2024
1 parent d0d9cae commit c292c71
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pytorch3d/csrc/marching_cubes/marching_cubes_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ std::tuple<at::Tensor, at::Tensor, at::Tensor> MarchingCubesCpu(
if ((j + 1) % 3 == 0 && ps[0] != ps[1] && ps[1] != ps[2] &&
ps[2] != ps[0]) {
for (int k = 0; k < 3; k++) {
int v = tri[k];
edge_id_to_v[tri.at(k)] = ps.at(k);
int64_t v = tri.at(k);
edge_id_to_v[v] = ps.at(k);
if (!uniq_edge_id.count(v)) {
uniq_edge_id[v] = verts.size();
verts.push_back(edge_id_to_v[v]);
Expand Down
12 changes: 12 additions & 0 deletions tests/test_marching_cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,18 @@ def test_double_ellipsoid(self):
self.assertClose(verts2[0], expected_verts)
self.assertClose(faces2[0], expected_faces)

def test_single_large_ellipsoid(self):
if USE_SCIKIT:
from skimage.draw import ellipsoid

ellip_base = ellipsoid(50, 60, 16, levelset=True)
volume = torch.Tensor(ellip_base).unsqueeze(0).cpu()
verts, faces = marching_cubes_naive(volume, 0)
verts2, faces2 = marching_cubes(volume, 0)

self.assertClose(verts[0], verts2[0], atol=1e-6)
self.assertClose(faces[0], faces2[0], atol=1e-6)

def test_cube_surface_area(self):
if USE_SCIKIT:
from skimage.measure import marching_cubes_classic, mesh_surface_area
Expand Down

3 comments on commit c292c71

@Khoa-NT
Copy link

Choose a reason for hiding this comment

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

Hello @adaGrad1 ,
Thank you for fixing this bug. However, After I saved the object
save_obj('marching_cubes_cuda.obj', verts2[0], faces2[0])

I got this result from meshlab
image
image

@adaGrad1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @Khoa-NT -- thanks for letting me know you're running into this. To be clear -- this happens when you're running marching_cubes on GPU? I think I may have been running into something similar. Seems like it's worth opening up a separate issue?

@Khoa-NT
Copy link

Choose a reason for hiding this comment

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

I created an issue #1731

Please sign in to comment.