INTRODUCTION
Overview
Download and Install
Documentation
Publications

REPOSITORY
Libraries

DEVELOPER
Dev Guide
Dashboard

PEOPLE
Contributors
Users

SourceForge.net Logo
Project
Download
Mailing lists

 

         

mathdefs.h

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

Generated for GearBox by  doxygen 1.4.5