123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- #ifndef _NUMERICTOOLS_FBASICS_H
- #define _NUMERICTOOLS_FBASICS_H
- /*
- * NICE-Core - efficient algebra and computer vision methods
- * - libfbasics - library of some basic tools
- * See file License for license information.
- */
- #include <cmath>
- #include <math.h>
- #include <stdlib.h>
- #include <limits>
- #include <string>
- #ifdef LIMUN_AIBO_MODE
- // some functions missing in math.h (NOT in linum namespace!!)
- inline float roundf(float x) {
- return floor(x + 0.5);
- }
- inline double round(double x) {
- return floor(x + 0.5);
- }
- inline long double roundl(long double x) {
- return floor(x + 0.5);
- }
- inline long lroundf(float x) {
- return (long) floor(x + 0.5);
- }
- inline long lround(double x) {
- return (long) floor(x + 0.5);
- }
- inline long lroundl(long double x) {
- return (long) floor(x + 0.5);
- }
- #endif // LIMUN_AIBO_MODE
- namespace NICE {
- /**
- * Is a numerical value zero?
- */
- template<class T>
- inline bool isZero(const T& x) {
- return x == (T)0;
- }
- /**
- * Is a numerical value zero up to a tolerance?
- */
- inline bool isZero(float x, float tolerance) {
- return fabs(x) < tolerance;
- }
- /**
- * Is a numerical value zero up to a tolerance?
- */
- inline bool isZero(double x, double tolerance) {
- return fabs(x) < tolerance;
- }
- /**
- * Is a numerical value zero?
- * Specialization for floating point: Zero up to machine precision?
- */
- template<>
- inline bool isZero(const float& x) {
- return isZero(x, std::numeric_limits<float>::epsilon());
- }
- /**
- * Is a numerical value zero?
- * Specialization for floating point: Zero up to machine precision?
- */
- template<>
- inline bool isZero(const double& x) {
- return isZero(x, std::numeric_limits<double>::epsilon());
- }
- /**
- * Is a numerical value almost zero ? :) up to 1e-15
- */
- inline bool almostZero(const double a) {
- return (fabs(a)<1e-15);
- }
- /**
- * Is \c x = \c y up to a tolerance?
- */
- inline bool isEqual(float x, float y, float tolerance) {
- return fabs(x - y) < tolerance;
- }
- /**
- * Is \c x = \c y up to a tolerance?
- */
- inline bool isEqual(double x, double y, double tolerance) {
- return fabs(x - y) < tolerance;
- }
- /**
- * Is \c x = \c y up to machine precision?
- */
- inline bool isEqual(float x, float y) {
- return isEqual(x, y, std::numeric_limits<float>::epsilon());
- }
- /**
- * Is \c x = \c y up to machine precision?
- */
- inline bool isEqual(double x, double y) {
- return isEqual(x, y, std::numeric_limits<double>::epsilon());
- }
- /**
- * Sign of a numerical value?
- * (For floating point values, zero is checked up to machine precision,
- * see isZero()).
- */
- template<class T>
- inline T sign(const T& x) {
- if (isZero(x)) {
- return (T)0;
- } else if (x > (T)0) {
- return (T)1;
- } else {
- return (T)(-1);
- }
- }
- /**
- * Absolute value (as std::abs() or fabs() for floating point values).
- */
- template<class T>
- inline T absolute(const T& x) {
- //return std::abs(x);
- if (x >= T(0)) {
- return x;
- } else {
- return -x;
- }
- }
- /**
- * Absolute value (as fabs()).
- */
- template<>
- inline float absolute(const float& x) {
- return (float) fabs(x);
- }
- /**
- * Absolute value (as fabs()).
- */
- template<>
- inline double absolute(const double& x) {
- return (double) fabs(x);
- }
- /**
- * Impose the sign of a numerical value to the sign of another value.
- * (For floating point values, zero is checked/handled up to machine precision,
- * see isZero()).
- * @param x The input value
- * @param s The value supplying the sign (its absolute value may be arbitrary)
- * @return \c x with the same sign as \c s
- */
- template<class T>
- inline T imposeSign(const T& x, const T& s) {
- return absolute(x) * sign(s);
- }
- /**
- * Square.
- */
- template<class T>
- inline T square(const T& t) {
- return t * t;
- }
- /**
- * Cube.
- */
- template<class T>
- inline T cube(const T& t) {
- return t * t * t;
- }
- /**
- * Cube root: t^(1/3)
- */
- inline double cubeRoot(const double& t) {
- return sign(t) * pow(fabs(t), 1.0 / 3.0);
- }
- /**
- * Check if a floating point value is NaN
- */
- inline bool isNaN(double x) {
- #if (__GNUC__ > 3)
- return std::isnan(x);
- #else
- return x != x;
- #endif
- }
- /**
- * Check if a floating point value is NaN
- */
- inline bool isNaN(float x) {
- #if (__GNUC__ > 3)
- return std::isnan(x);
- #else
- return x != x;
- #endif
- }
- /**
- * Create NaN
- */
- inline double doubleNaN() {
- double zero = 1.0;
- zero -= zero;
- return 0.0 / zero;
- }
- /**
- * Create NaN
- */
- inline float floatNaN() {
- float zero = 1.0f;
- zero -= zero;
- return 0.0f / zero;
- }
- /**
- * Restrict \c value to be within [\c min, \c max].
- */
- template<class T>
- inline const T& limitValue(const T& value, const T& min, const T& max) {
- if (value < min) {
- return min;
- } else if (value > max) {
- return max;
- } else {
- return value;
- }
- }
- /**
- * Degree to radian.
- */
- inline double degreeToRadian(double a) {
- return a * M_PI / 180.0;
- }
- /**
- * Radian to degree.
- */
- inline double radianToDegree(double a) {
- return a / M_PI * 180.0;
- }
- /**
- * Normalize an angle to be between 0 and 2 Pi.
- */
- inline double normalizeAngle(double a) {
- const double TWO_PI = 2.0 * M_PI;
- return a - floor(a / TWO_PI) * TWO_PI;
- }
- /**
- * Initialize random number generator.
- */
- void initRand( bool fixedSeed = false, unsigned int seed = 0 );
- /**
- * A pseudo random number in the range [0,limit), based on \c rand().
- * (Initialize generator with \c initRand()).
- */
- inline int randInt(const int limit) {
- if (limit == 0) {
- return 0;
- } else {
- return rand() % limit;
- }
- }
- /**
- * A pseudo random number in the range [0,1), based on \c rand().
- * (Initialize generator with \c initRand()).
- */
- inline double randDouble() {
- return ((double)rand() / ((double)(RAND_MAX)+1.0));
- }
- /**
- * A pseudo random number in the range [0,limit), based on \c rand().
- * (Initialize generator with \c initRand()).
- */
- inline double randDouble(const double limit) {
- return ((double)rand() / ((double)(RAND_MAX)+1.0)) * limit;
- }
- /**
- * A pseudo random number in the range [0,1), based on \c rand().
- * (Initialize generator with \c initRand()).
- */
- inline float randFloat() {
- return ((float)rand() / ((float)(RAND_MAX)+1.0f));
- }
- /**
- * A pseudo random number in the range [0,limit), based on \c rand().
- * (Initialize generator with \c initRand()).
- */
- inline float randFloat(const float limit) {
- return ((float)rand() / ((float)(RAND_MAX)+1.0f)) * limit;
- }
- /**
- * A pseudo random number generated using a normal distribution
- * with an arbitrary standard deviation \c s and zero mean.
- */
- inline double randGaussDouble ( const double stddev ) {
- // adapted from k_reconstruction (Olaf Kähler)
- double r1, r2, d;
- do {
- r1 = 2.0 * randDouble() - 1.0;
- r2 = 2.0 * randDouble() - 1.0;
- d = r1*r1 + r2*r2;
- } while ((d >= 1.0)||(isZero(d)));
- d = sqrt((-2.0 * log(d))/d);
- double y1 = r1*d;
- // y2 = r2*d; this is another random variable
- return y1*stddev;
- }
- /**
- * If \c x is NaN, return infinity, x otherwise.
- */
- inline double nanToInf(double x) {
- if (isNaN(x)) {
- return std::numeric_limits<double>::infinity();
- } else {
- return x;
- }
- }
- /**
- * Convert string to double, including inf and nan.
- * @param s Input string
- * @return the double represented by s
- * @throw Exception if format not ok
- */
- double stringToDouble(const char* s);
- inline double stringToDouble(std::string s) {
- return stringToDouble(s.c_str());
- }
- /**
- * Read double from istream, including inf and nan.
- *
- * Example1: double x; cin >> x;
- *
- * Example2: double y; readDouble(y);
- *
- * If a correct double can be read from the stream,
- * both examples do the same. In case of inf and nan,
- * only the second version reads correctly.
- * Also: errors are signaled differently.
- *
- * @param s Input string
- * @return the double represented by s
- * @throw Exception if format not ok
- */
- inline double readDouble(std::istream& str) {
- std::string s;
- str >> s;
- return stringToDouble(s);
- }
- /**
- * Convert string to int.
- * @param s Input string
- * @return the int represented by s
- * @throw Exception if format not ok
- */
- long stringToInt(const char* s);
- inline long stringToInt(std::string s) {
- return stringToInt(s.c_str());
- }
- /**
- * Convert an integer into a string.
- */
- std::string intToString(const int i);
- /**
- * Convert a double into a string.
- */
- std::string doubleToString(const double d, const unsigned int digits = 6,
- const bool fixedPoint = false);
- /**
- * Round a floating point value and convert to int.
- */
- inline int roundInt(double d) {
- return int(round(d));
- }
- /**
- * Round a floating point value and convert to int.
- */
- inline int roundInt(float d) {
- return int(roundf(d));
- }
- } // namespace
- #endif // _NUMERICTOOLS_FBASICS_H
|