Beispiel #1
0
    def _get_cgal_elem(self):
        """
        Get a cgal geometry element representing this object, if any
        Width is only considered for Wire
        """
        from CGAL.CGAL_Kernel import \
            Segment_2,\
            Polygon_2,\
            Point_2,\
            Bbox_2,\
            Iso_rectangle_2,\
            do_intersect

        if isinstance(self, Swoop.Rectangle):
            rect = Rectangle(self.get_point(0), self.get_point(1), check=False)
            verts = list(rect.vertices_ccw())
            if self.get_rot() is not None:
                angle_obj = angle_match(self.get_rot())
                angle = math.radians(angle_obj['angle'])
                if angle_obj['mirrored']:
                    angle *= -1
                origin = rect.center()
                rmat = Rectangle.rotation_matrix(angle)
                for i, v in enumerate(verts):
                    verts[i] = np.dot(v - origin, rmat) + origin
            return Polygon_2(list(map(np2cgal, verts)))

        elif isinstance(self, Swoop.Wire):
            p1 = self.get_point(0)
            p2 = self.get_point(1)
            if self.get_width() is None or self.get_width() == 0:
                return Segment_2(np2cgal(p1), np2cgal(p2))
            else:
                # Wire has width
                # This is important to consider because wires can represent traces
                # When doing a query, it is important we can pick up the trace
                if p1[0] > p2[0]:
                    p1, p2 = p2, p1  # p1 is the left endpoint
                elif p1[0] == p2[0] and p1[1] > p2[1]:
                    p1, p2 = p2, p1  # or the bottom

                vec = (p2 - p1)
                vec /= np.linalg.norm(vec)  # Normalize
                radius = np.array(vec[1], -vec[0])
                radius *= self.get_width(
                ) / 2.0  # "Radius" of the wire, perpendicular to it

                vertices = []
                # Go around the vertices of the wire in CCW order
                # This should give you a rotated rectangle
                vertices.append(np2cgal(p1 + radius))
                vertices.append(np2cgal(p2 + radius))
                vertices.append(np2cgal(p2 - radius))
                vertices.append(np2cgal(p1 - radius))
                return Polygon_2(vertices)
        elif isinstance(self, Swoop.Polygon):
            return Polygon_2(
                [np2cgal(v.get_point()) for v in self.get_vertices()])
        else:
            return None
def get_intersection(ch_obj, bisector):
    # Next we find the intersection point of the biggest convex hull with the bisector

    intersection_pt_lst = []
    for edge in ch_obj.get_edge_list():
        intersection_obj = CGAL_Kernel.intersection(edge, bisector)
        if not intersection_obj.empty():
            if intersection_obj.is_Segment_2():
                print("Intersection is a segemnt")
                pass

            else:
                m = intersection_obj.get_Point_2()

            isExistent = False
            if len(intersection_pt_lst) > 0:
                # print('m ', m, 'len(intersection_pt_lst):', len(intersection_pt_lst))
                for pt in intersection_pt_lst:
                    # print('pt:', pt)
                    seg = Segment_2(m, pt)
                    seg_len = seg.squared_length()
                    # print('seg_len: ', seg_len)

                    if 0.0000000001 > seg_len >= 0:
                        # new_val = remove_exponent(seg_len)
                        # print('isExistent')
                        isExistent = True

            if isExistent is False:
                intersection_pt_lst.append(
                    m)  # Add the intersection point to the list

    return intersection_pt_lst
Beispiel #3
0
def segment_is_free(DT, vh_a, vh_b, ax=None):
    # requires points, not vertex handles
    # N.B. that means it does its own locate, which is O(sqrt(n))
    # also, without a face passed in, it will start at the boundary of
    # the convex hull.
    # so get a real face:
    for init_face in DT.incident_faces(vh_a):
        if not DT.is_infinite(init_face):
            break
    else:
        assert False

    lw = DT.line_walk(vh_a.point(), vh_b.point(), init_face)

    seg = Segment_2(vh_a.point(), vh_b.point())

    faces = []
    for face in lw:
        # check to make sure we don't go through a vertex
        for i in [0, 1, 2]:
            v = face.vertex(i)
            if v == vh_a or v == vh_b:
                continue
            if seg.has_on(v.point()):
                return False  # collinear!
        if len(faces):
            # figure out the common edge
            for i in [0, 1, 2]:
                if face.neighbor(i) == faces[-1]:
                    break
            else:
                tri_1 = face_to_tri(face)
                tri_2 = face_to_tri(faces[-1])

                pcoll = PolyCollection([tri_1, tri_2])
                ax = ax or plt.gca()
                ax.add_collection(pcoll)

                #assert False
                pdb.set_trace()

            edge = Constrained_Delaunay_triangulation_2_Edge(face, i)
            if DT.is_constrained(edge):
                if 0:
                    tri_1 = face_to_tri(face)
                    tri_2 = face_to_tri(faces[-1])
                    pcoll = PolyCollection([tri_1, tri_2])
                    ax = ax or plt.gca()
                    ax.add_collection(pcoll)
                return False

        faces.append(face)
        if face.has_vertex(vh_b):
            break
    return True
Beispiel #4
0
    def edges(self, exm_pnt_lst):

        #  generates the counterclockwise sequence of extreme points of the points in the hull

        CGAL_Convex_hull_2.ch_akl_toussaint(exm_pnt_lst, self.pnt_list)

        self.polygon = Polygon_2(self.pnt_list)

        lst_cycle = cycle(self.pnt_list)
        next_elem = next(lst_cycle)

        for i in range(len(self.pnt_list)):
            this_elem, next_elem = next_elem, next(lst_cycle)
            self.edge_list.append(Segment_2(this_elem, next_elem))

            pt_set = {(round(this_elem.x(), 13), round(this_elem.y(), 13)),
                      (round(next_elem.x(), 13), round(next_elem.y(), 13))}  # The two vertexes making up an edge
            self.edge_set_list.append(pt_set)
Beispiel #5
0
    def line_is_free(self, a, b, ax=None):
        """
        Check whether inserting a constraint between nodes a
        and b would intersect any existing constraints.

        This nodes a and b must have vertex handles in g.nodes['vh'].
        The points attached to those vertex handles are used here, and
        may differ from the locations in g.nodes['x'].
        
        ax: if specified and an error arises, will try to plot some details.

        returns an exception if the line is free (does not raise, just
        queues it up for you).
        or None if all is well.
        """
        DT = self.DT
        vh_a = self.g.nodes['vh'][a]
        vh_b = self.g.nodes['vh'][b]

        # requires points, not vertex handles
        # N.B. that means it does its own locate, which is O(sqrt(n))
        # also, without a face passed in, it will start at the boundary of
        # the convex hull.
        # so get a real face, which keeps the running time constant
        for init_face in DT.incident_faces(vh_a):
            if not DT.is_infinite(init_face):
                break
        else:
            assert False

        lw = DT.line_walk(vh_a.point(), vh_b.point(), init_face)
        seg = Segment_2(vh_a.point(), vh_b.point())

        faces = []
        for face in lw:
            # check to make sure we don't go through a vertex
            for i in [0, 1, 2]:
                v = face.vertex(i)
                if v == vh_a or v == vh_b:
                    continue
                if seg.has_on(v.point()):
                    n_collide = self.vh_info[v]
                    return self.ConstraintCollinearNode(
                        "Hit node %s along %s--%s" % (n_collide, a, b))

            if len(faces):
                # figure out the common edge
                for i in [0, 1, 2]:
                    if face.neighbor(i) == faces[-1]:
                        break
                else:
                    if ax:
                        tri_1 = face_to_tri(face)
                        tri_2 = face_to_tri(faces[-1])

                        pcoll = PolyCollection([tri_1, tri_2])
                        ax = ax or plt.gca()
                        ax.add_collection(pcoll)

                        pdb.set_trace()
                    assert False

                edge = Constrained_Delaunay_triangulation_2_Edge(face, i)
                if DT.is_constrained(edge):
                    if ax:
                        tri_1 = face_to_tri(face)
                        tri_2 = face_to_tri(faces[-1])
                        pcoll = PolyCollection([tri_1, tri_2])
                        ax = ax or plt.gca()
                        ax.add_collection(pcoll)
                    return self.IntersectingConstraints(
                        "Intersection along %s--%s" % (a, b))

            faces.append(face)
            if face.has_vertex(vh_b):
                break
        return None
from CGAL.CGAL_Kernel import Point_2
from CGAL.CGAL_Kernel import Segment_2
from CGAL.CGAL_Kernel import intersection
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *

segments = []
segments.append(Segment_2(Point_2(0, 1), Point_2(1, 0)))
segments.append(Segment_2(Point_2(1, 1), Point_2(2, 2)))
segments.append(Segment_2(Point_2(0, 0), Point_2(1, 2)))
segments.append(Segment_2(Point_2(0, 0), Point_2(2, 1)))
segments.append(Segment_2(Point_2(-1, 0), Point_2(-1, 2)))
segments.append(Segment_2(Point_2(0, -1), Point_2(2, -1)))

head = [
    Point_2(0, 1),
    Point_2(1, 1),
    Point_2(0, 0),
    Point_2(0, 0),
    Point_2(-1, 0),
    Point_2(0, -1)
]
tail = [
    Point_2(1, 0),
    Point_2(2, 2),
    Point_2(1, 2),
    Point_2(2, 1),
    Point_2(-1, 2),
    Point_2(2, -1)
]
Beispiel #7
0
from CGAL.CGAL_Kernel import Point_2
from CGAL.CGAL_Kernel import Segment_2
from CGAL.CGAL_Kernel import Bbox_2
from CGAL.CGAL_Box_intersection_d import Collect_ids_callback_2
from CGAL.CGAL_Box_intersection_d import Box_with_id_2
from CGAL.CGAL_Box_intersection_d import Pair_of_int
from CGAL import CGAL_Box_intersection_d

#First set of segments
segments_1 = []
segments_1.append(Segment_2(Point_2(1, 2), Point_2(2, 1)))
segments_1.append(Segment_2(Point_2(1, 3), Point_2(2, 3)))
segments_1.append(Segment_2(Point_2(5, 2), Point_2(6, 1)))

#Second set of segments
segments_2 = []
segments_2.append(Segment_2(Point_2(1, 1), Point_2(2, 2)))
segments_2.append(Segment_2(Point_2(5, 1), Point_2(6, 2)))
segments_2.append(Segment_2(Point_2(5, 3), Point_2(6, 3)))

#Create the set of bboxes of the first set
bboxes_1 = []
i = 0
for s in segments_1:
    bboxes_1.append(Box_with_id_2(s.bbox(), i))
    i += 1

#Create the set of bboxes of the second set
#Note that i is not reset to 0 as the id of each bbox should be unique
bboxes_2 = []
for s in segments_2:
Beispiel #8
0
 def same_direction_as_constraint(v0, v1):
     s = Segment(v0.point(), v1.point())
     if s.direction() == constraint_direction:
         return (v0, v1)
     else:
         return (v1, v0)