Beispiel #1
0
    def test_join_segments(self):
        pd = PolygonDecomposition()
        decomp = pd.decomp

        sg0, = pd.add_line((0, 0), (1, 0))
        mid_pt = sg0.vtxs[1]
        sg1, = pd.add_line((2, 0), (1, 0))
        sg2, = pd.add_line((2, 0), (3, 0))
        decomp.join_segments(sg0.vtxs[1], sg0, sg1)
        decomp.join_segments(sg0.vtxs[1], sg0, sg2)
Beispiel #2
0
 def __init__(self, line=None):
     self.decomposition = PolygonDecomposition()
     res = self.decomposition.get_last_polygon_changes()
     assert res[0] == PolygonChange.add
     self.outer_id = res[1]
     """ Decomposition of the a plane into polygons."""
     self.tmp_line = None
     """Line that will be reuse after line splitting"""
     self.tmp_polygon_id = None
     self.rest_polygon_id = None
     """Polygon that will be  created during removing splitted line"""
Beispiel #3
0
 def test_contains(self):
     decomp = PolygonDecomposition()
     sg_a, = decomp.add_line((0, 0), (2, 0))
     sg_b, = decomp.add_line((2, 0), (2, 2))
     sg_c, = decomp.add_line((2, 2), (0, 2))
     sg_d, = decomp.add_line((0, 2), (0, 0))
     in_wire = sg_a.wire[left_side]
     assert in_wire.contains_point([-1, 1]) == False
     assert in_wire.contains_point([-0.0001, 1]) == False
     assert in_wire.contains_point([+0.0001, 1]) == True
     assert in_wire.contains_point([1.999, 1]) == True
     assert in_wire.contains_point([2.0001, 1]) == False
Beispiel #4
0
def test_frac_intersections():
    # import sys
    # import trace
    #
    # # create a Trace object, telling it what to ignore, and whether to
    # # do tracing or line-counting or both.
    # tracer = trace.Trace(
    #     ignoredirs=[sys.prefix, sys.exec_prefix],
    #     trace=0,
    #     count=1)


    da = PolygonDecomposition()
    box = np.array([[0.0, 0.0],
                    [2.0, 3.0]])
    p00, p11 = box
    p01 = np.array([p00[0], p11[1]])
    p10 = np.array([p11[0], p00[1]])
    da.add_line(p00, p01)
    da.add_line(p01, p11)
    da.add_line(p11, p10)
    da.add_line(p10, p00)
    decomps = [da]

    np.random.seed(1)
    n_frac = 50
    p0 = np.random.rand(n_frac, 2) * (box[1] - box[0]) + box[0]
    p1 = np.random.rand(n_frac, 2) * (box[1] - box[0]) + box[0]

    for pa, pb in zip(p0, p1):
        dd = PolygonDecomposition()
        dd.add_line(pa, pb)
        decomps.append(dd)

    def tracer_func():
        return merge.intersect_decompositions(decomps)

    # import cProfile
    # cProfile.runctx('tracer_func()', globals(), locals(), 'prof_stats')
    #
    # import pstats
    # p = pstats.Stats('prof_stats')
    # p.sort_stats('cumulative').print_stats()

    decomp, maps = tracer_func()

    #######
    # Test merge with empty decomp.
    # Test fix for split points under tolerance.
    copy_decomp, maps = merge.intersect_decompositions([ decomp ])
Beispiel #5
0
    def test_join_poly(self):
        pd = PolygonDecomposition()
        decomp = pd.decomp

        sg0, = pd.add_line((0, 0), (0, 2))
        pd.add_line((0, 0), (2, 0))
        sg2, = pd.add_line((0, 2), (2, 0))
        pd.delete_segment(sg2)
Beispiel #6
0
    def test_join_wires(self):
        decomp = PolygonDecomposition()
        sg, = decomp.add_line((0, 0), (0, 2))
        pt0 = sg.vtxs[0]
        decomp.add_line((0, 0), (2, 0))
        decomp.add_line((0, 2), (2, 0))

        sg4, = decomp.add_line((.5, .5), (0.6, 0.6))
        decomp.new_segment(sg4.vtxs[0], pt0)
        assert len(decomp.decomp.wires) == 3
Beispiel #7
0
 def test_main_polygon_with_childs(self):
     da = PolygonDecomposition()
     decomp = da.decomp
     seg_in, = da.add_line((0.1, 0.5), (0.9, 0.5))
     seg_out, = da.add_line((0.1, -0.5), (0.9, -0.5))
     da.add_line((0, 0), (1, 0))
     da.add_line((0, 0), (0, 1))
     da.add_line((1, 1), (1, 0))
     da.add_line((1, 1), (0, 1))
     assert seg_in.wire[0] == seg_in.wire[1]
     assert seg_in.wire[0].polygon != decomp.outer_polygon
     assert seg_out.wire[0] == seg_out.wire[1]
     assert seg_out.wire[0].polygon == decomp.outer_polygon
