OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
PchipInterpolatorHelper Class Reference

A helper class to perform Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) interpolation. More...

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/GCode/PchipInterpolatorHelper.hpp>

Collaboration diagram for PchipInterpolatorHelper:

Public Member Functions

 PchipInterpolatorHelper ()=default
 Default constructor.
 PchipInterpolatorHelper (const std::vector< double > &x, const std::vector< double > &y)
 Constructs the PCHIP interpolator with given data points.
void setData (const std::vector< double > &x, const std::vector< double > &y)
 Sets the data points for the interpolator.
double interpolate (double xi) const
 Interpolates the value at a given point.

Private Member Functions

void computePCHIP ()
 Computes the PCHIP coefficients.
void sortData ()
 Sorts the data points by x-coordinate.
double h (int i) const
 Computes the difference between successive x-coordinates.
double delta (int i) const
 Computes the slope of the segment between successive data points.

Private Attributes

std::vector< double > x_
 The x-coordinates of the data points.
std::vector< double > y_
 The y-coordinates of the data points.
std::vector< double > h_
 The differences between successive x-coordinates.
std::vector< double > delta_
 The slopes of the segments between successive data points.
std::vector< double > d_
 The derivatives at the data points.

Detailed Description

A helper class to perform Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) interpolation.

Constructor & Destructor Documentation

◆ PchipInterpolatorHelper() [1/2]

PchipInterpolatorHelper::PchipInterpolatorHelper ( )
default

Default constructor.

◆ PchipInterpolatorHelper() [2/2]

PchipInterpolatorHelper::PchipInterpolatorHelper ( const std::vector< double > & x,
const std::vector< double > & y )

Constructs the PCHIP interpolator with given data points.

Parameters
xThe x-coordinates of the data points.
yThe y-coordinates of the data points.
20 {
21 setData(x, y);
22}
void setData(const std::vector< double > &x, const std::vector< double > &y)
Sets the data points for the interpolator.
Definition PchipInterpolatorHelper.cpp:30

References setData().

Here is the call graph for this function:

Member Function Documentation

◆ computePCHIP()

void PchipInterpolatorHelper::computePCHIP ( )
private

Computes the PCHIP coefficients.

59 {
60 size_t n = x_.size() - 1;
61 h_.resize(n);
62 delta_.resize(n);
63 d_.resize(n+1);
64
65 for (size_t i = 0; i < n; ++i) {
66 h_[i] = h(i);
67 delta_[i] = delta(i);
68 }
69
70 d_[0] = delta_[0];
71 d_[n] = delta_[n-1];
72 for (size_t i = 1; i < n; ++i) {
73 if (delta_[i-1] * delta_[i] > 0) {
74 double w1 = 2 * h_[i] + h_[i-1];
75 double w2 = h_[i] + 2 * h_[i-1];
76 d_[i] = (w1 + w2) / (w1 / delta_[i-1] + w2 / delta_[i]);
77 } else {
78 d_[i] = 0;
79 }
80 }
81}
std::vector< double > d_
The derivatives at the data points.
Definition PchipInterpolatorHelper.hpp:49
std::vector< double > h_
The differences between successive x-coordinates.
Definition PchipInterpolatorHelper.hpp:47
std::vector< double > delta_
The slopes of the segments between successive data points.
Definition PchipInterpolatorHelper.hpp:48
std::vector< double > x_
The x-coordinates of the data points.
Definition PchipInterpolatorHelper.hpp:45
double h(int i) const
Computes the difference between successive x-coordinates.
Definition PchipInterpolatorHelper.hpp:66
double delta(int i) const
Computes the slope of the segment between successive data points.
Definition PchipInterpolatorHelper.hpp:73

References d_, delta(), delta_, h(), h_, and x_.

Referenced by setData().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ delta()

double PchipInterpolatorHelper::delta ( int i) const
inlineprivate

Computes the slope of the segment between successive data points.

Parameters
iThe index of the segment.
Returns
The slope of the segment between y_[i] and y_[i+1].
73{ return (y_[i+1] - y_[i]) / h(i); }
std::vector< double > y_
The y-coordinates of the data points.
Definition PchipInterpolatorHelper.hpp:46

