|
| 1 | +from nibabel.filebasedimages import FileBasedHeader |
| 2 | +from nibabel.dataobj_images import DataobjImage |
| 3 | + |
| 4 | + |
| 5 | +class SurfaceGeometry: |
| 6 | + """ The SurfaceHeader is a mixin API """ |
| 7 | + |
| 8 | + def __init__(self, *args, **kwargs): |
| 9 | + """ Surface header objects have access to an internal |
| 10 | + ``_meshes`` dictionary that has keys that are mesh names. |
| 11 | + The values may be any structure that permits the class to |
| 12 | + provide coordinates and triangles on request. |
| 13 | + """ |
| 14 | + self._meshes = {} |
| 15 | + super().__init__(*args, **kwargs) |
| 16 | + |
| 17 | + @property |
| 18 | + def n_coords(self): |
| 19 | + """ Number of coordinates (vertices) |
| 20 | +
|
| 21 | + The default implementation loads coordinates. Subclasses may |
| 22 | + override with more efficient implementations. |
| 23 | + """ |
| 24 | + return self.get_coords().shape[0] |
| 25 | + |
| 26 | + @property |
| 27 | + def n_triangles(self): |
| 28 | + """ Number of triangles (faces) |
| 29 | +
|
| 30 | + The default implementation loads triangles. Subclasses may |
| 31 | + override with more efficient implementations. |
| 32 | + """ |
| 33 | + return self.get_triangles().shape[0] |
| 34 | + |
| 35 | + def get_coords(self, name=None): |
| 36 | + """ Nx3 array of coordinates in RAS+ space """ |
| 37 | + raise NotImplementedError |
| 38 | + |
| 39 | + def get_triangles(self, name=None): |
| 40 | + """ Mx3 array of indices into coordinate table """ |
| 41 | + raise NotImplementedError |
| 42 | + |
| 43 | + def get_mesh(self, name=None): |
| 44 | + return self.get_coords(name=name), self.get_triangles(name=name) |
| 45 | + |
| 46 | + def get_names(self): |
| 47 | + """ List of surface names that can be passed to |
| 48 | + ``get_{coords,triangles,mesh}`` |
| 49 | + """ |
| 50 | + return list(self._meshes.keys()) |
| 51 | + |
| 52 | + def decimate(self, *, ncoords=None, ratio=None): |
| 53 | + """ Return a SurfaceHeader with a smaller number of vertices that |
| 54 | + preserves the geometry of the original. |
| 55 | +
|
| 56 | + Please contribute a generic decimation algorithm at |
| 57 | + https://github.com/nipy/nibabel |
| 58 | + """ |
| 59 | + raise NotImplementedError |
| 60 | + |
| 61 | + def load_vertex_data(self): |
| 62 | + """ Return a SurfaceImage with data corresponding to each vertex """ |
| 63 | + raise NotImplementedError |
| 64 | + |
| 65 | + def load_face_data(self): |
| 66 | + """ Return a SurfaceImage with data corresponding to each face """ |
| 67 | + raise NotImplementedError |
| 68 | + |
| 69 | + |
| 70 | +class SurfaceHeader(FileBasedHeader): |
| 71 | + """ Template class to implement SurfaceHeader protocol """ |
| 72 | + def get_geometry(): |
| 73 | + """ Generate ``SurfaceGeometry`` object from header object |
| 74 | +
|
| 75 | + If no default geometry can be provided, returns ``None``. |
| 76 | + """ |
| 77 | + return None |
| 78 | + |
| 79 | + |
| 80 | +class SurfaceImage(DataobjImage): |
| 81 | + header_class = SurfaceHeader |
| 82 | + |
| 83 | + def __init__(self, dataobj, header=None, geometry=None, extra=None, file_map=None): |
| 84 | + """ Initialize image |
| 85 | +
|
| 86 | + The image is a combination of |
| 87 | + """ |
| 88 | + super().__init__(self, dataobj, header=header, extra=extra, file_map=file_map) |
| 89 | + if geometry is None: |
| 90 | + geometry = header.get_geometry() |
| 91 | + self._geometry = geometry |
| 92 | + |
| 93 | + def load_geometry(self, pathlike): |
| 94 | + """ Specify a header to a data-only image """ |
0 commit comments