For a signed distance field, produce a mesh using marching cubes. :param field: a 3D tensor of field values, where negative values correspond to the outside of the shape. The dimensions correspond to the x, y, and z directions, respectively. :param min_p
(self, field: torch.Tensor, min_point: torch.Tensor, size: torch.Tensor)
| 496 | self.register_buffer("masks", masks) |
| 497 | |
| 498 | def forward(self, field: torch.Tensor, min_point: torch.Tensor, size: torch.Tensor): |
| 499 | """ |
| 500 | For a signed distance field, produce a mesh using marching cubes. |
| 501 | |
| 502 | :param field: a 3D tensor of field values, where negative values correspond |
| 503 | to the outside of the shape. The dimensions correspond to the x, y, and z directions, respectively. |
| 504 | :param min_point: a tensor of shape [3] containing the point corresponding |
| 505 | to (0, 0, 0) in the field. |
| 506 | :param size: a tensor of shape [3] containing the per-axis distance from the |
| 507 | (0, 0, 0) field corner and the (-1, -1, -1) field corner. |
| 508 | """ |
| 509 | assert len(field.shape) == 3, "input must be a 3D scalar field" |
| 510 | dev = field.device |
| 511 | |
| 512 | cases = self.cases.to(dev) |
| 513 | masks = self.masks.to(dev) |
| 514 | |
| 515 | min_point = min_point.to(dev) |
| 516 | size = size.to(dev) |
| 517 | |
| 518 | grid_size = field.shape |
| 519 | grid_size_tensor = torch.tensor(grid_size).to(size) |
| 520 | |
| 521 | # Create bitmasks between 0 and 255 (inclusive) indicating the state |
| 522 | # of the eight corners of each cube. |
| 523 | bitmasks = (field > 0).to(torch.uint8) |
| 524 | bitmasks = bitmasks[:-1, :, :] | (bitmasks[1:, :, :] << 1) |
| 525 | bitmasks = bitmasks[:, :-1, :] | (bitmasks[:, 1:, :] << 2) |
| 526 | bitmasks = bitmasks[:, :, :-1] | (bitmasks[:, :, 1:] << 4) |
| 527 | |
| 528 | # Compute corner coordinates across the entire grid. |
| 529 | corner_coords = torch.empty(*grid_size, 3, device=dev, dtype=field.dtype) |
| 530 | corner_coords[range(grid_size[0]), :, :, 0] = torch.arange(grid_size[0], device=dev, dtype=field.dtype)[ |
| 531 | :, None, None |
| 532 | ] |
| 533 | corner_coords[:, range(grid_size[1]), :, 1] = torch.arange(grid_size[1], device=dev, dtype=field.dtype)[ |
| 534 | :, None |
| 535 | ] |
| 536 | corner_coords[:, :, range(grid_size[2]), 2] = torch.arange(grid_size[2], device=dev, dtype=field.dtype) |
| 537 | |
| 538 | # Compute all vertices across all edges in the grid, even though we will |
| 539 | # throw some out later. We have (X-1)*Y*Z + X*(Y-1)*Z + X*Y*(Z-1) vertices. |
| 540 | # These are all midpoints, and don't account for interpolation (which is |
| 541 | # done later based on the used edge midpoints). |
| 542 | edge_midpoints = torch.cat( |
| 543 | [ |
| 544 | ((corner_coords[:-1] + corner_coords[1:]) / 2).reshape(-1, 3), |
| 545 | ((corner_coords[:, :-1] + corner_coords[:, 1:]) / 2).reshape(-1, 3), |
| 546 | ((corner_coords[:, :, :-1] + corner_coords[:, :, 1:]) / 2).reshape(-1, 3), |
| 547 | ], |
| 548 | dim=0, |
| 549 | ) |
| 550 | |
| 551 | # Create a flat array of [X, Y, Z] indices for each cube. |
| 552 | cube_indices = torch.zeros( |
| 553 | grid_size[0] - 1, grid_size[1] - 1, grid_size[2] - 1, 3, device=dev, dtype=torch.long |
| 554 | ) |
| 555 | cube_indices[range(grid_size[0] - 1), :, :, 0] = torch.arange(grid_size[0] - 1, device=dev)[:, None, None] |
nothing calls this directly
no test coverage detected