Skip to content

Commit ad47ecb

Browse files
committed
polyslab accepts numpy arrays for vertices
1 parent ea22a2a commit ad47ecb

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

tests/test_components.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ def test_geometry():
127127
s = Sphere(radius=1, center=(0, 0, 0))
128128
s = Cylinder(radius=1, center=(0, 0, 0), axis=1, length=1)
129129
s = PolySlab(vertices=((1, 2), (3, 4), (5, 4)), slab_bounds=(-1, 1), axis=1)
130+
vertices_np = np.array(s.vertices)
131+
s_np = PolySlab(vertices=vertices_np, slab_bounds=(-1, 1), axis=1)
130132

131133
# make sure wrong axis arguments error
132134
with pytest.raises(pydantic.ValidationError) as e_info:

tidy3d/components/geometry.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
from descartes import PolygonPatch
1212

1313
from .base import Tidy3dBaseModel
14-
from .types import Literal, Bound, Size, Coordinate, Axis, Coordinate2D
14+
from .types import Literal, Bound, Size, Coordinate, Axis, Coordinate2D, Array
1515
from .types import Vertices, Ax, Shapely
1616
from .viz import add_ax_if_none
17-
from ..log import Tidy3dKeyError
17+
from ..log import Tidy3dKeyError, SetupError
1818

1919
# add this around extents of plots
2020
PLOT_BUFFER = 0.3
@@ -780,8 +780,9 @@ class PolySlab(Planar):
780780
781781
Parameters
782782
----------
783-
vertices : List[Tuple[float, float]]
783+
vertices : List[Tuple[float, float]] or np.ndarray
784784
List of vertices defining the polygon face along dimensions parallel to slab normal axis.
785+
If numpy.ndarray, must have shape (N, 2) for N vertices.
785786
axis : int
786787
Integer index into the polygon's slab axis. (0,1,2) -> (x,y,z)
787788
slab_bounds: Tuple[float, float]
@@ -793,7 +794,7 @@ class PolySlab(Planar):
793794
"""
794795

795796
slab_bounds: Tuple[float, float]
796-
vertices: Vertices
797+
vertices: Union[Vertices, Array[float]]
797798
type: Literal["PolySlab"] = "PolySlab"
798799

799800
@pydantic.validator("slab_bounds", always=True)
@@ -803,6 +804,18 @@ def set_length(cls, val, values):
803804
values["length"] = zmax - zmin
804805
return val
805806

807+
@pydantic.validator("vertices", always=True)
808+
def correct_shape(cls, val):
809+
"""makes sure vertices is correct shape if numpy array"""
810+
if isinstance(val, np.ndarray):
811+
shape = val.shape
812+
if len(shape) != 2 or shape[1] != 2:
813+
raise SetupError(
814+
"PolySlab.vertices must be a 2 dimensional array shaped (N, 2). "
815+
f"Given array with shape of {shape}."
816+
)
817+
return val
818+
806819
@pydantic.validator("vertices", always=True)
807820
def set_center(cls, val, values):
808821
"""sets the .center field using zmin, zmax, and polygon vertices"""

0 commit comments

Comments
 (0)