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

A class to interpolate pressure advance (PA) values based on flow rate and acceleration using Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) interpolation. More...

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

Collaboration diagram for AdaptivePAInterpolator:

Public Member Functions

 AdaptivePAInterpolator ()
 Default constructor.
int parseAndSetData (const std::string &data)
 Parses the input data and sets up the interpolators.
double operator() (double flow_rate, double acceleration)
 Interpolates the PA value for the given flow rate and acceleration.
bool isInitialised () const
 Returns the initialization status.

Private Attributes

std::map< double, PchipInterpolatorHelper > flow_interpolators_
 Map each acceleration to a flow-rate-to-PA interpolator.
std::vector< double > accelerations_
 Store unique accelerations.
bool m_isInitialised

Detailed Description

A class to interpolate pressure advance (PA) values based on flow rate and acceleration using Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) interpolation.

Constructor & Destructor Documentation

◆ AdaptivePAInterpolator()

AdaptivePAInterpolator::AdaptivePAInterpolator ( )
inline

Default constructor.

23: m_isInitialised(false) {}
bool m_isInitialised
Definition AdaptivePAInterpolator.hpp:51

References m_isInitialised.

Member Function Documentation

◆ isInitialised()

bool AdaptivePAInterpolator::isInitialised ( ) const
inline

Returns the initialization status.

Returns
The value of m_isInitialised.
44 {
45 return m_isInitialised;
46 }

References m_isInitialised.

Referenced by Slic3r::AdaptivePAProcessor::process_layer().

Here is the caller graph for this function:

◆ operator()()

double AdaptivePAInterpolator::operator() ( double flow_rate,
double acceleration )

Interpolates the PA value for the given flow rate and acceleration.

Parameters
flow_rateThe flow rate at which to interpolate.
accelerationThe acceleration at which to interpolate.
Returns
The interpolated PA value, or -1 if interpolation fails.
91 {
92 std::vector<double> pa_values;
93 std::vector<double> acc_values;
94
95 // Estimate PA value for every flow to PA model for the given flow rate
96 for (const auto& kv : flow_interpolators_) {
97 double pa_value = kv.second.interpolate(flow_rate);
98
99 // Check if the interpolated PA value is valid
100 if (pa_value != -1) {
101 pa_values.push_back(pa_value);
102 acc_values.push_back(kv.first);
103 }
104 }
105
106 // Check if there are enough acceleration values for interpolation
107 if (acc_values.size() < 2) {
108 // Special case: Only one acceleration value
109 if (acc_values.size() == 1) {
110 return std::round(pa_values[0] * 1000.0) / 1000.0; // Rounded to 3 decimal places
111 }
112 return -1; // Error: Not enough data points for interpolation
113 }
114
115 // Create a new PchipInterpolatorHelper for PA-acceleration interpolation
116 // Use the estimated PA values from the for loop above and their corresponding accelerations to
117 // generate the new PCHIP model. Then run this model to interpolate the PA value for the given acceleration value.
118 PchipInterpolatorHelper pa_accel_interpolator(acc_values, pa_values);
119 return std::round(pa_accel_interpolator.interpolate(acceleration) * 1000.0) / 1000.0; // Rounded to 3 decimal places
120}
std::map< double, PchipInterpolatorHelper > flow_interpolators_
Map each acceleration to a flow-rate-to-PA interpolator.
Definition AdaptivePAInterpolator.hpp:49

References flow_interpolators_, and PchipInterpolatorHelper::interpolate().

Here is the call graph for this function:

◆ parseAndSetData()

int AdaptivePAInterpolator::parseAndSetData ( const std::string & data)

Parses the input data and sets up the interpolators.

Parameters
dataA string containing the data in CSV format (PA, flow rate, acceleration).
Returns
0 on success, -1 on error.
23 {
24 flow_interpolators_.clear();
25 accelerations_.clear();
26
27 try {
28 std::istringstream ss(data);
29 std::string line;
30 std::map<double, std::vector<std::pair<double, double>>> acc_to_flow_pa;
31
32 while (std::getline(ss, line)) {
33 std::istringstream lineStream(line);
34 std::string value;
35 double paValue, flowRate, acceleration;
36 paValue = flowRate = acceleration = 0.f; // initialize all to zero.
37
38 // Parse PA value
39 if (std::getline(lineStream, value, ',')) {
40 paValue = std::stod(value);
41 }
42
43 // Parse flow rate value
44 if (std::getline(lineStream, value, ',')) {
45 flowRate = std::stod(value);
46 }
47
48 // Parse acceleration value
49 if (std::getline(lineStream, value, ',')) {
50 acceleration = std::stod(value);
51 }
52
53 // Store the parsed values in a map with acceleration as the key
54 acc_to_flow_pa[acceleration].emplace_back(flowRate, paValue);
55 }
56
57 // Iterate through the map to set up the interpolators
58 for (const auto& kv : acc_to_flow_pa) {
59 double acceleration = kv.first;
60 const auto& data = kv.second;
61
62 std::vector<double> flowRates;
63 std::vector<double> paValues;
64
65 for (const auto& pair : data) {
66 flowRates.push_back(pair.first);
67 paValues.push_back(pair.second);
68 }
69
70 // Only set up the interpolator if there are enough data points
71 if (flowRates.size() > 1) {
72 PchipInterpolatorHelper interpolator(flowRates, paValues);
73 flow_interpolators_[acceleration] = interpolator;
74 accelerations_.push_back(acceleration);
75 }
76 }
77 } catch (const std::exception&) {
78 m_isInitialised = false;
79 return -1; // Error: Exception during parsing
80 }
81 m_isInitialised = true;
82 return 0; // Success
83}
std::vector< double > accelerations_
Store unique accelerations.
Definition AdaptivePAInterpolator.hpp:50

References accelerations_, flow_interpolators_, and m_isInitialised.

Member Data Documentation

◆ accelerations_

std::vector<double> AdaptivePAInterpolator::accelerations_
private

Store unique accelerations.

Referenced by parseAndSetData().

◆ flow_interpolators_

std::map<double, PchipInterpolatorHelper> AdaptivePAInterpolator::flow_interpolators_
private

Map each acceleration to a flow-rate-to-PA interpolator.

Referenced by operator()(), and parseAndSetData().

◆ m_isInitialised

bool AdaptivePAInterpolator::m_isInitialised
private

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