openrave.org

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.h
Go to the documentation of this file.
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2006-2012 Rosen Diankov <rosen.diankov@gmail.com>
3 //
4 // This file is part of OpenRAVE.
5 // OpenRAVE is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
23 #ifndef OPENRAVE_UTILS_H
24 #define OPENRAVE_UTILS_H
25 
26 #include <openrave/config.h>
27 #include <stdint.h>
28 #include <string>
29 #include <istream>
30 #include <vector>
31 
32 #include <boost/shared_ptr.hpp>
33 #include <boost/weak_ptr.hpp>
34 #include <boost/function.hpp>
35 #include <boost/assert.hpp>
36 
37 #include <time.h>
38 
39 #ifndef _WIN32
40 #if !(defined(CLOCK_GETTIME_FOUND) && (POSIX_TIMERS > 0 || _POSIX_TIMERS > 0))
41 #include <sys/time.h>
42 #endif
43 #else
44 #define WIN32_LEAN_AND_MEAN
45 #include <windows.h>
46 #include <sys/timeb.h> // ftime(), struct timeb
47 inline void usleep(unsigned long microseconds) {
48  Sleep((microseconds+999)/1000);
49 }
50 #endif
51 
52 #include <bitset>
53 
54 namespace OpenRAVE {
55 namespace utils {
56 
57 #ifdef _WIN32
58 inline uint32_t GetMilliTime()
59 {
60  LARGE_INTEGER count, freq;
61  QueryPerformanceCounter(&count);
62  QueryPerformanceFrequency(&freq);
63  return (uint32_t)((count.QuadPart * 1000) / freq.QuadPart);
64 }
65 
66 inline uint64_t GetMicroTime()
67 {
68  LARGE_INTEGER count, freq;
69  QueryPerformanceCounter(&count);
70  QueryPerformanceFrequency(&freq);
71  return (count.QuadPart * 1000000) / freq.QuadPart;
72 }
73 
74 inline uint64_t GetNanoTime()
75 {
76  LARGE_INTEGER count, freq;
77  QueryPerformanceCounter(&count);
78  QueryPerformanceFrequency(&freq);
79  return (count.QuadPart * 1000000000) / freq.QuadPart;
80 }
81 
82 inline static uint64_t GetNanoPerformanceTime() {
83  return GetNanoTime();
84 }
85 
86 #else
87 
88 inline void GetWallTime(uint32_t& sec, uint32_t& nsec)
89 {
90 #if defined(CLOCK_GETTIME_FOUND) && (POSIX_TIMERS > 0 || _POSIX_TIMERS > 0)
91  struct timespec start;
92  clock_gettime(CLOCK_REALTIME, &start);
93  sec = start.tv_sec;
94  nsec = start.tv_nsec;
95 #else
96  struct timeval timeofday;
97  gettimeofday(&timeofday,NULL);
98  sec = timeofday.tv_sec;
99  nsec = timeofday.tv_usec * 1000;
100 #endif
101 }
102 
103 inline uint64_t GetNanoTime()
104 {
105  uint32_t sec,nsec;
106  GetWallTime(sec,nsec);
107  return (uint64_t)sec*1000000000 + (uint64_t)nsec;
108 }
109 
110 inline uint64_t GetMicroTime()
111 {
112  uint32_t sec,nsec;
113  GetWallTime(sec,nsec);
114  return (uint64_t)sec*1000000 + (uint64_t)nsec/1000;
115 }
116 
117 inline uint32_t GetMilliTime()
118 {
119  uint32_t sec,nsec;
120  GetWallTime(sec,nsec);
121  return (uint64_t)sec*1000 + (uint64_t)nsec/1000000;
122 }
123 
124 inline static uint64_t GetNanoPerformanceTime()
125 {
126 #if defined(CLOCK_GETTIME_FOUND) && (POSIX_TIMERS > 0 || _POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
127  struct timespec start;
128  uint32_t sec, nsec;
129  clock_gettime(CLOCK_MONOTONIC, &start);
130  sec = start.tv_sec;
131  nsec = start.tv_nsec;
132  return (uint64_t)sec*1000000000 + (uint64_t)nsec;
133 #else
134  return GetNanoTime();
135 #endif
136 }
137 
138 #endif
139 
141 {
142  void operator()(void const *) const {
143  }
144 };
145 
146 template <class T> boost::shared_ptr<T> sptr_from(boost::weak_ptr<T> const& wpt)
147 {
148  return boost::shared_ptr<T>(wpt); // throws on wpt.expired()
149 }
150 
151 template<typename T>
152 struct index_cmp
153 {
154  index_cmp(const T arr) : arr(arr) {
155  }
156  bool operator()(const size_t a, const size_t b) const
157  {
158  return arr[a] < arr[b];
159  }
160  const T arr;
161 };
162 
166 template<class P>
168 {
169 private:
170  P p_;
171  boost::function<void(void const*)> _deleterfn;
172  boost::function<void()> _postdeleterfn;
173 public:
174  smart_pointer_deleter(P const & p, const boost::function<void(void const*)>& deleterfn, const boost::function<void()>& postdeleterfn = boost::function<void()>()) : p_(p), _deleterfn(deleterfn), _postdeleterfn(postdeleterfn)
175  {
176  }
177 
178  void operator()(void const * x)
179  {
180  if( !!_deleterfn ) {
181  _deleterfn(x);
182  }
183  p_.reset();
184  if( !!_postdeleterfn ) {
185  _postdeleterfn();
186  }
187  }
188 
189  P const & get() const
190  {
191  return p_;
192  }
193 };
194 
196 inline std::string ConvertToLowerCase(const std::string & s)
197 {
198  std::string d = s;
199  std::transform(d.begin(), d.end(), d.begin(), ::tolower);
200  return d;
201 }
202 
219 template<typename C>
220 inline void TokenizeString(std::string const& s, char const* d, C& ret)
221 {
222  C output;
223  std::bitset<255> delims;
224  while( *d ) {
225  unsigned char code = *d++;
226  delims[code] = true;
227  }
228  std::string::const_iterator beg;
229  bool in_token = false;
230  for( std::string::const_iterator it = s.begin(), end = s.end(); it != end; ++it ) {
231  if( delims[*it] ) {
232  if( in_token ) {
233  output.push_back(typename C::value_type(beg, it));
234  in_token = false;
235  }
236  }
237  else if( !in_token ) {
238  beg = it;
239  in_token = true;
240  }
241  }
242  if( in_token ) {
243  output.push_back(typename C::value_type(beg, s.end()));
244  }
245  output.swap(ret);
246 }
247 
251 OPENRAVE_API std::string GetFilenameUntilSeparator(std::istream& sinput, char separator);
252 
256 OPENRAVE_API std::string& SearchAndReplace(std::string& out, const std::string& in, const std::vector< std::pair<std::string, std::string> >& pairs);
257 
259 OPENRAVE_API std::string GetMD5HashString(const std::string& s);
261 OPENRAVE_API std::string GetMD5HashString(const std::vector<uint8_t>& v);
262 
263 template<class T>
264 inline T ClampOnRange(T value, T min, T max)
265 {
266  if (value < min) {
267  return min;
268  }
269  if (value > max) {
270  return max;
271  }
272  return value;
273 }
274 
275 template <typename T>
276 inline T NormalizeCircularAngle(T theta, T min, T max)
277 {
278  if (theta < min) {
279  T range = max-min;
280  BOOST_ASSERT(range>0);
281  theta += range;
282  while (theta < min) {
283  theta += range;
284  }
285  }
286  else if (theta > max) {
287  T range = max-min;
288  BOOST_ASSERT(range>0);
289  theta -= range;
290  while (theta > max) {
291  theta -= range;
292  }
293  }
294  return theta;
295 }
296 
297 template <typename T>
298 inline T SubtractCircularAngle(T f0, T f1)
299 {
300  const T PI = T(3.14159265358979323846);
301  return NormalizeCircularAngle(f0-f1, T(-PI), T(PI));
302 }
303 
304 template <typename T>
305 inline T InterpolateCircularAngle(T start, T end, T fraction, T lowerLimit, T upperLimit)
306 {
307  return NormalizeCircularAngle(start + fraction * SubtractCircularAngle(end,start), lowerLimit, upperLimit);
308 }
309 
310 template <typename T>
311 inline T Sqr(T t) {
312  return t*t;
313 }
314 
316 inline bool IsValidCharInName(char c) {
317  return c < 0 || c >= 33; //isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/';
318 }
319 
321 inline bool IsValidName(const std::string& s) {
322  if( s.size() == 0 ) {
323  return false;
324  }
325  return std::count_if(s.begin(), s.end(), IsValidCharInName) == (int)s.size();
326 }
327 
329 inline std::string ConvertToOpenRAVEName(const std::string& name)
330 {
331  if( IsValidName(name) ) {
332  return name;
333  }
334  std::string newname = name;
335  for(size_t i = 0; i < newname.size(); ++i) {
336  if( !IsValidCharInName(newname[i]) ) {
337  newname[i] = '_';
338  }
339  }
340  return newname;
341 }
342 
343 } // utils
344 } // OpenRAVE
345 
346 #endif