splinelib package

Submodules

splinelib.fit module

splinelib.fit.centripetal(data)[source]

Returns a column vector with parametrization for the data using the centripetal parametrization scheme.

Parameters:data (np.ndarray) – Points to parametrize
Returns:Centripetal parametrization of the data
Return type:np.ndarray
splinelib.fit.cord_length(data)[source]

Returns a column vector with parametrization for the data using the cord length parametrization scheme.

Parameters:data (np.ndarray) – Points to parametrize
Returns:Cord length parametrization of the data
Return type:np.ndarray
splinelib.fit.fit_curve(data, knots, degree, method=<function least_squares>, parametrization=<function cord_length>)[source]

Fits a parametric curve of arbitrary dimension D from m given data points

Parameters:
  • data (np.ndarray) – Data to fit curve from. An (m, D) matrix.
  • knots (np.ndarray) – Knot vector.
  • degree (int) – Degree of desired spline.
  • method (callable) –

    Optional. Method for approximation. Available options are

    • splinelib.fit.least_squares
  • parametrization (callable) –

    Optional. Method for parametrization. Available options are

    • splinelib.fit.uniform
    • splinelib.fit.cord_length
    • splinelib.fit.centripetal

    Default is cord_length.

Returns:

A spline curve fitted to the data.

Return type:

Spline

splinelib.fit.fit_surface(data, knots_u, knots_v, degree, method=<function least_squares_3d>, parametrization=<function cord_length>, par_u=None, par_v=None)[source]

Fit a parametric spline surface to given data of arbitrary dimension D.

Parameters:
  • data (np.ndarray) – A data matrix with shape (n1, n2, D), where n1 and n2 are the number of points in each parameter direction, and D is the dimension of space (typically 3).
  • knots_u (np.ndarray) – Knot vector in u direction.
  • knots_v (np.ndarray) – Knot vecotr in v direction.
  • degree (int) – Degree of splines.
  • method (callable) –
    Approximation method. Available options are:
    • spinelib.fit.least_squares_3d

    Optional. Default is least_squares_3d.

  • parametrization (callable) –
    Scheme for generation of parametrization. Options are:
    • splinelib.fit.uniform
    • splinelib.fit.cord_length
    • splinelib.fit.centripetal

    Optional. Default is cord_length.

  • par_u (np.ndarray) – Override parametrization in u direction (this ignores whatever that is passed as ‘parametrization’).
  • par_v (np.ndarray) – Override parametrization in v direction (this ignores whatever that is passed as ‘parametrization’).
Returns:

A parametrix surface fitted to the data.

Return type:

SplineSurface

splinelib.fit.generate_uniform_knots(data, degree, length=20, regular=True)[source]

Generates a d+1-extended knot vector for the data, with knots uniformly distributed.

Parameters:
  • data (np.ndarray) – Data to fit knots from (parametrization). Used to find min and max value needed.
  • degree (int) – Degree of spline to fit on knot vector.
  • length (int) – Optional. Length of created knot vector. Default is 20.
  • regular (bool) – Optional. Make vector d+1-regular in addition to d+1-extended. Default is True
Returns:

A knot vector for the data with knots uniformly distributed.

Return type:

np.ndarray

splinelib.fit.least_squares(data, knots, degree, weights=None)[source]

Fit a non-parametric spline function to the data using least squares optimization

Parameters:
  • data (np.ndarray) – Data to fit from, with dimensions (m, 2). First column is x direction, and second column is y direction.
  • knots (np.ndarray) – Knot vector to fit spline onto
  • degree (int) – Desired degree of spline
  • weights (np.ndarray) – Optional. Associated weights to data, assumed to all be equal to 1 if omitted. Must be of the same dimensions as data.
Returns:

a spline function fitted to the data

Return type:

Spline

splinelib.fit.least_squares_3d(data_x, data_y, data_f, knots_x, knots_y, degree, weights_x=None, weights_y=None)[source]

