Skip to content

Commit

Permalink
Add label for geometrical object (#2631)
Browse files Browse the repository at this point in the history
* Add label for geometrical object

* Fix formatting

* Change default behaviour and check label for None

* fix bug
  • Loading branch information
Yaraslaut authored Oct 11, 2023
1 parent e908ef8 commit 261a3f5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
5 changes: 4 additions & 1 deletion python/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,9 @@ class GeometricObject:
```
"""

def __init__(self, material=Medium(), center=Vector3(), epsilon_func=None):
def __init__(
self, material=Medium(), center=Vector3(), epsilon_func=None, label=None
):
"""
Construct a `GeometricObject`.
Expand Down Expand Up @@ -1095,6 +1097,7 @@ def __init__(self, material=Medium(), center=Vector3(), epsilon_func=None):
epsilon_func.eps = True
material = epsilon_func

self.label = label
self.material = material
self.center = Vector3(*center)

Expand Down
4 changes: 4 additions & 0 deletions python/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4745,6 +4745,7 @@ def plot2D(
output_plane: Optional[Volume] = None,
fields: Optional = None,
labels: bool = False,
label_geometry: bool = True,
eps_parameters: Optional[dict] = None,
boundary_parameters: Optional[dict] = None,
source_parameters: Optional[dict] = None,
Expand Down Expand Up @@ -4797,6 +4798,8 @@ def plot2D(
no fields are superimposed.
* `labels`: if `True`, then labels will appear over each of the simulation
elements. Defaults to `False`.
* `label_geometry`: if `True`, then labels will appear over each of the geometry
elements. Defaults to `True`.
* `eps_parameters`: a `dict` of optional plotting parameters that override the
default parameters for the geometry.
- `interpolation='spline36'`: interpolation algorithm used to upsample the pixels.
Expand Down Expand Up @@ -4871,6 +4874,7 @@ def plot2D(
output_plane=output_plane,
fields=fields,
labels=labels,
label_geometry=label_geometry,
eps_parameters=eps_parameters,
boundary_parameters=boundary_parameters,
source_parameters=source_parameters,
Expand Down
46 changes: 45 additions & 1 deletion python/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@

default_label_parameters = {"label_color": "r", "offset": 20, "label_alpha": 0.3}

default_label_geometry_parameters = {
"label_color": "w",
"arrow_color": "r",
"offset": 20,
"label_alpha": 0.8,
}

# Used to remove the elements of a dictionary (dict_to_filter) that
# don't correspond to the keyword arguments of a particular
# function (func_with_kwargs.)
Expand Down Expand Up @@ -138,6 +145,10 @@ def place_label(
offset = label_parameters["offset"]
alpha = label_parameters["label_alpha"]
color = label_parameters["label_color"]
if "arrow_color" in label_parameters:
arrow_color = label_parameters["arrow_color"]
else:
arrow_color = color

if x > centerx:
xtext = -offset
Expand All @@ -156,7 +167,9 @@ def place_label(
ha="center",
va="bottom",
bbox=dict(boxstyle="round,pad=0.2", fc=color, alpha=alpha),
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0.5", color=color),
arrowprops=dict(
arrowstyle="->", connectionstyle="arc3,rad=0.5", color=arrow_color
),
)
return ax

Expand Down Expand Up @@ -507,6 +520,7 @@ def plot_eps(
eps_parameters: Optional[dict] = None,
colorbar_parameters: Optional[dict] = None,
frequency: Optional[float] = None,
label_geometry: bool = False,
) -> Union[Axes, Any]:
# consolidate plotting parameters
if eps_parameters is None:
Expand Down Expand Up @@ -615,6 +629,34 @@ def plot_eps(
colorbar_parameters=colorbar_parameters,
)

if label_geometry:
for el in sim.geometry:
if sim_size.x == 0:
center_first = el.center.y
center_second = el.center.z
sim_first = sim_center.y
sim_second = sim_center.z
elif sim_size.y == 0:
center_first = el.center.x
center_second = el.center.z
sim_first = sim_center.x
sim_second = sim_center.z
elif sim_size.z == 0:
center_first = el.center.x
center_second = el.center.y
sim_first = sim_center.x
sim_second = sim_center.y
if el.label is not None:
ax = place_label(
ax,
el.label,
center_first,
center_second,
sim_first,
sim_second,
label_parameters=default_label_geometry_parameters,
)

ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
return ax
Expand Down Expand Up @@ -903,6 +945,7 @@ def plot2D(
output_plane: Optional[Volume] = None,
fields: Optional = None,
labels: bool = False,
label_geometry: bool = False,
eps_parameters: Optional[dict] = None,
boundary_parameters: Optional[dict] = None,
source_parameters: Optional[dict] = None,
Expand Down Expand Up @@ -945,6 +988,7 @@ def plot2D(
eps_parameters=eps_parameters,
colorbar_parameters=colorbar_parameters,
frequency=frequency,
label_geometry=label_geometry,
)

# Plot boundaries
Expand Down

0 comments on commit 261a3f5

Please sign in to comment.