fsl.data.mesh
This module provides the Mesh class, which represents a
3D model made of triangles.
See also the following modules:
This module provides the
VTKMeshclass, for loading triangle meshes from VTK files.This class provides classes and functions for working with GIFTI files.
This module provides the
FreesurferMeshclass, which can be used for loading Freesurfer geometry and vertex data files.
A handful of standalone functions are provided in this module, for doing various things with meshes:
Calculates face normals for the mesh described by
verticesandindices.Calculates vertex normals for the mesh described by
verticesandindices.Determines whether the triangle winding order, for the mesh described by
verticesandindices, needs to be flipped.
- exception fsl.data.mesh.IncompatibleVerticesError[source]
Bases:
ValueErrorValueErrorraised by theMesh.addVertices()method if an attempt is made to add a vertex set with the wrong number of vertices.- __module__ = 'fsl.data.mesh'
- __weakref__
list of weak references to the object (if defined)
- class fsl.data.mesh.Mesh(*args, **kwargs)[source]
-
The
Meshclass represents a 3D model. A mesh is defined by a collection ofNvertices, andMtriangles. The triangles are defined by(M, 3)indices into the list of vertices.A
Meshinstance has the following attributes:nameA name, typically the file name sans-suffix.
dataSourceFull path to the mesh file (or
Noneif there is no file associated with this mesh).nverticesThe number of vertices in the mesh.
verticesA
(n, 3)array containing the currently selected vertices. You can assign a vertex set key to this attribute to change the selected vertex set.boundsThe lower and upper bounds
indicesA
(m, 3)array containing the vertex indices formtrianglesnormalsA
(m, 3)array containing face normals for the trianglesvnormalsA
(n, 3)array containing vertex normals for the the current vertices.trimesh(if the trimesh library is present) A
trimesh.Trimeshobject which can be used for geometric queries on the mesh.Vertex sets
A
Meshobject can be associated with multiple sets of vertices, but only one set of triangles. Vertices can be added via theaddVertices()method. Each vertex set must be associated with a unique key - you can then select the current vertex set via thevertices()property. MostMeshmethods will raise aKeyErrorif you have not added any vertex sets, or selected a vertex set. The following methods are available for managing vertex sets:Loads vertex data from the given
infile, and adds it as a vertex set with the givenkey.Adds a set of vertices to this
Mesh.Returns the key of the currently selected vertex set.
Returns a list containing the keys of all vertex sets.
Note
Internally the
Meshclass may store two versions of the triangles, with opposite unwinding orders. It keeps track of the required triangle unwinding order for each vertex set, so that theindices()method will return the appropriate copy for the currently selected vertex set.Vertex data
A
Meshobject can store vertex-wise data. The following methods can be used for adding/retrieving vertex data:Loads vertex-wise data from the given
infile, and adds it with the givenkey.Adds a vertex-wise data set to the
Mesh.Returns the vertex data for the given
keyfrom the internal vertex data cache.Returns a list of keys for all loaded vertex data sets.
Clears the internal vertex data cache - see the
addVertexData()andgetVertexData()methods.Notification
The
Meshclass inherits from theNotifierclass. Whenever theMeshvertex set is changed, a notification is emitted via theNotifierinterface, with a topic of'vertices'. When this occurs, thevertices(),bounds(),normals()andvnormalsproperties will all change so that they return data specific to the newly selected vertex set.Metadata
The
Meshclass also inherits from theMetaclass, so any metadata associated with theMeshmay be added via those methods.Geometric queries
If the
trimeshlibrary is present, the following methods may be used to perform geometric queries on a mesh:Calculate the intersection between the mesh, and the rays defined by
originsanddirections.Calculate the intersection of this
TriangleMeshwith the plane defined bynormalandorigin.Identifies the nearest vertex to each of the provided points.
- __init__(indices=None, name='mesh', dataSource=None, vertices=None, fixWinding=False)[source]
Create a
Meshinstance.Before a
Meshcan be used, some vertices must be added via theaddVertices()method.- Parameters:
indices – A list of indices into the vertex data, defining the mesh triangles. If not provided, must be provided after creation via the
indices()setter method.name – A name for this
Mesh.dataSource – The data source for this
Mesh.vertices – Initial vertex set to add - given the key
'default'.fixWinding – Ignored if
vertices is None. Passed through to theaddVertices()method along withvertices.
- property name
Returns the name of this
Mesh.
- property dataSource
Returns the data source of this
Mesh.
- property nvertices
Returns the number of vertices in the mesh.
- property vertices
The
(N, 3)vertices of this mesh.
- property indices
The
(M, 3)triangles of this mesh. ReturnsNoneif indices have not yet been assigned.
- property normals
A
(M, 3)array containing surface normals for every triangle in the mesh, normalised to unit length.
- property vnormals
A
(N, 3)array containing normals for every vertex in the mesh.
- property bounds
Returns a tuple of values which define a minimal bounding box that will contain all of the currently selected vertices in this
Meshinstance. The bounding box is arranged like so:((xlow, ylow, zlow), (xhigh, yhigh, zhigh))Returns
Noneif indices or vertices have not yet been assigned.
- loadVertices(infile, key=None, **kwargs)[source]
Loads vertex data from the given
infile, and adds it as a vertex set with the givenkey. This implementation supports loading vertex data from white-space delimited text files vianumpy.loadtxt, but sub-classes may override this method to support additional file types.- Parameters:
infile – File to load data from.
key – Key to pass to
addVertices(). If not provided, set toinfile(converted to an absolute path)
All of the other arguments are passed through to
addVertices().- Returns:
The loaded vertices.
- addVertices(vertices, key=None, select=True, fixWinding=False)[source]
Adds a set of vertices to this
Mesh.- Parameters:
vertices – A (n, 3) array containing
nvertices, compatible with the indices specified in__init__().key – A key for this vertex set. If
Nonedefaults to'default'.select – If
True(the default), this vertex set is made the currently selected vertex set.fixWinding – Defaults to
False. IfTrue, the vertex winding order of every triangle is is fixed so they all have outward-facing normal vectors.
- Returns:
The vertices, possibly reshaped
- Raises:
IncompatibleVerticesErrorif the providedverticesarray has the wrong number of vertices.
- loadVertexData(infile, key=None)[source]
Loads vertex-wise data from the given
infile, and adds it with the givenkey. This implementation supports loading data from whitespace-delimited text files vianumpy.loadtxt, but sub-classes may override this method to support additional file types.- Parameters:
infile – File to load data from.
key – Key to pass to
addVertexData(). If not provided, set toinfile(converted to an absolute path)
- Returns:
The loaded vertex data.
- addVertexData(key, vdata)[source]
Adds a vertex-wise data set to the
Mesh. It can be retrieved by passing the specifiedkeyto thegetVertexData()method.- Returns:
The vertex data, possibly reshaped.
- getVertexData(key)[source]
Returns the vertex data for the given
keyfrom the internal vertex data cache. If there is no vertex data iwth the given key, aKeyErroris raised.
- clearVertexData()[source]
Clears the internal vertex data cache - see the
addVertexData()andgetVertexData()methods.
- property trimesh
Reference to a
trimesh.Trimeshobject which can be used for geometric operations on the mesh.If the
trimeshorrtreelibraries are not available, this function returnsNone, and none of the geometric query methods will do anything.
- rayIntersection(origins, directions, vertices=False)[source]
Calculate the intersection between the mesh, and the rays defined by
originsanddirections.- Parameters:
origins – Sequence of ray origins
directions – Sequence of ray directions
- Returns:
A tuple containing:
A
(n, 3)array containing the coordinates where the mesh was intersected by each of thenrays.A
(n,)array containing the indices of the triangles that were intersected by each of thenrays.
- nearestVertex(points)[source]
Identifies the nearest vertex to each of the provided points.
- Parameters:
points – A
(n, 3)array containing the points to query.- Returns:
A tuple containing:
A
(n, 3)array containing the nearest vertex for for each of theninput points.A
(n,)array containing the indices of each vertex.A
(n,)array containing the distance from each point to the nearest vertex.
- __annotations__ = {}
- __module__ = 'fsl.data.mesh'
- planeIntersection(normal, origin, distances=False)[source]
Calculate the intersection of this
TriangleMeshwith the plane defined bynormalandorigin.- Parameters:
normal – Vector defining the plane orientation
origin – Point defining the plane location
distances – If
True, barycentric coordinates for each intersection line vertex are calculated and returned, giving their respective distance from the intersected triangle vertices.
- Returns:
A tuple containing
A
(m, 2, 3)array containingmvertices: of a set of lines, defining the plane intersectionA
(m,)array containing the indices of themtriangles that were intersected.(if
distances is True) A(m, 2, 3)array containing the barycentric coordinates of each line vertex with respect to its intersected triangle.
- fsl.data.mesh.calcFaceNormals(vertices, indices)[source]
Calculates face normals for the mesh described by
verticesandindices.- Parameters:
vertices – A
(n, 3)array containing the mesh vertices.indices – A
(m, 3)array containing the mesh triangles.
- Returns:
A
(m, 3)array containing normals for every triangle in the mesh.
- fsl.data.mesh.calcVertexNormals(vertices, indices, fnormals)[source]
Calculates vertex normals for the mesh described by
verticesandindices.- Parameters:
vertices – A
(n, 3)array containing the mesh vertices.indices – A
(m, 3)array containing the mesh triangles.fnormals – A
(m, 3)array containing the face/triangle normals.
- Returns:
A
(n, 3)array containing normals for every vertex in the mesh.
- fsl.data.mesh.needsFixing(vertices, indices, fnormals, loBounds, hiBounds)[source]
Determines whether the triangle winding order, for the mesh described by
verticesandindices, needs to be flipped.If this function returns
True, the givenindicesandfnormalsneed to be adjusted so that all face normals are facing outwards from the centre of the mesh. The necessary adjustments are as follows:indices[:, [1, 2]] = indices[:, [2, 1]] fnormals = fnormals * -1
- Parameters:
vertices – A
(n, 3)array containing the mesh vertices.indices – A
(m, 3)array containing the mesh triangles.fnormals – A
(m, 3)array containing the face/triangle normals.loBounds – A
(3, )array contaning the low vertex bounds.hiBounds – A
(3, )array contaning the high vertex bounds.
- Returns:
Trueif theindicesandfnormalsneed to be adjusted,Falseotherwise.