|
@@ -0,0 +1,98 @@
|
|
|
+#include "quintic_polynomial.h"
|
|
|
+#include "absl/strings/str_cat.h"
|
|
|
+#include "absl/strings/str_join.h"
|
|
|
+
|
|
|
+namespace iv {
|
|
|
+namespace math {
|
|
|
+
|
|
|
+QuinticPolynomial::QuinticPolynomial(const double x0, const double dx0, const double ddx0,
|
|
|
+ const double x1, const double dx1, const double ddx1,
|
|
|
+ const double t)
|
|
|
+{
|
|
|
+ t_ = t;
|
|
|
+ start_condition_[0] = x0;
|
|
|
+ start_condition_[1] = dx0;
|
|
|
+ start_condition_[2] = ddx0;
|
|
|
+ end_condition_[0] = x1;
|
|
|
+ end_condition_[1] = dx1;
|
|
|
+ end_condition_[2] = ddx1;
|
|
|
+ computeCoefficients(x0, dx0, ddx0, x1, dx1, ddx1, t);
|
|
|
+}
|
|
|
+
|
|
|
+QuinticPolynomial::QuinticPolynomial(const std::array<double, 3> &start,
|
|
|
+ const std::array<double, 3> &end,
|
|
|
+ const double t)
|
|
|
+ : QuinticPolynomial::QuinticPolynomial(start[0], start[1], start[2],
|
|
|
+ end[0], end[1], end[2], t) {}
|
|
|
+
|
|
|
+double QuinticPolynomial::evaluate(std::uint32_t order, const double p) const
|
|
|
+{
|
|
|
+ switch (order)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ return ((((coef_[5] * p + coef_[4]) * p + coef_[3]) * p + coef_[2]) * p + coef_[1]) * p + coef_[0];
|
|
|
+ case 1:
|
|
|
+ return (((5.0 * coef_[5] * p + 4.0 * coef_[4]) * p + 3.0 * coef_[3]) * p + 2.0 * coef_[2]) * p + coef_[1];
|
|
|
+ case 2:
|
|
|
+ return (((20.0 * coef_[5] * p + 12.0 * coef_[4]) * p) + 6.0 * coef_[3]) * p + 2.0 * coef_[2];
|
|
|
+ case 3:
|
|
|
+ return (60.0 * coef_[5] * p + 24.0 * coef_[4]) * p + 6.0 * coef_[3];
|
|
|
+ case 4:
|
|
|
+ return 120.0 * coef_[5] * p + 24.0 * coef_[4];
|
|
|
+ case 5:
|
|
|
+ return 120.0 * coef_[5];
|
|
|
+ default:
|
|
|
+ return 0.0;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void QuinticPolynomial::fitByBoundaryConditions(const double x0, const double dx0, const double ddx0,
|
|
|
+ const double x1, const double dx1, const double ddx1,
|
|
|
+ const double t)
|
|
|
+{
|
|
|
+ t_ = t;
|
|
|
+ start_condition_[0] = x0;
|
|
|
+ start_condition_[1] = dx0;
|
|
|
+ start_condition_[2] = ddx0;
|
|
|
+ end_condition_[0] = x1;
|
|
|
+ end_condition_[1] = dx1;
|
|
|
+ end_condition_[2] = ddx1;
|
|
|
+ computeCoefficients(x0, dx0, ddx0, x1, dx1, ddx1, t);
|
|
|
+}
|
|
|
+
|
|
|
+double QuinticPolynomial::coef(const size_t order) const
|
|
|
+{
|
|
|
+ return coef_[order];
|
|
|
+}
|
|
|
+
|
|
|
+const std::array<double, 6> &QuinticPolynomial::coef() const
|
|
|
+{
|
|
|
+ return coef_;
|
|
|
+}
|
|
|
+
|
|
|
+void QuinticPolynomial::computeCoefficients(const double x0, const double dx0, const double ddx0,
|
|
|
+ const double x1, const double dx1, const double ddx1,
|
|
|
+ const double t)
|
|
|
+{
|
|
|
+ coef_[0] = x0;
|
|
|
+ coef_[1] = dx0;
|
|
|
+ coef_[2] = 0.5 * ddx0;
|
|
|
+
|
|
|
+ double t2 = t * t;
|
|
|
+ double t3 = t * t2;
|
|
|
+
|
|
|
+ double c0 = (x1 - 0.5 * t2 * ddx0 - dx0 * t - x0) / t3;
|
|
|
+ double c1 = (dx1 - ddx0 * t - dx0) / t2;
|
|
|
+ double c2 = (ddx1 - ddx0) / t;
|
|
|
+
|
|
|
+ coef_[3] = 0.5 * (20.0 * c0 - 8.0 * c1 + c2);
|
|
|
+ coef_[4] = (-15.0 * c0 + 7.0 * c1 - c2) / t;
|
|
|
+ coef_[5] = (6.0 * c0 - 3.0 * c1 + 0.5 * c2) / t2;
|
|
|
+}
|
|
|
+
|
|
|
+std::string QuinticPolynomial::toString() const
|
|
|
+{
|
|
|
+ return absl::StrCat("QuinticPolynomial(", absl::StrJoin(coef_, ", "), ")");
|
|
|
+}
|
|
|
+}
|
|
|
+}
|