def test_qvoronoi(self): data = [[0, 0], [-0.5, -0.5], [-0.5, 0.5], [0.5, -0.5], [0.5, 0.5]] self.assertEqual( ['2', '4', '0 -0.5', '-0.5 0', '0.5 0', '0 0.5'], qvoronoi("p", data)) self.assertEqual([ '2', '5 5 1', '-10.101 -10.101', '0 -0.5', '-0.5 0', '0.5 0', '0 0.5', '4 4 2 1 3', '3 2 0 1', '3 4 0 2', '3 3 0 1', '3 4 0 3', '2', '4', '0 -0.5', '-0.5 0', '0.5 0', '0 0.5' ], qvoronoi("o p", data))
def test_qvoronoi(self): data = [[0,0], [-0.5, -0.5], [-0.5, 0.5], [0.5, -0.5], [0.5, 0.5]] self.assertEqual(['2', '4', '0 -0.5', '-0.5 0', '0.5 0', '0 0.5'], qvoronoi("p", data)) self.assertEqual(['2', '5 5 1', '-10.101 -10.101', '0 -0.5', '-0.5 0', '0.5 0', '0 0.5', '4 4 2 1 3', '3 2 0 1', '3 4 0 2', '3 3 0 1', '3 4 0 3', '2', '4', '0 -0.5', '-0.5 0', '0.5 0', '0 0.5'], qvoronoi("o p", data))
def __init__(self, points, add_bounding_box=False): """ Args: points: All the points as a sequence of sequences. e.g., [[-0.5, -0.5], [-0.5, 0.5], [0.5, -0.5], [0.5, 0.5]] add_bounding_box: If True, a hypercube corresponding to the extremes of each coordinate will be added to the list of points. """ self.points = list(points) dim = map(len, self.points) if max(dim) != min(dim): raise ValueError("Input points must all have the same dimension!") self.dim = dim[0] if add_bounding_box: coord_ranges = zip(np.amin(points, 0), np.amax(points, 0)) for coord in itertools.product(*coord_ranges): self.points.append(coord) output = qvoronoi("o Fv", self.points) output.pop(0) nvertices, nregions, i = map(int, output.pop(0).split()) self.vertices = [map(float, output.pop(0).split()) for i in xrange(nvertices)] self.regions = [map(int, output.pop(0).split()[1:]) for i in xrange(nregions)] output.pop(0) ridges = {} for line in output: val = map(int, line.split()) ridges[tuple(val[1:3])] = val[3:] self.ridges = ridges
def __init__(self, points, add_bounding_box=False): """ Initializes a VoronoiTess from points. Args: points ([[float]]): All the points as a sequence of sequences. e.g., [[-0.5, -0.5], [-0.5, 0.5], [0.5, -0.5], [0.5, 0.5]] add_bounding_box (bool): If True, a hypercube corresponding to the extremes of each coordinate will be added to the list of points. """ self.points = list(points) dim = [len(i) for i in self.points] if max(dim) != min(dim): raise ValueError("Input points must all have the same dimension!") self.dim = dim[0] if add_bounding_box: coord_ranges = zip(np.amin(points, 0), np.amax(points, 0)) for coord in itertools.product(*coord_ranges): self.points.append(coord) output = qvoronoi("o Fv", self.points) output.pop(0) nvertices, nregions, i = [int(i) for i in output.pop(0).split()] self.vertices = [[float(j) for j in output.pop(0).split()] for i in range(nvertices)] self.regions = [[int(j) for j in output.pop(0).split()[1:]] for i in range(nregions)] output.pop(0) ridges = {} for line in output: val = [int(i) for i in line.split()] ridges[tuple(val[1:3])] = val[3:] self.ridges = ridges
def _qvertex_target(data, index): """ Input data should be in the form of a list of a list of floats. index is the index of the targeted point Returns the vertices of the voronoi construction around this target point. """ from pyhull import qvoronoi output = qvoronoi("p QV"+str(index), data) output.pop(0) output.pop(0) return [[float(i) for i in row.split()] for row in output]
def _qvertex_target(data, index): """ Input data should be in the form of a list of a list of floats. index is the index of the targeted point Returns the vertices of the voronoi construction around this target point. """ from pyhull import qvoronoi output = qvoronoi("p QV" + str(index), data) output.pop(0) output.pop(0) return [[float(i) for i in row.split()] for row in output]
def __init__(self, points, bounds=None): #points is list of points with same dimension D #bounds is 2-by-D dimension array where [0,:] are lower bounds and [1:] are upper bounds (hypercube) self.points = points self.v = pyhull.voronoi.VoronoiTess(points, False) self.v.vertices = np.array(self.v.vertices) self.dimension = len(points[0]) d = self.dimension out = qvoronoi("Fo Fi FD", points) n_outer = int(out[0]) outer_planes = np.zeros((n_outer, d + 1)) outer_pairs = [] for i, line in enumerate(out[1:1 + n_outer]): outer_pairs.append([int(x) for x in line.split()[1:3]]) outer_planes[i, :] = [float(x) for x in line.split()[3:]] n_inner = int(out[1 + n_outer]) inner_planes = np.zeros((n_inner, d + 1)) inner_pairs = [] for i, line in enumerate(out[2 + n_outer:]): inner_pairs.append([int(x) for x in line.split()[1:3]]) inner_planes[i, :] = [float(x) for x in line.split()[3:]] self.n_outer = n_outer self.outer_planes = outer_planes self.outer_pairs = outer_pairs self.n_inner = n_inner self.inner_planes = inner_planes self.inner_pairs = inner_pairs #make axis-aligned planes from the bounds given self.bounds = bounds if bounds is not None: self.bounds = np.array(bounds) bounds_planes = np.zeros((2 * self.dimension, self.dimension + 1)) for i in range(self.dimension): low = bounds[0][i] upp = bounds[1][i] plane = np.zeros((self.dimension + 1, )) plane[i] = 1 plane[-1] = -upp bounds_planes[2 * i, :] = plane plane[i] = -1 plane[-1] = low bounds_planes[2 * i + 1, :] = plane self.bounds_planes = bounds_planes else: self.bounds_planes = np.zeros((0, self.dimension + 1))