C++三次样条轨迹
我正在编写一个 C++ 程序来生成一组点的三次样条轨迹。这些点不需要沿 x 轴排序。例如,它可能是一个圆圈等。
我在网上找到了一些库,例如ALGLIB库或这里的一个类 https://www.marcusbannerman.co.uk/index.php/home/42-articles/96-cubic-spline-class.html,但所有这些库都会对数据进行排序点。我不需要这个,因为我想要生成的是一个类似圆圈的东西。有办法实现这个目标吗?
I'm writing a C++ program to generate a cubic spline trajectory for a set of points. These points need not be sorted along the x-axis. For example, it may be a circle, etc.
I have found some libraries on the web, for example, the ALGLIB library or a class here https://www.marcusbannerman.co.uk/index.php/home/42-articles/96-cubic-spline-class.html, but all of these libraries sort the data points. I do not need this because what I want to generate is something like a circle. Is there anyway to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
样条曲线是关于某些自变量的分段函数(通常是
t
,尽管它们似乎在您链接的代码中使用x
)。由于要评估的特定函数取决于最接近输入值t
的控制点,因此按t
对控制点进行排序是有意义的,以便您可以快速确定需要评估的函数。然而,即使它们没有排序,您仍然无法使用单个一维样条线创建圆。您的样条函数
y = f(t)
只能为任何给定的t
提供一个值。如果您要相对于t
绘制y
图形,并希望围绕原点绘制一个半径为 1 的圆,则需要将f(0)
等于1
和-1
都没有任何意义。要获得类似圆的形状,您需要一个二维样条线或两个样条线;一个用于
x
值,一个用于y
值。一旦您有了这两个样条函数f(t)
和g(t)
,那么您只需在同一t
处计算这两个函数即可将为您提供该t
样条线的x
和y
值。Splines are piecewise functions with respect to some independent variable (usually
t
, though they seem to usex
in the code you have linked). Since the specific function to be evaluated depends on the control points closest to the input valuet
, it make sense to sort the control points byt
so that you can quickly determine the function that needs to be evaluated.However even if they were not sorted, you still could not create a circle with a single one dimensional spline. Your spline function
y = f(t)
only gives you one value for any givent
. If you are graphingy
with respect tot
and want a circle with radius 1 about the origin, you would needf(0)
to equal both1
and-1
, which doesn't make any sense.To get something like a circle you instead need a two dimensional spline, or two splines; one for the
x
value and one for they
value. Once you have these two spline functionsf(t)
andg(t)
, then you simply evaluate both functions at the samet
and that will give you thex
andy
values of your spline for thatt
.简单、常见的技巧是使用累积线性弧长作为参数。因此,如果我在曲线中有一组点,简单地表示平面上的 (x,y) 对,其中 x 和 y 是向量,请执行以下操作:
这为我们提供了沿着每对点之间的分段线性段的累积距离,按照您拥有它们的顺序呈现。将样条曲线拟合为两个单独的样条模型,即 x(t) 和 y(t)。所以你可以使用 interp1,或者直接使用 spline 或 pchip 函数。请注意,当您构建插值时,pchip 和样条线将具有不同的属性。
最后,如果您确实有一条闭合曲线,因此 x(1) 和 x(end) 应该相同,那么您确实需要使用具有周期性结束条件的样条模型。除了我的 SLM工具,不过理论上并不难做到。
The simple, common trick is to use cumulative linear arclength as the parameter. So, if I have a set of points in a curve as simply (x,y) pairs in the plane where x and y are vectors, do this:
This gives us the cumulative distance along the piecewise linear segments between each pair of points, presented in the order you have them. Fit the spline curve as two separate spline models, thus x(t) and y(t). So you could use interp1, or use the spline or pchip functions directly. Note that pchip and spline will have different properties when you build that interpolant.
Finally, in the event that you really had a closed curve, so that x(1) and x(end) were supposed to be the same, then you would really want to use a spline model with periodic end conditions. I don't know of any implementations for that except in the spline model in my SLM tools, but it is not difficult to do in theory.