splines§

Piecewise polynomial curves (in Euclidean space).

Submodules

quaternion

Quaternions and unit-quaternion splines.

Classes

Bernstein(segments[, grid])

Piecewise Bézier curve using Bernstein basis.

CatmullRom(vertices[, grid, alpha, …])

Catmull–Rom spline.

ConstantSpeedAdapter(curve)

Re-parameterize a spline to have constant speed.

CubicHermite(vertices, tangents[, grid])

Cubic Hermite curve.

KochanekBartels(vertices[, grid, tcb, …])

Kochanek–Bartels spline.

Monomial(segments, grid)

Piecewise polynomial curve using monomial basis.

MonotoneCubic(values, *args, **kwargs)

Monotone cubic curve.

Natural(vertices[, grid, alpha, endconditions])

Natural spline.

NewGridAdapter(curve[, new_grid])

Re-parameterize a spline with new grid values.

PiecewiseMonotoneCubic(values[, grid, …])

Piecewise monotone cubic curve.

class splines.Monomial(segments, grid)[source]§

Bases: object

Piecewise polynomial curve using monomial basis.

Arbitrary degree, arbitrary dimension.

\[\boldsymbol{p}_i(t) = \sum_{k=0}^n \boldsymbol{a}_k \left(\frac{t - t_i}{t_{i+1} - t_i}\right)^k \text{ for } t_i \leq t < t_{i+1}\]

Similar to https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.PPoly.html, which states:

“High-order polynomials in the power basis can be numerically unstable. Precision problems can start to appear for orders larger than 20-30.”

Parameters
  • segments – Sequence of polynomial segments. Each segment contains coefficients for the monomial basis (in order of decreasing degree). Different segments can have different polynomial degree.

  • grid – Sequence of parameter values corresponding to segment boundaries. Must be strictly increasing.

evaluate(t, n=0)[source]§

Get value (or n-th derivative) at given parameter value(s).

class splines.Bernstein(segments, grid=None)[source]§

Bases: object

Piecewise Bézier curve using Bernstein basis.

Parameters
  • segments – Sequence of segments, each one consisting of multiple Bézier control points. Different segments can have different numbers of control points (and therefore different polynomial degrees).

  • grid (optional) – Sequence of parameter values corresponding to segment boundaries. Must be strictly increasing. If not specified, a uniform grid is used (0, 1, 2, 3, …).

static basis(degree, t)[source]§

Bernstein basis polynomials of given degree, evaluated at t.

Returns a list of values corresponding to \(i = 0, \ldots, n\), given the degree \(n\), using the formula

\[b_{i,n}(t) = {n \choose i} t^i \left( 1 - t \right)^{n - i},\]

with the binomial coefficient \({n \choose i} = \frac{n!}{i!(n - i)!}\).

evaluate(t, n=0)[source]§

Get value at the given parameter value(s).

class splines.CubicHermite(vertices, tangents, grid=None)[source]§

Bases: splines.Monomial

Cubic Hermite curve.

See Hermite Splines.

Parameters
  • vertices – Sequence of vertices.

  • tangents – Sequence of tangent vectors (two per segment, outgoing and incoming).

  • grid (optional) – Sequence of parameter values. Must be strictly increasing. If not specified, a uniform grid is used (0, 1, 2, 3, …).

matrix = array([[ 2, -2,  1,  1],        [-3,  3, -2, -1],        [ 0,  0,  1,  0],        [ 1,  0,  0,  0]])§
class splines.CatmullRom(vertices, grid=None, *, alpha=None, endconditions='natural')[source]§

Bases: splines.CubicHermite

Catmull–Rom spline.

This class implements one specific member of the family of splines described in [CR74], which is commonly known as Catmull–Rom spline: The cubic spline that can be constructed by linear Lagrange interpolation (and extrapolation) followed by quadratic B-spline blending, or equivalently, quadratic Lagrange interpolation followed by linear B-spline blending.

