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

properly render point-like objects in viz #987

Merged
merged 1 commit into from
Jul 6, 2023
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

# Changed
### Added

### Changed
- Source validation happens before simulation upload and raises an error if no source is present.

### Fixed
- Point-like objects correctly appear as single points using `plt.scatter`.

## [2.3.0] - 2023-6-30

### Added
Expand Down
26 changes: 26 additions & 0 deletions tests/test_components/test_viz.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
"""Tests visualization operations."""
import pytest
import matplotlib.pylab as plt
import tidy3d as td
from tidy3d.components.viz import Polygon


def test_make_polygon_dict():
p = Polygon(context={"coordinates": [(1, 0), (0, 1), (0, 0)]})
p.interiors


@pytest.mark.parametrize("center_z, len_collections", ((0, 1), (0.1, 0)))
def test_0d_plot(center_z, len_collections):
"""Ensure that 0d objects show up in plots."""

sim = td.Simulation(
size=(1, 1, 1),
sources=[
td.PointDipole(
center=(0, 0, center_z),
source_time=td.GaussianPulse(
freq0=td.C_0 / 1.0,
fwidth=td.C_0 / 5.0,
),
polarization="Ez",
)
],
run_time=1e-13,
)

ax = sim.plot(z=0)

# if a point is plotted, a single collection will be present, otherwise nothing
assert len(ax.collections) == len_collections
15 changes: 14 additions & 1 deletion tidy3d/components/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ def plot_shape(self, shape: Shapely, plot_params: PlotParams, ax: Ax) -> Ax:
if isinstance(_shape, LineString):
xs, ys = zip(*_shape.coords)
ax.plot(xs, ys, color=plot_params.edgecolor)
elif isinstance(_shape, Point):
ax.scatter(shape.x, shape.y, color=plot_params.facecolor)
else:
patch = polygon_patch(_shape, **plot_params.to_kwargs())
ax.add_artist(patch)
Expand Down Expand Up @@ -1275,6 +1277,7 @@ def surfaces_with_exclusion(
surfaces = [surf for surf in surfaces if surf.name[-2:] not in exclude_surfaces]
return surfaces

# pylint:disable=too-many-locals
def intersections_plane(self, x: float = None, y: float = None, z: float = None):
"""Returns shapely geometry at plane specified by one non None value of x,y,z.

Expand Down Expand Up @@ -1302,7 +1305,17 @@ def intersections_plane(self, x: float = None, y: float = None, z: float = None)
dz = np.abs(z0 - position)
if dz > Lz / 2 + fp_eps:
return []
return [box(minx=x0 - Lx / 2, miny=y0 - Ly / 2, maxx=x0 + Lx / 2, maxy=y0 + Ly / 2)]

minx = x0 - Lx / 2
miny = y0 - Ly / 2
maxx = x0 + Lx / 2
maxy = y0 + Ly / 2

# handle case where the box vertices are identical
if isclose(minx, maxx) and isclose(miny, maxy):
return [Point(minx, miny)]

return [box(minx=minx, miny=miny, maxx=maxx, maxy=maxy)]

def inside(
self, x: np.ndarray[float], y: np.ndarray[float], z: np.ndarray[float]
Expand Down