C++ interpolation library.
This library provides several interpolation methods.
- Linear Interpolation
- Cubic Natural Spline
- Cubic Hermite Spline
- Cubic Catmull-Rom Spline
- Quintic Natural Spline
- Quintic Hermite Spline
- Quintic Catmull-Rom Spline
Interpolate depends on Eigen3, a C++ linear algebra library. Make sure Eigen3 is installed and accessible from your CMake project.
$ git clone https://github.com/0Jaeyoung0/Interpolate.git
$ cd Interpolate
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build .
$ cmake --install . --prefix /your/path/toadd_executable(MyApp src/main.cpp)
target_include_directories(MyAPP PRIVATE /your/path/to/interpolate/include)
target_link_directories(MyAPP PRIVATE /your/path/to/interpolate/lib)
target_link_libraries(MyApp PRIVATE Interpolate)You can create a CubicNaturalSpline to interpolate a set of points with smooth 3rd-degree splines.
#include <iostream>
#include <vector>
#include <optional>
#include <Interpolate/Explicit/CubicNaturalSpline.h>
int main()
{
std::vector<double> x = {0.0, 1.0, 2.0, 3.0};
std::vector<double> y = {0.0, 0.5, 0.8, 1.0};
auto interpolator = CubicNaturalSpline::create(x, y, false);
if (interpolator)
{
// Evaluate at x = 1.5
if(auto val = interpolator->evaluate(1.5))
{
std::cout << "Interpolated value at x=1.5 is " << *val << std::endl;
}
// Get first derivative at x = 1.5
if(auto derivative = interpolator->firstDerivative(1.5))
{
std::cout << "First derivative at x=1.5 is " << *derivative << std::endl;
}
}
else
{
std::cerr << "Failed to create interpolator." << std::endl;
}
return 0;
}For parametric curves, you can interpolate x(t) and y(t) independently to create a smooth path.
#include <iostream>
#include <vector>
#include <optional>
#include <Interpolate/Explicit/CubicNaturalSpline.h>
int main()
{
std::vector<double> t = {0.0, 1.0, 2.0, 3.0};
std::vector<double> x = {4.0, 5.0, 2.0, 3.0};
std::vector<double> y = {3.0, 0.5, 0.8, 1.0};
auto x_interpolator = CubicNaturalSpline::create(t, x, false);
auto y_interpolator = CubicNaturalSpline::create(t, y, false);
if (x_interpolator && y_interpolator)
{
// Evaluate the parametric curve at t = 1.5
double t_eval = 1.5;
auto x_val = x_interpolator->evaluate(t_eval);
auto y_val = y_interpolator->evaluate(t_eval);
if (x_val && y_val)
{
std::cout << "Interpolated point at t=" << t_eval << " is (" << *x_val << ", " << *y_val << ")" << std::endl;
}
else
{
std::cerr << "Failed to evaluate at t=" << t_eval << std::endl;
}
}
else
{
std::cerr << "Failed to create interpolators for the parametric function." << std::endl;
}
return 0;
}Both CubicNaturalSpline and QuinticNaturalSpline can be used to create closed curves (loops). To do this, set the closed parameter to true when creating the interpolator.
When creating a closed spline, the last data point should not be the same as the first one; the algorithm will automatically connect the last point back to the first.
// Example for a closed cubic natural spline
std::vector<double> t = {0.0, 1.0, 2.0, 3.0};
std::vector<double> x = {4.0, 5.0, 2.0, 3.0};
std::vector<double> y = {3.0, 0.5, 0.8, 1.0};
auto closed_x_spline = CubicNaturalSpline::create(t, x, true);
auto closed_y_spline = CubicNaturalSpline::create(t, y, true);