简体   繁体   English

如何在SciPy中沿着3D样条曲线找到点?

[英]How to find points along a 3D spline curve in SciPy?

I want to make a curve like this: 我想画一条曲线:

3D曲线

A 3D curve, with 3 points in space defining the end and middle points, which the curve passes through, but also 2 points in space that the curve bends towards without touching. 3D曲线,在空间中有3个点定义了曲线的终点和中间点,在空间中还有2个点,曲线向该点弯曲没有接触。

Similar to defining curves using points in 2D in Inkscape: 类似于在Inkscape中使用2D点定义曲线:

在此处输入图片说明

Additionally, I want to calculate the points along this curve equally spaced along the x dimension of the space. 另外,我想计算沿着这条曲线沿点的x维度等距分布的点。 (Not equally spaced along the t variable that defines the spline, and not equally spaced along the curve length. The curve will not backtrack along the x dimension.) (沿定义样条曲线的t变量不等距,并且沿曲线长度不等距。曲线不会沿x维度回退。)

I've tried reading the documentation , but I'm confused. 我尝试阅读文档 ,但感到困惑。 It either shows the curve going through all the points: 它要么显示通过所有点的曲线:

在此处输入图片说明

or none of them: 或没有一个:

在此处输入图片说明

Here is a code using the bezier python package to obtain points along a Bezier curve . 这是一个使用bezier python包获取沿Bezier曲线的点的代码。

To obtain points "along this curve equally spaced along the x dimension" a linear interpolation is performed using numpy.interp . 为了获得“沿x方向等间距分布的曲线”点,使用numpy.interp进行线性插值。 For each coordinate x wanted, the corresponding curvilinear coordinate t is interpolated. 对于每个所需的坐标x ,都将插入相应的曲线坐标t Then, the coordinates of the point at t is obtain using curve.evaluate_multi again. 然后,再次使用curve.evaluate_multi获得t点的坐标。

The example is in 2D but should be working in 3D by defining 3D nodes coordinates. 该示例处于2D模式,但应通过定义3D nodes坐标在3D模式下工作。

%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();

示例图

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

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