2
0
Fork 0
OB-Xd/Modules/gin/utilities/spline.h
2020-05-21 09:14:57 +02:00

47 lines
1.4 KiB
C++
Executable file

/*==============================================================================
Copyright 2018 by Roland Rabien, Devin Lane
For more information visit www.rabiensoftware.com
==============================================================================*/
#pragma once
/** Cubic spline interpolation is a simple way to obtain a smooth curve from a set of
discrete points. It has both C1 (first derivative) and C2 (second derivative) continuity,
enabling it to produce a continuous piecewise function given a set of data points.
Add points in increasing x order */
class Spline
{
public:
Spline (const Array<juce::Point<double>>& points);
double operator[] (double x) const;
double interpolate (double x) const;
class Element
{
public:
Element (double x_ = 0) : x (x_) {}
Element (double x_, double a_, double b_, double c_, double d_)
: x (x_), a (a_), b (b_), c (c_), d (d_) {}
double eval (double xx) const
{
double xix (xx - x);
return a + b * xix + c * (xix * xix) + d * (xix * xix * xix);
}
bool operator< (const Element& e) const { return x < e.x; }
bool operator< (const double xx) const { return x < xx; }
double x = 0, a = 0, b = 0, c = 0, d = 0;
};
private:
Array<Element> elements;
JUCE_LEAK_DETECTOR (Spline)
};