23template<
typename T,
typename U>
struct Point
35template<
typename T,
typename U,
size_t maxSize = 24>
class LinearCurve
42 template<
size_t N>
LinearCurve(
const std::array<point_t, N>& points) : _count(0)
44 static_assert(N <= maxSize,
"Array size must be less than MaxSize");
46 assert(points.size() >= 2 &&
"Linear curve must have more than 1 points");
49 for (
size_t i = 0; i < N; ++i)
55 for (
size_t i = 0; i < N; ++i)
57 const auto& p = pts[i];
58 assert(not std::isnan(p.x) &&
"invalid value in curve parameters");
59 assert(std::isfinite(p.x) &&
"invalid value in curve parameters");
60 assert(not std::isnan(p.y) &&
"invalid value in curve parameters");
61 assert(std::isfinite(p.y) &&
"invalid value in curve parameters");
65 for (
size_t i = 1; i < N; ++i)
67 assert(pts[i - 1].x <= pts[i].x &&
"Points must be sorted by X coordinate in ascending order");
72 for (
size_t readIdx = 1; readIdx < N; ++readIdx)
74 if (pts[readIdx - 1].x != pts[readIdx].x || pts[readIdx - 1].y != pts[readIdx].y)
76 pts[writeIdx] = pts[readIdx];
82 assert(_count >= 2 &&
"Linear curve must have more than 1 unique points");
85 for (
size_t i = 1; i < N; ++i)
87 assert(pts[i - 1].x < pts[i].x &&
"Points must be sorted by X coordinate in ascending order");
100 if (x >= pts[_count - 1].x)
101 return pts[_count - 1].y;
103 for (
size_t i = 1; i < _count; ++i)
107 if (x >= lastPt.
x and x <= pt.
x)
109 return lmpd_map<U>(x, lastPt.
x, pt.
x, lastPt.
y, pt.
y);
121 std::array<point_t, maxSize> pts;
126template<
typename T,
typename U,
typename... Args>
127LinearCurve<T, U> make_linear_curve(
const Point<T, U>& firstPoint,
const Point<T, U>& secondPoint, Args&&... args)
129 constexpr size_t N = 2 +
sizeof...(Args);
130 std::array<Point<T, U>, N> pts = {{firstPoint, secondPoint, args...}};
131 return LinearCurve<T, U>(pts);
153 lowerBound(pointA.y),
156 const double div = pointA.
y /
static_cast<double>(pointB.
y);
165 const double A = exp(log(div) / exponent);
167 _a = (
static_cast<double>(pointA.
x) -
static_cast<double>(pointB.
x) * A) / (A - 1);
168 _b =
static_cast<double>(pointA.
y) / pow(
static_cast<double>(pointA.
x + _a), exponent);
178 const float res = pow(
static_cast<double>(x) + _a, _exp) * _b;
179 return lmpd_constrain<float>(res, lowerBound, upperBound);
Given two points and an exponent, fit an exponential function.
Definition: curves.h:140
float sample(const T x) const
Sample the exponential curve. Be aware that the result is always casted from a floating point !...
Definition: curves.h:176
ExponentialCurve(const point_t &pointA, const point_t &pointB, const double exponent=15.0)
Exponential curve fitting to two points.
Definition: curves.h:152
Given a set of points, will fit multiple linear segments to it.
Definition: curves.h:36
U sample(const T x) const
Sample a point Y from a given x.
Definition: curves.h:92
LinearCurve(const std::array< point_t, N > &points)
Build a curve from a set of points.
Definition: curves.h:42
Program scope.
Definition: control_fixed_modes.hpp:14
Define a 2D point.
Definition: curves.h:24
U y
Y coordinate of the point.
Definition: curves.h:26
T x
X coordinate of the point.
Definition: curves.h:25