00001 #ifndef _DEFINITIONS_H_
00002 #define _DEFINITIONS_H_
00003
00012 #define WIN32_LEAN_AND_MEAN
00013
00014 #include <stdlib.h>
00015 #include <math.h>
00016 #include <float.h>
00017 #include <limits.h>
00018
00019 #ifdef _WIN32
00020 #define M_PI 3.14159265358979323846f
00021 #endif
00022
00023 #define INV_PI 0.31830988618379067154f
00024 #define INV_TWOPI 0.15915494309189533577f
00025 #define INV_FOURPI 0.079577471545947673f
00026
00027 #define RAY_EPSILON 1e-4f
00028 #define EPSILON 1e-5f
00029
00030 #ifndef INFINITY
00031 #define INFINITY FLT_MAX
00032 #endif
00033
00034 #define FOR(__i,__times) for(int __i=0;__i<__times;__i++)
00035 #define FOR2(__i,__from,__to) for(int __i=__from;__i<=__to;__i++)
00036 #define FOR3(__i,__from,__to) for(int __i=__from;__i<__to;__i++)
00037
00038 inline float lerp(float t, float v1, float v2) {
00039 return (1.f - t) * v1 + t * v2;
00040 }
00041
00042 inline float clamp(float val, float low, float high) {
00043 if (val < low) return low;
00044 else if (val > high) return high;
00045 else return val;
00046 }
00047
00048 inline int clamp(int val, int low, int high) {
00049 if (val < low) return low;
00050 else if (val > high) return high;
00051 else return val;
00052 }
00053
00054 inline int mod(int a, int b) {
00055 int n = int(a/b);
00056 a -= n*b;
00057 if (a < 0)
00058 a += b;
00059 return a;
00060 }
00061
00062 inline float radians(float deg) {
00063 return ((float)M_PI/180.f) * deg;
00064 }
00065
00066 inline float degrees(float rad) {
00067 return (180.f/(float)M_PI) * rad;
00068 }
00069
00070 inline float randf()
00071 {
00072 return (float)rand() / RAND_MAX;
00073 }
00074
00075 inline float random(float lower=0.0, float upper=1.0, int granularity=RAND_MAX-1)
00076 {
00077 float width = (upper - lower) / granularity;
00078 return lower + width * (rand() % (granularity+1));
00079 }
00080
00081 inline int ceilpow2(int x)
00082 {
00083 if (x < 0) return 1;
00084 int ans = 1;
00085 while (ans < x)
00086 ans = ans << 1;
00087 return ans;
00088 }
00089
00090 inline int divceil(int dividend, int divisor)
00091 {
00092 if (dividend <= 0) return 0;
00093 return (dividend-1) / divisor + 1;
00094 }
00095
00096 #endif