WPILibC++ 2023.4.3
CubicHermiteSpline.h
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
7#include <wpi/SymbolExports.h>
8#include <wpi/array.h>
9
10#include "frc/EigenCore.h"
11#include "frc/spline/Spline.h"
12
13namespace frc {
14/**
15 * Represents a hermite spline of degree 3.
16 */
18 public:
19 /**
20 * Constructs a cubic hermite spline with the specified control vectors. Each
21 * control vector contains info about the location of the point and its first
22 * derivative.
23 *
24 * @param xInitialControlVector The control vector for the initial point in
25 * the x dimension.
26 * @param xFinalControlVector The control vector for the final point in
27 * the x dimension.
28 * @param yInitialControlVector The control vector for the initial point in
29 * the y dimension.
30 * @param yFinalControlVector The control vector for the final point in
31 * the y dimension.
32 */
34 wpi::array<double, 2> xFinalControlVector,
35 wpi::array<double, 2> yInitialControlVector,
36 wpi::array<double, 2> yFinalControlVector);
37
38 protected:
39 /**
40 * Returns the coefficients matrix.
41 * @return The coefficients matrix.
42 */
43 Matrixd<6, 3 + 1> Coefficients() const override { return m_coefficients; }
44
45 private:
46 Matrixd<6, 4> m_coefficients = Matrixd<6, 4>::Zero();
47
48 /**
49 * Returns the hermite basis matrix for cubic hermite spline interpolation.
50 * @return The hermite basis matrix for cubic hermite spline interpolation.
51 */
52 static Matrixd<4, 4> MakeHermiteBasis() {
53 // Given P(i), P'(i), P(i+1), P'(i+1), the control vectors, we want to find
54 // the coefficients of the spline P(t) = a₃t³ + a₂t² + a₁t + a₀.
55 //
56 // P(i) = P(0) = a₀
57 // P'(i) = P'(0) = a₁
58 // P(i+1) = P(1) = a₃ + a₂ + a₁ + a₀
59 // P'(i+1) = P'(1) = 3a₃ + 2a₂ + a₁
60 //
61 // [P(i) ] = [0 0 0 1][a₃]
62 // [P'(i) ] = [0 0 1 0][a₂]
63 // [P(i+1) ] = [1 1 1 1][a₁]
64 // [P'(i+1)] = [3 2 1 0][a₀]
65 //
66 // To solve for the coefficients, we can invert the 4x4 matrix and move it
67 // to the other side of the equation.
68 //
69 // [a₃] = [ 2 1 -2 1][P(i) ]
70 // [a₂] = [-3 -2 3 -1][P'(i) ]
71 // [a₁] = [ 0 1 0 0][P(i+1) ]
72 // [a₀] = [ 1 0 0 0][P'(i+1)]
73
74 static const Matrixd<4, 4> basis{{+2.0, +1.0, -2.0, +1.0},
75 {-3.0, -2.0, +3.0, -1.0},
76 {+0.0, +1.0, +0.0, +0.0},
77 {+1.0, +0.0, +0.0, +0.0}};
78 return basis;
79 }
80
81 /**
82 * Returns the control vector for each dimension as a matrix from the
83 * user-provided arrays in the constructor.
84 *
85 * @param initialVector The control vector for the initial point.
86 * @param finalVector The control vector for the final point.
87 *
88 * @return The control vector matrix for a dimension.
89 */
90 static Eigen::Vector4d ControlVectorFromArrays(
91 wpi::array<double, 2> initialVector, wpi::array<double, 2> finalVector) {
92 return Eigen::Vector4d{initialVector[0], initialVector[1], finalVector[0],
93 finalVector[1]};
94 }
95};
96} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
Represents a hermite spline of degree 3.
Definition: CubicHermiteSpline.h:17
Matrixd< 6, 3+1 > Coefficients() const override
Returns the coefficients matrix.
Definition: CubicHermiteSpline.h:43
CubicHermiteSpline(wpi::array< double, 2 > xInitialControlVector, wpi::array< double, 2 > xFinalControlVector, wpi::array< double, 2 > yInitialControlVector, wpi::array< double, 2 > yFinalControlVector)
Constructs a cubic hermite spline with the specified control vectors.
Represents a two-dimensional parametric spline that interpolates between two points.
Definition: Spline.h:25
This class is a wrapper around std::array that does compile time size checking.
Definition: array.h:25
Definition: AprilTagFieldLayout.h:22