LoMath.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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef LOBOT_MATH_UTILITIES_DOT_H
00042 #define LOBOT_MATH_UTILITIES_DOT_H
00043
00044
00045
00046
00047 #include "Robots/LoBot/util/range.hh"
00048
00049
00050 #include <algorithm>
00051 #include <limits>
00052
00053
00054 #include <math.h>
00055
00056
00057
00058 namespace lobot {
00059
00060
00061
00062
00063 template<typename T>
00064 int sign(T t)
00065 {
00066 return (t < 0) ? -1 : +1 ;
00067 }
00068
00069
00070 template<typename T>
00071 int sign0(T t)
00072 {
00073 if (t < 0)
00074 return -1 ;
00075 if (t > 0)
00076 return +1 ;
00077 return 0 ;
00078 }
00079
00080
00081 template<typename T>
00082 T abs(T t)
00083 {
00084 return (t < 0) ? -t : t ;
00085 }
00086
00087
00088 template<typename T>
00089 inline T sqr(T t)
00090 {
00091 return t * t ;
00092 }
00093
00094
00095 int next_power_of_two(int n) ;
00096
00097
00098
00099
00100 inline bool is_zero(float t)
00101 {
00102 return t > -std::numeric_limits<float>::epsilon()
00103 && t < std::numeric_limits<float>::epsilon() ;
00104 }
00105
00106
00107 inline bool is_zero(double t)
00108 {
00109 return t > -std::numeric_limits<double>::epsilon()
00110 && t < std::numeric_limits<double>::epsilon() ;
00111 }
00112
00113
00114 int round(float t) ;
00115
00116
00117
00118
00119 template<typename T>
00120 T clamp(T value, T min, T max)
00121 {
00122 if (value < min)
00123 return min ;
00124 if (value > max)
00125 return max ;
00126 return value ;
00127 }
00128
00129
00130 template<typename T>
00131 inline T clamp(T value, const range<T>& R)
00132 {
00133 return clamp(value, R.min(), R.max()) ;
00134 }
00135
00136
00137 template<typename T>
00138 inline range<T> clamp(const range<T>& input, const range<T>& bounds)
00139 {
00140 return range<T>(std::max(input.min(), bounds.min()),
00141 std::min(input.max(), bounds.max())) ;
00142 }
00143
00144
00145
00146
00147 float clamp_angle(float angle) ;
00148
00149
00150 inline int quadrant(float angle)
00151 {
00152 return static_cast<int>(clamp_angle(angle)/90) + 1 ;
00153 }
00154
00155
00156
00157
00158
00159
00160 inline int octant(float angle)
00161 {
00162 return static_cast<int>(clamp_angle(angle)/45) + 1 ;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171 int random(int min, int max) ;
00172
00173
00174
00175
00176
00177 float randomf(float min, float max) ;
00178
00179
00180
00181
00182
00183
00184
00185 inline float sin(float angle)
00186 {
00187 return sinf(angle * 0.01745329f) ;
00188 }
00189
00190 inline double sin(double angle)
00191 {
00192 return ::sin(angle * 0.0174532925199432) ;
00193 }
00194
00195 inline float sin(int angle)
00196 {
00197 return sin(static_cast<float>(angle)) ;
00198 }
00199
00200
00201 inline float cos(float angle)
00202 {
00203 return cosf(angle * 0.01745329f) ;
00204 }
00205
00206 inline double cos(double angle)
00207 {
00208 return ::cos(angle * 0.0174532925199432) ;
00209 }
00210
00211 inline float cos(int angle)
00212 {
00213 return cos(static_cast<float>(angle)) ;
00214 }
00215
00216
00217 inline float asin(float x)
00218 {
00219 return asinf(x) * 57.29577976f ;
00220 }
00221
00222 inline double asin(double x)
00223 {
00224 return ::asin(x) * 57.2957795130823215 ;
00225 }
00226
00227 inline float asin(int x)
00228 {
00229 return asin(static_cast<float>(x)) ;
00230 }
00231
00232
00233 inline float acos(float x)
00234 {
00235 return acosf(x) * 57.29577976f ;
00236 }
00237
00238 inline double acos(double x)
00239 {
00240 return ::acos(x) * 57.2957795130823215 ;
00241 }
00242
00243 inline float acos(int x)
00244 {
00245 return acos(static_cast<float>(x)) ;
00246 }
00247
00248
00249 inline float atan(float x)
00250 {
00251 return atanf(x) * 57.29577976f ;
00252 }
00253
00254 inline double atan(double x)
00255 {
00256 return ::atan(x) * 57.2957795130823215 ;
00257 }
00258
00259 inline float atan(int x)
00260 {
00261 return atan(static_cast<float>(x)) ;
00262 }
00263
00264
00265 inline float atan(float y, float x)
00266 {
00267 return atan2f(y, x) * 57.29577976f ;
00268 }
00269
00270 inline double atan(double y, double x)
00271 {
00272 return ::atan2(y, x) * 57.2957795130823215 ;
00273 }
00274
00275 inline float atan(int y, int x)
00276 {
00277 return atan(static_cast<float>(y), static_cast<float>(x)) ;
00278 }
00279
00280
00281
00282
00283
00284
00285 inline float log(float n)
00286 {
00287 return log10f(n) ;
00288 }
00289
00290 inline double log(double n)
00291 {
00292 return log10(n) ;
00293 }
00294
00295
00296
00297
00298 inline float ln(float n)
00299 {
00300 return logf(n) ;
00301 }
00302
00303 inline double ln(double n)
00304 {
00305 return ::log(n) ;
00306 }
00307
00308
00309
00310
00311 inline float exp(float n)
00312 {
00313 return expf(n) ;
00314 }
00315
00316 inline double exp(double n)
00317 {
00318 return ::exp(n) ;
00319 }
00320
00321
00322
00323
00324
00325 float prob_to_log_odds(float p) ;
00326 float log_odds_to_prob(float log_odds) ;
00327
00328
00329
00330
00331
00332 float gaussian(float x, float mu, float sigma) ;
00333
00334
00335
00336 template<typename T = float>
00337 class gaussian_weight {
00338 T mean ;
00339 T two_sigma_sqr, sqrt_2pi_sigma ;
00340 public:
00341 gaussian_weight(T mean, T sigma) ;
00342 T operator()(T x, T w) const {
00343 return w * exp(-sqr(x - mean)/two_sigma_sqr)/sqrt_2pi_sigma ;
00344 }
00345 } ;
00346
00347 template<typename T>
00348 gaussian_weight<T>::gaussian_weight(T m, T sigma)
00349 : mean(m),
00350 two_sigma_sqr(2 * sqr(sigma)),
00351 sqrt_2pi_sigma(T(2.5066282746310004) * sigma)
00352 {}
00353
00354
00355
00356
00357 float sample_tri(float sigma) ;
00358
00359
00360
00361 }
00362
00363 #endif
00364
00365
00366
00367
00368