├── LICENSE ├── dist ├── BezierCurve.ghuser ├── DeBoorAlgorithmNurbs.gh └── DeBoorAlgorithmNurbs.ghuser ├── images ├── 01.png ├── 02.png ├── 05.png ├── 06.png ├── 07.png ├── 08.gif ├── 09.gif ├── 10.png ├── bezier.png ├── bspline.png ├── deboor-nurbs.gif ├── deboor-spline.gif ├── diagram.png ├── fun.png ├── line.gif ├── nurbs-diagram.png ├── nurbs.ai ├── nurbsCircle.png ├── spline2.gif ├── spline3.gif ├── spline4.gif └── spline5.gif ├── readme-cn.md ├── readme.md └── src ├── BezierCurve.py ├── DeBoorAlgorithmNurbs.gh ├── DeBoorAlgorithmNurbs.py ├── TextGoo.py ├── lineGoo.py ├── pointGoo.py └── resource ├── diagram.ai ├── icon-small-bc.ai ├── icon-small-bc.png ├── icon-small-db.ai ├── icon-small-db.png ├── icon.ai └── icon.png /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /dist/BezierCurve.ghuser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/dist/BezierCurve.ghuser -------------------------------------------------------------------------------- /dist/DeBoorAlgorithmNurbs.gh: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/dist/DeBoorAlgorithmNurbs.gh -------------------------------------------------------------------------------- /dist/DeBoorAlgorithmNurbs.ghuser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/dist/DeBoorAlgorithmNurbs.ghuser -------------------------------------------------------------------------------- /images/01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/01.png -------------------------------------------------------------------------------- /images/02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/02.png -------------------------------------------------------------------------------- /images/05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/05.png -------------------------------------------------------------------------------- /images/06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/06.png -------------------------------------------------------------------------------- /images/07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/07.png -------------------------------------------------------------------------------- /images/08.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/08.gif -------------------------------------------------------------------------------- /images/09.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/09.gif -------------------------------------------------------------------------------- /images/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/10.png -------------------------------------------------------------------------------- /images/bezier.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/bezier.png -------------------------------------------------------------------------------- /images/bspline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/bspline.png -------------------------------------------------------------------------------- /images/deboor-nurbs.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/deboor-nurbs.gif -------------------------------------------------------------------------------- /images/deboor-spline.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/deboor-spline.gif -------------------------------------------------------------------------------- /images/diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/diagram.png -------------------------------------------------------------------------------- /images/fun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/fun.png -------------------------------------------------------------------------------- /images/line.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/line.gif -------------------------------------------------------------------------------- /images/nurbs-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/nurbs-diagram.png -------------------------------------------------------------------------------- /images/nurbs.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/nurbs.ai -------------------------------------------------------------------------------- /images/nurbsCircle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/nurbsCircle.png -------------------------------------------------------------------------------- /images/spline2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/spline2.gif -------------------------------------------------------------------------------- /images/spline3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/spline3.gif -------------------------------------------------------------------------------- /images/spline4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/spline4.gif -------------------------------------------------------------------------------- /images/spline5.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/images/spline5.gif -------------------------------------------------------------------------------- /readme-cn.md: -------------------------------------------------------------------------------- 1 | # NURBS曲线DeBoor算法(Grasshopper数学插件) 2 | 3 | * 版本 0.0.2 alpha 4 | * 版权 (c) 2019-2020 mahaidong 5 | * Github源码下载: [https://github.com/caadxyz/DeBoorAlgorithmNurbs](https://github.com/caadxyz/DeBoorAlgorithmNurbs) 6 | * Supported by ikuku.cn & caad.xyz 7 | 8 | [English](readme.md) 9 | 10 | # 这个插件能做什么? 11 | 12 | ### 演示Nurbs曲线的DeBoor算法 13 | 14 | 仅仅靠自己的抽象想象力去理解不同的控制点位置(point)、权重向量(weight)和结点向量(knot)对NURBS曲线的影响是很难获得直观的理解与认识的. 15 | 16 | 这个grasshopper组件可以让你通过设定曲线的度数、控制点、重量向量和结点向量来演示如何生成NURBS曲线。其结果是让用户可以直观的看到最一般意义上的NURBS曲线的生成. 17 | 18 | 同时在曲线的绘制的时, 直接展示了与DeBoor算法相关的辅助线,及辅助线如何生成曲线. 19 | 20 | DeBoor算法参考文献: https://pages.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/de-Boor.html 21 | 22 | **什么是DeBoor算法?** 23 | 24 | * DeBoor算法是DeCasteljau算法的B-spline版本 25 | * 通过DeBoor算法可以精准的描绘B-spline 26 | * 通过控制点及结点向量knot的值u,通过递归求得曲线上的点的位置 27 | * DeBoor算法也可以用于Nurbs曲线的生成 28 | 29 | ![fun](images/fun.png) 30 | 31 | BSpline的DeBoor算法伪代码, python源码见[github](https://github.com/caadxyz/DeBoorAlgorithmNurbs) 32 | 33 | ``` 34 | Input: a value u 35 | Output: the point on the curve, p(u) 36 | 37 | If u lies in [uk,uk+1) and u != uk, let h = p (i.e., inserting u p times) and s = 0; 38 | If u = uk and uk is a knot of multiplicity s, let h = p - s (i.e., inserting u p - s time); 39 | Copy the affected control points pk-s, pk-s-1, pk-s-2, ..., pk-p+1 and pk-p to 40 | a new array and rename them as pk-s,0, pk-s-1,0, pk-s-2,0, ..., pk-p+1,0; 41 | 42 | for r := 1 to h do 43 | for i := k-p+r to k-s do 44 | begin 45 | Let ai,r = (u - ui) / ( ui+p-r+1 - ui ) 46 | Let pi,r = (1 - ai,r) pi-1,r-1 + ai,r pi,r-1 47 | end 48 | pk-s,p-s is the point p(u). 49 | ``` 50 | 51 | **5阶BSpline的演示** 52 | 53 | ![spline](images/deboor-spline.gif) 54 | 55 | 10个控制点 56 | knots= [0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 5] 57 | weight= [1, 1, 1, 1, 1, 1, 1, 1, 1,1] 58 | 59 | 60 | 61 | **NURBS曲线的DeBoor算法** 62 | De Boor的算法也适用于NURBS曲线。我们只需将每个控制点乘以它的权重,将NURBS曲线转换为4D B-spline曲线,在这条4D B-spline曲线上执行de Boor算法,然后将得到的曲线上的点的xyz除以w,就可以将曲线投影回来. python源码见[github](https://github.com/caadxyz/DeBoorAlgorithmNurbs) 63 | 64 | **圆的Nurbs拟合演示** 65 | 66 | ![circle](images/deboor-nurbs.gif) 67 | 68 | 9个控制点 69 | knots= [0, 0, 1, 1, 2, 2, 3, 3, 4, 4] 70 | weight=[1, 0.707107, 1, 0.707107, 1, 0.707107, 1, 0.707107, 1] 71 | 72 | 73 | ### 演示Bezier曲线与Bernstein多项式 74 | 75 | **参数曲线的范畴** 76 | 77 | ![nurbs](images/nurbs-diagram.png) 78 | 79 | **什么是Bezier曲线** 80 | 81 | Bezier曲线的数学基础--伯恩斯坦多项式--早在1912年就已为人所知,但直到大约50年后,法国工程师皮埃尔-贝兹尔(Pierre Bézier)才将这些多项式应用于图形,并将其广泛宣传,他用它们在雷诺公司设计汽车车身。这些曲线的研究最早是在1959年由数学家Paul de Casteljau利用de Casteljau的算法发展起来的,该算法是一种数值稳定的方法,用于评估法国另一家汽车制造商Citroën的Bézier曲线 82 | 83 | 关于Bernstein多项式的介绍见: [http://www.ikuku.cn/post/1872817 ](http://www.ikuku.cn/post/1872817) 84 | 85 | **Bezier曲线的数学公式** 86 | 87 | Bernstein多项式 88 | ![01](images/01.png) 89 | 90 | 其中: 91 | 92 | ![02](images/02.png) 93 | 94 | 95 | n阶贝塞尔曲线的定义 96 | 97 | ![05](images/05.png) 98 | 99 | 100 | n=1: 101 | 当n=1的时候可以用来表示直线. 102 | 103 | ![06](images/06.png) 104 | 105 | ![08](images/08.gif) 106 | 107 | 108 | n=2: 109 | 当n=2的时候可以用来表示曲线. 110 | ![07](images/07.png) 111 | 112 | ![09](images/09.gif) 113 | 114 | **5阶贝塞尔曲线的演示** 115 | 116 | ![5](images/10.png) 117 | 118 | ![component](images/spline5.gif) 119 | 120 | ### 如何安装与使用? 121 | 122 | **方法一** 123 | * 拷贝dist文件夹中的ghuser 文件到 User Objects 文件夹 124 | * 打开dist文件夹中的案例文件 DeBoorAlgorithmNurbs.gh 查看演示效果 125 | 126 | **方法二** 127 | * 打src文件夹中的案例文件 DeBoorAlgorithmNurbs.gh 查看演示效果 128 | 129 | **b-spline**: 130 | ![component](images/bspline.png) 131 | 132 | **nurbs circle**: 133 | ![component](images/nurbsCircle.png) 134 | 135 | **bezier curve**: 136 | ![component](images/bezier.png) 137 | 138 | ### todo 139 | 140 | * Single Insertion 141 | * Inserting a Knot Multiple Times 142 | * compute tangent and normal vectors at a point on a Bézier curve 143 | * b-spline surface 144 | * curve interpolation 145 | 146 | ### 许可证 147 | 148 | 你可以根据自由软件基金会发布的GNU Lesser General Public License版本3的条款重新发布它和/或修改它。 149 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # De Boor's Algorithm for NURBS Curve 2 | 3 | * verion 0.0.2 4 | * Copyright (c) 2019-2020 mahaidong 5 | * github https://github.com/caadxyz/DeBoorAlgorithmNurbs 6 | * Supported by ikuku.cn & caad.xyz 7 | 8 | [中文](readme-cn.md) 9 | 10 | ### What is this plugin? 11 | 12 | #### Demostrate De Boor's Algorithm for Nurbs 13 | 14 | Gaining an intuitive understanding for NURBS is difficult without directly seeing the effects of different control point position, weight vectors, and knot vectors. 15 | 16 | This grasshopper component allows you demonstrate NURBS curves by specifying the degree of the curve, control points, weight vector and the knot vector. 17 | 18 | This allows the user to see NURBS curves in its most general sense. 19 | 20 | De Boor's Algorithm reference: https://pages.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/de-Boor.html 21 | 22 | #### what is De Boor's Algorithm? 23 | 24 | * The de boor's algorithm is a B-spline version of the DeCasteljau algorithm 25 | * A precise method to evaluate the curve 26 | * Starting from control points and parameter value u, recursively solve. 27 | * de boor's algorithm can also be used for Nurbs curve generation 28 | 29 | ![fun](images/fun.png) 30 | 31 | BSpline's DeBoor Algorithm Pseudocode, python code: [github](https://github.com/caadxyz/DeBoorAlgorithmNurbs) 32 | 33 | ``` 34 | Input: a value u 35 | Output: the point on the curve, p(u) 36 | 37 | If u lies in [uk,uk+1) and u != uk, let h = p (i.e., inserting u p times) and s = 0; 38 | If u = uk and uk is a knot of multiplicity s, let h = p - s (i.e., inserting u p - s time); 39 | Copy the affected control points pk-s, pk-s-1, pk-s-2, ..., pk-p+1 and pk-p to 40 | a new array and rename them as pk-s,0, pk-s-1,0, pk-s-2,0, ..., pk-p+1,0; 41 | 42 | for r := 1 to h do 43 | for i := k-p+r to k-s do 44 | begin 45 | Let ai,r = (u - ui) / ( ui+p-r+1 - ui ) 46 | Let pi,r = (1 - ai,r) pi-1,r-1 + ai,r pi,r-1 47 | end 48 | pk-s,p-s is the point p(u). 49 | ``` 50 | 51 | #### Demostrate 5-degree BSpline 52 | 53 | ![spline](images/deboor-spline.gif) 54 | 55 | 10 control points 56 | knots= [0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 5] 57 | weight= [1, 1, 1, 1, 1, 1, 1, 1, 1,1] 58 | 59 | #### de boor's algorithm for NURBS curves 60 | 61 | De Boor's algorithm also works for NURBS curves. We just multiply every control point by its weight converting the NURBS curve to a 4D B-spline curve, perform de Boor's algorithm on this 4D B-spline curve, and then project the resulting curve back by dividing the first three components with the fourth and keeping the fourth component as its new weight. 62 | 63 | The python source code is available at [github](https://github.com/ caadxyz/DeBoorAlgorithmNurbs) 64 | 65 | 66 | #### Demonstrate a NURBS to fit circle 67 | 68 | ![circle](images/deboor-nurbs.gif) 69 | 70 | 9 control points 71 | knots= [0, 0, 1, 1, 2, 2, 3, 3, 4, 4] 72 | weight=[1, 0.707107, 1, 0.707107, 1, 0.707107, 1, 0.707107, 1] 73 | 74 | 75 | ### Bezier Curve with Bernstein Polynomial 76 | 77 | #### category of parametric curves 78 | 79 | ![nurbs](images/nurbs-diagram.png) 80 | 81 | #### what is bezier curve 82 | 83 | The mathematical basis of the Bezier curve - the Bernstein polynomial - was known as early as 1912, but it was not until approximately It was 50 years before French engineer Pierre Bézier applied these polynomials to the graphics and advertised them widely, and he used them to design car bodies at Renault. These curves were first studied in 1959 by mathematician Paul de Casteljau using de Casteljau's algorithm was developed as a numerically stable method for evaluating another French automotive Bézier curve from the manufacturer Citroën 84 | 85 | The Detail of Bernstein Polynomial: [http://www.ikuku.cn/post/1872817 ](http://www.ikuku.cn/post/1872817) 86 | 87 | **The mathermatical formula for bezier curve** 88 | 89 | Bernstein Polynomial 90 | ![01](images/01.png) 91 | 92 | In which: 93 | 94 | ![02](images/02.png) 95 | 96 | 97 | Definition of n-degree bezier curve 98 | 99 | ![05](images/05.png) 100 | 101 | 102 | n=1: 103 | It can be a line 104 | 105 | ![06](images/06.png) 106 | 107 | ![08](images/08.gif) 108 | 109 | 110 | n=2: 111 | 112 | ![07](images/07.png) 113 | 114 | ![09](images/09.gif) 115 | 116 | #### demostrate 5-degree bezier curve 117 | 118 | ![5](images/10.png) 119 | 120 | ![component](images/spline5.gif) 121 | 122 | ### install and use 123 | 124 | * copy ghuser file to User Objects Folder 125 | * open sample file DeBoorAlgorithmNurbs.gh to see the result 126 | 127 | 128 | 129 | **b-spline**: 130 | ![component](images/bspline.png) 131 | 132 | **nurbs circle**: 133 | ![component](images/nurbsCircle.png) 134 | 135 | **bezier curve**: 136 | ![component](images/bezier.png) 137 | 138 | ### todo 139 | 140 | * Single Insertion 141 | * Inserting a Knot Multiple Times 142 | * compute tangent and normal vectors at a point on a Bézier curve 143 | * b-spline surface 144 | * curve interpolation 145 | 146 | ### License 147 | 148 | You can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3 as published by the Free Software Foundation. 149 | -------------------------------------------------------------------------------- /src/BezierCurve.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | ################################################################################ 3 | # BezierCurve.py 4 | # Copyright (c) 2020.6 mahaidong 5 | # Supported by ikuku.cn & caad.xyz 6 | # See License in the root of this repository for details. 7 | ################################################################################ 8 | 9 | import rhinoscriptsyntax as rs 10 | import Rhino 11 | from System.Drawing import Color 12 | 13 | import os 14 | import sys 15 | sys.path.append( os.path.dirname( ghenv.Component.OnPingDocument().FilePath ) ) 16 | from textGoo import TextGoo 17 | from pointGoo import PointGoo 18 | import ghpythonlib.treehelpers as th 19 | 20 | #debug 21 | import textGoo 22 | import pointGoo 23 | reload(textGoo) 24 | reload(pointGoo) 25 | 26 | 27 | class BezierCurve(object): 28 | 29 | def __init__(self, pts, textHeight): 30 | self.pts = pts 31 | self.legend = [] 32 | self.textHeight = textHeight 33 | self.basisStr() 34 | 35 | def basisStr(self): 36 | n = len(self.pts) 37 | for i in range(n): 38 | text0 = BezierCurve.superscript("",'u',i) 39 | text1 = BezierCurve.superscript(text0, '(1-u)',n-1-i) 40 | if BezierCurve.binomial(n-1,i)==1: 41 | text = text0+text1+"•P"+str(i) 42 | else: 43 | text = str(BezierCurve.binomial(n-1,i))+"•"+text0+text1+"•P"+str(i) 44 | 45 | plane = Rhino.Geometry.Plane(self.pts[i], Rhino.Geometry.Vector3d(0,0,1)) 46 | text3d = TextGoo( Rhino.Display.Text3d( text, plane, self.textHeight )) 47 | hslColor = Rhino.Display.ColorHSL(i/(n+1),1,0.5) 48 | text3d.SetColor(hslColor.ToArgbColor()) 49 | self.legend.append(text3d) 50 | 51 | def basisFunction(self,i,l, origin,nums ): 52 | pts = [] 53 | n = len(self.pts) 54 | for j in range(nums+1): 55 | x = origin.X + l*j/nums 56 | y = origin.Y + l*BezierCurve.binomial(n-1,i)*( (j/nums)**i)*((1-j/nums)**(n-1-i)) 57 | pts.append( Rhino.Geometry.Point3d(x,y,0) ) 58 | return pts 59 | 60 | # algorithm: https://pages.mtu.edu/~shene/COURSES/cs3621/NOTES/notes.html 61 | def calculatePoint( self, u ): 62 | p = None 63 | n = len(self.pts) 64 | for i in range(n): 65 | if i == 0: 66 | p = BezierCurve.binomial(n-1,i)*(u**i)*((1-u)**(n-1-i))*self.pts[i] 67 | else: 68 | p += BezierCurve.binomial(n-1,i)*(u**i)*((1-u)**(n-1-i))*self.pts[i] 69 | return p 70 | 71 | def drawCurvePts(self,nums,u, colorLimit): 72 | pts = [] 73 | for i in range(nums+1): 74 | if i/float(nums+1)<=u: 75 | pt = self.calculatePoint( i/float(nums) ) 76 | ptGoo = PointGoo(pt) 77 | hslColor = Rhino.Display.ColorHSL( colorLimit*i/float(nums+1),1,0.5 ) 78 | ptGoo.SetColor(hslColor.ToArgbColor()) 79 | pts.append( ptGoo ) 80 | return pts 81 | 82 | 83 | @staticmethod 84 | def superscript(p,u,i): 85 | result ="" 86 | if i==0: 87 | pass 88 | elif i==1: 89 | result +=u 90 | elif i==2: 91 | result +=u+'\xB2' 92 | elif i==3: 93 | result +=u+'\xB3' 94 | else: 95 | result +=u+'^'+str(i) 96 | if p is "": 97 | return result 98 | else: 99 | if result is "": 100 | return "" 101 | else: 102 | return '•'+result 103 | 104 | @staticmethod 105 | def binomial(n, k): 106 | """ 107 | A fast way to calculate binomial coefficients by Andrew Dalke. 108 | See http://stackoverflow.com/questions/3025162/statistics-combinations-in-python 109 | """ 110 | if 0 <= k <= n: 111 | ntok = 1 112 | ktok = 1 113 | for t in range(1, min(k, n - k) + 1): 114 | ntok *= n 115 | ktok *= t 116 | n -= 1 117 | return ntok // ktok 118 | else: 119 | return 0 120 | 121 | bc = BezierCurve(pts, textHeight) 122 | legend = bc.legend 123 | crvPoints = bc.drawCurvePts(nums,u,(len(pts)-1)/(len(pts)+1) ) 124 | u2p = [] 125 | u2p.append(bc.calculatePoint(u)) 126 | u2p.append(funPosition+Rhino.Geometry.Point3d(u*funLength,0,0)) 127 | 128 | basisFunction = [] 129 | for i in range(len(pts)): 130 | basisPts = bc.basisFunction(i,funLength,funPosition,nums) 131 | basisPtGooList = [] 132 | for pt in basisPts: 133 | ptGoo = PointGoo(pt) 134 | hslColor = Rhino.Display.ColorHSL(i/(len(pts)+1),1,0.5) 135 | ptGoo.SetColor(hslColor.ToArgbColor()) 136 | basisPtGooList.append(ptGoo) 137 | basisFunction.append( basisPtGooList ) 138 | basisFunction = th.list_to_tree(basisFunction, source=[0,0]) 139 | 140 | 141 | -------------------------------------------------------------------------------- /src/DeBoorAlgorithmNurbs.gh: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/src/DeBoorAlgorithmNurbs.gh -------------------------------------------------------------------------------- /src/DeBoorAlgorithmNurbs.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | ################################################################################ 3 | # DeBoorAlgorithmNurbs.py 4 | # Copyright (c) 2020.4 mahaidong 5 | # Supported by ikuku.cn & caad.xyz 6 | # See License in the root of this repository for details. 7 | ################################################################################ 8 | import rhinoscriptsyntax as rs 9 | import Rhino 10 | from System.Drawing import Color 11 | import ghpythonlib.treehelpers as th 12 | 13 | import os 14 | import sys 15 | sys.path.append( os.path.dirname( ghenv.Component.OnPingDocument().FilePath ) ) 16 | from textGoo import TextGoo 17 | from lineGoo import LineGoo 18 | 19 | # debug 20 | import textGoo 21 | import lineGoo 22 | reload(textGoo) 23 | reload(lineGoo) 24 | 25 | class DeBoorAlgorithmNurbs(object): 26 | def __init__( self,degree, pts,weights,knots,isPeriodic, isRational): 27 | self.degree = degree 28 | self.pts = pts 29 | self.weights = weights 30 | self.knots = knots 31 | self.isPeriodic = isPeriodic 32 | self.isRational = isRational 33 | 34 | def findKnotIndexByU(self, u): 35 | s = 0 36 | index = 0 37 | if uself.knots[-1]: 38 | return 39 | for i in range(len(self.knots)): 40 | if self.knots[i]>u: 41 | if s==0 : 42 | return s,i-1 43 | elif self.knots[i] == u: 44 | index = i 45 | s += 1 46 | if s>0: 47 | return s,index 48 | 49 | def getKnotValue(self,i): 50 | if self.isPeriodic == True: 51 | if i>=len(self.knots): 52 | return self.knots[-1]+( self.knots[i+1-len(self.knots)]-self.knots[0] ) 53 | elif i<0: 54 | return self.knots[0]-( self.knots[-1]-self.knots[len(self.knots)+i-1] ) 55 | return self.knots[i] 56 | 57 | def calculateNurbsPoint(self,u, haveLines=0): 58 | """ 59 | parameters: 60 | havelines: 0=nolines, 1=drawLines 61 | """ 62 | p = self.degree 63 | s,k = self.findKnotIndexByU(u) 64 | h = p-s 65 | pt = {} 66 | ptsGoo=[] 67 | 68 | # initialize pt[i][0] for starting calculation 69 | for i in range(k-p+1,k-s+2): 70 | pt[i]={} 71 | if i < len(self.pts): 72 | j = i 73 | else: 74 | j = i-len(self.pts) 75 | 76 | if self.isRational: 77 | x = self.pts[j].X*self.weights[j] 78 | y = self.pts[j].Y*self.weights[j] 79 | z = self.pts[j].Z*self.weights[j] 80 | w = weights[j] 81 | else: 82 | x = self.pts[j].X 83 | y = self.pts[j].Y 84 | z = self.pts[j].Z 85 | w = 1 86 | pt[i][0] = Rhino.Geometry.Point4d(x,y,z,w) 87 | if haveLines==1: 88 | 89 | plane = Rhino.Geometry.Plane(self.pts[j], Rhino.Geometry.Vector3d(0,0,1)) 90 | text3d = TextGoo( Rhino.Display.Text3d( "P"+str(i), plane, 3 )) 91 | hslColor = Rhino.Display.ColorHSL(0,1,0.5) 92 | text3d.SetColor(hslColor.ToArgbColor()) 93 | ptsGoo.append(text3d) 94 | 95 | def calculatePoint4d( p0, factor0,p1,factor1 ): 96 | return Rhino.Geometry.Point4d( 97 | p0.X*factor0+p1.X*factor1, 98 | p0.Y*factor0+p1.Y*factor1, 99 | p0.Z*factor0+p1.Z*factor1, 100 | p0.W*factor0+p1.W*factor1) 101 | 102 | # starting calculation 103 | a = {} 104 | lines = [] 105 | for r in range (1,h+1): 106 | lines.append([]) 107 | for i in range(k-p+r+1,k-s+2) : 108 | a[i]={} 109 | Vai_1 = self.getKnotValue( i-1 ) 110 | Vaip_r = self.getKnotValue( i+p-r ) 111 | a[i][r] = (u-Vai_1)/( Vaip_r-Vai_1) 112 | pt[i][r] = calculatePoint4d( pt[i-1][r-1],(1-a[i][r]),pt[i][r-1],a[i][r] ) 113 | if haveLines == 1: 114 | line = Rhino.Geometry.Line( 115 | Rhino.Geometry.Point3d(pt[i-1][r-1]), 116 | Rhino.Geometry.Point3d(pt[i][r-1]) ) 117 | lineGoo = LineGoo(line) 118 | hslColor = Rhino.Display.ColorHSL((r-1)/(h+1),1,0.5) 119 | lineGoo.SetColor(hslColor.ToArgbColor()) 120 | lines[r-1].append(lineGoo) 121 | 122 | return Rhino.Geometry.Point3d(pt[k-s+1][p-s]), ptsGoo, lines 123 | 124 | # draw curves and lines 125 | def drawNurbsCurvePts(self,nums,u): 126 | pts = [] 127 | for f in rs.frange( self.knots[0],self.knots[-1],(self.knots[-1]-self.knots[0])/float(nums) ): 128 | if f" 41 | return self.m_value.Text 42 | 43 | def get_Boundingbox(self): 44 | if self.m_value is None: 45 | return Rhino.Geometry.BoundingBox.Empty 46 | return self.m_value.BoundingBox 47 | 48 | def GetBoundingBox(self, xform): 49 | if self.m_value is None: 50 | return Rhino.Geometry.BoundingBox.Empty 51 | box = self.m_value.BoundingBox 52 | corners = xform.TransformList(box.GetCorners()) 53 | return Rhino.Geometry.BoundingBox(corners) 54 | 55 | #region methods 56 | def Transform(self, xform): 57 | text = TextGoo.DuplicateText3d(self.m_value) 58 | if text is None: return TextGoo(None) 59 | 60 | plane = text.TextPlane 61 | point = plane.PointAt(1, 1) 62 | 63 | plane.Transform(xform) 64 | point.Transform(xform) 65 | dd = point.DistanceTo(plane.Origin) 66 | 67 | text.TextPlane = plane 68 | text.Height *= dd / sqrt(2) 69 | return TextGoo(text) 70 | 71 | def Morph(self, xmorph): 72 | return self.DuplicateGeometry() 73 | 74 | #region preview 75 | def get_ClippingBox(self): 76 | return self.get_Boundingbox() 77 | 78 | def DrawViewportWires(self, args): 79 | if self.m_value is None: return 80 | # args.Pipeline.Draw3dText(self.m_value, args.Color) 81 | args.Pipeline.Draw3dText(self.m_value, self.color) 82 | args.Pipeline.DrawPoint(self.m_value.TextPlane.Origin,self.color) 83 | 84 | def DrawViewportMeshes(self, args): 85 | # Do not draw in meshing layer. 86 | pass 87 | 88 | #region baking 89 | def BakeGeometry(self, doc, att, id): 90 | id = System.Guid.Empty 91 | if self.m_value is None: 92 | return False, id 93 | if att is None: 94 | att = doc.CreateDefaultAttributes() 95 | id = doc.Objects.AddText(self.m_value, att) 96 | return True, id 97 | -------------------------------------------------------------------------------- /src/lineGoo.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import rhinoscriptsyntax as rs 3 | import Grasshopper as gh 4 | import Rhino 5 | import System 6 | from math import sqrt 7 | 8 | class LineGoo( gh.Kernel.Types.GH_GeometricGoo[Rhino.Geometry.Line], 9 | gh.Kernel.IGH_PreviewData ): 10 | 11 | #region construction 12 | def __init__(self, line): 13 | self.m_value = line 14 | self.color = None 15 | 16 | def SetColor(self,color): 17 | self.color = color 18 | 19 | @staticmethod 20 | def DuplicateLine(original): 21 | if original is None: return None 22 | return Rhino.Geometry.Line(original.From, original.To) 23 | 24 | def DuplicateGeometry(self): 25 | return LineGoo(LineGoo.DuplicateLine(self.m_value)) 26 | 27 | #region properties 28 | def get_TypeName(self): 29 | return "colored point3d" 30 | 31 | def get_TypeDescription(self): 32 | return "colored point3d" 33 | 34 | def ToString(self): 35 | if self.m_value is None: return "" 36 | return self.m_value.ToString() 37 | 38 | def get_Boundingbox(self): 39 | if self.m_value is None: 40 | return Rhino.Geometry.BoundingBox.Empty 41 | return self.m_value.BoundingBox 42 | 43 | def GetBoundingBox(self, xform): 44 | if self.m_value is None: 45 | return Rhino.Geometry.BoundingBox.Empty 46 | box = self.m_value.BoundingBox 47 | corners = xform.TransformList(box.GetCorners()) 48 | return Rhino.Geometry.BoundingBox(corners) 49 | 50 | #region preview 51 | def DrawViewportWires(self, args): 52 | if self.m_value is None: return 53 | args.Pipeline.DrawLine(self.m_value,self.color) 54 | 55 | def DrawViewportMeshes(self, args): 56 | pass 57 | 58 | -------------------------------------------------------------------------------- /src/pointGoo.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import rhinoscriptsyntax as rs 3 | import Grasshopper as gh 4 | import Rhino 5 | import System 6 | from math import sqrt 7 | 8 | class PointGoo( gh.Kernel.Types.GH_GeometricGoo[Rhino.Geometry.Point3d], 9 | gh.Kernel.IGH_PreviewData ): 10 | 11 | #region construction 12 | def __init__(self, point): 13 | self.m_value = point 14 | self.color = None 15 | 16 | def SetColor(self,color): 17 | self.color = color 18 | 19 | @staticmethod 20 | def DuplicatePoint(original): 21 | if original is None: return None 22 | return Rhino.Geometry.Point3d(original.X,original.Y,original.Z) 23 | 24 | def DuplicateGeometry(self): 25 | return PointGoo(PointGoo.DuplicatePoint(self.m_value)) 26 | 27 | #region properties 28 | def get_TypeName(self): 29 | return "colored point3d" 30 | 31 | def get_TypeDescription(self): 32 | return "colored point3d" 33 | 34 | def ToString(self): 35 | if self.m_value is None: return "" 36 | return str(self.m_value.X)+','+str(self.m_value.Y)+','+str(self.m_value.Z) 37 | 38 | #region preview 39 | def DrawViewportWires(self, args): 40 | if self.m_value is None: return 41 | args.Pipeline.DrawPoint(self.m_value,self.color) 42 | 43 | def DrawViewportMeshes(self, args): 44 | pass 45 | 46 | 47 | -------------------------------------------------------------------------------- /src/resource/diagram.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/src/resource/diagram.ai -------------------------------------------------------------------------------- /src/resource/icon-small-bc.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/src/resource/icon-small-bc.ai -------------------------------------------------------------------------------- /src/resource/icon-small-bc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/src/resource/icon-small-bc.png -------------------------------------------------------------------------------- /src/resource/icon-small-db.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/src/resource/icon-small-db.ai -------------------------------------------------------------------------------- /src/resource/icon-small-db.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/src/resource/icon-small-db.png -------------------------------------------------------------------------------- /src/resource/icon.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/src/resource/icon.ai -------------------------------------------------------------------------------- /src/resource/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/caadxyz/DeBoorAlgorithmNurbs/683f93a9c430eaecd45684521ebdc4561181de9b/src/resource/icon.png --------------------------------------------------------------------------------