def bench_gdstk(): gdstk_ring.contain(*points) gdstk_ring.contain_any(*points) gdstk_ring.contain_all(*points) gdstk.inside(points, [gdstk_ring, gdstk_circle]) gdstk.any_inside(points, [gdstk_ring, gdstk_circle]) gdstk.all_inside(points, [gdstk_ring, gdstk_circle])
def render_text(text, size=None, position=(0, 0), font_prop=None, tolerance=0.1): tol = 0.1 * tolerance path = TextPath(position, text, size=size, prop=font_prop) polys = [] xmax = position[0] for points, code in path.iter_segments(): if code == path.MOVETO: c = gdstk.Curve(points, tolerance=tolerance) elif code == path.LINETO: c.segment(points.reshape(points.size // 2, 2)) elif code == path.CURVE3: c.quadratic(points.reshape(points.size // 2, 2)) elif code == path.CURVE4: c.cubic(points.reshape(points.size // 2, 2)) elif code == path.CLOSEPOLY: poly = c.points() if poly.size > 0: if poly[:, 0].min() < xmax: i = len(polys) - 1 while i >= 0: if gdstk.inside(poly[:1], [polys[i]], precision=tol)[0]: p = polys.pop(i) b = gdstk.boolean([p], [poly], "xor", tol) poly = b[0].points break elif gdstk.inside(polys[i][:1], [poly], precision=tol)[0]: p = polys.pop(i) b = gdstk.boolean([p], [poly], "xor", tol) poly = b[0].points i -= 1 xmax = max(xmax, poly[:, 0].max()) polys.append(poly) return polys
def inside_example(): rect = gdstk.rectangle((0, 0), (1, 1)) print(gdstk.inside([(0.5, 0.5), (2, 2)], rect)) print(gdstk.inside([(0.5, 0.5), (2, 2)], rect, "any")) print(gdstk.inside([(0.5, 0.5), (2, 2)], rect, "all")) # Point groups print( gdstk.inside([[(0.5, 0.5), (2, 2)], [(0, 0), (1, 1)], [(2, 2), (3, 3)]], rect))
def inside_example(): rect = gdstk.rectangle((0, 0), (1, 1)) assert gdstk.inside([(0.5, 0.5), (2, 2)], rect) == (True, False) assert gdstk.inside([(0.5, 0.5), (2, 2)], rect, "any") == (True, ) assert gdstk.inside([(0.5, 0.5), (2, 2)], rect, "all") == (False, ) # Point groups assert gdstk.inside([[(0.5, 0.5), (2, 2)], [(0, 0), (1, 1)], [(2, 2), (3, 3)]], rect) == (True, True, False)
def bench_gdstk(): r = gdstk.rectangle((0, 0), (20, 10)) pts = [[(1, 1), (-1, -1)], [(2, 2), (-2, 2), (2, -2)], [(5, 5), (10, 5)], [(-1, -1), (-2, -2)], [(2, 3)]] assert gdstk.inside(pts[0], r) == (True, False) assert gdstk.inside(pts[1], r) == (True, False, False) assert gdstk.inside(pts[1], r, "any") == (True, ) assert gdstk.inside(pts[1], r, "all") == (False, ) assert gdstk.inside(pts[4], r) == (True, ) assert gdstk.inside(pts, r, "any") == (True, True, True, False, True) assert gdstk.inside(pts, r, "all") == (False, False, True, False, True)
def test_inside(): ring = gdstk.ellipse((0, 0), 1, inner_radius=0.5, tolerance=1e-3) circle = gdstk.ellipse((0, 0), 0.5, tolerance=1e-3) points = [(0, 0), (0.2, 0), (-0.1, -0.8), (0.9, 0.7), (-0.4, 0.4)] truth_ring = tuple([0.25 <= p[0]**2 + p[1]**2 <= 1 for p in points]) truth_circle = tuple([p[0]**2 + p[1]**2 <= 0.25 for p in points]) assert ring.contain(*points) == truth_ring assert ring.contain_all(*points) == all(truth_ring) assert ring.contain_any(*points) == any(truth_ring) assert circle.contain(*points) == truth_circle assert circle.contain_all(*points) == all(truth_circle) assert circle.contain_any(*points) == any(truth_circle) assert gdstk.inside(points, [ring, circle]) == tuple( [r or c for r, c in zip(truth_ring, truth_circle)]) assert gdstk.all_inside(points, [ring, circle]) == all( [r or c for r, c in zip(truth_ring, truth_circle)]) assert gdstk.any_inside(points, [ring, circle]) == any( [r or c for r, c in zip(truth_ring, truth_circle)]) polys = [ gdstk.rectangle((0, 0), (10, 10)), gdstk.rectangle((10, 0), (20, 10)) ] for pts, _any, _all in ( ([(1, 1), (-1, -1)], True, False), ([(2, 2), (-2, 2), (2, -2)], True, False), ([(5, 5), (10, 5)], True, True), ([(-1, -1), (-2, -2)], False, False), ([(2, 3)], True, True), ): assert gdstk.any_inside(pts, polys) == _any assert gdstk.all_inside(pts, polys) == _all
if __name__ == "__main__": path = pathlib.Path(__file__).parent.absolute() unit = gdstk.Cell("Unit") unit.add(gdstk.cross((0, 0), 1, 0.2)) main = gdstk.Cell("Main") # Create repeating pattern using references d = 2 ref1 = gdstk.Reference(unit, columns=11, rows=6, spacing=(d, d * 3**0.5)) ref2 = gdstk.Reference(unit, (d / 2, d * 3**0.5 / 2), columns=10, rows=5, spacing=(d, d * 3**0.5)) main.add(ref1, ref2) main.flatten() hole = gdstk.text("PY", 8 * d, (0.5 * d, 0), layer=1) test = gdstk.inside([pol.points for pol in main.polygons], hole, "any") for pol, inside in zip(main.polygons, test): if inside: main.remove(pol) main.add(*hole) gdstk.Library().add(main).write_gds(path / "pos_filtering.gds") main.name = "pos_filtering" draw(main, path / "how-tos")
unit = gdstk.Cell("Unit") unit.add(gdstk.cross((0, 0), 1, 0.2)) main = gdstk.Cell("Main") # Create repeating pattern using references d = 2 main.add(gdstk.Reference(unit, columns=11, rows=6, spacing=(d, d * 3**0.5))) main.add( gdstk.Reference( unit, (d / 2, d * 3**0.5 / 2), columns=10, rows=5, spacing=(d, d * 3**0.5), )) main.flatten() hole = gdstk.text("PY", 8 * d, (0.5 * d, 0), layer=1) for pol in main.polygons: if gdstk.inside(pol.points, hole, "any")[0]: main.remove(pol) main.add(*hole) gdstk.Library().add(main).write_gds(path / "pos_filtering.gds") main.name = "pos_filtering" draw(main, path / "_static/how-tos")