Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
galaxis_polyfit.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef GALAXIS_POLYFIT_H
4 #define GALAXIS_POLYFIT_H
5 
6 #include "polyfit.h"
7 #include "utils/poly.hpp"
8 #include <Eigen/Dense>
9 #include <iostream>
10 #include <vector>
11 #include <exception>
12 #include <galaxis/linatypes.hpp>
13 
14 
15 constexpr int GALAXIS_MAXIMUM_DEGREE = 3;
16 
17 namespace {
18 
19 std::shared_ptr<std::vector<double>> galaxis_polyfit(std::vector<double>& pointsX,
20  std::vector<double>& pointsY,
21  int p_amountOfRandomPointsForFitting,
22  int p_iterationAmount,
23  float p_epsilon,
25  int p_degree = GALAXIS_MAXIMUM_DEGREE) {
26  try {
27  if(pointsX.size() < (p_degree + 1) || pointsX.size() != pointsY.size()) {
28  std::shared_ptr<std::vector<double>>
29  coeff = std::make_shared<std::vector<double>>();
30  coeff->push_back(0);
31  coeff->push_back(0);
32  coeff->push_back(0);
33  coeff->push_back(0);
34 
35  return coeff;
36  }
37 
38  if(pointsX.size() <= p_amountOfRandomPointsForFitting) {
39  if(!(((ssize_t)pointsX.size() - 2) > 1)) {
40  std::shared_ptr<std::vector<double>>
41  coeff = std::make_shared<std::vector<double>>();
42  coeff->push_back(0);
43  coeff->push_back(0);
44  coeff->push_back(0);
45  coeff->push_back(0);
46 
47  return coeff;
48  } else {
49  p_amountOfRandomPointsForFitting = pointsX.size() - 2;
50  }
51  }
52 
53  // Create options for fitting
55 
56  options.polyDeg = p_degree;
57  options.solver = solver; // SVD solver
58  options.maxMPts = p_amountOfRandomPointsForFitting; // #Pts to use for fitting
59  options.maxTrial = p_iterationAmount; // Max. nr. of trials
60  options.tolerance = p_epsilon; // Distance tolerance for inlier
61 
62  PolyFit<double> polyfit(pointsX, pointsY, options);
63 
64  // Solve using RANSAC
65  polyfit.solveRLS();
66 
67  auto polynomVecType = polyfit.getCoefficients();
68 
69  std::shared_ptr<std::vector<double>>
70  coeff = std::make_shared<std::vector<double>>();
71 
72  //std::cout << "Eigen PlynomVecTypes size: " << std::to_string(polynomVecType.size()) << std::endl;
73 
74  if(polynomVecType.size() != (p_degree + 1)) {
75  std::shared_ptr<std::vector<double>>
76  coeff = std::make_shared<std::vector<double>>();
77  coeff->push_back(0);
78  coeff->push_back(0);
79  coeff->push_back(0);
80  coeff->push_back(0);
81 
82  return coeff;
83  }
84 
85  for(ssize_t i = p_degree; i >= 0; i--) {
86  coeff->push_back(polynomVecType(i, 0));
87  }
88  for(ssize_t i = 0; i < (GALAXIS_MAXIMUM_DEGREE - p_degree); i++) {
89  coeff->push_back(0);
90  }
91 
92  //std::cout << "[POLYFIT] Degree: " << p_degree << "; [" << coeff->at(0) << ", " << coeff->at(1) << ", " << coeff->at(2) << ", " << coeff->at(3) << "]" << std::endl;
93 
94  return coeff;
95  } catch(std::exception& e) {
96  std::cout << "Exception in Polyfit" << std::endl;
97  std::shared_ptr<std::vector<double>> coeff = std::make_shared<std::vector<double>>();
98  coeff->push_back(0);
99  coeff->push_back(0);
100  coeff->push_back(0);
101  coeff->push_back(0);
102 
103  return coeff;
104  }
105 }
106 
107 std::shared_ptr<std::vector<double>> galaxis_polyfit(std::vector<galaxis::lina::Vector2f> const& p_pointCloud,
108  int p_amountOfRandomPointsForFitting,
109  int p_iterationAmount,
110  float p_epsilon,
111  bool p_switchxy = false,
112  double p_pointsDivisionFactor = 1,
114  int p_degree = GALAXIS_MAXIMUM_DEGREE) {
115 
116  throw "[GALAXIS POLYFIT] Function deprecated. Update your Code!";
117  return std::make_shared<std::vector<double>>();
118 }
119 
120 }
121 #endif
PolyFit
Definition: polyfit.h:38
polyfit.h
poly.hpp
PolyFit::Options::maxMPts
int maxMPts
Definition: polyfit.h:57
PolyFit::Options::tolerance
double tolerance
Definition: polyfit.h:58
PolyFit::Options::solver
SolverType solver
Definition: polyfit.h:54
PolyFit::solveRLS
void solveRLS(void)
Definition: polyfit.h:224
PolyFit::Options::polyDeg
int polyDeg
Definition: polyfit.h:55
linatypes.hpp
GALAXIS_MAXIMUM_DEGREE
constexpr int GALAXIS_MAXIMUM_DEGREE
Definition: galaxis_polyfit.h:15
PolyFit::SolverType
SolverType
Definition: polyfit.h:40
PolyFit::getCoefficients
const VecType getCoefficients() const
Definition: polyfit.h:104
PolyFit::Options::maxTrial
int maxTrial
Definition: polyfit.h:56
PolyFit::Options
Definition: polyfit.h:46