|
15 | 15 | from nibabel import funcs as _nbfuncs
|
16 | 16 | from nibabel.nifti1 import intent_codes as INTENT_CODES
|
17 | 17 | from nibabel.cifti2 import Cifti2Image
|
| 18 | +import nibabel as nb |
18 | 19 |
|
19 | 20 | EQUALITY_TOL = 1e-5
|
20 | 21 |
|
@@ -88,6 +89,76 @@ def shape(self):
|
88 | 89 | return self._shape
|
89 | 90 |
|
90 | 91 |
|
| 92 | +class SurfaceMesh(SampledSpatialData): |
| 93 | + """Class to represent surface meshes.""" |
| 94 | + |
| 95 | + __slots__ = ["_triangles"] |
| 96 | + |
| 97 | + def __init__(self, dataset): |
| 98 | + """Create a sampling reference.""" |
| 99 | + self._shape = None |
| 100 | + |
| 101 | + if isinstance(dataset, SurfaceMesh): |
| 102 | + self._coords = dataset._coords |
| 103 | + self._triangles = dataset._triangles |
| 104 | + self._ndim = dataset._ndim |
| 105 | + self._npoints = dataset._npoints |
| 106 | + self._shape = dataset._shape |
| 107 | + return |
| 108 | + |
| 109 | + if isinstance(dataset, (str, Path)): |
| 110 | + dataset = _nbload(str(dataset)) |
| 111 | + |
| 112 | + if hasattr(dataset, "numDA"): # Looks like a Gifti file |
| 113 | + _das = dataset.get_arrays_from_intent(INTENT_CODES["pointset"]) |
| 114 | + if not _das: |
| 115 | + raise TypeError( |
| 116 | + "Input Gifti file does not contain reference coordinates." |
| 117 | + ) |
| 118 | + self._coords = np.vstack([da.data for da in _das]) |
| 119 | + _tris = dataset.get_arrays_from_intent(INTENT_CODES["triangle"]) |
| 120 | + self._triangles = np.vstack([da.data for da in _tris]) |
| 121 | + self._npoints, self._ndim = self._coords.shape |
| 122 | + self._shape = self._coords.shape |
| 123 | + return |
| 124 | + |
| 125 | + if isinstance(dataset, Cifti2Image): |
| 126 | + raise NotImplementedError |
| 127 | + |
| 128 | + raise ValueError("Dataset could not be interpreted as an irregular sample.") |
| 129 | + |
| 130 | + def check_sphere(self, tolerance=1.001): |
| 131 | + """Check sphericity of surface. |
| 132 | + Based on https://github.com/Washington-University/workbench/blob/\ |
| 133 | +7ba3345d161d567a4b628ceb02ab4471fc96cb20/src/Files/SurfaceResamplingHelper.cxx#L503 |
| 134 | + """ |
| 135 | + dists = np.linalg.norm(self._coords, axis=1) |
| 136 | + return (dists.min() * tolerance) > dists.max() |
| 137 | + |
| 138 | + def set_radius(self, radius=100): |
| 139 | + if not self.check_sphere(): |
| 140 | + raise ValueError("You should only set the radius on spherical surfaces.") |
| 141 | + dists = np.linalg.norm(self._coords, axis=1) |
| 142 | + self._coords = self._coords * (radius / dists).reshape((-1, 1)) |
| 143 | + |
| 144 | + @classmethod |
| 145 | + def from_arrays(cls, coordinates, triangles): |
| 146 | + darrays = [ |
| 147 | + nb.gifti.GiftiDataArray( |
| 148 | + coordinates.astype(np.float32), |
| 149 | + intent=nb.nifti1.intent_codes['NIFTI_INTENT_POINTSET'], |
| 150 | + datatype=nb.nifti1.data_type_codes['NIFTI_TYPE_FLOAT32'], |
| 151 | + ), |
| 152 | + nb.gifti.GiftiDataArray( |
| 153 | + triangles.astype(np.int32), |
| 154 | + intent=nb.nifti1.intent_codes['NIFTI_INTENT_TRIANGLE'], |
| 155 | + datatype=nb.nifti1.data_type_codes['NIFTI_TYPE_INT32'], |
| 156 | + ), |
| 157 | + ] |
| 158 | + gii = nb.gifti.GiftiImage(darrays=darrays) |
| 159 | + return cls(gii) |
| 160 | + |
| 161 | + |
91 | 162 | class ImageGrid(SampledSpatialData):
|
92 | 163 | """Class to represent spaces of gridded data (images)."""
|
93 | 164 |
|
|
0 commit comments