|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import igneous.task_creation as tc |
| 4 | +from taskqueue import LocalTaskQueue |
| 5 | +import numpy as np |
| 6 | +from cloudvolume import CloudVolume |
| 7 | + |
| 8 | + |
| 9 | +def generate_basic_object(output_path, shape=(1024, 1024, 512), radius=0.8): |
| 10 | + x = np.linspace(-1, 1, shape[0]) |
| 11 | + y = np.linspace(-1, 1, shape[1]) |
| 12 | + z = np.linspace(-1, 1, shape[2]) |
| 13 | + xx, yy, zz = np.meshgrid(x, y, z, indexing="ij") |
| 14 | + |
| 15 | + sphere = xx**2 + yy**2 + zz**2 <= radius**2 |
| 16 | + sphere = sphere.astype(np.uint32) |
| 17 | + |
| 18 | + CloudVolume.from_numpy( |
| 19 | + sphere, |
| 20 | + vol_path=output_path, |
| 21 | + resolution=(20, 20, 40), |
| 22 | + chunk_size=(256, 256, 128), |
| 23 | + layer_type="segmentation", |
| 24 | + progress=True, |
| 25 | + compress=False, |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def convert(layer_path, num_lod): |
| 30 | + tq = LocalTaskQueue() |
| 31 | + tasks = tc.create_meshing_tasks( |
| 32 | + layer_path, |
| 33 | + mip=0, |
| 34 | + shape=(256, 256, 256), |
| 35 | + sharded=True, |
| 36 | + fill_missing=True, |
| 37 | + max_simplification_error=10, |
| 38 | + simplification=True, |
| 39 | + mesh_dir="mesh", |
| 40 | + ) |
| 41 | + tq.insert(tasks) |
| 42 | + tq.execute() |
| 43 | + |
| 44 | + tasks = tc.create_mesh_manifest_tasks( |
| 45 | + layer_path, |
| 46 | + magnitude=3, |
| 47 | + mesh_dir="mesh", |
| 48 | + ) |
| 49 | + tq.insert(tasks) |
| 50 | + tq.execute() |
| 51 | + |
| 52 | + tasks = tc.create_sharded_multires_mesh_tasks( |
| 53 | + layer_path, |
| 54 | + num_lod=num_lod, |
| 55 | + min_chunk_size=(32, 32, 16), |
| 56 | + mesh_dir="mesh", |
| 57 | + ) |
| 58 | + tq.insert(tasks) |
| 59 | + tq.execute() |
| 60 | + |
| 61 | + |
| 62 | +def clean_mesh_folder(output_path: str | Path, mesh_directory: str = "mesh"): |
| 63 | + """Remove unnecessary files from the mesh folder |
| 64 | +
|
| 65 | + The conversion produces extra unnecessary files, so we clean up |
| 66 | + The only needed files are the info file, and any file that ends with .shard |
| 67 | + However, if the sharding fails, we may have other files, so we only delete |
| 68 | + If there is at least one shard file |
| 69 | + """ |
| 70 | + output_path = Path(output_path) |
| 71 | + mesh_dir = output_path / mesh_directory |
| 72 | + if mesh_dir.exists() and any(f.name.endswith(".shard") for f in mesh_dir.iterdir()): |
| 73 | + for f in mesh_dir.iterdir(): |
| 74 | + if f.is_file() and not (f.name == "info" or f.name.endswith(".shard")): |
| 75 | + f.unlink() |
| 76 | + |
| 77 | + |
| 78 | +def main(output_folder="test_mesh", port=1030, num_lod=4): |
| 79 | + here = Path(__file__).parent |
| 80 | + output_path = here / output_folder |
| 81 | + output_cloudvolume = f"file://{output_path.resolve()}" |
| 82 | + generate_basic_object(output_path=output_cloudvolume) |
| 83 | + convert(output_cloudvolume, num_lod) |
| 84 | + clean_mesh_folder(output_path=output_path) |
| 85 | + |
| 86 | + cv = CloudVolume(output_cloudvolume) |
| 87 | + cv.viewer(port=port) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments