def test_scale(): poly = gdstk.Polygon([0j, 1 + 0j, 1j]) poly.scale(0.5) assert_close(poly.points, [[0, 0], [0.5, 0], [0, 0.5]]) poly = gdstk.Polygon([0j, 1 + 0j, 1j]) poly.scale(-2, 3, 1 + 0j) assert_close(poly.points, [[3, 0], [1, 0], [3, 3]])
def test_notempty(): name = "ca_notempty" c = gdstk.Cell(name) ref = gdstk.Reference(c, (1, -1), numpy.pi / 2, 2, True, 2, 3, (3, 2)) ref.origin = (0, 0) c.add(gdstk.rectangle((0, 0), (1, 2), 2, 3)) assert_close(ref.bounding_box(), ((0, 0), (8, 5))) assert_same_shape( [gdstk.rectangle((0, 0), (8, 2)), gdstk.rectangle((0, 3), (8, 5))], gdstk.Cell("TMP").add(ref).flatten().polygons, )
def test_label_bounding_box(): c = gdstk.Cell("CELL") l = gdstk.Label("Label", (2, 3)) c.add(l) bb = c.bounding_box(); assert bb[0][0] == 2 and bb[0][1] == 3 assert bb[1][0] == 2 and bb[1][1] == 3 ref = gdstk.Reference(c, (-1, 1)) bb = ref.bounding_box() assert bb[0][0] == 1 and bb[0][1] == 4 assert bb[1][0] == 1 and bb[1][1] == 4 ang = numpy.pi / 4 x = ref.origin[0] + l.origin[0] * numpy.cos(ang) - l.origin[1] * numpy.sin(ang) y = ref.origin[1] + l.origin[0] * numpy.sin(ang) + l.origin[1] * numpy.cos(ang) ref.rotation = ang bb = ref.bounding_box() assert_close(bb, ((x, y), (x, y)))
def test_bb(tree): c3, c2, c1 = tree assert_close(c3.bounding_box(), ((0, 0), (8, 4))) p2 = gdstk.Polygon(((-1, 2), (-1, 1), (0, 2)), 2, 2) c2.add(p2) assert_close(c3.bounding_box(), ((-1, 0), (8, 5))) p1 = gdstk.Polygon(((0, 3), (0, 2), (1, 3)), 3, 3) c1.add(p1) assert_close(c3.bounding_box(), ((-1, 0), (8, 6)))
def test_bb_label_repetition(): lbl = gdstk.Label("label", (1, 2)) lbl.repetition = gdstk.Repetition(x_offsets=(1, 3, -2)) c_lbl = gdstk.Cell("A") c_lbl.add(lbl) assert_close(c_lbl.bounding_box(), ((-1, 2), (4, 2))) ref = gdstk.Reference(c_lbl) ref.repetition = gdstk.Repetition(y_offsets=(-1, 2, -4)) c_ref = gdstk.Cell("B") c_ref.add(ref) assert_close(c_ref.bounding_box(), ((-1, -2), (4, 4))) ref.rotation = numpy.pi / 4 a = (-1 + 2j) * numpy.exp(0.25j * numpy.pi) b = (4 + 2j) * numpy.exp(0.25j * numpy.pi) assert_close(c_ref.bounding_box(), ((a.real, a.imag - 4), (b.real, b.imag + 2)))
def test_bb_robustpath_repetition(): pth = gdstk.RobustPath(0.5j, 1).segment((1, 0.5)) pth.repetition = gdstk.Repetition(x_offsets=(1, 3, -2)) c_pth = gdstk.Cell("G") c_pth.add(pth) assert_close(c_pth.bounding_box(), ((-2, 0), (4, 1))) ref = gdstk.Reference(c_pth) ref.repetition = gdstk.Repetition(y_offsets=(-1, 2, -4)) c_ref = gdstk.Cell("H") c_ref.add(ref) assert_close(c_ref.bounding_box(), ((-2, -4), (4, 3))) ref.rotation = numpy.pi / 4 a = (-2 + 1j) * numpy.exp(0.25j * numpy.pi) b = (-2 + 0j) * numpy.exp(0.25j * numpy.pi) c = (4 + 0j) * numpy.exp(0.25j * numpy.pi) d = (4 + 1j) * numpy.exp(0.25j * numpy.pi) assert_close(c_ref.bounding_box(), ((a.real, b.imag - 4), (c.real, d.imag + 2)))
def test_bb_flexpath_repetition(): pth = gdstk.FlexPath([0.5 + 0j, 0.5 + 1j], 1) pth.repetition = gdstk.Repetition(x_offsets=(1, 3, -2)) c_pth = gdstk.Cell("E") c_pth.add(pth) assert_close(c_pth.bounding_box(), ((-2, 0), (4, 1))) ref = gdstk.Reference(c_pth) ref.repetition = gdstk.Repetition(y_offsets=(-1, 2, -4)) c_ref = gdstk.Cell("F") c_ref.add(ref) assert_close(c_ref.bounding_box(), ((-2, -4), (4, 3))) ref.rotation = numpy.pi / 4 a = (-2 + 1j) * numpy.exp(0.25j * numpy.pi) b = (-2 + 0j) * numpy.exp(0.25j * numpy.pi) c = (4 + 0j) * numpy.exp(0.25j * numpy.pi) d = (4 + 1j) * numpy.exp(0.25j * numpy.pi) assert_close(c_ref.bounding_box(), ((a.real, b.imag - 4), (c.real, d.imag + 2)))
def test_bb_polygon_repetition(): pol = gdstk.rectangle((0, 0), (1, 1)) pol.repetition = gdstk.Repetition(x_offsets=(1, 3, -2)) c_pol = gdstk.Cell("C") c_pol.add(pol) assert_close(c_pol.bounding_box(), ((-2, 0), (4, 1))) ref = gdstk.Reference(c_pol) ref.repetition = gdstk.Repetition(y_offsets=(-1, 2, -4)) c_ref = gdstk.Cell("D") c_ref.add(ref) assert_close(c_ref.bounding_box(), ((-2, -4), (4, 3))) ref.rotation = numpy.pi / 4 a = (-2 + 1j) * numpy.exp(0.25j * numpy.pi) b = (-2 + 0j) * numpy.exp(0.25j * numpy.pi) c = (4 + 0j) * numpy.exp(0.25j * numpy.pi) d = (4 + 1j) * numpy.exp(0.25j * numpy.pi) assert_close(c_ref.bounding_box(), ((a.real, b.imag - 4), (c.real, d.imag + 2)))
def test_translate(): poly = gdstk.Polygon([0j, 1 + 0j, 1j]) poly.translate(-1, 2) assert_close(poly.points, [[-1, 2], [0, 2], [-1, 3]])
def test_rotate(): poly = gdstk.Polygon([0j, 1 + 0j, 1j]) poly.rotate(numpy.pi / 2, 0.5 + 0.5j) assert_close(poly.points, [[1, 0], [1, 1], [0, 0]])
def test_position(robust_path): assert_close(robust_path.position(-0.1), (-1, 0)) assert_close(robust_path.position(0), (-1, 0)) assert_close(robust_path.position(0.5), (0, 0.5)) assert_close(robust_path.position(1), (1, 1)) assert_close(robust_path.position(1.5), (1 + 2**0.5, -1 + 2**0.5)) assert_close(robust_path.position(2), (3, -1)) assert_close(robust_path.position(2.1), (3, -1))
def test_offsets(robust_path): assert_close(robust_path.offsets(-0.1), (0, 0)) assert_close(robust_path.offsets(0), (0, 0)) assert_close(robust_path.offsets(0.5), (-0.05, 0.05)) assert_close(robust_path.offsets(1), (-0.1, 0.1)) assert_close(robust_path.offsets(1, False), (-0.1, 0.1)) assert_close(robust_path.offsets(1.5), (-0.1, 0.05)) assert_close(robust_path.offsets(2), (-0.1, 0)) assert_close(robust_path.offsets(2, False), (-0.1, 0)) assert_close(robust_path.offsets(2.1), (-0.1, 0))
def test_widths(robust_path): assert_close(robust_path.widths(-0.1), (0.1, 0)) assert_close(robust_path.widths(0), (0.1, 0)) assert_close(robust_path.widths(0.5), (0.15, 0.05)) assert_close(robust_path.widths(1), (0.2, 0.1)) assert_close(robust_path.widths(1, False), (0.15, 0.15)) assert_close(robust_path.widths(1.5), (0.15, 0.15)) assert_close(robust_path.widths(2), (0.15, 0.15)) assert_close(robust_path.widths(2, False), (0.15, 0.15)) assert_close(robust_path.widths(2.1), (0.15, 0.15))
def test_gradient(robust_path): assert_close(robust_path.gradient(-0.1), (2, 1)) assert_close(robust_path.gradient(0), (2, 1)) assert_close(robust_path.gradient(0.5), (2, 1)) assert_close(robust_path.gradient(1), (2, 1)) assert_close(robust_path.gradient(1, False), (numpy.pi, 0)) assert_close(robust_path.gradient(1.5), (numpy.pi / 2**0.5, -numpy.pi / 2**0.5)) assert_close(robust_path.gradient(2), (0, -numpy.pi)) assert_close(robust_path.gradient(2, False), (0, -numpy.pi)) assert_close(robust_path.gradient(2.1), (0, -numpy.pi))
def test_transform(): poly = gdstk.Polygon([0j, 1 + 0j, 1j]) poly.transform() assert_close(poly.points, [[0, 0], [1, 0], [0, 1]]) poly = gdstk.Polygon([0j, 1 + 0j, 1j]) poly.transform(2, True, numpy.pi / 2, -1j) assert_close(poly.points, [[0, -1], [0, 1], [2, -1]]) poly = gdstk.Polygon([0j, 1 + 0j, 1j]) poly.transform(matrix=[[1, 2], [3, 4]]) assert_close(poly.points, [[0, 0], [1, 3], [2, 4]]) poly = gdstk.Polygon([0j, 1 + 0j, 1j]) poly.transform(matrix=[[1, 2], [3, 4], [1, -0.5]]) assert_close(poly.points, [[0, 0], [0.5, 1.5], [4, 8]]) poly = gdstk.Polygon([0j, 1 + 0j, 1j]) poly.transform(matrix=[[1, 2, 3], [4, 5, 6]]) assert_close(poly.points, [[3, 6], [4, 10], [5, 11]]) poly = gdstk.Polygon([0j, 1 + 0j, 1j]) poly.transform(matrix=[[1, 2, 3], [4, 5, 6], [3, 2, -1]]) assert_close(poly.points, [[-3, -6], [2, 5], [5, 11]])