def snap(line_collection: vp.LineCollection, pitch: float) -> vp.LineCollection: """Snap all points to a grid with with a spacing of PITCH. This command moves every point of every paths to the nearest grid point. If sequential points of a segment end up on the same grid point, they are deduplicated. If the resulting path contains less than 2 points, it is discarded. Example: Snap all points to a grid of 3x3mm: vpype [...] snap 3mm [...] """ line_collection.scale(1 / pitch) new_lines = vp.LineCollection() for line in line_collection: new_line = np.round(line) idx = np.ones(len(new_line), dtype=bool) idx[1:] = new_line[1:] != new_line[:-1] if idx.sum() > 1: new_lines.append(np.round(new_line[idx])) new_lines.scale(pitch) return new_lines
def _all_line_collection_ops(lc: LineCollection): lc.merge(1) lc.scale(2, 2) lc.translate(2, 2) lc.rotate(10) lc.reloop(1) lc.skew(4, 4) lc.bounds()