21 #include <Eigen/Geometry> 26 double lerp(
double a,
double b,
double f){
27 return a + f * (b - a);
30 double polyval(
const Eigen::VectorXd& poly,
double val){
32 for(uint8_t idx = 0; idx < poly.rows(); idx++){
33 result += poly[idx] * std::pow(val, poly.rows() - 1 - idx);
40 bool is_increasing_sequence = matrix(matrix.rows() - 1, 0) > matrix(0, 0);
41 if(is_increasing_sequence){
42 for(row_idx = 1; row_idx < matrix.rows() - 1; row_idx++){
43 if(key <= matrix(row_idx, 0)){
49 for(row_idx = 1; row_idx < matrix.rows() - 1; row_idx++){
50 if(key >= matrix(row_idx, 0)){
61 size_t num_of_rows = table.rows();
62 while(row_idx + 2 < num_of_rows && table(row_idx + 1, 0) < value){
69 const Eigen::MatrixXd& y,
70 const Eigen::MatrixXd& z,
75 size_t x2_idx = x1_idx + 1;
76 size_t y2_idx = y1_idx + 1;
77 double Q11 = z(y1_idx, x1_idx);
78 double Q12 = z(y2_idx, x1_idx);
79 double Q21 = z(y1_idx, x2_idx);
80 double Q22 = z(y2_idx, x2_idx);
81 double R1 = ((x(x2_idx) - x_val) * Q11 + (x_val - x(x1_idx)) * Q21) / (x(x2_idx) - x(x1_idx));
82 double R2 = ((x(x2_idx) - x_val) * Q12 + (x_val - x(x1_idx)) * Q22) / (x(x2_idx) - x(x1_idx));
83 double f = ((y(y2_idx) - y_val) * R1 + (y_val - y(y1_idx)) * R2) / (y(y2_idx) - y(y1_idx));
89 Eigen::VectorXd& polynomialCoeffs){
90 if(table.cols() < 2 || table.rows() < 2 || polynomialCoeffs.rows() < table.cols() - 1){
95 if(prevRowIdx + 2 > table.rows()){
99 const size_t nextRowIdx = prevRowIdx + 1;
100 const double airspeedStep = table.row(nextRowIdx)(0, 0) - table.row(prevRowIdx)(0, 0);
101 if (abs(airspeedStep) < 0.001) {
105 double delta = (airSpeedMod - table.row(prevRowIdx)(0, 0)) / airspeedStep;
106 const size_t numberOfCoeffs = table.cols() - 1;
107 for(
size_t coeff_idx = 0; coeff_idx < numberOfCoeffs; coeff_idx++){
108 const double prevValue = table.row(prevRowIdx)(0, coeff_idx + 1);
109 const double nextValue = table.row(nextRowIdx)(0, coeff_idx + 1);
110 polynomialCoeffs[coeff_idx] =
Math::lerp(prevValue, nextValue, delta);
double lerp(double a, double b, double f)
size_t findPrevRowIdxInMonotonicSequence(const Eigen::MatrixXd &matrix, double key)
Given monotonic sequence (increasing or decreasing) and key, return the index of the previous element...
double polyval(const Eigen::VectorXd &poly, double val)
double griddata(const Eigen::MatrixXd &x, const Eigen::MatrixXd &y, const Eigen::MatrixXd &z, double x_val, double y_val)
bool calculatePolynomial(const Eigen::MatrixXd &table, double airSpeedMod, Eigen::VectorXd &polynomialCoeffs)
size_t findPrevRowIdxInIncreasingSequence(const Eigen::MatrixXd &table, double value)
Given an increasing sequence and a key, return the index of the previous element closest to the key...