Skip to content

API Reference

Catmull-Rom spline based on splines library.

CatmullRom #

CatmullRom(
    vertices,
    grid=None,
    alpha=None,
    bc_type="natural",
    gaussian_sigma=None,
)

Catmull-Rom spline generator.

Parameters:

  • vertices (array_like) –

    Sequence of (x, y) or (x, y, z) vertices.

  • grid (array_like, default: None ) –

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

  • alpha (float, default: None ) –

    Catmull-Rom parameter. If specified, grid is ignored.

  • bc_type (('closed', 'natural', 'clamped'), default: "closed" ) –

    Start/end conditions. If "closed", the first vertex is reused as last vertex and an additional grid value has to be specified. If "clamped", endpoint tangents are set to ensure the spline passes through the start and end points without deviation.

  • gaussian_sigma (float, default: None ) –

    Standard deviation for Gaussian kernel. If specified, applies Gaussian smoothing to the vertices before fitting the curve. Default is None (no smoothing).

Examples:

>>> verts = [(0., 0.), (1., 1.), (2., 0.), (3., 1.)]
>>> s = CatmullRom(verts, alpha=0.5, bc_type="clamped")
>>> grid = np.linspace(0, s.grid[-1], 5)
>>> s.evaluate(grid)
array([[0.        , 0.        ],
       [0.78125   , 0.9375    ],
       [1.5625    , 0.375     ],
       [2.34375   , 0.21875   ],
       [3.        , 1.        ]])
References

[1] E. Catmull and R. Rom, "A Class of Local Interpolating Splines," in Computer Aided Geometric Design, 1974. DOI: 10.1016/B978-0-12-079050-0.50020-5

evaluate #

evaluate(distances, n=0)

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

Parameters:

  • distances (array_like) –

    Distance(s) along the curve to evaluate.

  • n (int, default: 0 ) –

    Order of derivative, by default 0, i.e., the value itself.

Returns:

  • array_like

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

uniform_distances #

uniform_distances(
    n_pts, tolerance=1e-06, max_iterations=100
)

Get uniformly spaced parameter values.

Parameters:

  • n_pts (int) –

    Number of points to generate.

  • tolerance (float, default: 1e-06 ) –

    Tolerance for uniform spacing, by default 1e-6.

  • max_iterations (int, default: 100 ) –

    Maximum number of iterations for uniform spacing, by default 100.

Returns:

  • array_like

    Uniformly spaced parameter values.

smooth_linestrings #

smooth_linestrings(
    lines: LineString,
    distances: float | None = None,
    n_pts: int | None = None,
    gaussian_sigmas: float | None = None,
    bc_types: BCType | None = None,
    tolerance: float = 1e-06,
    max_iterations: int = 100,
) -> LineString
smooth_linestrings(
    lines: list[LineString],
    distances: list[float] | None = None,
    n_pts: list[int] | None = None,
    gaussian_sigmas: list[float] | None = None,
    bc_types: list[BCType] | None = None,
    tolerance: float = 1e-06,
    max_iterations: int = 100,
) -> list[LineString]
smooth_linestrings(
    lines,
    distances=None,
    n_pts=None,
    gaussian_sigmas=None,
    bc_types=None,
    tolerance=1e-06,
    max_iterations=100,
)

Smooth LineStrings using Centripetal Catmull-Rom splines and uniform spacing.

Parameters:

  • lines (list of shapely.LineString) –

    LineStrings to be smoothed.

  • distances (list of float, default: None ) –

    Distance between two consecutive points, by default None. You must specify either distances or n_pts.

  • n_pts (list of int, default: None ) –

    Number of points to be generated, by default None. You must specify either distances or n_pts.

  • gaussian_sigmas (list of float, default: None ) –

    Standard deviation for Gaussian kernel, by default None, i.e., no smoothing.

  • bc_types (list of {"closed", "natural", "clamped"}, default: None ) –

    Start/end conditions for each LineString, defaults to clamped. If closed, the first vertex is reused as last vertex and an additional distances value has to be specified. If clamped, endpoint tangents are set to ensure the spline passes through the start and end points without deviation.

  • tolerance (float, default: 1e-06 ) –

    Tolerance for uniform spacing, by default 1e-6.

  • max_iterations (int, default: 100 ) –

    Maximum number of iterations for uniform spacing, by default 100.

Returns:

  • shapely.LineString or numpy.ndarray of shapely.LineString

    Fitted CatmullRom curve as either a LineString.

linestrings_tangent_angles #

linestrings_tangent_angles(
    lines: list[LineString],
    gaussian_sigmas: list[float] | None = None,
) -> list[FloatArray]
linestrings_tangent_angles(
    lines: LineString, gaussian_sigmas: float | None = None
) -> FloatArray
linestrings_tangent_angles(lines, gaussian_sigmas=None)

Compute tangent angles for a line.

Parameters:

  • lines (list of shapely.LineString) –

    LineStrings to be smoothed.

  • gaussian_sigmas (list of float, default: None ) –

    Standard deviation for Gaussian kernel, by default None, i.e., no smoothing.

Returns:

  • np.ndarray or list of np.ndarray

    Signed tangent angles in radians.

smooth_polygon #

smooth_polygon(
    polygon,
    distance=None,
    n_pts=None,
    gaussian_sigma=None,
    tolerance=1e-06,
    max_iterations=100,
)

Smooth a (Multi)Polygon using Centripetal Catmull-Rom splines with uniform spacing.

Parameters:

  • polygon (Polygon or MultiPolygon) –

    (Multi)Polygon to be smoothed.

  • distance (float, default: None ) –

    Distance between two consecutive points, by default None. You must specify either distance or n_pts.

  • n_pts (int, default: None ) –

    Number of points to be generated, by default None. You must specify either distance or n_pts.

  • gaussian_sigma (float, default: None ) –

    Standard deviation for Gaussian kernel, by default None, i.e., no smoothing.

  • tolerance (float, default: 1e-06 ) –

    Tolerance for uniform spacing, by default 1e-6.

  • max_iterations (int, default: 100 ) –

    Maximum number of iterations for uniform spacing, by default 100.

Returns:

  • Polygon or MultiPolygon

    Smoothed (Multi)Polygon.