I am working with bone STL data and slicing the bone to get an outline of the bone at that height. I need to find the centroid of the outline, but the points are not evenly spaced out around the curve. The points are also not plotted in order around the curve, which has made using a spline fit difficult. This is a picture of the points on the outline that I am working with. I have had a hard time finding a way to interpolate the entire closed loop. Let me know if you have any suggestions!
MATLAB: Interpolating and resampling around closed curve to calculate centroid
centroidclose curvecurve fitting
Related Solutions
hello
this would be my suggestion :
load data1.matload centroids.matx=data1(:,1)-centroids(:,1); % centered plot
y=data1(:,2)-centroids(:,2); % centered plot[theta,r] = cart2pol(x,y);% closing the circle
r(end+1) = r(1);theta(end+1) = theta(1);% sort theta in ascending order
[theta_sorted,ind] = sort(theta);r_sorted = r(ind);% remove duplicates before interpolation
[theta_unique,IA,IC] = unique(theta_sorted);r_unique = r_sorted(IA);% interpolation on n values to make it nicer
n = 360; % 1 deg resolution
theta_n = linspace(min(theta_unique),max(theta_unique),n);r_n = interp1(theta_unique,r_unique,theta_n);% convert to cartesian
[xn,yn] = pol2cart(theta_n,r_n);%% XY plot
figure(1),plot(x,y,'b.',xn,yn,'o');grid%% polar plot
figure(2),polarplot(theta_n, r_n,'--r')
Currently there is no tool in MATLAB that will directly give the desired output. However, we can compute the distances by creating code that implements a few mathematical steps, included between the dashed lines below.
-----------------------------------------------------------------------------------------------------------
Suppose we have a line AB on a unit sphere. We want to find the shortest distance from another point, C, to the line AB. In other words, consider the plane defined by A, B, and O (the center of the sphere, also the origin). Let's call this plane P. We are interested in the angle theta between the vector OC and plane P. If the sphere has radius r, the surface distance is simply r*theta.
1. Convert A, B, and C to Cartesian coordinates
2. Computer n, the unit normal vector to the plane defined by A, B, and O. n is the normalized cross product of OA and OB
3. Consider the Euclidean distance from C to plane P. This distance is equal to sin(theta), which is mathematically equal to cos(90-theta) if we define distance such that theta<90
4. cos(90-theta) can also be represented as the dot product of n and OC
5. set steps 3 and 4 equal to each other and solve for theta
-----------------------------------------------------------------------------------------------------------
% MATLAB code segment to implement the above
% V is an m x 2 matrix where each row is [lat long] of one point whose distance to AB we wish to calculate
% Convert everything from degrees to radians, then to Cartesian coordinates
a = deg2rad([lat1 lon1]);
[ax, ay, az] = sph2cart(a(2), a(1), 1);
A = [ax ay az];
b = deg2rad([lat2 lon2]);
[bx, by, bz] = sph2cart(b(2), b(1), 1);
B = [bx by bz];
c = deg2rad(V);
[cx, cy, cz] = sph2cart(c(:,2), c(:,1), 1);
C = [cx cy cz];
% Compute n, the unit normal vector to the plane defined by A, B, and O
n = cross(A, B);
n = n/norm(n);
% Find theta for all points in V in radians and degrees
sinTheta = abs(C*n'); % dot product
theta = asin(sinTheta); % theta is an m x 1 vector containing the angular distance (in radians) from each of the m points in V to line AB
% To get surface distance, multiply by radius of sphere
% Get shortest distance and the index of the corresponding point
(dist, ind) = min(theta);
Best Answer