add get_bounds_nonempty

nolock
jan 2 years ago
parent 780d1ca97f
commit 0471addd65

@ -568,6 +568,17 @@ class Pattern(LockableImpl, AnnotatableImpl, Mirrorable, metaclass=AutoSlots):
else: else:
return numpy.vstack((min_bounds, max_bounds)) return numpy.vstack((min_bounds, max_bounds))
def get_bounds_nonempty(self) -> NDArray[numpy.float64]:
"""
Convenience wrapper for `get_bounds()` which asserts that the Pattern as non-None bounds.
Returns:
`[[x_min, y_min], [x_max, y_max]]`
"""
bounds = self.get_bounds()
assert(bounds is not None)
return bounds
def flatten(self: P) -> P: def flatten(self: P) -> P:
""" """
Removes all subpatterns and adds equivalent shapes. Removes all subpatterns and adds equivalent shapes.

@ -332,7 +332,7 @@ class Path(Shape, metaclass=AutoSlots):
bounds = numpy.array([[+inf, +inf], [-inf, -inf]]) bounds = numpy.array([[+inf, +inf], [-inf, -inf]])
polys = self.to_polygons() polys = self.to_polygons()
for poly in polys: for poly in polys:
poly_bounds = poly.get_bounds() poly_bounds = poly.get_bounds_nonempty()
bounds[0, :] = numpy.minimum(bounds[0, :], poly_bounds[0, :]) bounds[0, :] = numpy.minimum(bounds[0, :], poly_bounds[0, :])
bounds[1, :] = numpy.maximum(bounds[1, :], poly_bounds[1, :]) bounds[1, :] = numpy.maximum(bounds[1, :], poly_bounds[1, :])
else: else:

@ -65,9 +65,21 @@ class Positionable(metaclass=ABCMeta):
def get_bounds(self) -> Optional[NDArray[numpy.float64]]: def get_bounds(self) -> Optional[NDArray[numpy.float64]]:
""" """
Returns `[[x_min, y_min], [x_max, y_max]]` which specify a minimal bounding box for the entity. Returns `[[x_min, y_min], [x_max, y_max]]` which specify a minimal bounding box for the entity.
Returns `None` for an empty entity.
""" """
pass pass
def get_bounds_nonempty(self) -> NDArray[numpy.float64]:
"""
Returns `[[x_min, y_min], [x_max, y_max]]` which specify a minimal bounding box for the entity.
Asserts that the entity is non-empty (i.e., `get_bounds()` does not return None).
This is handy for destructuring like `xy_min, xy_max = entity.get_bounds_nonempty()`
"""
bounds = self.get_bounds()
assert(bounds is not None)
return bounds
class PositionableImpl(Positionable, metaclass=ABCMeta): class PositionableImpl(Positionable, metaclass=ABCMeta):
""" """

Loading…
Cancel
Save