bezier Python Library

repository·main·Indexed 43 days ago

https://github.com/dhermes/bezier

A Python library providing high-performance support for Bézier curves, triangles, and higher-order geometric objects using the Bernstein basis. It includes a Fortran shared library (libbezier) and a Python binary extension for optimized computations, including curve-curve and triangle-triangle intersections.

Tokens
33.9K
Snippets
115
Records
144
Agent score
50%

What's inside bezier

  1. Overview of bezier library capabilities

    main

    The bezier library is a helper for working with Bézier geometric objects. It provides support for:

    • Bézier Curves: Parametric curves defined by the Bernstein basis.
    • Bézier Triangles: Higher-order geometric objects.

    Key features include efficient and numerically stable computation via de Casteljau's algorithm and support for convex optimization techniques (useful for curve-curve intersection).

  2. Overview of the bezier library capabilities

    main

    The bezier library is a Python helper designed for working with Bézier curves and triangles. It uses the Bernstein basis, which provides better numerical stability and intuitive geometric manipulation compared to the traditional power basis.

    Key capabilities include:

    • 2D Plotting: Visualizing curves and triangles.
    • Intersection: Finding intersections between 2D objects using both geometric and algebraic algorithms.
    • Subdivision: Splitting curves and triangles into smaller segments.
    • Degree Manipulation: Performing degree-elevation and degree-reduction.
    • Evaluation: Calculating points on a curve or triangle given a parameter.
    • Inverse Evaluation (Locating): Determining the parameter(s) corresponding to a specific point on a curve or triangle.
    • Reparameterization: Specialization and reparameterization of curves.
    • Singularity Checks: Checking for self-intersection or singularities in 2D triangles.
  3. Explore the bezier package submodules

    main

    The bezier package provides tools for working with various geometric shapes and curves. The core functionality is organized into the following submodules:

    • bezier.curve: Likely contains primary classes and functions for defining and manipulating Bezier curves.
    • bezier.curved_polygon: Tools for working with polygons composed of curved segments.
    • bezier.triangle: Specialized functionality for triangle-based geometric operations.

    For low-level or internal geometric primitives, refer to the bezier.hazmat subpackage.

  4. What is bezier and what does it support?

    main

    The bezier library is a helper for working with Bézier curves, triangles, and higher-order objects. It uses the Bernstein basis to define parametric curves and triangles.

    Key features include:

    • Support for Bézier Curves (bezier.curve).
    • Support for Bézier Triangles (bezier.triangle).
    • Efficient and numerically stable computation via de Casteljau's algorithm.
    • Convexity properties that allow for efficient intersection algorithms and convex optimization.
  5. Overview of the triangle module

    main

    The triangle module provides a collection of procedures for performing computations on Bézier triangles.

    When using the procedures in this module, you will frequently encounter two parameters:

    • N: The number of nodes.
    • d: The degree of the Bézier triangle.

    Note that these two values are mathematically related by the formula $N = \binom{d + 2}{2}$. While providing both is technically redundant, the module requires both as arguments to avoid unnecessary re-computation. Users are expected to provide both the degree and the node count directly.

  6. Follow docstring style guidelines

    main

    All public objects require docstrings. The project uses PEP257 and the Google style. Documentation is processed using the Napoleon Sphinx extension and sphinx-docstring-typing to support type annotations.

    Args:
        path (str): The path of the file to wrap
        field_storage (FileStorage): The :class:`FileStorage` instance to wrap
        temporary (bool): Whether or not to delete the file when the File
           instance is destructed
    
    Returns:
        BufferedFileStorage: A buffered writable file descriptor
  7. Handling tangency and coincident segments in intersections

    main

    The bezier library includes robust handling for several edge cases in curve intersection:

    • Points of Tangency: Intersections occurring at points of tangency (mathematically equivalent to repeated roots of a polynomial) are handled by switching to a robust Newton's method to resolve numerical issues caused by double roots.
    • Coincident Curve Segments: For segments that overlap, the library identifies the intersection points as the beginning and end of the common segment.

    Note that while the library makes an earnest effort to compute these, intersections at points of tangency are generally more numerically sensitive than transverse intersections.

  8. Use node count and degree in triangle_intersection procedures

    main

    When calling procedures in the triangle_intersection module, you are often required to provide both the number of nodes $N$ and the degree $d$ of the Bézier triangle.

    While these values are mathematically redundant because $N = \binom{d + 2}{2}$, providing both allows the module to avoid unnecessary re-computation. For optimal performance, callers should pass the known values for both $N$ and $d$ directly.

  9. Windows-specific build considerations for Bezier

    main

    Building bezier on Windows involves specific constraints:

    1. Compiler Compatibility: Python extensions must be built with the same version of MSVC used to build the official Python interpreter to ensure C runtime (CRT) compatibility.
    2. Fortran Support: Since MSVC does not provide a Fortran compiler, MinGW-w64 (which includes gfortran) is used.
    3. Memory Safety: While mixing MSVC and MinGW can be problematic due to different C runtimes (MSVCRT.dll vs ucrtbase.dll), bezier mitigates this by ensuring subroutines marked with bind(c) (the C ABI) do not use allocatable or deferred-shape output variables. This ensures memory allocated in Fortran is isolated within the Fortran code and not incorrectly freed by the Python process's CRT.
    4. Import Library: To enable building the Python binary extension, a corresponding import library (usr/lib/bezier.lib) is provided to specify the symbols in the DLL.
  10. How Curve Subdivision works

    main

    Curve Subdivision is the process used to narrow down the search for intersection points when bounding boxes overlap.

    1. Splitting: A curve $\mathcal{C} = b([0, 1])$ is split into two sub-curves by dividing the unit interval into $[0, \frac{1}{2}]$ and $[\frac{1}{2}, 1]$.
    2. Recursive Pruning: The algorithm recursively applies the bounding box predicate to these sub-curves to identify sub-intervals where an intersection is possible.
    3. Pruning with Convex Hulls: To prevent an explosion of candidate pairs in cases where curves are almost parallel, the algorithm uses a convex hull predicate to prune the number of subdivided pairs once a certain threshold is exceeded.