measure the distance between two points along a 3d surface in matlab

To calculate the distance between two points along a 3D surface in MATLAB, we can use the pdist() and dsearchn() functions. Here's an example code snippet:

main.m
% Define surface points and triangulation
[x, y, z] = peaks(50); % generate surface points
tri = delaunay(x, y);  % triangulate surface

% Define two points to calculate distance between
p1 = [7, -3, 4];   % point 1
p2 = [-3, 6, -2];  % point 2

% Find closest vertices to the two points
[idx1, ~] = dsearchn([x(:), y(:), z(:)], tri, p1);
[idx2, ~] = dsearchn([x(:), y(:), z(:)], tri, p2);

% Calculate distance along surface using triangulation
dist = pdist([x(tri(idx1, :)), y(tri(idx1, :)), z(tri(idx1, :));...
             x(tri(idx2, :)), y(tri(idx2, :)), z(tri(idx2, :)))];
580 chars
16 lines

In this code, the surface points are generated using the peaks() function and then triangulated with the delaunay() function. Two points (p1 and p2) are then defined, and the dsearchn() function is used to find the closest vertices to each of these points in the triangulated surface. Finally, the distance between these two vertices along the surface is calculated using the pdist() function.

Note that this method assumes a continuous surface and will not work well for surfaces with discontinuities or sharp edges.

gistlibby LogSnag