Files
j3ml/include/J3ML/Algorithm/Bezier.hpp
2024-12-11 01:34:38 -05:00

89 lines
4.5 KiB
C++

/// @file Bezier.hpp
/// @desc Cubic Bezier Curve impl. using De Casteljau's Method
/// @author Joshua O'Leary
/// @edited 20 June 2024
/// @version 2
/// @copyright (c) Redacted Software 2024
/// @license Unlicense - Public Domain
#pragma once
// Bezier Method:
// p = (1-t)^3 * P0 + 3*t*(1-t)^2*P1 + 3*t^2*(1-t)*P2 + t^3*P3;
// For cubics:
// p = (1-t)^2 * P0 + 2*(1-t)*t*P1 + t*t*P2;
// Transcribed from here: explicit form and derivative
// https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B%C3%A9zier_curves
#include <J3ML/LinearAlgebra/Vector2.hpp>
namespace J3ML::Algorithm
{
using namespace J3ML::LinearAlgebra;
template <typename T>
inline T Square(T f) { return f * f; }
template <typename T>
inline T Cube(T f) { return f * f * f; }
/// Four points P0, P1, P2, P3 in the plane space define a cubic Bezier curve.
/// The curve can be modeled as a polynomial of third order.
/// The curve starts at P0, going toward P1, and arrives at P3 coming from the direction of P2.
/// Usually, it will not pass through P1, or P2, these points are only there to provide directional information.
template <typename T>
inline T Bezier(float t, const T& p0, const T& p1, const T& p2, const T& p3)
{
return Cube(1 - t) * p0 + 3 * Square(1 - t) * t * p1 + 3 * (1 - t) * Square(t) * p2 + Cube(t) * p3;
}
/// Computes a point along a 2-dimensional Cubic Bezier curve.
/// @param t The normalized distance along the curve to compute, with range of [0, 1].
/// @param p0 The start-point of the curve.
/// @param p1
/// @param p2
/// @param p3 The end-point of the curve.
Vector2 Bezier(float t, const Vector2& p0, const Vector2& p1, const Vector2& p2, const Vector2& p3);
/// Computes a point along the tangent of a 2-dimensional Cubic Bezier Curve.
/// @param t The normalized distance along the curve to compute, with range of [0, 1].
/// @param p0 The start-point of the curve.
/// @param p1
/// @param p2
/// @param p3 The end-point of the curve.
Vector2 BezierDerivative(float t, const Vector2& p0, const Vector2& p1, const Vector2& p2, const Vector2& p3);
/// Computes a point along the normal of a 2-dimensional Cubic Bezier Curve.
/// @param t The normalized distance along the curve to compute, with range of [0, 1].
/// @param p0 The start-point of the curve.
/// @param p1 The first control point, which determines the direction at which the curve meets point 0.
/// @param p2 The second control point, which determines the direction at which the curve meets point 3.
/// @param p3 The end-point of the curve.
Vector2 BezierNormal(float t, const Vector2& p0, const Vector2& p1, const Vector2& p2, const Vector2& p3);
/// Computes a point along a 3-dimensional Cubic Bezier curve.
/// @param t The normalized distance along the curve to compute, with range of [0, 1].
/// @param p0 The start-point of the curve.
/// @param p1 The first control point, which determines the direction at which the curve meets point 0.
/// @param p2 The second control point, which determines the direction at which the curve meets point 3.
/// @param p3 The end-point of the curve.
Vector3 Bezier(float t, const Vector3& p0, const Vector3& p1, const Vector3& p2, const Vector3& p3);
/// Computes a point along the tangent of a 3-dimensional Cubic Bezier Curve.
/// @param t The normalized distance along the curve to compute, with range of [0, 1].
/// @param p0 The start-point of the curve.
/// @param p1 The first control point, which determines the direction at which the curve meets point 0.
/// @param p2 The second control point, which determines the direction at which the curve meets point 3.
/// @param p3 The end-point of the curve.
Vector3 BezierDerivative(float t, const Vector3& p0, const Vector3& p1, const Vector3& p2, const Vector3& p3);
/// Computes a point along the normal of a 3-dimensional Cubic Bezier Curve.
/// @param t The normalized distance along the curve to compute, with range of [0, 1].
/// @param p0 The start-point of the curve.
/// @param p1 The first control point, which determines the direction at which the curve meets point 0.
/// @param p2 The second control point, which determines the direction at which the curve meets point 3.
/// @param p3 The end-point of the curve.
Vector3 BezierNormal(float t, const Vector3& p0, const Vector3& p1, const Vector3& p2, const Vector3& p3);
}