improve type annotations
This commit is contained in:
parent
d5ed7a1298
commit
b957ac7abe
5 changed files with 49 additions and 30 deletions
|
|
@ -1,4 +1,5 @@
|
|||
from typing import Any
|
||||
from collections.abc import Iterable
|
||||
import numpy
|
||||
from numpy.linalg import norm
|
||||
from numpy.typing import ArrayLike, NDArray
|
||||
|
|
@ -38,7 +39,7 @@ def planes_from_boundary(
|
|||
return planes
|
||||
|
||||
|
||||
def build_polyhedron(vertices: ArrayLike, faces: list[ArrayLike]) -> poly_t:
|
||||
def build_polyhedron(vertices: ArrayLike, faces: Iterable[ArrayLike]) -> poly_t:
|
||||
"""
|
||||
Generate a 3-verted-connected Schlegel diagram representation
|
||||
corresponding to the provided boundary-representation polyhedron.
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
from typing import overload, Literal
|
||||
import numpy
|
||||
|
||||
from . import poly_t, plane_t
|
||||
|
|
@ -38,7 +39,22 @@ def split(poly: poly_t, plane: plane_t) -> tuple[poly_t, poly_t]:
|
|||
return _split(poly, plane, same_only=False)
|
||||
|
||||
|
||||
def _split(poly: poly_t, plane: plane_t, same_only: bool = False):
|
||||
@overload
|
||||
def _split(poly: poly_t, plane: plane_t, same_only: Literal[True]) -> poly_t:
|
||||
...
|
||||
|
||||
|
||||
@overload
|
||||
def _split(poly: poly_t, plane: plane_t, same_only: Literal[False]) -> tuple[poly_t, poly_t]:
|
||||
...
|
||||
|
||||
|
||||
@overload
|
||||
def _split(poly: poly_t, plane: plane_t, same_only: bool) -> tuple[poly_t, poly_t] | poly_t:
|
||||
...
|
||||
|
||||
|
||||
def _split(poly: poly_t, plane: plane_t, same_only: bool = False) -> tuple[poly_t, poly_t] | poly_t:
|
||||
"""
|
||||
Clip a polyhedron with a plane.
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import pytest
|
|||
from pytest import approx
|
||||
|
||||
import numpy
|
||||
from numpy.typing import NDArray
|
||||
from numpy import floating
|
||||
from numpy.linalg import norm
|
||||
from numpy.testing import assert_allclose
|
||||
|
||||
|
|
@ -66,7 +68,7 @@ def test_manual_pyramid(poly_order: int) -> None:
|
|||
]
|
||||
|
||||
# initialize a general polyhedron
|
||||
poly = adze.build_polyhedron(vertices, faces)
|
||||
poly = adze.build_polyhedron(vertices, faces) # type: ignore # (mypy error?)
|
||||
adze.util.check_poly(poly)
|
||||
|
||||
brep = adze.boundary_from_poly(poly)
|
||||
|
|
@ -141,13 +143,13 @@ def test_manual_nonconvex(poly_order: int, n_zigs: int = 3, z_offset: float = 0.
|
|||
check_split_moments(poly, poly_l, poly_r)
|
||||
|
||||
# split along the x-axis (two single connected components)
|
||||
split_and_check(poly, [numpy.array((1, 0, 0)), -0.5 * (n_zigs - 1)], poly_order)
|
||||
split_and_check(poly, (numpy.array((1, 0, 0)), -0.5 * (n_zigs - 1)), poly_order)
|
||||
|
||||
# split along the z-axis (two single connected components)
|
||||
split_and_check(poly, [numpy.array((0, 0, 1)), -0.5], poly_order)
|
||||
split_and_check(poly, (numpy.array((0, 0, 1)), -0.5), poly_order)
|
||||
|
||||
# split along the y-axis (multiple connected components)
|
||||
split_and_check(poly, [numpy.array((0, 1, 0)), -0.5 * (1.0 + z_offset)], poly_order)
|
||||
split_and_check(poly, (numpy.array((0, 1, 0)), -0.5 * (1.0 + z_offset)), poly_order)
|
||||
|
||||
|
||||
def test_torus(poly_order: int) -> None:
|
||||
|
|
@ -199,7 +201,7 @@ def check_split_moments(
|
|||
left_poly: adze.poly_t,
|
||||
right_poly: adze.poly_t,
|
||||
poly_order: int = 0,
|
||||
) -> None:
|
||||
) -> tuple[NDArray[floating], NDArray[floating], NDArray[floating]]:
|
||||
m0, ml, mr = (adze.get_moments(x, poly_order) for x in (original_poly, left_poly, right_poly))
|
||||
logger.debug(f'Check: original {m0[0]} parts {(ml[0], mr[0])} sum {ml[0] + mr[0]}')
|
||||
|
||||
|
|
@ -225,7 +227,7 @@ def check_split_moments(
|
|||
|
||||
def stack_split(
|
||||
poly: adze.poly_t,
|
||||
poly_order: adze.poly_t,
|
||||
poly_order: int,
|
||||
plane_fun: Callable | None = None,
|
||||
perturb: float = 0,
|
||||
max_depth: int = 10,
|
||||
|
|
@ -239,10 +241,10 @@ def stack_split(
|
|||
|
||||
# pick a plane
|
||||
if plane_fun is None:
|
||||
pfun_used = PRNG.choice(plane_generators)
|
||||
pfun_used = PRNG.choice(plane_generators) # type: ignore
|
||||
plane = pfun_used(poly)
|
||||
while norm(plane[0]) == 0:
|
||||
pfun_used = PRNG.choice(plane_generators)
|
||||
pfun_used = PRNG.choice(plane_generators) # type: ignore
|
||||
plane = pfun_used(poly)
|
||||
plane = list(plane)
|
||||
logger.debug(f'used {pfun_used}')
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
from typing import cast
|
||||
import numpy
|
||||
from numpy.typing import NDArray
|
||||
from numpy import pi, cos, sin, floating
|
||||
|
|
@ -61,7 +62,7 @@ def gen_torus_brep(
|
|||
return vertices, faces
|
||||
|
||||
|
||||
def tetrahedron_volume(vertices: NDArray[numpy.float64]) -> float:
|
||||
def tetrahedron_volume(vertices: NDArray[numpy.floating] | list[NDArray[numpy.floating]]) -> float:
|
||||
ad, bd, cd = vertices[0:3] - vertices[3]
|
||||
return -ad @ numpy.cross(bd, cd) / 6
|
||||
|
||||
|
|
@ -73,14 +74,14 @@ def flip_plane(plane: adze.plane_t) -> adze.plane_t:
|
|||
def rand_tetrahedron_vertices(min_volume: float = MIN_VOLUME) -> NDArray[numpy.float64]:
|
||||
# generates a random tetrahedron with vertices on the unit sphere,
|
||||
# guaranteeing a volume of at least min_volume
|
||||
volume = 0
|
||||
volume = 0.0
|
||||
while volume < min_volume:
|
||||
verts = [rand_unit_vector() for _ in range(4)]
|
||||
volume = tetrahedron_volume(verts)
|
||||
if volume < 0:
|
||||
verts[2], verts[3] = verts[3], verts[2]
|
||||
volume = -volume
|
||||
return verts
|
||||
return numpy.asarray(verts)
|
||||
|
||||
|
||||
def build_rand_tetrahedron(min_volume: float = MIN_VOLUME) -> adze.poly_t:
|
||||
|
|
@ -89,8 +90,8 @@ def build_rand_tetrahedron(min_volume: float = MIN_VOLUME) -> adze.poly_t:
|
|||
|
||||
def rand_unit_vector() -> NDArray[numpy.float64]:
|
||||
# generates a random, isotropically distributed unit vector
|
||||
s = 0
|
||||
while s == 0:
|
||||
s: numpy.floating | float = 0.0
|
||||
while s == 0.0:
|
||||
v = PRNG.randn(3)
|
||||
s = norm(v)
|
||||
return v / s
|
||||
|
|
@ -112,7 +113,7 @@ def gen_plane_through_centroid(
|
|||
"""
|
||||
centroid = get_centroid(poly)
|
||||
n = rand_unit_vector()
|
||||
d = -n @ centroid
|
||||
d = cast(numpy.floating, -n @ centroid)
|
||||
return n, d
|
||||
|
||||
|
||||
|
|
|
|||
27
adze/util.py
27
adze/util.py
|
|
@ -7,7 +7,7 @@ if TYPE_CHECKING:
|
|||
import stl
|
||||
|
||||
|
||||
plane_t = tuple[NDArray, float]
|
||||
plane_t = tuple[NDArray[numpy.floating], float | numpy.floating]
|
||||
poly_t = dict[str, NDArray]
|
||||
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ def find_components(neighbors: NDArray[numpy.integer]) -> NDArray[numpy.integer]
|
|||
return component - 1
|
||||
|
||||
|
||||
def find_faces(neighbors: NDArray[numpy.integer]) -> list[NDArray[numpy.integer]]:
|
||||
def find_faces(neighbors: NDArray[numpy.integer]) -> list[NDArray[numpy.intp]]:
|
||||
"""
|
||||
Traverse the Schlegel diagram to find chordless cycles
|
||||
(i.e., faces of the polyhedron). Assumes 3-vertex-connected
|
||||
|
|
@ -85,32 +85,31 @@ def find_faces(neighbors: NDArray[numpy.integer]) -> list[NDArray[numpy.integer]
|
|||
Choose a method for storing the face info based on expected
|
||||
runtime vs unnecessary memory allocation
|
||||
"""
|
||||
faces: list[list[NDArray[numpy.integer]]] | NDArray[numpy.integer]
|
||||
if num_verts < 1000:
|
||||
# ~3n^2 in memory, but much faster in cases where n isn't prohibitively large
|
||||
faces = numpy.full((3 * num_verts, num_verts - 1), -1, dtype=int)
|
||||
faces[:, 0] = v_current
|
||||
faces_arr = numpy.full((3 * num_verts, num_verts - 1), -1, dtype=int)
|
||||
faces_arr[:, 0] = v_current
|
||||
|
||||
def add_edges(edge_id: NDArray[numpy.intp], v_nxt: NDArray[numpy.intp], kk: int) -> None:
|
||||
faces[edge_id, kk] = v_nxt
|
||||
faces_arr[edge_id, kk] = v_nxt
|
||||
|
||||
def cleaned_faces(remove: NDArray[numpy.int8], kk: int) -> list[NDArray[numpy.intp]]:
|
||||
faces_set = []
|
||||
for row in faces[~remove, :kk]:
|
||||
for row in faces_arr[~remove, :kk]:
|
||||
faces_set.append(row[row >= 0])
|
||||
return faces_set
|
||||
else:
|
||||
# Alternative approach, less memory-intensive but slower
|
||||
faces = [[vv] for vv in v_current]
|
||||
faces_list = [[vv] for vv in v_current]
|
||||
|
||||
def add_edges(edge_id: NDArray[numpy.intp], v_nxt: NDArray[numpy.intp], kk: int) -> None: # noqa: ARG001
|
||||
for ii, vn in zip(edge_id, v_nxt, strict=True):
|
||||
faces[ii].append(vn)
|
||||
faces_list[ii].append(vn)
|
||||
|
||||
def cleaned_faces(remove: NDArray[numpy.int8], kk: int) -> list[NDArray[numpy.intp]]: # noqa: ARG001
|
||||
faces_set = []
|
||||
for ii in numpy.where(~remove)[0]:
|
||||
faces_set.append(faces[ii])
|
||||
faces_set.append(numpy.asarray(faces_list[ii], dtype=numpy.intp))
|
||||
return faces_set
|
||||
|
||||
"""
|
||||
|
|
@ -148,7 +147,7 @@ def _get_map(a: NDArray, b: NDArray) -> NDArray[numpy.intp]:
|
|||
Assumes that all elements appear exactly once in each array
|
||||
"""
|
||||
if a.size == 0:
|
||||
return numpy.array(()), numpy.array(())
|
||||
return numpy.array((), dtype=numpy.intp)
|
||||
sa = a.argsort()
|
||||
sb = b.argsort()
|
||||
r = numpy.empty_like(sa)
|
||||
|
|
@ -166,13 +165,13 @@ def _get_map_rows(a: NDArray, b: NDArray) -> NDArray[numpy.intp]:
|
|||
return _get_map(a1d, b1d)
|
||||
|
||||
|
||||
def _get_maps(a: NDArray, b: NDArray) -> tuple[int, int]:
|
||||
def _get_maps(a: NDArray, b: NDArray) -> tuple[NDArray[numpy.intp], NDArray[numpy.intp]]:
|
||||
"""
|
||||
Returns m, n such that a[m] == b, b[n] == a
|
||||
Assumes that all elements appear exactly once in each array
|
||||
"""
|
||||
if a.size == 0:
|
||||
return numpy.array(()), numpy.array(())
|
||||
return numpy.array((), dtype=numpy.intp), numpy.array((), dtype=numpy.intp)
|
||||
sa = a.argsort()
|
||||
sb = b.argsort()
|
||||
r = numpy.empty_like(sa)
|
||||
|
|
@ -183,7 +182,7 @@ def _get_maps(a: NDArray, b: NDArray) -> tuple[int, int]:
|
|||
return m, n
|
||||
|
||||
|
||||
def _get_maps_rows(a: NDArray, b: NDArray) -> NDArray[numpy.intp]:
|
||||
def _get_maps_rows(a: NDArray, b: NDArray) -> tuple[NDArray[numpy.intp], NDArray[numpy.intp]]:
|
||||
"""
|
||||
Row-wise _get_maps
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in a new issue