CVR-Lib last update 20 Sep 2009

cvrMath.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2004
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  *
00006  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00007  *
00008  * The CVR-Lib is free software; you can redistribute it and/or
00009  * modify it under the terms of the BSD License.
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  *
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  *
00023  * 3. Neither the name of the authors nor the names of its contributors may be
00024  *    used to endorse or promote products derived from this software without
00025  *    specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  */
00039 
00040 
00041 
00042 /**
00043  * \file   cvrMath.h
00044  *         Definition of some usually used global functions like min, max,
00045  *         and many standard functions with are used differently by
00046  *         the different compilers.
00047  * \author Thomas Rusert
00048  * \author Pablo Alvarado
00049  * \date   28.04.1999
00050  *
00051  * $Id: cvrMath.h,v 1.12 2007/09/15 03:36:57 alvarado Exp $
00052  */
00053 
00054 #ifndef _CVR_MATH_H_
00055 #define _CVR_MATH_H_
00056 
00057 #include "cvrObject.h"
00058 #include "cvrTypes.h"
00059 #include <cmath>
00060 #include <limits>
00061 
00062 /*
00063  * MS Visual C++ 6.0 and old GCC versions do not provide the standard
00064  * cmath interface of functions for several types in the namespace std.
00065  * For this compilers a cumbersome implementation must be provided.
00066  */
00067 #undef _CVR_MATH_H_STYLE
00068 #if (defined _CVR_MSC_6) || (defined _CVR_GNUC_2 )
00069 #define _CVR_MATH_H_STYLE 1
00070 #endif
00071 
00072 # ifdef min
00073 #   undef min
00074 # endif
00075 
00076 # ifdef max
00077 #   undef max
00078 # endif
00079 
00080 namespace cvr {
00081 
00082   /**
00083    * Constant Pi (double precision)
00084    *
00085    * @ingroup gBasicMathFunc
00086    */
00087   const double Pi = 3.1415926535897932; //3.1415926535897932384626433832795;
00088 
00089   /**
00090    * Constant Not a Number (NaN) (doble precision)
00091    *
00092    * @ingroup gBasicMathFunc
00093    */
00094   const double NaN = log(-1.0);
00095 
00096   /**
00097    * Constant Infinity (Inf) (double precision)
00098    *
00099    * @ingroup gBasicMathFunc
00100    */
00101   const double Inf = tan(Pi/2.0);
00102 
00103   /**
00104    * Returns true if \a a is close to \a b, i.e. if \a abs(a-b) <= \a epsilon.
00105    *
00106    * This is always better than testing equality with a==b for
00107    * floating point types. For default \a epsilon integers are tested
00108    * for equality.
00109    *
00110    * \b Note: Do not used for unsigned types T.
00111    *
00112    * @param a first value
00113    * @param b second value
00114    * @param epsilon max difference between \a a and \a b
00115    *                         , default std::numeric_limits<T>::epsilon()
00116    * @returns true if distance between \a a and \a b is <= \a epsilon
00117    *
00118    * @ingroup gBasicMathFunc
00119    */
00120   template <class T>
00121   inline bool closeTo(const T& a, const T& b,
00122                       const T epsilon=std::numeric_limits<T>::epsilon()) {
00123     const T diff=a-b;
00124     return ((diff<=epsilon) && (diff>=-epsilon));
00125   }
00126 
00127 
00128   /**
00129    * Returns true if \a a is close to T(0), i.e. if abs(a) <= \a epsilon.
00130    *
00131    * This is always better than testing equality with a==0. for
00132    * floating point types. For default \a epsilon integers are tested
00133    * for ==0 automagically.
00134    *
00135    * \b Note: Do not used for unsigned types T.
00136    *
00137    * @param a value
00138    * @param epsilon maximum absolute value of \a a
00139    *                         , default std::numeric_limits<T>::epsilon()
00140    * @returns true if absolute value of \a a is <= \a epsilon
00141    *
00142    * @ingroup gBasicMathFunc
00143    */
00144   template <class T>
00145   inline bool closeToZero(const T& a,
00146                           const T epsilon=std::numeric_limits<T>::epsilon()) {
00147     return ((a<=epsilon) && (a>=-epsilon));
00148   }
00149 
00150   /*
00151    * Exchange the content of the two given variables.  It works for all
00152    * types T that properly define the copy constructor and operator=().
00153    *
00154    * @param a first variable
00155    * @param b second variable
00156    *
00157    * At the end \a b contains the previous contents of \a a, which at the
00158    * same time contains the previous contents of \a b.
00159    *
00160    * @ingroup gBasicMathFunc
00161    */
00162 //   template<typename T>
00163 //   inline void swap(T& a,T& b);
00164 
00165   /**
00166    * Get the minimum of x and y
00167    *
00168    * @ingroup gBasicMathFunc
00169    */
00170   template<typename T>
00171   inline T min(const T x, const T y);
00172 
00173   /**
00174    * Get the maximum of x and y
00175    *
00176    * @ingroup gBasicMathFunc
00177    */
00178   template<typename T>
00179   inline T max(const T x, const T y);
00180 
00181   /**
00182    * Get the minimum of x, y and z
00183    *
00184    * @ingroup gBasicMathFunc
00185    */
00186   template<typename T>
00187   inline T min(const T x, const T y, const T z);
00188 
00189   /**
00190    * Get the maximum of x, y and z
00191    *
00192    * @ingroup gBasicMathFunc
00193    */
00194   template<typename T>
00195   inline T max(const T x, const T y, const T z);
00196 
00197   /**
00198    * Check if the \a x value lies within the lower and upper bounds, and
00199    * if not return the closest bound.
00200    *
00201    * This is equivalent (but faster than)  cvr::max(cvr::min(x,upper),lower).
00202    *
00203    * @ingroup gBasicMathFunc
00204    */
00205   template<typename T>
00206   inline T within(const T x, const T lower, const T upper);
00207 
00208   /**
00209    * Store the minimum of x and y in theMin and the maximum in theMax
00210    *
00211    * @ingroup gBasicMathFunc
00212    */
00213   template<typename T>
00214   inline void minmax(const T x, const T y, T& theMin, T& theMax);
00215 
00216   /**
00217    * Ensure that the minimum of the two given values is stored in theMin and
00218    * the maximum in theMax.
00219    *
00220    * @ingroup gBasicMathFunc
00221    */
00222   template<typename T>
00223   inline void minmax(T& theMin, T& theMax);
00224 
00225   /**
00226    * Store the minimum of x and theMin in theMin and the maximum of
00227    * x and theMax in theMax, however, making just one comparison each time.
00228    *
00229    * \warning This method assumes that theMin <= theMax, and if this condition
00230    * is not met, be carefull as just one condition will be ensured, i.e. wether
00231    * theMin will be ok or theMax, but not both.
00232    *
00233    * @ingroup gBasicMathFunc
00234    */
00235   template<typename T>
00236   inline void minmax(const T x, T& theMin, T& theMax);
00237 
00238   /**
00239    * Convert the given angle in degrees to an angle in radians
00240    *
00241    * @ingroup gBasicMathFunc
00242    */
00243   inline double degToRad(const double& deg);
00244 
00245   /**
00246    * Convert the given angle in degrees to an angle in radians
00247    *
00248    * @ingroup gBasicMathFunc
00249    */
00250   inline float degToRad(const float& deg);
00251 
00252   /**
00253    * Convert the given angle in degrees to an angle in radians
00254    *
00255    * @ingroup gBasicMathFunc
00256    */
00257   inline double degToRad(const int deg);
00258 
00259   /**
00260    * Convert the given angle in radians to an angle in degrees
00261    *
00262    * @ingroup gBasicMathFunc
00263    */
00264   inline double radToDeg(const double& rad);
00265 
00266   /**
00267    * Convert the given angle in radians to an angle in degrees
00268    *
00269    * @ingroup gBasicMathFunc
00270    */
00271   inline float radToDeg(const float& rad);
00272 
00273   /**
00274    * Absolute value for signed bytes
00275    *
00276    * @ingroup gBasicMathFunc
00277    */
00278   inline byte abs(byte x);
00279 
00280   /**
00281    * Absolute value for integers
00282    *
00283    * @ingroup gBasicMathFunc
00284    */
00285   inline int abs(int x);
00286 
00287   /**
00288    * Absolute value for any type
00289    *
00290    * @ingroup gBasicMathFunc
00291    */
00292   inline float abs(float x);
00293 
00294   /**
00295    * Absolute value for any type
00296    *
00297    * @ingroup gBasicMathFunc
00298    */
00299   inline double abs(const double& x);
00300 
00301   /**
00302    * Absolute difference for the given values
00303    * equals if (x>y) then (x-y) else (y-x).
00304    *
00305    * Note that with complex numbers it does not return the real abs value.
00306    *
00307    * @ingroup gBasicMathFunc
00308    */
00309   template<typename T>
00310   inline T absdiff(const T& x,const T& y);
00311 
00312   /**
00313    * Compute the reciprocal of the given value \e x, defined as 1/x.
00314    *
00315    * @ingroup gBasicMathFunc
00316    */
00317   inline double reciprocal(const double& x);
00318 
00319   /**
00320    * Compute the reciprocal of the given value \e x, defined as 1/x.
00321    *
00322    * @ingroup gBasicMathFunc
00323    */
00324   inline float reciprocal(const float& x);
00325 
00326   /**
00327    * Compute 1 minus the given value.
00328    *
00329    * This function is usually employed to revert a probability value
00330    *
00331    * @ingroup gBasicMathFunc
00332    */
00333   inline double oneMinus(const double& x);
00334 
00335   /**
00336    * Compute 1 minus the given value
00337    *
00338    * This function is usually employed to revert a probability value
00339    *
00340    * @ingroup gBasicMathFunc
00341    */
00342   inline float oneMinus(const float& x);
00343 
00344   /**
00345    * Compute 1 minus the given value
00346    *
00347    * @ingroup gBasicMathFunc
00348    */
00349   inline int oneMinus(const int x);
00350 
00351   /**
00352    * Rectify is 0 if x<0 or x otherwise
00353    *
00354    * @ingroup gBasicMathFunc
00355    */
00356   template<typename T>
00357   inline T rectify(const T& x);
00358 
00359   /**
00360    * Square (x*x)
00361    *
00362    * @ingroup gBasicMathFunc
00363    */
00364   template<typename T>
00365   inline T sqr(const T& x);
00366 
00367   /**
00368    * Square of the magnitude of a number (|x|*|x|)
00369    *
00370    * This function is useful in generic programming for those cases where
00371    * complex numbers will occur in products of conjugate pairs.
00372    *
00373    * @ingroup gBasicMathFunc
00374    */
00375   template<typename T>
00376   inline T sqrAbs(const T& x);
00377 
00378   /**
00379    * Return the conjugate of a scalar number, i.e. the same number.
00380    *
00381    * This function is used in generic programming algorithms in which also
00382    * complex numbers are expected.
00383    */
00384   template<typename T>
00385   inline T conj(const T cn);
00386 
00387   /**
00388    * Square root of integer type.  Equals floor(sqrt(x))
00389    *
00390    * @ingroup gBasicMathFunc
00391    */
00392   inline int sqrt(int x);
00393 
00394   /**
00395    * Square root
00396    *
00397    * @ingroup gBasicMathFunc
00398    */
00399   inline unsigned int sqrt(unsigned int x);
00400 
00401   /**
00402    * Square root
00403    *
00404    * @ingroup gBasicMathFunc
00405    */
00406   inline float sqrt(const float& x);
00407 
00408   /**
00409    * Square root
00410    *
00411    * @ingroup gBasicMathFunc
00412    */
00413   inline double sqrt(const double& x);
00414 
00415   /**
00416    * Square root of rectified value, i.e. returns 0 if x<=0 or sqrt(x)
00417    * otherwise.
00418    *
00419    * @ingroup gBasicMathFunc
00420    */
00421   template<typename T>
00422   inline T sqrtrect(const T x);
00423 
00424   // other standard functions used
00425 
00426 #ifdef _CVR_MATH_H_STYLE
00427   /**
00428    * \name Standard math functions
00429    */
00430   //@{
00431   /**
00432    * Sinus
00433    *
00434    * @ingroup gBasicMathFunc
00435    */
00436   inline float sin(float x);
00437 
00438   /**
00439    * Sinus
00440    *
00441    * @ingroup gBasicMathFunc
00442    */
00443   inline double sin(const double& x);
00444 
00445   /**
00446    * Cosinus
00447    *
00448    * @ingroup gBasicMathFunc
00449    */
00450   inline float cos(float x);
00451 
00452   /**
00453    * Cosinus
00454    *
00455    * @ingroup gBasicMathFunc
00456    */
00457   inline double cos(const double& x);
00458 
00459   /**
00460    * Tangent
00461    *
00462    * @ingroup gBasicMathFunc
00463    */
00464   inline float tan(float x);
00465 
00466   /**
00467    * Tangent
00468    *
00469    * @ingroup gBasicMathFunc
00470    */
00471   inline double tan(const double& x);
00472 
00473   /**
00474    * Arc sine
00475    *
00476    * @ingroup gBasicMathFunc
00477    */
00478   inline float asin(float x);
00479 
00480   /**
00481    * Arc sine
00482    *
00483    * @ingroup gBasicMathFunc
00484    */
00485   inline double asin(const double& x);
00486 
00487   /**
00488    * Arc cosine
00489    *
00490    * @ingroup gBasicMathFunc
00491    */
00492   inline float acos(float x);
00493 
00494   /**
00495    * Arc cosine
00496    *
00497    * @ingroup gBasicMathFunc
00498    */
00499   inline double acos(const double& x);
00500 
00501   /**
00502    * Arc tangent
00503    *
00504    * @ingroup gBasicMathFunc
00505    */
00506   inline float atan(float x);
00507 
00508   /**
00509    * Arc tangent
00510    *
00511    * @ingroup gBasicMathFunc
00512    */
00513   inline double atan(const double& x);
00514 
00515   /**
00516    * Arc tangent of y/x
00517    *
00518    * @see cvr::arctanLUT
00519    *
00520    * @ingroup gBasicMathFunc
00521    */
00522   inline float atan2(float y,float x);
00523 
00524   /**
00525    * Arc tangent of y/x
00526    *
00527    * @see cvr::arctanLUT
00528    *
00529    * @ingroup gBasicMathFunc
00530    */
00531   inline double atan2(const double& y,const double& x);
00532 
00533   /**
00534    * Hyperbolic sine
00535    *
00536    * @ingroup gBasicMathFunc
00537    */
00538   inline double sinh(const double& x);
00539 
00540   /**
00541    * Hyperbolic sinus
00542    *
00543    * @ingroup gBasicMathFunc
00544    */
00545   inline double sinh(float x);
00546 
00547   /**
00548    * Hyperbolic cosine
00549    *
00550    * @ingroup gBasicMathFunc
00551    */
00552   inline float cosh(float x);
00553 
00554   /**
00555    * Hyperbolic cosine
00556    *
00557    * @ingroup gBasicMathFunc
00558    */
00559   inline double cosh(const double& x);
00560 
00561   /**
00562    * Hyperbolic tangent
00563    *
00564    * @ingroup gBasicMathFunc
00565    */
00566   inline float tanh(float x);
00567 
00568   /**
00569    * Hyperbolic tangent
00570    *
00571    * @ingroup gBasicMathFunc
00572    */
00573   inline double tanh(const double& x);
00574 
00575   /**
00576    * Natural logarithm
00577    *
00578    * @ingroup gBasicMathFunc
00579    */
00580   inline float log(float x);
00581 
00582   /**
00583    * Natural logarithm
00584    *
00585    * @ingroup gBasicMathFunc
00586    */
00587   inline double log(const double& x);
00588 
00589   /**
00590    * Natural logarithm
00591    *
00592    * @ingroup gBasicMathFunc
00593    */
00594   inline double log(int x);
00595 
00596   /**
00597    * Logarithm base 10
00598    *
00599    * @ingroup gBasicMathFunc
00600    */
00601   inline float log10(float x);
00602 
00603   /**
00604    * Logarithm base 10
00605    *
00606    * @ingroup gBasicMathFunc
00607    */
00608   inline double log10(const double& x);
00609 
00610   /**
00611    * Logarithm base 10
00612    *
00613    * @ingroup gBasicMathFunc
00614    */
00615   inline double log10(int x);
00616 
00617   /**
00618    * Natural exponential
00619    *
00620    * @ingroup gBasicMathFunc
00621    */
00622   inline float exp(float x);
00623 
00624   /**
00625    * Natural exponential
00626    *
00627    * @ingroup gBasicMathFunc
00628    */
00629   inline double exp(const double& x);
00630 
00631   /**
00632    * Natural exponential
00633    *
00634    * @ingroup gBasicMathFunc
00635    */
00636   inline double exp(int x);
00637 
00638   /**
00639    * Power function, computes \f$x^y\f$
00640    *
00641    * @ingroup gBasicMathFunc
00642    */
00643   inline double pow(const double& x, const double& y);
00644 
00645   /**
00646    * Power function, computes \f$x^y\f$
00647    *
00648    * @ingroup gBasicMathFunc
00649    */
00650   inline float pow(float x,float y);
00651 
00652   /**
00653    * Power function, computes \f$x^y\f$
00654    *
00655    * @ingroup gBasicMathFunc
00656    */
00657   inline double pow(const double& x, int y);
00658 
00659   /**
00660    * Power function, computes \f$x^y\f$
00661    *
00662    * @ingroup gBasicMathFunc
00663    */
00664   inline float pow(float x,int y);
00665   //@}
00666 
00667 #else
00668   // methods optimized for different types
00669   /**
00670    * \name Standard math functions
00671    */
00672   //@{
00673   using std::swap;
00674   using std::sin;
00675   using std::cos;
00676   using std::tan;
00677   using std::asin;
00678   using std::acos;
00679   using std::atan;
00680   using std::atan2;
00681   using std::sinh;
00682   using std::cosh;
00683   using std::tanh;
00684   using std::log;
00685   using std::log10;
00686   using std::exp;
00687   using std::pow;
00688   //@}
00689 #endif
00690 
00691   /**
00692    * Sigmoid for floats.
00693    *
00694    * The sigmoid is defined as \f$\frac{1}{1+\exp(-x)}\f$
00695    *
00696    * @ingroup gBasicMathFunc
00697    */
00698   inline float sigmoid(const float x);
00699 
00700   /**
00701    * Sigmoid for doubles
00702    *
00703    * The sigmoid is defined as \f$\frac{1}{1+\exp(-x)}\f$
00704    *
00705    * @ingroup gBasicMathFunc
00706    */
00707   inline double sigmoid(const double& x);
00708 
00709   /**
00710    * Sigmoid for ints
00711    *
00712    * The sigmoid is defined as \f$\frac{1}{1+\exp(-x)}\f$
00713    *
00714    * @ingroup gBasicMathFunc
00715    */
00716   inline double sigmoid(const int x);
00717 
00718   /**
00719    * Signum of x (for x==0 -> signum = 1) (@see signum0)
00720    *
00721    * @ingroup gBasicMathFunc
00722    */
00723   template<typename T>
00724   inline T signum(T x);
00725 
00726   /**
00727    * Signum of x (for x==0 -> signum = 0) (@see signum)
00728    *
00729    * @ingroup gBasicMathFunc
00730    */
00731   template<typename T>
00732   inline T signum0(T x);
00733 
00734   /**
00735    * Return true if the given number is an even number.
00736    *
00737    * For floating point types, this function will return true only if the
00738    * number is \e exacltly an integer and it is even.
00739    *
00740    * @ingroup gBasicMathFunc
00741    */
00742   template<typename T>
00743   inline bool even(const T x);
00744 
00745   /**
00746    * Return true if the given number is an odd number.
00747    *
00748    * For floating point types, this function will return true only if the
00749    * number is \e exacltly an integer and it is odd.
00750    *
00751    * @ingroup gBasicMathFunc
00752    */
00753   template<typename T>
00754   inline bool odd(const T x);
00755 }
00756 
00757 #include "cvrMath_inline.h"
00758 
00759 #endif

Generated on Sun Sep 20 22:08:00 2009 for CVR-Lib by Doxygen 1.5.8