我曾经发过两篇关于贝塞尔的文章:数学图形(1.47)贝塞尔(Bézier)曲线,数学图形之贝塞尔(Bézier)曲面。那是使用我自己定义的脚本语言生成贝塞尔图形。由于我自己定义的脚本语法功能有限,所以最多只能支持5次贝塞尔函数,而这里将实现N次。

N阶贝塞尔曲线可如下推断:

给定点P0P1、…、Pn,其贝塞尔曲线即

看其公式需要先为之生成一套杨辉三角形数组。

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

.h文件

 1 /****************************************************************
 2
 3   File name   :  YcBezierSpline.h
 4   Author      :  叶峰
 5   Version     :  2.0
 6   Create Date :  2014/08/18
 7   Description :  Bezier样条
 8
 9 *****************************************************************/
10
11 #ifndef __YcBezierSpline_H__
12 #define __YcBezierSpline_H__
13
14 // INCLUDES -----------------------------------------------------------------------------
15
16 #include "YicSpline.h"
17
18 // --------------------------------------------------------------------------------------
19
20 #define YD_MAX_BEZIER_CONTROL_VALUE 33
21
22 // --------------------------------------------------------------------------------------
23
24 class YcBezierSpline : public YicSpline
25 {
26 public:
27     YcBezierSpline();
28
29     ~YcBezierSpline();
30
31     // 设置输出样条值的数目
32     void    SetSplineValuesCount(Yuint count);
33
34     // 获得输出样条值的数目
35     Yuint   GetSplineValuesCount() const;
36
37     // 计算样条数值
38     bool    BuildSpline(const void* ctrlValuesPtr, Yuint ctrlStride, Yuint ctrlCount,
39         void* splineValuesPtr, Yuint splineStride) const;
40
41 protected:
42     void    ClearPowT();
43
44     void    BuildPowT();
45
46     Yreal    GetValueT(Yint t, Yint p) const
47     {
48         return m_pow_t[YD_MAX_BEZIER_CONTROL_VALUE*t + p];
49     }
50
51 protected:
52     Yuint   m_valuesCount;
53     Yreal*  m_pow_t;
54
55 protected:
56     static void    BuildYanghuiTriangle();
57     static Yint m_yanghuiRowIndex[YD_MAX_BEZIER_CONTROL_VALUE];
58     static Yint m_yanghuiTriangle[(YD_MAX_BEZIER_CONTROL_VALUE+1)*YD_MAX_BEZIER_CONTROL_VALUE/2];
59 };
60
61 // --------------------------------------------------------------------------------------
62
63 #endif

CPP文件

  1 /****************************************************************
  2
  3   File name   :  YcBezierSpline.cpp
  4   Author      :  叶峰
  5   Version     :  2.0
  6   Create Date :  2014/08/18
  7   Description :
  8
  9 *****************************************************************/
 10
 11 // INCLUDES -----------------------------------------------------------------------------
 12
 13 #include "..\..\YCommon_h\YSpline\YcBezierSpline.h"
 14 #include <assert.h>
 15
 16 // --------------------------------------------------------------------------------------
 17
 18 Yint    YcBezierSpline::m_yanghuiRowIndex[YD_MAX_BEZIER_CONTROL_VALUE] = {0};
 19 Yint    YcBezierSpline::m_yanghuiTriangle[(YD_MAX_BEZIER_CONTROL_VALUE+1)*YD_MAX_BEZIER_CONTROL_VALUE/2] = {0};
 20
 21 void    YcBezierSpline::BuildYanghuiTriangle()
 22 {
 23     // 第0行
 24     m_yanghuiRowIndex[0] = 0;
 25     m_yanghuiTriangle[0] = 1;
 26
 27     Yint index = 1;
 28     Yint t0,t1;
 29     Yint* lastRow;
 30     for (Yint i = 1; i < YD_MAX_BEZIER_CONTROL_VALUE; i++)
 31     {
 32         m_yanghuiRowIndex[i] = index;
 33         m_yanghuiTriangle[index] = 1;
 34         index++;
 35
 36         for (Yint j = 1; j <= i; j++)
 37         {
 38             lastRow = m_yanghuiTriangle + m_yanghuiRowIndex[i-1];
 39             t0 = lastRow[j - 1];
 40             t1 = (j < i) ? lastRow[j] : 0;
 41
 42             m_yanghuiTriangle[index] = t0 + t1;
 43             index++;
 44         }
 45     }
 46
 47     assert(index == (YD_MAX_BEZIER_CONTROL_VALUE+1)*YD_MAX_BEZIER_CONTROL_VALUE/2);
 48 }
 49
 50 // --------------------------------------------------------------------------------------
 51
 52 YcBezierSpline::YcBezierSpline()
 53 {
 54     if (m_yanghuiTriangle[0] == 0)
 55     {
 56         BuildYanghuiTriangle();
 57     }
 58
 59     m_valuesCount = 0;
 60     m_pow_t = NULL;
 61
 62     SetSplineValuesCount(100);
 63 }
 64
 65 YcBezierSpline::~YcBezierSpline()
 66 {
 67     ClearPowT();
 68 }
 69
 70 // 设置输出样条值的数目
 71 void   YcBezierSpline::SetSplineValuesCount(Yuint count)
 72 {
 73     if (count < 2)
 74     {
 75         count = 2;
 76     }
 77
 78     if (count == m_valuesCount)
 79     {
 80         return;
 81     }
 82     m_valuesCount = count;
 83     BuildPowT();
 84 }
 85
 86 // 获得输出样条值的数目
 87 Yuint   YcBezierSpline::GetSplineValuesCount() const
 88 {
 89     return m_valuesCount;
 90 }
 91
 92 void    YcBezierSpline::ClearPowT()
 93 {
 94     if (m_pow_t)
 95     {
 96         free(m_pow_t);
 97         m_pow_t = NULL;
 98     }
 99 }
