Git Product home page Git Product logo

bernstein's Introduction

bernstein: generic polynomials in Bernstein basis

Attention: This library is experimental and is based on the unstable generic_const_exprs feature that is only available in Nightly Rust.

This library implements basic operation with polynomials in the Bernstein basis in Rust, which can be used to create and manipulate generic Bézier curves. It is built on the following basic principles.

  • Being generic and work with various data types for both coefficient of the polynomials (Bézier control polygon), and curve parameterization.

  • Rely only on static dispatch and stack memory allocations with the polynomial degree (the size of the Bézier control polygon) being a compile time constant expression.

Examples

Quintic Pythagorean-hodograph cornering curve

The following example shows how to construct a 5th degree Pythagorean-hodograph curve complex representation using multiplication and integration in the Bernstein basis. For the entire source code of this example, see ./examples/pythagorean_hodograph.rs.

First, let put the following dependencies in the Cargo.toml.

[dependencies]
bernstein = {git = "https://github.com/sciprosk/bernstein.git"}
num = "0.4.3"

Specify that we want the unstable generic_const_exprs Rust feature, and add necessary imports. Our planar Pythagorean-hodograph curve has its control polygon in the complex plane, and is parameterized with f32.

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use bernstein::Bernstein;
use num::{complex::Complex32 as Complex, Zero};

Define curve parameters such as size, shape, orientation, and the initial point, and write down the control polygon of an internal cubic hodograph curve in the complex plane. For the details on the math formalism see Refs. [2, 3].

// Specify parameters of the curve
let d = 1.0;                             // cornering length -- distance
                                         // from entry/exit to the corner
let theta = 0.0;                         // orientation of the corner
let delta = std::f32::consts::FRAC_PI_2; // angle of the corner

// Actual formula that sets the internal representation
// of the quintic Pythgorean-hodograh curve.
let rho = f32::sqrt(30.0 * d * f32::cos(0.5 * delta) / (6.0 * f32::cos(0.5 * delta) + 1.0));

// Initial point of the curve in the complex plane.
let p0 = Complex::new(-1.0, 0.0);

// Control poligon for the internal representation of the curve
// in the complex plane.
let w0 = rho * Complex::exp(0.5 * theta * Complex::i());
let w1 = Complex::zero();
let w2 = rho * Complex::exp(0.5 * (theta + delta) * Complex::i());

The following creates a cubic curve in the complex plane (internal representation), that is used to generate the fifth order Pythagorean-hodograph curve by taking a square of the cubic curve (this sets the hodograph of the quintic curve), and then integrating from the starting point in the complex plane. The resulting curve has six points in its control polygon.

// Construct Bernstein polynomial in the complex plane from control polygon.
let w: Bernstein<Complex, f32, 3> = Bernstein::new([w0, w1, w2]);
// Take the square and integrate from the initial point.
let p = (w * w).integ(p0);

Now, we can use p.eval(u) to sample this curve at some values of the parameter u. For example, sampling using Plotters gives the following symmetric cornering curves for 90-degree and 60-degree corners. Note that for any symmetric quintic Pythagorean-hodograph curve, the control polygon has identical control points (P0, P1 = P2, P3 = P4, P5). pythgorean_hodograph1 pythgorean_hodograph

References

  1. L. Piegl & W. Tiller. "The NURBS book". Springer Science & Business Media (2012).
  2. R. T. Farouki, "Pythagorean-Hodograph Curves: Algebra and Geometry Inseparable", Geometry and Computing (Eds. H. Edelsbrunner, K. Polthier, and L Kobbelt) Springer (2008).
  3. R.T. Farouki, "Construction of G2 rounded corners with Pythagorean-hodograph curves", Computer Aided Geometric Design, 31(2) (2014).

bernstein's People

Contributors

sciprosk avatar

Stargazers

Pablo P. avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.