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 #ifndef MATH_H_
00026 #define MATH_H_
00027
00028 #include <cmath>
00029 #include <cfloat>
00030
00031 namespace Lamp{
00032
00033
00034
00035
00036
00037 class Math{
00038 public:
00039
00040
00041
00042
00043 static const float PI;
00044
00045
00046 static const float quadruplePI;
00047
00048
00049 static const float doublePI;
00050
00051
00052 static const float halfPI;
00053
00054
00055 static const float quadrantPI;
00056
00057
00058 static const float E;
00059
00060
00061 static const float epsilon;
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 static inline float toRadian(float degree){
00072 static const float scale = PI / 180.f;
00073 return degree * scale;
00074 }
00075
00076
00077
00078
00079
00080
00081 static inline float toDegree(float radian){
00082 static const float scale = 180.f / PI;
00083 return radian * scale;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 static inline int maximum(int a, int b){
00096 if(a > b){ return a; }
00097 else{ return b; }
00098 }
00099
00100
00101
00102
00103
00104
00105
00106 static inline u_int maximum(u_int a, u_int b){
00107 if(a > b){ return a; }
00108 else{ return b; }
00109 }
00110
00111
00112
00113
00114
00115
00116
00117 static inline float maximum(float a, float b){
00118 if(a > b){ return a; }
00119 else{ return b; }
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 static inline int minimum(int a, int b){
00129 if(a < b){ return a; }
00130 else{ return b; }
00131 }
00132
00133
00134
00135
00136
00137
00138
00139 static inline int minimum(u_int a, u_int b){
00140 if(a < b){ return a; }
00141 else{ return b; }
00142 }
00143
00144
00145
00146
00147
00148
00149
00150 static inline float minimum(float a, float b){
00151 if(a < b){ return a; }
00152 else{ return b; }
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 static inline void swap(int& a, int& b){
00164 int tmp = a;
00165 a = b;
00166 b = tmp;
00167 }
00168
00169
00170
00171
00172
00173
00174 static inline void swap(u_int& a, u_int& b){
00175 u_int tmp = a;
00176 a = b;
00177 b = tmp;
00178 }
00179
00180
00181
00182
00183
00184
00185 static inline void swap(float& a, float& b){
00186 float tmp = a;
00187 a = b;
00188 b = tmp;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 static inline int abs(int x){ return ::abs(x); }
00200
00201
00202
00203
00204
00205
00206 static inline float abs(float x){ return ::fabsf(x); }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 static inline float floor(float x){ return ::floorf(x); }
00217
00218
00219
00220
00221
00222
00223 static inline float ceil(float x){ return ::ceilf(x); }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 static inline float fmod(float x, float y){ return ::fmodf(x, y); }
00235
00236
00237
00238
00239
00240
00241
00242 static inline float modf(float x, float* integer){
00243 return ::modff(x, integer);
00244 }
00245
00246
00247
00248
00249
00250
00251 static inline float sqrt(float x){
00252 Assert(x >= 0.f);
00253 return ::sqrtf(x);
00254 }
00255
00256
00257
00258
00259
00260
00261 static inline bool classCheck(float x){
00262 int result = ::_fpclass(x);
00263 if((result == _FPCLASS_SNAN) ||
00264 (result == _FPCLASS_QNAN) ||
00265 (result == _FPCLASS_PINF) ||
00266 (result == _FPCLASS_NINF)){
00267 return false;
00268 }
00269 return true;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 static inline float pow(float x, float y){ return ::powf(x, y); }
00282
00283
00284
00285
00286
00287
00288 static inline float exp(float x){ return ::expf(x); }
00289
00290
00291
00292
00293
00294
00295 static inline float log(float x){ return ::logf(x); }
00296
00297
00298
00299
00300
00301
00302 static inline float log10(float x){ return ::log10f(x); }
00303
00304
00305
00306
00307
00308 static inline bool checkPow2(int value){
00309 while(true){
00310 if(value == 1){ return true; }
00311 if((value % 2) == 1){ return false; }
00312 value /= 2;
00313 }
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 static inline float sin(float x){ return ::sinf(x); }
00325
00326
00327
00328
00329
00330
00331 static inline float cos(float x){ return ::cosf(x); }
00332
00333
00334
00335
00336
00337
00338 static inline float tan(float x){ return ::tanf(x); }
00339
00340
00341
00342
00343
00344
00345 static inline float sinh(float x){ return ::sinhf(x); }
00346
00347
00348
00349
00350
00351
00352 static inline float cosh(float x){ return ::coshf(x); }
00353
00354
00355
00356
00357
00358
00359 static inline float tanh(float x){ return ::tanhf(x); }
00360
00361
00362
00363
00364
00365
00366 static inline float asin(float x){ return ::asinf(x); }
00367
00368
00369
00370
00371
00372
00373 static inline float acos(float x){ return ::acosf(x); }
00374
00375
00376
00377
00378
00379
00380 static inline float atan(float x){ return ::atanf(x); }
00381
00382
00383
00384
00385
00386
00387
00388 static inline float atan2(float y, float x){ return ::atan2f(y, x); }
00389
00390
00391 private:
00392
00393 Math();
00394
00395 };
00396
00397 }
00398 #endif // End of MATH_H_
00399