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

layer priority operation #1413

Merged
merged 2 commits into from Mar 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions gdsfactory/geometry/__init__.py
Expand Up @@ -11,6 +11,7 @@
from gdsfactory.geometry.check_space import check_space
from gdsfactory.geometry.check_width import check_width
from gdsfactory.geometry.invert import invert
from gdsfactory.geometry.layer_priority import layer_priority
from gdsfactory.geometry.offset import offset
from gdsfactory.geometry.outline import outline
from gdsfactory.geometry.trim import trim
Expand All @@ -32,4 +33,5 @@
"xor_diff",
"functions",
"trim",
"layer_priority",
)
119 changes: 119 additions & 0 deletions gdsfactory/geometry/layer_priority.py
@@ -0,0 +1,119 @@
import gdsfactory as gf
from gdsfactory.typings import LayerSpec, ComponentSpec
import numpy as np


def layer_priority(
component: ComponentSpec,
layer_high_order: LayerSpec,
layer_low_order: LayerSpec,
remove_high_order: bool = False,
**kwargs,
):
"""Returns a new component where a specified layer is removed from another.

Arguments:
layer_high_order: layer used to etch
layer_low_order: layer etched into
remove_high_order: whether to also remove the high order layer polygons. Useful if the higher order layer is purely logical.
kwargs: leyword arguments for boolean difference operation
"""
c = gf.Component()

# Obtain component subsets
layers_to_remove = (
[gf.get_layer(layer_low_order), gf.get_layer(layer_high_order)]
if remove_high_order
else [gf.get_layer(layer_low_order)]
)
component_minus_layers = component.extract(
[
gf.get_layer(layer)
for layer in component.get_layers()
if gf.get_layer(layer) not in layers_to_remove
]
)
component_high_order = component.extract([layer_high_order])
component_low_order = component.extract([layer_low_order])

# Remove high priority from low priority
component_high_order_removed_from_low_order = gf.geometry.boolean(
A=component_low_order,
B=component_high_order,
operation="A-B",
layer=layer_low_order,
**kwargs,
)

# Place all the other layers
c << component_minus_layers
c << component_high_order_removed_from_low_order

c.add_ports(ports=component.ports)

return c


def test_layer_priority() -> None:
funky_cross_section = gf.cross_section.cross_section(
width=0.5,
layer="WG",
sections=[
gf.cross_section.Section(width=2, layer="N"),
gf.cross_section.Section(width=2, layer="SLAB150"),
],
)

c = gf.components.coupler(cross_section=funky_cross_section)

c_WG_before = c.extract([gf.get_layer("WG")])
c_SLAB150_before = c.extract([gf.get_layer("SLAB150")])
c_N_before = c.extract([gf.get_layer("N")])

c_processed = layer_priority(
component=c,
layer_high_order="WG",
layer_low_order="SLAB150",
remove_high_order=False,
)

c_WG_after = c_processed.extract([gf.get_layer("WG")])
c_SLAB150_after = c_processed.extract([gf.get_layer("SLAB150")])
c_N_after = c_processed.extract([gf.get_layer("N")])

assert np.isclose(c_WG_before.area(), c_WG_after.area(), atol=1e-3)
assert np.isclose(c_N_before.area(), c_N_after.area(), atol=1e-3)
assert c_SLAB150_before.area() > c_SLAB150_after.area()

c_processed = layer_priority(
component=c,
layer_high_order="WG",
layer_low_order="SLAB150",
remove_high_order=True,
)
c_WG_after = c_processed.extract([gf.get_layer("WG")])

assert c_WG_after.area() == 0


if __name__ == "__main__":
funky_cross_section = gf.cross_section.cross_section(
width=0.5,
layer="WG",
sections=[
gf.cross_section.Section(width=2, layer="N"),
gf.cross_section.Section(width=2, layer="SLAB150"),
],
)

c = gf.components.coupler(cross_section=funky_cross_section)

c_processed = layer_priority(
component=c,
layer_high_order="WG",
layer_low_order="SLAB150",
remove_high_order=False,
)
c_processed.show(show_ports=True)

# test_layer_priority()