Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: faster multi-resolution meshing #182

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion igneous/task_creation/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def create_downsampling_tasks(
"""
def ds_shape(mip, chunk_size=None, factor=None):
nonlocal num_mips
if chunk_size:
if chunk_size is not None:
shape = Vec(*chunk_size)
else:
shape = vol.meta.chunk_size(mip)[:3]
Expand Down
54 changes: 52 additions & 2 deletions igneous/tasks/mesh/multires.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def generate_lods(
assert num_lods >= 0, num_lods

lods = [ mesh ]
next_mesh = mesh

# from pyfqmr documentation:
# threshold = alpha * (iteration + K) ** agressiveness
Expand All @@ -306,9 +307,9 @@ def generate_lods(
# deleting a vertex.
for i in range(1, num_lods+1):
simplifier = pyfqmr.Simplify()
simplifier.setMesh(mesh.vertices, mesh.faces)
simplifier.setMesh(next_mesh.vertices, next_mesh.faces)
simplifier.simplify_mesh(
target_count=max(int(len(mesh.faces) / (decimation_factor ** i)), 4),
target_count=max(int(len(next_mesh.faces) / decimation_factor), 4),
aggressiveness=aggressiveness,
preserve_border=True,
verbose=False,
Expand All @@ -324,6 +325,7 @@ def generate_lods(
lods.append(
Mesh(*simplifier.getMesh())
)
next_mesh = lods[-1]

return lods

Expand Down Expand Up @@ -495,6 +497,54 @@ def create_octree_level_from_mesh(mesh, chunk_shape, lod, num_lods):
Create submeshes by slicing the orignal mesh to produce smaller chunks
by slicing them from x,y,z dimensions.

This creates (2^lod)^3 submeshes.
"""
try:
return create_octree_level_from_mesh_accelerated(mesh, chunk_shape, lod, num_lods)
except RuntimeError:
return create_octree_level_from_mesh_generalized(mesh, chunk_shape, lod, num_lods)

def create_octree_level_from_mesh_accelerated(mesh, chunk_shape, lod, num_lods):
"""
Uses zmesh.chunk_mesh to accelerate slicing meshes, but it can only handle
vertices that are one 26-connected grid space apart and raises a RuntimeError
if this condition isn't met.
"""
if lod == num_lods - 1:
return ([ mesh ], ((0,0,0),) )

scale = Vec(*(np.array(chunk_shape) * (2 ** lod)))
chunked_meshes = zmesh.chunk_mesh(mesh, scale)
gpts = list(chunked_meshes.keys())

nodes = []

for gpt in gpts:
mesh = chunked_meshes[gpt]
if mesh.is_empty():
continue
# test for totally degenerate meshes by checking if
# all of two axes match, meaning the mesh must be a
# point or a line.
if np.sum([ np.all(mesh.vertices[:,i] == mesh.vertices[0,i]) for i in range(3) ]) >= 2:
continue

nodes.append(gpt)

# Sort in Z-curve order
nodes.sort(
key=functools.cmp_to_key(lambda x, y: cmp_zorder(x, y))
)

submeshes = [ chunked_meshes[node] for node in nodes ]

return (submeshes, nodes)

def create_octree_level_from_mesh_generalized(mesh, chunk_shape, lod, num_lods):
"""
Create submeshes by slicing the orignal mesh to produce smaller chunks
by slicing them from x,y,z dimensions.

This creates (2^lod)^3 submeshes.
"""
if lod == num_lods - 1:
Expand Down
14 changes: 12 additions & 2 deletions igneous_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1617,9 +1617,8 @@ def memory_used(data_width, shape, factor):
@click.argument("path", type=CloudPath())
@click.option('--browser/--no-browser', default=True, is_flag=True, help="Open the dataset in the system's default web browser.")
@click.option('--port', default=1337, help="localhost server port for the file server.", show_default=True)
@click.option('--ng', default="https://neuroglancer-demo.appspot.com/", help="Alternative Neuroglancer webpage to use.", show_default=True)
@click.option('--ng', default=None, help="Alternative Neuroglancer webpage to use.", show_default=True)
@click.option('--pos', type=Tuple3(), default=None, help="Position in volume to open to.", show_default=True)

def view(path, browser, port, ng, pos):
"""
Open an on-disk dataset for viewing in neuroglancer.
Expand Down Expand Up @@ -1688,6 +1687,17 @@ def view(path, browser, port, ng, pos):
config["layers"][0]["shader"] = rgb_shader

fragment = urllib.parse.quote(jsonify(config))

has_alternative_codec = any([
scale["encoding"] in ["crackle", "zfpc", "kempressed", "fpzip"]
for scale in cv.scales
])

if ng is None:
if has_alternative_codec:
ng = "https://allcodecs-dot-neuromancer-seung-import.appspot.com/"
else:
ng = "https://neuroglancer-demo.appspot.com/"

url = f"{ng}#!{fragment}"
if browser:
Expand Down
Loading