21 #define GRT_DLL_EXPORTS
27 #ifdef GRT_CXX11_ENABLED
36 #if defined( __GRT_WINDOWS_BUILD__ )
37 Sleep( numMilliseconds );
41 #if defined(__GRT_OSX_BUILD__)
42 usleep( numMilliseconds * 1000 );
46 #if defined(__GRT_LINUX_BUILD__)
47 usleep( numMilliseconds * 1000 );
53 Float
Util::scale(
const Float &x,
const Float &minSource,
const Float &maxSource,
const Float &minTarget,
const Float &maxTarget,
const bool constrain){
55 if( x <= minSource )
return minTarget;
56 if( x >= maxSource )
return maxTarget;
58 if( minSource == maxSource )
return minTarget;
59 return (((x-minSource)*(maxTarget-minTarget))/(maxSource-minSource))+minTarget;
105 return b ?
"1" :
"0";
127 std::stringstream s( value );
134 std::stringstream s( value );
141 std::stringstream s( 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;
158 if (str.length() >= ending.length()) {
159 return (0 == str.compare (str.length() - ending.length(), ending.length(), ending));
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;
173 std::size_t N = x.size();
174 for(std::size_t i=0; i<N; i++)
180 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
181 std::size_t N = a.size();
183 for(std::size_t i=0; i<N; i++){
190 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
191 std::size_t N = a.size();
193 for(std::size_t i=0; i<N; i++){
194 d += (a[i]-b[i])*(a[i]-b[i]);
200 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
201 std::size_t N = a.size();
203 for(std::size_t i=0; i<N; i++){
204 d += fabs(a[i]-b[i]);
210 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
211 std::size_t N = a.size();
212 Float dotProduct = 0;
215 for(std::size_t i=0; i<N; i++){
216 dotProduct += a[i]*b[i];
220 return dotProduct / sqrt(aSum*bSum);
224 std::size_t N = x.size();
226 for(std::size_t i=0; i<N; i++){
227 y[i] = scale(x[i],minSource,maxSource,minTarget,maxTarget,constrain);
233 std::size_t N = x.size();
236 for(std::size_t i=0; i<N; i++)
240 for(std::size_t i=0; i<N; i++)
243 for(std::size_t i=0; i<N; i++)
250 std::size_t N = x.size();
252 for(std::size_t i=0; i<N; i++)
253 y[i] = limit(x[i],minValue,maxValue);
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++){
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++){
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++){
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++){
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++){
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++){
328 #ifdef __GRT_OSX_BUILD__
332 #ifdef __GRT_LINUX_BUILD__
336 #ifdef __GRT_WINDOWS_BUILD__
346 Float PI = 3.14159265358979323846;
350 Float TWO_PI = 6.28318530718;
357 r = sqrt( (x*x) + (y*y) );
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;
370 theta = atan( y/x ) * (180.0/PI);
373 theta = (atan( y/x ) + TWO_PI) * (180.0/PI);
376 theta = (atan( y/x ) + PI) * (180.0/PI);
379 theta = (PI/2.0) * (180.0/PI);
382 theta = ( (3*PI)/2.0 ) * (180.0/PI);
398 bool Util::parseDirectory(
const std::string directoryPath,
const std::string type, std::vector< std::string > &filenames ){
402 #if defined( __GRT_WINDOWS_BUILD__ )
406 #if defined( __GRT_OSX_BUILD__ ) || defined( __GRT_LINUX_BUILD__ )
408 std::vector< std::string > types;
411 std::string temp =
"";
413 while( i < type.length() ){
414 if( type[i] ==
'|' ){
415 types.push_back( temp );
422 if( temp.length() > 0 ) types.push_back( temp );
423 unsigned int numTypes = (
unsigned int)types.size();
428 bool matchFound =
false;
430 if ((dir = opendir ( directoryPath.c_str() )) != NULL) {
431 while ((ent = readdir (dir)) != NULL) {
434 for(i=0; i<numTypes; i++){
435 if( f !=
"." && f !=
".." ){
436 if( GRT::Util::stringEndsWith(f,types[i]) || types[i] ==
".*" ){
443 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)