|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import enum |
| 4 | + |
| 5 | +import pytest |
| 6 | +import torch |
| 7 | + |
| 8 | +import torchhull |
| 9 | + |
| 10 | +try: |
| 11 | + import pytorch3d.ops.marching_cubes |
| 12 | + |
| 13 | + pytorch3d_available = True |
| 14 | +except ImportError: |
| 15 | + pytorch3d_available = False |
| 16 | + |
| 17 | + |
| 18 | +DEVICE = torch.device("cuda") |
| 19 | + |
| 20 | + |
| 21 | +class TensorType(enum.Enum): |
| 22 | + DENSE = enum.auto() |
| 23 | + SPARSE = enum.auto() |
| 24 | + |
| 25 | + def __str__(self) -> str: |
| 26 | + return f"{self.name}" |
| 27 | + |
| 28 | + |
| 29 | +class ImplementationType(enum.Enum): |
| 30 | + TORCHHULL = enum.auto() |
| 31 | + PYTORCH3D = enum.auto() |
| 32 | + |
| 33 | + def __str__(self) -> str: |
| 34 | + return f"{self.name}" |
| 35 | + |
| 36 | + |
| 37 | +def sdf_sphere(center: torch.Tensor, radius: float, samples: torch.Tensor) -> torch.Tensor: |
| 38 | + return torch.linalg.norm(samples - torch.unsqueeze(center, 0), dim=1) - radius |
| 39 | + |
| 40 | + |
| 41 | +def run_marching_cubes(sdf: torch.Tensor, implementation_type: ImplementationType) -> tuple[torch.Tensor, torch.Tensor]: |
| 42 | + if implementation_type == ImplementationType.TORCHHULL: |
| 43 | + v, f = torchhull.marching_cubes(sdf, isolevel=0, return_local_coords=False) |
| 44 | + verts, faces = [], [] |
| 45 | + verts.append(v) |
| 46 | + faces.append(f) |
| 47 | + elif implementation_type == ImplementationType.PYTORCH3D and pytorch3d_available: |
| 48 | + verts, faces = pytorch3d.ops.marching_cubes.marching_cubes(sdf, isolevel=0, return_local_coords=False) |
| 49 | + else: |
| 50 | + verts, faces = [], [] |
| 51 | + verts.append(torch.empty([0, 3], dtype=torch.float32, device=DEVICE)) |
| 52 | + faces.append(torch.empty([0, 3], dtype=torch.int64, device=DEVICE)) |
| 53 | + return verts[0], faces[0] |
| 54 | + |
| 55 | + |
| 56 | +def list_sizes_mc() -> list[int]: |
| 57 | + return [100, 200, 300, 400, 500, 600] |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize("size", list_sizes_mc()) |
| 61 | +@pytest.mark.parametrize( |
| 62 | + ("implementation_type", "tensor_type"), |
| 63 | + [ |
| 64 | + pytest.param( |
| 65 | + ImplementationType.PYTORCH3D, |
| 66 | + TensorType.DENSE, |
| 67 | + marks=pytest.mark.skipif(not pytorch3d_available, reason="PyTorch3D not available"), |
| 68 | + ), |
| 69 | + (ImplementationType.TORCHHULL, TensorType.DENSE), |
| 70 | + (ImplementationType.TORCHHULL, TensorType.SPARSE), |
| 71 | + ], |
| 72 | +) |
| 73 | +def test_marching_cubes( |
| 74 | + benchmark, # noqa: ANN001 |
| 75 | + size: int, |
| 76 | + implementation_type: ImplementationType, |
| 77 | + tensor_type: TensorType, |
| 78 | +) -> None: |
| 79 | + torch.cuda.empty_cache() |
| 80 | + |
| 81 | + grid_1d = torch.arange(size, device=DEVICE) |
| 82 | + |
| 83 | + grid_verts = torch.stack( |
| 84 | + torch.meshgrid(3 * [grid_1d], indexing="ij"), |
| 85 | + dim=0, |
| 86 | + ) # 3 x size x size x size |
| 87 | + grid_verts = grid_verts.reshape([3, -1]).T # size^3 x 3 |
| 88 | + |
| 89 | + center = torch.full((3,), size / 2, dtype=torch.float32, device=DEVICE) |
| 90 | + radius = 0.8731945 * (size / 2) # Use very uneven fraction to avoid zero values at the vertices |
| 91 | + sdf = sdf_sphere(center, radius, grid_verts) |
| 92 | + |
| 93 | + sdf = sdf.reshape([1, *(3 * [size])]) |
| 94 | + |
| 95 | + if tensor_type == TensorType.SPARSE: |
| 96 | + truncation_distance = 2 # max(3, 0.01 * max(sdf.shape)) |
| 97 | + sdf[sdf.abs() > truncation_distance] = 0 |
| 98 | + |
| 99 | + sdf = sdf.to_sparse() |
| 100 | + |
| 101 | + # Warmup |
| 102 | + run_marching_cubes(sdf=sdf, implementation_type=implementation_type) |
| 103 | + |
| 104 | + benchmark(run_marching_cubes, sdf=sdf, implementation_type=implementation_type) |
0 commit comments