LRT14  1.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
Util.h
1 #ifndef LRT_UTILS_H_
2 #define LRT_UTILS_H_
3 
4 #include <string>
5 #include <math.h>
6 #include <sstream>
7 
8 #include <Rhesus/Toolkit/Defines.h>
9 
21 class Util
22 {
23 public:
31  template<typename T> static inline T Clamp(T val, T min, T max)
32  {
33  if (val > max)
34  return max;
35  else if (val < min)
36  return min;
37  else
38  return val;
39  }
40 
50  template<typename T> static inline T Rescale(T val, T min0, T max0, T min1, T max1)
51  {
52  if (max0 == min0)
53  return min1;
54  val = Clamp<T> (val, min0, max0);
55  return (val - min0) * (max1 - min1) / (max0 - min0) + min1;
56  }
57 
63  template<typename T> static inline std::string ToString(T val)
64  {
65  std::stringstream ss;
66  ss << val;
67  return ss.str();
68  }
69 
75  template<typename T> static inline int Sign(T val)
76  {
77  if (val > 0)
78  return 1;
79  else if (val < 0)
80  return -1;
81  return 0;
82  }
83 
89  template<typename T> static inline T Abs(T val)
90  {
91  if (val < 0)
92  return -val;
93  return val;
94  }
95 
104  template<typename T> static void MinMaxMean(T val[], int n, T* minOut,
105  T* maxOut, T* meanOut)
106  {
107  if (n == 0)
108  {
109  *minOut = 0;
110  *maxOut = 0;
111  *meanOut = 0;
112  return;
113  }
114 
115  T min = val[0], max = val[0];
116  T mean = 0;
117  for (int i = 0; i < n; ++i)
118  {
119  mean += val[i];
120  if (val[i] < min)
121  min = val[i];
122  if (val[i] > max)
123  max = val[i];
124  }
125  mean /= n;
126 
127  *minOut = min;
128  *maxOut = max;
129  *meanOut = mean;
130  }
131 
138  template<typename T> static inline T AddDeadband(T raw, T deadbandSize)
139  {
140  if (Util::Abs<T>(raw) < deadbandSize)
141  return 0;
142  return Util::Sign<T>(raw) * Util::Rescale<T>(Util::Abs<T>(raw),
143  deadbandSize, 1, 0, 1);
144  }
145 
152  template<typename T>
153  static inline T ValWithAbsMax(T val1, T val2)
154  {
155  if (Util::Abs<T>(val1) > Util::Abs<T>(val2))
156  return val1;
157  return val2;
158  }
159 
166  template<typename T>
167  static inline T Max(T val1, T val2)
168  {
169  if (val1 > val2)
170  return val1;
171  return val2;
172  }
173 
180  template<typename T>
181  static inline T Min(T val1, T val2)
182  {
183  if (val1 < val2)
184  return val1;
185  return val2;
186  }
187 
194  template<typename T>
195  static inline T MinAbs(T val1, T val2)
196  {
197  if (fabs(val1) < fabs(val2))
198  return val1;
199  return val2;
200  }
201 
208  template<typename T>
209  static inline T PowPreseveSign(T val, int power)
210  {
211  T result = pow(val, power);
212  if (Sign<T> (val) == Sign<T> (result))
213  return result;
214  else
215  return -result;
216  }
217 
218  template<typename T>
219  static inline T lexical_cast(std::string value)
220  {
221  T temp;
222  std::stringstream sstream(value);
223  sstream >> std::boolalpha >> temp;
224  return temp;
225  }
226 
227  template<typename T>
228  static inline std::string lexical_cast(T value)
229  {
230  return std::string(static_cast<std::ostringstream*>(&(std::ostringstream() << value))->str());
231  }
232 
233  template<typename T>
234  static inline T swap_endian(T u)
235  {
236  union
237  {
238  T u;
239  unsigned char u8[sizeof(T)];
240  } source, dest;
241 
242  source.u = u;
243 
244  for (size_t k = 0; k < sizeof(T); k++)
245  dest.u8[k] = source.u8[sizeof(T) - k - 1];
246 
247  return dest.u;
248  }
249 
253  template<class A, class B>
254  static bool DeleteMapFirst(std::pair<A, B> x)
255  {
256  if(x.first == NULL)
257  return true;
258 
259  DELETE(x.first);
260 
261  return true;
262  }
263 
267  template<class A, class B>
268  static bool DeleteMapSecond(std::pair<A, B> x)
269  {
270  if(x.second == NULL)
271  return true;
272 
273  DELETE(x.second);
274 
275  return true;
276  }
277 
281  template<class A, class B>
282  static bool DeleteMapBoth(std::pair<A, B> x)
283  {
284  if(x.first != NULL)
285  {
286  DELETE(x.first);
287  }
288 
289  if(x.second != NULL)
290  {
291  DELETE(x.second);
292  }
293 
294  return true;
295  }
296 
297 
301  template<class A>
302  static bool DeleteVector(A x)
303  {
304  if(x == NULL)
305  return true;
306 
307  DELETE(x);
308 
309  return true;
310  }
311 
312  static inline std::string Trim(std::string str)
313  {
314  int startIndex = -1, endIndex = str.size() - 1;
315 
316  // Find first non-whitespace index
317  for (unsigned int i = 0; i < str.size(); i++)
318  {
319  if (!isspace(str[i]))
320  {
321  startIndex = i;
322  break;
323  }
324  }
325 
326  if (startIndex == -1)
327  return ""; // Blank line
328 
329  // Find index of end of non-whitespace
330  for (int i = str.size() - 1; i >= 0; i--)
331  {
332  if (!isspace(str[i]))
333  {
334  endIndex = i;
335  break;
336  }
337  }
338 
339  return str.substr(startIndex, endIndex - startIndex + 1);
340  }
341 
345  static void Die();
346 
351  static void Die(const char* message);
352 
359  static bool Assert(bool test, const char* message);
360 };
361 
362 #endif