C++三次样条轨迹

发布于 2024-12-22 05:32:18 字数 376 浏览 1 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往昔成烟 2024-12-29 05:32:18

样条曲线是关于某些自变量的分段函数(通常是 t,尽管它们似乎在您链接的代码中使用 x)。由于要评估的特定函数取决于最接近输入值t的控制点,因此按t对控制点进行排序是有意义的,以便您可以快速确定需要评估的函数。

然而,即使它们没有排序,您仍然无法使用单个一维样条线创建圆。您的样条函数 y = f(t) 只能为任何给定的 t 提供一个值。如果您要相对于 t 绘制 y 图形,并希望围绕原点绘制一个半径为 1 的圆,则需要将 f(0) 等于1-1 都没有任何意义。

要获得类似圆的形状,您需要一个二维样条线或两个样条线;一个用于 x 值,一个用于 y 值。一旦您有了这两个样条函数 f(t)g(t),那么您只需在同一 t 处计算这两个函数即可将为您提供该 t 样条线的 xy 值。

Splines are piecewise functions with respect to some independent variable (usually t, though they seem to use x in the code you have linked). Since the specific function to be evaluated depends on the control points closest to the input value t, it make sense to sort the control points by t 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 given t. If you are graphing y with respect to t and want a circle with radius 1 about the origin, you would need f(0) to equal both 1 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 the y value. Once you have these two spline functions f(t) and g(t), then you simply evaluate both functions at the same t and that will give you the x and y values of your spline for that t.

坏尐絯℡ 2024-12-29 05:32:18

简单、常见的技巧是使用累积线性弧长作为参数。因此,如果我在曲线中有一组点,简单地表示平面上的 (x,y) 对,其中 x 和 y 是向量,请执行以下操作:

t = cumsum([0;sqrt(diff(x(:)).^2 + diff(y(:)).^2)]);

这为我们提供了沿着每对点之间的分段线性段的累积距离,按照您拥有它们的顺序呈现。将样条曲线拟合为两个单独的样条模型,即 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:

t = cumsum([0;sqrt(diff(x(:)).^2 + diff(y(:)).^2)]);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文