How to Use Interp function to get cartesian coordinates from arc lenth?

Hello,
For my simulations, I read in an array which has x and y coordinates and then I convert it to the streamwise coordinate S ( or arc length) which is basically the distance between adjacent points.
The sample code is below which gets me S. What I would like to do now, is how can I get back x and y from S? I believe I can use the interp1 function but I am not sure what information I need to provide to the function. Any advice or guidance would be greatly appreciated.
%-----------------------------------------------------
x=data(:,1)%get x data
y=data(:,2)%get y data
%now lets get the difference in distance between adjacent points
dx=diff(x)
dy=diff(y)
%now if we have an arc with a delta x and delta y, the length of the arc is
%simply pythagorean theorem
ds=sqrt(dx.^2+dy.^2)
%the length of S will be one less than the lenth of x so we add zero at the
%beginning
s=[0.0;cumsum(ds)]

9 个评论

This is not exactly what you're asking, but perhaps these functions might be helpful?
Hi dream chaser
you cannot recover the original coordinates with just s or ds.
You need additional information
  • at least one point
  • the phases
without that, you can carry on attempting interpolations, or interpacs, but it's just guesswork, and no way to validate it.
Even if you get to know let's say the last point, the only data you have is the radius of the circle where the previous points lays.
Without additional data, you have to place another circle on each point of the 1st circle, the 2nd circle with the 2nd radius.
Even if you manage to build a tree of circles, if there are cycling patterns of points like
[x1 x2 x1 x2 x3 x4 x3 x4 .. ;
y1 y2 y1 y2 y3 y4 y3 y4 ..]
if the guessing folds back on previous points, you are trapped in an endless loop, requiring further guessing.
No way, not even with interparc
@John BG. Yes, you can predict the original values.
Without the phases, and at least one point, there's no way to retrieve the data. You may, but it is going to be a lucky strike.
Guessing a 2D points distribution by just knowing the distance among them, without a single distance to a reference point, is just guess work.
It is not only the distances that are known. The vector s is now used to compute a pair of splines. dream chaser is well along the way to learning how to do exactly that, since they got the important first part right.
s is only the cumulative sum of ds.
ds only contains the relative distances AMONG the points.
How can any one get a reference to, for instance, an axis origin, the axis origin precisely used to define the points to find, without at least 1 reliable additional measure?
The sequential correct guessing along a list of scattered 2D points requires 2 new data, at each hop. With only 1 new data, the uncertainty of a whole circle remains.
John, yes, if you completely discard x and y, after having computed s, then it is meaningless to try to predict anything. For example, if I tell you only the interpoint distances, then they might lie along a circle, or they might lie purely on the x axis, with y always zero.
As the question was asked, it was showing a CLASSIC way to begin the computation of a parametric spline. Then they asked how to use interpolation to predict x and y. That implies they are looking to learn how to do interpolation. You are focusing too closely on their question as to how to use s.
ok,
let be just 3 points:
p1=[1 1]
p2=[2 2]
p3=[3 3]
Dream Chaser only gives you ds
[1.41 1.41]
or s
[0 1.41 2.42]
how on heaven can any one retrieve, not any of p1 p2 p3, but just a single point of the hidden set if not playing mine craft along circles?
But where do you start? there is no common reference!
Dream Chaser would like to know, and I would like to know as well. how can any interpolation, or interpac, or any other other function, solely reading s or ds, come anywhere close to a reasonable guess?
I will mark your answer as accepted, in case Dream Chaser doesn't, if you, Mr John D'Errico, write MATLAB code solving these 3 points, a simplification of Dream Chaser question.
Appreciating time and attention
John BG
John, again, you are focusing on the wrong thing. Focus is good. But here to listen and learn is better.

请先登录,再进行评论。

回答(2 个)

