B-Spline样条曲线

生成样条曲线

splrep(spline representation)

splprep

  • 代码示例
    import matplotlib.pyplot as plt
    from scipy.interpolate import splev, splrep, splprep
    import numpy as np
     
    points = [[0,1], [1, 1.1],[2, 1.2],[3,3],[4,2.9],[5,2.8],[6,2.7]]
     
    x = [point[0] for point in points]
    y = [point[1] for point in points]
    tck, u = splprep([x, y], k=3,s=0)
    t, c, k = tck
      
    spline = splev(np.linspace(0,1,100),tck)
     
    plt.plot(x, y, 'o', color='#ff7f0e', markersize=12,label='raw data')
    plt.plot(spline[0], spline[1], color='#d62728', linewidth=3, label='splprep approximation (splev)')
    plt.plot(c[0], c[1], 's', color='#1f77b4', markersize=12, label='c of tck')
    plt.plot(c[0], c[1], '--', color='#2ca02c', linewidth=3)
    plt.legend(loc = 'lower right')
    plt.show()
  • splprep返回两个参数tcku,其中tck又包含三个参数tc,和k
    • t:(knots)节点,长度为11(提供了7个参考点,len(t)=len(c)+k+1)
    • c:(coefficients)系数,控制点?不在拟合的曲线上, 长度为7
    • k:(curve degree )阶数在使用splprep时已将其赋值为3(cubic)
    • u:An array of the values of the parameter.

空间变换Spatial transform

旋转插值Slerp

  • 给定起始和终止转角进行插值,代码:
    • key_rots可以从欧拉角、四元数或旋转矩阵等声明而来,N个旋转角则为N个元素组成的列表,每个元素为相应的欧拉角、四元数、旋转矩阵或其它相应格式
from scipy.spatial.transform import Rotation as R
from scipy.spatial.transform import Slerp
import numpy as np
 
n = 10
 
key_rots = R.from_quat([[1, 0, 0, 0], [0, 1, 0, 0]])
key_times = [0, n-1]
slerp = Slerp(key_times, key_rots)
 
times = list(range(n))
interp_rots = slerp(times)
print(interp_rots.as_quat())