1 #ifndef PROJECT_UTILITIES_H 2 #define PROJECT_UTILITIES_H 7 #include <unordered_map> 16 return std::abs(a - b) <= tol;
23 bool vectorEqual(
const std::vector<T>& a,
const std::vector<T>& b) {
24 if (a.size() != b.size())
return false;
25 for (
size_t i = 0; i < a.size(); i++) {
26 if (a[i] != b[i])
return false;
47 if (x < range && x > -range) x = T(0);
53 for (
size_t i = 0; i < T::RowsAtCompileTime; i++) {
54 for (
size_t j = 0; j < T::ColsAtCompileTime; j++) {
66 return (T(0) < val) - (val < T(0));
76 Eigen::MatrixBase<T>& v, std::mt19937& gen,
77 std::uniform_real_distribution<typename T::Scalar>& dist) {
78 for (
size_t i = 0; i < T::RowsAtCompileTime; i++) {
79 for (
size_t j = 0; j < T::ColsAtCompileTime; j++) {
90 static bool hasSpare =
false;
91 static T rand1, rand2;
95 return mean + sqrt(var * rand1) * sin(rand2);
99 rand1 = rand() / ((T)RAND_MAX);
100 if (rand1 < 1e-100) rand1 = 1e-100;
101 rand1 = -2 * log(rand1);
102 rand2 = rand() / ((T)RAND_MAX) * M_PI * 2.;
105 return mean + sqrt(var * rand1) * cos(rand2);
111 template <
typename T1,
typename T2>
113 return set.find(key) !=
set.end();
119 template <
typename T1,
typename T2>
121 return set.find(key) !=
set.end();
129 template <
typename T>
131 static_assert(std::is_floating_point<T>::value,
132 "numberToString must use a floating point type!");
134 sprintf(buffer,
"%g", number);
135 return std::string(buffer);
141 template <
typename T>
142 T
mapToRange(T x, T inputMin, T inputMax, T outputMin, T outputMax) {
144 (x - inputMin) * (outputMax - outputMin) / (inputMax - inputMin);
147 template <
typename T>
149 std::stringstream ss;
155 return std::string(b ?
"true" :
"false");
159 const std::string& fileData);
167 template <
typename T>
169 Mat3<T> Mat3_Z, Mat3_Y, Mat3_X;
170 Mat3_Z << cos(euler_zyx[0]), -sin(euler_zyx[0]), 0, sin(euler_zyx[0]),
171 cos(euler_zyx[0]), 0, 0, 0, 1;
172 Mat3_Y << cos(euler_zyx[1]), 0, sin(euler_zyx[1]), 0, 1, 0,
173 -sin(euler_zyx[1]), 0, cos(euler_zyx[1]);
174 Mat3_X << 1, 0, 0, 0, cos(euler_zyx[2]), -sin(euler_zyx[2]), 0,
175 sin(euler_zyx[2]), cos(euler_zyx[2]);
177 SO3 = Mat3_Z * Mat3_Y * Mat3_X;
181 template <
typename T>
183 if (curr_time > moving_duration) {
187 (end - ini) * 0.5 * (1 - cos(curr_time / moving_duration * M_PI)));
190 template <
typename T>
192 if (curr_time > moving_duration) {
195 return ((end - ini) * 0.5 * (M_PI / moving_duration) *
196 sin(curr_time / moving_duration * M_PI));
199 template <
typename T>
201 if (curr_time > moving_duration) {
204 return ((end - ini) * 0.5 * (M_PI / moving_duration) *
205 (M_PI / moving_duration) * cos(curr_time / moving_duration * M_PI));
208 template <
typename T>
210 static_assert(std::is_same<T, double>::value || std::is_same<T, float>::value,
211 "stringToNumber only works for double/float");
213 if (std::is_same<T, double>::value) {
214 return std::stod(str);
215 }
else if (std::is_same<T, float>::value) {
216 return std::stof(str);
220 template <
typename T>
222 return stringToNumber<T>(std::string(str));
225 template <
typename T>
231 while (str.at(i) ==
' ') i++;
233 if (str.at(i) ==
'[') {
236 throw std::runtime_error(
"stringToVec3 didn't find open bracket");
240 while (str.at(i) ==
' ') i++;
244 while (str.at(i) !=
',') i++;
245 v[0] = stringToNumber<T>(str.substr(start, i - start));
248 while (str.at(i) ==
' ') i++;
250 while (str.at(i) !=
',') i++;
251 v[1] = stringToNumber<T>(str.substr(start, i - start));
254 while (str.at(i) ==
' ') i++;
256 while (str.at(i) !=
']') i++;
257 v[2] = stringToNumber<T>(str.substr(start, i - start));
263 #endif // PROJECT_UTILITIES_H T generator_gaussian_noise(T mean, T var)
bool fpEqual(T a, T b, T tol)
std::string getCurrentTimeAndDate()
T smooth_change(T ini, T end, T moving_duration, T curr_time)
typename Eigen::Matrix< T, 3, 3 > Mat3
std::string numberToString(T number)
typename Eigen::Matrix< T, 3, 1 > Vec3
std::string eigenToString(Eigen::MatrixBase< T > &value)
std::string getLcmUrl(s64 ttl)
T coerce(T in, T min, T max)
void writeStringToFile(const std::string &fileName, const std::string &fileData)
T stringToNumber(const std::string &str)
std::string getConfigDirectoryPath()
T smooth_change_acc(T ini, T end, T moving_duration, T curr_time)
Vec3< T > stringToVec3(const std::string &str)
void EulerZYX_2_SO3(const Vec3< T > &euler_zyx, Mat3< T > &SO3)
bool vectorEqual(const std::vector< T > &a, const std::vector< T > &b)
void eigenDeadband(Eigen::MatrixBase< T > &v, typename T::Scalar band)
bool uMapContains(const std::unordered_map< T1, T2 > &set, T1 key)
bool mapContains(const std::map< T1, T2 > &set, T1 key)
void fillEigenWithRandom(Eigen::MatrixBase< T > &v, std::mt19937 &gen, std::uniform_real_distribution< typename T::Scalar > &dist)
T smooth_change_vel(T ini, T end, T moving_duration, T curr_time)
static std::string boolToString(bool b)
T mapToRange(T x, T inputMin, T inputMax, T outputMin, T outputMax)