The implementation used in this class, however, does nothing of that sort. It simply calculates the appropriate tangent vectors at the control points and instantiates a CubicHermite spline.

See Catmull--Rom Splines.

Parameters
  • vertices – Sequence of vertices.

  • grid (optional) – Sequence of parameter values. Must be strictly increasing. If not specified, a uniform grid is used (0, 1, 2, 3, …).

  • alpha (optional) – TODO

  • endconditions (optional) – Start/end conditions. Can be 'closed', 'natural' or pair of tangent vectors (a.k.a. “clamped”). If 'closed', the first vertex is re-used as last vertex and an additional grid time has to be specified.

class splines.KochanekBartels(vertices, grid=None, *, tcb=(0, 0, 0), alpha=None, endconditions='natural')[source]§

Bases: splines.CubicHermite

Kochanek–Bartels spline.

See Kochanek--Bartels Splines.

Parameters
  • vertices – Sequence of vertices.

  • grid (optional) – Sequence of parameter values. Must be strictly increasing. If not specified, a uniform grid is used (0, 1, 2, 3, …).

  • tcb (optional) – Sequence of tension, continuity and bias triples. TCB values can only be given for the interior vertices.

  • alpha (optional) – TODO

  • endconditions (optional) – Start/end conditions. Can be 'closed', 'natural' or pair of tangent vectors (a.k.a. “clamped”). If 'closed', the first vertex is re-used as last vertex and an additional grid time has to be specified.

class splines.Natural(vertices, grid=None, *, alpha=None, endconditions='natural')[source]§

Bases: splines.CubicHermite

Natural spline.

See Natural Splines.

Parameters
  • vertices – Sequence of vertices.

  • grid (optional) – Sequence of parameter values. Must be strictly increasing. If not specified, a uniform grid is used (0, 1, 2, 3, …).

  • alpha (optional) – TODO

  • endconditions (optional) – Start/end conditions. Can be 'closed', 'natural' or pair of tangent vectors (a.k.a. “clamped”). If 'closed', the first vertex is re-used as last vertex and an additional grid time has to be specified.

class splines.PiecewiseMonotoneCubic(values, grid=None, slopes=None, *, alpha=None, closed=False)[source]§

Bases: splines.CatmullRom

Piecewise monotone cubic curve.

See Piecewise Monotone Interpolation.

This only works for one-dimensional values.

For undefined slopes, _calculate_tangent() is called on the base class.

Parameters
  • values – Sequence of values to be interpolated.

  • grid (optional) – Sequence of parameter values. Must be strictly increasing. If not specified, a uniform grid is used (0, 1, 2, 3, …).

  • slopes (optional) – Sequence of slopes or None if slope should be computed from neighboring values. An error is raised if a segment would become non-monotone with a given slope.

class splines.MonotoneCubic(values, *args, **kwargs)[source]§

Bases: splines.PiecewiseMonotoneCubic

Monotone cubic curve.

This takes the same arguments as PiecewiseMonotoneCubic (except closed), but it raises an error if the given values are not montone.

See Monotone Interpolation.

get_time(value)[source]§

Get the time instance for the given value.

If the solution is not unique (i.e. there is a plateau), None is returned.

class splines.ConstantSpeedAdapter(curve)[source]§

Bases: object

Re-parameterize a spline to have constant speed.

For splines in Euclidean space this amounts to arc-length parameterization.

However, this class is implemented in a way that also allows using rotation splines which will be re-parameterized to have constant angular speed.

The parameter s represents the cumulative arc-length or the cumulative rotation angle, respectively.

evaluate(s)[source]§
class splines.NewGridAdapter(curve, new_grid=1)[source]§

Bases: object

Re-parameterize a spline with new grid values.

Parameters
  • curve – A spline.

  • new_grid (optional) – If a single number is given, the new parameter will range from 0 to that number. Otherwise, a sequence of numbers has to be given, one for each grid value. Instead of a value, None can be specified to choose a value automatically. The first and last value cannot be None.

evaluate(u)[source]§