2018-03-30 16:14:03 -07:00
|
|
|
from typing import Tuple, List
|
2018-03-20 15:10:45 -07:00
|
|
|
import numpy
|
|
|
|
|
|
2018-03-28 14:58:38 -07:00
|
|
|
from . import poly_t, plane_t
|
2018-04-05 15:09:36 -07:00
|
|
|
from .split import split, clip
|
2018-03-21 14:54:32 -07:00
|
|
|
from .moments import get_moments, number_of_moments
|
2018-03-30 16:14:03 -07:00
|
|
|
from .shapes import planes_from_box
|
2018-03-20 15:10:45 -07:00
|
|
|
|
|
|
|
|
try:
|
2018-03-28 10:54:15 -07:00
|
|
|
# Check if running with line_profiler
|
2018-03-20 15:10:45 -07:00
|
|
|
profile
|
|
|
|
|
except NameError:
|
|
|
|
|
def profile(f):
|
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@profile
|
2018-03-28 14:58:38 -07:00
|
|
|
def voxelize(poly: poly_t,
|
2018-03-30 16:14:03 -07:00
|
|
|
planes: List[numpy.ndarray],
|
2018-04-05 15:09:36 -07:00
|
|
|
include_outside: bool = False,
|
|
|
|
|
order: int = 0,
|
2018-03-28 14:58:38 -07:00
|
|
|
) -> numpy.ndarray:
|
|
|
|
|
'''
|
2018-03-30 16:14:03 -07:00
|
|
|
Voxelize a Schelegel-representation polyhedron on an arbitrary rectangular grid.
|
|
|
|
|
|
|
|
|
|
The grid is specified by supplying 3 sets of axis-aligned planes, in the form
|
|
|
|
|
[[x0, x1, x2, ..., xX], [y0, y1, y2, ..., yY], [z0, z1, ..., zZ]]
|
2018-04-05 15:09:36 -07:00
|
|
|
The output array has size [X-1, Y-1, Z-1, N] (include_outside False) or
|
|
|
|
|
[X+1, Y+1, Z+1, N] (include_outside True), where N is the number of moments
|
|
|
|
|
corresponding to the requested order. If include_outside is true, the outer borders
|
|
|
|
|
of the first three dimensions of the output correspond to portions of the polyhedron
|
|
|
|
|
that protruded outside the provided grid.
|
2018-03-30 16:14:03 -07:00
|
|
|
|
|
|
|
|
:param poly: Polyhedron to voxelize.
|
|
|
|
|
:param planes: List of coordinate lists, specifying the axis-aligned planes
|
|
|
|
|
which form the voxel grid:
|
|
|
|
|
[[x0, x1, ..., xX], [y0, y1, ..., yY], [z0, z1, ..., zZ]]
|
2018-04-05 15:09:36 -07:00
|
|
|
:param include_outside: Whether to include cells corresponding to portions of the
|
|
|
|
|
polyhedron that fall outside of the grid. Default False.
|
2018-03-30 16:14:03 -07:00
|
|
|
:param order: Polynomial order of moments to calculate. Default 0.
|
2018-04-05 15:09:36 -07:00
|
|
|
:return: (X - 1) x (Y - 1) x (Z - 1) x N (include_outside False) or
|
|
|
|
|
(X + 1) x (Y + 1) x (Z + 1) x N (include_outside True) ndarray.
|
|
|
|
|
If include_outside is true, the outer borders of the first three dimensions
|
|
|
|
|
correspond to portions of the polyhedron that protruded outside the outermost
|
|
|
|
|
planes. N is number_of_moments(order).
|
2018-03-28 14:58:38 -07:00
|
|
|
'''
|
2018-03-30 16:14:03 -07:00
|
|
|
planes = [numpy.array(p) for p in planes]
|
2018-03-20 15:10:45 -07:00
|
|
|
|
2018-04-05 15:09:36 -07:00
|
|
|
num_planes = numpy.array([e.size for e in planes], dtype=int)
|
|
|
|
|
if (num_planes < 2).any():
|
|
|
|
|
# TODO raw exception
|
|
|
|
|
raise Exception(f'Need at least two planes per dimension. Got {num_planes} planes.')
|
|
|
|
|
|
|
|
|
|
if include_outside:
|
|
|
|
|
grid_shape = num_planes + 1
|
|
|
|
|
if not include_outside:
|
|
|
|
|
grid_shape = num_planes - 1
|
|
|
|
|
for face in planes_from_box([[p[0] for p in planes],
|
|
|
|
|
[p[-1] for p in planes]]):
|
|
|
|
|
poly = clip(poly, face)
|
|
|
|
|
|
|
|
|
|
grid = numpy.zeros((*grid_shape, number_of_moments(order)), dtype=float)
|
|
|
|
|
|
|
|
|
|
if poly['vertices'].shape[0] == 0:
|
|
|
|
|
return grid
|
2018-03-20 15:10:45 -07:00
|
|
|
|
2018-03-30 16:14:03 -07:00
|
|
|
slices = numpy.array([(0, e.size) for e in planes], dtype=int)
|
|
|
|
|
stack = [(poly, slices)]
|
2018-03-20 15:10:45 -07:00
|
|
|
while stack:
|
2018-03-30 16:14:03 -07:00
|
|
|
poly, slices = stack.pop()
|
2018-03-20 15:10:45 -07:00
|
|
|
|
2018-03-30 16:14:03 -07:00
|
|
|
# Base case 1: No vertices in this region, moments are 0
|
|
|
|
|
if poly['vertices'].shape[0] == 0:
|
2018-03-20 15:10:45 -07:00
|
|
|
continue
|
|
|
|
|
|
2018-03-30 16:14:03 -07:00
|
|
|
num_planes_remaining = numpy.diff(slices, axis=1)
|
2018-03-20 15:10:45 -07:00
|
|
|
|
|
|
|
|
# Base case 2: calculate moments for single voxel
|
2018-03-30 16:14:03 -07:00
|
|
|
if not num_planes_remaining.any():
|
|
|
|
|
moments = get_moments(poly, order)
|
|
|
|
|
i, j, k = slices[:, 0] - 1
|
2018-04-05 15:09:36 -07:00
|
|
|
grid[i, j, k, :] = moments
|
2018-03-20 15:10:45 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# Recursion case: split along longest side
|
2018-03-30 16:14:03 -07:00
|
|
|
axis = num_planes_remaining.argmax()
|
|
|
|
|
plane_ind = num_planes_remaining[axis] // 2 + slices[axis, 0]
|
|
|
|
|
plane_pos = planes[axis][plane_ind]
|
2018-03-20 15:10:45 -07:00
|
|
|
|
2018-03-30 16:14:03 -07:00
|
|
|
plane = (numpy.roll([-1, 0, 0], axis), plane_pos)
|
2018-03-30 16:52:18 -07:00
|
|
|
poly_left, poly_right = split(poly, plane)
|
2018-03-20 15:10:45 -07:00
|
|
|
|
2018-04-05 15:09:36 -07:00
|
|
|
slices_left = slices
|
2018-03-30 16:14:03 -07:00
|
|
|
slices_right = slices.copy()
|
|
|
|
|
slices_left[axis, 1] = plane_ind
|
|
|
|
|
slices_right[axis, 0] = plane_ind + 1
|
2018-03-20 15:10:45 -07:00
|
|
|
|
2018-03-30 16:52:18 -07:00
|
|
|
stack.append((poly_left, slices_left))
|
|
|
|
|
stack.append((poly_right, slices_right))
|
2018-04-05 15:09:36 -07:00
|
|
|
return grid
|
2018-03-20 15:10:45 -07:00
|
|
|
|