WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
SplineParameterizer.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/*
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 <utility>
33#include <vector>
34
36#include "wpi/units/angle.hpp"
37#include "wpi/units/curvature.hpp"
38#include "wpi/units/length.hpp"
39#include "wpi/units/math.hpp"
41
42namespace wpi::math {
43
44/**
45 * Class used to parameterize a spline by its arc length.
46 */
48 public:
49 using PoseWithCurvature = std::pair<Pose2d, wpi::units::curvature_t>;
50
51 struct MalformedSplineException : public std::runtime_error {
52 explicit MalformedSplineException(const char* what_arg)
53 : runtime_error(what_arg) {}
54 };
55
56 /**
57 * Parametrizes the spline. This method breaks up the spline into various
58 * arcs until their dx, dy, and dtheta are within specific tolerances.
59 *
60 * @param spline The spline to parameterize.
61 * @param t0 Starting internal spline parameter. It is recommended to leave
62 * this as default.
63 * @param t1 Ending internal spline parameter. It is recommended to leave this
64 * as default.
65 *
66 * @return A vector of poses and curvatures that represents various points on
67 * the spline.
68 */
69 template <int Dim>
70 static std::vector<PoseWithCurvature> Parameterize(const Spline<Dim>& spline,
71 double t0 = 0.0,
72 double t1 = 1.0) {
73 constexpr const char* kMalformedSplineExceptionMsg =
74 "Could not parameterize a malformed spline. This means that you "
75 "probably had two or more adjacent waypoints that were very close "
76 "together with headings in opposing directions.";
77 std::vector<PoseWithCurvature> splinePoints;
78
79 // The parameterization does not add the initial point. Let's add that.
80 if (auto point = spline.GetPoint(t0)) {
81 splinePoints.push_back(point.value());
82 } else {
83 throw MalformedSplineException(kMalformedSplineExceptionMsg);
84 }
85
86 // We use an "explicit stack" to simulate recursion, instead of a recursive
87 // function call This give us greater control, instead of a stack overflow
88 std::stack<StackContents> stack;
89 stack.emplace(StackContents{t0, t1});
90
91 int iterations = 0;
92
93 while (!stack.empty()) {
94 auto current = stack.top();
95 stack.pop();
96
97 auto start = spline.GetPoint(current.t0);
98 if (!start) {
99 throw MalformedSplineException(kMalformedSplineExceptionMsg);
100 }
101
102 auto end = spline.GetPoint(current.t1);
103 if (!end) {
104 throw MalformedSplineException(kMalformedSplineExceptionMsg);
105 }
106
107 const auto twist = (end.value().first - start.value().first).Log();
108
109 if (wpi::units::math::abs(twist.dy) > kMaxDy ||
110 wpi::units::math::abs(twist.dx) > kMaxDx ||
111 wpi::units::math::abs(twist.dtheta) > kMaxDtheta) {
112 stack.emplace(StackContents{(current.t0 + current.t1) / 2, current.t1});
113 stack.emplace(StackContents{current.t0, (current.t0 + current.t1) / 2});
114 } else {
115 splinePoints.push_back(end.value());
116 }
117
118 if (iterations++ >= kMaxIterations) {
119 throw MalformedSplineException(kMalformedSplineExceptionMsg);
120 }
121 }
122
123 return splinePoints;
124 }
125
126 private:
127 // Constraints for spline parameterization.
128 static inline constexpr wpi::units::meter_t kMaxDx = 5_in;
129 static inline constexpr wpi::units::meter_t kMaxDy = 0.05_in;
130 static inline constexpr wpi::units::radian_t kMaxDtheta = 0.0872_rad;
131
132 struct StackContents {
133 double t0;
134 double t1;
135 };
136
137 /**
138 * A malformed spline does not actually explode the LIFO stack size. Instead,
139 * the stack size stays at a relatively small number (e.g. 30) and never
140 * decreases. Because of this, we must count iterations. Even long, complex
141 * paths don't usually go over 300 iterations, so hitting this maximum should
142 * definitely indicate something has gone wrong.
143 */
144 static constexpr int kMaxIterations = 5000;
145
148};
149} // namespace wpi::math
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
Represents a two-dimensional parametric spline that interpolates between two points.
Definition Spline.hpp:27
std::optional< PoseWithCurvature > GetPoint(double t) const
Gets the pose and curvature at some point t on the spline.
Definition Spline.hpp:62
Class used to parameterize a spline by its arc length.
Definition SplineParameterizer.hpp:47
static std::vector< PoseWithCurvature > Parameterize(const Spline< Dim > &spline, double t0=0.0, double t1=1.0)
Parametrizes the spline.
Definition SplineParameterizer.hpp:70
friend class QuinticHermiteSplineTest
Definition SplineParameterizer.hpp:147
std::pair< Pose2d, wpi::units::curvature_t > PoseWithCurvature
Definition SplineParameterizer.hpp:49
friend class CubicHermiteSplineTest
Definition SplineParameterizer.hpp:146
Definition LinearSystem.hpp:20
MalformedSplineException(const char *what_arg)
Definition SplineParameterizer.hpp:52