Factories

Curves

Handy utilities for creating curves.

class splipy.curve_factory.Boundary[source]

Enumeration representing different boundary conditions used in interpolate().

FREE = 1

The curve will be smooth at the second and second-to-last unique knot.

HERMITE = 3

Specify the derivatives at the knots.

NATURAL = 2

The curve will have zero second derivatives at the endpoints.

PERIODIC = 4

The curve will be periodic at the endpoints.

TANGENT = 5

Specify the tangents at the endpoints.

TANGENTNATURAL = 6

Use TANGENT for the start and NATURAL for the end.

splipy.curve_factory.line(a, b, relative=False)[source]

Create a line between two points.

Parameters:
  • a (point-like) – Start point
  • b (point-like) – End point
  • relative (bool) – Whether b is relative to a
Returns:

Linear curve from a to b

Return type:

Curve

splipy.curve_factory.polygon(points...)[source]

Create a linear interpolation between input points.

Parameters:
  • points ([point-like]) – The points to interpolate
  • relative (bool) – If controlpoints are interpreted as relative to the previous one
Returns:

Linear curve through the input points

Return type:

Curve

splipy.curve_factory.n_gon([n=5][, r=1])[source]

Create a regular polygon of n equal sides centered at the origin.

Parameters:
  • n (int) – Number of sides and vertices
  • r (float) – Radius
  • center (point-like) – local origin
  • normal (vector-like) – local normal
Returns:

A linear, periodic, 2D curve

Return type:

Curve

Raises:
  • ValueError – If radius is not positive
  • ValueError – If n < 3
splipy.curve_factory.circle([r=1])[source]

Create a circle.

Parameters:
  • r (float) – Radius
  • center (point-like) – local origin
  • normal (vector-like) – local normal
  • type (string) – The type of parametrization (‘p2C0’ or ‘p4C1’)
Returns:

A periodic, quadratic rational curve

Return type:

Curve

Raises:

ValueError – If radius is not positive

splipy.curve_factory.circle_segment(theta[, r=1])[source]

Create a circle segment starting paralell to the rotated x-axis.

Parameters:
  • theta (float) – Angle in radians
  • r (float) – Radius
Returns:

A quadratic rational curve

Return type:

Curve

Raises:
  • ValueError – If radiusis not positive
  • ValueError – If theta is not in the range [-2pi, 2pi]
splipy.curve_factory.interpolate(x, basis[, t=None])[source]

Perform general spline interpolation on a provided basis.

Parameters:
  • x (matrix-like) – Matrix X[i,j] of interpolation points xi with components j
  • basis (BSplineBasis) – Basis on which to interpolate
  • t (array-like) – parametric values at interpolation points; defaults to Greville points if not provided
Returns:

Interpolated curve

Return type:

Curve

splipy.curve_factory.least_square_fit(x, basis, t)[source]

Perform a least-square fit of a point cloud onto a spline basis

Parameters:
  • x (matrix-like) – Matrix X[i,j] of interpolation points xi with components j. The number of points must be equal to or larger than the number of basis functions in basis
  • basis (BSplineBasis) – Basis on which to interpolate
  • t (array-like) – parametric values at evaluation points
Returns:

Approximated curve

Return type:

Curve

splipy.curve_factory.cubic_curve(x[, boundary=Boundary.FREE][, t=None][, tangents=None])[source]

Perform cubic spline interpolation on a provided basis.

The valid boundary conditions are enumerated in Boundary. The meaning of the tangents parameter depends on the specified boundary condition:

  • TANGENT: two points,
  • TANGENTNATURAL: one point,
  • HERMITE: n points
Parameters:
  • x (matrix-like) – Matrix X[i,j] of interpolation points x_i with components j
  • boundary (int) – Any value from Boundary.
  • t (array-like) – parametric values at interpolation points, defaults to Euclidean distance between evaluation points
  • tangents (matrix-like) – Tangent information according to the boundary conditions.
Returns:

Interpolated curve

Return type:

Curve

splipy.curve_factory.bezier(pts, quadratic=False, relative=False)[source]

Generate a cubic or quadratic bezier curve from a set of control points

Parameters:
  • pts ([array-like]) – list of control-points. In addition to a starting point we need three points per bezier interval for cubic splines and two points for quadratic splines
  • quadratic (bool) – True if a quadratic spline is to be returned, False if a cubic spline is to be returned
  • relative (bool) – If controlpoints are interpreted as relative to the previous one
Returns:

Bezier curve

Return type:

Curve

splipy.curve_factory.manipulate(crv, f, normalized=False, vectorized=False)[source]

Create a new curve based on an expression-evaluation of an existing one :param function f: expression of the physical point x, the velocity

(tangent) v, parametric point t and/or acceleration a.
Parameters:
  • normalized – If velocity and acceleration terms should be normalized to have length 1
  • vectorized – True if f is expressed in terms of vectorized operations. The method is sped up whenever this is used.

Examples:

def scale_by_two(x):
    return 2*x

new_curve = manipulate(old_curve, scale_by_two) 
new_curve = old_curve * 2 # will give the same result

def move_3_to_right(x, v):
    result = x
    # *v* is velocity. Rotate this 90 to the right and it points (+v[1], -v[0])
    result[0] += 3*v[1]
    result[1] -= 3*v[0]
    return result

# note that the velocity vector will not have length one unless normalized is passed
new_curve = manipulate(old_curve, move_3_to_right, normalized=True)

def move_3_to_right_fast(x, v):
    result = x
    result[:,0] += 3*v[:,1]
    result[:,1] -= 3*v[:,0]
    return result

new_curve = manipulate(old_curve, move_3_to_right_fast, normalized=True, vectorized=True)
splipy.curve_factory.fit(x, t0, t1, rtol=0.0001, atol=0.0)[source]

Computes an interpolation for a parametric curve up to a specified tolerance. The method will iteratively refine parts where needed resulting in a non-uniform knot vector with as optimized knot locations as possible.

Parameters:
  • x (function) – callable function which takes as input a vector of evaluation points t and gives as output a matrix x where x[i,j] is component j evaluated at point t[i]
  • t0 (float) – start of parametric domain
  • t1 (float) – end of parametric domain
  • rtol (float) – relative tolerance for stopping criterium. It is defined to be ||e||_L2 / D, where D is the length of the curve and ||e||_L2 is the L2-error (see Curve.error)
  • atol (float) – absolute tolerance for stopping criterium. It is defined to be the maximal distance between the curve approximation and the exact curve
Returns:

Curve Non-uniform cubic B-spline curve

Examples:

import numpy as np

# gives a B-spline approximation to the circle with arclength parametrization;
# unlike curve_factory.circle which is exact, but not arclength
def arclength_circle(t):
    return np.array( [np.cos(t), np.sin(t)] ).T
crv = curve_factory.fit(arclength_circle, 0, 2*np.pi)
print crv

# approximates a difficult function with wild behaviour around t=0, but
# this is overcome by a higher knot density around this point
def one_over_t(t):
    t = np.array(t)
    eps = 1e-8 # to avoid 1/0 we add a small epsilon
    return np.array( [t, 1.0/(t+eps)] ).T
crv = curve_factory.fit(one_over_t, 0, 1, rtol=1e-6)
print crv # first knot span is ~1e-9, last knot is ~1e-1

# one can specify the target curve in terms of existing Curve objects
crv = curve_factory.circle(r=1)     # Curve-object, quadratic NURBS
def move_along_tangent(t):
    return crv(t) + crv.tangent(t)  # can evaluate curve or its derivatives
# fit() will create a B-spline approximation using non-uniform refinement
crv2 = curve_factory.fit(move_along_tangent, crv.start(0), crv.end(0))

Surfaces

Handy utilities for creating surfaces.

splipy.surface_factory.square([size=1])[source]

Create a square with parametric origin at (0,0).

Parameters:size (float or (float)) – Size(s), either a single scalar or a tuple of scalars per axis
Returns:A linear parametrized square
Return type:Surface
splipy.surface_factory.disc([r=1][, type='radial'])[source]

Create a circular disc. The type parameter distinguishes between different parametrizations.

