Skip to main content

Multilabel marching cubes and simplification of volumetric data.

Project description

zmesh: Multi-Label Marching Cubes & Mesh Simplification

Tests PyPI version

from zmesh import Mesher

labels = ... # some dense volumetric labeled image
mesher = Mesher( (4,4,40) ) # anisotropy of image

# initial marching cubes pass
# close controls whether meshes touching
# the image boundary are left open or closed
mesher.mesh(labels, close=False) 

meshes = []
for obj_id in mesher.ids():
  meshes.append(
    mesher.get(
      obj_id, 
      normals=False, # whether to calculate normals or not

      # tries to reduce triangles by this factor
      # 0 disables simplification
      reduction_factor=100, 

      # Max tolerable error in physical distance
      # note: if max_error is not set, the max error
      # will be set equivalent to one voxel along the 
      # smallest dimension.
      max_error=8,
      # whether meshes should be centered in the voxel
      # on (0,0,0) [False] or (0.5,0.5,0.5) [True]
      voxel_centered=False, 
    )
  )
  mesher.erase(obj_id) # delete high res mesh

mesher.clear() # clear memory retained by mesher

mesh = meshes[0]
mesh = mesher.simplify(
  mesh, 
  # same as reduction_factor in get
  reduction_factor=100, 
  # same as max_error in get
  max_error=40, 
  compute_normals=False, # whether to also compute face normals
) # apply simplifier to a pre-existing mesh

# compute normals without simplifying
mesh = mesher.compute_normals(mesh) 

mesh.vertices
mesh.faces 
mesh.normals
mesh.triangles() # compute triangles from vertices and faces

# Extremely common obj format
with open('iconic_doge.obj', 'wb') as f:
  f.write(mesh.to_obj())

# Common binary format
with open('iconic_doge.ply', 'wb') as f:
  f.write(mesh.to_ply())

# Neuroglancer Precomputed format
with open('10001001:0', 'wb') as f:
  f.write(mesh.to_precomputed())

Note: As of the latest version, mesher.get_mesh has been deprecated in favor of mesher.get which fixes a long standing bug where you needed to transpose your data in order to get a mesh in the correct orientation.

Installation

If binaries are not available for your system, ensure you have a C++ compiler installed.

pip install zmesh

Performance Tuning & Notes

  • The mesher will consume about double memory in 64 bit mode if the size of the object exceeds <1023, 1023, 511> on the x, y, or z axes. This is due to a limitation of the 32-bit format.
  • The mesher is ambidextrous, it can handle C or Fortran order arrays.
  • The maximum vertex range supported .simplify after converting to voxel space is 220 (appx. 1M) due to the packed 64-bit vertex format.

Related Projects

  • zi_lib - zmesh makes heavy use of Aleks' C++ library.
  • Igneous - Visualization of connectomics data using cloud computing.

Credits

Thanks to Aleks Zlateski for creating and sharing this beautiful mesher.

Later changes by Will Silversmith, Nico Kemnitz, and Jingpeng Wu.

References

  1. W. Lorensen and H. Cline. "Marching Cubes: A High Resolution 3D Surface Construction Algorithm". pp 163-169. Computer Graphics, Volume 21, Number 4, July 1987. (link)
  2. M. Garland and P. Heckbert. "Surface simplification using quadric error metrics". SIGGRAPH '97: Proceedings of the 24th annual conference on Computer graphics and interactive techniques. Pages 209–216. August 1997. doi: 10.1145/258734.258849 (link)
  3. H. Hoppe. "New Quadric Metric for Simplifying Meshes with Appearance Attributes". IEEE Visualization 1999 Conference. pp. 59-66. doi: 10.1109/VISUAL.1999.809869 (link)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zmesh-1.7.1.tar.gz (262.4 kB view hashes)

Uploaded Source

Built Distributions

zmesh-1.7.1-cp312-cp312-win_amd64.whl (132.6 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

zmesh-1.7.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

zmesh-1.7.1-cp312-cp312-macosx_10_9_x86_64.whl (181.1 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

zmesh-1.7.1-cp312-cp312-macosx_10_9_universal2.whl (360.7 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

zmesh-1.7.1-cp311-cp311-win_amd64.whl (135.6 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

zmesh-1.7.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

zmesh-1.7.1-cp311-cp311-macosx_10_9_x86_64.whl (186.2 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

zmesh-1.7.1-cp311-cp311-macosx_10_9_universal2.whl (364.5 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

zmesh-1.7.1-cp310-cp310-win_amd64.whl (135.9 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

zmesh-1.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

zmesh-1.7.1-cp310-cp310-macosx_10_9_x86_64.whl (188.6 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

zmesh-1.7.1-cp310-cp310-macosx_10_9_universal2.whl (361.6 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

zmesh-1.7.1-cp39-cp39-win_amd64.whl (138.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

zmesh-1.7.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

zmesh-1.7.1-cp39-cp39-macosx_10_9_x86_64.whl (188.8 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

zmesh-1.7.1-cp39-cp39-macosx_10_9_universal2.whl (362.9 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

zmesh-1.7.1-cp38-cp38-win_amd64.whl (138.5 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

zmesh-1.7.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

zmesh-1.7.1-cp38-cp38-macosx_11_0_universal2.whl (362.3 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ universal2 (ARM64, x86-64)

zmesh-1.7.1-cp38-cp38-macosx_10_9_x86_64.whl (186.4 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

zmesh-1.7.1-cp37-cp37m-win_amd64.whl (136.6 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

zmesh-1.7.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.7 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

zmesh-1.7.1-cp37-cp37m-macosx_10_9_x86_64.whl (184.9 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page