Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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:
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)
"""
Use the pyclipper library to "union" a polygon on its own. This operation
uses the even-odd rule to determine which points are in the interior of
the polygon, and can reconstruct the orientation of the polygon from that.
The pyclipper library is robust, and uses integer coordinates, so should
not produce any additional degeneracies.
Before cleaning the polygon, we remove all degenerate inners. This is
useful to remove inners which have collapsed to points or lines, which can
interfere with the cleaning process.
"""
# drop all degenerate inners
clean_shape = _drop_degenerate_inners(shape)
pc = pyclipper.Pyclipper()
try:
pc.AddPaths(_coords(clean_shape), pyclipper.PT_SUBJECT, True)
# note: Execute2 returns the polygon tree, not the list of paths
result = pc.Execute2(pyclipper.CT_UNION, pyclipper.PFT_EVENODD)
except pyclipper.ClipperException:
return MultiPolygon([])
return _polytree_to_shapely(result)
----------
polygon1 : list of arrays
Vertices of the first polygon in counterclockwise order.
polygon1 : list of arrays
Vertices of the second polygon in counterclockwise order.
Returns
-------
intersection : list of arrays
Vertices of the intersection in counterclockwise order.
"""
from pyclipper import Pyclipper, PT_CLIP, PT_SUBJECT, CT_INTERSECTION
from pyclipper import scale_to_clipper, scale_from_clipper
# could be accelerated by removing the scale_to/from_clipper()
subj, clip = (polygon1,), polygon2
pc = Pyclipper()
pc.AddPath(scale_to_clipper(clip), PT_CLIP)
pc.AddPaths(scale_to_clipper(subj), PT_SUBJECT)
solution = pc.Execute(CT_INTERSECTION)
if not solution:
return []
return scale_from_clipper(solution)[0]
solution = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO)
# pts = np.array(region_poly, np.int32)
# pts = pts.reshape((-1,1,2))
# cv2.polylines(img,[pts],True,(0,0,255), thickness=3)
area = 0
for path in solution:
area += pyclipper.Area(path)
scores_i.append(area)
background_scores = []
total_areas = []
for p in polys:
pc = pyclipper.Pyclipper()
try:
pc.AddPath(p, pyclipper.PT_CLIP, True)
except:
total_areas.append(np.inf)
background_scores.append(np.inf)
# print p
print "Failed to assign text line, probably not an issue"
continue
pc.AddPaths([r['bounding_poly'] for r in regions], pyclipper.PT_SUBJECT, True)
solution = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO)
area = 0
for path in solution:
area += pyclipper.Area(path)
simple_path = pyclipper.SimplifyPolygon(p, pyclipper.PFT_NONZERO)
def _prepare_clipper(self, poly):
"""Prepare 2D polygons for clipping operations.
:param poly: The clip polygon.
:returns: A Pyclipper object.
"""
s1 = pc.scale_to_clipper(self.vertices_list)
s2 = pc.scale_to_clipper(poly.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
# Get vertices in an x- and y-array of size (ntraf-1)*3x1
x = np.concatenate((gseast[i_other],
x1[ind] * fix + gseast[i_other],
x2[ind] * fix + gseast[i_other]))
y = np.concatenate((gsnorth[i_other],
y1[ind] * fix + gsnorth[i_other],
y2[ind] * fix + gsnorth[i_other]))
# Reshape [(ntraf-1)x3] and put arrays in one array [(ntraf-1)x3x2]
x = np.transpose(x.reshape(3, np.shape(i_other)[0]))
y = np.transpose(y.reshape(3, np.shape(i_other)[0]))
xy = np.dstack((x,y))
# Make a clipper object
pc = pyclipper.Pyclipper()
# Add circles (ring-shape) to clipper as subject
pc.AddPaths(pyclipper.scale_to_clipper(circle_tup), pyclipper.PT_SUBJECT, True)
# Extra stuff needed for RotA
if priocode == "RS6":
# Make another clipper object for RotA
pc_rota = pyclipper.Pyclipper()
pc_rota.AddPaths(pyclipper.scale_to_clipper(circle_tup), pyclipper.PT_SUBJECT, True)
# Bearing calculations from own view and other view
brg_own = np.mod((np.rad2deg(qdr[ind]) + fix_ang - hdg[i]) + 540., 360.) - 180.
brg_other = np.mod((np.rad2deg(qdr[ind]) + 180. - fix_ang - hdg[i_other]) + 540., 360.) - 180.
# Add each other other aircraft to clipper as clip
for j in range(np.shape(i_other)[0]):
## Debug prints
## print(traf.id[i] + " - " + traf.id[i_other[j]])
Args:
box1: The coordinates for box 1 as a list of (x, y) coordinates
box2: The coordinates for box 2 in same format as box1.
"""
if len(box1) == 2:
x1, y1 = box1[0]
x2, y2 = box1[1]
box1 = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])
if len(box2) == 2:
x1, y1 = box2[0]
x2, y2 = box2[1]
box2 = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])
if any(cv2.contourArea(np.int32(box)[:, np.newaxis, :]) == 0 for box in [box1, box2]):
warnings.warn('A box with zero area was detected.')
return 0
pc = pyclipper.Pyclipper()
pc.AddPath(np.int32(box1), pyclipper.PT_SUBJECT, closed=True)
pc.AddPath(np.int32(box2), pyclipper.PT_CLIP, closed=True)
intersection_solutions = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_EVENODD,
pyclipper.PFT_EVENODD)
union_solutions = pc.Execute(pyclipper.CT_UNION, pyclipper.PFT_EVENODD, pyclipper.PFT_EVENODD)
union = sum(cv2.contourArea(np.int32(points)[:, np.newaxis, :]) for points in union_solutions)
intersection = sum(
cv2.contourArea(np.int32(points)[:, np.newaxis, :]) for points in intersection_solutions)
return intersection / union