References h(), and y_.

Referenced by computePCHIP().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ h()

double PchipInterpolatorHelper::h ( int i) const
inlineprivate

Computes the difference between successive x-coordinates.

Parameters
iThe index of the x-coordinate.
Returns
The difference between x_[i+1] and x_[i].
66{ return x_[i+1] - x_[i]; }

References x_.

Referenced by computePCHIP(), and delta().

Here is the caller graph for this function:

◆ interpolate()

double PchipInterpolatorHelper::interpolate ( double xi) const

Interpolates the value at a given point.

Parameters
xiThe x-coordinate at which to interpolate.
Returns
The interpolated y-coordinate.
86 {
87 if (xi <= x_.front()) return y_.front();
88 if (xi >= x_.back()) return y_.back();
89
90 auto it = std::lower_bound(x_.begin(), x_.end(), xi);
91 size_t i = std::distance(x_.begin(), it) - 1;
92
93 double h_i = h_[i];
94 double t = (xi - x_[i]) / h_i;
95 double t2 = t * t;
96 double t3 = t2 * t;
97
98 double h00 = 2 * t3 - 3 * t2 + 1;
99 double h10 = t3 - 2 * t2 + t;
100 double h01 = -2 * t3 + 3 * t2;
101 double h11 = t3 - t2;
102
103 return h00 * y_[i] + h10 * h_i * d_[i] + h01 * y_[i+1] + h11 * h_i * d_[i+1];
104}

References d_, h_, x_, and y_.

Referenced by AdaptivePAInterpolator::operator()().

Here is the caller graph for this function:

◆ setData()

void PchipInterpolatorHelper::setData ( const std::vector< double > & x,
const std::vector< double > & y )

Sets the data points for the interpolator.

Parameters
xThe x-coordinates of the data points.
yThe y-coordinates of the data points.
Exceptions
std::invalid_argumentif x and y have different sizes or if they contain fewer than two points.
30 {
31 if (x.size() != y.size() || x.size() < 2) {
32 throw std::invalid_argument("Input vectors must have the same size and contain at least two points.");
33 }
34 x_ = x;
35 y_ = y;
36 sortData();
38}
void computePCHIP()
Computes the PCHIP coefficients.
Definition PchipInterpolatorHelper.cpp:59
void sortData()
Sorts the data points by x-coordinate.
Definition PchipInterpolatorHelper.cpp:43

References computePCHIP(), sortData(), x_, and y_.

Referenced by PchipInterpolatorHelper().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sortData()

void PchipInterpolatorHelper::sortData ( )
private

Sorts the data points by x-coordinate.

43 {
44 std::vector<std::pair<double, double>> data;
45 for (size_t i = 0; i < x_.size(); ++i) {
46 data.emplace_back(x_[i], y_[i]);
47 }
48 std::sort(data.begin(), data.end());
49
50 for (size_t i = 0; i < data.size(); ++i) {
51 x_[i] = data[i].first;
52 y_[i] = data[i].second;
53 }
54}

References x_, and y_.

Referenced by setData().

Here is the caller graph for this function:

Member Data Documentation

◆ d_

std::vector<double> PchipInterpolatorHelper::d_
private

The derivatives at the data points.

Referenced by computePCHIP(), and interpolate().

◆ delta_

std::vector<double> PchipInterpolatorHelper::delta_
private

The slopes of the segments between successive data points.

Referenced by computePCHIP().

◆ h_

std::vector<double> PchipInterpolatorHelper::h_
private

The differences between successive x-coordinates.

Referenced by computePCHIP(), and interpolate().

◆ x_

std::vector<double> PchipInterpolatorHelper::x_
private

The x-coordinates of the data points.

Referenced by computePCHIP(), h(), interpolate(), setData(), and sortData().

◆ y_

std::vector<double> PchipInterpolatorHelper::y_
private

The y-coordinates of the data points.

Referenced by delta(), interpolate(), setData(), and sortData().


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