INTRODUCTION Overview Download and Install Documentation Publications REPOSITORY Libraries DEVELOPER Dev Guide Dashboard PEOPLE Contributors Users Project Download Mailing lists
|
mathdefs.h00001 /* 00002 * GearBox Project: Peer-Reviewed Open-Source Libraries for Robotics 00003 * http://gearbox.sf.net/ 00004 * Copyright (c) 2004-2010 Alex Brooks, Alexei Makarenko, Tobias Kaupp 00005 * 00006 * This distribution is licensed to you under the terms described in 00007 * the LICENSE file included in this distribution. 00008 * 00009 */ 00010 00011 /*dox 00012 @file 00013 @brief Mathematical macros 00014 @ingroup orca_library_orcaiceutil 00015 */ 00016 00017 #ifndef GBXUTILACFR_MATH_DEFINITIONS_H 00018 #define GBXUTILACFR_MATH_DEFINITIONS_H 00019 00020 #if defined (WIN32) 00021 #if defined (GBXUTILACFR_STATIC) 00022 #define GBXUTILACFR_EXPORT 00023 #elif defined (GBXUTILACFR_EXPORTS) 00024 #define GBXUTILACFR_EXPORT __declspec (dllexport) 00025 #else 00026 #define GBXUTILACFR_EXPORT __declspec (dllimport) 00027 #endif 00028 #else 00029 #define GBXUTILACFR_EXPORT 00030 #endif 00031 00032 #include <assert.h> 00033 00034 /***************************************************************************** 00035 * INCLUDE THE RELEVANT MATHS LIBRARIES 00036 *****************************************************************************/ 00037 00038 // MSVC compiler requires this symbol before exposing the (apparently) 00039 // non-standard symbols M_PI, etc... 00040 #ifdef WIN32 00041 #define _USE_MATH_DEFINES 00042 #endif 00043 00044 #include <cmath> 00045 00046 /***************************************************************************** 00047 * CONSTANTS 00048 *****************************************************************************/ 00049 // M_PI is not defined after including cmath for the MS visual studio compiler? 00050 #ifndef M_PI 00051 //dox Defines number Pi 00052 #define M_PI 3.14159265358979323846 00053 #endif 00054 00055 #ifndef NAN 00056 //dox Defines not-a-number 00057 #define NAN (__builtin_nanf("")) 00058 #endif 00059 00060 #ifndef INF 00061 //dox Defines infinity 00062 #define INF (__builtin_inff()) 00063 #endif 00064 00065 /***************************************************************************** 00066 * CONVERSION MACROS 00067 *****************************************************************************/ 00068 #ifndef DEG2RAD_RATIO 00069 //dox Convertion factor from degrees to radians. 00070 #define DEG2RAD_RATIO (M_PI/180.0) 00071 #endif 00072 00073 //dox Converts from degrees to radians. 00074 #define DEG2RAD(deg) ((deg)*DEG2RAD_RATIO) 00075 //dox Converts from radians to degrees. 00076 #define RAD2DEG(rad) ((rad)/DEG2RAD_RATIO) 00077 00078 //dox Normalises the angle [rad] to the range [-pi,pi) 00079 //dox Don't return the normalised angle, because it's easy to make the 00080 //dox mistake of doing: 'NORMALISE_ANGLE( myAngle )', ignoring the return value. 00081 GBXUTILACFR_EXPORT inline void NORMALISE_ANGLE( double &theta ) 00082 { 00083 double multiplier; 00084 00085 if (theta >= -M_PI && theta < M_PI) 00086 return; 00087 00088 multiplier = std::floor(theta / (2*M_PI)); 00089 theta -= multiplier*2*M_PI; 00090 if (theta >= M_PI) 00091 theta -= 2*M_PI; 00092 else if (theta < -M_PI) 00093 theta += 2*M_PI; 00094 } 00095 00096 //dox Normalises the angle [rad] to the range [-pi,pi) 00097 //dox Don't return the normalised angle, because it's easy to make the 00098 //dox mistake of doing: 'NORMALISE_ANGLE( myAngle )', ignoring the return value. 00099 GBXUTILACFR_EXPORT inline void NORMALISE_ANGLE( float &theta ) 00100 { 00101 double thDouble = theta; 00102 NORMALISE_ANGLE( thDouble ); 00103 theta = (float)thDouble; 00104 } 00105 00106 /***************************************************************************** 00107 * MATH MACROS 00108 *****************************************************************************/ 00109 // for compatability retain this old one 00110 //#ifndef ABS 00111 //#define ABS(x) (std::abs(x)) 00112 //#endif 00113 #ifndef MIN 00114 //dox Minimum of two numbers 00115 #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 00116 #endif 00117 #ifndef MAX 00118 //dox Maximum of two numbers 00119 #define MAX(x, y) (((x) > (y)) ? (x) : (y)) 00120 #endif 00121 #ifndef APPLY_LIMITS 00122 //dox Limits x to an interval between min_x and max_x. 00123 //dox x must be a variable which can be assigned a value; 00124 //dox Compares using less_than and greater_than. 00125 #define APPLY_LIMITS(max_x, x, min_x) \ 00126 if((x)>(max_x)) x=(max_x); if((x)<(min_x)) x=(min_x); 00127 #endif 00128 #ifndef NORM2 00129 //dox Norm of a 2D vector 00130 #define NORM2(x, y) (sqrt((x)*(x)+(y)*(y))) 00131 #endif 00132 #ifndef NORM3 00133 //dox Norm of a 3D vector 00134 #define NORM3(x, y, z) (sqrt((x)*(x)+(y)*(y)+(z)*(z))) 00135 #endif 00136 #ifndef ROUND 00137 //dox Rounds double or float to the nearest integer. 00138 #define ROUND(x) ((int)(x+0.5)) 00139 #endif 00140 #ifndef ROUND_TO 00141 //dox Rounds @c n to the nearest whole number of multiples of @c d. 00142 //dox For example, ROUND_TO(8,5) and ROUND_TO(12,5) will both result in 10. 00143 #define ROUND_TO(n,d) (d*rint(n/d)) 00144 #endif 00145 #ifndef SIGN 00146 //dox Sign of a number. 00147 #define SIGN(A) ((A)<0?(-1):(1)) 00148 #endif 00149 #ifndef COS_LAW 00150 //dox Law of cosines. 00151 #define COS_LAW(side1, side2, theta) \ 00152 (sqrt(SQR(side1)+SQR(side2)-2.0*(side1)*(side2)*cos(theta))) 00153 #endif 00154 #ifndef INV_COS_LAW 00155 //dox Inverse law of cosines.. 00156 #define INV_COS_LAW(oppSide, side1, side2) \ 00157 (acos((SQR(side1)+SQR(side2)-SQR(oppSide))/(2.0*(side1)*(side2)))) 00158 #endif 00159 00160 // isinf not defined on all systems (eg Solaris) 00161 #if defined (__SVR4) && defined (__sun) 00162 #define isfinite(x) \ 00163 __extension__ ({ __typeof (x) __x_f = (x); \ 00164 __builtin_expect(!isnan(__x_f - __x_f), 1); }) 00165 00166 #define isinf(x) \ 00167 __extension__ ({ __typeof (x) __x_i = (x); \ 00168 __builtin_expect(!isnan(__x_i) && !isfinite(__x_i), 0); }) 00169 #endif 00170 00171 00172 /***************************************************************************** 00173 * COMPARISON MACROS 00174 *****************************************************************************/ 00175 #ifndef NEAR 00176 //dox Check that two numbers are sufficiently close. 00177 //dox Compares using less_than and greater_than. 00178 #define NEAR(x,y,epsilon) (((x) > (y)-(epsilon)) && ((x) < (y)+(epsilon))) 00179 #endif 00180 00181 //dox Modifies x to lie within [x_min,x_max] 00182 //dox 00183 template<typename T> 00184 GBXUTILACFR_EXPORT void 00185 CLIP_TO_LIMITS( const T &min_x, T &x, const T &max_x ) 00186 { 00187 assert( min_x <= max_x ); 00188 if ( x > max_x ) 00189 x = max_x; 00190 else if ( x < min_x ) 00191 x = min_x; 00192 } 00193 00194 #endif |