Coverage for qpdk / samples / sample4.py: 100%
8 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-14 10:27 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-14 10:27 +0000
1# ---
2# jupyter:
3# jupytext:
4# text_representation:
5# extension: .py
6# format_name: percent
7# format_version: '1.3'
8# jupytext_version: 1.17.3
9# ---
11# %% [markdown]
12# # Packing Algorithm Sample
13#
14# This sample demonstrates the use of gdsfactory's packing algorithm to efficiently arrange shapes.
16# %%
17import gdsfactory as gf
18import numpy as np
20# %% [markdown]
21# ## Sample Function
22#
23# Creates a set of random ellipses and packs them efficiently into a rectangular area.
26# %%
27@gf.cell
28def sample4_pack():
29 """Returns a component with a packed set of ellipses."""
30 rng = np.random.default_rng()
31 ellipses = [
32 gf.components.ellipse(radii=tuple(rng.random(2) * n + 2), layer="M1_DRAW")
33 for n in range(80)
34 ]
35 bins = gf.pack(
36 ellipses, # Must be a list or tuple of Components
37 spacing=4, # Minimum distance between adjacent shapes
38 aspect_ratio=(1, 1), # Shape of the box
39 max_size=(500, 500), # Limits the size into which the shapes will be packed
40 density=1.05, # Values closer to 1 pack tighter but require more computation
41 sort_by_area=True, # Pre-sorts the shapes by area
42 )
43 return bins[0]