Parameters:
  • r (float) – Radius
  • type (string) – The type of parametrization (‘radial’ or ‘square’)
Returns:

The disc

Return type:

Surface

splipy.surface_factory.sphere([r=1])[source]

Create a spherical shell.

Parameters:
  • r (float) – Radius
  • center (point-like) – Local origin of the sphere
Returns:

The spherical shell

Return type:

Surface

splipy.surface_factory.extrude(curve, amount)[source]

Extrude a curve by sweeping it to a given height.

Parameters:
  • curve (Curve) – Curve to extrude
  • amount (vector-like) – 3-component vector of sweeping amount and direction
Returns:

The extruded curve

Return type:

Surface

splipy.surface_factory.revolve(curve, [theta=2pi], [axis=[0,0,1]])[source]

Revolve a surface by sweeping a curve in a rotational fashion around the z axis.

Parameters:
  • curve (Curve) – Curve to revolve
  • theta (float) – Angle to revolve, in radians
  • axis (vector-like) – Axis of rotation
Returns:

The revolved surface

Return type:

Surface

splipy.surface_factory.cylinder([r=1][, h=1][, center=(0, 0, 0)][, axis=(0, 0, 1)])[source]

Create a cylinder shell with no top or bottom

Parameters:
  • r (float) – Radius
  • h (float) – Height
  • center (point-like) – The center of the bottom circle
  • axis (vector-like) – Cylinder axis
Returns:

The cylinder shell

Return type:

Surface

splipy.surface_factory.torus([minor_r=1][, major_r=3])[source]

Create a torus (doughnut) by revolving a circle of size minor_r around the z axis with radius major_r.

Parameters:
  • minor_r (float) – The thickness of the torus (radius in the xz plane)
  • major_r (float) – The size of the torus (radius in the xy plane)
  • center (point-like) – Local origin of the torus
Returns:

A periodic torus

Return type:

Surface

splipy.surface_factory.edge_curves(curves...)[source]

Create the surface defined by the region between the input curves.

In case of four input curves, these must be given in an ordered directional closed loop around the resulting surface.

Parameters:curves ([Curve]) – Two or four edge curves
Returns:The enclosed surface
Return type:Surface
Raises:ValueError – If the length of curves is not two or four
splipy.surface_factory.thicken(curve, amount)[source]

Generate a surface by adding thickness to a curve.

  • For 2D curves this will generate a 2D planar surface with the curve through the center.
  • For 3D curves this will generate a surface “tube” which is open at both ends (that is, for a straight line it will generate a cylinder shell).

The resulting surface is an approximation generated by interpolating at the Greville points. It will use the same discretization as the input curve. It does not check for self-intersecting geometries.

Parameters:
  • curve (Curve) – The generating curve
  • amount – The amount of thickness, either constant or variable (if variable, the function must accept parameters named x, y, z and/or t)
Returns:

Surrounding surface

Return type:

Surface

splipy.surface_factory.sweep(path, shape)[source]

Generate a surface by sweeping a shape along a path

The resulting surface is an approximation generated by interpolating at the Greville points. It is generated by sweeping a shape curve along a path.

The shape object has to be contained in the ‘xy’ plane (preferably centered around the origin) as its x-coordinate is extruded in the normal direction, and its y-coordinate in the binormal direction of the path curve.

Parameters:
  • path (Curve) – The path to drag shape along
  • shape (Curve) – The shape to be dragged out to a surface
Returns:

Surrounding surface

Return type:

Surface

splipy.surface_factory.interpolate(x, bases[, u=None])[source]

Interpolate a surface on a set of regular gridded interpolation points x.

The points can be either a matrix (in which case the first index is interpreted as a flat row-first index of the interpolation grid) or a 3D tensor. In both cases the last index is the physical coordinates.

Parameters:
  • x (matrix-like or 3D-tensor-like) – Grid of interpolation points
  • bases ([BSplineBasis]) – The basis to interpolate on
  • u ([array-like]) – Parametric interpolation points, defaults to Greville points of the basis
Returns:

Interpolated surface

Return type:

Surface

splipy.surface_factory.least_square_fit(x, bases, u)[source]