Fit a non-parametric spline function in two variables to given data using least squares optimization.

Parameters:
  • data_x (np.ndarray) – Data points in x direction. Vector of size m1.
  • data_y (np.ndarray) – Data points in y direction. Vector of size m2.
  • data_f (np.ndarray) – Estimated function values for each x and y. Matrix of shape (m1, m2).
  • knots_x (np.ndarray) – Knot vector for x space.
  • knots_y (np.ndarray) – Knot vector for y space.
  • degree (int) – Degree of spline surface.
  • weights_x (np.ndarray) – Weight vector for x direction (of length m1). Optional, defaults to 1s if omitted.
  • weights_y (np.ndarray) – Weight vector for y direction (of length m2). Optional, defaults to 1s if omitted.
Returns:

A spline surface fitted to the data.

Return type:

SplineSurface

splinelib.fit.sample(spline, num=20, min=None, max=None)[source]

Samples the spline uniformly, and returns points.

Parameters:
  • spline (Spline) – Spline to sample
  • num (int) – Number of points to sample. Optional, defaults to 20.
  • min (int) – Lower bound of sampling range. Optional, defaults to lower bound of the support for the spline if omitted.
  • max (int) – Upper bound of sampling range. Optional, defaults to upper bound of the support for the spline if omitted.

Returns (np.ndarray): Sampled points

splinelib.fit.uniform(data)[source]

Returns a column vector with parametrization for the data using the uniform parametrization scheme.

Parameters:data (np.ndarray) – Points to parametrize
Returns:Uniform parametrization of the data
Return type:np.ndarray

splinelib.plotting module

splinelib.plotting.add_spline_to_plot(spline, include_control_polygon=True, include_knots=True)[source]

Adds given spline to current matplotlib plot

Parameters:
  • spline (Spline) – Spline to add
  • include_control_polygon (bool) – plot or not. Default is True.
  • include_knots (bool) – Default is True
Returns:

None

splinelib.splinelib module

class splinelib.splinelib.Spline(space: splinelib.splinelib.SplineSpace, coeffs)[source]

Bases: object

Class for representing a spline.

close()[source]

Forces the spline curve to close. This will change the above space.

Raises:ValueError – if spline is not a curve, but a function
evaluate(x)[source]

Evaluate a spline in the (given) point(s).

Parameters:

x – Point to evaluate spline in. If x is a float, it will be evaluated in the single point. If it is an array-like object, it will be evaulated in every point.

Returns:

The value of the spline in the given point

Return type:

float or np.ndarray

Raises:
  • ValueError – If x is outside the knot vector range
  • TypeError – If any arg is of the wrong type
evaluate_all()[source]

Evaluate a spline on entire knot vector

Returns:A tuple of np.ndarrays with arguments and values.
get_coeffs()[source]

Returns a copy of the coefficients to the spline that can safely be edited without accidentally modifying the spline.

Returns:coefficients of spline
Return type:np.ndarray
get_control_polygon()[source]

Get points for the control polygon of the spline. This will only work for functions or 2D curves.

Returns:x and y coordinates for control polygon
Return type:(np.ndarray, np.ndarray)
get_degree()[source]

Returns the degree of the spline

Returns:degree of spline
Return type:int
get_knots()[source]

Returns a copy of the knot vector for the spline that can safely be edited without accidentally modifying the spline.

Returns:knot vector for spline
Return type:np.ndarray
get_support()[source]

Returns the support of the spline

Returns:boundaries of support (minimum and maximum)
Return type:float, float
insert_knots(new_knots)[source]

Replace knot vector and recompute coefficients to fit these new knots.

Parameters:new_knots – New knot vector containing the old as a subset
Returns:None
is_parametric()[source]

Returns True if this object represents a parametric curve, False if it represents a function.

Returns:Whether the Spline object represents a curve or a function
Return type:bool
class splinelib.splinelib.SplineSpace(knots, degree)[source]

