def calculate_gradient(points, dps, cells_dic, polygon_bd): npoints = points.shape[0] D = np.zeros((npoints,1), np.float32) for npt in range(npoints): if npt not in cells_dic: continue cell = cells_dic[npt] poly = polygon(dps[cell]) region = poly & polygon_bd D[npt] = region.area() return D
def intersect_ray_polygon(ray, poly, scale, thresh=0.001): a = ray[0:2] b = ray[0:2] + scale * ray[2:4] c = [b[0] + thresh, b[1] + thresh] tri = polygon(np.array([a, b, c])) region = poly & tri npoints = region.nPoints() if npoints == 0: return [] equal_thresh = 0.00001 for ii in range(npoints): if np.sum(np.abs(region[0][ii] - a) > equal_thresh): return region[0][ii] return []
def Geojsonify(data, Type='Point'): # Determine which function to use if Type == 'Point': return point(data) elif Type == 'LineString': return line_string(data) elif Type == 'Polygon': return polygon(data) else: print("Error: Type has to be either Point, LineString, or Polygon")
if _iter % 20 == 0: plot_diagram(points, faces_pd, cells_dic, dps, dps_bd, points_bd, scale = 1.0) return new_h def calculate_gradient(points, dps, cells_dic, polygon_bd): npoints = points.shape[0] D = np.zeros((npoints,1), np.float32) for npt in range(npoints): if npt not in cells_dic: continue cell = cells_dic[npt] poly = polygon(dps[cell]) region = poly & polygon_bd D[npt] = region.area() return D if __name__ == '__main__': npoints = 50 points = np.random.random((npoints, 2)) minxy = np.min(points, axis = 0) - 0.1 maxxy = np.max(points, axis = 0) + 0.1 scale = np.sqrt(np.sum((maxxy - minxy)**2)) points_bd = np.array([[minxy[0], minxy[1]], [minxy[0], maxxy[1]], [maxxy[0], maxxy[1]], [maxxy[0], minxy[1]], [minxy[0], minxy[1]]]) polygon_bd = polygon(points_bd) delta = (maxxy[0] - minxy[0]) * (maxxy[1] - minxy[1]) / npoints * np.ones((npoints, 1), np.float32) new_h = None new_h = discrete_optimal_transform(points, delta, polygon_bd, scale, new_h, learnrate = 0.05, max_iter = 2000, epsilon = 1e-5)
def power_diagram_2d(points, h=None): npoints = points.shape[0] if h is None: h = np.zeros((points.shape[0], 1), np.float32) pl = np.concatenate( (points, np.sum(points * points, axis=1, keepdims=True) - h), axis=1) hull = convexhull(pl) ind = hull.equations[:, 2] < 0 nfaces = np.sum(ind) faces = hull.simplices[ind] dps = np.zeros((nfaces, 2), np.float32) dic = dict() for _face, cnt in zip(faces, xrange(nfaces)): dps[cnt] = face_dual_uv(pl[_face]) left, right = _cmp(_face[0], _face[1]) _dict_update(dic, '%d_%d' % (left, right), cnt) left, right = _cmp(_face[0], _face[2]) _dict_update(dic, '%d_%d' % (left, right), cnt) left, right = _cmp(_face[2], _face[1]) _dict_update(dic, '%d_%d' % (left, right), cnt) cells_dic = dict() flag_bd = np.zeros((npoints), np.int32) for _face, cnt in zip(faces, xrange(nfaces)): for pos in range(3): if _face[pos] in cells_dic: continue cell = [cnt] flag_bd[_face[pos]] = compute_cell(_face, pos, faces, dic, cell) cells_dic.update({_face[pos]: cell}) dps_bd = np.zeros((np.sum(flag_bd), 2), np.float32) minxy = np.min(dps, axis=0) - 1.0 maxxy = np.max(dps, axis=0) + 1.0 scale = np.sqrt(np.sum((maxxy - minxy)**2)) points_bd = np.array([[minxy[0], minxy[0]], [minxy[0], maxxy[1]], [maxxy[0], maxxy[1]], [maxxy[0], minxy[1]], [minxy[0], minxy[0]]]) polygon_bd = polygon(points_bd) cnt = 0 for ii in xrange(npoints): if flag_bd[ii]: flag_bd[ii] = nfaces + cnt cnt += 1 cnt = 0 for ii in xrange(npoints): if flag_bd[ii] == 0: continue cell = cells_dic[ii] for pp in faces[cell[0]]: if pp != ii and flag_bd[pp]: B_inx = pp for pp in faces[cell[-1]]: if pp != ii and flag_bd[pp]: C_inx = pp if ccw(points[ii], points[B_inx], points[C_inx]): face_num = cell[0] other_inx = B_inx cell.insert(0, flag_bd[ii]) cell.append(flag_bd[C_inx]) else: face_num = cell[-1] other_inx = C_inx cell.append(flag_bd[ii]) cell.insert(0, flag_bd[B_inx]) vec = points[ii] - points[other_inx] ray = np.array([dps[face_num, 0], dps[face_num, 1], -vec[1], vec[0]]) point_inter = intersect_ray_polygon(ray, polygon_bd, scale) if point_inter == []: print 'intersect_ray_polygon error!' dps_bd[cnt] = np.array(point_inter) cnt += 1 return dps, dps_bd, cells_dic, faces