WPILibC++ 2023.4.3
SplineParameterizer.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/*
6 * MIT License
7 *
8 * Copyright (c) 2018 Team 254
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#pragma once
30
31#include <stack>
32#include <string>
33#include <utility>
34#include <vector>
35
36#include <wpi/SymbolExports.h>
37
38#include "frc/spline/Spline.h"
39#include "units/angle.h"
40#include "units/curvature.h"
41#include "units/length.h"
42#include "units/math.h"
43
44namespace frc {
45
46/**
47 * Class used to parameterize a spline by its arc length.
48 */
50 public:
51 using PoseWithCurvature = std::pair<Pose2d, units::curvature_t>;
52
53 struct MalformedSplineException : public std::runtime_error {
54 explicit MalformedSplineException(const char* what_arg)
55 : runtime_error(what_arg) {}
56 };
57
58 /**
59 * Parametrizes the spline. This method breaks up the spline into various
60 * arcs until their dx, dy, and dtheta are within specific tolerances.
61 *
62 * @param spline The spline to parameterize.
63 * @param t0 Starting internal spline parameter. It is recommended to leave
64 * this as default.
65 * @param t1 Ending internal spline parameter. It is recommended to leave this
66 * as default.
67 *
68 * @return A vector of poses and curvatures that represents various points on
69 * the spline.
70 */
71 template <int Dim>
72 static std::vector<PoseWithCurvature> Parameterize(const Spline<Dim>& spline,
73 double t0 = 0.0,
74 double t1 = 1.0) {
75 std::vector<PoseWithCurvature> splinePoints;
76
77 // The parameterization does not add the initial point. Let's add that.
78 splinePoints.push_back(spline.GetPoint(t0));
79
80 // We use an "explicit stack" to simulate recursion, instead of a recursive
81 // function call This give us greater control, instead of a stack overflow
82 std::stack<StackContents> stack;
83 stack.emplace(StackContents{t0, t1});
84
85 StackContents current;
88 int iterations = 0;
89
90 while (!stack.empty()) {
91 current = stack.top();
92 stack.pop();
93 start = spline.GetPoint(current.t0);
94 end = spline.GetPoint(current.t1);
95
96 const auto twist = start.first.Log(end.first);
97
98 if (units::math::abs(twist.dy) > kMaxDy ||
99 units::math::abs(twist.dx) > kMaxDx ||
100 units::math::abs(twist.dtheta) > kMaxDtheta) {
101 stack.emplace(StackContents{(current.t0 + current.t1) / 2, current.t1});
102 stack.emplace(StackContents{current.t0, (current.t0 + current.t1) / 2});
103 } else {
104 splinePoints.push_back(spline.GetPoint(current.t1));
105 }
106
107 if (iterations++ >= kMaxIterations) {
109 "Could not parameterize a malformed spline. "
110 "This means that you probably had two or more adjacent "
111 "waypoints that were very close together with headings "
112 "in opposing directions.");
113 }
114 }
115
116 return splinePoints;
117 }
118
119 private:
120 // Constraints for spline parameterization.
121 static constexpr units::meter_t kMaxDx = 5_in;
122 static constexpr units::meter_t kMaxDy = 0.05_in;
123 static constexpr units::radian_t kMaxDtheta = 0.0872_rad;
124
125 struct StackContents {
126 double t0;
127 double t1;
128 };
129
130 /**
131 * A malformed spline does not actually explode the LIFO stack size. Instead,
132 * the stack size stays at a relatively small number (e.g. 30) and never
133 * decreases. Because of this, we must count iterations. Even long, complex
134 * paths don't usually go over 300 iterations, so hitting this maximum should
135 * definitely indicate something has gone wrong.
136 */
137 static constexpr int kMaxIterations = 5000;
138
139 friend class CubicHermiteSplineTest;
140 friend class QuinticHermiteSplineTest;
141};
142} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
Represents a two-dimensional parametric spline that interpolates between two points.
Definition: Spline.h:25
PoseWithCurvature GetPoint(double t) const
Gets the pose and curvature at some point t on the spline.
Definition: Spline.h:57
Class used to parameterize a spline by its arc length.
Definition: SplineParameterizer.h:49
static std::vector< PoseWithCurvature > Parameterize(const Spline< Dim > &spline, double t0=0.0, double t1=1.0)
Parametrizes the spline.
Definition: SplineParameterizer.h:72
std::pair< Pose2d, units::curvature_t > PoseWithCurvature
Definition: SplineParameterizer.h:51
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
static EIGEN_DEPRECATED const end_t end
Definition: IndexedViewHelper.h:181
Definition: AprilTagFieldLayout.h:22
Definition: SplineParameterizer.h:53
MalformedSplineException(const char *what_arg)
Definition: SplineParameterizer.h:54