Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Fix bounding box edge cases (#164)
Browse files Browse the repository at this point in the history
* fixes case where cell is full of references which yield no valid polygons or bounding boxes and ends up returning initialization value

* test case 7b will cause master to fail but this fixed version will pass

* removing top if statement and making the code more readable. changing extend to append when adding ref bboxes

* caching the bbox=None case

Co-authored-by: Troy Tamas <[email protected]>
  • Loading branch information
heitzmann and tvt173 authored Dec 23, 2020
2 parents 3a86422 + c4232c6 commit 16c7012
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
28 changes: 13 additions & 15 deletions gdspy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,15 +559,9 @@ def get_bounding_box(self):
Bounding box of this cell [[x_min, y_min], [x_max, y_max]],
or None if the cell is empty.
"""
if (
len(self.polygons) == 0
and len(self.paths) == 0
and len(self.references) == 0
):
return None
if not (
self._bb_valid and all(ref._bb_valid for ref in self.get_dependencies(True))
):
deps_still_valid = all(ref._bb_valid for ref in self.get_dependencies(True))
cached_bbox_still_valid = self._bb_valid and deps_still_valid
if not cached_bbox_still_valid:
bb = numpy.array(((1e300, 1e300), (-1e300, -1e300)))
all_polygons = []
for polygon in self.polygons:
Expand All @@ -577,19 +571,23 @@ def get_bounding_box(self):
for reference in self.references:
reference_bb = reference.get_bounding_box()
if reference_bb is not None:
bb[0, 0] = min(bb[0, 0], reference_bb[0, 0])
bb[0, 1] = min(bb[0, 1], reference_bb[0, 1])
bb[1, 0] = max(bb[1, 0], reference_bb[1, 0])
bb[1, 1] = max(bb[1, 1], reference_bb[1, 1])
all_polygons.append(reference_bb)
if len(all_polygons) > 0:
all_points = numpy.concatenate(all_polygons).transpose()
bb[0, 0] = min(bb[0, 0], all_points[0].min())
bb[0, 1] = min(bb[0, 1], all_points[1].min())
bb[1, 0] = max(bb[1, 0], all_points[0].max())
bb[1, 1] = max(bb[1, 1], all_points[1].max())
self._bounding_box = bb
else:
self._bounding_box = None
self._bb_valid = True
self._bounding_box = bb
return numpy.array(self._bounding_box)

if self._bounding_box is None:
return None
else:
# return a *copy* of the cached bounding box to ensure it doesn't get inadvertently modified
return numpy.array(self._bounding_box)

def get_polygons(self, by_spec=False, depth=None):
"""
Expand Down
20 changes: 20 additions & 0 deletions tests/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,26 @@ def test_bounding_box():

assert_bb(cell6.get_bounding_box(), ((2, 0), (3, 1)))

cell7a = gdspy.Cell("7a")

assert cell7a.get_bounding_box() is None

cell7b = gdspy.Cell("7b")
cell7b.add(gdspy.CellReference(cell7a, rotation=90))

assert cell7b.get_bounding_box() is None

cell7c = gdspy.Cell("7c")
cell7c.add(gdspy.CellReference(cell7b))

assert cell7c.get_bounding_box() is None

cell7 = gdspy.Cell("7")
cell7.add(cell1)
cell7.add(cell7c)

assert_bb(cell7.get_bounding_box(), ((0, 0), (1, 1)))


def test_bounding_box2():
cell0 = gdspy.Cell("0")
Expand Down

0 comments on commit 16c7012

Please sign in to comment.