Make cut generate clean polygons
This commit is contained in:
parent
4466198924
commit
cea172e7f2
@ -191,19 +191,46 @@ class Polygon(Shape):
|
|||||||
cx = numpy.hstack((min(tuple(cut_xs) + (mins[0],)) - dx, cut_xs, max((maxs[0],) + tuple(cut_xs)) + dx))
|
cx = numpy.hstack((min(tuple(cut_xs) + (mins[0],)) - dx, cut_xs, max((maxs[0],) + tuple(cut_xs)) + dx))
|
||||||
cy = numpy.hstack((min(tuple(cut_ys) + (mins[1],)) - dy, cut_ys, max((maxs[1],) + tuple(cut_ys)) + dy))
|
cy = numpy.hstack((min(tuple(cut_ys) + (mins[1],)) - dy, cut_ys, max((maxs[1],) + tuple(cut_ys)) + dy))
|
||||||
|
|
||||||
shape_with_extra_verts = float_raster.create_vertices(xy, cx, cy)
|
all_verts = float_raster.create_vertices(xy, cx, cy)
|
||||||
|
|
||||||
polygons = []
|
polygons = []
|
||||||
for cx_min, cx_max in zip(cx, cx[1:]):
|
for cx_min, cx_max in zip(cx, cx[1:]):
|
||||||
for cy_min, cy_max in zip(cy, cy[1:]):
|
for cy_min, cy_max in zip(cy, cy[1:]):
|
||||||
clipped_verts = float_raster.clip_vertices_to_window(
|
clipped_verts = (numpy.real(all_verts).clip(cx_min, cx_max) + 1j *
|
||||||
copy.deepcopy(shape_with_extra_verts),
|
numpy.imag(all_verts).clip(cy_min, cy_max))
|
||||||
cx_min, cx_max, cy_min, cy_max)
|
|
||||||
final_verts = numpy.hstack((
|
cleaned_verts = _clean_complex_vertices(clipped_verts)
|
||||||
numpy.real(clipped_verts)[:, None],
|
if len(cleaned_verts) == 0:
|
||||||
numpy.imag(clipped_verts)[:, None]))
|
continue
|
||||||
|
|
||||||
|
final_verts = numpy.hstack((numpy.real(clipped_verts)[:, None],
|
||||||
|
numpy.imag(clipped_verts)[:, None]))
|
||||||
polygons.append(Polygon(
|
polygons.append(Polygon(
|
||||||
vertices=final_verts,
|
vertices=final_verts,
|
||||||
layer=self.layer,
|
layer=self.layer,
|
||||||
dose=self.dose))
|
dose=self.dose))
|
||||||
return polygons
|
return polygons
|
||||||
|
|
||||||
|
|
||||||
|
def _clean_complex_vertices(vertices: numpy.ndarray) -> numpy.ndarray:
|
||||||
|
eps = numpy.finfo(vertices.dtype).eps
|
||||||
|
|
||||||
|
def cleanup(vertices):
|
||||||
|
# Remove duplicate points
|
||||||
|
dv = v - numpy.roll(v, 1)
|
||||||
|
v = v[numpy.abs(dv) > eps]
|
||||||
|
|
||||||
|
# Remove colinear points
|
||||||
|
dv = v - numpy.roll(v, 1)
|
||||||
|
m = numpy.angle(dv) % pi
|
||||||
|
diff_m = numpy.abs(m - numpy.roll(m, -1))
|
||||||
|
return v[diff_m > eps]
|
||||||
|
|
||||||
|
n = len(vertices)
|
||||||
|
cleaned = cleanup(vertices)
|
||||||
|
while n != len(cleaned):
|
||||||
|
n = len(cleaned)
|
||||||
|
cleaned = cleanup(cleaned)
|
||||||
|
|
||||||
|
return cleaned
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user