27 #if defined( __GRT_WINDOWS_BUILD__ )
28 Sleep( numMilliseconds );
32 #if defined(__GRT_OSX_BUILD__)
33 usleep( numMilliseconds * 1000 );
37 #if defined(__GRT_LINUX_BUILD__)
38 usleep( numMilliseconds * 1000 );
44 double Util::scale(
const double &x,
const double &minSource,
const double &maxSource,
const double &minTarget,
const double &maxTarget,
const bool constrain){
46 if( x <= minSource )
return minTarget;
47 if( x >= maxSource )
return maxTarget;
49 if( minSource == maxSource )
return minTarget;
50 return (((x-minSource)*(maxTarget-minTarget))/(maxSource-minSource))+minTarget;
118 std::stringstream s( value );
125 std::stringstream s( 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;
142 if (str.length() >= ending.length()) {
143 return (0 == str.compare (str.length() - ending.length(), ending.length(), ending));
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;
157 std::size_t N = x.size();
158 for(std::size_t i=0; i<N; i++)
164 if( a.size() != b.size() )
return std::numeric_limits< double >::max();
165 std::size_t N = a.size();
167 for(std::size_t i=0; i<N; i++){
174 if( a.size() != b.size() )
return std::numeric_limits< double >::max();
175 std::size_t N = a.size();
177 for(std::size_t i=0; i<N; i++){
178 d += (a[i]-b[i])*(a[i]-b[i]);
184 if( a.size() != b.size() )
return std::numeric_limits< double >::max();
185 std::size_t N = a.size();
187 for(std::size_t i=0; i<N; i++){
188 d += fabs(a[i]-b[i]);
194 if( a.size() != b.size() )
return std::numeric_limits< double >::max();
195 std::size_t N = a.size();
199 for(std::size_t i=0; i<N; i++){
200 dotProduct += a[i]*b[i];
204 return dotProduct / sqrt(aSum*bSum);
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);
217 std::size_t N = x.size();
218 std::vector<double> y(N);
220 for(std::size_t i=0; i<N; i++)
224 for(std::size_t i=0; i<N; i++)
227 for(std::size_t i=0; i<N; i++)
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);
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++){
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++){
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++){
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++){
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++){
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++){
312 #ifdef __GRT_OSX_BUILD__
316 #ifdef __GRT_LINUX_BUILD__
320 #ifdef __GRT_WINDOWS_BUILD__
330 double PI = 3.14159265358979323846;
334 double TWO_PI = 6.28318530718;
341 r = sqrt( (x*x) + (y*y) );
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;
354 theta = atan( y/x ) * (180.0/PI);
357 theta = (atan( y/x ) + TWO_PI) * (180.0/PI);
360 theta = (atan( y/x ) + PI) * (180.0/PI);
363 theta = (PI/2.0) * (180.0/PI);
366 theta = ( (3*PI)/2.0 ) * (180.0/PI);
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)
static std::string toString(const int &i)
static double dotProduct(const std::vector< double > &a, const std::vector< double > &b)
static double manhattanDistance(const std::vector< double > &a, const std::vector< double > &b)
static unsigned int getMaxIndex(const std::vector< double > &x)
static bool stringToBool(const std::string &s)
static double scale(const double &x, const double &minSource, const double &maxSource, const double &minTarget, const double &maxTarget, const bool constrain=false)
static std::string intToString(const int &i)
static double getMin(const std::vector< double > &x)
static bool sleep(const unsigned int &numMilliseconds)
static double stringToDouble(const std::string &s)
static double euclideanDistance(const std::vector< double > &a, const std::vector< double > &b)
static void cartToPolar(const double x, const double y, double &r, double &theta)
static int stringToInt(const std::string &s)
static void polarToCart(const double r, const double theta, double &x, double &y)
static bool stringEndsWith(const std::string &str, const std::string &ending)
static double getMax(const std::vector< double > &x)
static double sum(const std::vector< double > &x)
static unsigned int getOS()
static double limit(const double value, const double minValue, const double maxValue)
static std::vector< double > normalize(const std::vector< double > &x)