neuroglancer_scripts.mesh module

I/O for meshes in formats understood by Neuroglancer.

Neuroglancer understands two file formats for meshes:

  • A binary “precomputed” format for meshes that correspond to a segmentation layer.

  • A sub-set of the legacy VTK ASCII format can be used with the vtk:// datasource to represent a mesh that is not associated with a voxel segmentation (SingleMesh layer). Vertices may have arbitrary scalar attributes.

exception neuroglancer_scripts.mesh.InvalidMeshDataError[source]

Bases: Exception

Raised when mesh data cannot be decoded properly.

neuroglancer_scripts.mesh.affine_transform_mesh(vertices, triangles, coord_transform)[source]

Transform a mesh through an affine transformation.

Parameters:
  • vertices (numpy.ndarray) – the list of vertices of the mesh. Must be convertible to an array of size Nx3, type float32.

  • triangles (numpy.ndarray) – the list of triangles of the mesh, in an array of size Mx3.

  • coord_transform (numpy.ndarray) – the affine transform to be applied to mesh vertices, as a 3×4 or 4×4 matrix in homogeneous coordinates.

Returns:

transformed (vertices, triangles)

Return type:

tuple

Vertex coordinates are transformed with the provided affine transformation. Triangles will be flipped if the transformation has a negative determinant, in order to conserve the inside/outside of the mesh.

neuroglancer_scripts.mesh.read_precomputed_mesh(file)[source]

Load a mesh in Neuroglancer pre-computed format.

Parameters:

file – a file-like object opened in binary mode (its read method is expected to return bytes objects).

Returns tuple:

a 2-tuple (vertices, triangles), where vertices is an array of size Nx3 and type float32 containing the vertex coordinates expressed in nanometres; and triangles is an array of size Mx3 and uint32 data type.

neuroglancer_scripts.mesh.save_mesh_as_neuroglancer_vtk(file, vertices, triangles, vertex_attributes=None, title='')[source]

Store a mesh in VTK format such that it can be read by Neuroglancer.

Parameters:
  • file – a file-like object opened in text mode (its write method will be called with str objects).

  • vertices (numpy.ndarray) – the list of vertices of the mesh. Must be convertible to an array of size Nx3, type float32. Coordinates will be interpreted by Neuroglancer in nanometres.

  • triangles (numpy.ndarray) – the list of triangles of the mesh. Must be convertible to an array of size Mx3, with integer data type.

  • vertex_attributes (list) – an iterable containing a description of vertex attributes (see below).

  • title (str) – a title (comment) for the dataset. Cannot contain n, will be truncated to 255 characters.

Raises:

AssertionError – if the inputs do not match the constraints above

Each element of vertex_attributes must be a mapping (e.g. dict) with the following keys:

name

The name of the vertex attribute, as a str. Cannot contain white space.

values

The values of the attribute. Must be convertible to an array of size N or NxC, where N is the number of vertices, and C is the number of channels for the attribute (between 1 and 4).

The output uses a sub-set of the legacy VTK ASCII format, which can be read by Neuroglancer (as of https://github.com/google/neuroglancer/blob/a8ce681660864ab3ac7c1086c0b4262e40f24707/src/neuroglancer/datasource/vtk/parse.ts).

neuroglancer_scripts.mesh.save_mesh_as_precomputed(file, vertices, triangles)[source]

Store a mesh in Neuroglancer pre-computed format.

Parameters:
  • file – a file-like object opened in binary mode (its write method will be called with bytes objects).

  • vertices (numpy.ndarray) – the list of vertices of the mesh. Must be convertible to an array of size Nx3 and type float32. Coordinates will be interpreted by Neuroglancer in nanometres.

  • triangles (numpy.ndarray) – the list of triangles of the mesh. Must be convertible to an array of size Mx3 and uint32 data type.

Raises:

AssertionError – if the inputs do not match the constraints above