Cheetah Software  1.0
utilities.h File Reference
#include <algorithm>
#include <map>
#include <random>
#include <unordered_map>
#include <vector>
#include "cppTypes.h"
+ Include dependency graph for utilities.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename T >
bool fpEqual (T a, T b, T tol)
 
template<typename T >
bool vectorEqual (const std::vector< T > &a, const std::vector< T > &b)
 
template<typename T >
coerce (T in, T min, T max)
 
template<typename T >
deadband (T x, T range)
 
template<typename T >
void eigenDeadband (Eigen::MatrixBase< T > &v, typename T::Scalar band)
 
template<typename T >
int sgn (T val)
 
template<typename T >
void fillEigenWithRandom (Eigen::MatrixBase< T > &v, std::mt19937 &gen, std::uniform_real_distribution< typename T::Scalar > &dist)
 
template<typename T >
generator_gaussian_noise (T mean, T var)
 
template<typename T1 , typename T2 >
bool uMapContains (const std::unordered_map< T1, T2 > &set, T1 key)
 
template<typename T1 , typename T2 >
bool mapContains (const std::map< T1, T2 > &set, T1 key)
 
template<typename T >
std::string numberToString (T number)
 
template<typename T >
mapToRange (T x, T inputMin, T inputMax, T outputMin, T outputMax)
 
template<typename T >
std::string eigenToString (Eigen::MatrixBase< T > &value)
 
static std::string boolToString (bool b)
 
void writeStringToFile (const std::string &fileName, const std::string &fileData)
 
std::string getCurrentTimeAndDate ()
 
std::string getConfigDirectoryPath ()
 
template<typename T >
void EulerZYX_2_SO3 (const Vec3< T > &euler_zyx, Mat3< T > &SO3)
 
template<typename T >
smooth_change (T ini, T end, T moving_duration, T curr_time)
 
template<typename T >
smooth_change_vel (T ini, T end, T moving_duration, T curr_time)
 
template<typename T >
smooth_change_acc (T ini, T end, T moving_duration, T curr_time)
 
template<typename T >
stringToNumber (const std::string &str)
 
template<typename T >
stringToNumber (const char *str)
 
template<typename T >
Vec3< T > stringToVec3 (const std::string &str)
 
std::string getLcmUrl (s64 ttl)
 

Function Documentation

static std::string boolToString ( bool  b)
inlinestatic

Definition at line 154 of file utilities.h.

References getConfigDirectoryPath(), getCurrentTimeAndDate(), and writeStringToFile().

154  {
155  return std::string(b ? "true" : "false");
156 }

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename T >
T coerce ( in,
min,
max 
)

Coerce in to be between min and max

Definition at line 35 of file utilities.h.

35  {
36  if (in < min) {
37  in = min;
38  }
39  if (in > max) {
40  in = max;
41  }
42  return in;
43 }

+ Here is the caller graph for this function:

template<typename T >
T deadband ( x,
range 
)

Definition at line 46 of file utilities.h.

46  {
47  if (x < range && x > -range) x = T(0);
48  return x;
49 }

+ Here is the caller graph for this function:

template<typename T >
void eigenDeadband ( Eigen::MatrixBase< T > &  v,
typename T::Scalar  band 
)

Definition at line 52 of file utilities.h.

References deadband().

52  {
53  for (size_t i = 0; i < T::RowsAtCompileTime; i++) {
54  for (size_t j = 0; j < T::ColsAtCompileTime; j++) {
55  v(i, j) = deadband(v(i, j), band);
56  }
57  }
58 }
T deadband(T x, T range)
Definition: utilities.h:46

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename T >
std::string eigenToString ( Eigen::MatrixBase< T > &  value)

Definition at line 148 of file utilities.h.

148  {
149  std::stringstream ss;
150  ss << value;
151  return ss.str();
152 }

+ Here is the caller graph for this function:

template<typename T >
void EulerZYX_2_SO3 ( const Vec3< T > &  euler_zyx,
Mat3< T > &  SO3 
)

