rmath.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef SOFA_HELPER_RMATH_H
00028 #define SOFA_HELPER_RMATH_H
00029
00030 #include <sofa/helper/system/config.h>
00031 #include <math.h>
00032
00033 namespace sofa
00034 {
00035
00036 namespace helper
00037 {
00038
00039 #ifdef M_PI
00040 #define R_PI M_PI
00041 #else
00042 #define R_PI 3.141592653589793238462
00043 #endif
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 template<class real>
00077 inline int rfloor(real r)
00078 {
00079 static const double FLOATTOINTCONST=(1.5*(1LL<<(52-16)));
00080 union { double d;
00081 int i;
00082 } temp;
00083 temp.d = r+FLOATTOINTCONST;
00084 return ((temp.i)>>16);
00085 }
00086
00087 template<class real>
00088 inline int rnear(real r)
00089 {
00090 static const double FLOATTOINTCONST_0_5=(1.5*(1LL<<(52-16)))+0.5;
00091 union { double d;
00092 int i;
00093 } temp;
00094 temp.d = r+FLOATTOINTCONST_0_5;
00095 return ((temp.i)>>16);
00096 }
00097
00098 inline int rceil(double r)
00099 {
00100 return -rfloor(-r);
00101 }
00102
00103 template<class real>
00104 inline real rabs(real r)
00105 {
00106 return (r>=0)?r:-r;
00107 }
00108
00109 template<class real>
00110 inline real rmin(real r, real s)
00111 {
00112 return (r<=s)?r:s;
00113 }
00114
00115 template<class real>
00116 inline real rmax(real r, real s)
00117 {
00118 return (r>s)?r:s;
00119 }
00120
00121 template<class T>
00122 inline T rlerp(const T& a, const T& b, float f)
00123 {
00124 return a+(b-a)*f;
00125 }
00126
00127 template<class T>
00128 inline T rsqrt(const T& a)
00129 {
00130 return (T)sqrtf((float)a);
00131 }
00132
00133 inline double rsqrt(const double& a)
00134 {
00135 #if defined(__arm__) || defined(__mips__)
00136 return sqrt((double)a);
00137 #else
00138 return (double)sqrtl((long double)a);
00139 #endif
00140 }
00141
00142 inline long double rsqrt(const long double& a)
00143 {
00144 #if defined(__arm__) || defined(__mips__)
00145 return (long double)sqrt((double)a);
00146 #else
00147 return sqrtl(a);
00148 #endif
00149 }
00150
00151 template<class T>
00152 inline const T SQR(const T& a)
00153 {
00154 return a*a;
00155 }
00156
00157 template<class T>
00158 inline const T SIGN(const T &a, const T &b)
00159 {
00160 return b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a);
00161 }
00162
00163 template<class T>
00164 inline void SWAP(T &a, T &b)
00165 {
00166 T dum=a;
00167 a=b;
00168 b=dum;
00169 }
00170
00171 inline void shft3(double &a, double &b, double &c, const double d)
00172 {
00173 a=b;
00174 b=c;
00175 c=d;
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 }
00187
00188 }
00189
00190 #endif
00191
00192