Perform a least-square fit of a point cloud x onto a spline basis.

The points can be either a matrix (in which case the first index is interpreted as a flat row-first index of the interpolation grid) or a 3D tensor. In both cases the last index is the physical coordinates.

There must be at least as many points as basis functions.

Parameters:
  • x (matrix-like or 3D-tensor-like) – Grid of evaluation points
  • bases ([BSplineBasis]) – Basis on which to interpolate
  • u ([array-like]) – Parametric values at evaluation points
Returns:

Approximated surface

Return type:

Surface

splipy.surface_factory.teapot()[source]

Generate the Utah teapot as 32 cubic bezier patches. This teapot has a rim, but no bottom. It is also self-intersecting making it unsuitable for perfect-match multipatch modeling.

The data is picked from http://www.holmes3d.net/graphics/teapot/

Returns:The utah teapot
Return type:List of Surface

Volumes

Handy utilities for creating volumes.

splipy.volume_factory.cube([size=1])[source]

Create a cube with parmetric origin at (0,0,0).

Parameters:size (float or (float)) – Size(s), either a single scalar or a tuple of scalars per axis
Returns:A linear parametrized box
Return type:Volume
splipy.volume_factory.sphere([r=1], [center=(0,0,0)], type=[radial])[source]

Create a solid sphere

Parameters:
  • r (float) – Radius
  • center (point-like) – Local origin of the sphere
  • type (string) – The type of parametrization (‘radial’ or ‘square’)
Returns:

A solid ball

Return type:

Volume

splipy.volume_factory.revolve(surf[, theta=2pi])[source]

Revolve a volume by sweeping a surface in a rotational fashion around the z axis.

Parameters:
  • surf (Surface) – Surface to revolve
  • theta (float) – Angle to revolve, in radians
Returns:

The revolved surface

Return type:

Volume

splipy.volume_factory.cylinder([r=1][, h=1][, center=(0, 0, 0)][, axis=(0, 0, 1)])[source]

Create a solid cylinder

Parameters:
  • r (float) – Radius
  • h (float) – Height
  • center (point-like) – The center of the bottom circle
  • axis (vector-like) – Cylinder axis
Returns:

The cylinder

Return type:

Volume

splipy.volume_factory.extrude(surf, amount)[source]

Extrude a surface by sweeping it to a given height.

Parameters:
  • surf (Surface) – Surface to extrude
  • amount (vector-like) – 3-component vector of sweeping amount and direction
Returns:

The extruded surface

Return type:

Volume

splipy.volume_factory.edge_surfaces(surfaces...)[source]

Create the volume defined by the region between the input surfaces.

In case of six input surfaces, these must be given in the order: bottom, top, left, right, back, front. Opposing sides must be parametrized in the same directions.

Parameters:surfaces ([Surface]) – Two or six edge surfaces
Returns:The enclosed volume
Return type:Volume
Raises:ValueError – If the length of surfaces is not two or six
splipy.volume_factory.interpolate(x, bases[, u=None])[source]

Interpolate a volume on a set of regular gridded interpolation points x.

The points can be either a matrix (in which case the first index is interpreted as a flat row-first index of the interpolation grid) or a 4D tensor. In both cases the last index is the physical coordinates.

Parameters:
  • x (matrix-like or 4D-tensor-like) – Grid of interpolation points
  • bases ([BSplineBasis]) – The basis to interpolate on
  • u ([array-like]) – Parametric interpolation points, defaults to Greville points of the basis
Returns:

Interpolated volume

Return type:

Volume

splipy.volume_factory.least_square_fit(x, bases, u)[source]

Perform a least-square fit of a point cloud x onto a spline basis.

The points can be either a matrix (in which case the first index is interpreted as a flat row-first index of the interpolation grid) or a 4D tensor. In both cases the last index is the physical coordinates.

There must be at least as many points as basis functions.

Parameters:
  • x (matrix-like or 4D-tensor-like) – Grid of evaluation points
  • bases ([BSplineBasis]) – Basis on which to interpolate
  • u ([array-like]) – Parametric values at evaluation points
Returns:

Approximated volume

Return type:

Volume