예제 #1
0
    def test_apply_fill(self):
        drawable = Polygon([Node(0, 0)])
        self.__backend.create_canvas()
        drawable.style().add_color((0.1, 0.2, 0.3))
        drawable.style().set_type('solid')
        self.__backend.apply_fill(drawable)
        assert type(self.__backend.ctx().get_source()) == cairo.SolidPattern
        assert self.__backend.ctx().get_source().get_rgba() == (0.1, 0.2, 0.3,
                                                                1.0)

        drawable = Polygon([Node(0, 0)])
        drawable.style().add_color((0.1, 0.2, 0.3, 0.4))
        self.__backend.apply_fill(drawable)
        assert self.__backend.ctx().get_source().get_rgba() == (0.1, 0.2, 0.3,
                                                                0.4)

        drawable = Polygon([Node(0, 0)])
        drawable.style().add_color((0.1, 0.2, 0.3))
        drawable.style().add_color((0.1, 0.2, 0.3))
        self.__backend.apply_fill(drawable)
        assert type(self.__backend.ctx().get_source()) == cairo.LinearGradient

        drawable = Polygon([Node(0, 0)])
        drawable.style().add_color((0.1, 0.2, 0.3, 0.4))
        drawable.style().add_color((0.1, 0.2, 0.3, 0.4))
        self.__backend.apply_fill(drawable)
        assert type(self.__backend.ctx().get_source()) == cairo.LinearGradient
예제 #2
0
 def test_div(self):
     node = Node(5.5, -10.0, 'curve', True)
     quotient = node / 5
     assert quotient.position() == (1.1, -2.0)
     assert quotient.style() == node.style()
     assert quotient.fusable() == node.fusable()
     quotient = node / 'a'
예제 #3
0
 def test_frame(self):
     polygon = Polygon([Node(-1, 5), Node(4, 1)])
     assert len(polygon.frame().paths()) == 1
     assert len(polygon.frame().paths()[0]) == 2
     assert TestUtils.unordered_lists_equal(
         [Node(-1, 5), Node(4, 1)],
         polygon.frame().paths()[0])
예제 #4
0
 def test_div(self):
     node = Node(5.5, -10.0, 'curve', True)
     quotient = node / 5
     assert quotient.position() == (1.1, -2.0)
     assert quotient.style() == node.style()
     assert quotient.fusable() == node.fusable()
     quotient = node / 'a'
예제 #5
0
 def test_cmp(self):
     node1 = Node(1.1, 2.2, 'miter', True)
     node2 = Node(2.2, 3.3, 'miter', True)
     node3 = Node(1.1, 2.2, 'curve', False)
     assert node1 != node2
     assert node1 < node2
     assert node2 > node1
     assert node1 == node3
예제 #6
0
 def test_min_max(self):
     graph = nx.Graph()
     graph.add_node(Node(0,-1))
     graph.add_node(Node(1,1))
     graph.add_node(Node(-2,4))
     graph.add_node(Node(3,-5))
     opengraph = OpenGraph(graph)
     assert opengraph.min() == (-2, -5)
     assert opengraph.max() == (3, 4)
예제 #7
0
 def test_paths(self):
     g = nx.Graph()
     g.add_edge(Node(0, 0), Node(1, 0))
     g.add_edge(Node(3, 0), Node(2, 0))
     g.add_edge(Node(2, 0), Node(1, 0))
     g.add_edge(Node(2, 0), Node(2, 2))
     g.add_edge(Node(3, 0), Node(3, 2))
     opengraph = OpenGraph(g)
     assert len(opengraph.paths()) == 2
예제 #8
0
    def test_mul(self):
        node = Node(1.1, -2.0, 'curve', True)
        product = node * 5
        assert product.position() == (5.5, -10.0)
        assert product.style() == node.style()
        assert product.fusable() == node.fusable()

        product = node * (2, 3)
        assert product.position() == (2.2, -6.0)
        assert product.style() == node.style()
        assert product.fusable() == node.fusable()
예제 #9
0
    def test_fusable(self):
        node = Node(0, 0)
        assert node.fusable() == True

        node.set_fusable(False)
        assert node.fusable() == False

        node.set_fusable(True)
        assert node.fusable() == True
