Skip to content

A simple Python tool for visualizing and interactively editing B-spline curves.

License

Notifications You must be signed in to change notification settings

Liam-Xander/Simple-BSpline-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

B-spline Curves in Python

Python Version License

This is a B-spline curve plotting and demonstration program implemented in Python. It utilizes the numpy and matplotlib libraries to provide both static plotting and dynamic, interactive demonstrations.

Demonstration

Features

  • Object-Oriented Design: The code is encapsulated in a BSplineCurve class, making it clean, understandable, and easy to extend.
  • Three Types of Node Vectors:
    • Uniform B-spline: Nodes are evenly spaced on the parameter axis. The curve generally does not pass through the start and end control points.
    • Quasi-uniform B-spline: The first and last nodes have a multiplicity of k+1, ensuring the curve is "clamped" to the start and end control points.
    • Piecewise Bézier Curve: By setting specific node multiplicities, the B-spline curve degenerates into a series of connected Bézier curve segments.
  • Dual Mode: Static and Interactive:
    • Static Plotting: Directly run a script to generate images of the three B-spline curve types.
    • Interactive Demonstration: Provides an interactive interface allowing users to modify and observe B-spline curves in real-time by dragging control points and pressing keys.

Dependencies

  1. Clone or download this repository.

  2. Install the required dependencies. It is recommended to create a requirements.txt file with the following content:

    numpy
    matplotlib
    
  3. Install via pip:

    pip install -r requirements.txt

Usage

This project includes two usage modes: static plotting (Bsplne.py) and an interactive demonstration (Bsplne_animation.py).

1. Static Plotting (Bsplne.py)

This script is used to generate and display three types of B-spline curves with fixed control points.

Run Directly

python Bsplne.py

As a Module

You can also import the BSplineCurve class into your own project.

import numpy as np
from Bsplne import BSplineCurve

# Define control points
control_points = np.array(...)
# Create a B-spline curve object
bspline = BSplineCurve(control_points, degree=2)
# Call methods to plot
bspline.draw_uniform_bspline()

2. Interactive Demonstration (Bsplne_animation.py)

This script provides a real-time interactive window for exploring how changes to control points and curve types affect the shape of a B-spline curve.

Run the Demo

python Bsplne_animation.py

Interaction Guide

  • Drag Control Points: Use the left mouse button to drag the green control points, and the curve will update in real-time.
  • Switch Curve Types: With the window active, press the number keys 1, 2, or 3 to switch.
    • 1: Uniform B-spline
    • 2: Quasi-uniform B-spline (Default)
    • 3: Piecewise Bézier (Only available if n % k == 0)

Mathematical Principles

B-spline Curve Formula

A B-spline curve is defined by its control points $P_i$ and basis functions $N_{i,k}(u)$: $$P(u) = \sum_{i=0}^{n} P_i N_{i,k}(u)$$

B-spline Basis Functions

The basis functions $N_{i,k}(u)$ are defined by the de Boor-Cox recursion formula. Here, $k$ is the degree of the basis function, and $u$ is a parameter from the node vector $U = {u_0, u_1, ..., u_{n+k+1}}$.

$$ N_{i,0}(u) = \begin{cases} 1, & \text{if } u_i \le u < u_{i+1} \\ 0, & \text{otherwise} \end{cases} $$$$N_{i,k}(u) = \frac{u - u_i}{u_{i+k} - u_i} N_{i,k-1}(u) + \frac{u_{i+k+1} - u}{u_{i+k+1} - u_{i+1}} N_{i+1,k-1}(u)$$

By convention, 0/0 = 0.

Notes

  1. Parameter Requirement: The number of control points (n+1) must be greater than or equal to the degree of the B-spline (k), i.e., n >= k.
  2. Piecewise Bézier Condition: To generate a piecewise Bézier curve, the number of control points minus one (n) must be an integer multiple of the degree (k), i.e., n % k == 0, and k >= 1.
  3. Performance: For high-degree B-splines (e.g., k > 5), the computational complexity increases due to deeper recursion, which may affect performance.

License

This project is licensed under the MIT License.

About

A simple Python tool for visualizing and interactively editing B-spline curves.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages