代码之家  ›  专栏  ›  技术社区  ›  endolith

如何在SciPy中沿三维样条曲线查找点?

  •  0
  • endolith  · 技术社区  · 7 年前

    我想画一条这样的曲线:

    3D curve

    一种三维曲线,在空间中有3个点定义曲线通过的端点和中点,但在空间中也有2个点定义曲线弯曲 没有接触。

    类似于在Inkscape中使用二维点定义曲线:

    enter image description here

    我试过阅读 the documentation ,但我很困惑。它要么显示穿过所有点的曲线:

    enter image description here

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  3
  •   xdze2    7 年前

    下面是一个使用 bezier python包以获取 Bezier curve .

    为了获得“沿该曲线沿x维等距分布”的点,使用 numpy.interp 对于每个坐标 x 需要,相应的曲线坐标 t 是插值的。然后,点的坐标 t型 使用获得 curve.evaluate_multi 再一次。

    这个例子是在2D中,但是应该通过定义3D在3D中工作 nodes

    %matplotlib inline
    import matplotlib.pylab as plt
    
    import bezier
    import numpy as np
    
    # Define the Bezier curve
    nodes = np.array([
            [0.0, 0.2, 1.0, 2.0],
            [0.0, 1.8, 0.3, 0.5] ])
    
    curve = bezier.Curve(nodes, degree=3)
    
    t_fine = np.linspace(0, 1, 60) # Curvilinear coordinate
    points_fine = curve.evaluate_multi(t_fine)
    points_fine.shape  # (2, 60)
    
    # Interpolation on regular x coordinates
    x_xregular = np.linspace(0, 2, 7)
    
    t_xregular = np.interp(x_xregular, points_fine[0], t_fine)
    points_xregular = curve.evaluate_multi(t_xregular)
    
    # Plot
    plt.plot(*nodes, '-o', label='definition nodes');
    plt.plot(*points_fine, label='Bezier curve');
    plt.plot(*points_xregular, 'ok', label='regularly spaced along x');
    plt.xlabel('x'); plt.ylabel('y'); plt.legend();
    

    example graph