def contour(self, points): """ sets the contour of the tail performing some sanity tests """ # do a first regularization points = regions.regularize_contour_points(points) spacing = self.contour_spacing # make the contour line equidistant points = curves.make_curve_equidistant(points, spacing=spacing) # regularize again, just to be sure points = regions.regularize_contour_points(points) # call parent setter shapes.Polygon.contour.fset(self, points)
def connect_burrow_chunks(self, burrow_chunks): """ takes a list of burrow chunks and connects them such that in the end all burrow chunks are connected to the ground line. """ if len(burrow_chunks) == 0: return [] dist_max = self.params['burrows/chunk_dist_max'] # build the contour profiles of the burrow chunks linear_rings = [geometry.LinearRing(c) for c in burrow_chunks] # handle all burrows close to the ground connected, disconnected = [], [] for k, ring in enumerate(linear_rings): ground_dist = self.ground.linestring.distance(ring) if ground_dist < dist_max: # burrow is close to ground if 1 < ground_dist: burrow_chunks[k] = \ self._connect_burrow_to_structure(burrow_chunks[k], self.ground.linestring) connected.append(k) else: disconnected.append(k) assert (set(connected) | set(disconnected)) == set(range(len(burrow_chunks))) # calculate distances to other burrows burrow_dist = np.empty([len(burrow_chunks)]*2) np.fill_diagonal(burrow_dist, np.inf) for x, contour1 in enumerate(linear_rings): for y, contour2 in enumerate(linear_rings[x+1:], x+1): dist = contour1.distance(contour2) burrow_dist[x, y] = dist burrow_dist[y, x] = dist # handle all remaining chunks, which need to be connected to other chunks while connected and disconnected: # find chunks which is closest to all the others dist = burrow_dist[disconnected, :][:, connected] k1, k2 = np.unravel_index(dist.argmin(), dist.shape) if dist[k1, k2] > dist_max: # don't connect structures that are too far from each other break c1, c2 = disconnected[k1], connected[k2] # k1 is chunk to connect, k2 is closest chunk to connect it to # connect the current chunk to the other structure structure = geometry.LinearRing(burrow_chunks[c2]) enlarged_chunk = self._connect_burrow_to_structure(burrow_chunks[c1], structure) # merge the two structures poly1 = geometry.Polygon(enlarged_chunk) poly2 = regions.regularize_polygon(geometry.Polygon(structure)) poly = poly1.union(poly2).buffer(0.1) # find and regularize the common contour contour = regions.get_enclosing_outline(poly) contour = regions.regularize_linear_ring(contour) contour = contour.coords # replace the current chunk by the merged one burrow_chunks[c1] = contour # replace all other burrow chunks with the same id id_c2 = id(burrow_chunks[c2]) for k, bc in enumerate(burrow_chunks): if id(bc) == id_c2: burrow_chunks[k] = contour # mark the cluster as connected del disconnected[k1] connected.append(c1) # return the unique burrow structures burrows = [] connected_chunks = (burrow_chunks[k] for k in connected) for contour in unique_based_on_id(connected_chunks): contour = regions.regularize_contour_points(contour) try: burrow = Burrow(contour) except ValueError: continue else: if burrow.area >= self.params['burrows/area_min']: burrows.append(burrow) return burrows