-
Notifications
You must be signed in to change notification settings - Fork 54
Fix region_to_shapely_polygons #662
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4e612d5
fix: use each_point_hole() for polygon hole iteration
benvial 1dd1519
fix: correct polygon hole iteration. added tests
benvial ff52626
Refactor meshwell utils, fix file overwrite, and improve Palace tests
benvial 4b4a2d3
Increased mesh parameters in Palace tests to speed it up
benvial ee103e7
Update gplugins/common/utils/geometry.py
joamatab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from typing import List | ||
| import gdsfactory as gf | ||
| import kfactory as kf | ||
| from shapely.geometry import Polygon, MultiPolygon | ||
|
|
||
| def region_to_shapely_polygons(region: kf.kdb.Region) -> MultiPolygon: | ||
| """Convert a kfactory Region to a list of Shapely polygons.""" | ||
| polygons = [] | ||
| for polygon_kdb in region.each(): | ||
| exterior_coords = [ | ||
| (gf.kcl.to_um(point.x), gf.kcl.to_um(point.y)) | ||
| for point in polygon_kdb.each_point_hull() | ||
| ] | ||
| # Extract hole coordinates | ||
| holes = [] | ||
| num_holes = polygon_kdb.holes() | ||
| for hole_idx in range(num_holes): | ||
| hole_coords = [] | ||
| for point in polygon_kdb.each_point_hole(hole_idx): | ||
| hole_coords.append((gf.kcl.to_um(point.x), gf.kcl.to_um(point.y))) | ||
| holes.append(hole_coords) | ||
|
|
||
|
|
||
| # Create Shapely polygon | ||
| if holes: | ||
| polygon = Polygon(exterior_coords, holes) | ||
| else: | ||
| polygon = Polygon(exterior_coords) | ||
| polygons.append(polygon) | ||
|
|
||
| return MultiPolygon(polygons) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import pytest | ||
| import gdsfactory as gf | ||
|
|
||
|
|
||
| from gdsfactory.components import bend_circular, add_frame | ||
| from gdsfactory.generic_tech.layer_stack import get_layer_stack | ||
| from meshwell.cad import cad | ||
| from meshwell.mesh import mesh | ||
| from pathlib import Path | ||
| from tempfile import TemporaryDirectory | ||
| from gplugins.meshwell import ( | ||
| get_meshwell_prisms, get_meshwell_cross_section | ||
| ) | ||
| from shapely.geometry import LineString | ||
|
|
||
| @pytest.mark.parametrize("component", [(bend_circular), (add_frame)]) | ||
| def test_prisms(component) -> None: | ||
| prisms = get_meshwell_prisms( | ||
| component=component(), | ||
| layer_stack=get_layer_stack(sidewall_angle_wg=0), | ||
| name_by="layer", | ||
| ) | ||
|
|
||
| with TemporaryDirectory() as tmp_dir: | ||
| xao_file = Path(tmp_dir) / "meshwell_prisms_3D.xao" | ||
| msh_file = Path(tmp_dir) / "meshwell_prisms_3D.msh" | ||
| cad(entities_list=prisms, output_file=xao_file) | ||
| mesh( | ||
| input_file=xao_file, | ||
| output_file=msh_file, | ||
| default_characteristic_length=1000, | ||
| dim=3, | ||
| verbosity=10, | ||
| ) | ||
|
|
||
|
|
||
| def test_prisms_empty_component() -> None: | ||
| """Test that get_meshwell_prisms handles empty components gracefully.""" | ||
| c = gf.Component() | ||
| prisms = get_meshwell_prisms( | ||
| component=c, | ||
| layer_stack=get_layer_stack(sidewall_angle_wg=0), | ||
| name_by="layer", | ||
| wafer_padding=None, | ||
| ) | ||
| assert len(prisms) == 0 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("component", [(bend_circular), (add_frame)]) | ||
| def test_cross_section(component) -> None: | ||
| cross_section_line = LineString([(4, -15), (4, 15)]) | ||
| surfaces = get_meshwell_cross_section( | ||
| component=component(), | ||
| line=cross_section_line, | ||
| layer_stack=get_layer_stack(sidewall_angle_wg=0), | ||
| name_by="layer", | ||
| ) | ||
|
|
||
| with TemporaryDirectory() as tmp_dir: | ||
| xao_file = Path(tmp_dir) / "meshwell_prisms_3D.xao" | ||
| msh_file = Path(tmp_dir) / "meshwell_prisms_3D.msh" | ||
| cad(entities_list=surfaces, output_file=xao_file) | ||
| mesh( | ||
| input_file=xao_file, | ||
| output_file=msh_file, | ||
| default_characteristic_length=1000, | ||
| dim=2, | ||
| verbosity=10, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): Output file naming may cause collisions if component names are not unique.
Overwriting may occur if component names are duplicated. Add unique identifiers to filenames or implement collision checks.