100
101 void    YcBezierSpline::BuildPowT()
102 {
103     ClearPowT();
104
105     m_pow_t = (Yreal*)malloc(m_valuesCount*YD_MAX_BEZIER_CONTROL_VALUE*sizeof(Yreal));
106     Yreal t;
107     for (Yuint i = 0; i < m_valuesCount; i++)
108     {
109         t = i/(m_valuesCount - 1.0f);
110
111         m_pow_t[i*YD_MAX_BEZIER_CONTROL_VALUE] = 1.0f;
112         for (Yint j = 1; j < YD_MAX_BEZIER_CONTROL_VALUE; j++)
113         {
114             m_pow_t[i*YD_MAX_BEZIER_CONTROL_VALUE + j] = m_pow_t[i*YD_MAX_BEZIER_CONTROL_VALUE + j - 1]*t;
115         }
116     }
117 }
118
119 // 计算样条数值
120 bool    YcBezierSpline::BuildSpline(const void* ctrlValuesPtr, Yuint ctrlStride, Yuint ctrlCount,
121     void* splineValuesPtr, Yuint splineStride) const
122 {
123     if (ctrlCount < 2 || ctrlCount > YD_MAX_BEZIER_CONTROL_VALUE)
124     {
125         return false;
126     }
127
128     Yreal* destValue;
129     Yreal* srcValue;
130     Yreal v;
131     const Yint* yanghuiRow = m_yanghuiTriangle + m_yanghuiRowIndex[ctrlCount - 1];
132
133     for (Yuint i = 0; i < m_valuesCount; i++)
134     {
135         v = 0.0f;
136         for (Yuint j = 0; j < ctrlCount; j++)
137         {
138             srcValue = (Yreal*)((char*)ctrlValuesPtr + ctrlStride*j);
139             v += yanghuiRow[j] * (*srcValue) * GetValueT(i, j) * GetValueT(m_valuesCount - 1 - i, ctrlCount - 1 - j);
140         }
141
142         destValue = (Yreal*)((char*)splineValuesPtr + splineStride*i);
143         *destValue = v;
144     }
145
146     return true;
147 }
148
149 // --------------------------------------------------------------------------------------

图像:

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

转载于:https://www.cnblogs.com/WhyEngine/p/4020365.html

样条之贝塞尔(Bezier)相关推荐

  1. Unity 工具类 之 贝塞尔 Bezier 曲线

    Unity 工具类 之 贝塞尔 Bezier 曲线 目录 Unity 工具类 之 贝塞尔 Bezier 曲线 一.简单介绍 二.原理与分类 三.公式与原理图演示 五.注意事项 六.样例使用步骤(三次贝 ...

  2. 贝塞尔Bezier曲线的使用

    1 简介   贝塞尔曲线就是这样的一条曲线,它是依据N个位置任意的点坐标绘制出的一条光滑曲线.那么,我们可以直观地认为,为了得到一条贝塞尔曲线,我们只要输入起点.终点及控制点既可.变化参数t都是位于[ ...

  3. [摘抄] Bezier曲线、B样条和NURBS

    Bezier曲线.B样条和NURBS,NURBS是Non-Uniform Rational B-Splines的缩写,都是根据控制点来生成曲线的,那么他们有什么区别了?简单来说,就是: Bezier曲 ...

  4. Bezier曲线、B样条和NURBS的基本概念

    最不能理解的一点,一讨论软件的曲面,曲线功能,最后就变成曲线.曲面的数学原理的讨论了,但是里面也没数学好的,讨论的结果可想而知. 我不是数学家,我不懂这么复杂的方程,只要好用就行了. 在CAD中,设计 ...

  5. 贝塞尔曲线与B样条曲线

    文章目录 0.参考 1.问题起源与插值法的曲线拟合 1.1.问题起源 1.2.拉格朗日插值 1.3."基"的概念 1.4.插值存在的Runge现象 2.贝塞尔曲线 2.1.控制点的 ...

  6. [MATLAB]b样条方程基函数方程的表达式, 及n阶基函数作图

    这篇文章是作者我基于山东大学杨维强老师在B站的这个视频创作的: 贝塞尔(Bezier)曲线与B样条_哔哩哔哩_bilibili 杨老师在视频中用的ppt的获取链接(下简称ppt): 链接:https: ...

  7. 开源项目推荐:Bezier曲线、B-Spline和NURBS的区别与《THE NURBS BOOK 2nd》简介,曲线拟合可视化工具

    一.基本概念 B-Spline:B样条曲线 NURBS(Non Uniform Rational B-Spline):非均匀有理B样条曲线 B样条曲线有三种类型: 当起始点和终止点的重复度为最高次数加 ...

  8. 3阶贝塞尔曲线沿线长等距分割方法

    3阶贝塞尔曲线等距分割 1.引言 2.数学计算 3.应用 4.demo下载 1.引言 贝塞尔(bezier)曲线又称样条曲线,常用的有2阶跟3阶形式,3阶曲线最为常用,其公式(1)为: P0/P1/P ...

  9. 图形学笔记(九)几何 ——几何表示方法(CSG、距离函数、水平集 、点云、网格(obj格式))、贝塞尔曲线(面)

    图形学笔记(八)着色2 -- 纹理映射.重心坐标.双线性插值.Mipmap.三线性插值.各向异性过滤.纹理的应用(环境贴图.法线贴图等) 图形学笔记(十)几何2 -- 曲面细分(Loop细分.Catm ...

最新文章

  1. Python编写循环的两个建议 | 鹅厂实战
  2. Redis BitMap适应场景
  3. 计算机导航 骨科 ppt模板,(医学PPT课件)术中即时三维导航在脊柱侧弯矫形的应用...
  4. 分布与并行计算—生命游戏(Java)
  5. ftp 服务器 文件 连接 导出,ftp 服务器 文件 连接 导出
  6. 7 记账期 012 2021 没有打开
  7. Python稳基修炼之计算机等级考试易错概念题6(含答案)
  8. oracle怎么装系统,【Oracle安装与操作系统用户组】
  9. Java基础:什么是返回对象
  10. 2021-06-06滚动条de面板
  11. Cacti监控Varnish
  12. AD09铺铜 (画完PCB后改线与铺铜冲突)
  13. dnf外挂java代码,使用Java实现简朴的斗地主案例_rust辅助,绝地求生卡盟
  14. 虚拟化工具介绍 (资源)
  15. db2 正则匹配_SQL正则表达式
  16. vue使用富文本编辑器vue-quill-editor
  17. 山东省第八届acm大赛 G题 (SDUT 3899)
  18. 发动机启动计算机,不自检直接启动伤车?看完发动机电脑自检原理你就明白了...
  19. Windows环境下在局域网内建立Git远程仓库
  20. linux自动清理磁盘日志的一种方案

热门文章

  1. Thymeleaf select 使用 和多select 级联选择
  2. [pytorch、学习] - 4.6 GPU计算
  3. javascript --- JSON字符串化
  4. setInterval只执行一次的原因
  5. npm run dev 在本地调试出现跨域问题解决方法
  6. Android得到一个闹钟在第三方
  7. [summary] 单调队列
  8. 大学生成绩管理系统(C语言)
  9. 看看你的网站有几个这样的链接?
  10. 对VS2008生成智能win32程序简单理解