Bezier

The bezier header provides a templated implementation of cubic bezier curves and rational cubic bezier curves. Intended use for these templated functions is on raw floating point values or on the associated Vector2d class.

API Reference

Header File

Classes

template<typename T>
class Bezier

Implements rational / weighted and unweighted cubic bezier curves between control points.

Example

    using Bezier = espp::Bezier<espp::Vector2f>;
    std::array<espp::Vector2f, 4> control_points = {espp::Vector2f(6, 220), espp::Vector2f(62, 115),
                                                    espp::Vector2f(176, 151),
                                                    espp::Vector2f(217, 50)};
    Bezier bezier(Bezier::Config{.control_points = control_points});
    std::array<float, 4> weights = {0.5f, 2.0f, 0.5f, 2.0f};
    Bezier rational_bezier(
        Bezier::WeightedConfig{.control_points = control_points, .weights = weights});
    // NOTE: this is the built-in log format for pyqtgraph
    fmt::print("\"bezier_x\",\"bezier_y\",\"rational bezier_x\",\"rational bezier_y\"\n");
    float t = 0;
    while (t < 1.0f) {
      auto p0 = bezier(t);
      auto p1 = rational_bezier(t);
      fmt::print("{},{},{},{}\n", p0.x(), p0.y(), p1.x(), p1.y());
      t += 0.05f;
    }

Note

See https://pomax.github.io/bezierinfo/ for information on bezier curves.

Note

Template class which can be used individually on floating point values directly or on containers such as Vector2d<float>.

Note

The bezier curve is defined by 4 control points, P0, P1, P2, P3. The curve is defined by the equation: \(B(t) = (1-t)^3 * P0 + 3 * (1-t)^2 * t * P1 + 3 * (1-t) * t^2 * P2 + t^3 * P3\) where t is the evaluation parameter, [0, 1].

Note

The weighted bezier curve is defined by 4 control points, P0, P1, P2, P3 and 4 weights, W0, W1, W2, W3. The curve is defined by the equation: \(B(t) = (W0 * (1-t)^3 * P0 + W1 * 3 * (1-t)^2 * t * P1 + W2 * 3 * (1-t) * t^2 * P2 + W3 * t^3 * P3) / (W0 + W1 + W2 + W3)\) where t is the evaluation parameter, [0, 1].

Template Parameters

T – The type of the control points, e.g. float or Vector2d<float>.

Public Functions

inline explicit Bezier(const Config &config)

Construct an unweighted cubic bezier curve for evaluation.

Parameters

config – Unweighted Config structure containing the control points.

inline explicit Bezier(const WeightedConfig &config)

Construct a rational / weighted cubic bezier curve for evaluation.

Parameters

config – Rational / weighted WeightedConfig structure containing the control points and their weights.

inline T at(float t) const

Evaluate the bezier at t.

Parameters

t – The evaluation parameter, [0, 1].

Returns

The bezier evaluated at t.

inline T operator()(float t) const

Evaluate the bezier at t.

Note

Convienience wrapper around the at() method.

Parameters

t – The evaluation parameter, [0, 1].

Returns

The bezier evaluated at t.

struct Config

Unweighted cubic bezier configuration for 4 control points.

Public Members

std::array<T, 4> control_points

Array of 4 control points.

struct WeightedConfig

Weighted cubic bezier configuration for 4 control points with individual weights.

Public Members

std::array<T, 4> control_points

Array of 4 control points.

std::array<float, 4> weights = {1.0f, 1.0f, 1.0f, 1.0f}

Array of 4 weights, default is array of 1.0f.