Package: nurbspy
nurbspyDescriptionnurbspyis a Python package for Non-Uniform Rational Basis Spline (NURBS) curves and surfaces.
The classes and methods were inspired by the algorithms presented inThe NURBS Bookand the code was implemented using vectorizedNumpyfunctions andNumba'sjust-in-time compilation decorators to achieve C-like speed.nurbspyaims to be a simple NURBS library, not a fully fledged CAD kernel. If you need a powerful, open source CAD kernel we recommend you to check out the C++OpenCascadelibrary. If you feel that OpenCascade is too complex or you are not sure how to start using it,this repositorymight be useful for you!Capabilitiesnurbspyhas the following features for NURBS curves:Constructors for rational and non-rational Bézier and B-Spline curvesMethods to evaluate curve coordinatesMethods to evaluate arbitrary-order derivatives analyticallyMethods to evaluate the tangent, normal, and binormal unitary vectors (Frenet-Serret frame of reference)Methods to compute the curvature and torsionMethods to compute the arc-length of the curve by numerical quadratureMethods to for point projection / point inversionMethods to visualize the curve using the Matplotlib libraryIn addition,nurbspyprovides the following capabilities for NURBS surfaces:Constructors for rational and non-rational Bézier and B-Spline surfacesAdditional constructors for some common special surfaces:Bilinear surfacesRuled surfacesExtruded surfacesRevolution surfacesCoons surfacesMethods to evaluate surface coordinatesMethods to evaluate arbitrary-order derivatives analyticallyMethods to evaluate the unitary normal vectorsMethods to evaluate the mean and Gaussian curvaturesMethods to compute u- and v-isoparametic curvesMethods to for point projection / point inversionMethods to visualize the surface using the Matplotlib libraryIn addition,nurbspycan work with real and complex data types natively. This allows to compute accurate (down tomachine epsilon!) shape derivatives using thecomplex step methodand avoid the numerical error incurred by finite-difference derivative approximations. This shape sensitivity information is necessary to solve shape optimization problems with many design variables using gradient based-optimization algorithms. To our knowledge,nurbspyis the only Python package that provides the flexibility to work with complex numbers right away.Installationnurbspyhas the following mandatory runtime dependencies:numpy(multidimensional array library)scipy(scientific computing library)numba(just-in-time Python compiler)pygmo(optimization library)matplotlib(visualization library)In addition,nurbspyusespytestfor local tests.nurbspyis available on Linux via thepippackage manager. The installation with pip is straightfoward:pipinstallnurbspynurbspyis also available on Linux, Windows, and macOS via thecondainstaller. In order to installnurbspyvia conda you need to addconda-forgeandroberagroto the list of available channels:condainstallnurbspy--channelconda-forge--channelroberagroYou can verify thatnurbspywas installed successfully with this minimal Python script:# Nurbspy minimal working exampleimportnurbspynurbspy.minimal_example.run()or by typing this one-liner on your terminal:python3-c"import nurbspy; nurbspy.minimal_example.run()"Minimal working examplesNURBS curvesnurbspycan  be used to create Bézier, B-Spline and NURBS curves. The type of curve depends on the arguments used to initialize the curve class. As an example, the following piece of code can be used to generate a degree four Bézier curve in two dimensions:# Import packagesimportnumpyasnpimportnurbspyasnrbimportmatplotlib.pyplotasplt# Define the array of control pointsP=np.zeros((2,5))P[:,0]=[0.20,0.50]P[:,1]=[0.40,0.70]P[:,2]=[0.80,0.60]P[:,3]=[0.80,0.40]P[:,4]=[0.40,0.20]# Create and plot the Bezier curvebezierCurve=nrb.NurbsCurve(control_points=P)bezierCurve.plot()plt.show()If the installation was succesful, you should be able to see the Bézier curve when you execute the previous code snippet.Check out thecurve demosdirectory to see more examples showing the capabilities of the library and how to use them.NURBS surfacesSimilarly,nurbspycan  be used to create Bézier, B-Spline and NURBS surfaces. The type of surface depends on the arguments used to initialize the surface class. As an example, the following code snippet can be used to generate a simple Bézier surface of degree 3 in the u-direction and degree 2 in the v-direction:# Import packagesimportnumpyasnpimportnurbspyasnrbimportmatplotlib.pyplotasplt# Define the array of control pointsn_dim,n,m=3,4,3P=np.zeros((n_dim,n,m))# First rowP[:,0,0]=[0.00,0.00,0.00]P[:,1,0]=[1.00,0.00,1.00]P[:,2,0]=[2.00,0.00,1.00]P[:,3,0]=[3.00,0.00,0.00]# Second rowP[:,0,1]=[0.00,1.00,1.00]P[:,1,1]=[1.00,1.00,2.00]P[:,2,1]=[2.00,1.00,2.00]P[:,3,1]=[3.00,1.00,1.00]# Third rowP[:,0,2]=[0.00,2.00,0.00]P[:,1,2]=[1.00,2.00,1.00]P[:,2,2]=[2.00,2.00,1.00]P[:,3,2]=[3.00,2.00,0.00]# Create and plot the Bezier surfacebezierSurface=nrb.NurbsSurface(control_points=P)bezierSurface.plot(control_points=True,isocurves_u=6,isocurves_v=6)plt.show()If the installation was successful, you should be able to see the Bézier surface when you execute the previous script.Check out thesurface demosdirectory to see more examples showing the capabilities of the library and how to use them.Mathematical backgroundCheck out theBézier,B-Spline, andNURBSnotes if you want to learn more about the definition and mathematical properties of these curves and surfacesContact informationnurbspywas developed byRoberto Agromayorunder the supervision of Associate ProfessorLars O. Nordat theNorwegian University of Science and Technology (NTNU)as part of his PhD on turbomachinery shape optimization. Please, drop us an email toroberto.agromayor@ntnu.noif you have questions about the code or you have a bug to report!