Use to_polygons() for bounds calculation for simple path shapes

This commit is contained in:
jan 2019-05-20 21:06:28 -07:00
parent 2c9af3f63f
commit dcd8324eb4

View File

@ -2,7 +2,7 @@ from typing import List, Tuple, Dict
import copy import copy
from enum import Enum from enum import Enum
import numpy import numpy
from numpy import pi from numpy import pi, inf
from . import Shape, normalized_shape_tuple, Polygon, Circle from . import Shape, normalized_shape_tuple, Polygon, Circle
from .. import PatternError from .. import PatternError
@ -299,18 +299,12 @@ class Path(Shape):
elif self.cap in (Path.Cap.Flush, elif self.cap in (Path.Cap.Flush,
Path.Cap.Square, Path.Cap.Square,
Path.Cap.SquareCustom): Path.Cap.SquareCustom):
extensions = self._calculate_cap_extensions() bounds = numpy.array([[+inf, +inf], [-inf, -inf]])
polys = self.to_polygons()
v = remove_colinear_vertices(self.vertices, closed_path=False) for poly in polys:
dv = numpy.diff(v, axis=0) poly_bounds = poly.get_bounds()
dvdir = dv / numpy.sqrt((dv * dv).sum(axis=1))[:, None] bounds[0, :] = numpy.minimum(bounds[0, :], poly_bounds[0, :])
perp = dvdir[:, ::-1] * [[1, -1]] * self.width / 2 bounds[1, :] = numpy.maximum(bounds[1, :], poly_bounds[1, :])
v[0] -= dvdir * extensions[0]
v[-1] += dvdir * extensions[1]
bounds = self.offset + numpy.vstack((numpy.min(v - numpy.abs(perp), axis=0),
numpy.max(v + numpy.abs(perp), axis=0)))
else: else:
raise PatternError('get_bounds() not implemented for endcaps: {}'.format(self.cap)) raise PatternError('get_bounds() not implemented for endcaps: {}'.format(self.cap))