本文介绍了Python中的Catmull-Rom样条曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python中是否有一个库或函数可以从三个点计算Catmull-Rom样条线?

Is there a library or function in python to compute Catmull-Rom spline from three points ?

最后,我需要的是沿样条线的点的x,y坐标,条件是它们始终沿样条线与给定量t等距(例如,样条线的长度为3个单位,我希望x ,y坐标位于样条线长度0、1、2和3)

What I need in the end are the x,y coordinates of points along the spline, provided that they are always equidistant of a given amount t along the spline (say, the spline curve is 3 units long and I want the x,y coordinates at spline length 0, 1, 2 and 3)

没有什么真正令人兴奋的.我自己写的,但是如果您发现不错的东西,那对测试(或节省时间)将非常有用

Nothing really exciting. I am writing it by myself, but if you find something nice, It would be great for testing (or to save time)

推荐答案

3分? Catmull-Rom被定义为4个点,例如p_1 p0 p1 p2;三次曲线从p0到p1,外点p_1和p2确定p0和p1处的斜率.要通过数组P中的某些点绘制曲线,请执行以下操作:

3 points ? Catmull-Rom is defined for 4 points, say p_1 p0 p1 p2;a cubic curve goes from p0 to p1, and outer points p_1 and p2 determine the slopes at p0 and p1.To draw a curve through some points in an array P, do something like this:

for j in range( 1, len(P)-2 ):  # skip the ends
    for t in range( 10 ):  # t: 0 .1 .2 .. .9
        p = spline_4p( t/10, P[j-1], P[j], P[j+1], P[j+2] )
        # draw p

def spline_4p( t, p_1, p0, p1, p2 ):
    """ Catmull-Rom
        (Ps can be numpy vectors or arrays too: colors, curves ...)
    """
        # wikipedia Catmull-Rom -> Cubic_Hermite_spline
        # 0 -> p0,  1 -> p1,  1/2 -> (- p_1 + 9 p0 + 9 p1 - p2) / 16
    # assert 0 <= t <= 1
    return (
          t*((2-t)*t - 1)   * p_1
        + (t*t*(3*t - 5) + 2) * p0
        + t*((4 - 3*t)*t + 1) * p1
        + (t-1)*t*t         * p2 ) / 2

一个可以通过3个点使用分段二次曲线-请参见道奇森,用于图像重采样的二次插值.你真的想做什么?

One can use piecewise quadratic curves through 3 points --see Dodgson, Quadratic Interpolation for Image Resampling.What do you really want to do ?

这篇关于Python中的Catmull-Rom样条曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:56