#include #include #include #include /* Math.h Written by: Michael Götting (2006) University of Bielefeld AG Neuroinformatik mgoettin@techfak.uni-bielefeld.de */ #ifndef ICL_MATH_H #define ICL_MATH_H namespace icl { /* {{{ random functions */ //-------------------------------------------------------------------------- /*! @brief Initilaize the random number generator. @param seedval The seed value @return - @sa void randomSeed() */ inline void randomSeed(long int seedval) { #ifdef WIN32 srand(seedval); #else srand48(seedval); #endif } //-------------------------------------------------------------------------- /*! @brief Initilaize the random number generator. @sa void randomSeed(long int) */ inline void randomSeed() {randomSeed(Time::now().toMicroSeconds());} struct MathematicsRandomSeedInitializer{ inline MathematicsRandomSeedInitializer(){ randomSeed(); } }; //-------------------------------------------------------------------------- /*! @brief Generates random numbers between 0 and 1. @param - @return random number @sa double random() */ inline double random() { #ifdef WIN32 // this generates quite poor random numbers, because RAND_MAX = 32767 return static_cast(rand()) / (1.0 + static_cast(RAND_MAX)); #else return drand48(); #endif } //-------------------------------------------------------------------------- /*! @brief Generate a random number to an upper limit. @param max a float argument. The upper limit for the returned number @return - @sa float gaussRandom(float), void random(vector, float), void random(float), void gaussRandom(vector, float); */ inline double random(double max) { FUNCTION_LOG("float"); return max * random(); } //-------------------------------------------------------------------------- /*! @brief Generate a random number between a lower and upper limit. @param min a float argument. The lower intervall limit @param max a float argument. The upper interval limit @return - @sa float gaussRandom(float), void random(vector, float), float random(float), void gaussRandom(vector, float); */ inline double random(double min, double max) { FUNCTION_LOG("float, float"); return ((max - min) * random() + min); } //-------------------------------------------------------------------------- /*! @brief Creates a non-negative random number to an upper limit max @param max The upper limit for the returned number @return The random number */ inline unsigned int random(unsigned int max) { FUNCTION_LOG("unsigned int"); unsigned int val = static_cast(floor(random (static_cast(max)+1.0))); return std::min(val, max); } /// fill an image with uniform distributed random values in the given range /** @param poImage image to fill with random values (NULL is not allowed) @param range for the random value @param roiOnly decides whether to apply the operation on the whole image or on its ROI only **/ void random(ImgBase *poImage, const Range &range=Range(0,255), bool roiOnly=true); /// fill an image with gauss-distributed random values with given mean, variance and min/max value /** @param poImage image to fill with random values (NULL is not allowed) @param mean mean value for all gauss distributed random variables @param var variance for all gauss distributed random variables @param minAndMax clipping range for all variables @param roiOnly decides whether to apply the operation on the whole image or on its ROI only **/ void gaussRandom(ImgBase *poImage, double mean, double var, const Range &minAndMax, bool roiOnly=true); /// Generate a gaussian random number with given mean and variance /** @param mean mode of the gaussian @param var variance of the gaussian @return gaussian distributed variable @sa double(double,double,const Range&), **/ double gaussRandom(double mean, double var); /// Generate a gaussian random number with given mean and variance and clips the result to a range /** @param mean mode of the gaussian @param var variance of the gaussian @param range clipping range for the returned value @return gaussian distributed variable clipped to range range @sa double(double,double,const Range&), **/ inline double gaussRandom(double mean, double var, const Range &range){ return icl::clip( gaussRandom(mean,var), range.minVal, range.maxVal); } //-------------------------------------------------------------------------- /*! @brief Initialize the elements of a std::vector by values of an generator function taking no arguments, e.g. random() */ template void initVector(std::vector &vec, double (*gen)()) { FUNCTION_LOG("vector &, Generator"); std::generate (vec.begin(), vec.end(), gen); } /*! @brief Initialize the elements of a std::vector by values of an generator function taking one argument, e.g. randomGauss(max) */ template void initVector(std::vector &vec, double (*f)(double), double arg) { FUNCTION_LOG("vector &, Function, arg"); for (typename std::vector::iterator it=vec.begin(), end=vec.end(); it != end; ++it) { *it = f(arg); } } /* }}} */ /* {{{ distance functions */ /*! @brief Calculate the euclidian distance of two vectors v1 and v2 @param v1Begin first element of v1 @param v1End end of v1 (points the first element behind v1) @param v2Begin first element of v2 @return The euclidian distance |v1-v2| */ template float euclidian(ForwardIterator v1Begin, ForwardIterator v1End, ForwardIterator v2Begin) { float fSum = 0.0, fDiff; for (; v1Begin != v1End; ++v1Begin, ++v2Begin) { fDiff = (*v1Begin-*v2Begin); fSum += fDiff*fDiff; } return sqrt(fSum); } /*! @brief Calculate the euclidian distance of points a and b @param a The first point @param b The second point @return The distance of point a and b */ template float euclidian(const std::vector &a, const std::vector &b) { ICLASSERT_RETURN_VAL(a.size() == b.size(), float(0)); return euclidian (a.begin(), a.end(), b.begin()); } /* }}} */ /* {{{ statistic functions */ /* {{{ mean */ //-------------------------------------------------------------------------- /*! @brief Compute the mean value from a vector. @param iDim an in argument. The dimension of the destination vector @param ptData The data vector @return The mean value form the vector */ template float mean(const T *ptData, int iDim); //-------------------------------------------------------------------------- /*! @brief Compute the mean value from a vector. @param vecData The data vector @return The mean value form the vector */ template float mean(const std::vector &vecData); //-------------------------------------------------------------------------- /*! @brief Compute the mean value from a vector. @param poImg The data Image @param iChannel The number of channels @return The mean value form the vector */ template std::vector mean(const Img *poImg, int iChannel=-1); //-------------------------------------------------------------------------- /*! @brief Compute the mean value from a vector. @param poImg The data Image @param iChannel The number of channels @return The mean value form the vector */ std::vector mean(const ImgBase *poImg, int iChannel=-1); /* }}} */ /* {{{ variance */ //-------------------------------------------------------------------------- /*! @brief Compute the variance value from a vector. @param ptData The data vector @param iDim an in argument. The dimension of the destination vector @return The variance value form the vector */ template float variance(const T *ptData, int iDim); //-------------------------------------------------------------------------- /*! @brief Compute the variance value from a vector. @param vecData The data vector @return The variance value form the vector */ template float variance(const std::vector &vecData); //-------------------------------------------------------------------------- /*! @brief Compute the variance value from a vector. @param poImg The data vector @param iChannel The number of channels @return The variance value form the vector */ template std::vector variance(const Img *poImg, int iChannel=-1); //-------------------------------------------------------------------------- /*! @brief Compute the variance value from a vector. @param poImg The data vector @param iChannel The number of channels @return The variance value form the vector */ std::vector variance(const ImgBase *poImg, int iChannel=-1); /* }}} */ /* {{{ deviation */ //-------------------------------------------------------------------------- /*! @brief Compute the deviation value from a vector. @param ptData The data vector @param iDim an in argument. The dimension of the destination vector @return The deviation value form the vector */ template float deviation(const T *ptData, int iDim); //-------------------------------------------------------------------------- /*! @brief Compute the deviation value from a vector. @param vecData The data vector @return The deviation value form the vector */ template float deviation(const std::vector &vecData); //-------------------------------------------------------------------------- /*! @brief Compute the deviation value from a vector. @param poImg The data vector @param iChannel The number of channels @return The deviation value form the vector */ template std::vector deviation(const Img *poImg, int iChannel=-1); //-------------------------------------------------------------------------- /*! @brief Compute the deviation value from a vector. @param poImg The data vector @param iChannel The number of channels @return The deviation value form the vector */ std::vector deviation(const ImgBase *poImg, int iChannel=-1); /* }}} */ /* }}} */ } //namespace icl #endif