Exemple #1
0
    def test_canvas_shapes4(self):

        b0 = Blok([Point(10, 11), Point(12, 13), Point(13, 11)])
        nb0 = Blok([Point(5, 5), Point(5, 8), Point(8, 9), Point(7, 5)])
        b1 = Blok([Point(10, 11), Point(12, 13), Point(13, 11)])
        shape_b0 = PolygonShape(b0.points, style=Style(color='green'))
        shape_b1 = PolygonShape(b1.points, style=Style(color='blue'))
        shape_nb0 = PolygonShape(nb0.points, style=Style(color='red'))
        shape_c1 = CompositeShape([shape_b0, shape_nb0])

        cv = Canvas(width=20, height=20, nrows=4, ncols=4)

        for r in range(4):
            for c in range(4):
                box = cv.get_box_for_cell(r, c)
                if r == 3 and c == 3:
                    cv = Canvas.add_shape2(cv,
                                           shape_b1,
                                           box,
                                           label=f"({str(r)},{str(c)})")
                else:
                    cv = Canvas.add_shape2(cv,
                                           shape_c1,
                                           box,
                                           label=f"({str(r)},{str(c)})")

        with open('canvas4.svg', 'w') as fd:
            Canvas.render_as_svg(cv, file=fd)
Exemple #2
0
    def gen_next_level(self, levelset, b1):
        unique_blocks = set()
        for b0 in iter(levelset):
            for b0e in range(Blok.num_edges(b0)):
                for b1e in range(Blok.num_edges(b1)):
                    (b, bm) = Blok.align_blocks_on_edge(b0, b0e, b1, b1e)
                    newb = Blok.merge(b, bm)
                    unique_blocks.add(newb)

        return unique_blocks
Exemple #3
0
    def test_align_block1(self):
        b1 = Blok([Point(0, 0), Point(0, 2), Point(2, 2), Point(2, 0)])
        b2 = Blok.translate(b1, 4, 4)
        b3 = Blok.rotate(b2, b2.points[0], math.pi / 4)

        print(f"b1: {b1}")
        print(f"b3: {b3}")

        for i in range(4):
            print(f"{i}:")
            for j in range(4):
                orig, aligned = Blok.align_blocks_on_edge(b1, i, b3, j)
                print(f"aligned edge {i} {j}: {aligned}")
Exemple #4
0
def runn(name, basic, num_levels, svg_file, is_symmetric=False):
    start_time = time.time()
    b0 = Blok(basic)
    b1 = Blok.rotate(b0, Blok.get_edge(b0, 0).start_pt, math.pi / 4)
    prev_level = set()
    prev_level.add(b0)
    all = [(1, b0)]

    for lev in range(num_levels - 1):
        cache = levels_cache.get((name, lev), None)
        if cache:
            curr_level = cache
        else:
            curr_level = gen_next_level(prev_level, b1, is_symmetric)
            levels_cache[(name, lev)] = curr_level
        for s in iter(curr_level):
            all.append((lev + 2, s))
        prev_level = curr_level

    normalized_bloks = [(n, Blok.normalize(b)) for n, b in all]
    bigbox = Box.bounding_box_of_boxex(
        [PolygonShape(b.points).bounding_box() for _, b in normalized_bloks])

    d = int(math.sqrt(len(all)))
    cv = Canvas(width=20, height=20, nrows=d + 2, ncols=d + 2)
    i = 0
    for n, b in normalized_bloks:
        c = int(i / d)
        r = int(i % d)
        box = cv.get_box_for_cell(r, c)
        sh = PolygonShape(b.points, style=Style(color='black', size='0.005'))
        if b.component_blocks:
            component_polygons = [
                PolygonShape(bp.points, style=Style(color='red'))
                for bp in b.component_blocks
            ] + [sh]
            sh = CompositeShape(component_polygons)
            cv = Canvas.add_shape3(cv,
                                   sh,
                                   box,
                                   bigbox,
                                   margin_percent=0.3,
                                   label=f"{str(n)}")
        i = i + 1

    with open(svg_file, 'w') as fd:
        Canvas.render_as_svg(cv, file=fd)
    print(
        f"{name}: Num shapes: {len(all)} --- Time: {(time.time() - start_time)} seconds ---"
    )
