Skip to content

Commit 321c0cd

Browse files
committed
feature: scene support plot3d
1 parent 11ae8a3 commit 321c0cd

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

tidy3d/components/scene.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
equal_aspect,
7070
plot_params_fluid,
7171
plot_params_structure,
72+
plot_scene_3d,
7273
polygon_path,
7374
)
7475

@@ -1955,3 +1956,15 @@ def _pcolormesh_shape_doping_box(
19551956
alpha=alpha,
19561957
clip_box=ax.bbox,
19571958
)
1959+
1960+
def plot_3d(self, width=800, height=800) -> None:
1961+
"""Render 3D plot of ``Scene`` (in jupyter notebook only).
1962+
Parameters
1963+
----------
1964+
width : float = 800
1965+
width of the 3d view dom's size
1966+
height : float = 800
1967+
height of the 3d view dom's size
1968+
1969+
"""
1970+
return plot_scene_3d(self, width=width, height=height)

tidy3d/components/viz/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
plot_params_structure,
2222
plot_params_symmetry,
2323
)
24-
from .plot_sim_3d import plot_sim_3d
24+
from .plot_sim_3d import plot_sim_3d, plot_scene_3d
2525
from .styles import (
2626
ARROW_ALPHA,
2727
ARROW_COLOR_MONITOR,
@@ -76,6 +76,7 @@
7676
"plot_params_structure",
7777
"plot_params_symmetry",
7878
"plot_sim_3d",
79+
"plot_scene_3d",
7980
"polygon_patch",
8081
"polygon_path",
8182
"restore_matplotlib_rcparams",

tidy3d/components/viz/plot_sim_3d.py

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,65 @@
55
from tidy3d.exceptions import SetupError
66

77

8-
def plot_sim_3d(sim, width=800, height=800) -> None:
9-
"""Make 3D display of simulation in ipyython notebook."""
8+
def plot_scene_3d(scene, width=800, height=800) -> None:
9+
import gzip
10+
import json
11+
from base64 import b64encode
12+
from io import BytesIO
13+
14+
import h5py
15+
16+
# Serialize scene to HDF5 in-memory
17+
buffer = BytesIO()
18+
scene.to_hdf5(buffer)
19+
buffer.seek(0)
20+
21+
# Open source HDF5 for reading and prepare modified copy
22+
with h5py.File(buffer, "r") as src:
23+
buffer2 = BytesIO()
24+
with h5py.File(buffer2, "w") as dst:
25+
26+
def copy_item(name, obj):
27+
if isinstance(obj, h5py.Group):
28+
dst.create_group(name)
29+
for k, v in obj.attrs.items():
30+
dst[name].attrs[k] = v
31+
elif isinstance(obj, h5py.Dataset):
32+
data = obj[()]
33+
if name == "JSON_STRING":
34+
# Parse and update JSON string
35+
json_str = (
36+
data.decode("utf-8")
37+
if isinstance(data, (bytes, bytearray))
38+
else data
39+
)
40+
json_data = json.loads(json_str)
41+
json_data["size"] = list(scene.size)
42+
json_data["center"] = list(scene.center)
43+
json_data["grid_spec"] = {}
44+
new_str = json.dumps(json_data)
45+
dst.create_dataset(name, data=new_str.encode("utf-8"))
46+
else:
47+
dst.create_dataset(name, data=data)
48+
for k, v in obj.attrs.items():
49+
dst[name].attrs[k] = v
50+
51+
src.visititems(copy_item)
52+
buffer2.seek(0)
53+
54+
# Gzip the modified HDF5
55+
gz_buffer = BytesIO()
56+
with gzip.GzipFile(fileobj=gz_buffer, mode="wb") as gz:
57+
gz.write(buffer2.read())
58+
gz_buffer.seek(0)
59+
60+
# Base64 encode and display with gzipped flag
61+
sim_base64 = b64encode(gz_buffer.read()).decode("utf-8")
62+
plot_sim_3d(sim_base64, width=width, height=height, is_gz_base64=True)
63+
64+
65+
def plot_sim_3d(sim, width=800, height=800, is_gz_base64=False) -> None:
66+
"""Make 3D display of simulation in ipython notebook."""
1067

1168
try:
1269
from IPython.display import HTML, display
@@ -19,10 +76,14 @@ def plot_sim_3d(sim, width=800, height=800) -> None:
1976
from base64 import b64encode
2077
from io import BytesIO
2178

22-
buffer = BytesIO()
23-
sim.to_hdf5_gz(buffer)
24-
buffer.seek(0)
25-
base64 = b64encode(buffer.read()).decode("utf-8")
79+
if not is_gz_base64:
80+
buffer = BytesIO()
81+
sim.to_hdf5_gz(buffer)
82+
buffer.seek(0)
83+
base64 = b64encode(buffer.read()).decode("utf-8")
84+
else:
85+
base64 = sim
86+
2687
js_code = """
2788
/**
2889
* Simulation Viewer Injector

0 commit comments

Comments
 (0)