26 #ifdef GRT_CXX11_ENABLED
35 #if defined( __GRT_WINDOWS_BUILD__ )
36 Sleep( numMilliseconds );
40 #if defined(__GRT_OSX_BUILD__)
41 usleep( numMilliseconds * 1000 );
45 #if defined(__GRT_LINUX_BUILD__)
46 usleep( numMilliseconds * 1000 );
52 Float
Util::scale(
const Float &x,
const Float &minSource,
const Float &maxSource,
const Float &minTarget,
const Float &maxTarget,
const bool constrain){
54 if( x <= minSource )
return minTarget;
55 if( x >= maxSource )
return maxTarget;
57 if( minSource == maxSource )
return minTarget;
58 return (((x-minSource)*(maxTarget-minTarget))/(maxSource-minSource))+minTarget;
104 return b ?
"1" :
"0";
126 std::stringstream s( value );
133 std::stringstream s( value );
140 std::stringstream s( 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;
157 if (str.length() >= ending.length()) {
158 return (0 == str.compare (str.length() - ending.length(), ending.length(), ending));
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;
172 std::size_t N = x.size();
173 for(std::size_t i=0; i<N; i++)
179 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
180 std::size_t N = a.size();
182 for(std::size_t i=0; i<N; i++){
189 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
190 std::size_t N = a.size();
192 for(std::size_t i=0; i<N; i++){
193 d += (a[i]-b[i])*(a[i]-b[i]);
199 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
200 std::size_t N = a.size();
202 for(std::size_t i=0; i<N; i++){
203 d += fabs(a[i]-b[i]);
209 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
210 std::size_t N = a.size();
211 Float dotProduct = 0;
214 for(std::size_t i=0; i<N; i++){
215 dotProduct += a[i]*b[i];
219 return dotProduct / sqrt(aSum*bSum);
223 std::size_t N = x.size();
225 for(std::size_t i=0; i<N; i++){
226 y[i] = scale(x[i],minSource,maxSource,minTarget,maxTarget,constrain);
232 std::size_t N = x.size();
235 for(std::size_t i=0; i<N; i++)
239 for(std::size_t i=0; i<N; i++)
242 for(std::size_t i=0; i<N; i++)
249 std::size_t N = x.size();
251 for(std::size_t i=0; i<N; i++)
252 y[i] = limit(x[i],minValue,maxValue);
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++){
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++){
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++){
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++){
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++){
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++){
327 #ifdef __GRT_OSX_BUILD__
331 #ifdef __GRT_LINUX_BUILD__
335 #ifdef __GRT_WINDOWS_BUILD__
345 Float PI = 3.14159265358979323846;
349 Float TWO_PI = 6.28318530718;
356 r = sqrt( (x*x) + (y*y) );
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;
369 theta = atan( y/x ) * (180.0/PI);
372 theta = (atan( y/x ) + TWO_PI) * (180.0/PI);
375 theta = (atan( y/x ) + PI) * (180.0/PI);
378 theta = (PI/2.0) * (180.0/PI);
381 theta = ( (3*PI)/2.0 ) * (180.0/PI);
397 bool Util::parseDirectory(
const std::string directoryPath,
const std::string type, std::vector< std::string > &filenames ){
401 #if defined( __GRT_WINDOWS_BUILD__ )
405 #if defined( __GRT_OSX_BUILD__ ) || defined( __GRT_LINUX_BUILD__ )
407 std::vector< std::string > types;
410 std::string temp =
"";
412 while( i < type.length() ){
413 if( type[i] ==
'|' ){
414 types.push_back( temp );
421 if( temp.length() > 0 ) types.push_back( temp );
422 unsigned int numTypes = (
unsigned int)types.size();
427 bool matchFound =
false;
429 if ((dir = opendir ( directoryPath.c_str() )) != NULL) {
430 while ((ent = readdir (dir)) != NULL) {
433 for(i=0; i<numTypes; i++){
434 if( f !=
"." && f !=
".." ){
435 if( GRT::Util::stringEndsWith(f,types[i]) || types[i] ==
".*" ){
442 filenames.push_back( directoryPath +
"/" + ent->d_name );
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)
static Float manhattanDistance(const VectorFloat &a, const VectorFloat &b)
static void polarToCart(const Float r, const Float theta, Float &x, Float &y)
static Float scale(const Float &x, const Float &minSource, const Float &maxSource, const Float &minTarget, const Float &maxTarget, const bool constrain=false)
static Float stringToFloat(const std::string &s)
static double stringToDouble(const std::string &s)
static Float getMin(const VectorFloat &x)
static bool sleep(const unsigned int &numMilliseconds)
static std::string intToString(const int &i)
static bool getCxx11Enabled()
static void cartToPolar(const Float x, const Float y, Float &r, Float &theta)
static bool stringToBool(const std::string &s)
static VectorFloat normalize(const VectorFloat &x)
static Float euclideanDistance(const VectorFloat &a, const VectorFloat &b)
static Float limit(const Float value, const Float minValue, const Float maxValue)
static unsigned int getOS()
static bool stringEndsWith(const std::string &str, const std::string &ending)
static Float getMax(const VectorFloat &x)
static unsigned int getMaxIndex(const VectorFloat &x)
static int stringToInt(const std::string &s)
static Float sum(const VectorFloat &x)
static Float cosineDistance(const VectorFloat &a, const VectorFloat &b)
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...
static Float dotProduct(const VectorFloat &a, const VectorFloat &b)