Bases: object

Class for representing a spline space

create_spline(coeffs)[source]

Creates a spline within this spline space with given coefficients

Parameters:

coeffs (np.ndarray) – Coefficient vector for the spline

Returns:

A Spline object, representing a spline inside the space

Raises:
  • ValueError – If the number of coefficients doesn’t match space dimension.
  • TypeError – If any arg is of the wrong type
evaluate_basis(x)[source]

Evaluates all the active basis splines on x

Parameters:

x (float) – Point to evaluate in

Returns:

The value of all active B-splines

Return type:

np.ndarray

Raises:
  • ValueError – If x is outside the knot vector range
  • TypeError – If any arg is of the wrong type
find_knot_index(x)[source]

Given a knot vector and a real number x with x in [t_1, t_{n+d+1} ), returns the index µ such that t_µ ≤ x < t_{µ+1}.

Parameters:

x (float) – Real value to find the knot interval of

Returns:

first index µ such that t_µ ≤ x < t_{µ+1}

Return type:

int

Raises:
  • ValueError – If x is outside the knot vector range
  • TypeError – If any arg is of the wrong type
get_degree()[source]

Returns the degree of the space

Returns:degree of spline
Return type:int
get_knots()[source]

Returns a copy of the knot vector for the space that can safely be edited without accidentally modifying the space.

Returns:knot vector for space
Return type:np.ndarray
get_support()[source]

Returns the largest possible support of all the splines in the space

Returns:boundaries of support (minimum and maximum)
Return type:float, float
class splinelib.splinelib.SplineSurface(space, coeffs)[source]

Bases: object

Class for representing an element in a tensor-product spline space

evaluate(u, v)[source]

Evaluate surface in given point

Parameters:
  • u (float) – Either x coordinate (if non-parametric) or parameter value in first dimension (if parametric)
  • v (float) – Either y coordinate (if non-parametric) or parameter value in second dimension (if parametric):
Returns:

Function value (if non-parametric) or point (if parametric) of surface at given (u, v).

Return type:

float or np.ndarray

evaluate_all(points_u=50, points_v=50)[source]

Evaluate surface on its support. Only supported for 3D surfaces.

Parameters:
  • points_u (int) – Number of points in u direction. Optional, default is 50.
  • points_v (int) – Number of points in v direction. Optional, default is 50.
Returns:

x, y and z coordinates for surface. If the spline is non-paramatric, it’s a triple of (x, y, z) where x and y are a meshgrid of parameters, and z is the corresponding function values. If the spline is parametric it’s a (points_u, points_v, 3)-shaped np.ndarray of points.

Return type:

triple of np.ndarray or np.ndarray

get_coeffs()[source]

Returns a copy of the coefficients to the spline that can safely be edited without accidentally modifying the spline.

Returns:coefficients of spline
Return type:np.ndarray
get_space()[source]

Get the space this surface is in.

Returns:The space of the surface.
Return type:TensorProductSplineSpace
is_parametric()[source]

Returns true if the object represents a parametric spline surface.

Returns:Whether spline is a parametric surface or not.
Return type:bool
class splinelib.splinelib.TensorProductSplineSpace(spaces)[source]

Bases: object

Class for representing a Tensor product space between two spline spaces.

create_spline(coeffs)[source]

Creates a spline surface within this spline space with given coefficients

Parameters:

coeffs (np.ndarray) – Coefficient matrix for the spline. If ndim is 2 the spline will be non-parametric, if ndim is 3, the spline will be parametric.

Returns:

A Spline object, representing a spline inside the space

Raises:
  • ValueError – If the number of coefficients doesn’t match space dimension.
  • TypeError – If any arg is of the wrong type
get_spaces()[source]

Returns all the spaces this space is a tensor product space of.

Returns:Each individual dimension in the tensor product spline space.
Return type:List of SplineSpace

Module contents