HighMap library (C++)
Loading...
Searching...
No Matches
hmap::Interpolator1D Class Reference

A class for performing 1D interpolation using the GSL library. More...

#include <interpolate1d.hpp>

Public Member Functions

 Interpolator1D (const std::vector< float > &x, const std::vector< float > &y, InterpolationMethod1D method=InterpolationMethod1D::LINEAR)
 Constructs an Interpolator1D object with the given data and method.
 
 ~Interpolator1D ()
 Destructor for Interpolator1D.
 
float operator() (float x) const
 Function call operator for performing interpolation.
 
float interpolate (float x) const
 Interpolates the value at the specified x coordinate.
 

Detailed Description

A class for performing 1D interpolation using the GSL library.

The Interpolator1D class provides an interface for performing 1D interpolation on a set of data points. The interpolation methods supported are defined by the InterpolationMethod1D enumeration. The class uses the GSL library for the interpolation computations.

Example

#include <iostream>
#include "highmap.hpp"
int main(void)
{
std::vector<float> x = {0.f, 0.1f, 0.5f, 0.7f, 1.f};
std::vector<float> y = {0.2f, 0.1f, 0.5f, 0.6f, 0.4f};
std::vector<float> xi = hmap::linspace(0.f, 1.f, 50);
x,
y,
x,
y,
x,
y,
std::vector<float> yl, yc, ya;
for (auto &x_ : xi)
{
yl.push_back(fl(x_));
yc.push_back(fc(x_));
ya.push_back(fa(x_));
}
// plots
std::vector<float> v = std::vector<float>(xi.size(), 1.f);
hmap::Path(xi, yl, v).to_png("ex_interpolate1d_0.png", {128, 128});
hmap::Path(xi, yc, v).to_png("ex_interpolate1d_1.png", {128, 128});
hmap::Path(xi, ya, v).to_png("ex_interpolate1d_2.png", {128, 128});
}
A class for performing 1D interpolation using the GSL library.
Definition interpolate1d.hpp:65
Represents an ordered set of points in 2D, forming a polyline (open or closed).
Definition path.hpp:49
void to_png(std::string fname, Vec2< int > shape={512, 512})
Export path as PNG image file.
Definition path.cpp:895
std::vector< float > linspace(float start, float stop, int num, bool endpoint=true)
Generate a vector of evenly spaced numbers over a specified interval.
Definition vector.cpp:46
@ CUBIC
Cubic spline interpolation.
Definition interpolate1d.hpp:39
@ LINEAR
Linear interpolation.
Definition interpolate1d.hpp:41
@ AKIMA
Akima interpolation.
Definition interpolate1d.hpp:37

Result

Constructor & Destructor Documentation

◆ Interpolator1D()

hmap::Interpolator1D::Interpolator1D ( const std::vector< float > &  x,
const std::vector< float > &  y,
InterpolationMethod1D  method = InterpolationMethod1D::LINEAR 
)

Constructs an Interpolator1D object with the given data and method.

This constructor initializes the interpolation object with the provided x and y data points and the specified interpolation method.

Parameters
xA vector of x coordinates (independent variable).
yA vector of y coordinates (dependent variable).
methodThe interpolation method to use (default is linear).
Exceptions
std::invalid_argumentifxandyhavedifferentsizesoriftherearefewerthantwo points. An exception is also thrown if the method requires monotonic data and the provided data is not monotonic.

◆ ~Interpolator1D()

hmap::Interpolator1D::~Interpolator1D ( )

Destructor for Interpolator1D.

The destructor frees the GSL resources used by the interpolation object.

Member Function Documentation

◆ operator()()

float hmap::Interpolator1D::operator() ( float  x) const

Function call operator for performing interpolation.

This operator overload allows the Interpolator1D object to be called as a function, directly returning the interpolated value at the specified x coordinate. It serves as a shorthand for the interpolate() method.

Parameters
xThe x coordinate at which to interpolate.
Returns
The interpolated y value corresponding to the given x.

◆ interpolate()

float hmap::Interpolator1D::interpolate ( float  x) const

Interpolates the value at the specified x coordinate.

This function computes the interpolated y value corresponding to the given x coordinate using the previously specified interpolation method.

Parameters
xThe x coordinate at which to interpolate.
Returns
The interpolated y value.

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