00001 /** 00002 * @file Math/Common.h 00003 * 00004 * This contains some often used mathematical definitions and functions. 00005 * 00006 * @author <a href="mailto:martin.kallnik@gmx.de">Martin Kallnik</a> 00007 * @author Max Risler 00008 */ 00009 00010 /** 00011 * defines the maximum of a and b 00012 */ 00013 #ifdef max 00014 #undef max 00015 #endif 00016 #define max(a,b) (((a) > (b)) ? (a) : (b)) 00017 00018 /** 00019 * defines the minimum of a and b 00020 */ 00021 #ifdef min 00022 #undef min 00023 #endif 00024 #define min(a,b) (((a) < (b)) ? (a) : (b)) 00025 00026 #ifndef __Math_Common_h__ 00027 #define __Math_Common_h__ 00028 00029 #include <math.h> 00030 #include <stdlib.h> 00031 00032 00033 /** 00034 * defines the sign of a 00035 */ 00036 #ifndef sgn 00037 #define sgn(a) ( (a) < 0 ? -1 : ((a)==0) ? 0 : 1 ) 00038 #endif 00039 00040 /** 00041 * defines the square of a value 00042 */ 00043 #ifndef sqr 00044 #define sqr(a) ( (a) * (a) ) 00045 #endif 00046 00047 inline double sec(const double a){return 1/cos(a);} 00048 00049 inline double cosec(const double a){return 1/sin(a);} 00050 00051 /** @name constants for some often used angles */ 00052 ///@{ 00053 /** constant for a half circle*/ 00054 const double pi = 3.1415926535897932384626433832795; 00055 /** constant for a full circle*/ 00056 const double pi2 = 2.0*3.1415926535897932384626433832795; 00057 /** constant for three quater circle*/ 00058 const double pi3_2 = 1.5*3.1415926535897932384626433832795; 00059 /** constant for a quarter circle*/ 00060 const double pi_2 = 0.5*3.1415926535897932384626433832795; 00061 /** constant for a 1 degree*/ 00062 const double pi_180 = 3.1415926535897932384626433832795/180; 00063 /** constant for a 1/8 circle*/ 00064 const double pi_4 = 3.1415926535897932384626433832795*0.25; 00065 /** constant for a 3/8 circle*/ 00066 const double pi3_4 = 3.1415926535897932384626433832795*0.75; 00067 ///@} 00068 00069 /** @name constant for bayesian filter */ 00070 ///@{ 00071 /** constant for e*/ 00072 const double e = 2.71828182845902353602874713527; 00073 ///@} 00074 /** 00075 * Converts angle from rad to long in microrad. 00076 * Robot uses long microrad for joint angles. 00077 * \param angle coded in rad 00078 * \return angle coded in microrad 00079 */ 00080 inline long toMicroRad(double angle) {return long(angle*1000000.0);} 00081 00082 /** 00083 * Converts angle long in microrad to rad. 00084 * \param microrad angle coded in microrad 00085 * \return angle coded in rad 00086 */ 00087 inline double fromMicroRad(long microrad){return ((double)microrad)/1000000.0;} 00088 00089 /** 00090 * Converts angle from rad to degrees. 00091 * \param angle code in rad 00092 * \return angle coded in degrees 00093 */ 00094 inline double toDegrees(double angle){return angle * 180.0 / pi;} 00095 00096 /** Converts angle from degrees to rad. 00097 * \param degrees angle coded in degrees 00098 * \return angle coded in rad 00099 */ 00100 inline double fromDegrees(double degrees){return degrees * pi_180;} 00101 00102 /** 00103 * reduce angle to [-pi..+pi] 00104 * \param data angle coded in rad 00105 * \return normalized angle coded in rad 00106 */ 00107 inline double normalize(double data) 00108 { 00109 if (data <= pi && data >= -pi) return data; 00110 double ndata = data - ((int )(data / pi2))*pi2; 00111 if (ndata > pi) 00112 { 00113 ndata -= pi2; 00114 } 00115 else if (ndata < -pi) 00116 { 00117 ndata += pi2; 00118 } 00119 return ndata; 00120 } 00121 00122 /** 00123 * The function returns a random number in the range of [0..1]. 00124 * @return The random number. 00125 */ 00126 inline double random() {return double(rand()) / RAND_MAX;} 00127 00128 /** 00129 * The function returns a random integer number in the range of [0..n-1]. 00130 * @param n the number of possible return values (0 ... n-1) 00131 * @return The random number. 00132 */ 00133 inline int random(int n) {return (int)(random()*n*0.999999);} 00134 00135 00136 /** 00137 * Round floating point value to nearest integer. 00138 * 00139 * \note Not tested (or even useful?!?) on MIPS platform! 00140 * \see http://www.agner.org/assem/ 00141 */ 00142 /*inline int fast_round(float val) 00143 { 00144 double temp = val + 6755399441055744.0; 00145 return (int)*((__int64*)&temp); 00146 }*/ 00147 00148 00149 /** 00150 * Magical fast floor function for floats. 00151 * 00152 * \note Not tested (or even useful?!?) on MIPS platform! 00153 * \see http://www.agner.org/assem/ 00154 */ 00155 /*inline int fast_floor(float val) 00156 { 00157 double temp = val + 6755399441055744.0; 00158 float diff = (float)(val - (int)*((__int64*)&temp)); 00159 return (int)*((__int64*)&temp) - (*(int*)&diff < 0); 00160 }*/ 00161 00162 00163 #endif // __Math_Common_h__ 00164 00165 /* 00166 * Change log : 00167 * 00168 * $Log: Common.h,v $ 00169 * Revision 1.3 2004/06/15 08:49:11 thomas 00170 * work around: comment out new fast-round-functions because does not compile 00171 * 00172 * Revision 1.2 2004/06/14 23:37:26 kindler 00173 * - added fast floor() and round() functions. 00174 * 00175 * Revision 1.1.1.1 2004/05/22 17:37:02 cvsadm 00176 * created new repository GT2004_WM 00177 * 00178 * Revision 1.3 2004/03/09 13:26:44 dueffert 00179 * serious bug fixed: int random(int) always returned 0 due to integer overflow on mips, may have worked on x86 00180 * 00181 * Revision 1.2 2003/12/21 13:37:07 goehring 00182 * Euler constant added 00183 * 00184 * Revision 1.1 2003/10/07 10:13:23 cvsadm 00185 * Created GT2004 (M.J.) 00186 * 00187 * Revision 1.2 2003/08/06 10:59:33 dueffert 00188 * int random(int) added 00189 * 00190 * Revision 1.1.1.1 2003/07/02 09:40:28 cvsadm 00191 * created new repository for the competitions in Padova from the 00192 * tamara CVS (Tuesday 2:00 pm) 00193 * 00194 * removed unused solutions 00195 * 00196 * Revision 1.12 2003/03/28 17:17:14 risler 00197 * min/max now get redefined with each include of Common.h 00198 * 00199 * Revision 1.11 2003/03/06 18:17:29 dueffert 00200 * fixed max problem (Math/Common.h has to be included after iostream.h!?!) 00201 * 00202 * Revision 1.10 2003/02/21 16:34:16 dueffert 00203 * common pi in own code, warnings removed, platform compatibility restored 00204 * 00205 * Revision 1.9 2003/01/11 21:58:30 roefer 00206 * Comment corrected 00207 * 00208 * Revision 1.8 2002/12/12 22:09:08 roefer 00209 * Random functions added 00210 * 00211 * Revision 1.7 2002/11/20 13:37:31 dueffert 00212 * missing or wrong doxygen file corrected 00213 * 00214 * Revision 1.6 2002/11/19 15:43:03 dueffert 00215 * doxygen comments corrected 00216 * 00217 * Revision 1.5 2002/11/12 23:00:47 dueffert 00218 * started restore greenhills compatibility 00219 * 00220 * Revision 1.4 2002/09/23 14:14:02 risler 00221 * no message 00222 * 00223 * Revision 1.3 2002/09/23 13:54:48 risler 00224 * abs replaced by fabs/labs 00225 * 00226 * Revision 1.2 2002/09/22 18:40:52 risler 00227 * added new math functions, removed GTMath library 00228 * 00229 * Revision 1.1 2002/09/22 13:10:50 risler 00230 * new Math headers added 00231 * 00232 * 00233 */