44
55#include < cmath>
66#include < numbers>
7+ #include < numeric>
78#include < vector>
89
910namespace trajopt {
@@ -20,61 +21,91 @@ namespace trajopt {
2021 */
2122inline size_t GetIndex (const std::vector<size_t >& N, size_t wptIndex,
2223 size_t sampleIndex = 0 ) {
23- size_t index = 0 ;
24-
25- for (size_t _wptIndex = 0 ; _wptIndex < wptIndex; ++_wptIndex) {
26- index += N.at (_wptIndex);
27- }
28- index += sampleIndex;
29- return index;
24+ return std::accumulate (N.begin (), N.begin () + wptIndex, size_t {0 }) +
25+ sampleIndex;
3026}
3127
32- inline std::vector<double > Linspace (double startValue, double endValue,
28+ /* *
29+ * Returns a vector of linearly spaced elements between start exclusive and end
30+ * inclusive.
31+ *
32+ * @param start The initial value exclusive.
33+ * @param end The final value exclusive.
34+ * @param numSamples The number of samples in the vector.
35+ * @return A vector of linearly spaced elements between start exclusive and end
36+ * inclusive.
37+ */
38+ inline std::vector<double > Linspace (double start, double end,
3339 size_t numSamples) {
3440 std::vector<double > result;
35- double delta = (endValue - startValue ) / numSamples;
36- for (size_t index = 1 ; index <= numSamples; ++index ) {
37- result.push_back (startValue + index * delta);
41+ double delta = (end - start ) / numSamples;
42+ for (size_t i = 1 ; i <= numSamples; ++i ) {
43+ result.push_back (start + i * delta);
3844 }
3945 return result;
4046}
4147
42- inline double InputModulus (double input, double max, double min) {
43- const double modulus = max - min;
48+ /* *
49+ * Returns modulus of input.
50+ *
51+ * @param input Input value to wrap.
52+ * @param minimumInput The minimum value expected from the input.
53+ * @param maximumInput The maximum value expected from the input.
54+ */
55+ constexpr double InputModulus (double input, double max, double min) {
56+ double modulus = max - min;
4457
4558 // Wrap input if it's above the maximum input
46- const double numMax = std::trunc (( input - min) / modulus) ;
59+ int numMax = ( input - min) / modulus;
4760 input -= numMax * modulus;
4861
4962 // Wrap input if it's below the minimum input
50- const double numMin = std::trunc (( input - max) / modulus) ;
63+ int numMin = ( input - max) / modulus;
5164 input -= numMin * modulus;
5265
5366 return input;
5467}
5568
56- inline double AngleModulus (double radians) {
57- return InputModulus (radians, std::numbers::pi, -std::numbers::pi);
69+ /* *
70+ * Wraps an angle to the range -π to π radians (-180 to 180 degrees).
71+ *
72+ * @param angle Angle to wrap in radians.
73+ */
74+ constexpr double AngleModulus (double angle) {
75+ return InputModulus (angle, std::numbers::pi, -std::numbers::pi);
5876}
5977
60- inline std::vector<double > AngleLinspace (double startValue, double endValue,
78+ /* *
79+ * Returns a vector of linearly spaced angles between start exclusive and end
80+ * inclusive.
81+ *
82+ * @param start The initial value exclusive.
83+ * @param end The final value exclusive.
84+ * @param numSamples The number of samples in the vector.
85+ * @return A vector of linearly spaced elements between start exclusive and end
86+ * inclusive.
87+ */
88+ inline std::vector<double > AngleLinspace (double start, double end,
6189 size_t numSamples) {
62- auto diff = endValue - startValue;
63-
64- // angleModulus
65- diff = AngleModulus (diff);
66-
67- return Linspace (startValue, startValue + diff, numSamples);
90+ return Linspace (start, start + AngleModulus (end - start), numSamples);
6891}
6992
93+ /* *
94+ * Returns the time a trapezoid profile takes to travel a given distance from
95+ * rest to rest.
96+ *
97+ * @param distance The distance to travel.
98+ * @param velocity The profile's maximum velocity.
99+ * @param acceleration The profile's maximum acceleration.
100+ */
70101inline double CalculateTrapezoidalTime (double distance, double velocity,
71102 double acceleration) {
72103 if (distance > ((velocity * velocity) / acceleration)) {
73- // trapezoid
104+ // Velocity profile is shaped like a trapezoid
74105 return distance / velocity + velocity / acceleration;
75106 } else {
76- // triangle
77- return 2.0 * ( std::sqrt (distance * acceleration) / acceleration) ;
107+ // Velocity profile is shaped like a triangle
108+ return 2.0 * std::sqrt (distance * acceleration) / acceleration;
78109 }
79110}
80111
0 commit comments