예제 #10
0
파일: utils.py 프로젝트: markdevries/shaape
 def generate_test_opengraph(seed=0, points=12, radius_range=(10, 50)):
     random.seed(seed)
     point_list = []
     graph = nx.Graph()
     for i in range(0, points / 2):
         angle1 = math.radians(i * 360 / points - 180)
         angle2 = math.radians(-1 * (i * 360 / points - 180))
         radius = random.randint(*radius_range)
         point1 = (math.sin(angle1) * radius, math.cos(angle1) * radius)
         point2 = (math.sin(angle2) * radius, math.cos(angle2) * radius)
         graph.add_edge(Node(*point1), Node(0, 0))
         graph.add_edge(Node(*point2), Node(0, 0))
     opengraph = OpenGraph(graph)
     return opengraph
예제 #11
0
    def test_contains(self):
        polygon = TestUtils.generate_test_polygon(seed=0,
                                                  points=12,
                                                  radius_range=(5, 10))
        assert polygon.contains((2, 3)) == True
        assert polygon.contains((11, 3)) == False
        assert polygon.contains((3, 11)) == False

        polygon = Polygon(
            [Node(0, 0),
             Node(4, 0),
             Node(17, 4),
             Node(0, 4),
             Node(0, 0)])
        assert polygon.contains((8, 1)) == False
예제 #12
0
    def test_normalize(self):
        node1 = Node(2.0, 4.0)
        node1.normalize()
        print(node1.length(), node1.length() == 1.0)
        assert node1.length() - 1.0 < 0.001

        node2 = Node(0.0, 0.0)
        assert_raises(ArithmeticError, node2.normalize)
예제 #13
0
    def test_init(self):
        node = Node(3.2, 5.0)
        assert node.position() == (3.2, 5.0)
        assert node.style() == 'miter'
        assert node.fusable() == True

        node = Node(3.2, 5.0, 'curve', False)
        assert node.position() == (3.2, 5.0)
        assert node.style() == 'curve'
        assert node.fusable() == False

        node = Node(3.2, 5.0, 'dummy', False)
예제 #14
0
    def test_fusable(self):
        node = Node(0, 0)
        assert node.fusable() == True
        
        node.set_fusable(False)
        assert node.fusable() == False

        node.set_fusable(True)
        assert node.fusable() == True
예제 #15
0
 def test_scale(self):
     original_g = TestUtils.generate_test_opengraph(seed = 0, points = 12, radius_range = (5, 5))
     g = copy.deepcopy(original_g)
     scale = (2, 3)
     g.scale(scale)
     assert len(g.graph().nodes()) == len(original_g.graph().nodes())
     for node in original_g.graph().nodes():
         assert Node(node[0] * scale[0], node[1] * scale[1]) in g.graph().nodes()
예제 #16
0
 def test_iter(self):
     node = Node(1.1, 2.2)
     i = 0
     for n in node:
         if i == 0:
             assert n == 1.1
         elif i == 1:
             assert n == 2.2
         i = i + 1
예제 #17
0
 def test_sub(self):
     node1 = Node(1.1, -3.0, 'curve', True)
     node2 = Node(-4.0, 9.2, 'miter', False)
     sum1 = node1 - node2
     sum2 = node2 - node1
     assert sum1.position() == (5.1, -12.2)
     assert sum2.position() == (-5.1, 12.2)
     assert sum1.style() == node1.style()
     assert sum1.fusable() == node1.fusable()
     assert sum2.style() == node2.style()
     assert sum2.fusable() == node2.fusable()
예제 #18
0
 def test_add(self):
     node1 = Node(1.1, -3.0, 'curve', True)
     node2 = Node(-4.0, 9.1, 'miter', False)
     sum1 = node1 + node2
     sum2 = node2 + node1
     assert sum1.position() == (-2.9, 6.1)
     assert sum2.position() == sum1.position()
     assert sum1.style() == node1.style()
     assert sum1.fusable() == node1.fusable()
     assert sum2.style() == node2.style()
     assert sum2.fusable() == node2.fusable()
예제 #19
0
파일: utils.py 프로젝트: markdevries/shaape
 def generate_test_polygon(seed=0, points=12, radius_range=(10, 50)):
     random.seed(seed)
     point_list = []
     for i in range(0, points):
         angle = math.radians(i * 360 / points - 180)
         radius = random.randint(*radius_range)
         point = (math.sin(angle) * radius, math.cos(angle) * radius)
         point_list.append(point)
     node_list = [Node(*p) for p in point_list]
     polygon = Polygon(node_list)
     return polygon
