WPILibC++ 2025.3.1
Loading...
Searching...
No Matches
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 constexpr const char* kMalformedSplineExceptionMsg =
76 "Could not parameterize a malformed spline. This means that you "
77 "probably had two or more adjacent waypoints that were very close "
78 "together with headings in opposing directions.";
79 std::vector<PoseWithCurvature> splinePoints;
80
81 // The parameterization does not add the initial point. Let's add that.
82 if (auto point = spline.GetPoint(t0)) {
83 splinePoints.push_back(point.value());
84 } else {
85 throw MalformedSplineException(kMalformedSplineExceptionMsg);
86 }
87
88 // We use an "explicit stack" to simulate recursion, instead of a recursive
89 // function call This give us greater control, instead of a stack overflow
90 std::stack<StackContents> stack;
91 stack.emplace(StackContents{t0, t1});
92
93 int iterations = 0;
94
95 while (!stack.empty()) {
96 auto current = stack.top();
97 stack.pop();
98
99 auto start = spline.GetPoint(current.t0);
100 if (!start) {
101 throw MalformedSplineException(kMalformedSplineExceptionMsg);
102 }
103
104 auto end = spline.GetPoint(current.t1);
105 if (!end) {
106 throw MalformedSplineException(kMalformedSplineExceptionMsg);
107 }
108
109 const auto twist = start.value().first.Log(end.value().first);
110
111 if (units::math::abs(twist.dy) > kMaxDy ||
112 units::math::abs(twist.dx) > kMaxDx ||
113 units::math::abs(twist.dtheta) > kMaxDtheta) {
114 stack.emplace(StackContents{(current.t0 + current.t1) / 2, current.t1});
115 stack.emplace(StackContents{current.t0, (current.t0 + current.t1) / 2});
116 } else {
117 splinePoints.push_back(end.value());
118 }
119
120 if (iterations++ >= kMaxIterations) {
121 throw MalformedSplineException(kMalformedSplineExceptionMsg);
122 }
123 }
124
125 return splinePoints;
126 }
127
128 private:
129 // Constraints for spline parameterization.
130 static inline constexpr units::meter_t kMaxDx = 5_in;
131 static inline constexpr units::meter_t kMaxDy = 0.05_in;
132 static inline constexpr units::radian_t kMaxDtheta = 0.0872_rad;
133
134 struct StackContents {
135 double t0;
136 double t1;
137 };
138
139 /**
140 * A malformed spline does not actually explode the LIFO stack size. Instead,
141 * the stack size stays at a relatively small number (e.g. 30) and never
142 * decreases. Because of this, we must count iterations. Even long, complex
143 * paths don't usually go over 300 iterations, so hitting this maximum should
144 * definitely indicate something has gone wrong.
145 */
146 static constexpr int kMaxIterations = 5000;
147
148 friend class CubicHermiteSplineTest;
149 friend class QuinticHermiteSplineTest;
150};
151} // namespace frc
#define WPILIB_DLLEXPORT
Definition SymbolExports.h:36
Represents a two-dimensional parametric spline that interpolates between two points.
Definition Spline.h:27
std::optional< PoseWithCurvature > GetPoint(double t) const
Gets the pose and curvature at some point t on the spline.
Definition Spline.h:62
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
constexpr UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition math.h:726
Definition CAN.h:11
Definition SplineParameterizer.h:53
MalformedSplineException(const char *what_arg)
Definition SplineParameterizer.h:54