GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
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 using namespace GRT;
24 
25 bool Util::sleep(const unsigned int &numMilliseconds){
26 
27 #if defined( __GRT_WINDOWS_BUILD__ )
28  Sleep( numMilliseconds );
29  return true;
30 #endif
31 
32 #if defined(__GRT_OSX_BUILD__)
33  usleep( numMilliseconds * 1000 );
34  return true;
35 #endif
36 
37 #if defined(__GRT_LINUX_BUILD__)
38  usleep( numMilliseconds * 1000 );
39  return true;
40 #endif
41 
42 }
43 
44 double Util::scale(const double &x,const double &minSource,const double &maxSource,const double &minTarget,const double &maxTarget,const bool constrain){
45  if( constrain ){
46  if( x <= minSource ) return minTarget;
47  if( x >= maxSource ) return maxTarget;
48  }
49  if( minSource == maxSource ) return minTarget;
50  return (((x-minSource)*(maxTarget-minTarget))/(maxSource-minSource))+minTarget;
51 }
52 
53 std::string Util::intToString(const int &i){
54  std::stringstream s;
55  s << i;
56  return s.str();
57 }
58 
59 std::string Util::intToString(const unsigned int &i){
60  std::stringstream s;
61  s << i;
62  return s.str();
63 }
64 
65 std::string Util::toString(const int &i){
66  std::stringstream s;
67  s << i;
68  return s.str();
69 }
70 
71 std::string Util::toString(const unsigned int &i){
72  std::stringstream s;
73  s << i;
74  return s.str();
75 }
76 
77 std::string Util::toString(const long &i){
78  std::stringstream s;
79  s << i;
80  return s.str();
81 }
82 
83 std::string Util::toString(const unsigned long &i){
84  std::stringstream s;
85  s << i;
86  return s.str();
87 }
88 
89 std::string Util::toString(const unsigned long long &i){
90  std::stringstream s;
91  s << i;
92  return s.str();
93 }
94 
95 std::string Util::toString(const bool &b){
96  return b ? "1" : "0";
97 }
98 
99 std::string Util::toString(const double &v){
100  std::stringstream s;
101  s << v;
102  return s.str();
103 }
104 
105 std::string Util::toString(const long double &v){
106  std::stringstream s;
107  s << v;
108  return s.str();
109 }
110 
111 std::string Util::toString(const float &v){
112  std::stringstream s;
113  s << v;
114  return s.str();
115 }
116 
117 int Util::stringToInt(const std::string &value){
118  std::stringstream s( value );
119  int i;
120  s >> i;
121  return i;
122 }
123 
124 double Util::stringToDouble(const std::string &value){
125  std::stringstream s( value );
126  double d;
127  s >> d;
128  return d;
129 }
130 
131 bool Util::stringToBool(const std::string &value){
132  if( value == "true" ) return true;
133  if( value == "True" ) return true;
134  if( value == "TRUE" ) return true;
135  if( value == "t" ) return true;
136  if( value == "T" ) return true;
137  if( value == "1" ) return true;
138  return false;
139 }
140 
141 bool Util::stringEndsWith(const std::string &str, const std::string &ending) {
142  if (str.length() >= ending.length()) {
143  return (0 == str.compare (str.length() - ending.length(), ending.length(), ending));
144  } else {
145  return false;
146  }
147 }
148 
149 double Util::limit(const double value,const double minValue,const double maxValue){
150  if( value <= minValue ) return minValue;
151  if( value >= maxValue ) return maxValue;
152  return value;
153 }
154 
155 double Util::sum(const std::vector< double > &x){
156  double s = 0;
157  std::size_t N = x.size();
158  for(std::size_t i=0; i<N; i++)
159  s += x[i];
160  return s;
161 }
162 
163 double Util::dotProduct(const std::vector<double> &a,const std::vector<double> &b){
164  if( a.size() != b.size() ) return std::numeric_limits< double >::max();
165  std::size_t N = a.size();
166  double d = 0;
167  for(std::size_t i=0; i<N; i++){
168  d += a[i]*b[i];
169  }
170  return d;
171 }
172 
173 double Util::euclideanDistance(const std::vector<double> &a,const std::vector<double> &b){
174  if( a.size() != b.size() ) return std::numeric_limits< double >::max();
175  std::size_t N = a.size();
176  double d = 0;
177  for(std::size_t i=0; i<N; i++){
178  d += (a[i]-b[i])*(a[i]-b[i]);
179  }
180  return sqrt( d );
181 }
182 
183 double Util::manhattanDistance(const std::vector<double> &a,const std::vector<double> &b){
184  if( a.size() != b.size() ) return std::numeric_limits< double >::max();
185  std::size_t N = a.size();
186  double d = 0;
187  for(std::size_t i=0; i<N; i++){
188  d += fabs(a[i]-b[i]);
189  }
190  return d;
191 }
192 
193 double Util::cosineDistance(const std::vector<double> &a,const std::vector<double> &b){
194  if( a.size() != b.size() ) return std::numeric_limits< double >::max();
195  std::size_t N = a.size();
196  double dotProduct = 0;
197  double aSum = 0;
198  double bSum = 0;
199  for(std::size_t i=0; i<N; i++){
200  dotProduct += a[i]*b[i];
201  aSum += a[i]*a[i];
202  bSum += b[i]*b[i];
203  }
204  return dotProduct / sqrt(aSum*bSum);
205 }
206 
207 std::vector<double> Util::scale(const std::vector<double> &x,const double minSource,const double maxSource,const double minTarget,const double maxTarget,const bool constrain){
208  std::size_t N = x.size();
209  std::vector<double> y(N);
210  for(std::size_t i=0; i<N; i++){
211  y[i] = scale(x[i],minSource,maxSource,minTarget,maxTarget,constrain);
212  }
213  return y;
214 }
215 
216 std::vector<double> Util::normalize(const std::vector<double> &x){
217  std::size_t N = x.size();
218  std::vector<double> y(N);
219  double s = 0;
220  for(std::size_t i=0; i<N; i++)
221  s += x[i];
222 
223  if( s != 0 ){
224  for(std::size_t i=0; i<N; i++)
225  y[i] = x[i] / s;
226  }else{
227  for(std::size_t i=0; i<N; i++)
228  y[i] = 0;
229  }
230  return y;
231 }
232 
233 std::vector<double> Util::limit(const std::vector<double> &x,const double minValue,const double maxValue){
234  std::size_t N = x.size();
235  std::vector<double> y(N);
236  for(std::size_t i=0; i<N; i++)
237  y[i] = limit(x[i],minValue,maxValue);
238  return y;
239 }
240 
241 double Util::getMin(const std::vector< double > &x){
242  double min = std::numeric_limits< double >::max();
243  std::size_t N = x.size();
244  for(std::size_t i=0; i<N; i++){
245  if( x[i] < min ){
246  min = x[i];
247  }
248  }
249  return min;
250 }
251 
252 unsigned int getMinIndex(const std::vector< double > &x){
253  unsigned int minIndex = 0;
254  double min = std::numeric_limits< double >::max();
255  unsigned int N = (unsigned int)x.size();
256  for(unsigned int i=0; i<N; i++){
257  if( x[i] < min ){
258  min = x[i];
259  minIndex = i;
260  }
261  }
262  return minIndex;
263 }
264 
265 double Util::getMax(const std::vector< double > &x){
266  double max = std::numeric_limits< double >::min();
267  std::size_t N = x.size();
268  for(std::size_t i=0; i<N; i++){
269  if( x[i] > max ){
270  max = x[i];
271  }
272  }
273  return max;
274 }
275 
276 unsigned int Util::getMaxIndex(const std::vector< double > &x){
277  unsigned int maxIndex = 0;
278  double max = std::numeric_limits< double >::min();
279  unsigned int N = (unsigned int)x.size();
280  for(unsigned int i=0; i<N; i++){
281  if( x[i] > max ){
282  max = x[i];
283  maxIndex = i;
284  }
285  }
286  return maxIndex;
287 }
288 
289 unsigned int Util::getMin(const std::vector< unsigned int > &x){
290  unsigned int min = std::numeric_limits< unsigned int >::max();
291  const std::size_t N = x.size();
292  for(std::size_t i=0; i<N; i++){
293  if( x[i] < min ){
294  min = x[i];
295  }
296  }
297  return min;
298 }
299 
300 unsigned int Util::getMax(const std::vector< unsigned int > &x){
301  unsigned int max = std::numeric_limits< unsigned int >::min();
302  const std::size_t N = x.size();
303  for(size_t i=0; i<N; i++){
304  if( x[i] > max ){
305  max = x[i];
306  }
307  }
308  return max;
309 }
310 
311 unsigned int Util::getOS(){
312  #ifdef __GRT_OSX_BUILD__
313  return OS_OSX;
314  #endif
315 
316  #ifdef __GRT_LINUX_BUILD__
317  return OS_LINUX;
318  #endif
319 
320  #ifdef __GRT_WINDOWS_BUILD__
321  return OS_WINDOWS;
322  #endif
323 
324  return OS_UNKNOWN;
325 }
326 
327 void Util::cartToPolar(const double x,const double y,double &r, double &theta){
328 
329 #ifndef PI
330  double PI = 3.14159265358979323846;
331 #endif
332 
333 #ifndef TWO_PI
334  double TWO_PI = 6.28318530718;
335 #endif
336 
337  r = 0;
338  theta = 0;
339 
340  //Compute r
341  r = sqrt( (x*x) + (y*y) );
342 
343  //Compute theta
344  int type = 0;
345  if( x > 0 && y >= 0) type = 1;
346  if( x > 0 && y < 0 ) type = 2;
347  if( x < 0 ) type = 3;
348  if( x==0 && y > 0 ) type = 4;
349  if( x==0 && y < 0 ) type = 5;
350  if( x==0 && y==0 ) type = 6;
351 
352  switch(type){
353  case(1):
354  theta = atan( y/x ) * (180.0/PI);
355  break;
356  case(2):
357  theta = (atan( y/x ) + TWO_PI) * (180.0/PI);
358  break;
359  case(3):
360  theta = (atan( y/x ) + PI) * (180.0/PI);
361  break;
362  case(4):
363  theta = (PI/2.0) * (180.0/PI);
364  break;
365  case(5):
366  theta = ( (3*PI)/2.0 ) * (180.0/PI);
367  break;
368  case(6):
369  theta = 0.0;
370  break;
371  default:
372  theta=0.0;
373  break;
374  }
375 }
376 
377 void Util::polarToCart(const double r,const double theta,double &x, double &y){
378  x = r * cos(theta);
379  y = r * sin(theta);
380 }
This file contains the Util class, a wrapper for a number of generic functions that are used througho...
static double cosineDistance(const std::vector< double > &a, const std::vector< double > &b)
Definition: Util.cpp:193
static std::string toString(const int &i)
Definition: Util.cpp:65
static double dotProduct(const std::vector< double > &a, const std::vector< double > &b)
Definition: Util.cpp:163
static double manhattanDistance(const std::vector< double > &a, const std::vector< double > &b)
Definition: Util.cpp:183
static unsigned int getMaxIndex(const std::vector< double > &x)
Definition: Util.cpp:276
static bool stringToBool(const std::string &s)
Definition: Util.cpp:131
Definition: AdaBoost.cpp:25
static double scale(const double &x, const double &minSource, const double &maxSource, const double &minTarget, const double &maxTarget, const bool constrain=false)
Definition: Util.cpp:44
static std::string intToString(const int &i)
Definition: Util.cpp:53
static double getMin(const std::vector< double > &x)
Definition: Util.cpp:241
static bool sleep(const unsigned int &numMilliseconds)
Definition: Util.cpp:25
static double stringToDouble(const std::string &s)
Definition: Util.cpp:124
static double euclideanDistance(const std::vector< double > &a, const std::vector< double > &b)
Definition: Util.cpp:173
static void cartToPolar(const double x, const double y, double &r, double &theta)
Definition: Util.cpp:327
static int stringToInt(const std::string &s)
Definition: Util.cpp:117
static void polarToCart(const double r, const double theta, double &x, double &y)
Definition: Util.cpp:377
static bool stringEndsWith(const std::string &str, const std::string &ending)
Definition: Util.cpp:141
static double getMax(const std::vector< double > &x)
Definition: Util.cpp:265
static double sum(const std::vector< double > &x)
Definition: Util.cpp:155
static unsigned int getOS()
Definition: Util.cpp:311
static double limit(const double value, const double minValue, const double maxValue)
Definition: Util.cpp:149
static std::vector< double > normalize(const std::vector< double > &x)
Definition: Util.cpp:216