# Pairwise distance between nodes on a surface

Hello AFNI gurus,

I need to compute the pairwise distance between every combination of nodes on a Freesurfer surface. I would like to be able to get both the Euclidean distance and the distance along the mesh. I can get the Euclidean distance by loading the surface into Matlab and using the coordinates, but how can I get the distance along the mesh? I wrote a little wrapper for SurfDist that can probably do it, but that seems a bit slow for a problem of this scale.

Any suggestions? I would be happy to use an external Matlab toolbox, if that is the easiest.

Best,

Peter J. Kohler

## pjkohler Wrote:

I need to compute the pairwise distance between
every combination of nodes on a Freesurfer
surface. I would like to be able to get both the
Euclidean distance and the distance along the
the surface into Matlab and using the coordinates,
but how can I get the distance along the mesh? I
wrote a little wrapper for SurfDist that can
probably do it, but that seems a bit slow for a
problem of this scale.

Any suggestions? I would be happy to use an
external Matlab toolbox, if that is the easiest.

The fast marching toolbox by Gabriel Peyre can do this, see https://github.com/gpeyre/matlab-toolboxes/tree/master/toolbox_fast_marching
In particular the perform_fast_marching_mesh function can be used for geodesic distances along the surface.

You may also be interested in the surfing toolbox (https://github.com/nno/surfing - I am the main author). It contains the fast marching toolbox code and also some helper functions to read and write surfaces (surfing_read and surfing_write).

Hi Nick,

thanks for your response, and apologies for not responding sooner. I actually use your surfing toolbox a lot, so I am aware of the perform_fast_marching_mesh function. I was hoping there was an even quicker/easier, perhaps non-Matlab way of doing it.

Best,

peter

## pjkohler Wrote:

thanks for your response, and apologies for not
responding sooner. I actually use your surfing
toolbox a lot, so I am aware of the
perform_fast_marching_mesh function. I was hoping
there was an even quicker/easier, perhaps
non-Matlab way of doing it.

Im my experience using dijkstra distances are usually quite similar to geodesic ones. I’m sure there are good implementations in many languages (C, python,…).
Or maybe this is helpful: https://www.cs.rochester.edu/~nelson/courses/csc_173/graphs/apsp.html

Hi Nick,

When I try to run perform_fast_marching_mesh.m, at the call to perform_front_propagation_mesh, Matlab crashes with a Segmentation Violation error. The compile_mex function runs fine, but I do get a bunch of warnings. Have you got any experience with this problem? I get the same error regardless of what version of the Fast Marching Toolbox I use.

Best,

Peter

Forgot to add, I am on a Macbook Pro running OSX 10.10 and Matlab 2013b, in case that helps.

## pjkohler Wrote:

When I try to run perform_fast_marching_mesh.m, at
the call to perform_front_propagation_mesh, Matlab
crashes with a Segmentation Violation error. The
compile_mex function runs fine, but I do get a
bunch of warnings. Have you got any experience
with this problem? I get the same error regardless
of what version of the Fast Marching Toolbox I
use.

I have not encountered this myself. I also use OSX and have used all recent versions of it and don’t remember having issues with compiling. Not sure if it depends on which compiler yo are using.

PM sent.

## pjkohler Wrote:

PM sent.

Code sent.

For your application, would it be useful to limit the dyjkstra search to those pairs of nodes that are less than some geometric distance? Geometric distances are very fast to calculate, so if you were interested only in those nodes say some distance apart, then you could cut the number of more complex searches down. Then SurfDist could be more amenable for this.

@Daniel: Yeah, that might be a good idea. Let me try that. Or should it perhaps be implemented in SurfDist directly?