예제 #20
0
 def test_intersects(self):
     edge1 = Edge(Node(-2, 0), Node(2, 1))
     edge2 = Edge(Node(0, 1), Node(0, -1))
     edge3 = Edge(Node(-2, 1), Node(2, 2))
     assert edge1.intersects(edge2)
     assert not edge1.intersects(edge3)
     assert_raises(TypeError, edge1.intersects, 1)
예제 #21
0
    def test_draw_open_graph_shadow(self):
        self.__backend.set_canvas_size(50, 150)
        self.__backend.create_canvas()
        graph = nx.Graph()
        open_graph = OpenGraph(graph)

        self.__backend.push_surface()
        self.__backend.draw_open_graph_shadow(open_graph)
        self.__backend.export_to_file(
            TestUtils.OPEN_GRAPH_SHADOW_EMPTY_GENERATED_IMAGE)
        self.__backend.pop_surface()
        assert TestUtils.images_equal(
            TestUtils.OPEN_GRAPH_SHADOW_EMPTY_GENERATED_IMAGE,
            TestUtils.OPEN_GRAPH_SHADOW_EMPTY_EXPECTED_IMAGE)

        edge_list = [((10, 10), (40, 10)), ((40, 10), (60, 10)),
                     ((40, 10), (50, 40)), ((50, 40), (20, 30)),
                     ((50, 40), (50, 60))]
        edge_list2 = [((10, 10), (40, 10)), ((40, 10), (50, 40)),
                      ((50, 40), (20, 30)), ((20, 30), (10, 10))]
        for edge in edge_list:
            graph.add_edge(Node(*edge[0]), Node(*edge[1]))
        graph2 = nx.Graph()
        for edge in edge_list2:
            graph2.add_edge(Node(*edge[0], style='curve'),
                            Node(*edge[1], style='curve'))
        open_graph = OpenGraph(graph)
        open_graph2 = OpenGraph(graph2)

        self.__backend.push_surface()
        self.__backend.draw_open_graph_shadow(open_graph)
        self.__backend.translate(0, 60)
        self.__backend.draw_open_graph_shadow(open_graph2)
        self.__backend.export_to_file(
            TestUtils.OPEN_GRAPH_SHADOW_GENERATED_IMAGE)
        self.__backend.pop_surface()
        assert TestUtils.images_equal(
            TestUtils.OPEN_GRAPH_SHADOW_GENERATED_IMAGE,
            TestUtils.OPEN_GRAPH_SHADOW_EXPECTED_IMAGE)
예제 #22
0
    def test_normalize(self):
        node1 = Node(2.0, 4.0) 
        node1.normalize()
        print(node1.length(), node1.length() == 1.0)
        assert node1.length() - 1.0 < 0.001

        node2 = Node(0.0, 0.0) 
        assert_raises(ArithmeticError, node2.normalize)
예제 #23
0
    def test_run(self):
        parser = StyleParser()
        arrow_style = Style('_arrow_', 'fill', ['red'], 1)
        fill_style = Style('.*', 'fill', ['blue'], 0)
        line_style = Style('_line_', 'fill', ['green'], 2)
        frame_style = Style('.*', 'frame', [[0.1, 0.2, 0.3]], 0)
        custom_fill_style = Style('(abc)|(def)', 'fill', [[0.3, 0.2, 0.3]], 3)
        custom_line_style = Style('line', 'fill', [[0.4, 0.3, 0.3]], 3)
        custom_frame_style = Style('(abc)|(def)', 'frame', [[0.5, 0.2, 0.3]],
                                   3)
        polygon1 = Polygon([Node(0, 0)])
        opengraph1 = OpenGraph()
        opengraph2 = OpenGraph()
        opengraph2.add_name('line')
        arrow = Arrow()
        background = Background()
        polygon2 = Polygon([Node(0, 0)])
        polygon2.add_name('abc')
        polygon3 = Polygon([Node(0, 0)])
        polygon3.add_name('def')

        objects = [
            arrow_style, fill_style, line_style, frame_style,
            custom_fill_style, custom_line_style, custom_frame_style, polygon1,
            polygon2, polygon3, opengraph1, opengraph2, arrow, background
        ]
        parser.run([], objects)
        assert arrow.style().color() == arrow_style.color(), str(
            arrow.style().color()) + " != " + str(arrow_style.color())
        assert polygon1.style().color() == fill_style.color(), str(
            polygon1.style().color()) + " != " + str(fill_style.color())
        assert polygon1.frame().style().color() == frame_style.color()
        assert opengraph1.style().color() == line_style.color()
        assert opengraph2.style().color() == custom_line_style.color()
        assert polygon2.style().color() == custom_fill_style.color()
        assert polygon3.style().color() == custom_fill_style.color()
        assert polygon2.frame().style().color() == custom_frame_style.color()
        assert polygon3.frame().style().color() == custom_frame_style.color()
예제 #24
0
 def test_sub(self):
     node1 = Node(1.1, -3.0, 'curve', True)
     node2 = Node(-4.0, 9.2, 'miter', False)
     sum1 = node1 - node2
     sum2 = node2 - node1
     assert sum1.position() == (5.1, -12.2)
     assert sum2.position() == (-5.1, 12.2)
     assert sum1.style() == node1.style()
     assert sum1.fusable() == node1.fusable()
     assert sum2.style() == node2.style()
     assert sum2.fusable() == node2.fusable()
예제 #25
0
 def test_add(self):
     node1 = Node(1.1, -3.0, 'curve', True)
     node2 = Node(-4.0, 9.1, 'miter', False)
     sum1 = node1 + node2
     sum2 = node2 + node1
     assert sum1.position() == (-2.9, 6.1)
     assert sum2.position() == sum1.position()
     assert sum1.style() == node1.style()
     assert sum1.fusable() == node1.fusable()
     assert sum2.style() == node2.style()
     assert sum2.fusable() == node2.fusable()
예제 #26
0
    def test_mul(self):
        node = Node(1.1, -2.0, 'curve', True)
        product = node * 5
        assert product.position() == (5.5, -10.0)
        assert product.style() == node.style()
        assert product.fusable() == node.fusable()

        product = node * (2, 3)
        assert product.position() == (2.2, -6.0)
        assert product.style() == node.style()
        assert product.fusable() == node.fusable()
예제 #27
0
    def test_draw_polygon_shadow(self):
        point_list = [(10, 10), (40, 10), (30, 20), (40, 40), (20, 30),
                      (10, 10)]
        node_list = [Node(*p) for p in point_list]
        polygon1 = Polygon(node_list)
        polygon2 = Polygon(node_list[:-1])

        self.__backend.set_canvas_size(50, 100)
        self.__backend.create_canvas()
        self.__backend.push_surface()
        self.__backend.draw_polygon_shadow(polygon1)
        self.__backend.translate(0, 50)
        self.__backend.draw_polygon_shadow(polygon2)
        self.__backend.export_to_file(TestUtils.POLYGON_SHADOW_GENERATED_IMAGE)
        self.__backend.pop_surface()
        assert TestUtils.images_equal(TestUtils.POLYGON_SHADOW_GENERATED_IMAGE,
                                      TestUtils.POLYGON_SHADOW_EXPECTED_IMAGE)
예제 #28
0
 def test_scale(self):
     polygon = Polygon([Node(-1, 5), Node(4, 1)])
     polygon.scale((2, 3))
     assert polygon.nodes() == [Node(-2, 15), Node(8, 3)]
예제 #29
0
 def test_min_max(self):
     polygon = Polygon([Node(-1, 5), Node(4, 1)])
     assert polygon.min() == (-1, 1)
     assert polygon.max() == (4, 5)
예제 #30
0
 def test_nodes(self):
     polygon = Polygon([Node(0, 0), Node(4, 0)])
     assert polygon.nodes() == [Node(0, 0), Node(4, 0)]
예제 #31
0
 def test_length(self):
     node1 = Node(0, 0) 
     node2 = Node(3.0, 4.0) 
     assert node1.length() == 0
     assert node2.length() == 5
예제 #32
0
    def test_reduce_path(self):
        path = [Node(0, 0), Node(1, 0), Node(2, 0), Node(3,0), Node(4, 1), Node(5, 2)]
        reduced_path = reduce_path(path)
        assert reduced_path == [Node(0, 0), Node(3, 0), Node(5, 2)]

        path = [Node(0, 0), Node(1, 0), Node(2, 0), Node(3,0), Node(3, 3), Node(-1, 0), Node(0, 0)]
        reduced_path = reduce_path(path)
        assert reduced_path == [Node(-1, 0), Node(3, 0), Node(3, 3), Node(-1, 0)]
예제 #33
0
 def test_is_chord_free(self):
     graph = nx.Graph()
     graph.add_edge(Node(0.5, 0.5), Node(0.5, 1.5))
     graph.add_edge(Node(0.5, 0.5), Node(1.5, 1.5))
     graph.add_edge(Node(0.5, 0.5), Node(1.5, 0.5))
     graph.add_edge(Node(0.5, 1.5), Node(1.5, 1.5))
     graph.add_edge(Node(0.5, 1.5), Node(1.5, 0.5))
     graph.add_edge(Node(1.5, 0.5), Node(1.5, 1.5))
     cycle = [Node(1.5, 1.5), Node(0.5, 1.5), Node(0.5, 0.5), Node(1.5, 0.5)]
     assert is_chord_free(graph, cycle) == False
예제 #34
0
 def test_has_same_direction(self):
     assert has_same_direction(Node(0, 0), Node(0, 0)) == False
     assert has_same_direction(Node(0, 0), Node(1, 0)) == False
     assert has_same_direction(Node(1, 0), Node(-2, 0)) == True
     assert has_same_direction(Node(1, 2), Node(2, 4)) == True
     assert has_same_direction(Node(1, 2), Node(-2, -4)) == True
     assert has_same_direction(Node(1, 2), Node(2, 3)) == False
예제 #35
0
 def test_position(self):
     node = Node(1.5, -0.5)
     assert node.position() == (1.5, -0.5)
예제 #36
0
 def test_cycle_len(self):
     parser = OverlayParser()
     cycle = [Node(0, 0), Node(4, 0), Node(4, 2), Node(0, 2), Node(0, 0)]
     assert parser.cycle_len(cycle) == 12
예제 #37
0
    def test_draw_open_graph(self):
        self.__backend.set_canvas_size(80, 80)
        self.__backend.create_canvas()
        graph = nx.Graph()
        open_graph = OpenGraph(graph)

        self.__backend.push_surface()
        self.__backend.draw_open_graph(open_graph)
        self.__backend.export_to_file(
            TestUtils.OPEN_GRAPH_EMPTY_GENERATED_IMAGE)
        self.__backend.pop_surface()
        assert TestUtils.images_equal(
            TestUtils.OPEN_GRAPH_EMPTY_GENERATED_IMAGE,
            TestUtils.OPEN_GRAPH_EMPTY_EXPECTED_IMAGE)

        graph.add_edge(Node(10, 10), Node(20, 10))
        graph.add_edge(Node(20, 10), Node(20, 20))
        graph.add_edge(Node(20, 20), Node(10, 20))
        graph.add_edge(Node(10, 20), Node(10, 10))
        open_graph = OpenGraph(graph)
        self.__backend.push_surface()
        self.__backend.draw_open_graph(open_graph)
        self.__backend.export_to_file(
            TestUtils.OPEN_GRAPH_CLOSED_GENERATED_IMAGE)
        self.__backend.pop_surface()
        assert TestUtils.images_equal(
            TestUtils.OPEN_GRAPH_CLOSED_GENERATED_IMAGE,
            TestUtils.OPEN_GRAPH_CLOSED_EXPECTED_IMAGE)

        graph = nx.Graph()
        edge_list = [((10, 10), (40, 10)), ((40, 10), (60, 10)),
                     ((40, 10), (50, 40)), ((50, 40), (20, 30)),
                     ((50, 40), (50, 60))]
        for edge in edge_list:
            graph.add_edge(Node(*edge[0]), Node(*edge[1]))
        graph.add_edge(Node(50, 60, 'curve'), Node(20, 80, 'curve'))
        graph.add_edge(Node(20, 80, 'curve'), Node(40, 90, 'curve'))
        open_graph = OpenGraph(graph)

        self.__backend.push_surface()
        self.__backend.draw_open_graph(open_graph)
        self.__backend.export_to_file(TestUtils.OPEN_GRAPH_GENERATED_IMAGE)
        self.__backend.pop_surface()
        assert TestUtils.images_equal(TestUtils.OPEN_GRAPH_GENERATED_IMAGE,
                                      TestUtils.OPEN_GRAPH_EXPECTED_IMAGE)
예제 #38
0
 def test_substitutes(self):
     overlay = Overlay([[None, '/'],['\+', None]], [Edge(Node(1, 1), Node(0.5, 1.5, fusable = False))])
     graph = overlay.substitutes(["  / "," +  "])
     assert len(graph.edges()) == 1
예제 #39
0
 def test_init(self):
     polygon = Polygon([Node(0, 0)])
     assert polygon != None
예제 #40
0
 def test_style(self):
     node = Node(0, 0, 'miter')
     assert node.style() == 'miter'
     
     node = Node(0, 0, 'curve')
     assert node.style() == 'curve'