Beispiel #8
0
    def test_join_polygons_embedded(self):
        pd = PolygonDecomposition()
        decomp = pd.decomp

        pd.add_line((0, 0), (3, 0))
        pd.add_line((0, 0), (0, 3))
        sg3, = pd.add_line((0, 3), (3, 0))
        pd.delete_segment(sg3)
        assert len(decomp.outer_polygon.outer_wire.childs) == 1
        wire = list(decomp.outer_polygon.outer_wire.childs)[0]
        assert len(wire.childs) == 0
Beispiel #9
0
    def test_polygon_depth(self):
        pd = PolygonDecomposition()
        decomp = pd.decomp

        # outer square
        sg_a, = pd.add_line((0, 0), (2, 0))
        sg_b, = pd.add_line((2, 0), (2, 2))
        sg_c, = pd.add_line((2, 2), (0, 2))
        sg_d, = pd.add_line((0, 2), (0, 0))

        # inner square
        sg_e, = pd.add_line((0.5, 0.5), (1, 0.5))
        sg_f, = pd.add_line((1, 0.5), (1, 1))
        sg_g, = pd.add_line((1, 1), (0.5, 1))
        sg_h, = pd.add_line((0.5, 1), (0.5, 0.5))

        assert decomp.polygons[0].depth() == 0
        assert decomp.polygons[1].depth() == 2
        assert decomp.polygons[2].depth() == 4
Beispiel #10
0
def test_simple_intersections():
    da = PolygonDecomposition()
    da.add_line((0, 0), (1,0))
    da.add_line((0, 0), (0, 1))
    da.add_line((1, 1), (1, 0))
    da.add_line((1, 1), (0, 1))
    da.add_line((0, 0), (1, 1))
    #print("da:\n", da)

    db = PolygonDecomposition()
    db.add_line((0, 0), (1,0))
    db.add_line((0, 0), (0, 1))
    db.add_line((1, 1), (1, 0))
    db.add_line((1, 1), (0, 1))
    db.add_line((1, 0), (0, 1))
    #print("db:\n", db)

    (dc, maps_a, maps_b) = merge.intersect_single(da, db)
    #print("dc\n", dc)
    #plot_polygon_decomposition(dc)
    assert maps_a[0] == {}
    assert maps_b[0] == { 0: 0, 1: 1, 2: 2, 3: 3}
    assert maps_a[1] == { 5:4}
    assert maps_b[1] == { 0: 0, 1: 1, 2: 2, 3: 3, 6:4, 7:4}
    assert maps_a[2] == { 3: 1, 4: 2}
    assert maps_b[2] == { 0: 0, 1: 1, 2: 1, 3: 2, 4: 2}
Beispiel #11
0
    def test_polygon_childs(self):
        pd = PolygonDecomposition()
        decomp = pd.decomp

        pd.add_line((0, 0), (4, 0))
        pd.add_line((0, 0), (0, 4))
        pd.add_line((0, 4), (4, 0))
        pd.add_line((1, 1), (2, 1))
        pd.add_line((1, 1), (1, 2))
        pd.add_line((1, 2), (2, 1))
        #plot_polygon_decomposition(decomp)
        lst = list(pd.get_childs(0))
        assert lst == [0, 1, 2]

        pd.add_line((1, 1), (0, 0))
        pd.add_line((2, 1), (4, 0))
        pd.add_line((1, 2), (0, 4))
Beispiel #12
0
    def test_split_poly(self):

        pd = PolygonDecomposition()
        decomp = pd.decomp
        sg_a, = pd.add_line((0, 0), (2, 0))
        sg_b, = pd.add_line((2, 0), (2, 2))
        sg_c, = pd.add_line((2, 2), (0, 2))
        sg_d, = pd.add_line((0, 2), (0, 0))
        # closed outer polygon

        assert sg_a.next == [(sg_d, 0), (sg_b, 1)]
        assert sg_b.next == [(sg_a, 0), (sg_c, 1)]
        assert sg_c.next == [(sg_b, 0), (sg_d, 1)]
        assert sg_d.next == [(sg_c, 0), (sg_a, 1)]

        external_wire = list(decomp.outer_polygon.outer_wire.childs)[0]
        assert sg_a.wire[right_side] == external_wire
        assert sg_b.wire[right_side] == external_wire
        assert sg_c.wire[right_side] == external_wire
        assert sg_d.wire[right_side] == external_wire
        #plot_polygon_decomposition(decomp)

        assert len(decomp.polygons) == 2
        sg_e, = pd.add_line((0.5, 0.5), (1, 0.5))
        sg_f, = pd.add_line((1, 0.5), (1, 1))
        sg_g, = pd.add_line((1, 1), (0.5, 1))
        sg_h, = pd.add_line((0.5, 1), (0.5, 0.5))
        # closed inner polygon
        #plot_polygon_decomposition(decomp)
        print("Decomp:\n", pd)
        out_square = sg_a, sg_b, sg_c, sg_d
        in_square = sg_e, sg_f, sg_g, sg_h
        self.check_split_poly_structure(decomp, out_square, in_square)

        # join nested wires
        sg_x = pd.new_segment(sg_a.vtxs[out_vtx], sg_e.vtxs[out_vtx])

        # split nested wires
        pd.delete_segment(sg_x)
        self.check_split_poly_structure(decomp, out_square, in_square)

        # split polygon - balanced
        seg_y, = pd.add_line((0.5, 0.5), (1, 1))

        # join polygons - balanced
        pd.delete_segment(seg_y)
        self.check_split_poly_structure(decomp, out_square, in_square)

        # join nested polygons
        pd.delete_segment(sg_h)
        #plot_polygon_decomposition(decomp)
        assert sg_b.wire == sg_a.wire
        assert sg_c.wire == sg_a.wire
        assert sg_d.wire == sg_a.wire

        assert sg_f.wire == sg_e.wire
        assert sg_g.wire == sg_e.wire
        assert sg_h.wire == sg_e.wire
        we_r, we_l = sg_e.wire
        assert we_r == we_l

        wire1 = decomp.outer_polygon.outer_wire
        wire2 = list(wire1.childs)[0]
        wire3 = list(wire2.childs)[0]
        assert sg_a.wire == [wire2, wire3]
        wire4 = list(wire3.childs)[0]
        assert we_r == wire4
        assert len(wire4.childs) == 0
Beispiel #13
0
class PolygonOperation():
    """
    Class for polygon localization
    """

    #    line_spliting = None
    #    """Note about spliting line in next add operation"""
    #    label = None
    #    """History label for this operation"""
    #    not_history = True
    #    """Not save this operation to history"""

    def __init__(self, line=None):
        self.decomposition = PolygonDecomposition()
        res = self.decomposition.get_last_polygon_changes()
        assert res[0] == PolygonChange.add
        self.outer_id = res[1]
        """ Decomposition of the a plane into polygons."""
        self.tmp_line = None
        """Line that will be reuse after line splitting"""
        self.tmp_polygon_id = None
        self.rest_polygon_id = None
        """Polygon that will be  created during removing splitted line"""

    def set_new_decomposition(self, diagram, decomposition):
        """set new decomposition"""
        self.decomposition = decomposition
        for segment in self.decomposition.segments.values():
            diagram.join_line_import(segment.vtxs[0].id, segment.vtxs[1].id,
                                     segment)
        for polygon in self.decomposition.polygons.values():
            if polygon.id != self.outer_id:
                self._add_polygon(diagram, polygon.id, None, True)

    def add_point(self, diagram, point):
        """Add new point to decomposition"""
        if self.rest_polygon_id is not None:
            polygon_id = self.rest_polygon_id
            self.rest_polygon_id = None
        else:
            polygon_id = self._find_in_polygon(diagram, point)
        self.decomposition.add_free_point(point.id, (point.x, -point.y),
                                          polygon_id)

    def move_points(self, diagram, points):
        """move set point in decomposition, return if all moving is possible"""
        ret = True
        if len(points) < 1:
            return True
        p0 = self.decomposition.points[points[0].de_id]
        dx = points[0].x - p0.xy[0]
        dy = -points[0].y - p0.xy[1]
        displacement = np.array([dx, dy])
        spoints = []
        for point in points:
            spoints.append(self.decomposition.points[point.de_id])
        if self.decomposition.check_displacment(spoints, displacement):
            self.decomposition.move_points(spoints, displacement)
        else:
            ret = False
        res = self.decomposition.get_last_polygon_changes()
        if res[0] == PolygonChange.shape:
            for polygon_id in res[1]:
                if polygon_id != self.outer_id:
                    self._reload_boundary(diagram, polygon_id)
        elif res[0] != PolygonChange.none:
            raise Exception("Invalid polygon change during move point.")
        return ret

    def remove_point(self, diagram, point):
        """remove set point from decomposition"""
        self.decomposition.remove_free_point(point.de_id)

    def add_line(self, diagram, line, label=None, not_history=True):
        """Add new line to decomposition"""
        if self.tmp_line is not None:
            segment = self.decomposition.new_segment(
                self.decomposition.points[self.tmp_line.p1.de_id],
                self.decomposition.points[self.tmp_line.p2.de_id])
            self.tmp_line.segment = segment
            res = self.decomposition.get_last_polygon_changes()
            if res[0] != PolygonChange.shape and res[0] != PolygonChange.none:
                raise Exception("Invalid polygon change during split line.")
            self.tmp_line = None
        segment = self.decomposition.new_segment(
            self.decomposition.points[line.p1.de_id],
            self.decomposition.points[line.p2.de_id])
        line.segment = segment
        res = self.decomposition.get_last_polygon_changes()
        if res[0] == PolygonChange.shape:
            for polygon_id in res[1]:
                if polygon_id != self.outer_id:
                    self._reload_boundary(diagram, polygon_id)
        elif res[0] == PolygonChange.add:
            if self.tmp_polygon_id is None:
                self._add_polygon(diagram, res[2], label, not_history)
                return True
            else:
                self._assign_add(diagram, res[2])
        elif res[0] == PolygonChange.split:
            if self.tmp_polygon_id is None:
                self._split_polygon(diagram, res[2], res[1], label,
                                    not_history)
                return True
            else:
                self._assign_split(diagram, res[2], res[1])
        elif res[0] != PolygonChange.none:
            raise Exception("Invalid polygon change during add line.")
        return False

    def split_line(self, diagram, line):
        """remove and move line"""
        self.tmp_line = line
        self.decomposition.delete_segment(line.segment)
        res = self.decomposition.get_last_polygon_changes()
        if res[0] == PolygonChange.shape:
            if len(res[1]) != 1:
                raise Exception("Invalid count of changed polygons.")
            self.rest_polygon_id = res[1][0]
        elif res[0] == PolygonChange.remove:
            self.rest_polygon_id = res[1]
            self.tmp_polygon_id = res[2]
        elif res[0] == PolygonChange.join:
            self.rest_polygon_id = res[1]
            self.tmp_polygon_id = res[2]
        elif res[0] != PolygonChange.none:
            raise Exception("Invalid polygon change during split line.")

    def remove_line(self, diagram, line, label=None, not_history=True):
        """remove set point from decomposition"""
        self.decomposition.delete_segment(line.segment)
        res = self.decomposition.get_last_polygon_changes()
        if res[0] == PolygonChange.shape:
            for polygon_id in res[1]:
                if polygon_id != self.outer_id:
                    self._reload_boundary(diagram, polygon_id)
        elif res[0] == PolygonChange.remove:
            self._remove_polygon(diagram, res[2], res[1], label, not_history)
        elif res[0] == PolygonChange.join:
            self._join_polygon(diagram, res[1], res[2], label, not_history)
        elif res[0] != PolygonChange.none:
            raise Exception("Invalid polygon change during remove line.")

    def get_polygon_origin_id(self, polygon):
        """Return polygon id in origin structure"""
        return self.decomposition.polygons[polygon.helpid].index

    def get_line_origin_id(self, line):
        """Return line id in origin structure"""
        return line.segment.index

    def get_point_origin_id(self, point_id):
        """Return point id in origin structure"""
        return self.decomposition.points[point_id].index

    def _find_in_polygon(self, diagram, point, polygon_id=None):
        """Find polygon for set point"""
        if polygon_id is None:
            polygon_id = self.outer_id
        childs = self.decomposition.get_childs(polygon_id)
        for id in childs:
            if id != polygon_id:
                children = self.decomposition.polygons[id].qtpolygon
                if children.containsPoint(point.qpointf(),
                                          QtCore.Qt.OddEvenFill):
                    return self._find_in_polygon(diagram, point, id)
        return polygon_id

    def _reload_boundary(self, diagram, polygon_id):
        """reload set polygon boundary"""
        spolygon = self._get_spolygon(diagram, polygon_id)
        polygon = self.decomposition.polygons[polygon_id]
        lines, qtpolygon = self._get_lines_and_qtpoly(diagram, polygon)
        for line in lines:
            if line not in spolygon.lines:
                line.add_polygon(spolygon)
                spolygon.lines.append(line)
        rem_lines = []
        for line in spolygon.lines:
            if not line in lines:
                rem_lines.append(line)
        for line in rem_lines:
            spolygon.lines.remove(line)
            line.del_polygon(spolygon)
        spolygon.qtpolygon = qtpolygon
        polygon.qtpolygon = qtpolygon
        spolygon.helpid = polygon_id
        spolygon.depth = polygon.depth()
        spolygon.drawpath = self._get_polygon_draw_path(polygon)
        if spolygon.object is not None:
            spolygon.object.refresh_polygon()

    def _assign_add(self, diagram, added_id):
        """Assign added polygon to existing polygon in diagram"""
        spolygon = self._get_spolygon(diagram, self.tmp_polygon_id)
        spolygon.helpid = added_id
        self._reload_boundary(diagram, added_id)
        self.tmp_polygon_id = None

    def _assign_split(self, diagram, added_id, old_id):
        """Assign added polygon to existing polygon in diagram"""
        spolygon = self._get_spolygon(diagram, self.tmp_polygon_id)
        spolygon.helpid = added_id
        self._reload_boundary(diagram, added_id)
        self._reload_boundary(diagram, old_id)
        self.tmp_polygon_id = None

    def _get_spolygon(self, diagram, polygon_id):
        spolygon = None
        for spoly in diagram.polygons:
            if spoly.helpid == polygon_id:
                spolygon = spoly
                break
        return spolygon

    def _fix_lines(self, diagram, polygon_id, polygon_old_id):
        """delete reference to old polygon in polygon lines"""
        polygon = self.decomposition.polygons[polygon_id]
        old_spolygon = self._get_spolygon(diagram, polygon_old_id)
        points = polygon.vertices()
        for i in range(0, len(points)):
            if i == 0:
                line = diagram.find_line(points[-1].id, points[0].id)
            else:
                line = diagram.find_line(points[i - 1].id, points[i].id)
            line.del_polygon(old_spolygon)

    def _reload_depth(self, diagram, polygon_id):
        """reload polygon depth recursivly"""
        spolygon = self._get_spolygon(diagram, polygon_id)
        if spolygon is None:
            return
        polygon = self.decomposition.polygons[polygon_id]

        # Temorary fix.
        # TODO: Do not call _reload_depth
        if spolygon is None:
            return

        spolygon.depth = polygon.depth()
        if spolygon.object is not None:
            spolygon.object.update_depth()
        childs = self.decomposition.get_childs(polygon_id)
        for children in childs:
            spolygon = self._get_spolygon(diagram, children)
            polygon = self.decomposition.polygons[children]
            spolygon.depth = polygon.depth()

    def _get_lines_and_qtpoly(self, diagram, polygon):
        """Return lines and qt polygon"""
        points = polygon.vertices()
        qtpolygon = QtGui.QPolygonF()
        lines = []
        for i in range(0, len(points)):
            qtpolygon.append(QtCore.QPointF(points[i].xy[0], -points[i].xy[1]))
            if i == 0:
                line = diagram.find_line(points[-1].id, points[0].id)
            else:
                line = diagram.find_line(points[i - 1].id, points[i].id)
            if line is None:
                raise Exception("Can't find polygon line in diagram")
            lines.append(line)
        qtpolygon.append(QtCore.QPointF(points[0].xy[0], -points[0].xy[1]))
        return lines, qtpolygon

    @staticmethod
    def _get_wire_oriented_vertices(wire):
        """
        Follow the wire segments and get the list of its vertices duplicating the first/last point.
        return: array, shape: n_vtx, 2
        """
        seggen = wire.segments()
        vtxs = []
        for seg, side in seggen:
            # Side corresponds to the end point of the segment. (Indicating also on which side thenwire lies.)
            if not vtxs:
                # first segment - add both vertices, so the loop is closed at the end.
                other_side = not side
                vtxs.append(seg.vtxs[other_side].xy)
            vtxs.append(seg.vtxs[side].xy)
        return np.array(vtxs)

    @classmethod
    def _add_to_painter_path(cls, path, wire):
        vtxs = cls._get_wire_oriented_vertices(wire)
        point_list = [QtCore.QPointF(vtxx, -vtxy) for vtxx, vtxy in vtxs]
        sub_poly = QtGui.QPolygonF(point_list)
        path.addPolygon(sub_poly)

    def _get_polygon_draw_path(self, polygon):
        """Get the path to draw the polygon in, i.e. the outer boundary and inner boundaries.
        The path approach allows holes in polygons and therefore flat depth for polygons (Odd-even paint rule)"""
        complex_path = QtGui.QPainterPath()
        self._add_to_painter_path(complex_path, polygon.outer_wire)
        # Subtract all inner parts
        for inner_wire in polygon.outer_wire.childs:
            self._add_to_painter_path(complex_path, inner_wire)
        return complex_path

    def _update_parent_drawpath(self, parent_polydata, diagram):
        """Update drawpath of the enclosing polygon (parent in decomposition), so it does not draw over the polygon"""
        # if the polygon is the base diagram polygon, do not update
        if not parent_polydata == self.decomposition.outer_polygon:
            parent_spoly = self._get_spolygon(diagram, parent_polydata.id)
            # recalculates the path by wire segments
            parent_spoly.drawpath = self._get_polygon_draw_path(
                parent_polydata)

    def _add_polygon(self,
                     diagram,
                     polygon_id,
                     label,
                     not_history,
                     copy_id=None):
        """Add polygon to boundary"""
        polygon = self.decomposition.polygons[polygon_id]
        if polygon == self.decomposition.outer_polygon:
            return
        self._update_parent_drawpath(polygon.outer_wire.parent.polygon,
                                     diagram)
        childs = self.decomposition.get_childs(polygon_id)
        for children in childs:
            if children != polygon_id:
                self._reload_depth(diagram, children)
        copy = None
        if copy_id is not None:
            copy = self._get_spolygon(diagram, copy_id)
        lines, qtpolygon = self._get_lines_and_qtpoly(diagram, polygon)
        spolygon = diagram.add_polygon(lines, label, not_history, copy)
        spolygon.qtpolygon = qtpolygon
        polygon.qtpolygon = qtpolygon
        spolygon.helpid = polygon_id
        spolygon.depth = polygon.depth()
        spolygon.drawpath = self._get_polygon_draw_path(polygon)

    def _remove_polygon(self, diagram, polygon_id, parent_id, label,
                        not_history):
        """Add polygon to boundary"""
        childs = self.decomposition.get_childs(parent_id)
        for children in childs:
            if children != parent_id:
                self._reload_depth(diagram, children)
        spolygon = self._get_spolygon(diagram, polygon_id)
        self._update_parent_drawpath(self.decomposition.polygons[parent_id],
                                     diagram)
        diagram.del_polygon(spolygon, label, not_history)

    def _split_polygon(self, diagram, polygon_id, polygon_old_id, label,
                       not_history):
        """split polygon"""
        self._reload_boundary(diagram, polygon_old_id)
        self._fix_lines(diagram, polygon_id, polygon_old_id)
        self._add_polygon(diagram, polygon_id, label, not_history,
                          polygon_old_id)

    def _join_polygon(self, diagram, polygon_id, del_polygon_id, label,
                      not_history):
        """Join to polygons"""
        spolygon = self._get_spolygon(diagram, polygon_id)
        del_spolygon = self._get_spolygon(diagram, del_polygon_id)
        regions = spolygon.cmp_polygon_regions(diagram, del_spolygon)
        diagram.del_polygon(del_spolygon, label, not_history)
        if regions is not None:
            spolygon.set_regions(diagram, regions, None, not_history)
            spolygon.object.update_color()
        self._reload_boundary(diagram, polygon_id)

    @classmethod
    def try_intersection(cls, diagram, p1, p2, label):
        """
        Try look up intersection and split lines. Return new points, and_lines.
        Points is sorted from p1 to p2.
        """
        new_points = []
        new_lines = []

        iline = QtCore.QLineF(p1.qpointf(), p2.qpointf())
        res_lines = []
        for line in diagram.lines:
            if line.p1 == p1 or line.p1 == p2 or line.p2 == p1 or line.p2 == p2:
                continue
            new_point = QtCore.QPointF()
            if iline.intersect(line.qlinef(),
                               new_point) == QtCore.QLineF.BoundedIntersection:
                new_points.append(new_point)
                res_lines.append(line)

        for i in range(0, len(res_lines)):
            if label == "Add line":
                label = "Add intersected line"
            p, l = diagram.add_new_point_to_line(res_lines[i],
                                                 new_points[i].x(),
                                                 new_points[i].y(), label)
            label = None
            new_lines.append(l)
            new_points[i] = p

        if len(new_points) > 1:
            if p1.x < p2.x:
                new_points.sort(key=lambda p: p.x)
            else:
                new_points.sort(key=lambda p: p.x, reverse=True)

        return new_points, new_lines, label
Beispiel #14
0
    def test_split_poly_1(self):
        # Test splitting of points and holes.
        pd = PolygonDecomposition()
        decomp = pd.decomp

        pd.add_line((0, 0), (1, 0))
        pd.add_line((0, 0), (0, 1))
        pd.add_line((1, 1), (1, 0))
        pd.add_line((1, 1), (0, 1))
        pd.add_point((0.2, 0.2))
        pd.add_point((0.8, 0.2))
        pd.add_line((0.2, 0.6), (0.3, 0.6))
        pd.add_line((0.8, 0.6), (0.7, 0.6))
        #plot_polygon_decomposition(decomp)
        pd.add_line((0.5, 0), (0.5, 1))
Beispiel #15
0
    def test_seg_add_remove(self):
        pd = PolygonDecomposition()
        decomp = pd.decomp

        pd.add_line((0, 1), (0, 0))
        pd.add_line((0, 0), (1, 0))
        seg_c, = pd.add_line((1, 0), (0, 1))

        pd.add_line((1, 0), (2, 0))
        pd.add_line((2, 0), (2, 1))
        pd.add_line((1, 0), (2, 1))
        #plot_polygon_decomposition(decomp)
        assert len(decomp.outer_polygon.outer_wire.childs) == 1
        assert len(decomp.outer_polygon.outer_wire.childs.pop().childs) == 2

        pd.delete_segment(seg_c)
Beispiel #16
0
    def test_complex_join_polygons(self):
        da = PolygonDecomposition()
        # outer triangle
        da.add_line((0, 4), (0, 0))
        da.add_line((0, 0), (4, 0))
        da.add_line((4, 0), (0, 4))

        # inner triangle
        da.add_line((1, 2), (1, 1))
        da.add_line((1, 1), (2, 1))
        seg, = da.add_line((2, 1), (1, 2))

        # inner triangle
        da.add_line((1.2, 1.6), (1.2, 1.2))
        da.add_line((1.2, 1.2), (1.6, 1.2))
        da.add_line((1.6, 1.2), (1.2, 1.6))
        da.decomp.check_consistency()
        #plot_polygon_decomposition(da)

        da.delete_segment(seg)
        da.decomp.check_consistency()
Beispiel #17
0
    def test_complex_wire_remove(self):
        da = PolygonDecomposition()
        # outer triangle
        da.add_line((0, 4), (0, 0))
        da.add_line((0, 0), (4, 0))
        da.add_line((4, 0), (0, 4))

        # inner triangle
        da.add_line((1, 2), (1, 1))
        da.add_line((1, 1), (2, 1))
        da.add_line((2, 1), (1, 2))

        # rugs
        sa, = da.add_line((2, 1), (4, 0))
        sb, = da.add_line((1, 2), (0, 4))

        #print("initial dc:\n", da)
        #plot_polygon_decomposition(da)

        da.delete_segment(sb)
        da.delete_segment(sa)
Beispiel #18
0
    def test_add_dendrite(self):
        pd = PolygonDecomposition()
        decomp = pd.decomp

        pt0 = pd.add_point((31.6, -40))
        pt1 = pd.add_point((32.4, -62.8))
        pt2 = pd.add_point((57.7, -37.4))
        pd.new_segment(pt0, pt1)
        pd.new_segment(pt0, pt2)
        pd.new_segment(pt1, pt2)
        # print(decomp)
        pt3 = pd.add_free_point(4, (75.7, -35), 0)
        pd.new_segment(pt2, pt3)
Beispiel #19
0
    def test_decomp(self):

        pd = PolygonDecomposition()
        decomp = pd.decomp
        pd.set_tolerance(0.01)
        outer = decomp.outer_polygon
        assert pd.get_last_polygon_changes() == (PolygonChange.add, outer.id,
                                                 outer.id)

        # test add point
        pt_a = pd.add_point([0, 0])
        assert pt_a.poly == outer
        pt_b = pd.add_point([1, 0])
        assert pt_a.poly == outer

        # test snap to point
        pt = pd._snap_point([0, 5e-3])
        assert pt == (0, pt_a, None)
        pt = pd._snap_point([5e-3, 5e-3])
        assert pt == (0, pt_a, None)
        pt = pd._snap_point([1 + 5e-3, 5e-3])
        assert pt == (0, pt_b, None)

        # test new_segment, new_wire
        sg_c = pd.new_segment(pt_a, pt_b)
        assert len(decomp.polygons) == 1
        assert len(decomp.outer_polygon.outer_wire.childs) == 1
        assert pd.get_last_polygon_changes() == (PolygonChange.none, None,
                                                 None)

        # test line matching existing segment
        sg_c = pd.new_segment(pt_a, pt_b)
        sg_c = pd.new_segment(pt_b, pt_a)

        # test add_line - new_segment, add_dendrite
        res = pd.add_line((0, 0), (0, 1))
        assert len(res) == 1
        sg_d = res[0]
        assert sg_d.next[left_side] == (sg_d, right_side)
        assert sg_d.next[right_side] == (sg_c, left_side)
        assert sg_c.next[left_side] == (sg_c, right_side)
        assert sg_c.next[right_side] == (sg_d, left_side)
        assert pt_a.poly == None
        assert pt_a.segment == (sg_c, out_vtx)
        assert pd.get_last_polygon_changes() == (PolygonChange.shape,
                                                 [outer.id], None)

        res = pd.add_line((2, 0), (3, 1))
        sg_x, = res
        assert len(decomp.polygons) == 1
        assert len(decomp.outer_polygon.outer_wire.childs) == 2

        # test snap point - snap to line
        pt = pd._snap_point([0.5, 5e-3])
        assert pt == (1, sg_c, 0.5)
        pt = pd._snap_point([5e-3, 0.3])
        assert pt == (1, sg_d, 0.3)
        pt = pd._snap_point([1 + 5e-3, 5e-3])
        assert pt == (0, pt_b, None)

        print(pd)
        # test _split_segment, new segment - add_dendrite
        result = pd.add_line((2, 1), (3, 0))
        sg_e, sg_f = result
        assert pd.get_last_polygon_changes() == (PolygonChange.shape,
                                                 [outer.id], None)

        assert sg_e.next[right_side] == (sg_e, left_side)
        sg_h = sg_e.next[left_side][0]
        assert sg_e.next[left_side] == (sg_h, left_side)
        assert sg_h.next[left_side] == (sg_h, right_side)
        assert sg_h.next[right_side] == (sg_f, left_side)
        assert sg_f.next[left_side] == (sg_f, right_side)
        sg_g = sg_f.next[right_side][0]
        assert sg_f.next[right_side] == (sg_g, right_side)
        assert sg_g.next[right_side] == (sg_g, left_side)
        assert sg_g.next[left_side] == (sg_e, right_side)
        sg_o, sg_p = pd.add_line((2.5, 0), (3, 0.5))

        # test add_point on segment
        pd.add_point((2.25, 0.75))

        # test new_segment - split polygon
        pd.add_line((-0.5, 1), (0.5, 0))
        assert pd.get_last_polygon_changes() == (PolygonChange.add, outer.id,
                                                 1)

        #plot_polygon_decomposition(decomp)
        # test split_segment in vertex
        pd.add_line((2, 0.5), (2, -0.5))

        # test new_segment - join_wires
        assert len(decomp.wires) == 4
        assert len(decomp.polygons) == 2

        print(decomp)
        #plot_polygon_decomposition(decomp)
        sg_m, = pd.add_line((0, 1), (2, 1))
        print(decomp)

        assert len(decomp.wires) == 3
        assert len(decomp.polygons) == 2
        assert pd.get_last_polygon_changes() == (PolygonChange.shape,
                                                 [outer.id], None)

        #plot_polygon_decomposition(decomp)

        # delete segment - split wire
        pd.delete_segment(sg_m)
        assert len(decomp.wires) == 4
        assert len(decomp.polygons) == 2
        #plot_polygon_decomposition(decomp)
        assert pd.get_last_polygon_changes() == (PolygonChange.shape,
                                                 [outer.id], None)

        # other split wire
        pt_op = sg_p.vtxs[out_vtx]
        pd.delete_segment(sg_f)
        assert len(decomp.wires) == 5
        assert len(decomp.polygons) == 2
        #plot_polygon_decomposition(decomp)

        #test split_segment connected on both sides; split non outer polygon
        seg_y, = pd.add_line((0, 0.25), (0.25, 0.25))
        assert pd.get_last_polygon_changes() == (PolygonChange.split, 1, 2)

        # test _join_segments - _split_segment inversion
        seg1 = sg_e
        mid_point = seg1.vtxs[in_vtx]
        seg0 = sg_e.next[left_side][0]
        decomp.join_segments(mid_point, seg0, seg1)

        # print("Decomp:\n", decomp)
        pd.delete_point(pt_op)
        # plot_polygon_decomposition(decomp)

        # test join polygons
        pd.delete_segment(seg_y)
        assert pd.get_last_polygon_changes() == (PolygonChange.join, 1, 2)

        # test add_free_point
        pd.add_free_point(100, (3.0, 0.3), decomp.outer_polygon.id)
        pd.remove_free_point(100)
Beispiel #20
0
    def test_polygon_childs_degenerate(self):
        pd = PolygonDecomposition()
        decomp = pd.decomp

        pd.add_line((0, 0), (3, 0))
        pd.add_line((0, 0), (0, 3))
        pd.add_line((0, 3), (3, 0))
        pd.add_line((1, 1), (2, 1))
        pd.add_line((1, 1), (1, 2))
        pd.add_line((1, 2), (2, 1))
        #plot_polygon_decomposition(decomp)

        pd.add_line((1, 1), (0, 0))
        pd.add_line((2, 1), (3, 0))
        pd.add_line((1, 2), (0, 3))