Easy3D 2.5.3
SplineInterpolation< FT > Class Template Reference

Cubic spline interpolation. More...

#include <easy3d/core/spline_interpolation.h>

Public Types

enum  BoundaryType { first_deriv = 1 , second_deriv = 2 }
 

Public Member Functions

 SplineInterpolation ()
 
void set_boundary (BoundaryType left, FT left_value, BoundaryType right, FT right_value, bool linear_extrapolation=false)
 
void set_data (const std::vector< FT > &x, const std::vector< FT > &y, bool cubic_spline=true)
 
FT operator() (FT x) const
 Evaluates the spline at x.
 
FT derivative (int order, FT x) const
 Returns the order -th derivative of the spline at x.
 

Detailed Description

template<typename FT>
class easy3d::SplineInterpolation< FT >

Cubic spline interpolation.

SplineInterpolation generates a piecewise polynomial function of degree 3 and is twice continuously differentiable everywhere. Boundary conditions default to zero-curvature at the end points. It extrapolates linearly, if default boundary conditions are used, or otherwise extrapolation is a quadratic function. The math behind this implementation is described here: https://kluge.in-chemnitz.de/opensource/spline/

Spline interpolation have many applications, e.g., curve interpolation (for any dimensions). The following code shows how to use SplineInterpolation for 3D curve interpolation.

// a 3D curve is represented in the parametric form: x(t), y(t), and z(t).
std::vector<double> T(points.size()), X(points.size()), Y(points.size()), Z(points.size());
double t = 0.0;
for (std::size_t i = 0; i < points.size(); ++i) {
const auto &p = points[i];
if (i > 0)
t += distance(points[i-1], p);
T[i] = t;
X[i] = p.x;
Y[i] = p.y;
Z[i] = p.z;
}
// class instantiation
typedef SplineInterpolation<double> Interpolator;
Interpolator x_spline, y_spline, z_spline;
// [optional] set the boundary conditions.
x_spline.set_boundary(Interpolator::second_deriv, 0.0, Interpolator::first_deriv, -2.0, false);
// set data
x_spline.set_data(T, X);
y_spline.set_data(T, Y);
z_spline.set_data(T, Z);
// evaluate the curve at equal intervals (this results in a sequence of points on the curve).
double total_length = t;
int steps = 1000;
double interval = total_length / steps;
for (int i=0; i<=steps; ++i) {
double d = i * interval;
vec3 p(x_spline(d), y_spline(d), z_spline(d));
std::cout << p << std::endl;
}
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition: types.h:45
T distance(const Vec< N, T > &v1, const Vec< N, T > &v2)
Computes the distance between two vectors/points.
Definition: vec.h:295

Constructor & Destructor Documentation

◆ SplineInterpolation()

SplineInterpolation ( )
inline

Constructor. Sets default boundary condition to be zero curvature at both ends

Member Function Documentation

◆ set_boundary()

void set_boundary ( BoundaryType  left,
FT  left_value,
BoundaryType  right,
FT  right_value,
bool  linear_extrapolation = false 
)

Sets the boundary condition (optional).

Attention
If called, it has to come before set_points().

◆ set_data()

void set_data ( const std::vector< FT > &  x,
const std::vector< FT > &  y,
bool  cubic_spline = true 
)

Sets the data can carry out the interpolation. true for cubic spline interpolation; false for linear interpolation.

Attention
The x has to be monotonously increasing.

The documentation for this class was generated from the following file: