GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
Util.cpp
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #define GRT_DLL_EXPORTS
22 #include "Util.h"
23 #include "TimeStamp.h"
24 
25 namespace GRT {
26 
28  #ifdef GRT_CXX11_ENABLED
29  return true;
30  #else
31  return false;
32  #endif
33 }
34 
35 bool Util::sleep(const unsigned int &numMilliseconds){
36 
37 #if defined( __GRT_WINDOWS_BUILD__ )
38  Sleep( numMilliseconds );
39  return true;
40 #endif
41 
42 #if defined(__GRT_OSX_BUILD__)
43  usleep( numMilliseconds * 1000 );
44  return true;
45 #endif
46 
47 #if defined(__GRT_LINUX_BUILD__)
48  usleep( numMilliseconds * 1000 );
49  return true;
50 #endif
51 
52  return false;
53 }
54 
55 Float Util::scale(const Float &x,const Float &minSource,const Float &maxSource,const Float &minTarget,const Float &maxTarget,const bool constrain){
56  if( constrain ){
57  if( x <= minSource ) return minTarget;
58  if( x >= maxSource ) return maxTarget;
59  }
60  if( minSource == maxSource ) return minTarget;
61  return (((x-minSource)*(maxTarget-minTarget))/(maxSource-minSource))+minTarget;
62 }
63 
64 std::string Util::timeAsString(const bool includeDate){
65  TimeStamp timestamp("now");
66  return timestamp.getTimeStampAsString( includeDate );
67 }
68 
69 std::string Util::intToString(const int &i){
70  std::stringstream s;
71  s << i;
72  return s.str();
73 }
74 
75 std::string Util::intToString(const unsigned int &i){
76  std::stringstream s;
77  s << i;
78  return s.str();
79 }
80 
81 std::string Util::toString(const int &i){
82  std::stringstream s;
83  s << i;
84  return s.str();
85 }
86 
87 std::string Util::toString(const unsigned int &i){
88  std::stringstream s;
89  s << i;
90  return s.str();
91 }
92 
93 std::string Util::toString(const long &i){
94  std::stringstream s;
95  s << i;
96  return s.str();
97 }
98 
99 std::string Util::toString(const unsigned long &i){
100  std::stringstream s;
101  s << i;
102  return s.str();
103 }
104 
105 std::string Util::toString(const unsigned long long &i){
106  std::stringstream s;
107  s << i;
108  return s.str();
109 }
110 
111 std::string Util::toString(const bool &b){
112  return b ? "1" : "0";
113 }
114 
115 std::string Util::toString(const Float &v){
116  std::stringstream s;
117  s << v;
118  return s.str();
119 }
120 
121 std::string Util::toString(const long double &v){
122  std::stringstream s;
123  s << v;
124  return s.str();
125 }
126 
127 std::string Util::toString(const float &v){
128  std::stringstream s;
129  s << v;
130  return s.str();
131 }
132 
133 int Util::stringToInt(const std::string &value){
134  std::stringstream s( value );
135  int i;
136  s >> i;
137  return i;
138 }
139 
140 Float Util::stringToFloat(const std::string &value){
141  std::stringstream s( value );
142  Float d;
143  s >> d;
144  return d;
145 }
146 
147 double Util::stringToDouble(const std::string &value){
148  std::stringstream s( value );
149  double d;
150  s >> d;
151  return d;
152 }
153 
154 bool Util::stringToBool(const std::string &value){
155  if( value == "true" ) return true;
156  if( value == "True" ) return true;
157  if( value == "TRUE" ) return true;
158  if( value == "t" ) return true;
159  if( value == "T" ) return true;
160  if( value == "1" ) return true;
161  return false;
162 }
163 
164 bool Util::stringEndsWith(const std::string &str, const std::string &ending) {
165  if (str.length() >= ending.length()) {
166  return (0 == str.compare (str.length() - ending.length(), ending.length(), ending));
167  } else {
168  return false;
169  }
170 }
171 
172 Float Util::limit(const Float value,const Float minValue,const Float maxValue){
173  if( value <= minValue ) return minValue;
174  if( value >= maxValue ) return maxValue;
175  return value;
176 }
177 
178 Float Util::sum(const VectorFloat &x){
179  Float s = 0;
180  std::size_t N = x.size();
181  for(std::size_t i=0; i<N; i++)
182  s += x[i];
183  return s;
184 }
185 
186 Float Util::dotProduct(const VectorFloat &a,const VectorFloat &b){
187  if( a.size() != b.size() ) return std::numeric_limits< Float >::max();
188  std::size_t N = a.size();
189  Float d = 0;
190  for(std::size_t i=0; i<N; i++){
191  d += a[i]*b[i];
192  }
193  return d;
194 }
195 
197  if( a.size() != b.size() ) return std::numeric_limits< Float >::max();
198  std::size_t N = a.size();
199  Float d = 0;
200  for(std::size_t i=0; i<N; i++){
201  d += (a[i]-b[i])*(a[i]-b[i]);
202  }
203  return sqrt( d );
204 }
205 
207  if( a.size() != b.size() ) return std::numeric_limits< Float >::max();
208  std::size_t N = a.size();
209  Float d = 0;
210  for(std::size_t i=0; i<N; i++){
211  d += (a[i]-b[i])*(a[i]-b[i]);
212  }
213  return d;
214 }
215 
217  if( a.size() != b.size() ) return std::numeric_limits< Float >::max();
218  std::size_t N = a.size();
219  Float d = 0;
220  for(std::size_t i=0; i<N; i++){
221  d += fabs(a[i]-b[i]);
222  }
223  return d;
224 }
225 
227  if( a.size() != b.size() ) return std::numeric_limits< Float >::max();
228  std::size_t N = a.size();
229  Float dotProduct = 0;
230  Float aSum = 0;
231  Float bSum = 0;
232  for(std::size_t i=0; i<N; i++){
233  dotProduct += a[i]*b[i];
234  aSum += a[i]*a[i];
235  bSum += b[i]*b[i];
236  }
237  return dotProduct / sqrt(aSum*bSum);
238 }
239 
240 VectorFloat Util::scale(const VectorFloat &x,const Float minSource,const Float maxSource,const Float minTarget,const Float maxTarget,const bool constrain){
241  std::size_t N = x.size();
242  VectorFloat y(N);
243  for(std::size_t i=0; i<N; i++){
244  y[i] = scale(x[i],minSource,maxSource,minTarget,maxTarget,constrain);
245  }
246  return y;
247 }
248 
250  std::size_t N = x.size();
251  VectorFloat y(N);
252  Float s = 0;
253  for(std::size_t i=0; i<N; i++)
254  s += x[i];
255 
256  if( s != 0 ){
257  for(std::size_t i=0; i<N; i++)
258  y[i] = x[i] / s;
259  }else{
260  for(std::size_t i=0; i<N; i++)
261  y[i] = 0;
262  }
263  return y;
264 }
265 
266 VectorFloat Util::limit(const VectorFloat &x,const Float minValue,const Float maxValue){
267  std::size_t N = x.size();
268  VectorFloat y(N);
269  for(std::size_t i=0; i<N; i++)
270  y[i] = limit(x[i],minValue,maxValue);
271  return y;
272 }
273 
274 Float Util::getMin(const VectorFloat &x){
275  Float min = std::numeric_limits< Float >::max();
276  std::size_t N = x.size();
277  for(std::size_t i=0; i<N; i++){
278  if( x[i] < min ){
279  min = x[i];
280  }
281  }
282  return min;
283 }
284 
285 unsigned int getMinIndex(const VectorFloat &x){
286  unsigned int minIndex = 0;
287  Float min = std::numeric_limits< Float >::max();
288  unsigned int N = (unsigned int)x.size();
289  for(unsigned int i=0; i<N; i++){
290  if( x[i] < min ){
291  min = x[i];
292  minIndex = i;
293  }
294  }
295  return minIndex;
296 }
297 
298 Float Util::getMax(const VectorFloat &x){
299  Float max = std::numeric_limits< Float >::min();
300  std::size_t N = x.size();
301  for(std::size_t i=0; i<N; i++){
302  if( x[i] > max ){
303  max = x[i];
304  }
305  }
306  return max;
307 }
308 
309 unsigned int Util::getMaxIndex(const VectorFloat &x){
310  unsigned int maxIndex = 0;
311  Float max = std::numeric_limits< Float >::min();
312  unsigned int N = (unsigned int)x.size();
313  for(unsigned int i=0; i<N; i++){
314  if( x[i] > max ){
315  max = x[i];
316  maxIndex = i;
317  }
318  }
319  return maxIndex;
320 }
321 
322 unsigned int Util::getMin(const std::vector< unsigned int > &x){
323  unsigned int min = std::numeric_limits< unsigned int >::max();
324  const std::size_t N = x.size();
325  for(std::size_t i=0; i<N; i++){
326  if( x[i] < min ){
327  min = x[i];
328  }
329  }
330  return min;
331 }
332 
333 unsigned int Util::getMax(const std::vector< unsigned int > &x){
334  unsigned int max = std::numeric_limits< unsigned int >::min();
335  const std::size_t N = x.size();
336  for(size_t i=0; i<N; i++){
337  if( x[i] > max ){
338  max = x[i];
339  }
340  }
341  return max;
342 }
343 
344 unsigned int Util::getOS(){
345 #ifdef __GRT_OSX_BUILD__
346  return OS_OSX;
347 #endif
348 
349 #ifdef __GRT_LINUX_BUILD__
350  return OS_LINUX;
351 #endif
352 
353 #ifdef __GRT_WINDOWS_BUILD__
354  return OS_WINDOWS;
355 #endif
356 
357  return OS_UNKNOWN;
358 }
359 
360 void Util::cartToPolar(const Float x,const Float y,Float &r, Float &theta){
361 
362 #ifndef PI
363  Float PI = 3.14159265358979323846;
364 #endif
365 
366 #ifndef TWO_PI
367  Float TWO_PI = 6.28318530718;
368 #endif
369 
370  r = 0;
371  theta = 0;
372 
373  //Compute r
374  r = sqrt( (x*x) + (y*y) );
375 
376  //Compute theta
377  int type = 0;
378  if( x > 0 && y >= 0) type = 1;
379  if( x > 0 && y < 0 ) type = 2;
380  if( x < 0 ) type = 3;
381  if( x==0 && y > 0 ) type = 4;
382  if( x==0 && y < 0 ) type = 5;
383  if( x==0 && y==0 ) type = 6;
384 
385  switch(type){
386  case(1):
387  theta = atan( y/x ) * (180.0/PI);
388  break;
389  case(2):
390  theta = (atan( y/x ) + TWO_PI) * (180.0/PI);
391  break;
392  case(3):
393  theta = (atan( y/x ) + PI) * (180.0/PI);
394  break;
395  case(4):
396  theta = (PI/2.0) * (180.0/PI);
397  break;
398  case(5):
399  theta = ( (3*PI)/2.0 ) * (180.0/PI);
400  break;
401  case(6):
402  theta = 0.0;
403  break;
404  default:
405  theta=0.0;
406  break;
407  }
408 }
409 
410 void Util::polarToCart(const Float r,const Float theta,Float &x, Float &y){
411  x = r * cos(theta);
412  y = r * sin(theta);
413 }
414 
415 bool Util::parseDirectory( const std::string directoryPath, const std::string type, std::vector< std::string > &filenames ){
416 
417  filenames.clear();
418 
419 #if defined( __GRT_WINDOWS_BUILD__ )
420  return false; //Windows not supported yet
421 #endif
422 
423 #if defined( __GRT_OSX_BUILD__ ) || defined( __GRT_LINUX_BUILD__ )
424 
425  std::vector< std::string > types; //Stores the file types
426 
427  //Parse out the types to search for, types should be seperated by |, e.g. .csv|.grt
428  std::string temp = "";
429  unsigned int i = 0;
430  while( i < type.length() ){
431  if( type[i] == '|' ){
432  types.push_back( temp );
433  temp = "";
434  }else{
435  temp += type[i];
436  }
437  i++;
438  }
439  if( temp.length() > 0 ) types.push_back( temp );
440  unsigned int numTypes = (unsigned int)types.size();
441 
442  //Search the directory for the files
443  DIR *dir;
444  struct dirent *ent;
445  bool matchFound = false;
446  std::string f = "";
447  if ((dir = opendir ( directoryPath.c_str() )) != NULL) {
448  while ((ent = readdir (dir)) != NULL) {
449  f = ent->d_name;
450  matchFound = false;
451  for(i=0; i<numTypes; i++){
452  if( f != "." && f != ".." ){
453  if( GRT::Util::stringEndsWith(f,types[i]) || types[i] == ".*" ){
454  matchFound = true;
455  break;
456  }
457  }
458  }
459  if( matchFound ){
460  filenames.push_back( directoryPath + "/" + ent->d_name );
461  }
462  }
463  closedir (dir);
464  } else {
465  //Failed to open directory
466  perror ("");
467  return false;
468  }
469 
470 #endif
471 
472  return true;
473 }
474 
475 } //End of GRT namespace
476 
This file contains the Util class, a wrapper for a number of generic functions that are used througho...
static std::string toString(const int &i)
Definition: Util.cpp:81
static Float manhattanDistance(const VectorFloat &a, const VectorFloat &b)
Definition: Util.cpp:216
static void polarToCart(const Float r, const Float theta, Float &x, Float &y)
Definition: Util.cpp:410
static Float scale(const Float &x, const Float &minSource, const Float &maxSource, const Float &minTarget, const Float &maxTarget, const bool constrain=false)
Definition: Util.cpp:55
static Float stringToFloat(const std::string &s)
Definition: Util.cpp:140
static std::string timeAsString(const bool includeDate=true)
Definition: Util.cpp:64
static double stringToDouble(const std::string &s)
Definition: Util.cpp:147
static Float getMin(const VectorFloat &x)
Definition: Util.cpp:274
static bool sleep(const unsigned int &numMilliseconds)
Definition: Util.cpp:35
static std::string intToString(const int &i)
Definition: Util.cpp:69
static bool getCxx11Enabled()
Definition: Util.cpp:27
static void cartToPolar(const Float x, const Float y, Float &r, Float &theta)
Definition: Util.cpp:360
static bool stringToBool(const std::string &s)
Definition: Util.cpp:154
static VectorFloat normalize(const VectorFloat &x)
Definition: Util.cpp:249
static Float euclideanDistance(const VectorFloat &a, const VectorFloat &b)
Definition: Util.cpp:196
static Float limit(const Float value, const Float minValue, const Float maxValue)
Definition: Util.cpp:172
static unsigned int getOS()
Definition: Util.cpp:344
static bool stringEndsWith(const std::string &str, const std::string &ending)
Definition: Util.cpp:164
static Float getMax(const VectorFloat &x)
Definition: Util.cpp:298
static unsigned int getMaxIndex(const VectorFloat &x)
Definition: Util.cpp:309
static int stringToInt(const std::string &s)
Definition: Util.cpp:133
static Float sum(const VectorFloat &x)
Definition: Util.cpp:178
static Float squaredEuclideanDistance(const VectorFloat &a, const VectorFloat &b)
Definition: Util.cpp:206
static Float cosineDistance(const VectorFloat &a, const VectorFloat &b)
Definition: Util.cpp:226
static bool parseDirectory(const std::string directoryPath, const std::string type, std::vector< std::string > &filenames)
Parses a directory and returns a list of filenames in that directory that match the file type...
Definition: Util.cpp:415
static Float dotProduct(const VectorFloat &a, const VectorFloat &b)
Definition: Util.cpp:186