GestureRecognitionToolkit  Version: 0.1.0
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 #include "Util.h"
22 
23 namespace GRT {
24 
26  #ifdef GRT_CXX11_ENABLED
27  return true;
28  #else
29  return false;
30  #endif
31 }
32 
33 bool Util::sleep(const unsigned int &numMilliseconds){
34 
35 #if defined( __GRT_WINDOWS_BUILD__ )
36  Sleep( numMilliseconds );
37  return true;
38 #endif
39 
40 #if defined(__GRT_OSX_BUILD__)
41  usleep( numMilliseconds * 1000 );
42  return true;
43 #endif
44 
45 #if defined(__GRT_LINUX_BUILD__)
46  usleep( numMilliseconds * 1000 );
47  return true;
48 #endif
49 
50 }
51 
52 Float Util::scale(const Float &x,const Float &minSource,const Float &maxSource,const Float &minTarget,const Float &maxTarget,const bool constrain){
53  if( constrain ){
54  if( x <= minSource ) return minTarget;
55  if( x >= maxSource ) return maxTarget;
56  }
57  if( minSource == maxSource ) return minTarget;
58  return (((x-minSource)*(maxTarget-minTarget))/(maxSource-minSource))+minTarget;
59 }
60 
61 std::string Util::intToString(const int &i){
62  std::stringstream s;
63  s << i;
64  return s.str();
65 }
66 
67 std::string Util::intToString(const unsigned int &i){
68  std::stringstream s;
69  s << i;
70  return s.str();
71 }
72 
73 std::string Util::toString(const int &i){
74  std::stringstream s;
75  s << i;
76  return s.str();
77 }
78 
79 std::string Util::toString(const unsigned int &i){
80  std::stringstream s;
81  s << i;
82  return s.str();
83 }
84 
85 std::string Util::toString(const long &i){
86  std::stringstream s;
87  s << i;
88  return s.str();
89 }
90 
91 std::string Util::toString(const unsigned long &i){
92  std::stringstream s;
93  s << i;
94  return s.str();
95 }
96 
97 std::string Util::toString(const unsigned long long &i){
98  std::stringstream s;
99  s << i;
100  return s.str();
101 }
102 
103 std::string Util::toString(const bool &b){
104  return b ? "1" : "0";
105 }
106 
107 std::string Util::toString(const Float &v){
108  std::stringstream s;
109  s << v;
110  return s.str();
111 }
112 
113 std::string Util::toString(const long double &v){
114  std::stringstream s;
115  s << v;
116  return s.str();
117 }
118 
119 std::string Util::toString(const float &v){
120  std::stringstream s;
121  s << v;
122  return s.str();
123 }
124 
125 int Util::stringToInt(const std::string &value){
126  std::stringstream s( value );
127  int i;
128  s >> i;
129  return i;
130 }
131 
132 Float Util::stringToFloat(const std::string &value){
133  std::stringstream s( value );
134  Float d;
135  s >> d;
136  return d;
137 }
138 
139 double Util::stringToDouble(const std::string &value){
140  std::stringstream s( value );
141  double d;
142  s >> d;
143  return d;
144 }
145 
146 bool Util::stringToBool(const std::string &value){
147  if( value == "true" ) return true;
148  if( value == "True" ) return true;
149  if( value == "TRUE" ) return true;
150  if( value == "t" ) return true;
151  if( value == "T" ) return true;
152  if( value == "1" ) return true;
153  return false;
154 }
155 
156 bool Util::stringEndsWith(const std::string &str, const std::string &ending) {
157  if (str.length() >= ending.length()) {
158  return (0 == str.compare (str.length() - ending.length(), ending.length(), ending));
159  } else {
160  return false;
161  }
162 }
163 
164 Float Util::limit(const Float value,const Float minValue,const Float maxValue){
165  if( value <= minValue ) return minValue;
166  if( value >= maxValue ) return maxValue;
167  return value;
168 }
169 
170 Float Util::sum(const VectorFloat &x){
171  Float s = 0;
172  std::size_t N = x.size();
173  for(std::size_t i=0; i<N; i++)
174  s += x[i];
175  return s;
176 }
177 
178 Float Util::dotProduct(const VectorFloat &a,const VectorFloat &b){
179  if( a.size() != b.size() ) return std::numeric_limits< Float >::max();
180  std::size_t N = a.size();
181  Float d = 0;
182  for(std::size_t i=0; i<N; i++){
183  d += a[i]*b[i];
184  }
185  return d;
186 }
187 
189  if( a.size() != b.size() ) return std::numeric_limits< Float >::max();
190  std::size_t N = a.size();
191  Float d = 0;
192  for(std::size_t i=0; i<N; i++){
193  d += (a[i]-b[i])*(a[i]-b[i]);
194  }
195  return sqrt( d );
196 }
197 
199  if( a.size() != b.size() ) return std::numeric_limits< Float >::max();
200  std::size_t N = a.size();
201  Float d = 0;
202  for(std::size_t i=0; i<N; i++){
203  d += fabs(a[i]-b[i]);
204  }
205  return d;
206 }
207 
209  if( a.size() != b.size() ) return std::numeric_limits< Float >::max();
210  std::size_t N = a.size();
211  Float dotProduct = 0;
212  Float aSum = 0;
213  Float bSum = 0;
214  for(std::size_t i=0; i<N; i++){
215  dotProduct += a[i]*b[i];
216  aSum += a[i]*a[i];
217  bSum += b[i]*b[i];
218  }
219  return dotProduct / sqrt(aSum*bSum);
220 }
221 
222 VectorFloat Util::scale(const VectorFloat &x,const Float minSource,const Float maxSource,const Float minTarget,const Float maxTarget,const bool constrain){
223  std::size_t N = x.size();
224  VectorFloat y(N);
225  for(std::size_t i=0; i<N; i++){
226  y[i] = scale(x[i],minSource,maxSource,minTarget,maxTarget,constrain);
227  }
228  return y;
229 }
230 
232  std::size_t N = x.size();
233  VectorFloat y(N);
234  Float s = 0;
235  for(std::size_t i=0; i<N; i++)
236  s += x[i];
237 
238  if( s != 0 ){
239  for(std::size_t i=0; i<N; i++)
240  y[i] = x[i] / s;
241  }else{
242  for(std::size_t i=0; i<N; i++)
243  y[i] = 0;
244  }
245  return y;
246 }
247 
248 VectorFloat Util::limit(const VectorFloat &x,const Float minValue,const Float maxValue){
249  std::size_t N = x.size();
250  VectorFloat y(N);
251  for(std::size_t i=0; i<N; i++)
252  y[i] = limit(x[i],minValue,maxValue);
253  return y;
254 }
255 
256 Float Util::getMin(const VectorFloat &x){
257  Float min = std::numeric_limits< Float >::max();
258  std::size_t N = x.size();
259  for(std::size_t i=0; i<N; i++){
260  if( x[i] < min ){
261  min = x[i];
262  }
263  }
264  return min;
265 }
266 
267 unsigned int getMinIndex(const VectorFloat &x){
268  unsigned int minIndex = 0;
269  Float min = std::numeric_limits< Float >::max();
270  unsigned int N = (unsigned int)x.size();
271  for(unsigned int i=0; i<N; i++){
272  if( x[i] < min ){
273  min = x[i];
274  minIndex = i;
275  }
276  }
277  return minIndex;
278 }
279 
280 Float Util::getMax(const VectorFloat &x){
281  Float max = std::numeric_limits< Float >::min();
282  std::size_t N = x.size();
283  for(std::size_t i=0; i<N; i++){
284  if( x[i] > max ){
285  max = x[i];
286  }
287  }
288  return max;
289 }
290 
291 unsigned int Util::getMaxIndex(const VectorFloat &x){
292  unsigned int maxIndex = 0;
293  Float max = std::numeric_limits< Float >::min();
294  unsigned int N = (unsigned int)x.size();
295  for(unsigned int i=0; i<N; i++){
296  if( x[i] > max ){
297  max = x[i];
298  maxIndex = i;
299  }
300  }
301  return maxIndex;
302 }
303 
304 unsigned int Util::getMin(const std::vector< unsigned int > &x){
305  unsigned int min = std::numeric_limits< unsigned int >::max();
306  const std::size_t N = x.size();
307  for(std::size_t i=0; i<N; i++){
308  if( x[i] < min ){
309  min = x[i];
310  }
311  }
312  return min;
313 }
314 
315 unsigned int Util::getMax(const std::vector< unsigned int > &x){
316  unsigned int max = std::numeric_limits< unsigned int >::min();
317  const std::size_t N = x.size();
318  for(size_t i=0; i<N; i++){
319  if( x[i] > max ){
320  max = x[i];
321  }
322  }
323  return max;
324 }
325 
326 unsigned int Util::getOS(){
327 #ifdef __GRT_OSX_BUILD__
328  return OS_OSX;
329 #endif
330 
331 #ifdef __GRT_LINUX_BUILD__
332  return OS_LINUX;
333 #endif
334 
335 #ifdef __GRT_WINDOWS_BUILD__
336  return OS_WINDOWS;
337 #endif
338 
339  return OS_UNKNOWN;
340 }
341 
342 void Util::cartToPolar(const Float x,const Float y,Float &r, Float &theta){
343 
344 #ifndef PI
345  Float PI = 3.14159265358979323846;
346 #endif
347 
348 #ifndef TWO_PI
349  Float TWO_PI = 6.28318530718;
350 #endif
351 
352  r = 0;
353  theta = 0;
354 
355  //Compute r
356  r = sqrt( (x*x) + (y*y) );
357 
358  //Compute theta
359  int type = 0;
360  if( x > 0 && y >= 0) type = 1;
361  if( x > 0 && y < 0 ) type = 2;
362  if( x < 0 ) type = 3;
363  if( x==0 && y > 0 ) type = 4;
364  if( x==0 && y < 0 ) type = 5;
365  if( x==0 && y==0 ) type = 6;
366 
367  switch(type){
368  case(1):
369  theta = atan( y/x ) * (180.0/PI);
370  break;
371  case(2):
372  theta = (atan( y/x ) + TWO_PI) * (180.0/PI);
373  break;
374  case(3):
375  theta = (atan( y/x ) + PI) * (180.0/PI);
376  break;
377  case(4):
378  theta = (PI/2.0) * (180.0/PI);
379  break;
380  case(5):
381  theta = ( (3*PI)/2.0 ) * (180.0/PI);
382  break;
383  case(6):
384  theta = 0.0;
385  break;
386  default:
387  theta=0.0;
388  break;
389  }
390 }
391 
392 void Util::polarToCart(const Float r,const Float theta,Float &x, Float &y){
393  x = r * cos(theta);
394  y = r * sin(theta);
395 }
396 
397 bool Util::parseDirectory( const std::string directoryPath, const std::string type, std::vector< std::string > &filenames ){
398 
399  filenames.clear();
400 
401 #if defined( __GRT_WINDOWS_BUILD__ )
402  return false; //Windows not supported yet
403 #endif
404 
405 #if defined( __GRT_OSX_BUILD__ ) || defined( __GRT_LINUX_BUILD__ )
406 
407  std::vector< std::string > types; //Stores the file types
408 
409  //Parse out the types to search for, types should be seperated by |, e.g. .csv|.grt
410  std::string temp = "";
411  unsigned int i = 0;
412  while( i < type.length() ){
413  if( type[i] == '|' ){
414  types.push_back( temp );
415  temp = "";
416  }else{
417  temp += type[i];
418  }
419  i++;
420  }
421  if( temp.length() > 0 ) types.push_back( temp );
422  unsigned int numTypes = (unsigned int)types.size();
423 
424  //Search the directory for the files
425  DIR *dir;
426  struct dirent *ent;
427  bool matchFound = false;
428  std::string f = "";
429  if ((dir = opendir ( directoryPath.c_str() )) != NULL) {
430  while ((ent = readdir (dir)) != NULL) {
431  f = ent->d_name;
432  matchFound = false;
433  for(i=0; i<numTypes; i++){
434  if( f != "." && f != ".." ){
435  if( GRT::Util::stringEndsWith(f,types[i]) || types[i] == ".*" ){
436  matchFound = true;
437  break;
438  }
439  }
440  }
441  if( matchFound ){
442  filenames.push_back( directoryPath + "/" + ent->d_name );
443  }
444  }
445  closedir (dir);
446  } else {
447  //Failed to open directory
448  perror ("");
449  return false;
450  }
451 
452 #endif
453 
454  return true;
455 }
456 
457 } //End of GRT namespace
458 
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:73
static Float manhattanDistance(const VectorFloat &a, const VectorFloat &b)
Definition: Util.cpp:198
static void polarToCart(const Float r, const Float theta, Float &x, Float &y)
Definition: Util.cpp:392
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:52
static Float stringToFloat(const std::string &s)
Definition: Util.cpp:132
Definition: DebugLog.cpp:23
static double stringToDouble(const std::string &s)
Definition: Util.cpp:139
static Float getMin(const VectorFloat &x)
Definition: Util.cpp:256
static bool sleep(const unsigned int &numMilliseconds)
Definition: Util.cpp:33
static std::string intToString(const int &i)
Definition: Util.cpp:61
static bool getCxx11Enabled()
Definition: Util.cpp:25
static void cartToPolar(const Float x, const Float y, Float &r, Float &theta)
Definition: Util.cpp:342
static bool stringToBool(const std::string &s)
Definition: Util.cpp:146
static VectorFloat normalize(const VectorFloat &x)
Definition: Util.cpp:231
static Float euclideanDistance(const VectorFloat &a, const VectorFloat &b)
Definition: Util.cpp:188
static Float limit(const Float value, const Float minValue, const Float maxValue)
Definition: Util.cpp:164
static unsigned int getOS()
Definition: Util.cpp:326
static bool stringEndsWith(const std::string &str, const std::string &ending)
Definition: Util.cpp:156
static Float getMax(const VectorFloat &x)
Definition: Util.cpp:280
static unsigned int getMaxIndex(const VectorFloat &x)
Definition: Util.cpp:291
static int stringToInt(const std::string &s)
Definition: Util.cpp:125
static Float sum(const VectorFloat &x)
Definition: Util.cpp:170
static Float cosineDistance(const VectorFloat &a, const VectorFloat &b)
Definition: Util.cpp:208
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:397
static Float dotProduct(const VectorFloat &a, const VectorFloat &b)
Definition: Util.cpp:178