TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::PrecomputedRandom Class Reference

#include <PrecomputedRandom.h>

Classes

class  HemiUniformData
 
class  SphereBitsData
 

Public Member Functions

 PrecomputedRandom (const HemiUniformData *data1, const SphereBitsData *data2, int dataSize, uint32 seed=0xF018A4D2)
 
 PrecomputedRandom (int dataSize, uint32 seed=0xF018A4D2)
 
 ~PrecomputedRandom ()
 
virtual uint32 bits ()
 
virtual float uniform (float low, float high)
 
virtual float uniform ()
 
virtual void cosHemi (float &x, float &y, float &z)
 
virtual void cosPowHemi (const float k, float &x, float &y, float &z)
 
virtual void sphere (float &x, float &y, float &z)
 
- Public Member Functions inherited from G3D::Random
 Random (uint32 seed=0xF018A4D2, bool threadsafe=true)
 
virtual ~Random ()
 
virtual void reset (uint32 seed=0xF018A4D2, bool threadsafe=true)
 
virtual int integer (int min, int max)
 
virtual float gaussian (float mean, float stdev)
 
virtual void cosSphere (float &x, float &y, float &z)
 
virtual void hemi (float &x, float &y, float &z)
 

Protected Attributes

const HemiUniformDatam_hemiUniform
 
const SphereBitsDatam_sphereBits
 
int m_modMask
 
int m_index
 
bool m_freeData
 
- Protected Attributes inherited from G3D::Random
Spinlock lock
 
uint32state
 
int index
 
bool m_threadsafe
 

Additional Inherited Members

- Static Public Member Functions inherited from G3D::Random
static Randomcommon ()
 
- Protected Types inherited from G3D::Random
enum  {
  N = 624, M = 397, R = 31, U = 11,
  S = 7, T = 15, L = 18, A = 0x9908B0DF,
  B = 0x9D2C5680, C = 0xEFC60000
}
 
- Protected Member Functions inherited from G3D::Random
virtual void generate ()
 
 Random (void *)
 

Detailed Description

Fast random numbers using a precomputed data table.

e.g., generates cosHemi about 13x faster than Random. This is useful for quickly generating seeded random numbers for reproducibility. G3D::Random takes a long time to seed; this is instantaneous (providing the precomputed data is already available.)

Not threadsafe.

Constructor & Destructor Documentation

G3D::PrecomputedRandom::PrecomputedRandom ( const HemiUniformData data1,
const SphereBitsData data2,
int  dataSize,
uint32  seed = 0xF018A4D2 
)
46  :
47  Random((void*)NULL),
48  m_hemiUniform(data1),
49  m_sphereBits(data2),
50  m_modMask(dataSize - 1),
51  m_freeData(false) {
52 
53  m_index = seed & m_modMask;
54  alwaysAssertM(isPow2(dataSize), "dataSize must be a power of 2");
55 }
int m_index
Definition: PrecomputedRandom.h:59
const HemiUniformData * m_hemiUniform
Definition: PrecomputedRandom.h:53
arena_t NULL
Definition: jemalloc_internal.h:624
bool isPow2(int num)
Definition: g3dmath.h:776
int m_modMask
Definition: PrecomputedRandom.h:57
const SphereBitsData * m_sphereBits
Definition: PrecomputedRandom.h:54
Random(void *)
Definition: Random.cpp:22
bool m_freeData
Definition: PrecomputedRandom.h:62
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165

+ Here is the call graph for this function:

G3D::PrecomputedRandom::PrecomputedRandom ( int  dataSize,
uint32  seed = 0xF018A4D2 
)
Parameters
dataSizeNumber of random numbers that can be requested before periodicity. Must be a power of 2.
18  :
19  Random((void*)NULL),
22  m_modMask(dataSize - 1),
23  m_freeData(true) {
24 
25  alwaysAssertM(isPow2(dataSize), "dataSize must be a power of 2");
26  m_index = seed & m_modMask;
27 
28  HemiUniformData* h;
29  SphereBitsData* s;
30  m_hemiUniform = h = (HemiUniformData*) System::malloc(sizeof(HemiUniformData) * dataSize);
31  m_sphereBits = s = (SphereBitsData*) System::malloc(sizeof(SphereBitsData) * dataSize);
32 
33  Random r;
34 
35  for (int i = 0; i < dataSize; ++i) {
36  h[i].uniform = r.uniform();
37  r.cosHemi(h[i].cosHemiX, h[i].cosHemiY, h[i].cosHemiZ);
38 
39  s[i].bits = r.bits();
40  r.sphere(s[i].sphereX, s[i].sphereY, s[i].sphereZ);
41  }
42 
43 }
int m_index
Definition: PrecomputedRandom.h:59
const HemiUniformData * m_hemiUniform
Definition: PrecomputedRandom.h:53
arena_t NULL
Definition: jemalloc_internal.h:624
bool isPow2(int num)
Definition: g3dmath.h:776
int m_modMask
Definition: PrecomputedRandom.h:57
static void * malloc(size_t bytes)
Definition: System.cpp:1441
const SphereBitsData * m_sphereBits
Definition: PrecomputedRandom.h:54
Random(void *)
Definition: Random.cpp:22
bool m_freeData
Definition: PrecomputedRandom.h:62
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165

+ Here is the call graph for this function:

G3D::PrecomputedRandom::~PrecomputedRandom ( )
58  {
59  if (m_freeData) {
60  System::free(const_cast<HemiUniformData*>(m_hemiUniform));
61  System::free(const_cast<SphereBitsData*>(m_sphereBits));
62  }
63 }
const HemiUniformData * m_hemiUniform
Definition: PrecomputedRandom.h:53
const SphereBitsData * m_sphereBits
Definition: PrecomputedRandom.h:54
static void free(void *p)
Definition: System.cpp:1473
bool m_freeData
Definition: PrecomputedRandom.h:62

+ Here is the call graph for this function:

Member Function Documentation

uint32 G3D::PrecomputedRandom::bits ( )
virtual

Each bit is random. Subclasses can choose to override just this method and the other methods will all work automatically.

Reimplemented from G3D::Random.

112  {
113  m_index = (m_index + 1) & m_modMask;
114  return m_sphereBits[m_index].bits;
115 }
int m_index
Definition: PrecomputedRandom.h:59
int m_modMask
Definition: PrecomputedRandom.h:57
uint32 bits
Definition: PrecomputedRandom.h:47
const SphereBitsData * m_sphereBits
Definition: PrecomputedRandom.h:54
void G3D::PrecomputedRandom::cosHemi ( float &  x,
float &  y,
float &  z 
)
virtual

Returns 3D unit vectors distributed according to a cosine distribution about the z axis.

Reimplemented from G3D::Random.

77  {
78  m_index = (m_index + 1) & m_modMask;
82 }
int m_index
Definition: PrecomputedRandom.h:59
const HemiUniformData * m_hemiUniform
Definition: PrecomputedRandom.h:53
int m_modMask
Definition: PrecomputedRandom.h:57
float cosHemiX
Definition: PrecomputedRandom.h:36
float cosHemiY
Definition: PrecomputedRandom.h:37
G3D::int16 z
Definition: Vector3int16.h:46
G3D::int16 y
Definition: Vector2int16.h:38
float cosHemiZ
Definition: PrecomputedRandom.h:38
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the caller graph for this function:

void G3D::PrecomputedRandom::cosPowHemi ( const float  k,
float &  x,
float &  y,
float &  z 
)
virtual

Returns 3D unit vectors distributed according to a cosine power distribution ( $ \mbox{cos}^k \theta $) about the z-axis.

Reimplemented from G3D::Random.

