modify function docstring style
This commit is contained in:
parent
cd0ca442b2
commit
a7bb9a4fa7
8 changed files with 137 additions and 79 deletions
|
|
@ -20,10 +20,13 @@ def planes_from_boundary(vertices: numpy.ndarray,
|
||||||
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.
|
||||||
|
|
||||||
:param vertices: Nx3 ndarray specifying the polyhedron's vertex positions.
|
Args:
|
||||||
:param faces: List of 1d ndarrays. Each ndarray is a list of vertex indices,
|
vertices: Nx3 ndarray specifying the polyhedron's vertex positions.
|
||||||
|
faces: List of 1d ndarrays. Each ndarray is a list of vertex indices,
|
||||||
specifying a face.
|
specifying a face.
|
||||||
:return: List of planes, represented by (normal vector, signed distance to origin) tuples.
|
|
||||||
|
Returns:
|
||||||
|
List of planes, represented by (normal vector, signed distance to origin) tuples.
|
||||||
'''
|
'''
|
||||||
planes = []
|
planes = []
|
||||||
for face in faces:
|
for face in faces:
|
||||||
|
|
@ -44,10 +47,13 @@ def build_polyhedron(vertices: numpy.ndarray, faces: numpy.ndarray) -> 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.
|
||||||
|
|
||||||
:param vertices: Nx3 ndarray specifying the polyhedron's vertex positions.
|
Args:
|
||||||
:param faces: List of 1d ndarrays. Each ndarray is a list of vertex indices,
|
vertices: Nx3 ndarray specifying the polyhedron's vertex positions.
|
||||||
|
faces: List of 1d ndarrays. Each ndarray is a list of vertex indices,
|
||||||
specifying a face.
|
specifying a face.
|
||||||
:return: Schelgel-representation polyhedron
|
|
||||||
|
Returns:
|
||||||
|
Schelgel-representation polyhedron
|
||||||
'''
|
'''
|
||||||
vertices = numpy.array(vertices, dtype=float)
|
vertices = numpy.array(vertices, dtype=float)
|
||||||
faces = [numpy.array(face, dtype=int) for face in faces]
|
faces = [numpy.array(face, dtype=int) for face in faces]
|
||||||
|
|
@ -263,8 +269,11 @@ def boundary_from_poly(poly: poly_t) -> Dict:
|
||||||
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.
|
||||||
|
|
||||||
:param poly: Polyhedron to convert, in Schlegel representation.
|
Args:
|
||||||
:return: Dict containing 'vertices' (Nx3 ndarray of vertex locations)
|
poly: Polyhedron to convert, in Schlegel representation.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
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).
|
||||||
'''
|
'''
|
||||||
|
|
|
||||||
|
|
@ -9,15 +9,16 @@ try:
|
||||||
profile
|
profile
|
||||||
except NameError:
|
except NameError:
|
||||||
def profile(f):
|
def profile(f):
|
||||||
return f
|
|
||||||
|
|
||||||
|
|
||||||
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.
|
||||||
|
|
||||||
:param order: Maximum polynomial order for the moments.
|
Args:
|
||||||
:return: Number of moments with that order or fewer.
|
order: Maximum polynomial order for the moments.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Number of moments with that order or fewer.
|
||||||
'''
|
'''
|
||||||
return ((order + 1) * (order + 2) * (order + 3)) // 6
|
return ((order + 1) * (order + 2) * (order + 3)) // 6
|
||||||
|
|
||||||
|
|
@ -26,8 +27,11 @@ def get_volume(poly: poly_t) -> float:
|
||||||
'''
|
'''
|
||||||
Calculates the volume of a polyhedron (0th moment).
|
Calculates the volume of a polyhedron (0th moment).
|
||||||
|
|
||||||
:param poly: Polyhedron to evaluate, in Schlegel representation.
|
Args:
|
||||||
:return: Volume of the polyhedron.
|
poly: Polyhedron to evaluate, in Schlegel representation.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Volume of the polyhedron.
|
||||||
'''
|
'''
|
||||||
return get_moments(poly, 0)[0]
|
return get_moments(poly, 0)[0]
|
||||||
|
|
||||||
|
|
@ -37,9 +41,11 @@ def get_moments(poly: poly_t, order: int) -> numpy.ndarray:
|
||||||
'''
|
'''
|
||||||
Calculates the moments of a polyhedron, up to the specified polynomial order.
|
Calculates the moments of a polyhedron, up to the specified polynomial order.
|
||||||
|
|
||||||
:param poly: Polyhedron to evaluate, in Schlegel representation.
|
Args:
|
||||||
:param order: Maximum polynomial order for moments.
|
poly: Polyhedron to evaluate, in Schlegel representation.
|
||||||
:return: Moments, increasing polynomial order:
|
order: Maximum polynomial order for moments.
|
||||||
|
|
||||||
|
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']
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,11 @@ def build_tetrahedron(vertices: numpy.ndarray) -> poly_t:
|
||||||
'''
|
'''
|
||||||
Build a Schelegel-representation tetrahedron.
|
Build a Schelegel-representation tetrahedron.
|
||||||
|
|
||||||
:param vertices: 4x3 array, representing the vertex coordinates.
|
Args:
|
||||||
:return: Tetrahedron in Schelgel representation.
|
vertices: 4x3 array, representing the vertex coordinates.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tetrahedron in Schelgel representation.
|
||||||
'''
|
'''
|
||||||
poly = {
|
poly = {
|
||||||
'vertices': numpy.array(vertices, dtype=float),
|
'vertices': numpy.array(vertices, dtype=float),
|
||||||
|
|
@ -27,9 +30,12 @@ def build_box(box_corners: numpy.ndarray) -> poly_t:
|
||||||
'''
|
'''
|
||||||
Build a Schelegel-representation box.
|
Build a Schelegel-representation box.
|
||||||
|
|
||||||
:param box_corners: 2x3 array, representing the vertex coordinates of
|
Args:
|
||||||
|
box_corners: 2x3 array, representing the vertex coordinates of
|
||||||
two diagonally-opposite corners of the box.
|
two diagonally-opposite corners of the box.
|
||||||
:return: Box in Schelgel representation.
|
|
||||||
|
Returns:
|
||||||
|
Box in Schelgel representation.
|
||||||
'''
|
'''
|
||||||
neighbors = [
|
neighbors = [
|
||||||
[1, 4, 3],
|
[1, 4, 3],
|
||||||
|
|
@ -63,9 +69,12 @@ def planes_from_box(box_corners: numpy.ndarray) -> 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.
|
||||||
|
|
||||||
:param box_corners: 2x3 array, representing the vertex coordinates of
|
Args:
|
||||||
|
box_corners: 2x3 array, representing the vertex coordinates of
|
||||||
two diagonally-opposite corners of the box.
|
two diagonally-opposite corners of the box.
|
||||||
:return: List of planes, facing inwards.
|
|
||||||
|
Returns:
|
||||||
|
List of planes, facing inwards.
|
||||||
'''
|
'''
|
||||||
box_corners = numpy.array(box_corners)
|
box_corners = numpy.array(box_corners)
|
||||||
faces = [
|
faces = [
|
||||||
|
|
@ -84,8 +93,11 @@ def planes_from_tetrahedron(vertices: numpy.ndarray) -> 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.
|
||||||
|
|
||||||
:param vertices: 4x3 array, representing the vertex coordinates.
|
Args:
|
||||||
:return: List of planes, facing inwards.
|
vertices: 4x3 array, representing the vertex coordinates.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of planes, facing inwards.
|
||||||
'''
|
'''
|
||||||
verts = numpy.array(vertices, dtype=float)
|
verts = numpy.array(vertices, dtype=float)
|
||||||
vset_inds = numpy.array((
|
vset_inds = numpy.array((
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,12 @@ def clip(poly: poly_t, plane: plane_t) -> poly_t:
|
||||||
|
|
||||||
This function is equivalent to calling split(poly, plane, same_only=True).
|
This function is equivalent to calling split(poly, plane, same_only=True).
|
||||||
|
|
||||||
:param poly: Polyhedron to clip, in chiral Schlegel representation.
|
Args:
|
||||||
:param plane: Plane to clip against.
|
poly: Polyhedron to clip, in chiral Schlegel representation.
|
||||||
:return: Clipped polyhedron.
|
plane: Plane to clip against.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Clipped polyhedron.
|
||||||
'''
|
'''
|
||||||
left_poly = split(poly, plane, same_only=True)
|
left_poly = split(poly, plane, same_only=True)
|
||||||
return left_poly
|
return left_poly
|
||||||
|
|
@ -35,11 +38,13 @@ def split(poly, plane, same_only: bool = False):
|
||||||
where same_side_poly is on the same side as the plane's normal vector. Otherwise,
|
where same_side_poly is on the same side as the plane's normal vector. Otherwise,
|
||||||
returns only same_side_poly (somewhat faster).
|
returns only same_side_poly (somewhat faster).
|
||||||
|
|
||||||
:param poly: Polyhedron to clip, in chiral Schlegel representation.
|
Args:
|
||||||
:param plane: Plane to clip against.
|
poly: Polyhedron to clip, in chiral Schlegel representation.
|
||||||
:param same_only: If True, only returns the clipped poly from the same side as the
|
plane: Plane to clip against.
|
||||||
|
same_only: If True, only returns the clipped poly from the same side as the
|
||||||
plane's normal vector. Default False.
|
plane's normal vector. Default False.
|
||||||
:return: (same_side_poly, other_side_poly) if same_only is False (default), otherwise
|
Returns:
|
||||||
|
(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']
|
||||||
|
|
|
||||||
|
|
@ -50,9 +50,10 @@ def visualize_isosurface(grid,
|
||||||
"""
|
"""
|
||||||
Draw an isosurface plot of the device.
|
Draw an isosurface plot of the device.
|
||||||
|
|
||||||
:param level: Value at which to find isosurface. Default (None) uses mean value in grid.
|
Args:
|
||||||
:param show_edges: Whether to draw triangle edges. Default True
|
level: Value at which to find isosurface. Default (None) uses mean value in grid.
|
||||||
:param finalize: Whether to call pyplot.show() after constructing the plot. Default True
|
show_edges: Whether to draw triangle edges. Default True
|
||||||
|
finalize: Whether to call pyplot.show() after constructing the plot. Default True
|
||||||
"""
|
"""
|
||||||
from matplotlib import pyplot
|
from matplotlib import pyplot
|
||||||
import skimage.measure
|
import skimage.measure
|
||||||
|
|
|
||||||
|
|
@ -8,10 +8,13 @@ def rotate(poly: poly_t, theta: float, axis: int) -> poly_t:
|
||||||
'''
|
'''
|
||||||
Rotate the polyhedron around an axis.
|
Rotate the polyhedron around an axis.
|
||||||
|
|
||||||
:param poly: Polyhedron to rotate. Changed in-place.
|
Args:
|
||||||
:param theta: Angle to rotate by (radians).
|
poly: Polyhedron to rotate. Changed in-place.
|
||||||
:param axis: Axis to rotate around (0-2).
|
theta: Angle to rotate by (radians).
|
||||||
:return: Rotated polyhedron.
|
axis: Axis to rotate around (0-2).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
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)
|
||||||
|
|
@ -26,9 +29,12 @@ def translate(poly: poly_t, shift: numpy.ndarray) -> poly_t:
|
||||||
'''
|
'''
|
||||||
Translate the polyhedron by a vector.
|
Translate the polyhedron by a vector.
|
||||||
|
|
||||||
:param poly: Polyhedron to translate. Changed in-place.
|
Args:
|
||||||
:param shift: Vector to translate by.
|
poly: Polyhedron to translate. Changed in-place.
|
||||||
:return: Translated polyhedron.
|
shift: Vector to translate by.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
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
|
||||||
|
|
@ -40,9 +46,12 @@ def scale(poly: poly_t, factor: numpy.ndarray or 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
|
||||||
|
|
||||||
:param poly: Polyhedron to scale. Changed in-place.
|
Args:
|
||||||
:param factor: Scalar or vector to scale by.
|
poly: Polyhedron to scale. Changed in-place.
|
||||||
:return: Scaled polyhedron.
|
factor: Scalar or vector to scale by.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
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
|
||||||
|
|
@ -53,9 +62,12 @@ def affine(poly: poly_t, xform: numpy.ndarray) -> poly_t:
|
||||||
'''
|
'''
|
||||||
Perform arbitrary affine trasnformation on the polyhedron.
|
Perform arbitrary affine trasnformation on the polyhedron.
|
||||||
|
|
||||||
:param poly: Polyhedron to transform. Changed in-place.
|
Args:
|
||||||
:param xform: 4x4 affine transformation matrix.
|
poly: Polyhedron to transform. Changed in-place.
|
||||||
:return: Transformed polyhedron.
|
xform: 4x4 affine transformation matrix.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Transformed polyhedron.
|
||||||
Data is altered in-place but also returned for convenience.
|
Data is altered in-place but also returned for convenience.
|
||||||
'''
|
'''
|
||||||
mat4x3 = xform[:, :3]
|
mat4x3 = xform[:, :3]
|
||||||
|
|
|
||||||
28
adze/util.py
28
adze/util.py
|
|
@ -17,7 +17,8 @@ def build_empty() -> poly_t:
|
||||||
'''
|
'''
|
||||||
Build an empty Schlegel representation (containing no vertices).
|
Build an empty Schlegel representation (containing no vertices).
|
||||||
|
|
||||||
:return: Empty Schelegel representation.
|
Returns:
|
||||||
|
Empty Schelegel representation.
|
||||||
'''
|
'''
|
||||||
return {
|
return {
|
||||||
'vertices': numpy.empty((0, 3), dtype=float),
|
'vertices': numpy.empty((0, 3), dtype=float),
|
||||||
|
|
@ -30,10 +31,13 @@ def find_components(neighbors: numpy.ndarray) -> numpy.ndarray:
|
||||||
'''
|
'''
|
||||||
Given an adjacency list, flood-fill to find and label connected components.
|
Given an adjacency list, flood-fill to find and label connected components.
|
||||||
|
|
||||||
:param neighbors: NxM adjacency list. Each of the N vertices is assumed
|
Args:
|
||||||
|
neighbors: NxM adjacency list. Each of the N vertices is assumed
|
||||||
to have the same number of neighbors, which are specified by their
|
to have the same number of neighbors, which are specified by their
|
||||||
indices.
|
indices.
|
||||||
:return: 1D ndarray with N entries. Entries are integer labels denoting
|
|
||||||
|
Returns:
|
||||||
|
1D ndarray with N entries. Entries are integer labels denoting
|
||||||
which connected component the vertex belongs to.
|
which connected component the vertex belongs to.
|
||||||
'''
|
'''
|
||||||
num_vertices = neighbors.shape[0]
|
num_vertices = neighbors.shape[0]
|
||||||
|
|
@ -61,9 +65,12 @@ def find_faces(neighbors: numpy.ndarray) -> List[numpy.ndarray]:
|
||||||
graph with exactly 3 neighbors per vertex. Neighbors are ordered
|
graph with exactly 3 neighbors per vertex. Neighbors are ordered
|
||||||
counter-clockwise.
|
counter-clockwise.
|
||||||
|
|
||||||
:param neighbors: Nx3 adjacency list. Neighbors are specified by their
|
Args:
|
||||||
|
neighbors: Nx3 adjacency list. Neighbors are specified by their
|
||||||
indices.
|
indices.
|
||||||
:return: List of ndarrays, each of which contains the indices in a given
|
|
||||||
|
Returns:
|
||||||
|
List of ndarrays, each of which contains the indices in a given
|
||||||
face, starting with the lowest index in the face.
|
face, starting with the lowest index in the face.
|
||||||
'''
|
'''
|
||||||
num_verts = neighbors.shape[0]
|
num_verts = neighbors.shape[0]
|
||||||
|
|
@ -215,10 +222,13 @@ def check_poly(poly: poly_t, check_connectivity: bool = True):
|
||||||
- has exactly 3 edges per vertex
|
- has exactly 3 edges per vertex
|
||||||
- is 3-vertex-connected (if check_connectivity is True; slow!)
|
- is 3-vertex-connected (if check_connectivity is True; slow!)
|
||||||
|
|
||||||
:param poly: Polyhedron to check.
|
Args:
|
||||||
:param check_connectivity: Whether to check for 3-vertex-connectedness.
|
poly: Polyhedron to check.
|
||||||
This is slow! Default True.
|
check_connectivity: Whether to check for 3-vertex-connectedness.
|
||||||
:raises: Exception if the polyhedron fails a check
|
This is slow! Default `True`.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Exception if the polyhedron fails a check
|
||||||
'''
|
'''
|
||||||
neighbors = poly['neighbors']
|
neighbors = poly['neighbors']
|
||||||
vertices = poly['vertices']
|
vertices = poly['vertices']
|
||||||
|
|
|
||||||
|
|
@ -24,25 +24,28 @@ def voxelize(poly: poly_t,
|
||||||
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
|
||||||
[[x0, x1, x2, ..., xX], [y0, y1, y2, ..., yY], [z0, z1, ..., zZ]]
|
`[[x0, x1, x2, ..., xX], [y0, y1, y2, ..., yY], [z0, z1, ..., zZ]]`
|
||||||
The output array has size [X-1, Y-1, Z-1, N] (include_outside False) or
|
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
|
`[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
|
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
|
of the first three dimensions of the output correspond to portions of the polyhedron
|
||||||
that protruded outside the provided grid.
|
that protruded outside the provided grid.
|
||||||
|
|
||||||
:param poly: Polyhedron to voxelize.
|
Args:
|
||||||
:param planes: List of coordinate lists, specifying the axis-aligned planes
|
poly: Polyhedron to voxelize.
|
||||||
|
planes: List of coordinate lists, specifying the axis-aligned planes
|
||||||
which form the voxel grid:
|
which form the voxel grid:
|
||||||
[[x0, x1, ..., xX], [y0, y1, ..., yY], [z0, z1, ..., zZ]]
|
`[[x0, x1, ..., xX], [y0, y1, ..., yY], [z0, z1, ..., zZ]]`
|
||||||
:param include_outside: Whether to include cells corresponding to portions of the
|
include_outside: Whether to include cells corresponding to portions of the
|
||||||
polyhedron that fall outside of the grid. Default False.
|
polyhedron that fall outside of the grid. Default `False`.
|
||||||
:param order: Polynomial order of moments to calculate. Default 0.
|
order: Polynomial order of moments to calculate. Default `0`.
|
||||||
: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.
|
Returns:
|
||||||
If include_outside is true, the outer borders of the first three dimensions
|
(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
|
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)`.
|
||||||
'''
|
'''
|
||||||
planes = [numpy.array(p) for p in planes]
|
planes = [numpy.array(p) for p in planes]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue