## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csutil/floatrand.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2004 by Jorrit Tyberghein
    00003     Copyright (C) 2005 by Eric Sunshine
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    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     This is a variant of the algorithm for computing a random point
    00120     on the unit sphere; the algorithm is suggested in Knuth, v2,
    00121     3rd ed, p136; and attributed to Robert E Knop, CACM, 13 (1970),
    00122     326.
    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 //    float x1, x2, x1sq, x2sq;
    00141 //    while (!haveNrs)
    00142 //    {
    00143 //      x1 = floatGen.Get (-1, 1);
    00144 //      x2 = floatGen.Get (-1, 1);
    00145 //      x1sq = x1*x1;
    00146 //      x2sq = x2*x2;
    00147 //      if ( (x1sq)+(x2sq) < 1) haveNrs = true;
    00148 //    }
    00149 //    csVector3 ret;
    00150 //    ret.x = 2*x1*sqrtf(1-x1sq-x2sq);
    00151 //    ret.y = 2*x2*sqrtf(1-x1sq-x2sq);
    00152 //    ret.z = 1-2*(x1sq+x2sq);
    00153
    00154     return ret;
    00155   }
    00156
    00157 private:
    00158   csRandomFloatGen floatGen;
    00159 };
    00160
    00161 #endif // __CS_CSUTIL_FLOATRAND_H__