def _get_tree(paths, closed): """ Add all paths to the tree and create mapping from ids of elements added to the tree to the elements data: Closed paths: [id of path, 1st point of segment, 2nd point of segment] Open paths [id of path, point of segment] """ # TODO: use models aabb? aabb = get_aabb(np.concatenate(paths)) tree = Quadtree([aabb.min[0], aabb.min[1], aabb.max[0], aabb.max[1]]) mapping = {} for path in paths: if closed: for i, j in zip(range(-1, len(path) - 1), range(len(path))): # add whole edge into the tree _add_edge(tree, mapping, path, i, j) else: _add_point(tree, mapping, path, 0) _add_point(tree, mapping, path, -1) tree.prune() return tree, mapping
def convert(contours=None, lines=None, polylines=None, outputfile=None, display=False): lines = _get_list(lines) contours = _get_list(contours) polylines = _get_list(polylines) all_elements = [lines, contours, polylines] all_l = sum(len(l) for l in all_elements) colors = _get_colors(all_l) info = {} text_width = 120 default_width = text_width + 15 * all_l line_height = 15 extra_height = 3 * line_height aabb = get_aabb(np.concatenate([np.concatenate(x) for x in all_elements if len(x) > 0])) add_x = -aabb.min[0] add_y = -aabb.min[1] add_v = to_ndarray([add_x, add_y]) result = "\n<svg width=\"800\" height=\"600\" viewbox=\"{} {} {} {}\">".format( 0, 0, int(aabb.max[0] - aabb.min[0]) + 10, int(aabb.max[1] - aabb.min[1]) + 10) for contour in contours: contour = contour + add_v color = colors.pop() result += "\n\t<polygon points=\"{}\" style=\"fill:none;stroke:{};stroke-width:0.1\" />".format( _format_polygon(contour), color) info.setdefault('Contours', []).append(color) for line in lines: line = line + add_v color = colors.pop() result += "\n\t<line x1=\"{}\" y1=\"{}\" x2=\"{}\" y2=\"{}\" style=\"stroke:{};stroke-width:0.1\" />".format( line[0][0], line[0][1], line[-1][0], line[-1][1], color) info.setdefault('Lines', []).append(color) for polyline in polylines: polyline = polyline + add_v color = colors.pop() result += "\n\t<polyline points=\"{}\" style=\"fill:none;stroke:{};stroke-width:0.1\" />".format( _format_polygon(polyline), color) info.setdefault('Polylines', []).append(color) result += "\n</svg>" result += "\n<br /><svg width=\"{}\" height=\"{}\">".format(default_width, extra_height) i = 0 for k, v in info.items(): x, y = 0, extra_height - (i * line_height) result += "\n\t<text x=\"{}\" y=\"{}\" font-family=\"sans-serif\" font-size=\"{}\" fill=\"black\">{} ({})</text>".format( x, y, line_height, k, len(v)) for j, color in enumerate(v): result += "\n\t<rect x=\"{}\" y=\"{}\" width=\"10\" height=\"10\" style=\"fill:{};stroke-width:0;\"/>".format( text_width + (j * 15), y - 10, color) i += 1 result += "\n</svg>" return result
def aabb(self): return get_aabb(self.mx)
def convert(contours=None, lines=None, polylines=None, outputfile=None, display=False): lines = _get_list(lines) contours = _get_list(contours) polylines = _get_list(polylines) all_elements = [lines, contours, polylines] all_l = sum(len(l) for l in all_elements) colors = _get_colors(all_l) info = {} text_width = 120 default_width = text_width + 15 * all_l line_height = 15 extra_height = 3 * line_height aabb = get_aabb( np.concatenate([np.concatenate(x) for x in all_elements if len(x) > 0])) add_x = -aabb.min[0] add_y = -aabb.min[1] add_v = to_ndarray([add_x, add_y]) result = "\n<svg width=\"800\" height=\"600\" viewbox=\"{} {} {} {}\">".format( 0, 0, int(aabb.max[0] - aabb.min[0]) + 10, int(aabb.max[1] - aabb.min[1]) + 10) for contour in contours: contour = contour + add_v color = colors.pop() result += "\n\t<polygon points=\"{}\" style=\"fill:none;stroke:{};stroke-width:0.1\" />".format( _format_polygon(contour), color) info.setdefault('Contours', []).append(color) for line in lines: line = line + add_v color = colors.pop() result += "\n\t<line x1=\"{}\" y1=\"{}\" x2=\"{}\" y2=\"{}\" style=\"stroke:{};stroke-width:0.1\" />".format( line[0][0], line[0][1], line[-1][0], line[-1][1], color) info.setdefault('Lines', []).append(color) for polyline in polylines: polyline = polyline + add_v color = colors.pop() result += "\n\t<polyline points=\"{}\" style=\"fill:none;stroke:{};stroke-width:0.1\" />".format( _format_polygon(polyline), color) info.setdefault('Polylines', []).append(color) result += "\n</svg>" result += "\n<br /><svg width=\"{}\" height=\"{}\">".format( default_width, extra_height) i = 0 for k, v in info.items(): x, y = 0, extra_height - (i * line_height) result += "\n\t<text x=\"{}\" y=\"{}\" font-family=\"sans-serif\" font-size=\"{}\" fill=\"black\">{} ({})</text>".format( x, y, line_height, k, len(v)) for j, color in enumerate(v): result += "\n\t<rect x=\"{}\" y=\"{}\" width=\"10\" height=\"10\" style=\"fill:{};stroke-width:0;\"/>".format( text_width + (j * 15), y - 10, color) i += 1 result += "\n</svg>" return result