Skip to content

Commit c8f2386

Browse files
committed
Option to flatten all geometries in flatten_geometry
1 parent b741187 commit c8f2386

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

tidy3d/components/geometry/utils.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,36 @@
2222
]
2323

2424

25-
def flatten_groups(*geometries: GeometryType) -> GeometryType:
25+
def flatten_groups(*geometries: GeometryType, flatten_nonunion_type: bool = False) -> GeometryType:
2626
"""Iterates over all geometries, flattening groups and unions.
2727
2828
Parameters
2929
----------
30-
*geometries: GeometryType
30+
*geometries : GeometryType
3131
Geometries to flatten.
3232
33-
Returns
34-
-------
35-
:class:`Geometry`
36-
Geometries after flattening groups and unions."""
33+
flatten_nonunion_type : bool = False
34+
If ``False``, only flatten geometry unions (and ``GeometryGroup``). If ``True``, flatten
35+
all clip operations.
36+
37+
Yields
38+
------
39+
GeometryType
40+
Geometries after flattening groups and unions.
41+
"""
3742
for geometry in geometries:
3843
if isinstance(geometry, base.GeometryGroup):
39-
yield from flatten_groups(*geometry.geometries)
40-
elif isinstance(geometry, base.ClipOperation) and geometry.operation == "union":
41-
yield from flatten_groups(geometry.geometry_a, geometry.geometry_b)
44+
yield from flatten_groups(
45+
*geometry.geometries, flatten_nonunion_type=flatten_nonunion_type
46+
)
47+
elif isinstance(geometry, base.ClipOperation) and (
48+
flatten_nonunion_type or geometry.operation == "union"
49+
):
50+
yield from flatten_groups(
51+
geometry.geometry_a,
52+
geometry.geometry_b,
53+
flatten_nonunion_type=flatten_nonunion_type,
54+
)
4255
else:
4356
yield geometry
4457

0 commit comments

Comments
 (0)