splines#

Piecewise polynomial curves (in Euclidean space).

class splines.Monomial(segments, grid=None)[source]#

Bases: object

Piecewise polynomial curve using monomial basis.

See Parametric Polynomial Curves.

Coefficients can have an arbitrary number of dimensions. An arbitrary polynomial degree \(d\) can be used by specifying \(d + 1\) coefficients per segment. The \(i\)-th segment is evaluated using

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

This is similar to scipy.interpolate.PPoly, 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.

This shouldn’t be a problem, since most commonly splines of degree 3 (i.e. cubic splines) are used.

Parameters:
  • segments – Sequence of polynomial segments. Each segment \(\boldsymbol{a}_i\) contains coefficients for the monomial basis (in order of decreasing degree). Different segments can have different polynomial degrees.

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

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

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

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

Bases: object

Piecewise Bézier curve using Bernstein basis.

See Bézier Splines.

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

\[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) t.

Only n=0 is currently supported.

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

Bases: 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: CubicHermite

Catmull–Rom spline.

This class implements one specific member of the family of splines described by Catmull and Rom [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) – See Parameterized Parameterization.

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

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

Bases: 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) – See Parameterized Parameterization.

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

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

Bases: 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) – See Parameterized Parameterization.

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

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

Bases: 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, grid=None, slopes=None, *, alpha=None, cyclic=False, **kwargs)[source]#

Bases: PiecewiseMonotoneCubic

Monotone cubic curve.

This takes the same arguments as PiecewiseMonotoneCubic (except closed is replaced by cyclic), 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. if there is a plateau), None is returned.

class splines.UnitSpeedAdapter(curve)[source]#

Bases: object

Re-parameterize a spline to have a constant speed of 1.

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 a Constant Angular Speed of 1. For this to work, the second derivative of curve must yield an angular velocity vector. See splines.quaternion.DeCasteljau for an example of a compatible rotation spline.

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

evaluate(s)[source]#

Get value at the given parameter value(s) s.

class splines.NewGridAdapter(curve, new_grid=1, cyclic=False)[source]#

Bases: object

Re-parameterize a spline with new grid values.

This can be used for both Euclidean splines and rotation splines.

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.

  • cyclic (optional) – If True, the slope of the re-parameterization function (but not necessarily the speed of the final spline!) will be the same at the beginning and end of the spline.

evaluate(u)[source]#

Get value at the given parameter value(s) u.