Walter is correct, in that interparc is a viable choice, because it does all the work for you in terms of computing the splines, and then predicting points along the curve. You don't need it though.
It seems you are looking to understand how one can interpolate a parametric function. It looks like you understand the computation of a chordal arclength perfectly. The only trick that remains is to then fit both x(s) and y(s) as splines.
xs = spline(s,x);
ys = spline(s,y);
You can now recover any point along the curve by use of those two splines. For example, the original points are predicted using two calls to ppval (or fnval if you prefer):
xpred = ppval(xs,s);
ypred = ppval(ys,s);
This will predict the original points (to within possible floating point slop, so possible errors on the order of eps.)
Alternatively, you can use interp1.
xpred = interp1(s,x,s);
ypred = interp1(s,y,s);
Any of the interp1 methods will suffice here. Intermediate points along the curve can be computed, by providing intermediate values in terms of arc length.
A nice thing is this general approach allows you to use splines to predict a function along a path in any number of dimensions. In fact, the approach using splines as I show can indeed predict points that wrap around in a circle very nicely. Or, it can handle a curve that crosses itself in a figure 8. Don't forget that splines can be somewhat dangerous though. They do strange things if you push them too far. Sometimes pchip can be a better choice than spline. Knowing when that might be true is important.
Note that you still cannot predict a value y for some given intermediate value of x. At least this will be more difficult, although not always impossible.

6 个评论

I miss the point where splines entered the discussion. "Length of the arc is simply pythagorean theorem" sounds, like the OP works with polygons.
Hi Everyone,
John describes closely what I am trying to do. The only thing is that after converting to the stream wise coordinate S, I then give this information to a program, which gives me back a solution at S. It gives many more values for S so that when I try with the spline (what John recommended) it says that the number of sites is incompatible with the number of values.
A way I'm thing to work around this is lets say we have 29000 data points for S but only 2300 data points for x and y. How can I generate additional data points between x so that I can use a spline? I"m thinking I can do a fit to the original x and y data. If I have the same amount of x and y coordinates as the S output, then I can just use spline.
Thanks, dreamchaser
Jan - this is how parametric splines are built. For example, suppose you have a set of points that lie around a circle? You can't just use a simple spline or interp1. In some cases, it is no problem to build a spline for y as a function of x, thus y(x). But for many parametric curves, that is not possible.
So the trick is to create a new parameter, I'll call it t. Here t is the cumulative arc length from point to point of the piecewise linear curve, so the connect-the-dots arc length.
theta = [0,sort(rand(1,20))*2*pi,2*pi];
x = cos(theta);y = sin(theta);
plot(x,y,'o-')
Ok, now suppose I have just been given x and y as vectors? So now I have no access to the theta vector, but I want to interpolate those points, and so so nice and smoothly using a spline? No problem.
The trick is the cumulative chordal arc length.
t = cumsum([0,sqrt(diff(x).^2 + diff(y).^2)]);
t = t./t(end);
I like to scale it to the interval [0,1].
Now, you build TWO splines, thus of the form x(t) & y(t). You can predict any point on the curve as a function of arc length.
tint = linspace(0,1,1000);
xs = spline(t,x);
ys = spline(t,y);
xint = ppval(xs,tint);
yint = ppval(ys,tint);
plot(x,y,'o',xint,yint,'b-')
That actually looks pretty good, especially given the size of some of the holes in the curve.
See that I never used any knowledge about theta there. I built two parametric splines, as a function of linear arc length between the points.
The whole point is, as soon as I saw the arc length being computed, I recognized what was being done. This really is a classic scheme for interpolation along a path.
We can use this scheme for curves that do all sorts of strange and even weird things.
theta = linspace(0,2*pi,50);
x = cos(3*theta);y = sin(2*theta);
t = cumsum([0,sqrt(diff(x).^2 + diff(y).^2)]);
t = t./t(end);
tint = linspace(0,1,1000);
xs = spline(t,x);
ys = spline(t,y);
xint = ppval(xs,tint);
yint = ppval(ys,tint);
plot(x,y,'o',xint,yint,'b-')
Or, you can build paths through points in a higher number of dimensions. As long as you can compute that point-to-point cumulative arc length, there is no problem.
Sahadeo, I'm not sure what your problem is. As you see, I interpolated the curves at 1000 points. The trick is to build the splines from your arc lengths and the x and y data, exactly as I did. So show what you tried. Or go back and look through the example I showed here.
John thanks so much for the help! This answered my question and I solved my problem. I'm not sure how to mark your response as accepted? thanks again for your help!
That is always nice to hear. Isn't there an accept this answer button? Hmm. Oh well, I'm not that worried. :) I'm happy that you found it useful, and that is what really matters to me. The goal of Answers is not to get points by answering questions, but to answer questions that need to be answered. So have a nice day.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Spline Postprocessing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by