Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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)
total_area = 0
for path in simple_path:
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]
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
def poly_intersect(subj, clip):
"""
"""
pc = pyclipper.Pyclipper()
pc.AddPath(clip, pyclipper.PT_CLIP, True)
pc.AddPath(subj, pyclipper.PT_SUBJECT, True)
solution = pc.Execute(
pyclipper.CT_INTERSECTION, pyclipper.PFT_EVENODD, pyclipper.PFT_EVENODD
)
return np.array(solution)
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)
bounds = pc.GetBounds()
if (bounds.bottom, bounds.left, bounds.top, bounds.right) == (0, 0, 0, 0):
# do nothing if there are no paths
return []
try:
solution = pc.Execute(_operationMap[operation],
_fillTypeMap[subjectFillType],
_fillTypeMap[clipFillType])
except pyclipper.ClipperException as exc:
raise ExecutionError(exc)
def clip(subj, clip_paths, subj_closed=True):
if not subj:
return []
if not clip_paths:
return []
pc = pyclipper.Pyclipper()
if subj:
subj = pyclipper.scale_to_clipper(subj, SCALING_FACTOR)
pc.AddPaths(subj, pyclipper.PT_SUBJECT, subj_closed)
if clip_paths:
clip_paths = pyclipper.scale_to_clipper(clip_paths, SCALING_FACTOR)
pc.AddPaths(clip_paths, pyclipper.PT_CLIP, True)
out_tree = pc.Execute2(pyclipper.CT_INTERSECTION, pyclipper.PFT_EVENODD, pyclipper.PFT_EVENODD)
outpaths = pyclipper.PolyTreeToPaths(out_tree)
outpaths = pyclipper.scale_from_clipper(outpaths, SCALING_FACTOR)
return outpaths
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]
x_los = leg * np.sin(angles_los)
y_los = leg * np.cos(angles_los)
# Put in array of correct format
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()
polys = []
for t in trimmed_polys:
p = t[:,:2,0].tolist() + t[::-1,:2,1].tolist()
polys.append(p)
scores = []
for i, r in enumerate(regions):
region_poly = r['bounding_poly']
scores_i = []
scores.append(scores_i)
for p in polys:
pc = pyclipper.Pyclipper()
try:
pc.AddPath(p, pyclipper.PT_CLIP, True)
except:
scores_i.append(0)
# print p
print "Failed to assign text line, probably not an issue"
continue
pc.AddPath(region_poly, pyclipper.PT_SUBJECT, True)
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)
def clip_poly_pair(pc, p, q):
""""
pc: an instance of pyclipper.Pyclipper.
p: the polygon by which to clip other polygon q.
pc and p may be held fixed through use of functools.partial so that
multiple q may be clipped by p.
"""
pc.Clear()
pc.AddPath(q, pyclipper.PT_SUBJECT, True)
pc.AddPath(p, pyclipper.PT_CLIP, True)
clip_polys = pc.Execute(clip_type=pyclipper.CT_INTERSECTION)
return clip_polys