Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix plotting of 2d objects #1643

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

### Fixed
- Bug in plotting and computing tilted plane intersections of transformed 0 thickness geometries.

## [2.7.0rc1] - 2024-04-22

Expand Down
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(
tylerflex marked this conversation as resolved.
Show resolved Hide resolved
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
Loading