Skip to content

Commit

Permalink
fixed an issue with plotting transformed objects with 0 thickness
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarek-flex committed Apr 26, 2024
1 parent 0f92238 commit 55bf5f4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
75 changes: 72 additions & 3 deletions tidy3d/components/geometry/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,76 @@ def _center_not_inf(cls, val):
return val


class Planar(Geometry, ABC):
class SimplePlaneIntersection(Geometry, ABC):
"""A geometry where intersections with an axis aligned plane may be computed efficiently."""

def intersections_tilted_plane(
self, normal: Coordinate, origin: Coordinate, to_2D: MatrixReal4x4
) -> List[Shapely]:
"""Return a list of shapely geometries at the plane specified by normal and origin.
Checks special cases before relying on the complete computation.
Parameters
----------
normal : Coordinate
Vector defining the normal direction to the plane.
origin : Coordinate
Vector defining the plane origin.
to_2D : MatrixReal4x4
Transformation matrix to apply to resulting shapes.
Returns
-------
List[shapely.geometry.base.BaseGeometry]
List of 2D shapes that intersect plane.
For more details refer to
`Shapely's Documentation <https://shapely.readthedocs.io/en/stable/project.html>`_.
"""

# Check if normal is a special case, where the normal is aligned with an axis.
if normal.count(0.0) == 2:
axis = np.nonzero(normal)[0][0]
coord = "xyz"[axis]
kwargs = {coord: origin[axis]}
section = self.intersections_plane(**kwargs)
# Apply transformation in the plane by removing row and column
to_2D_in_plane = np.delete(np.delete(to_2D, axis, 0), axis, 1)

def transform(p_array):
return np.dot(
np.hstack((p_array, np.ones((p_array.shape[0], 1)))), to_2D_in_plane.T
)[:, :2]

transformed_section = shapely.transform(section, transformation=transform)
return transformed_section
else: # Otherwise compute the arbitrary intersection
return self._do_intersections_tilted_plane(normal=normal, origin=origin, to_2D=to_2D)

@abstractmethod
def _do_intersections_tilted_plane(
self, normal: Coordinate, origin: Coordinate, to_2D: MatrixReal4x4
) -> List[Shapely]:
"""Return a list of shapely geometries at the plane specified by normal and origin.
Parameters
----------
normal : Coordinate
Vector defining the normal direction to the plane.
origin : Coordinate
Vector defining the plane origin.
to_2D : MatrixReal4x4
Transformation matrix to apply to resulting shapes.
Returns
-------
List[shapely.geometry.base.BaseGeometry]
List of 2D shapes that intersect plane.
For more details refer to
`Shapely's Documentation <https://shapely.readthedocs.io/en/stable/project.html>`_.
"""


class Planar(SimplePlaneIntersection, Geometry, ABC):
"""Geometry with one ``axis`` that is slab-like with thickness ``height``."""

axis: Axis = pydantic.Field(
Expand Down Expand Up @@ -1656,7 +1725,7 @@ def _intersect_dist(self, position, z0) -> float:
"""Primitive classes"""


class Box(Centered):
class Box(SimplePlaneIntersection, Centered):
"""Rectangular prism.
Also base class for :class:`Simulation`, :class:`Monitor`, and :class:`Source`.
Expand Down Expand Up @@ -1811,7 +1880,7 @@ def surfaces_with_exclusion(cls, size: Size, center: Coordinate, **kwargs):
return surfaces

@verify_packages_import(["trimesh"])
def intersections_tilted_plane(
def _do_intersections_tilted_plane(
self, normal: Coordinate, origin: Coordinate, to_2D: MatrixReal4x4
) -> List[Shapely]:
"""Return a list of shapely geometries at the plane specified by normal and origin.
Expand Down
2 changes: 1 addition & 1 deletion tidy3d/components/geometry/polyslab.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def _move_axis_reverse(arr):
return inside_height * inside_polygon

@verify_packages_import(["trimesh"])
def intersections_tilted_plane(
def _do_intersections_tilted_plane(
self, normal: Coordinate, origin: Coordinate, to_2D: MatrixReal4x4
) -> List[Shapely]:
"""Return a list of shapely geometries at the plane specified by normal and origin.
Expand Down
2 changes: 1 addition & 1 deletion tidy3d/components/geometry/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def _normal_2dmaterial(self) -> Axis:
return self.axis

@verify_packages_import(["trimesh"])
def intersections_tilted_plane(
def _do_intersections_tilted_plane(
self, normal: Coordinate, origin: Coordinate, to_2D: MatrixReal4x4
) -> List[Shapely]:
"""Return a list of shapely geometries at the plane specified by normal and origin.
Expand Down

0 comments on commit 55bf5f4

Please sign in to comment.