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 #ifndef __CS_CSUTIL_FLOATRAND_H__
00021 #define __CS_CSUTIL_FLOATRAND_H__
00022
00027 #include "csextern.h"
00028
00029 #include "csgeom/vector3.h"
00030
00034 class CS_CRYSTALSPACE_EXPORT csRandomFloatGen
00035 {
00036 private:
00037 unsigned int seed;
00038
00039 public:
00041 csRandomFloatGen ()
00042 { Initialize(); }
00044 csRandomFloatGen (unsigned int seed)
00045 { Initialize(seed); }
00046
00048 void Initialize();
00050 void Initialize(unsigned int new_seed)
00051 { seed = new_seed; }
00052
00054 inline float Get()
00055 {
00056 unsigned int const b = 1664525;
00057 unsigned int const c = 1013904223;
00058 seed = b * seed + c;
00059 union
00060 {
00061 uint32 jabi;
00062 float tastic;
00063 } pun;
00064 pun.jabi = 0x3f800000 | (0x007fffff & seed);
00065 return pun.tastic - 1.0;
00066 }
00067
00069 inline float Get(float max)
00070 {
00071 return max * Get();
00072 }
00073
00075 inline float Get(float min, float max)
00076 {
00077 float const w = max - min;
00078 return min + w * Get();
00079 }
00080
00082 inline float GetAngle()
00083 {
00084 return TWO_PI * Get();
00085 }
00086 };
00087
00091 class CS_CRYSTALSPACE_EXPORT csRandomVectorGen
00092 {
00093 public:
00094 public:
00096 csRandomVectorGen ()
00097 : floatGen ()
00098 {}
00100 csRandomVectorGen (unsigned int seed)
00101 : floatGen (seed)
00102 {}
00103
00105 void Initialize()
00106 {
00107 floatGen.Initialize ();
00108 }
00110 void Initialize(unsigned int new_seed)
00111 {
00112 floatGen.Initialize (new_seed);
00113 }
00114
00116 inline csVector3 Get ()
00117 {
00118
00119
00120
00121
00122
00123
00124 csVector3 ret (0.0f);
00125 float s, a;
00126
00127 do
00128 {
00129 ret.x = 2.0f * floatGen.Get () - 1.0f;
00130 ret.y = 2.0f * floatGen.Get () - 1.0f;
00131 s = ret.SquaredNorm ();
00132 } while(s > 1.0f);
00133
00134 ret.z = 2.0f * s - 1.0f;
00135
00136 a = 2.0f * sqrtf (1.0f - s);
00137 ret.x *= a;
00138 ret.y *= a;
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 return ret;
00155 }
00156
00157 private:
00158 csRandomFloatGen floatGen;
00159 };
00160
00161 #endif // __CS_CSUTIL_FLOATRAND_H__