From 58bbdf84dda85f221ac53212a49e3df15c1ef6d4 Mon Sep 17 00:00:00 2001 From: jan Date: Thu, 5 Apr 2018 15:09:36 -0700 Subject: [PATCH] cleanups and add include_ouside to voxelize() --- chop/__init__.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ chop/shapes.py | 1 + chop/voxel.py | 50 ++++++++++++++++++++++++++++++-------------- 3 files changed, 90 insertions(+), 15 deletions(-) diff --git a/chop/__init__.py b/chop/__init__.py index bce2b1c..fb8aeed 100644 --- a/chop/__init__.py +++ b/chop/__init__.py @@ -1,3 +1,57 @@ +''' + +Library for accurate polyhedron clipping and anti-aliased voxelization. +Written with python + numpy. + +Most operations are performed using an ordered adjacency-list polyhedron represenation, + capable of holding multiple unconnected possibly non-convex components. + +To convert from a boundary representation, use + poly = build_polyhedron(vertex_list, face_list) +To convert back to a boundary representation, use + list_of_boundaries = boundary_from_poly(poly) + vertex_list_0 = list_of_boundaries[0]['vertices'] + face_list_0 = list_of_boundaries[0]['faces'] + +Basic operations include + plane = (normal_vector, signed_distance_to_origin) + same_side_as_normal, other_side = split(poly, plane) + same_side_as_normal = clip(poly, plane) + + volume = get_volume(poly) + volume, m_x, m_y, m_z = get_moments(poly, order=1) + +Anti-aliased voxelization is done on a non-uniform rectangular grid, specified by the cell + edge coordinates: + x_planes = numpy.linspace(-5, 5, 100) + y_planes = numpy.linspace(-10, 10, 5) + z_planes = [-3, 0, 1, 2, 3] + voxel_weights = voxelize(poly, [x_planes, y_planes, z_planes], order=0) + + +Helper functions include + poly = build_box(box_corners) + poly = build_tetrahedron(vertices) + plane_list = planes_from_box(box_corners) + plane_list = planes_from_tetrahedron(vertices) + + n = number_of_moments(order=3) + +-------------------------- + +In the polyhedron representation returned by the build_* functions and accepted by + most other functions (split, clip, get_volume, get_moments, voxelize), the + polyhedron is stored as a 3-vertex-connected, triply-linked graph, with each vertex's + neighbors listed in counterclockwise order when viewed from outside the polyhedron. TODO + Each graph-vertex (g-vertex) is connected to exactly 3 other g-vertices, and + + This + representation looks like + poly = {'neighbors': ndarray[Px3, int], 'vertices': ndarray[Px3, float]} + where poly['neighbors'] contains + +''' + from .util import poly_t, plane_t from .split import split, clip diff --git a/chop/shapes.py b/chop/shapes.py index 7a0feae..3b7817c 100644 --- a/chop/shapes.py +++ b/chop/shapes.py @@ -67,6 +67,7 @@ def planes_from_box(box_corners: numpy.ndarray) -> List[plane_t]: two diagonally-opposite corners of the box. :return: List of planes, facing inwards. ''' + box_corners = numpy.array(box_corners) faces = [ (( 0, 0, 1), -box_corners[0, 2]), (( 0, 0, -1), box_corners[1, 2]), diff --git a/chop/voxel.py b/chop/voxel.py index 57aadb7..79f91a0 100644 --- a/chop/voxel.py +++ b/chop/voxel.py @@ -2,7 +2,7 @@ from typing import Tuple, List import numpy from . import poly_t, plane_t -from .split import split +from .split import split, clip from .moments import get_moments, number_of_moments from .shapes import planes_from_box @@ -17,32 +17,52 @@ except NameError: @profile def voxelize(poly: poly_t, planes: List[numpy.ndarray], - order: int = 0 + include_outside: bool = False, + order: int = 0, ) -> numpy.ndarray: ''' 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]] - The output array has size [X+1, Y+1, Z+1, N], where N is the number of moments - corresponding to the requested order. The outer borders of the output correspond - to portions of the polyhedron that protruded outside the bounds of the provided grid. + 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. :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]] + :param include_outside: Whether to include cells corresponding to portions of the + polyhedron that fall outside of the grid. Default False. :param order: Polynomial order of moments to calculate. Default 0. - :return: (X + 1) x (Y + 1) x (Z + 1) x N array, where the outer borders of the - first three dimensions correspond to portions of the polyhedron that protruded - outside the outermost planes, and N is number_of_moments(order). + :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). ''' planes = [numpy.array(p) for p in planes] - grid_shape = numpy.array([e.size for e in planes], dtype=int) + 1 - dest_grid = numpy.zeros((*grid_shape, number_of_moments(order)), dtype=float) - if poly['vertices'].shape[0] == 0 or (grid_shape <= 0).any(): - return dest_grid + 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 slices = numpy.array([(0, e.size) for e in planes], dtype=int) stack = [(poly, slices)] @@ -59,7 +79,7 @@ def voxelize(poly: poly_t, if not num_planes_remaining.any(): moments = get_moments(poly, order) i, j, k = slices[:, 0] - 1 - dest_grid[i, j, k, :] = moments + grid[i, j, k, :] = moments continue # Recursion case: split along longest side @@ -70,12 +90,12 @@ def voxelize(poly: poly_t, plane = (numpy.roll([-1, 0, 0], axis), plane_pos) poly_left, poly_right = split(poly, plane) - slices_left = slices.copy() + slices_left = slices slices_right = slices.copy() slices_left[axis, 1] = plane_ind slices_right[axis, 0] = plane_ind + 1 stack.append((poly_left, slices_left)) stack.append((poly_right, slices_right)) - return dest_grid + return grid