Skip to content

Commit

Permalink
add a few python surf utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoopes committed Oct 25, 2021
1 parent 689f1d2 commit e8625fa
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
6 changes: 3 additions & 3 deletions mris_diff/mris_diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,11 +1087,11 @@ static int parse_commandline(int argc, char **argv) {
printf("Use Exact = %d\n",UseExact);
MRI *mindist;
if(UseExact){
MRISdistanceBetweenSurfacesExact(surf2, surf1);
mindist = MRIcopyMRIS(NULL, surf2, 0, "curv");
MRISdistanceBetweenSurfacesExact(surf2, surf1);
mindist = MRIcopyMRIS(NULL, surf2, 0, "curv");
}
else
mindist = MRISminDist(surf1, surf2);
mindist = MRISminDist(surf1, surf2);
if(mindist==NULL) exit(1);
printf("Writing mindist to %s\n",pargv[3]);
MRIwrite(mindist,pargv[3]);
Expand Down
27 changes: 27 additions & 0 deletions python/bindings/utils/surface.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "mrisurf_base.h"
#include "surface.h"
#include "xform.h"
#include "mrisurf_metricProperties.h"


namespace surf {

Expand Down Expand Up @@ -169,6 +171,21 @@ int computeEulerNumber(Bridge surf)
}


/*
Count number of face intersections.
*/
int countIntersections(Bridge surf)
{
MRIS *mris = surf.mris();
mrisMarkIntersections(mris);
int nintersections = 0;
for(int n = 0; n < mris->nvertices; n++) {
if (mris->vertices[n].marked) nintersections++;
}
return nintersections;
}


/*
Smoothes an overlay along mesh vertices.
*/
Expand All @@ -178,6 +195,16 @@ py::object smoothOverlay(Bridge surf, vol::Bridge overlay, int steps)
}


/*
Smoothes an overlay along mesh vertices.
*/
py::object surfaceDistance(Bridge surf1, Bridge surf2)
{
MRISdistanceBetweenSurfacesExact(surf2, surf1);
return vol::Bridge(MRIcopyMRIS(NULL, surf2, 0, "curv"));
}


/*
namespace vec {
Expand Down
6 changes: 5 additions & 1 deletion python/bindings/utils/surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ void write(Bridge surf, const std::string& filename);
void computeNormals(Bridge surf);
void computeTangents(Bridge surf);
int computeEulerNumber(Bridge surf);
int countIntersections(Bridge surf);

py::object surfaceDistance(Bridge surf1, Bridge surf2);

// overlay utils
py::array parameterize(Bridge surf, const arrayf<float>& overlay, int scale, std::string interp);
py::array sampleParameterization(Bridge surf, const arrayf<float>& image, std::string interp);
py::object smoothOverlay(Bridge surf, vol::Bridge overlay, int steps);


// surface submodule binding
inline void bind(py::module &m)
{
Expand All @@ -59,9 +61,11 @@ inline void bind(py::module &m)
m.def("compute_normals", &computeNormals);
m.def("compute_tangents", &computeTangents);
m.def("compute_euler", &computeEulerNumber);
m.def("count_intersections", &countIntersections);
m.def("parameterize", &parameterize);
m.def("sample_parameterization", &sampleParameterization);
m.def("smooth_overlay", &smoothOverlay);
m.def("distance", &surfaceDistance);
// m.def("read_directly", &read_directly);
}

Expand Down
17 changes: 17 additions & 0 deletions python/freesurfer/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def compute_euler(self):
'''Computes euler number of the mesh.'''
return bindings.surf.compute_euler(self)

def count_intersections(self):
'''Counts the number of face intersection in the mesh.'''
return bindings.surf.count_intersections(self)

def neighboring_faces(self, vertex):
'''List of face indices that neighbor a vertex.'''
return np.where(self.faces == vertex)[0]
Expand Down Expand Up @@ -216,3 +220,16 @@ def get_vertex_normals(self): # TODEP
def get_vertex_faces(self): # TODEP
'''Deprecated - use Surface.neighboring_faces instead'''
raise DeprecationWarning('get_vertex_faces has been removed! Use Surface.neighboring_faces or email andrew if you get this!!!!')


def distance(a, b):
return bindings.surf.distance(a, b)


def read_label(filename, nvertices):
mask = np.zeros(nvertices, dtype=bool)
with open(filename) as f:
lines = f.read().splitlines()[2:]
vertices = np.array([int(l.split()[0]) for l in lines])
mask[vertices] = True
return Overlay(mask)
3 changes: 3 additions & 0 deletions python/freesurfer/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ def transform_to_midspace(a, b, transform=None, resample=True, interp_method='li
target: Resampling target geometry. If None, the target is determined
by a secondary midspace computed between the *aligned* images. In this
case, the target shape and resolution is determined by `b`.
Returns:
mid_a, mid_b: Midspace-transformed images.
"""
from scipy.linalg import sqrtm

Expand Down

0 comments on commit e8625fa

Please sign in to comment.