A Python implementation for rendering smooth, differentiable splines using PyTorch, designed for use in machine learning and generative art tasks. This repository demonstrates the creation of quadratic Bézier curves and their visualization on a canvas with customizable colors and brush sizes.
- Smooth spline generation: Uses quadratic Bézier interpolation to produce smooth curves.
- Customizable rendering: Specify brush sizes and colors for spline rendering.
- Differentiability: Built with PyTorch, enabling gradient-based optimization.
- Visualization: Render and display the resulting canvas with
matplotlib.
git clone https://github.com/your-username/differentiable-spline-renderer.git
cd differentiable-spline-rendererEnsure you have PyTorch and Matplotlib installed in your environment.
The spline requires exactly three control points for quadratic Bézier interpolation. Example:
import torch
from differentiable_spline import DifferentiableSpline
control_points = torch.tensor([
[0.2, 0.3],
[0.5, 0.8],
[0.8, 0.4]
], dtype=torch.float32)
spline = DifferentiableSpline(control_points, canvas_size=(128, 128), device='cpu')Specify colors and brush sizes, then generate the canvas:
colors = torch.tensor([
[1.0, 0.0, 0.0], # Red
[0.0, 1.0, 0.0], # Green
[0.0, 0.0, 1.0] # Blue
])
brush_sizes = torch.tensor([10.0]) # Single brush size for all strokes
canvas = spline.generate_spline_canvas(colors, brush_sizes, num_points=300)
spline.show_canvas(canvas)DifferentiableSpline(control_points, canvas_size=(128, 128), device='cpu')- control_points:
torch.Tensorof shape(3, 2). Control points for the quadratic Bézier curve. - canvas_size: Tuple of integers specifying the canvas dimensions.
- device: PyTorch device (
'cpu'or'cuda').
interpolate_points(num_points=100): Interpolatesnum_pointsalong the Bézier curve.generate_spline_canvas(colors, brush_sizes, num_points=300, test_mode=False): Renders the spline on a canvas.show_canvas(canvas): Visualizes the generated canvas usingmatplotlib.
Below is a generated spline rendered with smooth blending:
Feel free to submit issues or pull requests to enhance functionality or fix bugs.
This project is licensed under the MIT License.
