diff --git a/adze/test/test_voxel.py b/adze/test/test_voxel.py index fa0df4d..255d4b1 100644 --- a/adze/test/test_voxel.py +++ b/adze/test/test_voxel.py @@ -47,6 +47,19 @@ def test_voxelization(poly_order: int, grid_resolution: int | None = None) -> No warnings.warn('Moments failed tolerance in voxels', stacklevel=1) +def test_voxelization_include_outside_keeps_axis_assignment() -> None: + poly = adze.build_box([[-1, 0, 0], [2, 1, 1]]) + edges = [numpy.array([0.0, 1.0]) for _ in range(3)] + + grid = adze.voxelize(poly, edges, include_outside=True, order=0) + voxels = grid[..., 0] + + assert voxels.shape == (3, 3, 3) + assert_allclose(voxels.sum(axis=(1, 2)), [1.0, 1.0, 1.0]) + assert_allclose(voxels.sum(axis=(0, 2)), [0.0, 3.0, 0.0]) + assert_allclose(voxels.sum(axis=(0, 1)), [0.0, 3.0, 0.0]) + + def visualize_isosurface( grid: NDArray[numpy.float64], edges: NDArray[numpy.int64], diff --git a/adze/voxel.py b/adze/voxel.py index a2e09cb..ac84d76 100644 --- a/adze/voxel.py +++ b/adze/voxel.py @@ -50,8 +50,10 @@ def voxelize( if include_outside: grid_shape = num_planes + 1 - if not include_outside: + grid_offset = 0 + else: grid_shape = num_planes - 1 + grid_offset = -1 for face in planes_from_box([[p[0] for p in aplanes], [p[-1] for p in aplanes]]): poly = clip(poly, face) @@ -75,7 +77,7 @@ def voxelize( # Base case 2: calculate moments for single voxel if not num_planes_remaining.any(): moments = get_moments(poly, order) - i, j, k = slices[:, 0] - 1 + i, j, k = slices[:, 0] + grid_offset grid[i, j, k, :] = moments continue @@ -95,4 +97,3 @@ def voxelize( stack.append((poly_left, slices_left)) stack.append((poly_right, slices_right)) return grid -