Catmull_Rom算法的实现

Catmull_Rom算法的具体原理在网络上都有,感兴趣的可以自己查找,此处只是它的代码实现,实现环境为OSG:

给定一堆离散点,将该离散点进行插值,绘制的曲线经过每一个离散点。


实现的代码:

//生成Catmull_Rom Spline算法顶点
osg::Vec3 Catmull_Rom_SplinePoint_new(float u, osg::ref_ptr<osg::Vec3Array> subconctrl_P)
{
    float f0, f1, f2, f3;
    osg::Vec3 vert;
    f0 = (-0.5)*pow(u, 3) + pow(u, 2) + (-0.5*u);
    f1 = 1.5*pow(u, 3) - 2.5*pow(u, 2) + 1;
    f2 = (-1.5)*pow(u, 3) + 2.0*pow(u, 2) + 0.5*u;
    f3 = 0.5*pow(u, 3) - 0.5*pow(u, 2);
    vert = subconctrl_P->at(0)*f0 + subconctrl_P->at(1)*f1 + subconctrl_P->at(2)*f2 + subconctrl_P->at(3)*f3;
    return vert;
}

//生成Catmull_Rom Spline样条线顶点,过端点
osg::ref_ptr<osg::Vec3Array> Catmull_Rom_SplinePoints_new(osg::ref_ptr<osg::Vec3Array> conctrl_P, int Unum = 10)
{
    osg::ref_ptr<osg::Vec3Array> C_Line_vertices = new osg::Vec3Array;
    osg::ref_ptr<osg::Vec3Array> New_conctrl_P = new osg::Vec3Array;
    auto pointsnum = conctrl_P->size();
    auto P0 = conctrl_P->at(0) * 2 - conctrl_P->at(1);
    New_conctrl_P->push_back(P0);
    auto Pn = conctrl_P->at(pointsnum - 1) * 2 - conctrl_P->at(pointsnum - 2);
    for (int i = 0; i < pointsnum; i++)
    {
        New_conctrl_P->push_back(conctrl_P->at(i));
    }
    New_conctrl_P->push_back(Pn);
    pointsnum += 2;
    for (int i = 0; i < pointsnum; i++)
    {
        if (i < pointsnum - 3)
        {
            osg::ref_ptr<osg::Vec3Array> subcontrl = new osg::Vec3Array;
            subcontrl->push_back(New_conctrl_P->at(i));
            subcontrl->push_back(New_conctrl_P->at(i + 1));
            subcontrl->push_back(New_conctrl_P->at(i + 2));
            subcontrl->push_back(New_conctrl_P->at(i + 3));
            float deltaU = 1.0 / float(Unum);
            for (int j = 0; j < Unum; j++)
            {
                auto Uu = j*deltaU;
                auto tempp = Catmull_Rom_SplinePoint_new(Uu, subcontrl);
                C_Line_vertices->push_back(tempp);
            }
        }

    }
    return C_Line_vertices;
}


实现效果:

蓝色是生成的曲线,红色的线作为对照,是用于插值的离散点


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值