17 #ifndef _GAZEBO_MATH_FUNCTIONS_HH_ 18 #define _GAZEBO_MATH_FUNCTIONS_HH_ 21 #include <boost/math/special_functions/fpclassify.hpp> 22 #include <boost/math/special_functions/round.hpp> 32 #define GZ_DBL_MAX std::numeric_limits<double>::max() 35 #define GZ_DBL_MIN std::numeric_limits<double>::min() 38 #define GZ_DBL_INF std::numeric_limits<double>::infinity() 41 #define GZ_FLT_MAX std::numeric_limits<float>::max() 44 #define GZ_FLT_MIN std::numeric_limits<float>::min() 47 #define GZ_UINT32_MAX std::numeric_limits<uint32_t>::max() 50 #define GZ_UINT32_MIN std::numeric_limits<uint32_t>::min() 53 #define GZ_INT32_MAX std::numeric_limits<int32_t>::max() 56 #define GZ_INT32_MIN std::numeric_limits<int32_t>::min() 69 static const double NAN_D = std::numeric_limits<double>::quiet_NaN();
72 static const int NAN_I = std::numeric_limits<int>::quiet_NaN();
79 inline T
clamp(T _v, T _min, T _max)
105 return isnan(_v) || std::isinf(_v) ? 0.0f : _v;
113 return isnan(_v) || std::isinf(_v) ? 0.0 : _v;
120 inline T
mean(
const std::vector<T> &_values)
123 for (
unsigned int i = 0; i < _values.size(); ++i)
125 return sum / _values.size();
134 T avg = mean<T>(_values);
137 for (
unsigned int i = 0; i < _values.size(); ++i)
138 sum += (_values[i] - avg) * (_values[i] - avg);
139 return sum / _values.size();
146 inline T
max(
const std::vector<T> &_values)
149 for (
unsigned int i = 0; i < _values.size(); ++i)
150 if (_values[i] > max)
159 inline T
min(
const std::vector<T> &_values)
162 for (
unsigned int i = 0; i < _values.size(); ++i)
163 if (_values[i] < min)
173 inline bool equal(
const T &_a,
const T &_b,
174 const T &_epsilon = 1e-6)
176 return std::fabs(_a - _b) <= _epsilon;
184 inline T
precision(
const T &_a,
const unsigned int &_precision)
188 return boost::math::round(
189 _a * pow(10, _precision)) / pow(10, _precision);
202 return ((_x != 0) && ((_x & (~_x + 1)) == _x));
218 while (_x & (_x - 1))
231 const char *p = _input.c_str();
232 if (!*p || *p ==
'?')
246 while (*p >=
'0' && *p <=
'9')
247 acc = acc * 10 + *p++ -
'0';
251 std::cerr <<
"Invalid int numeric format[" << _input <<
"]\n";
264 const char *p = _input.c_str();
265 if (!*p || *p ==
'?')
278 while (*p >=
'0' && *p <=
'9')
279 acc = acc * 10 + *p++ -
'0';
285 while (*p >=
'0' && *p <=
'9')
287 acc += (*p++ -
'0') * k;
306 while (*p >=
'0' && *p <=
'9')
307 f = f * 10 + *p++ -
'0';
309 acc *= pow(10, f*es);
314 std::cerr <<
"Invalid double numeric format[" << _input <<
"]\n";
unsigned int roundUpPowerOfTwo(unsigned int _x)
Get the smallest power of two that is greater or equal to a given value.
Definition: Helpers.hh:210
Forward declarations for the common classes.
Definition: Animation.hh:33
int parseInt(const std::string &_input)
parse string into an integer
Definition: Helpers.hh:229
static const int NAN_I
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:72
double parseFloat(const std::string &_input)
parse string into float
Definition: Helpers.hh:262
bool isPowerOfTwo(unsigned int _x)
is this a power of 2?
Definition: Helpers.hh:200
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:159
static const double NAN_D
Returns the representation of a quiet not a number (NAN)
Definition: Helpers.hh:69
bool isnan(float _v)
check if a float is NaN
Definition: Helpers.hh:87
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:79
T variance(const std::vector< T > &_values)
get variance of vector of values
Definition: Helpers.hh:132
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6)
check if two values are equal, within a tolerance
Definition: Helpers.hh:173
T mean(const std::vector< T > &_values)
get mean of vector of values
Definition: Helpers.hh:120
float fixnan(float _v)
Fix a nan value.
Definition: Helpers.hh:103
bool isnan(double _v)
check if a double is NaN
Definition: Helpers.hh:95
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:184
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:146