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