Skip to main content

Spline modelling library for Python

Project description

Splipy Logo SpliPy

This repository contains the SpliPy packages. SpliPy is a pure python library for the creation, evaluation and manipulation of B-spline and NURBS geometries. It supports n-variate splines of any dimension, but emphasis is made on the use of curves, surfaces and volumes. The library is designed primarily for analysis use, and therefore allows fine-grained control over many aspects which is not possible to achieve with conventional CAD tools.

Features

SpliPy allows for the generation of parametric curves, surfaces and volumes in the form of non-uniform rational B-splines (NURBS). It supports traditional curve- and surface-fitting methods such as (but not limited to)

Curve fitting

  • Bezier curves
  • Hermite Interpolation
  • Cubic Curve Interpolation
  • B-spline Interpolation
  • Least Square Fit

Surface operations

  • Sweep

  • Revolve

  • Loft

  • Edge_Curves (interior from four edges)

  • Extrude

  • Structured Point Cloud Interpolation

  • Least Square Fit

    Revolve Revolve

Sweep Sweep

Loft Loft

Volume operations

  • Revolve
  • Extrude
  • Loft
  • Structured Point Cloud Interpolation
  • Least Square Fit

In addition to these basic building blocks, it also supports a number of primitive shapes such as (but not limited to)

Primitive shapes

  • Cube
  • Circle
  • Disc
  • Cylinder
  • Torus
  • Teapot

Examples

Derivatives of spline curves

  from splipy import *
  import numpy as np

  n = 250                                  # number of evaluation points
  c = curve_factory.circle()               # create the NURBS circle (r=1)
  t = np.linspace(c.start(0), c.end(0), n) # parametric evaluation points
  x = c(t)                                 # physical (x,y)-coordinates, size (n,2)
  v = c.derivative(t, 1)                   # velocity at all points
  a = c.derivative(t, 2)                   # acceleration at all points

Missing circle animation

Curve fitting

Lissajous curves are a family of parametric curves of the type

x = A sin(at+d)
y = B sin(bt)

More info: https://en.wikipedia.org/wiki/Lissajous_curve. Stripping the animation parts of the code, one can generate these curves in the following way

from splipy import *
import numpy as np
from fractions import gcd

def lissajous(a, b, d):
  # request a,b integers, so we have closed, periodic curves
  n = np.gcd(a,b)
  N = (a/n) * (b/n) # number of periods before looping

  # compute a set of interpolation points
  numb_pts = max(3*N, 100) # using 3N interpolation points is decent enough
  t = 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 conditions
my_curve = curve_factory.cubic_curve(x.T, curve_factory.Boundary.PERIODIC)

Missing Lissajous curve animation

Animation of the lissajous curve with a=3, b=4 and d=pi/2

Surface Sweep

This produces the trefoil knot shown above

from splipy import *
from numpy import pi,cos,sin,transpose,array,sqrt

# define a parametric representation of the trefoil knot (to be sampled)
def trefoil(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)]
  return transpose(array(x))

knot_curve   = curve_factory.fit(trefoil, 0, 2*pi) # adaptive curve fit of trefoil knot
square_curve = 15 * curve_factory.n_gon(4)         # square cross-section
my_surface   = surface_factory.sweep(crv, square)  # sweep out the surface

Working with the controlpoints

>>> from splipy import *
>>> my_curve = curve_factory.circle(r=3)
>>> print(my_curve[0])
[3. 0. 1.]
>>> print(my_curve[1])
[2.12132034 2.12132034 0.70710678]
>>> for controlpoint in my_curve:
...     print(controlpoint)
[3. 0. 1.]
[2.12132034 2.12132034 0.70710678]
[0. 3. 1.]
[-2.12132034  2.12132034  0.70710678]
[-3.  0.  1.]
[-2.12132034 -2.12132034  0.70710678]
[ 0. -3.  1.]
[ 2.12132034 -2.12132034  0.70710678]

Creating STL files

STL files are used extensively for 3D representation and is one of the only supported formats for 3D printing.

from splipy.io import STL
from splipy import surface_factory

# create a NURBS torus
my_torus = surface_factory.torus(minor_r=1, major_r=4)

# STL files are tessellated linear triangles. View with i.e. meshlab
with STL('torus.stl') as my_file:
    my_file.write(my_torus, n=(50, 150)) # specify resolution of 50x150 evaluation pts

Torus tesselation as viewed in Meshlab Torus

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Splipy-1.3.1.tar.gz (101.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Splipy-1.3.1-py2.py3-none-any.whl (117.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file Splipy-1.3.1.tar.gz.

File metadata

  • Download URL: Splipy-1.3.1.tar.gz
  • Upload date:
  • Size: 101.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Splipy-1.3.1.tar.gz
Algorithm Hash digest
SHA256 43b5f2d03cccf9339d3c582e52433d71dfd2bad7598351719e123dc8a87b9601
MD5 7f33c10fe894d39c259498a5a9fd3b37
BLAKE2b-256 67942d2cdd5eacbfcdb5aed034049db665eedc36e8ddba8f66267b0680c3b22b

See more details on using hashes here.

File details

Details for the file Splipy-1.3.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for Splipy-1.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 15bdd9148cc9a1c803543cd3705a8aa233e758a81daee1bdc6c255435efd0488
MD5 b9e2b7c4dde9c939dc63182451e9ea5c
BLAKE2b-256 f5df6a4dc273987f620795b438f773c0fe01e7d39fff5c2dfa712c5b808d43d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page