Exemple #5
0
    def test_canvas_shapes3(self):
        b0 = Blok([Point(10, 11), Point(12, 13), Point(13, 11)])
        nb0 = Blok.normalize(b0)
        shape_b0 = PolygonShape(b0.points, style=Style(color='green'))
        shape_nb0 = PolygonShape(nb0.points, style=Style(color='red'))
        shape_c1 = CompositeShape([shape_b0, shape_nb0])

        cv = Canvas(width=20, height=20, nrows=4, ncols=4)

        for r in range(4):
            for c in range(4):
                box = cv.get_box_for_cell(r, c)
                cv = Canvas.add_shape2(cv, shape_c1, box)

        with open('canvas3.svg', 'w') as fd:
            Canvas.render_as_svg(cv, file=fd)
Exemple #6
0
    def test_canvas_shapes2(self):
        b0 = Blok([Point(10, 11), Point(12, 13), Point(13, 11)])
        nb0 = Blok.normalize(b0)

        c1 = Canvas(width=20, height=20)
        shape_b0 = PolygonShape(b0.points, style=Style(color='green'))
        shape_nb0 = PolygonShape(nb0.points, style=Style(color='red'))

        shape_c1 = CompositeShape([shape_b0, shape_nb0])

        c1 = Canvas.add_shape(c1, shape_c1)

        box = Box(Point(10, 0), Point(20, 10))

        c2 = Canvas.add_shape2(c1, shape_c1, box)
        c3 = Canvas.add_shape2(c2, shape_c1, Box(Point(10, 10), Point(12, 12)))

        with open('canvas2.1.svg', 'w') as fd:
            Canvas.render_as_svg(c3, file=fd)
Exemple #7
0
    def test_simple_generator(self):
        b0 = Blok(SQUARE)
        b1 = Blok(SQUARE)
        cv = Canvas(width=20, height=20, nrows=4, ncols=4)

        for r in range(4):
            for c in range(4):
                (_, b2) = Blok.align_blocks_on_edge(b0, r, b1, c)
                b0_shape = PolygonShape(b0.points, style=Style(color='blue'))
                b2_shape = PolygonShape(b2.points, style=Style(color='red'))
                b0_b2_shape = CompositeShape([b0_shape, b2_shape])
                box = cv.get_box_for_cell(r, c)
                cv = Canvas.add_shape2(cv,
                                       b0_b2_shape,
                                       box,
                                       label=f"({str(r)},{str(c)})")

        with open('canvas5.svg', 'w') as fd:
            Canvas.render_as_svg(cv, file=fd)
Exemple #8
0
    def test_gen1(self):
        b0 = Blok(SQUARE)
        b1 = Blok.rotate(b0, Blok.get_edge(b0, 0).start_pt, math.pi / 4)
        unique_blocks = set()

        for b0e in range(Blok.num_edges(b0)):
            for b1e in range(Blok.num_edges(b1)):
                (b, bm) = Blok.align_blocks_on_edge(b0, b0e, b1, b1e)
                newb = Blok.merge(b, bm)
                unique_blocks.add(newb)
Exemple #9
0
def gen_next_level(levelset, b1, is_symmetric=False) -> Set[Blok]:
    unique_blocks = set()
    cntr = 0
    # if the piece we are adding is symmetric, namely all edges are indistinguiable from one another,
    # then we only need to attach one edge, there is no need to try all the edges as it would be the same
    if is_symmetric:
        b1_edges = [0]
    else:
        b1_edges = range(Blok.num_edges(b1))
    for b0 in iter(levelset):
        for b0e in range(Blok.num_edges(b0)):
            for b1e in b1_edges:
                try:
                    (b, bm) = Blok.align_blocks_on_edge(b0, b0e, b1, b1e)
                    newb = Blok.merge(b, bm)
                    flip_edge = Blok.find_flip_edge(newb)
                    flipped_newb = Blok.flip(newb, flip_edge)
                    cntr = cntr + 1
                    newb_not_in = newb not in unique_blocks
                    flipped_newb_not_in = flipped_newb not in unique_blocks
                    if newb_not_in and flipped_newb_not_in:
                        # print(f"adding newb: {newb}: id:{newb.gen_id()}, flipped_newb_id: {flipped_newb.gen_id()}")
                        # print(f"newb: {newb}")
                        unique_blocks.add(newb)
                except OverlapException:
                    continue
                except UnequalEdgesException:
                    continue
    return unique_blocks
Exemple #10
0
    def test_gen2(self):
        b0 = Blok(SQUARE)
        b1 = Blok.rotate(b0, Blok.get_edge(b0, 0).start_pt, math.pi / 4)
        uniq = set()
        uniq.add(b0)
        all = []
        level1 = self.gen_next_level(uniq, b1)

        print("level1")
        for s in iter(level1):
            all.append(s)

        print("level2")
        level2 = self.gen_next_level(level1, b1)
        for s in iter(level2):
            all.append(s)

        print("level3")
        level3 = self.gen_next_level(level2, b1)
        for s in iter(level3):
            all.append(s)

        normalized_bloks = [Blok.normalize(b) for b in all]
        bigbox = Box.bounding_box_of_boxex(
            [PolygonShape(b.points).bounding_box() for b in normalized_bloks])

        d = int(math.sqrt(len(all)))
        cv = Canvas(width=20, height=20, nrows=d, ncols=d)
        i = 0
        for b in normalized_bloks:
            print(b)
            c = int(i / d)
            r = int(i % d)
            sh = PolygonShape(b.points, style=Style(color='black'))
            box = cv.get_box_for_cell(r, c)
            cv = Canvas.add_shape2(cv, sh, box, label=f"({str(r)},{str(c)})")
            i = i + 1

        with open('all1.svg', 'w') as fd:
            Canvas.render_as_svg(cv, file=fd)
Exemple #11
0
    def test_canvas_shapes(self):
        b0 = Blok([Point(10, 11), Point(12, 13), Point(13, 11)])
        nb0 = Blok.normalize(b0)
        c1 = Canvas(width=20, height=20)
        shape_b0 = PolygonShape(b0.points)
        shape_nb0 = PolygonShape(nb0.points, style=Style(color='red'))
        bbox = shape_nb0.bounding_box()
        path = bbox.get_path()
        shape_bb = PolygonShape(path, style=Style(color='blue'))

        c2 = Canvas.add_shape(c1, shape_b0)
        c3 = Canvas.add_shape(c2, shape_nb0)
        c4 = Canvas.add_shape(c3, shape_bb)

        box = Box(Point(10, 0), Point(20, 10))
        c5 = Canvas.add_shape2(
            c4, PolygonShape(nb0.points, style=Style(color='green')), box)
        box2 = Box(Point(10, 10), Point(20, 20))
        c6 = Canvas.add_shape2(
            c5, PolygonShape(nb0.points, style=Style(color='yellow')), box2)

        with open('canvas1.svg', 'w') as fd:
            Canvas.render_as_svg(c6, file=fd)
Exemple #12
0
    def test_rotate(self):
        b0 = Blok([Point(1, 1), Point(2, 3), Point(3, 1)])
        br = Blok.rotate(b0, Point(1, 1), math.pi * (1.0 / 2.0))

        self.assertEqual(br, Blok([Point(1, 1), Point(3, 0), Point(1, -1)]))
        br2 = Blok.rotate(b0, Point(1, 1), math.pi)

        self.assertEqual(br2, Blok([Point(1, 1), Point(0, -1), Point(-1, 1)]))
        br3 = Blok.rotate(b0, Point(1, 1), -math.pi)
        self.assertEqual(br2, br3)
Exemple #13
0
    def test_merge(self):
        b0 = Blok(SQUARE)
        b1 = Blok(SQUARE)
        (b0, b2) = Blok.align_blocks_on_edge(b0, 0, b1, 0)
        mb = Blok.merge(b0, b2)

        mb1: Edge = Blok.get_edge(mb, 2)
        mb_rot = Blok.rotate(mb, mb1.start_pt, math.pi / 1.01)

        print(mb)
        print(mb_rot)
Exemple #14
0
 def test_flip(self):
     b0 = Blok([Point(1, 1), Point(2, 3), Point(3, 1)])
     bf = Blok.flip(b0, Blok.get_edge(b0, 2))
     print(bf)
     self.assertEqual(bf, Blok([Point(3, 1), Point(2, -1), Point(1, 1)]))