Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
curves.h
Go to the documentation of this file.
1
5#pragma once
6
8
9#include <cassert>
10#include <cmath>
11#include <array>
12
13namespace lampda {
14namespace common {
16namespace curves {
17
23template<typename T, typename U> struct Point
24{
25 T x;
26 U y;
27};
28
35template<typename T, typename U, size_t maxSize = 24> class LinearCurve
36{
37public:
40
42 template<size_t N> LinearCurve(const std::array<point_t, N>& points) : _count(0)
43 {
44 static_assert(N <= maxSize, "Array size must be less than MaxSize");
45 // first fail fast
46 assert(points.size() >= 2 && "Linear curve must have more than 1 points");
47
48 // Copy points
49 for (size_t i = 0; i < N; ++i)
50 {
51 pts[i] = points[i];
52 }
53
54 // Validate all points
55 for (size_t i = 0; i < N; ++i)
56 {
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");
62 }
63
64 // Pre-verify points are sorted by X coordinate
65 for (size_t i = 1; i < N; ++i)
66 {
67 assert(pts[i - 1].x <= pts[i].x && "Points must be sorted by X coordinate in ascending order");
68 }
69
70 // Remove consecutive duplicates (in place)
71 size_t writeIdx = 1;
72 for (size_t readIdx = 1; readIdx < N; ++readIdx)
73 {
74 if (pts[readIdx - 1].x != pts[readIdx].x || pts[readIdx - 1].y != pts[readIdx].y)
75 {
76 pts[writeIdx] = pts[readIdx];
77 writeIdx++;
78 }
79 }
80 _count = writeIdx;
81
82 assert(_count >= 2 && "Linear curve must have more than 1 unique points");
83
84 // Prost-verify points are sorted by X coordinate (after compaction)
85 for (size_t i = 1; i < N; ++i)
86 {
87 assert(pts[i - 1].x < pts[i].x && "Points must be sorted by X coordinate in ascending order");
88 }
89 }
90
92 U sample(const T x) const
93 {
94 point_t lastPt = pts[0];
95 // bounds failure
96 if (std::isnan(x))
97 return lastPt.y;
98 if (x <= lastPt.x)
99 return lastPt.y;
100 if (x >= pts[_count - 1].x)
101 return pts[_count - 1].y;
102
103 for (size_t i = 1; i < _count; ++i)
104 {
105 const point_t& pt = pts[i];
106 // in this segment bound
107 if (x >= lastPt.x and x <= pt.x)
108 {
109 return lmpd_map<U>(x, lastPt.x, pt.x, lastPt.y, pt.y);
110 }
111 // update last point
112 lastPt = pt;
113 }
114
115 // highest bound failure
116 return lastPt.y;
117 }
118
119private:
121 std::array<point_t, maxSize> pts;
123 size_t _count;
124};
125
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)
128{
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);
132}
133
139template<typename T, typename U> class ExponentialCurve
140{
141public:
144
152 ExponentialCurve(const point_t& pointA, const point_t& pointB, const double exponent = 15.0) :
153 lowerBound(pointA.y),
154 upperBound(pointB.y)
155 {
156 const double div = pointA.y / static_cast<double>(pointB.y);
157 if (div <= -1.0)
158 {
159 // Error: linear approx...
160 _exp = 1;
161 _a = 0;
162 _b = 1;
163 return;
164 }
165 const double A = exp(log(div) / exponent);
166 _exp = 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);
169 }
170
176 float sample(const T x) const
177 {
178 const float res = pow(static_cast<double>(x) + _a, _exp) * _b;
179 return lmpd_constrain<float>(res, lowerBound, upperBound);
180 }
181
182private:
184 const T lowerBound;
186 const T upperBound;
187
189 double _exp;
191 double _a;
193 double _b;
194};
195
196} // namespace curves
197} // namespace common
198} // namespace lampda
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
Define useful functions.