简体   繁体   English

获取三维三次样条轨迹Scipy

[英]Get Trajectory of Three Dimensional Cubic Spline Scipy

I am trying to approximate a given route (coordinates) with a three dimensional cubic spline.我正在尝试使用三维三次样条曲线来近似给定路线(坐标)。 Example data:示例数据:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np

%matplotlib inline

x = np.array([1, 2, 2.3, 3, 4, 5, 5.5, 8, 9, 9.5])
y = np.arange(0, 10)
z = np.sin(x) * np.cos(y^2) + x
fig = plt.figure(figsize=(10,6))
ax = axes3d.Axes3D(fig)
ax.stem(x, y, z)

在此处输入图像描述

I now approximate the data points with RBF Interpolator from scipy.我现在使用来自 scipy 的RBF 插值器来近似数据点。 Is this the right approach?这是正确的方法吗?

from scipy.interpolate import RBFInterpolator
coord_data = np.stack([x, y], -1)
spline = RBFInterpolator(coord_data, z, kernel = 'cubic')

How do I get the resulting spline now (the points it is following through)?我现在如何获得生成的样条曲线(它所遵循的点)? And how do I access it's derivatives?我如何访问它的衍生产品?

You are trying to approximate a route, ie, a curve in 3D, and not a surface.您正在尝试逼近一条路线,即 3D 中的曲线,而不是曲面。 The approach you are trying results in a surface and is therefore not suitable for your case.您尝试的方法会产生表面,因此不适合您的情况。

A suitable representation for a 3D curve is in parametric form as a tuple (x(u), y(u), z(u)) where u is some parameter and each coordinate is a function of u . 3D 曲线的合适表示形式为元组(x(u), y(u), z(u))的参数形式,其中u是某个参数,每个坐标是u的 function。 The curve fitting problem is reduced to three 2D problems of fitting (ui, xi) , (ui, yi) , and (ui, zi) separately ( see also my answer here ).曲线拟合问题简化为分别拟合(ui, xi)(ui, yi)(ui, zi)的三个 2D 问题(另请参见我的答案here )。

So, in order to perform a curve fit you are required to provide the parameterization ui for each input point.因此,为了执行曲线拟合,您需要为每个输入点提供参数化ui A common parameterization in spline fitting is the chord-length parameterization .样条拟合中常见的参数化是弦长参数化 This parameterization is defined by the accumulated length of the distances between the ordered points ( u0=0, u1=|p1-p0|, u2 = u1+|p2-p1| ... etc.).该参数化由有序点之间的距离的累积长度定义( u0=0, u1=|p1-p0|, u2 = u1+|p2-p1| ...等)。

The following code implements a spline interpolation of your data with the chord-length parameterization.以下代码使用弦长参数化实现数据的样条插值。

from scipy import interpolate

xyz = np.vstack([x, y, z]).T
u = np.cumsum(np.r_[[0], np.linalg.norm(np.diff(xyz, axis=0), axis=1)])
# u is the chord-legth parameterization for each xyz point

sx = interpolate.InterpolatedUnivariateSpline(u, x)  # x(u) spline
sy = interpolate.InterpolatedUnivariateSpline(u, y)  # y(u) spline
sz = interpolate.InterpolatedUnivariateSpline(u, z)  # z(u) spline

The code below samples the resulting spline and plots the result (in blue) on your data (black polyline).下面的代码对生成的样条曲线进行采样,并在数据(黑色折线)上绘制结果(蓝色)。 The result looks like this:结果如下所示:

在此处输入图像描述

uu = np.linspace(u[0], u[-1], 100)
xx = sx(uu)
yy = sy(uu)
zz = sz(uu)
plt.plot(xx, yy, zz, "b")

You can also use the RBF functions you suggested, for univariate interpolation.您还可以使用建议的RBF函数进行单变量插值。 The following code is an example of how to do this:以下代码是如何执行此操作的示例:

from scipy.interpolate import Rbf

rbfi_x = Rbf(u, x, function='cubic')
rbfi_y = Rbf(u, y, function='cubic')
rbfi_z = Rbf(u, z, function='cubic')

Sampling the resulting functions in a similar way to the spline sample above, and plotting the results on the previous figure, we get the plot below.以与上面的样条样本类似的方式对结果函数进行采样,并将结果绘制在上图中,我们得到下面的 plot。 As can be seen the spline and RBF interpolations are similar but not the same (for example near the endpoints).可以看出,样条和 RBF 插值相似但不相同(例如在端点附近)。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM