From 9d7fd2af6babcdf6ff54e102e44e3560d378ddc3 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Tue, 30 Jul 2024 23:55:41 -0700 Subject: [PATCH] comment styles --- adze/__init__.py | 4 ++-- adze/boundary.py | 51 ++++++++++++++++++++++++----------------------- adze/moments.py | 30 ++++++++++++++-------------- adze/shapes.py | 16 +++++++-------- adze/split.py | 48 ++++++++++++++++++++++---------------------- adze/transform.py | 16 +++++++-------- adze/voxel.py | 4 ++-- 7 files changed, 85 insertions(+), 84 deletions(-) diff --git a/adze/__init__.py b/adze/__init__.py index 34a5a11..de3636f 100644 --- a/adze/__init__.py +++ b/adze/__init__.py @@ -1,4 +1,4 @@ -''' +""" adze is a library for accurate polyhedron clipping and anti-aliased voxelization. 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]} where poly['neighbors'] contains -''' +""" from .util import poly_t, plane_t diff --git a/adze/boundary.py b/adze/boundary.py index 73de8b2..c6aefcc 100644 --- a/adze/boundary.py +++ b/adze/boundary.py @@ -11,7 +11,7 @@ def planes_from_boundary( vertices: ArrayLike, faces: list[ArrayLike] ) -> list[plane_t]: - ''' + """ Generate a list of planes (normal, signed distance) from the faces of a boundary- representation polyhedron. @@ -22,7 +22,7 @@ def planes_from_boundary( Returns: List of planes, represented by (normal vector, signed distance to origin) tuples. - ''' + """ vertices = numpy.asarray(vertices) afaces = [numpy.asarray(ff) for ff in faces] planes = [] @@ -39,7 +39,7 @@ def planes_from_boundary( def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t: - ''' + """ Generate a 3-verted-connected Schlegel diagram representation corresponding to the provided boundary-representation polyhedron. @@ -50,7 +50,7 @@ def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t: Returns: Schelgel-representation polyhedron - ''' + """ vertices = numpy.array(vertices, dtype=float) 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') 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) for face in afaces: # 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} - ''' + """ Some vertices link more than three edges. We only allow three edges per vertex in the graph, so we need to make 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 the links to reflect their removal. - ''' + """ + # starting row for each original vertex start = numpy.roll(numpy.cumsum(edges_per_vertex), 1) 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) - ''' - 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) for face in afaces: # 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) num_filled_rows[face] += 1 - ''' - Fill columns (0, 2) with new vertex inds (next_row, prev_row) - to complete doubly-linked rings - ''' + # + # Fill columns (0, 2) with new vertex inds (next_row, prev_row) + # to complete doubly-linked rings + # for v in range(num_vertices): v0 = start[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, 2] = m + v0 - ''' - for each pair of 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) - ''' + # + # for each pair of 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) + # # Mapping from row to original vertex index 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[src_mask, 1] = numpy.where(dst_mask)[0][ma] - ''' + """ Remove first two duplicates of each vertex (they aren't needed) - ''' + """ nxt, third, prv = neighbors[start, :].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]]: - ''' + """ Generate a boundary represeentation corresponding to the provided 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) and 'faces' (list of ndarrays specifying the indices of vertices used for constructing each face). - ''' + """ neighbors = poly['neighbors'] vertices = poly['vertices'] diff --git a/adze/moments.py b/adze/moments.py index c3a81e9..0f7dcbc 100644 --- a/adze/moments.py +++ b/adze/moments.py @@ -6,7 +6,7 @@ from .util import find_faces def number_of_moments(order: int) -> int: - ''' + """ Returns the number of moments with a given order or below. Args: @@ -14,12 +14,12 @@ def number_of_moments(order: int) -> int: Returns: Number of moments with that order or fewer. - ''' + """ return ((order + 1) * (order + 2) * (order + 3)) // 6 def get_volume(poly: poly_t) -> float: - ''' + """ Calculates the volume of a polyhedron (0th moment). Args: @@ -27,12 +27,12 @@ def get_volume(poly: poly_t) -> float: Returns: Volume of the polyhedron. - ''' + """ return get_moments(poly, 0)[0] def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]: - ''' + """ Calculates the moments of a polyhedron, up to the specified polynomial order. Args: @@ -41,7 +41,7 @@ def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]: Returns: Moments, increasing polynomial order: [1, x, y, z, xx, xy, xz, yy, yz, zz, xxx, xxy, xxz, ...] - ''' + """ vertices = poly['vertices'] neighbors = poly['neighbors'] num_verts = vertices.shape[0] @@ -49,9 +49,9 @@ def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]: if num_verts == 0: return numpy.array([0], dtype=float) - ''' - Decompose polyhedron into triangle fans - ''' + # + # Decompose polyhedron into triangle fans + # faces = find_faces(neighbors) triangle_fans = [] @@ -67,9 +67,9 @@ def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]: triverts = vertices[triangle_inds[:ilen]] triangle_fans.append(triverts) - ''' - Calculate the 0th-order moment - ''' + # + # Calculate the 0th-order moment + # triangles = numpy.vstack(triangle_fans) volumes = -numpy.linalg.det(triangles) moment0 = numpy.array([volumes.sum() / 6]) @@ -77,9 +77,9 @@ def get_moments(poly: poly_t, order: int) -> NDArray[numpy.float64]: if order == 0: return moment0 - ''' - Iteratively calculate higher-order moments, as per Koehl (2012). - ''' + # + # Iteratively calculate higher-order moments, as per Koehl (2012). + # prevlayer, curlayer = 0, 1 inds = numpy.arange(order + 1) mx, my = numpy.meshgrid(inds, inds, indexing='ij') diff --git a/adze/shapes.py b/adze/shapes.py index ef313c2..89f24aa 100644 --- a/adze/shapes.py +++ b/adze/shapes.py @@ -6,7 +6,7 @@ from . import poly_t, plane_t def build_tetrahedron(vertices: ArrayLike) -> poly_t: - ''' + """ Build a Schelegel-representation tetrahedron. Args: @@ -14,7 +14,7 @@ def build_tetrahedron(vertices: ArrayLike) -> poly_t: Returns: Tetrahedron in Schelgel representation. - ''' + """ poly = { 'vertices': numpy.array(vertices, dtype=float), 'neighbors': numpy.array(( @@ -27,7 +27,7 @@ def build_tetrahedron(vertices: ArrayLike) -> poly_t: def build_box(box_corners: ArrayLike) -> poly_t: - ''' + """ Build a Schelegel-representation box. Args: @@ -36,7 +36,7 @@ def build_box(box_corners: ArrayLike) -> poly_t: Returns: Box in Schelgel representation. - ''' + """ neighbors = [ [1, 4, 3], [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]: - ''' + """ Construct a list of planes corresponding to the faces of a box. Args: @@ -76,7 +76,7 @@ def planes_from_box(box_corners: ArrayLike) -> list[plane_t]: Returns: List of planes, facing inwards. - ''' + """ box_corners = numpy.asarray(box_corners) faces = [ (( 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]: - ''' + """ Construct a list of planes corresponding to the faces of a tetrahedron. Args: @@ -99,7 +99,7 @@ def planes_from_tetrahedron(vertices: ArrayLike) -> list[plane_t]: Returns: List of planes, facing inwards. - ''' + """ verts = numpy.asarray(vertices, dtype=float) vset_inds = numpy.array(( [3, 1, 2, 1], diff --git a/adze/split.py b/adze/split.py index b43310f..6b2be0b 100644 --- a/adze/split.py +++ b/adze/split.py @@ -5,7 +5,7 @@ from .util import build_empty, _get_map def clip(poly: poly_t, plane: plane_t) -> poly_t: - ''' + """ Clip a polyhedron with a plane, keeping only the part which is on the same side as the plane's normal vector. @@ -17,13 +17,13 @@ def clip(poly: poly_t, plane: plane_t) -> poly_t: Returns: Clipped polyhedron. - ''' + """ left_poly = _split(poly, plane, same_only=True) return left_poly def split(poly: poly_t, plane: plane_t) -> tuple[poly_t, poly_t]: - ''' + """ Clip a polyhedron with a plane, keeping both parts. 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: same_side_poly, other_side_poly The first polyhedron is on from the same side as the plane's normal vector. - ''' + """ return _split(poly, plane, same_only=False) def _split(poly: poly_t, plane: plane_t, same_only: bool = False): - ''' + """ Clip a polyhedron with a plane. 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: (same_side_poly, other_side_poly) if same_only is False (default), otherwise only returns same_side_poly. - ''' + """ vertices = poly['vertices'] neighbors = poly['neighbors'] 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_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 all on right or touching plane 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] 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) target_edge_inds = numpy.empty((0, 1), dtype=int) 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) lverts = rverts.copy() else: - ''' - Some edges cross the plane - ''' + # + # Some edges cross the 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) 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 - ''' - figure out new vertex positions - ''' + # + # figure out new vertex positions + # d_right = dists[rverts][:, None] d_left = dists[lverts][:, None] 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 cur_verts = lverts 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) - ''' - map from old vertex ind to new - ''' + # + # map from old vertex ind to new + # 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 ind_map_left[left] = numpy.arange(num_left) diff --git a/adze/transform.py b/adze/transform.py index 6150d80..6b05130 100644 --- a/adze/transform.py +++ b/adze/transform.py @@ -6,7 +6,7 @@ from . import poly_t, plane_t def rotate(poly: poly_t, theta: float, axis: int) -> poly_t: - ''' + """ Rotate the polyhedron around an axis. Args: @@ -17,7 +17,7 @@ def rotate(poly: poly_t, theta: float, axis: int) -> poly_t: Returns: Rotated polyhedron. Data is altered in-place but also returned for convenience. - ''' + """ s, c = sin(theta), cos(theta) v0 = poly['vertices'][:, axis - 2].copy() 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: - ''' + """ Translate the polyhedron by a vector. Args: @@ -37,13 +37,13 @@ def translate(poly: poly_t, shift: ArrayLike) -> poly_t: Returns: Translated polyhedron. Data is altered in-place but also returned for convenience. - ''' + """ poly['vertices'] += shift return poly def scale(poly: poly_t, factor: ArrayLike | float) -> poly_t: - ''' + """ Scale the polyhedron. 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: Scaled polyhedron. Data is altered in-place but also returned for convenience. - ''' + """ poly['vertices'] *= factor return poly def affine(poly: poly_t, xform: ArrayLike) -> poly_t: - ''' + """ Perform arbitrary affine trasnformation on the polyhedron. Args: @@ -70,7 +70,7 @@ def affine(poly: poly_t, xform: ArrayLike) -> poly_t: Returns: Transformed polyhedron. Data is altered in-place but also returned for convenience. - ''' + """ xform = numpy.array(xform, copy=False) mat4x3 = xform[:, :3] mat4x1 = xform[:, 3] diff --git a/adze/voxel.py b/adze/voxel.py index 3e1e8fa..291c600 100644 --- a/adze/voxel.py +++ b/adze/voxel.py @@ -14,7 +14,7 @@ def voxelize( include_outside: bool = False, order: int = 0, ) -> NDArray[numpy.float64]: - ''' + """ 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 @@ -40,7 +40,7 @@ def voxelize( 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)`. - ''' + """ aplanes = [numpy.asarray(p) for p in planes] num_planes = numpy.array([e.size for e in aplanes], dtype=int)