21 #define GRT_DLL_EXPORTS 23 #include "TimeStamp.h" 28 #ifdef GRT_CXX11_ENABLED 37 #if defined( __GRT_WINDOWS_BUILD__ ) 38 Sleep( numMilliseconds );
42 #if defined(__GRT_OSX_BUILD__) 43 usleep( numMilliseconds * 1000 );
47 #if defined(__GRT_LINUX_BUILD__) 48 usleep( numMilliseconds * 1000 );
55 Float
Util::scale(
const Float &x,
const Float &minSource,
const Float &maxSource,
const Float &minTarget,
const Float &maxTarget,
const bool constrain){
57 if( x <= minSource )
return minTarget;
58 if( x >= maxSource )
return maxTarget;
60 if( minSource == maxSource )
return minTarget;
61 return (((x-minSource)*(maxTarget-minTarget))/(maxSource-minSource))+minTarget;
66 return timestamp.getTimeStampAsString( includeDate );
112 return b ?
"1" :
"0";
134 std::stringstream s( value );
141 std::stringstream s( value );
148 std::stringstream s( value );
155 if( value ==
"true" )
return true;
156 if( value ==
"True" )
return true;
157 if( value ==
"TRUE" )
return true;
158 if( value ==
"t" )
return true;
159 if( value ==
"T" )
return true;
160 if( value ==
"1" )
return true;
165 if (str.length() >= ending.length()) {
166 return (0 == str.compare (str.length() - ending.length(), ending.length(), ending));
172 Float
Util::limit(
const Float value,
const Float minValue,
const Float maxValue){
173 if( value <= minValue )
return minValue;
174 if( value >= maxValue )
return maxValue;
180 std::size_t N = x.size();
181 for(std::size_t i=0; i<N; i++)
187 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
188 std::size_t N = a.size();
190 for(std::size_t i=0; i<N; i++){
197 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
198 std::size_t N = a.size();
200 for(std::size_t i=0; i<N; i++){
201 d += (a[i]-b[i])*(a[i]-b[i]);
207 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
208 std::size_t N = a.size();
210 for(std::size_t i=0; i<N; i++){
211 d += (a[i]-b[i])*(a[i]-b[i]);
217 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
218 std::size_t N = a.size();
220 for(std::size_t i=0; i<N; i++){
221 d += fabs(a[i]-b[i]);
227 if( a.size() != b.size() )
return std::numeric_limits< Float >::max();
228 std::size_t N = a.size();
229 Float dotProduct = 0;
232 for(std::size_t i=0; i<N; i++){
233 dotProduct += a[i]*b[i];
237 return dotProduct / sqrt(aSum*bSum);
241 std::size_t N = x.size();
243 for(std::size_t i=0; i<N; i++){
244 y[i] = scale(x[i],minSource,maxSource,minTarget,maxTarget,constrain);
250 std::size_t N = x.size();
253 for(std::size_t i=0; i<N; i++)
257 for(std::size_t i=0; i<N; i++)
260 for(std::size_t i=0; i<N; i++)
267 std::size_t N = x.size();
269 for(std::size_t i=0; i<N; i++)
270 y[i] = limit(x[i],minValue,maxValue);
275 Float min = std::numeric_limits< Float >::max();
276 std::size_t N = x.size();
277 for(std::size_t i=0; i<N; i++){
286 unsigned int minIndex = 0;
287 Float min = std::numeric_limits< Float >::max();
288 unsigned int N = (
unsigned int)x.size();
289 for(
unsigned int i=0; i<N; i++){
299 Float max = std::numeric_limits< Float >::min();
300 std::size_t N = x.size();
301 for(std::size_t i=0; i<N; i++){
310 unsigned int maxIndex = 0;
311 Float max = std::numeric_limits< Float >::min();
312 unsigned int N = (
unsigned int)x.size();
313 for(
unsigned int i=0; i<N; i++){
323 unsigned int min = std::numeric_limits< unsigned int >::max();
324 const std::size_t N = x.size();
325 for(std::size_t i=0; i<N; i++){
334 unsigned int max = std::numeric_limits< unsigned int >::min();
335 const std::size_t N = x.size();
336 for(
size_t i=0; i<N; i++){
345 #ifdef __GRT_OSX_BUILD__ 349 #ifdef __GRT_LINUX_BUILD__ 353 #ifdef __GRT_WINDOWS_BUILD__ 363 Float PI = 3.14159265358979323846;
367 Float TWO_PI = 6.28318530718;
374 r = sqrt( (x*x) + (y*y) );
378 if( x > 0 && y >= 0) type = 1;
379 if( x > 0 && y < 0 ) type = 2;
380 if( x < 0 ) type = 3;
381 if( x==0 && y > 0 ) type = 4;
382 if( x==0 && y < 0 ) type = 5;
383 if( x==0 && y==0 ) type = 6;
387 theta = atan( y/x ) * (180.0/PI);
390 theta = (atan( y/x ) + TWO_PI) * (180.0/PI);
393 theta = (atan( y/x ) + PI) * (180.0/PI);
396 theta = (PI/2.0) * (180.0/PI);
399 theta = ( (3*PI)/2.0 ) * (180.0/PI);
415 bool Util::parseDirectory(
const std::string directoryPath,
const std::string type, std::vector< std::string > &filenames ){
419 #if defined( __GRT_WINDOWS_BUILD__ ) 423 #if defined( __GRT_OSX_BUILD__ ) || defined( __GRT_LINUX_BUILD__ ) 425 std::vector< std::string > types;
428 std::string temp =
"";
430 while( i < type.length() ){
431 if( type[i] ==
'|' ){
432 types.push_back( temp );
439 if( temp.length() > 0 ) types.push_back( temp );
440 unsigned int numTypes = (
unsigned int)types.size();
445 bool matchFound =
false;
447 if ((dir = opendir ( directoryPath.c_str() )) != NULL) {
448 while ((ent = readdir (dir)) != NULL) {
451 for(i=0; i<numTypes; i++){
452 if( f !=
"." && f !=
".." ){
453 if( GRT::Util::stringEndsWith(f,types[i]) || types[i] ==
".*" ){
460 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 std::string timeAsString(const bool includeDate=true)
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 squaredEuclideanDistance(const VectorFloat &a, const VectorFloat &b)
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)