Skip to content

Commit a5ddf2d

Browse files
authored
[trajoptlib] Clean up trajopt utilities (#925)
1 parent b91f25d commit a5ddf2d

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed

trajoptlib/include/trajopt/util/TrajoptUtil.hpp

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <cmath>
66
#include <numbers>
7+
#include <numeric>
78
#include <vector>
89

910
namespace trajopt {
@@ -20,61 +21,91 @@ namespace trajopt {
2021
*/
2122
inline 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+
*/
70101
inline 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

trajoptlib/test/src/util/TrajoptUtilTest.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@
66
#include <trajopt/util/TrajoptUtil.hpp>
77

88
TEST_CASE("TrajoptUtil - GetIndex()", "[TrajoptUtil]") {
9-
auto result0 = trajopt::GetIndex({2, 3}, 0, 0);
10-
auto result1 = trajopt::GetIndex({2, 3}, 1, 1);
11-
auto result2 = trajopt::GetIndex({2, 3}, 1, 2);
12-
auto result3 = trajopt::GetIndex({2, 3}, 2, 0);
13-
CHECK(result0 == 0);
14-
CHECK(result1 == 3);
15-
CHECK(result2 == 4);
16-
CHECK(result3 == 5);
9+
CHECK(trajopt::GetIndex({2, 3}, 0, 0) == 0);
10+
CHECK(trajopt::GetIndex({2, 3}, 1, 1) == 3);
11+
CHECK(trajopt::GetIndex({2, 3}, 1, 2) == 4);
12+
CHECK(trajopt::GetIndex({2, 3}, 2, 0) == 5);
1713
}
1814

1915
TEST_CASE("TrajoptUtil - Linspace()", "[TrajoptUtil]") {
20-
auto result = trajopt::Linspace(0.0, 2.0, 2);
21-
std::vector correct{1.0, 2.0};
22-
CHECK(result == correct);
16+
CHECK(trajopt::Linspace(0.0, 2.0, 2) == std::vector{1.0, 2.0});
2317
}

0 commit comments

Comments
 (0)