Skip to content

Fix loop variable shadowing in marching_cubes_mesh#120

Open
Mr-Neutr0n wants to merge 1 commit intoopenai:mainfrom
Mr-Neutr0n:fix/pc-to-mesh-loop-variable-shadowing
Open

Fix loop variable shadowing in marching_cubes_mesh#120
Mr-Neutr0n wants to merge 1 commit intoopenai:mainfrom
Mr-Neutr0n:fix/pc-to-mesh-loop-variable-shadowing

Conversation

@Mr-Neutr0n
Copy link

Summary

In marching_cubes_mesh(), the outer loop iterator indices (a range object used in for i in indices) is reassigned to a torch.arange tensor on the first line of the loop body (line 54). This shadows the loop variable, causing the for-loop to exit after processing only the first batch.

As a result, only the first batch_size voxels (default 4096) out of grid_size**3 (default 128^3 = 2,097,152) are ever evaluated. The SDF volume is mostly uninitialized, producing a corrupt mesh output.

Fix: Rename the inner variable from indices to batch_indices to avoid the name collision with the loop iterator.

Before (bug)

for i in indices:
    indices = torch.arange(...)   # shadows the loop iterator
    zs = int_coord_to_float(indices % grid_size)
    ...

After (fix)

for i in indices:
    batch_indices = torch.arange(...)
    zs = int_coord_to_float(batch_indices % grid_size)
    ...

In marching_cubes_mesh(), the outer loop iterator `indices` (a range
object) is reassigned to a torch.arange tensor on the first line of the
loop body. This shadows the loop variable, causing the for-loop to
exit after only the first batch iteration.

As a result, only the first `batch_size` voxels out of `grid_size**3`
are evaluated, producing an incomplete SDF volume and a corrupt mesh.

Rename the inner variable to `batch_indices` to avoid the collision.
@valfonsoardila
Copy link

valfonsoardila commented Feb 11, 2026 via email

@valfonsoardila
Copy link

valfonsoardila commented Feb 11, 2026 via email

@valfonsoardila
Copy link

valfonsoardila commented Feb 11, 2026 via email

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.

2 participants