WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
QuinticHermiteSpline.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
10#include "wpi/util/array.hpp"
11
12namespace wpi::math {
13/**
14 * Represents a hermite spline of degree 5.
15 */
17 public:
18 /**
19 * Constructs a quintic hermite spline with the specified control vectors.
20 * Each control vector contains into about the location of the point, its
21 * first derivative, and its second derivative.
22 *
23 * @param xInitialControlVector The control vector for the initial point in
24 * the x dimension.
25 * @param xFinalControlVector The control vector for the final point in
26 * the x dimension.
27 * @param yInitialControlVector The control vector for the initial point in
28 * the y dimension.
29 * @param yFinalControlVector The control vector for the final point in
30 * the y dimension.
31 */
33 wpi::util::array<double, 3> xFinalControlVector,
34 wpi::util::array<double, 3> yInitialControlVector,
35 wpi::util::array<double, 3> yFinalControlVector)
36 : m_initialControlVector{xInitialControlVector, yInitialControlVector},
37 m_finalControlVector{xFinalControlVector, yFinalControlVector} {
38 const auto hermite = MakeHermiteBasis();
39 const auto x =
40 ControlVectorFromArrays(xInitialControlVector, xFinalControlVector);
41 const auto y =
42 ControlVectorFromArrays(yInitialControlVector, yFinalControlVector);
43
44 // Populate first two rows with coefficients.
45 m_coefficients.template block<1, 6>(0, 0) = (hermite * x).transpose();
46 m_coefficients.template block<1, 6>(1, 0) = (hermite * y).transpose();
47
48 // Populate Row 2 and Row 3 with the derivatives of the equations above.
49 // Then populate row 4 and 5 with the second derivatives.
50 for (int i = 0; i < 6; i++) {
51 // Here, we are multiplying by (5 - i) to manually take the derivative.
52 // The power of the term in index 0 is 5, index 1 is 4 and so on. To find
53 // the coefficient of the derivative, we can use the power rule and
54 // multiply the existing coefficient by its power.
55 m_coefficients.template block<2, 1>(2, i) =
56 m_coefficients.template block<2, 1>(0, i) * (5 - i);
57 }
58 for (int i = 0; i < 5; i++) {
59 // Here, we are multiplying by (4 - i) to manually take the derivative.
60 // The power of the term in index 0 is 4, index 1 is 3 and so on. To find
61 // the coefficient of the derivative, we can use the power rule and
62 // multiply the existing coefficient by its power.
63 m_coefficients.template block<2, 1>(4, i) =
64 m_coefficients.template block<2, 1>(2, i) * (4 - i);
65 }
66 }
67
68 /**
69 * Returns the coefficients matrix.
70 * @return The coefficients matrix.
71 */
72 Matrixd<6, 6> Coefficients() const override { return m_coefficients; }
73
74 /**
75 * Returns the initial control vector that created this spline.
76 *
77 * @return The initial control vector that created this spline.
78 */
79 const ControlVector& GetInitialControlVector() const override {
80 return m_initialControlVector;
81 }
82
83 /**
84 * Returns the final control vector that created this spline.
85 *
86 * @return The final control vector that created this spline.
87 */
88 const ControlVector& GetFinalControlVector() const override {
89 return m_finalControlVector;
90 }
91
92 private:
93 Matrixd<6, 6> m_coefficients = Matrixd<6, 6>::Zero();
94
95 ControlVector m_initialControlVector;
96 ControlVector m_finalControlVector;
97
98 /**
99 * Returns the hermite basis matrix for quintic hermite spline interpolation.
100 * @return The hermite basis matrix for quintic hermite spline interpolation.
101 */
102 static constexpr Matrixd<6, 6> MakeHermiteBasis() {
103 // Given P(i), P'(i), P"(i), P(i+1), P'(i+1), P"(i+1), the control vectors,
104 // we want to find the coefficients of the spline
105 // P(t) = a₅t⁵ + a₄t⁴ + a₃t³ + a₂t² + a₁t + a₀.
106 //
107 // P(i) = P(0) = a₀
108 // P'(i) = P'(0) = a₁
109 // P''(i) = P"(0) = 2a₂
110 // P(i+1) = P(1) = a₅ + a₄ + a₃ + a₂ + a₁ + a₀
111 // P'(i+1) = P'(1) = 5a₅ + 4a₄ + 3a₃ + 2a₂ + a₁
112 // P"(i+1) = P"(1) = 20a₅ + 12a₄ + 6a₃ + 2a₂
113 //
114 // [P(i) ] = [ 0 0 0 0 0 1][a₅]
115 // [P'(i) ] = [ 0 0 0 0 1 0][a₄]
116 // [P"(i) ] = [ 0 0 0 2 0 0][a₃]
117 // [P(i+1) ] = [ 1 1 1 1 1 1][a₂]
118 // [P'(i+1)] = [ 5 4 3 2 1 0][a₁]
119 // [P"(i+1)] = [20 12 6 2 0 0][a₀]
120 //
121 // To solve for the coefficients, we can invert the 6x6 matrix and move it
122 // to the other side of the equation.
123 //
124 // [a₅] = [ -6.0 -3.0 -0.5 6.0 -3.0 0.5][P(i) ]
125 // [a₄] = [ 15.0 8.0 1.5 -15.0 7.0 -1.0][P'(i) ]
126 // [a₃] = [-10.0 -6.0 -1.5 10.0 -4.0 0.5][P"(i) ]
127 // [a₂] = [ 0.0 0.0 0.5 0.0 0.0 0.0][P(i+1) ]
128 // [a₁] = [ 0.0 1.0 0.0 0.0 0.0 0.0][P'(i+1)]
129 // [a₀] = [ 1.0 0.0 0.0 0.0 0.0 0.0][P"(i+1)]
130 return Matrixd<6, 6>{{-06.0, -03.0, -00.5, +06.0, -03.0, +00.5},
131 {+15.0, +08.0, +01.5, -15.0, +07.0, -01.0},
132 {-10.0, -06.0, -01.5, +10.0, -04.0, +00.5},
133 {+00.0, +00.0, +00.5, +00.0, +00.0, +00.0},
134 {+00.0, +01.0, +00.0, +00.0, +00.0, +00.0},
135 {+01.0, +00.0, +00.0, +00.0, +00.0, +00.0}};
136 }
137
138 /**
139 * Returns the control vector for each dimension as a matrix from the
140 * user-provided arrays in the constructor.
141 *
142 * @param initialVector The control vector for the initial point.
143 * @param finalVector The control vector for the final point.
144 *
145 * @return The control vector matrix for a dimension.
146 */
147 static constexpr Vectord<6> ControlVectorFromArrays(
148 wpi::util::array<double, 3> initialVector,
149 wpi::util::array<double, 3> finalVector) {
150 return Vectord<6>{{initialVector[0]}, {initialVector[1]},
151 {initialVector[2]}, {finalVector[0]},
152 {finalVector[1]}, {finalVector[2]}};
153 }
154};
155} // namespace wpi::math
156
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
Matrixd< 6, 6 > Coefficients() const override
Returns the coefficients matrix.
Definition QuinticHermiteSpline.hpp:72
const ControlVector & GetInitialControlVector() const override
Returns the initial control vector that created this spline.
Definition QuinticHermiteSpline.hpp:79
const ControlVector & GetFinalControlVector() const override
Returns the final control vector that created this spline.
Definition QuinticHermiteSpline.hpp:88
QuinticHermiteSpline(wpi::util::array< double, 3 > xInitialControlVector, wpi::util::array< double, 3 > xFinalControlVector, wpi::util::array< double, 3 > yInitialControlVector, wpi::util::array< double, 3 > yFinalControlVector)
Constructs a quintic hermite spline with the specified control vectors.
Definition QuinticHermiteSpline.hpp:32
constexpr Spline()=default
This class is a wrapper around std::array that does compile time size checking.
Definition array.hpp:26
Definition LinearSystem.hpp:20
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.hpp:21