7 #include <utility/dllexport.h>
9 #include <rtti/object.h>
10 #include <glm/glm.hpp>
11 #include <nap/resource.h>
34 template<
typename T,
typename V>
74 template<
typename T,
typename V>
111 template<
typename T,
typename V>
161 T tForX(
const FComplex<T, V> (& pts)[4], T x, T threshold = 0.0001,
int maxIterations = 100);
170 V lerp(
const V& a,
const V& b,
const T& t);
215 int pointIndexAtTime(
const T& t)
const;
225 void limitOverhang(
const T& x0, T& x1, T& x2,
const T& x3);
237 mutable std::vector<FCurvePoint<T, V>*> mSortedPoints;
238 bool mPointsSorted =
false;
266 template<
typename T,
typename V>
276 mPointsSorted =
true;
280 if (t < firstPoint->mPos.mTime)
281 return firstPoint->
mPos.mValue;
284 if (t >= lastPoint->
mPos.mTime)
285 return lastPoint->
mPos.mValue;
287 int idx = pointIndexAtTime(t);
294 auto c = d + next->
mInTan;
296 limitOverhangPoints(a, b, c, d);
300 return evalCurveSegmentBezier({ a, b, c, d }, t);
302 return evalCurveSegmentLinear({ a, b, c, d }, t);
304 return evalCurveSegmentStepped({ a, b, c, d }, t);
312 template<
typename T,
typename V>
320 return pts[0] * a + pts[1] * b + pts[2] * c + pts[3] * d;
324 template<
typename T,
typename V>
329 for (
int i = 0; i < maxIterations; i++)
331 auto dx = x - bezier(pts, t).mTime;
332 auto adx = std::abs(dx);
333 if (adx <= threshold)
337 t += (dx > 0) ? depth : -depth;
342 template<
typename T,
typename V>
345 return a + t * (b - a);
348 template<
typename T,
typename V>
352 return bezier(pts, t).mValue;
355 template<
typename T,
typename V>
358 const auto& a = pts[0];
359 const auto& b = pts[3];
360 T t = (x - a.mTime) / (b.mTime - a.mTime);
361 return lerp(a.mValue, b.mValue, t);
365 template<
typename T,
typename V>
368 return pts[0].mValue;
371 template<
typename T,
typename V>
374 mSortedPoints.clear();
375 for (
int i = 0; i < mPoints.size(); i++)
376 mSortedPoints.emplace_back(&mPoints[i]);
378 std::sort(mSortedPoints.begin(), mSortedPoints.end(),
379 [](
const FCurvePoint<T, V>* lhs,
const FCurvePoint<T, V>* rhs)
381 return lhs->mPos.mTime < rhs->mPos.mTime;
385 template<
typename T,
typename V>
388 for (
size_t i = 0, len = mSortedPoints.size(); i < len; i++)
390 if (t < mSortedPoints[i]->mPos.mTime)
394 return static_cast<int>(i - 1);
401 template<
typename T,
typename V>
404 x1 = std::min(x1, x3);
405 x2 = std::max(x2, x0);
408 template<
typename T,
typename V>
418 limitOverhang(a, b, c, d);
421 auto rb = (b - a) / (bb - a);
422 auto rc = (c - d) / (cc - d);
429 pb.mValue = ((pb.mValue - pa.mValue) * rb) + pa.mValue;
430 pc.mValue = ((pc.mValue - pd.mValue) * rc) + pd.mValue;