Get the rotation matrix coincide with euler angle Intrisic ZYX rotation

Definition at line 168 of file utilities.h.

168  {
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]);
176 
177  SO3 = Mat3_Z * Mat3_Y * Mat3_X;
178 }
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
template<typename T >
void fillEigenWithRandom ( Eigen::MatrixBase< T > &  v,
std::mt19937 &  gen,
std::uniform_real_distribution< typename T::Scalar > &  dist 
)

Fill an eigen type with random numbers from a random generator and uniform real distribution. TODO: is there a way to make this work nicely with normal distributions too?

Definition at line 75 of file utilities.h.

77  {
78  for (size_t i = 0; i < T::RowsAtCompileTime; i++) {
79  for (size_t j = 0; j < T::ColsAtCompileTime; j++) {
80  v(i, j) = dist(gen);
81  }
82  }
83 }

+ Here is the caller graph for this function:

template<typename T >
bool fpEqual ( a,
b,
tol 
)

Are two floating point values almost equal?

Definition at line 15 of file utilities.h.

15  {
16  return std::abs(a - b) <= tol;
17 }

+ Here is the caller graph for this function:

template<typename T >
T generator_gaussian_noise ( mean,
var 
)

Generate a random number following normal distribution

Definition at line 89 of file utilities.h.

89  {
90  static bool hasSpare = false;
91  static T rand1, rand2;
92 
93  if (hasSpare) {
94  hasSpare = false;
95  return mean + sqrt(var * rand1) * sin(rand2);
96  }
97  hasSpare = true;
98 
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.;
103 
104  // printf("rand: %f, %f\n", rand1, rand2);
105  return mean + sqrt(var * rand1) * cos(rand2);
106 }
std::string getConfigDirectoryPath ( )

Todo: do something better to keep track of where we are relative to the config directory

Definition at line 30 of file utilities.cpp.

30 { return "../config/"; }

+ Here is the caller graph for this function:

std::string getCurrentTimeAndDate ( )

Definition at line 18 of file utilities.cpp.

18  {
19  auto t = std::time(nullptr);
20  auto tm = *std::localtime(&t);
21  std::ostringstream ss;
22  ss << std::put_time(&tm, "%c");
23  return ss.str();
24 }

+ Here is the caller graph for this function:

std::string getLcmUrl ( s64  ttl)

Definition at line 32 of file utilities.cpp.

32  {
33  assert(ttl >= 0 && ttl <= 255);
34  return "udpm://239.255.76.67:7667?ttl=" + std::to_string(ttl);
35 }

+ Here is the caller graph for this function:

template<typename T1 , typename T2 >
bool mapContains ( const std::map< T1, T2 > &  set,
T1  key 
)

Does the unordered map contain the given element?

Definition at line 120 of file utilities.h.

120  {
121  return set.find(key) != set.end();
122 }

+ Here is the caller graph for this function:

template<typename T >
T mapToRange ( x,
inputMin,
inputMax,
outputMin,
outputMax 
)

map value x in (inputMin, inputMax) to (outputMin, outputMax)

Definition at line 142 of file utilities.h.

142  {
143  return outputMin +
144  (x - inputMin) * (outputMax - outputMin) / (inputMax - inputMin);
145 }

+ Here is the caller graph for this function:

template<typename T >
std::string numberToString ( number)

Convert a floating point number to a string. Is preferable over std::to_string because this uses scientific notation and won't truncate small/large numbers.

Definition at line 130 of file utilities.h.

130  {
131  static_assert(std::is_floating_point<T>::value,
132  "numberToString must use a floating point type!");
133  char buffer[100];
134  sprintf(buffer, "%g", number);
135  return std::string(buffer);
136 }

+ Here is the caller graph for this function:

template<typename T >
int sgn ( val)

Get the sign of a number 1 for positive, 0 for 0, -1 for negative...

Definition at line 65 of file utilities.h.

65  {
66  return (T(0) < val) - (val < T(0));
67 }

+ Here is the caller graph for this function:

template<typename T >
T smooth_change ( ini,
end,
moving_duration,
curr_time 
)

Definition at line 182 of file utilities.h.

182  {
183  if (curr_time > moving_duration) {
184  return end;
185  }
186  return (ini +
187  (end - ini) * 0.5 * (1 - cos(curr_time / moving_duration * M_PI)));
188 }
template<typename T >
T smooth_change_acc ( ini,
end,
moving_duration,
curr_time 
)

Definition at line 200 of file utilities.h.

200  {
201  if (curr_time > moving_duration) {
202  return 0.0;
203  }
204  return ((end - ini) * 0.5 * (M_PI / moving_duration) *
205  (M_PI / moving_duration) * cos(curr_time / moving_duration * M_PI));
206 }
template<typename T >
T smooth_change_vel ( ini,
end,
moving_duration,
curr_time 
)

Definition at line 191 of file utilities.h.

191  {
192  if (curr_time > moving_duration) {
193  return 0.0;
194  }
195  return ((end - ini) * 0.5 * (M_PI / moving_duration) *
196  sin(curr_time / moving_duration * M_PI));
197 }
template<typename T >
T stringToNumber ( const std::string &  str)

Definition at line 209 of file utilities.h.

209  {
210  static_assert(std::is_same<T, double>::value || std::is_same<T, float>::value,
211  "stringToNumber only works for double/float");
212 
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);
217  }
218 }
template<typename T >
T stringToNumber ( const char *  str)

Definition at line 221 of file utilities.h.

221  {
222  return stringToNumber<T>(std::string(str));
223 }
template<typename T >
Vec3<T> stringToVec3 ( const std::string &  str)

Definition at line 226 of file utilities.h.

References getLcmUrl().

226  {
227  Vec3<T> v;
228  size_t i = 0;
229 
230  // seek past whitespace
231  while (str.at(i) == ' ') i++;
232 
233  if (str.at(i) == '[') {
234  i++;
235  } else {
236  throw std::runtime_error("stringToVec3 didn't find open bracket");
237  }
238 
239  // seek past whitespace
240  while (str.at(i) == ' ') i++;
241  size_t start = i;
242 
243  // seek to end of first number
244  while (str.at(i) != ',') i++;
245  v[0] = stringToNumber<T>(str.substr(start, i - start));
246  i++;
247 
248  while (str.at(i) == ' ') i++;
249  start = i;
250  while (str.at(i) != ',') i++;
251  v[1] = stringToNumber<T>(str.substr(start, i - start));
252  i++;
253 
254  while (str.at(i) == ' ') i++;
255  start = i;
256  while (str.at(i) != ']') i++;
257  v[2] = stringToNumber<T>(str.substr(start, i - start));
258  return v;
259 }
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26

+ Here is the call graph for this function:

template<typename T1 , typename T2 >
bool uMapContains ( const std::unordered_map< T1, T2 > &  set,
T1  key 
)

Does the unordered map contain the given element?

Definition at line 112 of file utilities.h.

112  {
113  return set.find(key) != set.end();
114 }

+ Here is the caller graph for this function:

template<typename T >
bool vectorEqual ( const std::vector< T > &  a,
const std::vector< T > &  b 
)

Are two std::vectors equal?

Definition at line 23 of file utilities.h.

23  {
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;
27  }
28  return true;
29 }

+ Here is the caller graph for this function:

void writeStringToFile ( const std::string &  fileName,
const std::string &  fileData 
)

Definition at line 7 of file utilities.cpp.

8  {
9  FILE* fp = fopen(fileName.c_str(), "w");
10  if (!fp) {
11  printf("Failed to fopen %s\n", fileName.c_str());
12  throw std::runtime_error("Failed to open file");
13  }
14  fprintf(fp, "%s", fileData.c_str());
15  fclose(fp);
16 }

+ Here is the caller graph for this function: