样条之CatmullRom

      所谓样条曲线是指给定一组控制点而得到一条曲线,曲线的大致形状由这些点予以控制,一般可分为插值样条和逼近样条两种,插值样条通常用于数字化绘图或动画的设计,逼近样条一般用来构造物体的表面。CatmullRom样条与上一节所讲的B样条很相似,不同在于CatmullRom样条的曲线会经过其每一个控制点。

      The centripetal Catmull–Rom is a subclass of cubic Hermite spline that extends the Catmull–Rom implementation by allowing each of the four control points to be associated with an arbitrary time interval in the computation of a value on the curve. This modifies the behavior of the curve. The curve is an interpolation, and will intersect with all but the first and last control points. If the time intervals are uniform, the result will be the same as that of the original Catmull–Rom spline curve. The chordal curve uses the two dimensional Euclidean distance between control points to provide the time elements, while the centripetal curve uses the square root of the Euclidean distance. The principal reason for using the centripetal version is that it has been shown to be free of cusps and self-intersections.

关于插值与样条的介绍请看:http://www.cnblogs.com/WhyEngine/p/4020294.html

核心代码:

 1 void    YcCatmullRomSpline::BuildWeights()
 2 {
 3     ClearWeights();
 4 
 5     for (Yuint i = 0; i < 4; i++)
 6     {
 7         m_splineWeights[i] = (Yreal*)malloc((m_subD)*sizeof(Yreal));
 8         m_tangentWeights[i] = (Yreal*)malloc((m_subD)*sizeof(Yreal));
 9     }
10 
11     Yreal u, u_2, u_3;
12     for (Yuint i = 0; i < m_subD; i++)
13     {
14         u = (float)i / m_subD;
15         u_2 = u * u;
16         u_3 = u_2 * u;
17 
18         // 参见"游戏编程精粹1"P333
19         m_splineWeights[0][i] = (-1.0f*u_3 + 2.0f*u_2 - 1.0f*u + 0.0f)*0.5f;
20         m_splineWeights[1][i] = ( 3.0f*u_3 - 5.0f*u_2 + 0.0f*u + 2.0f)*0.5f;
21         m_splineWeights[2][i] = (-3.0f*u_3 + 4.0f*u_2 + 1.0f*u + 0.0f)*0.5f;
22         m_splineWeights[3][i] = ( 1.0f*u_3 - 1.0f*u_2 + 0.0f*u + 0.0f)*0.5f;
23 
24         m_tangentWeights[0][i] = (-3.0f*u_2 +  4.0f*u - 1.0f)*0.5f;
25         m_tangentWeights[1][i] = ( 9.0f*u_2 - 10.0f*u + 0.0f)*0.5f;
26         m_tangentWeights[2][i] = (-9.0f*u_2 +  8.0f*u + 1.0f)*0.5f;
27         m_tangentWeights[3][i] = ( 3.0f*u_2 -  2.0f*u + 0.0f)*0.5f;
28     }
29 }

切图:

相关软件的下载地址为:http://files.cnblogs.com/WhyEngine/TestSpline.zip

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值