84  {
85  // Computing a cosPowHemi costs 4 slow functions (pow, sqrt, sin,
86  // cos). We can do it with two, given a cosHemi sample, basically
87  // saving the cost of sin and cos and making a single 128-byte
88  // memory read (for a vector) instead of two (for adjacent uniform
89  // floats).
90 
91  // cos^1 distribution sample
92  float cos1;
93  cosHemi(x, y, cos1);
94 
95  // Fix the distribution by adjusting the cosine:
96  // rnd(cos^k t) = (rnd(cos(t))^2)^(1/k)
97 
98  // produces cos^k distribution sample
99  z = pow(cos1, 2.0f / (1.0f + k));
100 
101  // Rescale x and y by sqrt(1.0f - square(z)) / sqrt(x*x + y*y).
102  // Add a very tiny offset to handle the (almost impossibly unlikely) case where
103  // z = 1 and x^2+y^2 = 0.
104  static const float eps = 0.000001f;
105  const float s = sqrt((1.0f + eps - square(z)) / (square(x) + square(y) + eps));
106 
107  x *= s;
108  y *= s;
109 }
virtual void cosHemi(float &x, float &y, float &z)
Definition: PrecomputedRandom.cpp:77
G3D::int16 z
Definition: Vector3int16.h:46
G3D::int16 y
Definition: Vector2int16.h:38
double square(double fValue)
Definition: g3dmath.h:698
G3D::Quat pow(const G3D::Quat &q, double x)
Definition: Quat.h:761
G3D::int16 x
Definition: Vector2int16.h:37
double eps(double a, double b)
Definition: g3dmath.h:824

+ Here is the call graph for this function:

void G3D::PrecomputedRandom::sphere ( float &  x,
float &  y,
float &  z 
)
virtual

Returns 3D unit vectors uniformly distributed on the sphere

Reimplemented from G3D::Random.

118  {
119  m_index = (m_index + 1) & m_modMask;
123 }
int m_index
Definition: PrecomputedRandom.h:59
float sphereY
Definition: PrecomputedRandom.h:45
int m_modMask
Definition: PrecomputedRandom.h:57
float sphereX
Definition: PrecomputedRandom.h:44
const SphereBitsData * m_sphereBits
Definition: PrecomputedRandom.h:54
G3D::int16 z
Definition: Vector3int16.h:46
float sphereZ
Definition: PrecomputedRandom.h:46
G3D::int16 y
Definition: Vector2int16.h:38
G3D::int16 x
Definition: Vector2int16.h:37
float G3D::PrecomputedRandom::uniform ( float  low,
float  high 
)
virtual

Uniform random float on the range [min, max]

Reimplemented from G3D::Random.

65  {
66  m_index = (m_index + 1) & m_modMask;
67  return low + m_hemiUniform[m_index].uniform * (high - low);
68 }
int m_index
Definition: PrecomputedRandom.h:59
const HemiUniformData * m_hemiUniform
Definition: PrecomputedRandom.h:53
int m_modMask
Definition: PrecomputedRandom.h:57
float uniform
Definition: PrecomputedRandom.h:39
float G3D::PrecomputedRandom::uniform ( )
virtual

Uniform random float on the range [0, 1]

Reimplemented from G3D::Random.

71  {
72  m_index = (m_index + 1) & m_modMask;
74 }
int m_index
Definition: PrecomputedRandom.h:59
const HemiUniformData * m_hemiUniform
Definition: PrecomputedRandom.h:53
int m_modMask
Definition: PrecomputedRandom.h:57
float uniform
Definition: PrecomputedRandom.h:39

Member Data Documentation

bool G3D::PrecomputedRandom::m_freeData
protected

If true, free m_hemiUniform and m_sphereBits in destructor

const HemiUniformData* G3D::PrecomputedRandom::m_hemiUniform
protected

Array of 2^n elements.

int G3D::PrecomputedRandom::m_index
protected
int G3D::PrecomputedRandom::m_modMask
protected

2^n - 1; the AND mask for computing a fast modulo

const SphereBitsData* G3D::PrecomputedRandom::m_sphereBits
protected

The documentation for this class was generated from the following files: