python样条建模库
Splip的Python项目详细描述
splipy
这个存储库包含splipy包。splipy是一个纯python库 用于创建、计算和操作B样条曲线和NURBS几何体。 它支持任意维的n元样条,但重点是 曲线、曲面和体积的使用。这个图书馆主要是为 分析使用,因此允许对许多方面进行细粒度控制 不可能用传统的cad工具来实现。
功能
splipy允许以非均匀有理b样条(nurbs)的形式生成参数曲线、曲面和体积。它支持传统的曲线和曲面拟合方法,例如(但不限于)
曲线拟合
- 贝塞尔曲线
- hermite插值
- 三次曲线插值
- B样条插值
- 最小二乘法拟合
地面作业
扫描
旋转
阁楼
边缘曲线(四条边缘的内部)
挤出
结构化点云插值
最小二乘法拟合
revolve
扫描
loft
容积操作
- 旋转
- 挤出
- 阁楼
- 结构化点云插值
- 最小二乘法拟合
除了这些基本构造块之外,它还支持许多基本形状,例如(但不限于)
基本形状
- 立方体
- 圆圈
- 光盘
- 气缸
- 圆环体
- 茶壶
示例
样条曲线的导数
fromsplipyimport*importnumpyasnpn=250# number of evaluation pointsc=curve_factory.circle()# create the NURBS circle (r=1)t=np.linspace(c.start(0),c.end(0),n)# parametric evaluation pointsx=c(t)# physical (x,y)-coordinates, size (n,2)v=c.derivative(t,1)# velocity at all pointsa=c.derivative(t,2)# acceleration at all points
曲线拟合
lissajous曲线是一类参数曲线
x = A sin(at+d)
y = B sin(bt)
更多信息:https://en.wikipedia.org/wiki/Lissajous_curve。剥离animation parts of the code,可以按以下方式生成这些曲线
fromsplipyimport*importnumpyasnpfromfractionsimportgcddeflissajous(a,b,d):# request a,b integers, so we have closed, periodic curvesn=np.gcd(a,b)N=(a/n)*(b/n)# number of periods before looping# compute a set of interpolation pointsnumb_pts=max(3*N,100)# using 3N interpolation points is decent enought=np.linspace(0,2*np.pi/n,numb_pts)x=np.array([np.sin(a*t+d),np.sin(b*t)])# do a cubic curve interpolation with periodic boundary conditionsmy_curve=curve_factory.cubic_curve(x.T,curve_factory.Boundary.PERIODIC)
用a=3,b=4和d=pi/2制作李萨如曲线的动画
表面扫描
这将产生上面所示的三叶结
fromsplipyimport*fromnumpyimportpi,cos,sin,transpose,array,sqrt# define a parametric representation of the trefoil knot (to be sampled)deftrefoil(u):x=[41*cos(u)-18*sin(u)-83*cos(2*u)-83*sin(2*u)-11*cos(3*u)+27*sin(3*u),36*cos(u)+27*sin(u)-113*cos(2*u)+30*sin(2*u)+11*cos(3*u)-27*sin(3*u),45*sin(u)-30*cos(2*u)+113*sin(2*u)-11*cos(3*u)+27*sin(3*u)]returntranspose(array(x))knot_curve=curve_factory.fit(trefoil,0,2*pi)# adaptive curve fit of trefoil knotsquare_curve=15*curve_factory.n_gon(4)# square cross-sectionmy_surface=surface_factory.sweep(crv,square)# sweep out the surface
使用控制点
>>>fromsplipyimport*>>>my_curve=curve_factory.circle(r=3)>>>print(my_curve[0])[3.0.1.]>>>print(my_curve[1])[2.121320342.121320340.70710678]>>>forcontrolpointinmy_curve:...print(controlpoint)[3.0.1.][2.121320342.121320340.70710678][0.3.1.][-2.121320342.121320340.70710678][-3.0.1.][-2.12132034-2.121320340.70710678][0.-3.1.][2.12132034-2.121320340.70710678]
创建stl文件
stl文件广泛用于3d表示,是3d打印唯一支持的格式之一。
fromsplipy.ioimportSTLfromsplipyimportsurface_factory# create a NURBS torusmy_torus=surface_factory.torus(minor_r=1,major_r=4)# STL files are tessellated linear triangles. View with i.e. meshlabwithSTL('torus.stl')asmy_file:my_file.write(my_torus,n=(50,150))# specify resolution of 50x150 evaluation pts
在meshlab中查看的圆环镶嵌