Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. def voronoi_finite_polygons_2d(vor, radius=None):
  2.  
  3. new_regions = []
  4. new_vertices = vor.vertices.tolist()
  5.  
  6. center = vor.points.mean(axis=0)
  7. if radius is None:
  8. radius = vor.points.ptp().max()
  9.  
  10. # Construct a map containing all ridges for a given point
  11. all_ridges = {}
  12. for (p1, p2), (v1, v2) in zip(vor.ridge_points, vor.ridge_vertices):
  13. all_ridges.setdefault(p1, []).append((p2, v1, v2))
  14. all_ridges.setdefault(p2, []).append((p1, v1, v2))
  15.  
  16. # Reconstruct infinite regions
  17. for p1, region in enumerate(vor.point_region):
  18. vertices = vor.regions[region]
  19.  
  20. if all(v >= 0 for v in vertices):
  21. # finite region
  22. new_regions.append(vertices)
  23. continue
  24.  
  25. # reconstruct a non-finite region
  26. ridges = all_ridges[p1]
  27. new_region = [v for v in vertices if v >= 0]
  28.  
  29. for p2, v1, v2 in ridges:
  30. if v2 < 0:
  31. v1, v2 = v2, v1
  32. if v1 >= 0:
  33. # finite ridge: already in the region
  34. continue
  35.  
  36. # Compute the missing endpoint of an infinite ridge
  37.  
  38. t = vor.points[p2] - vor.points[p1] # tangent
  39. t /= np.linalg.norm(t)
  40. n = np.array([-t[1], t[0]]) # normal
  41.  
  42. midpoint = vor.points[[p1, p2]].mean(axis=0)
  43. direction = np.sign(np.dot(midpoint - center, n)) * n
  44. far_point = vor.vertices[v2] + direction * radius
  45.  
  46. new_region.append(len(new_vertices))
  47. new_vertices.append(far_point.tolist())
  48.  
  49. # sort region counterclockwise
  50. vs = np.asarray([new_vertices[v] for v in new_region])
  51. c = vs.mean(axis=0)
  52. angles = np.arctan2(vs[:,1] - c[1], vs[:,0] - c[0])
  53. new_region = np.array(new_region)[np.argsort(angles)]
  54.  
  55. # finish
  56. new_regions.append(new_region.tolist())
  57.  
  58. return new_regions, np.asarray(new_vertices)
  59.  
  60. def get_polygons(X, Y, yard_line):
  61. points = list(zip(X, Y))
  62.  
  63. vor = Voronoi(points)
  64. regions, vertices = voronoi_finite_polygons_2d(vor)
  65.  
  66. x_diff = max(yard_line-vor.min_bound[0], vor.max_bound[0]-yard_line)
  67. min_x = yard_line - x_diff - 1
  68. max_x = yard_line + x_diff + 1
  69. min_y = vor.min_bound[1]
  70. max_y = vor.max_bound[1]
  71.  
  72. mins = np.tile((min_x, min_y), (vertices.shape[0], 1))
  73. bounded_vertices = np.max((vertices, mins), axis=0)
  74. maxs = np.tile((max_x, max_y), (vertices.shape[0], 1))
  75. bounded_vertices = np.min((bounded_vertices, maxs), axis=0)
  76.  
  77. box_vertices = [[min_x, min_y], [min_x, max_y], [max_x, max_y], [max_x, min_y]]
  78. box = Polygon(box_vertices)
  79.  
  80. polygons = []
  81. for region in regions:
  82. polygon = vertices[region]
  83. try:
  84. poly = Polygon(polygon)
  85. poly = poly.intersection(box)
  86. # Currently getting this error for PlayID 20181014052141
  87. except shapely.geos.TopologicalError:
  88. polygon = np.clip(polygon, -1000000000,1000000000)
  89. poly = Polygon(polygon)
  90. poly = poly.intersection(box)
  91. polygons.append(poly)
  92.  
  93. return polygons
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement