Commit 0467986e authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

New Mesh.loadVertices method. Mesh.vertices only raises a notification if

the vertex set changes.
parent 211858f3
...@@ -104,6 +104,32 @@ class FreesurferMesh(fslmesh.Mesh): ...@@ -104,6 +104,32 @@ class FreesurferMesh(fslmesh.Mesh):
self.addVertices(verts, f, select=False) self.addVertices(verts, f, select=False)
def loadVertices(self, infile, key=None, **kwargs):
"""Overrides :meth:`.Mesh.loadVertices`. If the given ``infile``
looks like a Freesurfer file, it is loaded via
``nibabel.freesurfer.load_geometry``. Otherwise, it is passed to
:meth:`.Mesh.loadVertices`.
"""
if not fslpath.hasExt(infile, GEOMETRY_EXTENSIONS):
return fslmesh.Mesh.loadVertices(
self, infile, key, **kwargs)
infile = op.abspath(infile)
if key is None:
key = infile
# TODO merge metadata
vertices, indices, meta, comment = nibfs.read_geometry(
infile,
read_metadata=True,
read_stamp=True)
vertices = vertices.reshape(self.vertices.shape)
self.addVertices(vertices, key, **kwargs)
return vertices
def loadVertexData(self, infile, key=None): def loadVertexData(self, infile, key=None):
"""Overrides :meth:`.Mesh.loadVertexData`. If the given ``infile`` """Overrides :meth:`.Mesh.loadVertexData`. If the given ``infile``
looks like a Freesurfer file, it is loaded via the looks like a Freesurfer file, it is loaded via the
......
...@@ -104,12 +104,38 @@ class GiftiMesh(fslmesh.Mesh): ...@@ -104,12 +104,38 @@ class GiftiMesh(fslmesh.Mesh):
self.setMeta( sfile, surfimg) self.setMeta( sfile, surfimg)
def loadVertices(self, infile, key=None, *args, **kwargs):
"""Overrides the :meth:`.Mesh.loadVertices` method.
Attempts to load vertices for this ``GiftiMesh`` from the given
``infile``, which may be a GIFTI file or a plain text file containing
vertices.
"""
if not infile.endswith('.gii'):
return fslmesh.Mesh.loadVertices(
self, infile, key, *args, **kwargs)
infile = op.abspath(infile)
if key is None:
key = infile
surfimg, vertices, _ = loadGiftiMesh(infile)
vertices = vertices.reshape(self.vertices.shape[0], 3)
self.addVertices(vertices, key, *args, **kwargs)
self.setMeta( infile, surfimg)
return vertices
def loadVertexData(self, infile, key=None): def loadVertexData(self, infile, key=None):
"""Overrides the :meth:`.TriangleMesh.loadVertexData` method. """Overrides the :meth:`.Mesh.loadVertexData` method.
Attempts to load data associated with each vertex of this Attempts to load data associated with each vertex of this
``GiftiMesh`` from the given ``dataSource``, which may be ``GiftiMesh`` from the given ``infile``, which may be a GIFTI file or
a GIFTI file or a plain text file which contains vertex data. a plain text file which contains vertex data.
""" """
infile = op.abspath(infile) infile = op.abspath(infile)
......
...@@ -91,6 +91,7 @@ class Mesh(notifier.Notifier, meta.Meta): ...@@ -91,6 +91,7 @@ class Mesh(notifier.Notifier, meta.Meta):
.. autosummary:: .. autosummary::
:nosignatures: :nosignatures:
loadVertices
addVertices addVertices
selectedVertices selectedVertices
vertexSets vertexSets
...@@ -253,14 +254,19 @@ class Mesh(notifier.Notifier, meta.Meta): ...@@ -253,14 +254,19 @@ class Mesh(notifier.Notifier, meta.Meta):
def vertices(self, key): def vertices(self, key):
"""Select the current vertex set - a ``KeyError`` is raised """Select the current vertex set - a ``KeyError`` is raised
if no vertex set with the specified ``key`` has been added. if no vertex set with the specified ``key`` has been added.
When the current vertex set is changed, a notification is emitted
through the :class:`.Notifier` interface, with the topic
``'vertices'``.
""" """
# Force a key error if # Force a key error if
# the key is invalid # the key is invalid
self.__vertices[key] self.__vertices[key]
self.__selected = key
self.notify(topic='vertices') if self.__selected != key:
self.__selected = key
self.notify(topic='vertices')
@property @property
...@@ -320,6 +326,36 @@ class Mesh(notifier.Notifier, meta.Meta): ...@@ -320,6 +326,36 @@ class Mesh(notifier.Notifier, meta.Meta):
return lo, hi return lo, hi
def loadVertices(self, infile, key=None, **kwargs):
"""Loads vertex data from the given ``infile``, and adds it as a vertex
set with the given ``key``. This implementation supports loading vertex
data from white-space delimited text files via ``numpy.loadtxt``, but
sub-classes may override this method to support additional file types.
:arg infile: File to load data from.
:arg key: Key to pass to :meth:`addVertices`. If not provided,
set to ``infile`` (converted to an absolute path)
All of the other arguments are passed through to :meth:`addVertices`.
:returns: The loaded vertices.
"""
infile = op.abspath(infile)
if key is None:
key = infile
vertices = np.loadtxt(infile)
vertices = vertices.reshape(self.vertices.shape)
self.addVertices(vertices, key, **kwargs)
return vertices
def addVertices(self, vertices, key=None, select=True, fixWinding=False): def addVertices(self, vertices, key=None, select=True, fixWinding=False):
"""Adds a set of vertices to this ``Mesh``. """Adds a set of vertices to this ``Mesh``.
...@@ -387,6 +423,8 @@ class Mesh(notifier.Notifier, meta.Meta): ...@@ -387,6 +423,8 @@ class Mesh(notifier.Notifier, meta.Meta):
:arg key: Key to pass to :meth:`addVertexData`. If not provided, :arg key: Key to pass to :meth:`addVertexData`. If not provided,
set to ``infile`` (converted to an absolute path) set to ``infile`` (converted to an absolute path)
:returns: The loaded vertex data.
""" """
infile = op.abspath(infile) infile = op.abspath(infile)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment