comment styles
This commit is contained in:
parent
dbe915feba
commit
9d7fd2af6b
7 changed files with 85 additions and 84 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
'''
|
"""
|
||||||
adze is a library for accurate polyhedron clipping and anti-aliased voxelization.
|
adze is a library for accurate polyhedron clipping and anti-aliased voxelization.
|
||||||
Written with python + numpy.
|
Written with python + numpy.
|
||||||
|
|
||||||
|
|
@ -51,7 +51,7 @@ In the polyhedron representation returned by the build_* functions and accepted
|
||||||
poly = {'neighbors': ndarray[Px3, int], 'vertices': ndarray[Px3, float]}
|
poly = {'neighbors': ndarray[Px3, int], 'vertices': ndarray[Px3, float]}
|
||||||
where poly['neighbors'] contains
|
where poly['neighbors'] contains
|
||||||
|
|
||||||
'''
|
"""
|
||||||
|
|
||||||
from .util import poly_t, plane_t
|
from .util import poly_t, plane_t
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ def planes_from_boundary(
|
||||||
vertices: ArrayLike,
|
vertices: ArrayLike,
|
||||||
faces: list[ArrayLike]
|
faces: list[ArrayLike]
|
||||||
) -> list[plane_t]:
|
) -> list[plane_t]:
|
||||||
'''
|
"""
|
||||||
Generate a list of planes (normal, signed distance) from the faces of a boundary-
|
Generate a list of planes (normal, signed distance) from the faces of a boundary-
|
||||||
representation polyhedron.
|
representation polyhedron.
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ def planes_from_boundary(
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List of planes, represented by (normal vector, signed distance to origin) tuples.
|
List of planes, represented by (normal vector, signed distance to origin) tuples.
|
||||||
'''
|
"""
|
||||||
vertices = numpy.asarray(vertices)
|
vertices = numpy.asarray(vertices)
|
||||||
afaces = [numpy.asarray(ff) for ff in faces]
|
afaces = [numpy.asarray(ff) for ff in faces]
|
||||||
planes = []
|
planes = []
|
||||||
|
|
@ -39,7 +39,7 @@ def planes_from_boundary(
|
||||||
|
|
||||||
|
|
||||||
def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||||
'''
|
"""
|
||||||
Generate a 3-verted-connected Schlegel diagram representation
|
Generate a 3-verted-connected Schlegel diagram representation
|
||||||
corresponding to the provided boundary-representation polyhedron.
|
corresponding to the provided boundary-representation polyhedron.
|
||||||
|
|
||||||
|
|
@ -50,7 +50,7 @@ def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Schelgel-representation polyhedron
|
Schelgel-representation polyhedron
|
||||||
'''
|
"""
|
||||||
vertices = numpy.array(vertices, dtype=float)
|
vertices = numpy.array(vertices, dtype=float)
|
||||||
afaces = [numpy.array(face, dtype=int) for face in faces]
|
afaces = [numpy.array(face, dtype=int) for face in faces]
|
||||||
|
|
||||||
|
|
@ -60,9 +60,9 @@ def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||||
raise Exception(f'Vertex {edges_per_vertex.argmin()} has < 3 edges')
|
raise Exception(f'Vertex {edges_per_vertex.argmin()} has < 3 edges')
|
||||||
|
|
||||||
if edges_per_vertex.max() == 3:
|
if edges_per_vertex.max() == 3:
|
||||||
'''
|
#
|
||||||
In this case, we don't need to duplicate any vertices to make a 3-vertex-connected graph
|
# In this case, we don't need to duplicate any vertices to make a 3-vertex-connected graph
|
||||||
'''
|
#
|
||||||
neighbors = numpy.full_like(vertices, vertices.size, dtype=int)
|
neighbors = numpy.full_like(vertices, vertices.size, dtype=int)
|
||||||
for face in afaces:
|
for face in afaces:
|
||||||
# Check if each vertex in this face is already linked to its next or previous face-neighbor
|
# Check if each vertex in this face is already linked to its next or previous face-neighbor
|
||||||
|
|
@ -90,7 +90,7 @@ def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||||
|
|
||||||
return {'vertices': vertices, 'neighbors': neighbors}
|
return {'vertices': vertices, 'neighbors': neighbors}
|
||||||
|
|
||||||
'''
|
"""
|
||||||
Some vertices link more than three edges.
|
Some vertices link more than three edges.
|
||||||
We only allow three edges per vertex in the graph, so we need to make
|
We only allow three edges per vertex in the graph, so we need to make
|
||||||
degenerate copies of those vertices.
|
degenerate copies of those vertices.
|
||||||
|
|
@ -151,7 +151,8 @@ def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||||
|
|
||||||
Finally, we can remove the first two ('excess') duplicate vertices in each ring, and update
|
Finally, we can remove the first two ('excess') duplicate vertices in each ring, and update
|
||||||
the links to reflect their removal.
|
the links to reflect their removal.
|
||||||
'''
|
"""
|
||||||
|
|
||||||
# starting row for each original vertex
|
# starting row for each original vertex
|
||||||
start = numpy.roll(numpy.cumsum(edges_per_vertex), 1)
|
start = numpy.roll(numpy.cumsum(edges_per_vertex), 1)
|
||||||
start[0] = 0
|
start[0] = 0
|
||||||
|
|
@ -162,9 +163,9 @@ def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||||
|
|
||||||
neighbors = numpy.empty((num_duplicates, 3), dtype=int)
|
neighbors = numpy.empty((num_duplicates, 3), dtype=int)
|
||||||
|
|
||||||
'''
|
#
|
||||||
Fill columns (1, 2) with original vertex inds (f#_next, f#_prev)
|
# Fill columns (1, 2) with original vertex inds (f#_next, f#_prev)
|
||||||
'''
|
#
|
||||||
num_filled_rows = numpy.zeros(num_duplicates, dtype=int)
|
num_filled_rows = numpy.zeros(num_duplicates, dtype=int)
|
||||||
for face in afaces:
|
for face in afaces:
|
||||||
# assume a face can't use the same vertex twice
|
# assume a face can't use the same vertex twice
|
||||||
|
|
@ -173,10 +174,10 @@ def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||||
neighbors[v_inds, 2] = numpy.roll(face, 1)
|
neighbors[v_inds, 2] = numpy.roll(face, 1)
|
||||||
num_filled_rows[face] += 1
|
num_filled_rows[face] += 1
|
||||||
|
|
||||||
'''
|
#
|
||||||
Fill columns (0, 2) with new vertex inds (next_row, prev_row)
|
# Fill columns (0, 2) with new vertex inds (next_row, prev_row)
|
||||||
to complete doubly-linked rings
|
# to complete doubly-linked rings
|
||||||
'''
|
#
|
||||||
for v in range(num_vertices):
|
for v in range(num_vertices):
|
||||||
v0 = start[v]
|
v0 = start[v]
|
||||||
s = slice(v0, v0 + edges_per_vertex[v])
|
s = slice(v0, v0 + edges_per_vertex[v])
|
||||||
|
|
@ -184,11 +185,11 @@ def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||||
neighbors[s, 0] = n + v0
|
neighbors[s, 0] = n + v0
|
||||||
neighbors[s, 2] = m + v0
|
neighbors[s, 2] = m + v0
|
||||||
|
|
||||||
'''
|
#
|
||||||
for each pair of original vertices,
|
# for each pair of original vertices,
|
||||||
find subvertices whose f#_next pointers point to each other's original vertices,
|
# find subvertices whose f#_next pointers point to each other's original vertices,
|
||||||
and link them using column 1 (i.e. set their third_row pointers to each other)
|
# and link them using column 1 (i.e. set their third_row pointers to each other)
|
||||||
'''
|
#
|
||||||
# Mapping from row to original vertex index
|
# Mapping from row to original vertex index
|
||||||
v0s = numpy.repeat(numpy.arange(len(edges_per_vertex)), edges_per_vertex)
|
v0s = numpy.repeat(numpy.arange(len(edges_per_vertex)), edges_per_vertex)
|
||||||
|
|
||||||
|
|
@ -218,9 +219,9 @@ def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||||
neighbors[dst_mask, 1] = numpy.where(src_mask)[0][mb]
|
neighbors[dst_mask, 1] = numpy.where(src_mask)[0][mb]
|
||||||
neighbors[src_mask, 1] = numpy.where(dst_mask)[0][ma]
|
neighbors[src_mask, 1] = numpy.where(dst_mask)[0][ma]
|
||||||
|
|
||||||
'''
|
"""
|
||||||
Remove first two duplicates of each vertex (they aren't needed)
|
Remove first two duplicates of each vertex (they aren't needed)
|
||||||
'''
|
"""
|
||||||
nxt, third, prv = neighbors[start, :].T
|
nxt, third, prv = neighbors[start, :].T
|
||||||
nxt_nxt, nxt_third, _nxt_prv = neighbors[nxt, :].T
|
nxt_nxt, nxt_third, _nxt_prv = neighbors[nxt, :].T
|
||||||
|
|
||||||
|
|
@ -260,7 +261,7 @@ def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||||
|
|
||||||
|
|
||||||
def boundary_from_poly(poly: poly_t) -> list[dict[str, Any]]:
|
def boundary_from_poly(poly: poly_t) -> list[dict[str, Any]]:
|
||||||
'''
|
"""
|
||||||
Generate a boundary represeentation corresponding to the provided
|
Generate a boundary represeentation corresponding to the provided
|
||||||
3-vertex-connected Schlegel diagram.
|
3-vertex-connected Schlegel diagram.
|
||||||
|
|
||||||
|
|
@ -271,7 +272,7 @@ def boundary_from_poly(poly: poly_t) -> list[dict[str, Any]]:
|
||||||
Dict containing 'vertices' (Nx3 ndarray of vertex locations)
|
Dict containing 'vertices' (Nx3 ndarray of vertex locations)
|
||||||
and 'faces' (list of ndarrays specifying the indices of vertices
|
and 'faces' (list of ndarrays specifying the indices of vertices
|
||||||
used for constructing each face).
|
used for constructing each face).
|
||||||
'''
|
"""
|
||||||
neighbors = poly['neighbors']
|
neighbors = poly['neighbors']
|
||||||
vertices = poly['vertices']
|
vertices = poly['vertices']
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from .util import find_faces
|
||||||
|
|
||||||
|
|
||||||
def number_of_moments(order: int) -> int:
|
def number_of_moments(order: int) -> int:
|
||||||
'''
|
"""
|
||||||
Returns the number of moments with a given order or below.
|
Returns the number of moments with a given order or below.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -14,12 +14,12 @@ def number_of_moments(order: int) -> int:
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Number of moments with that order or fewer.
|
Number of moments with that order or fewer.
|
||||||
'''
|
"""
|
||||||
return ((order + 1) * (order + 2) * (order + 3)) // 6
|
return ((order + 1) * (order + 2) * (order + 3)) // 6
|
||||||
|
|
||||||
|
|
||||||
def get_volume(poly: poly_t) -> float:
|
def get_volume(poly: poly_t) -> float:
|
||||||
'''
|
"""
|
||||||
Calculates the volume of a polyhedron (0th moment).
|
Calculates the volume of a polyhedron (0th moment).
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -27,12 +27,12 @@ def get_volume(poly: poly_t) -> float:
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Volume of the polyhedron.
|
Volume of the polyhedron.
|
||||||
'''
|
"""
|
||||||
return get_moments(poly, 0)[0]
|
return get_moments(poly, 0)[0]
|
||||||
|
|
||||||
|
|
||||||
def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]:
|
def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]:
|
||||||
'''
|
"""
|
||||||
Calculates the moments of a polyhedron, up to the specified polynomial order.
|
Calculates the moments of a polyhedron, up to the specified polynomial order.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -41,7 +41,7 @@ def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]:
|
||||||
|
|
||||||
Returns: Moments, increasing polynomial order:
|
Returns: Moments, increasing polynomial order:
|
||||||
[1, x, y, z, xx, xy, xz, yy, yz, zz, xxx, xxy, xxz, ...]
|
[1, x, y, z, xx, xy, xz, yy, yz, zz, xxx, xxy, xxz, ...]
|
||||||
'''
|
"""
|
||||||
vertices = poly['vertices']
|
vertices = poly['vertices']
|
||||||
neighbors = poly['neighbors']
|
neighbors = poly['neighbors']
|
||||||
num_verts = vertices.shape[0]
|
num_verts = vertices.shape[0]
|
||||||
|
|
@ -49,9 +49,9 @@ def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]:
|
||||||
if num_verts == 0:
|
if num_verts == 0:
|
||||||
return numpy.array([0], dtype=float)
|
return numpy.array([0], dtype=float)
|
||||||
|
|
||||||
'''
|
#
|
||||||
Decompose polyhedron into triangle fans
|
# Decompose polyhedron into triangle fans
|
||||||
'''
|
#
|
||||||
faces = find_faces(neighbors)
|
faces = find_faces(neighbors)
|
||||||
|
|
||||||
triangle_fans = []
|
triangle_fans = []
|
||||||
|
|
@ -67,9 +67,9 @@ def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]:
|
||||||
triverts = vertices[triangle_inds[:ilen]]
|
triverts = vertices[triangle_inds[:ilen]]
|
||||||
triangle_fans.append(triverts)
|
triangle_fans.append(triverts)
|
||||||
|
|
||||||
'''
|
#
|
||||||
Calculate the 0th-order moment
|
# Calculate the 0th-order moment
|
||||||
'''
|
#
|
||||||
triangles = numpy.vstack(triangle_fans)
|
triangles = numpy.vstack(triangle_fans)
|
||||||
volumes = -numpy.linalg.det(triangles)
|
volumes = -numpy.linalg.det(triangles)
|
||||||
moment0 = numpy.array([volumes.sum() / 6])
|
moment0 = numpy.array([volumes.sum() / 6])
|
||||||
|
|
@ -77,9 +77,9 @@ def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]:
|
||||||
if order == 0:
|
if order == 0:
|
||||||
return moment0
|
return moment0
|
||||||
|
|
||||||
'''
|
#
|
||||||
Iteratively calculate higher-order moments, as per Koehl (2012).
|
# Iteratively calculate higher-order moments, as per Koehl (2012).
|
||||||
'''
|
#
|
||||||
prevlayer, curlayer = 0, 1
|
prevlayer, curlayer = 0, 1
|
||||||
inds = numpy.arange(order + 1)
|
inds = numpy.arange(order + 1)
|
||||||
mx, my = numpy.meshgrid(inds, inds, indexing='ij')
|
mx, my = numpy.meshgrid(inds, inds, indexing='ij')
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from . import poly_t, plane_t
|
||||||
|
|
||||||
|
|
||||||
def build_tetrahedron(vertices: ArrayLike) -> poly_t:
|
def build_tetrahedron(vertices: ArrayLike) -> poly_t:
|
||||||
'''
|
"""
|
||||||
Build a Schelegel-representation tetrahedron.
|
Build a Schelegel-representation tetrahedron.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -14,7 +14,7 @@ def build_tetrahedron(vertices: ArrayLike) -> poly_t:
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tetrahedron in Schelgel representation.
|
Tetrahedron in Schelgel representation.
|
||||||
'''
|
"""
|
||||||
poly = {
|
poly = {
|
||||||
'vertices': numpy.array(vertices, dtype=float),
|
'vertices': numpy.array(vertices, dtype=float),
|
||||||
'neighbors': numpy.array((
|
'neighbors': numpy.array((
|
||||||
|
|
@ -27,7 +27,7 @@ def build_tetrahedron(vertices: ArrayLike) -> poly_t:
|
||||||
|
|
||||||
|
|
||||||
def build_box(box_corners: ArrayLike) -> poly_t:
|
def build_box(box_corners: ArrayLike) -> poly_t:
|
||||||
'''
|
"""
|
||||||
Build a Schelegel-representation box.
|
Build a Schelegel-representation box.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -36,7 +36,7 @@ def build_box(box_corners: ArrayLike) -> poly_t:
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Box in Schelgel representation.
|
Box in Schelgel representation.
|
||||||
'''
|
"""
|
||||||
neighbors = [
|
neighbors = [
|
||||||
[1, 4, 3],
|
[1, 4, 3],
|
||||||
[2, 5, 0],
|
[2, 5, 0],
|
||||||
|
|
@ -67,7 +67,7 @@ def build_box(box_corners: ArrayLike) -> poly_t:
|
||||||
|
|
||||||
|
|
||||||
def planes_from_box(box_corners: ArrayLike) -> list[plane_t]:
|
def planes_from_box(box_corners: ArrayLike) -> list[plane_t]:
|
||||||
'''
|
"""
|
||||||
Construct a list of planes corresponding to the faces of a box.
|
Construct a list of planes corresponding to the faces of a box.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -76,7 +76,7 @@ def planes_from_box(box_corners: ArrayLike) -> list[plane_t]:
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List of planes, facing inwards.
|
List of planes, facing inwards.
|
||||||
'''
|
"""
|
||||||
box_corners = numpy.asarray(box_corners)
|
box_corners = numpy.asarray(box_corners)
|
||||||
faces = [
|
faces = [
|
||||||
(( 0, 0, 1), -box_corners[0, 2]),
|
(( 0, 0, 1), -box_corners[0, 2]),
|
||||||
|
|
@ -91,7 +91,7 @@ def planes_from_box(box_corners: ArrayLike) -> list[plane_t]:
|
||||||
|
|
||||||
|
|
||||||
def planes_from_tetrahedron(vertices: ArrayLike) -> list[plane_t]:
|
def planes_from_tetrahedron(vertices: ArrayLike) -> list[plane_t]:
|
||||||
'''
|
"""
|
||||||
Construct a list of planes corresponding to the faces of a tetrahedron.
|
Construct a list of planes corresponding to the faces of a tetrahedron.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -99,7 +99,7 @@ def planes_from_tetrahedron(vertices: ArrayLike) -> list[plane_t]:
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List of planes, facing inwards.
|
List of planes, facing inwards.
|
||||||
'''
|
"""
|
||||||
verts = numpy.asarray(vertices, dtype=float)
|
verts = numpy.asarray(vertices, dtype=float)
|
||||||
vset_inds = numpy.array((
|
vset_inds = numpy.array((
|
||||||
[3, 1, 2, 1],
|
[3, 1, 2, 1],
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ from .util import build_empty, _get_map
|
||||||
|
|
||||||
|
|
||||||
def clip(poly: poly_t, plane: plane_t) -> poly_t:
|
def clip(poly: poly_t, plane: plane_t) -> poly_t:
|
||||||
'''
|
"""
|
||||||
Clip a polyhedron with a plane, keeping only the part which is on
|
Clip a polyhedron with a plane, keeping only the part which is on
|
||||||
the same side as the plane's normal vector.
|
the same side as the plane's normal vector.
|
||||||
|
|
||||||
|
|
@ -17,13 +17,13 @@ def clip(poly: poly_t, plane: plane_t) -> poly_t:
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Clipped polyhedron.
|
Clipped polyhedron.
|
||||||
'''
|
"""
|
||||||
left_poly = _split(poly, plane, same_only=True)
|
left_poly = _split(poly, plane, same_only=True)
|
||||||
return left_poly
|
return left_poly
|
||||||
|
|
||||||
|
|
||||||
def split(poly: poly_t, plane: plane_t) -> tuple[poly_t, poly_t]:
|
def split(poly: poly_t, plane: plane_t) -> tuple[poly_t, poly_t]:
|
||||||
'''
|
"""
|
||||||
Clip a polyhedron with a plane, keeping both parts.
|
Clip a polyhedron with a plane, keeping both parts.
|
||||||
|
|
||||||
This function is equivalent to calling _split(poly, plane, same_only=False).
|
This function is equivalent to calling _split(poly, plane, same_only=False).
|
||||||
|
|
@ -34,12 +34,12 @@ def split(poly: poly_t, plane: plane_t) -> tuple[poly_t, poly_t]:
|
||||||
Returns:
|
Returns:
|
||||||
same_side_poly, other_side_poly
|
same_side_poly, other_side_poly
|
||||||
The first polyhedron is on from the same side as the plane's normal vector.
|
The first polyhedron is on from the same side as the plane's normal vector.
|
||||||
'''
|
"""
|
||||||
return _split(poly, plane, same_only=False)
|
return _split(poly, plane, same_only=False)
|
||||||
|
|
||||||
|
|
||||||
def _split(poly: poly_t, plane: plane_t, same_only: bool = False):
|
def _split(poly: poly_t, plane: plane_t, same_only: bool = False):
|
||||||
'''
|
"""
|
||||||
Clip a polyhedron with a plane.
|
Clip a polyhedron with a plane.
|
||||||
|
|
||||||
If same_only is False (default), returns the tuple (same_side_poly, other_side_poly),
|
If same_only is False (default), returns the tuple (same_side_poly, other_side_poly),
|
||||||
|
|
@ -54,7 +54,7 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False):
|
||||||
Returns:
|
Returns:
|
||||||
(same_side_poly, other_side_poly) if same_only is False (default), otherwise
|
(same_side_poly, other_side_poly) if same_only is False (default), otherwise
|
||||||
only returns same_side_poly.
|
only returns same_side_poly.
|
||||||
'''
|
"""
|
||||||
vertices = poly['vertices']
|
vertices = poly['vertices']
|
||||||
neighbors = poly['neighbors']
|
neighbors = poly['neighbors']
|
||||||
num_start = neighbors.shape[0]
|
num_start = neighbors.shape[0]
|
||||||
|
|
@ -66,9 +66,9 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False):
|
||||||
num_left = numpy.count_nonzero(left)
|
num_left = numpy.count_nonzero(left)
|
||||||
num_right = num_start - num_left
|
num_right = num_start - num_left
|
||||||
|
|
||||||
'''
|
#
|
||||||
if the poly lies entirely on one side of the plane, just return it
|
# if the poly lies entirely on one side of the plane, just return it
|
||||||
'''
|
#
|
||||||
if num_left == 0:
|
if num_left == 0:
|
||||||
# if all on right or touching plane
|
# if all on right or touching plane
|
||||||
ret = build_empty(), poly
|
ret = build_empty(), poly
|
||||||
|
|
@ -86,9 +86,9 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False):
|
||||||
far_vertex_left = left[next_vertex]
|
far_vertex_left = left[next_vertex]
|
||||||
|
|
||||||
if not far_vertex_left.any():
|
if not far_vertex_left.any():
|
||||||
'''
|
#
|
||||||
No edges cross the plane, but components exist on either side
|
# No edges cross the plane, but components exist on either side
|
||||||
'''
|
#
|
||||||
r2l_edges = numpy.empty((0, 3), dtype=int)
|
r2l_edges = numpy.empty((0, 3), dtype=int)
|
||||||
target_edge_inds = numpy.empty((0, 1), dtype=int)
|
target_edge_inds = numpy.empty((0, 1), dtype=int)
|
||||||
new_vertices = numpy.empty((0, 3), dtype=float)
|
new_vertices = numpy.empty((0, 3), dtype=float)
|
||||||
|
|
@ -97,9 +97,9 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False):
|
||||||
rverts = numpy.empty(0, dtype=int)
|
rverts = numpy.empty(0, dtype=int)
|
||||||
lverts = rverts.copy()
|
lverts = rverts.copy()
|
||||||
else:
|
else:
|
||||||
'''
|
#
|
||||||
Some edges cross the plane
|
# Some edges cross the plane
|
||||||
'''
|
#
|
||||||
# remove edges which don't also touch the left half-plane
|
# remove edges which don't also touch the left half-plane
|
||||||
which_edges_cross, rl_edge_inds = numpy.divmod(numpy.where(far_vertex_left)[0], 3)
|
which_edges_cross, rl_edge_inds = numpy.divmod(numpy.where(far_vertex_left)[0], 3)
|
||||||
rverts = right_inds[which_edges_cross]
|
rverts = right_inds[which_edges_cross]
|
||||||
|
|
@ -107,16 +107,16 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False):
|
||||||
|
|
||||||
num_new_vertices = rverts.size
|
num_new_vertices = rverts.size
|
||||||
|
|
||||||
'''
|
#
|
||||||
figure out new vertex positions
|
# figure out new vertex positions
|
||||||
'''
|
#
|
||||||
d_right = dists[rverts][:, None]
|
d_right = dists[rverts][:, None]
|
||||||
d_left = dists[lverts][:, None]
|
d_left = dists[lverts][:, None]
|
||||||
new_vertices = (d_right * vertices[lverts] - d_left * vertices[rverts]) / (d_right - d_left)
|
new_vertices = (d_right * vertices[lverts] - d_left * vertices[rverts]) / (d_right - d_left)
|
||||||
|
|
||||||
'''
|
#
|
||||||
walk to next crossing edge
|
# walk to next crossing edge
|
||||||
'''
|
#
|
||||||
prev_verts = rverts
|
prev_verts = rverts
|
||||||
cur_verts = lverts
|
cur_verts = lverts
|
||||||
edge_walk_inds = numpy.arange(num_new_vertices)
|
edge_walk_inds = numpy.arange(num_new_vertices)
|
||||||
|
|
@ -153,9 +153,9 @@ def _split(poly: poly_t, plane: plane_t, same_only: bool = False):
|
||||||
target_edge_inds = _get_map(a1d, b1d)
|
target_edge_inds = _get_map(a1d, b1d)
|
||||||
|
|
||||||
|
|
||||||
'''
|
#
|
||||||
map from old vertex ind to new
|
# map from old vertex ind to new
|
||||||
'''
|
#
|
||||||
ind_map_left = numpy.zeros(num_start, dtype=int)
|
ind_map_left = numpy.zeros(num_start, dtype=int)
|
||||||
# current side vertices get new (equal or lower) indices from delete operation, remain in same order
|
# current side vertices get new (equal or lower) indices from delete operation, remain in same order
|
||||||
ind_map_left[left] = numpy.arange(num_left)
|
ind_map_left[left] = numpy.arange(num_left)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from . import poly_t, plane_t
|
||||||
|
|
||||||
|
|
||||||
def rotate(poly: poly_t, theta: float, axis: int) -> poly_t:
|
def rotate(poly: poly_t, theta: float, axis: int) -> poly_t:
|
||||||
'''
|
"""
|
||||||
Rotate the polyhedron around an axis.
|
Rotate the polyhedron around an axis.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -17,7 +17,7 @@ def rotate(poly: poly_t, theta: float, axis: int) -> poly_t:
|
||||||
Returns:
|
Returns:
|
||||||
Rotated polyhedron.
|
Rotated polyhedron.
|
||||||
Data is altered in-place but also returned for convenience.
|
Data is altered in-place but also returned for convenience.
|
||||||
'''
|
"""
|
||||||
s, c = sin(theta), cos(theta)
|
s, c = sin(theta), cos(theta)
|
||||||
v0 = poly['vertices'][:, axis - 2].copy()
|
v0 = poly['vertices'][:, axis - 2].copy()
|
||||||
v1 = poly['vertices'][:, axis - 1]
|
v1 = poly['vertices'][:, axis - 1]
|
||||||
|
|
@ -27,7 +27,7 @@ def rotate(poly: poly_t, theta: float, axis: int) -> poly_t:
|
||||||
|
|
||||||
|
|
||||||
def translate(poly: poly_t, shift: ArrayLike) -> poly_t:
|
def translate(poly: poly_t, shift: ArrayLike) -> poly_t:
|
||||||
'''
|
"""
|
||||||
Translate the polyhedron by a vector.
|
Translate the polyhedron by a vector.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -37,13 +37,13 @@ def translate(poly: poly_t, shift: ArrayLike) -> poly_t:
|
||||||
Returns:
|
Returns:
|
||||||
Translated polyhedron.
|
Translated polyhedron.
|
||||||
Data is altered in-place but also returned for convenience.
|
Data is altered in-place but also returned for convenience.
|
||||||
'''
|
"""
|
||||||
poly['vertices'] += shift
|
poly['vertices'] += shift
|
||||||
return poly
|
return poly
|
||||||
|
|
||||||
|
|
||||||
def scale(poly: poly_t, factor: ArrayLike | float) -> poly_t:
|
def scale(poly: poly_t, factor: ArrayLike | float) -> poly_t:
|
||||||
'''
|
"""
|
||||||
Scale the polyhedron.
|
Scale the polyhedron.
|
||||||
Can scale by a single value, or scale each axis individually
|
Can scale by a single value, or scale each axis individually
|
||||||
|
|
||||||
|
|
@ -54,13 +54,13 @@ def scale(poly: poly_t, factor: ArrayLike | float) -> poly_t:
|
||||||
Returns:
|
Returns:
|
||||||
Scaled polyhedron.
|
Scaled polyhedron.
|
||||||
Data is altered in-place but also returned for convenience.
|
Data is altered in-place but also returned for convenience.
|
||||||
'''
|
"""
|
||||||
poly['vertices'] *= factor
|
poly['vertices'] *= factor
|
||||||
return poly
|
return poly
|
||||||
|
|
||||||
|
|
||||||
def affine(poly: poly_t, xform: ArrayLike) -> poly_t:
|
def affine(poly: poly_t, xform: ArrayLike) -> poly_t:
|
||||||
'''
|
"""
|
||||||
Perform arbitrary affine trasnformation on the polyhedron.
|
Perform arbitrary affine trasnformation on the polyhedron.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -70,7 +70,7 @@ def affine(poly: poly_t, xform: ArrayLike) -> poly_t:
|
||||||
Returns:
|
Returns:
|
||||||
Transformed polyhedron.
|
Transformed polyhedron.
|
||||||
Data is altered in-place but also returned for convenience.
|
Data is altered in-place but also returned for convenience.
|
||||||
'''
|
"""
|
||||||
xform = numpy.array(xform, copy=False)
|
xform = numpy.array(xform, copy=False)
|
||||||
mat4x3 = xform[:, :3]
|
mat4x3 = xform[:, :3]
|
||||||
mat4x1 = xform[:, 3]
|
mat4x1 = xform[:, 3]
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ def voxelize(
|
||||||
include_outside: bool = False,
|
include_outside: bool = False,
|
||||||
order: int = 0,
|
order: int = 0,
|
||||||
) -> NDArray[numpy.float64]:
|
) -> NDArray[numpy.float64]:
|
||||||
'''
|
"""
|
||||||
Voxelize a Schelegel-representation polyhedron on an arbitrary rectangular grid.
|
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
|
The grid is specified by supplying 3 sets of axis-aligned planes, in the form
|
||||||
|
|
@ -40,7 +40,7 @@ def voxelize(
|
||||||
If include_outside is True, the outer borders of the first three dimensions
|
If include_outside is True, the outer borders of the first three dimensions
|
||||||
correspond to portions of the polyhedron that protruded outside the outermost
|
correspond to portions of the polyhedron that protruded outside the outermost
|
||||||
planes. N is `number_of_moments(order)`.
|
planes. N is `number_of_moments(order)`.
|
||||||
'''
|
"""
|
||||||
aplanes = [numpy.asarray(p) for p in planes]
|
aplanes = [numpy.asarray(p) for p in planes]
|
||||||
|
|
||||||
num_planes = numpy.array([e.size for e in aplanes], dtype=int)
|
num_planes = numpy.array([e.size for e in aplanes], dtype=int)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue