How to use pyclipper - 10 common examples

To help you get started, we’ve selected a few pyclipper examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github revarbat / mandoline-py / mandoline / geometry2d.py View on Github external
def paths_contain(pt, paths):
    cnt = 0
    pt = pyclipper.scale_to_clipper([pt], SCALING_FACTOR)[0]
    for path in paths:
        path = pyclipper.scale_to_clipper(path, SCALING_FACTOR)
        if pyclipper.PointInPolygon(pt, path):
            cnt = 1 - cnt
    return cnt % 2 != 0
github jamiebull1 / geomeppy / geomeppy / geom / clippers.py View on Github external
def _prepare_clipper(self, poly):
        """Prepare 3D polygons for clipping operations.

        :param poly: The clip polygon.
        :returns: A Pyclipper object.

        """
        if not self.is_coplanar(poly):
            return False
        poly1 = self.project_to_2D()
        poly2 = poly.project_to_2D()

        s1 = pc.scale_to_clipper(poly1.vertices_list)
        s2 = pc.scale_to_clipper(poly2.vertices_list)
        clipper = pc.Pyclipper()
        clipper.AddPath(s1, poly_type=pc.PT_SUBJECT, closed=True)
        clipper.AddPath(s2, poly_type=pc.PT_CLIP, closed=True)

        return clipper
github deeplycloudy / glmtools / glmtools / grid / clipping.py View on Github external
as appropriate for clipper.
    """

    # Get the lines along each dimension. h, v need not
    # correspond to the horizontal and vertical dimensions.
    # It's shorter than writing coord0, coord1
    lines = np.dstack((xedge, yedge))
    nh, nv, nd = lines.shape
    h_lines = [lines[i,:,:] for i in range(nh)]
    v_lines = [lines[:,j,:] for j in range(nv)]
    
    if scale:
        # The scale factor could be adjusted, but by default it results in 
        # a range of +/- 2 ** 31 with a precision of 2 ** -31. See the docs.
        h_lines = scale_to_clipper(h_lines)
        v_lines = scale_to_clipper(v_lines)
    return h_lines, v_lines
github TUDelft-CNS-ATM / bluesky / bluesky / traffic / asas / SSD.py View on Github external
def area(vset):
    """ This function calculates the area of the set of FRV or ARV """
    # Initialize A as it could be calculated iteratively
    A = 0
    # Check multiple exteriors
    if type(vset[0][0]) == list:
        # Calc every exterior separately
        for i in range(len(vset)):
            A += pyclipper.scale_from_clipper(pyclipper.scale_from_clipper(pyclipper.Area(pyclipper.scale_to_clipper(vset[i]))))
    else:
        # Single exterior
        A = pyclipper.scale_from_clipper(pyclipper.scale_from_clipper(pyclipper.Area(pyclipper.scale_to_clipper(vset))))
    return A
github TUDelft-CNS-ATM / bluesky / bluesky / traffic / asas / SSD.py View on Github external
xy_los = np.vstack((x_los,y_los)).T
                        # Scale darttip
                        VO = pyclipper.scale_to_clipper(tuple(map(tuple,xy_los)))
                    # Add scaled VO to clipper
                    pc.AddPath(VO, pyclipper.PT_CLIP, True)
                    # For RotA it is possible to ignore
                    if priocode == "RS6":
                        if brg_own[j] >= -20. and brg_own[j] <= 110.:
                            # Head-on or converging from right
                            pc_rota.AddPath(VO, pyclipper.PT_CLIP, True)
                        elif brg_other[j] <= -110. or brg_other[j] >= 110.:
                            # In overtaking position
                            pc_rota.AddPath(VO, pyclipper.PT_CLIP, True)
                    # Detect conflicts for smaller layer in RS7 and RS8
                    if priocode == "RS7" or priocode == "RS8":
                        if pyclipper.PointInPolygon(pyclipper.scale_to_clipper((gseast[i],gsnorth[i])),VO):
                            asas.inconf2[i] = True
                    if priocode == "RS5":
                        if pyclipper.PointInPolygon(pyclipper.scale_to_clipper((apeast[i],apnorth[i])),VO):
                            asas.ap_free[i] = False

                # Execute clipper command
                FRV = pyclipper.scale_from_clipper(pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO))

                ARV = pc.Execute(pyclipper.CT_DIFFERENCE, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO)

                if not priocode == "RS1" and not priocode == "RS5" and not priocode == "RS7" and not priocode == "RS8":
                    # Make another clipper object for extra intersections
                    pc2 = pyclipper.Pyclipper()
                    # When using RotA clip with pc_rota
                    if priocode == "RS6":
                        # Calculate ARV for RotA
github revarbat / mandoline-py / mandoline / geometry2d.py View on Github external
def offset(paths, amount):
    pco = pyclipper.PyclipperOffset()
    pco.ArcTolerance = SCALING_FACTOR / 40
    paths = pyclipper.scale_to_clipper(paths, SCALING_FACTOR)
    pco.AddPaths(paths, pyclipper.JT_SQUARE, pyclipper.ET_CLOSEDPOLYGON)
    outpaths = pco.Execute(amount * SCALING_FACTOR)
    outpaths = pyclipper.scale_from_clipper(outpaths, SCALING_FACTOR)
    return outpaths
github revarbat / mandoline-py / mandoline / geometry2d.py View on Github external
def orient_path(path, dir):
    orient = pyclipper.Orientation(path)
    path = pyclipper.scale_to_clipper(path, SCALING_FACTOR)
    if orient != dir:
        path = pyclipper.ReversePath(path)
    path = pyclipper.scale_from_clipper(path, SCALING_FACTOR)
    return path
github ecopoesis / nek-type-a / body / body.py View on Github external
def fillet_shape(poly, radius, convex = True):
    """
    fillet a polygon
    :param poly: list of point tuples describing the polygon
    :param radius: radius to fillet by
    :param convex: if true fillet the convex corners, if false fillet the concave corners
    :return: list of points representing the filleted polygon
    """
    scaled_radius = radius * 2 ** 31

    pco = pyclipper.PyclipperOffset()
    pco.ArcTolerance = arc_tolerance

    # shrink
    pco.AddPath(pyclipper.scale_to_clipper(poly), pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
    expanded = pco.Execute(-scaled_radius if convex else scaled_radius)

    # expand
    pco.Clear()
    pco.AddPath(expanded[0], pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
    result = pyclipper.scale_from_clipper(pco.Execute(scaled_radius if convex else -scaled_radius))

    return map(lambda point: (point[0], point[1]), result[0])
github TUDelft-CNS-ATM / bluesky / bluesky / traffic / asas / SSD.py View on Github external
# Detect conflicts for smaller layer in RS7 and RS8
                    if priocode == "RS7" or priocode == "RS8":
                        if pyclipper.PointInPolygon(pyclipper.scale_to_clipper((gseast[i],gsnorth[i])),VO):
                            asas.inconf2[i] = True
                    if priocode == "RS5":
                        if pyclipper.PointInPolygon(pyclipper.scale_to_clipper((apeast[i],apnorth[i])),VO):
                            asas.ap_free[i] = False

                # Execute clipper command
                FRV = pyclipper.scale_from_clipper(pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO))

                ARV = pc.Execute(pyclipper.CT_DIFFERENCE, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO)

                if not priocode == "RS1" and not priocode == "RS5" and not priocode == "RS7" and not priocode == "RS8":
                    # Make another clipper object for extra intersections
                    pc2 = pyclipper.Pyclipper()
                    # When using RotA clip with pc_rota
                    if priocode == "RS6":
                        # Calculate ARV for RotA
                        ARV_rota = pc_rota.Execute(pyclipper.CT_DIFFERENCE, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO)
                        if len(ARV_rota) > 0:
                            pc2.AddPaths(ARV_rota, pyclipper.PT_CLIP, True)
                    else:
                        # Put the ARV in there, make sure it's not empty
                        if len(ARV) > 0:
                            pc2.AddPaths(ARV, pyclipper.PT_CLIP, True)

                # Scale back
                ARV = pyclipper.scale_from_clipper(ARV)

                # Check if ARV or FRV is empty
                if len(ARV) == 0:
github typemytype / booleanOperations / Lib / booleanOperations / booleanOperationManager.py View on Github external
def clipExecute(subjectContours, clipContours, operation, subjectFillType="nonZero",
                clipFillType="nonZero"):
    pc = pyclipper.Pyclipper()

    for i, subjectContour in enumerate(subjectContours):
        try:
            pc.AddPath(subjectContour, pyclipper.PT_SUBJECT)
        except pyclipper.ClipperException:
            # skip invalid paths with no area
            if pyclipper.Area(subjectContour) != 0:
                raise InvalidSubjectContourError("contour %d is invalid for clipping" % i)

    for j, clipContour in enumerate(clipContours):
        try:
            pc.AddPath(clipContour, pyclipper.PT_CLIP)
        except pyclipper.ClipperException:
            # skip invalid paths with no area
            if pyclipper.Area(clipContour) == 0:
                raise InvalidClippingContourError("contour %d is invalid for clipping" % j)