seung-lab / zmesh

Marching Cubes & Mesh Simplification on multi-label 3D images.
GNU General Public License v3.0
59 stars 8 forks source link

User Friendly Mesh Output #3

Closed william-silversmith closed 5 years ago

william-silversmith commented 5 years ago

Igneous does the following to make user friendly meshes. It would be good to integrate some of this logic. The output of zmesh.get_mesh is fixed point premultiplied by 2, which is not obvious...

  def _create_mesh(self, obj_id):
    mesh = self._mesher.get_mesh(
      obj_id,
      simplification_factor=self.options['simplification_factor'],
      max_simplification_error=self.options['max_simplification_error']
    )

    self._mesher.erase(obj_id)

    vertices = self._update_vertices(
      np.array(mesh['points'], dtype=np.float32)
    )
    vertex_index_format = [
      np.uint32(len(vertices) / 3), # Number of vertices (3 coordinates)
      vertices,
      np.array(mesh['faces'], dtype=np.uint32)
    ]
    return b''.join([array.tobytes() for array in vertex_index_format])

  def _update_vertices(self, points):
    # zi_lib meshing multiplies vertices by 2.0 to avoid working with floats,
    # but we need to recover the exact position for display
    # Note: points are already multiplied by resolution, but missing the offset
    points /= 2.0
    resolution = self._volume.resolution
    xmin, ymin, zmin = self._bounds.minpt - self.options['low_padding']
    points[0::3] = points[0::3] + xmin * resolution.x
    points[1::3] = points[1::3] + ymin * resolution.y
    points[2::3] = points[2::3] + zmin * resolution.z
    return points