code cleanup: give rng functions BLI prefix.

This commit is contained in:
Campbell Barton 2012-10-23 13:50:44 +00:00
parent da9f9c27bc
commit 101660c809
7 changed files with 75 additions and 78 deletions

@ -166,7 +166,7 @@ void free_partdeflect(PartDeflect *pd)
pd->tex->id.us--; pd->tex->id.us--;
if (pd->rng) if (pd->rng)
rng_free(pd->rng); BLI_rng_free(pd->rng);
MEM_freeN(pd); MEM_freeN(pd);
} }
@ -175,9 +175,9 @@ static void precalculate_effector(EffectorCache *eff)
{ {
unsigned int cfra = (unsigned int)(eff->scene->r.cfra >= 0 ? eff->scene->r.cfra : -eff->scene->r.cfra); unsigned int cfra = (unsigned int)(eff->scene->r.cfra >= 0 ? eff->scene->r.cfra : -eff->scene->r.cfra);
if (!eff->pd->rng) if (!eff->pd->rng)
eff->pd->rng = rng_new(eff->pd->seed + cfra); eff->pd->rng = BLI_rng_new(eff->pd->seed + cfra);
else else
rng_srandom(eff->pd->rng, eff->pd->seed + cfra); BLI_rng_srandom(eff->pd->rng, eff->pd->seed + cfra);
if (eff->pd->forcefield == PFIELD_GUIDE && eff->ob->type==OB_CURVE) { if (eff->pd->forcefield == PFIELD_GUIDE && eff->ob->type==OB_CURVE) {
Curve *cu= eff->ob->data; Curve *cu= eff->ob->data;
@ -455,8 +455,8 @@ static float eff_calc_visibility(ListBase *colliders, EffectorCache *eff, Effect
// noise function for wind e.g. // noise function for wind e.g.
static float wind_func(struct RNG *rng, float strength) static float wind_func(struct RNG *rng, float strength)
{ {
int random = (rng_getInt(rng)+1) % 128; // max 2357 int random = (BLI_rng_get_int(rng)+1) % 128; // max 2357
float force = rng_getFloat(rng) + 1.0f; float force = BLI_rng_get_float(rng) + 1.0f;
float ret; float ret;
float sign = 0; float sign = 0;

@ -2464,7 +2464,7 @@ static int psys_threads_init_path(ParticleThread *threads, Scene *scene, float c
totthread = 1; totthread = 1;
for (i = 0; i < totthread; i++) { for (i = 0; i < totthread; i++) {
threads[i].rng_path = rng_new(seed); threads[i].rng_path = BLI_rng_new(seed);
threads[i].tot = totthread; threads[i].tot = totthread;
} }

@ -632,10 +632,10 @@ static void hammersley_create(float *out, int n, int seed, float amount)
double p, t, offs[2]; double p, t, offs[2];
int k, kk; int k, kk;
rng = rng_new(31415926 + n + seed); rng = BLI_rng_new(31415926 + n + seed);
offs[0] = rng_getDouble(rng) + (double)amount; offs[0] = BLI_rng_get_double(rng) + (double)amount;
offs[1] = rng_getDouble(rng) + (double)amount; offs[1] = BLI_rng_get_double(rng) + (double)amount;
rng_free(rng); BLI_rng_free(rng);
for (k = 0; k < n; k++) { for (k = 0; k < n; k++) {
t = 0; t = 0;
@ -661,13 +661,13 @@ static void init_mv_jit(float *jit, int num, int seed2, float amount)
rad2= (float)(1.0f/((float)num)); rad2= (float)(1.0f/((float)num));
rad3= (float)sqrt((float)num)/((float)num); rad3= (float)sqrt((float)num)/((float)num);
rng = rng_new(31415926 + num + seed2); rng = BLI_rng_new(31415926 + num + seed2);
x= 0; x= 0;
num2 = 2 * num; num2 = 2 * num;
for (i=0; i<num2; i+=2) { for (i=0; i<num2; i+=2) {
jit[i] = x + amount*rad1*(0.5f - rng_getFloat(rng)); jit[i] = x + amount*rad1*(0.5f - BLI_rng_get_float(rng));
jit[i+1] = i/(2.0f*num) + amount*rad1*(0.5f - rng_getFloat(rng)); jit[i+1] = i/(2.0f*num) + amount*rad1*(0.5f - BLI_rng_get_float(rng));
jit[i]-= (float)floor(jit[i]); jit[i]-= (float)floor(jit[i]);
jit[i+1]-= (float)floor(jit[i+1]); jit[i+1]-= (float)floor(jit[i+1]);
@ -684,7 +684,7 @@ static void init_mv_jit(float *jit, int num, int seed2, float amount)
BLI_jitterate2(jit, jit2, num, rad2); BLI_jitterate2(jit, jit2, num, rad2);
} }
MEM_freeN(jit2); MEM_freeN(jit2);
rng_free(rng); BLI_rng_free(rng);
} }
static void psys_uv_to_w(float u, float v, int quad, float *w) static void psys_uv_to_w(float u, float v, int quad, float *w)
@ -804,8 +804,8 @@ static void distribute_threads_exec(ParticleThread *thread, ParticleData *pa, Ch
} }
break; break;
case PART_DISTR_RAND: case PART_DISTR_RAND:
randu= rng_getFloat(thread->rng); randu= BLI_rng_get_float(thread->rng);
randv= rng_getFloat(thread->rng); randv= BLI_rng_get_float(thread->rng);
rng_skip_tot -= 2; rng_skip_tot -= 2;
psys_uv_to_w(randu, randv, mface->v4, pa->fuv); psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
@ -881,8 +881,8 @@ static void distribute_threads_exec(ParticleThread *thread, ParticleData *pa, Ch
mf= dm->getTessFaceData(dm, ctx->index[p], CD_MFACE); mf= dm->getTessFaceData(dm, ctx->index[p], CD_MFACE);
randu= rng_getFloat(thread->rng); randu= BLI_rng_get_float(thread->rng);
randv= rng_getFloat(thread->rng); randv= BLI_rng_get_float(thread->rng);
rng_skip_tot -= 2; rng_skip_tot -= 2;
psys_uv_to_w(randu, randv, mf->v4, cpa->fuv); psys_uv_to_w(randu, randv, mf->v4, cpa->fuv);
@ -934,7 +934,7 @@ static void distribute_threads_exec(ParticleThread *thread, ParticleData *pa, Ch
} }
if (rng_skip_tot > 0) /* should never be below zero */ if (rng_skip_tot > 0) /* should never be below zero */
rng_skip(thread->rng, rng_skip_tot); BLI_rng_skip(thread->rng, rng_skip_tot);
} }
static void *distribute_threads_exec_cb(void *data) static void *distribute_threads_exec_cb(void *data)
@ -951,12 +951,12 @@ static void *distribute_threads_exec_cb(void *data)
for (p=0; p<totpart; p++, cpa++) { for (p=0; p<totpart; p++, cpa++) {
if (thread->ctx->skip) /* simplification skip */ if (thread->ctx->skip) /* simplification skip */
rng_skip(thread->rng, PSYS_RND_DIST_SKIP * thread->ctx->skip[p]); BLI_rng_skip(thread->rng, PSYS_RND_DIST_SKIP * thread->ctx->skip[p]);
if ((p+thread->num) % thread->tot == 0) if ((p+thread->num) % thread->tot == 0)
distribute_threads_exec(thread, NULL, cpa, p); distribute_threads_exec(thread, NULL, cpa, p);
else /* thread skip */ else /* thread skip */
rng_skip(thread->rng, PSYS_RND_DIST_SKIP); BLI_rng_skip(thread->rng, PSYS_RND_DIST_SKIP);
} }
} }
else { else {
@ -1353,7 +1353,7 @@ static int distribute_threads_init_data(ParticleThread *threads, Scene *scene, D
seed= 31415926 + ctx->sim.psys->seed; seed= 31415926 + ctx->sim.psys->seed;
for (i=0; i<totthread; i++) { for (i=0; i<totthread; i++) {
threads[i].rng= rng_new(seed); threads[i].rng= BLI_rng_new(seed);
threads[i].tot= totthread; threads[i].tot= totthread;
} }
@ -1492,9 +1492,9 @@ void psys_threads_free(ParticleThread *threads)
/* threads */ /* threads */
for (i=0; i<totthread; i++) { for (i=0; i<totthread; i++) {
if (threads[i].rng) if (threads[i].rng)
rng_free(threads[i].rng); BLI_rng_free(threads[i].rng);
if (threads[i].rng_path) if (threads[i].rng_path)
rng_free(threads[i].rng_path); BLI_rng_free(threads[i].rng_path);
} }
MEM_freeN(ctx); MEM_freeN(ctx);

@ -40,18 +40,18 @@
struct RNG; struct RNG;
typedef struct RNG RNG; typedef struct RNG RNG;
struct RNG *rng_new(unsigned int seed); struct RNG *BLI_rng_new(unsigned int seed);
void rng_free(struct RNG *rng); void BLI_rng_free(struct RNG *rng);
void rng_seed(struct RNG *rng, unsigned int seed); void BLI_rng_seed(struct RNG *rng, unsigned int seed);
void rng_srandom(struct RNG *rng, unsigned int seed); void BLI_rng_srandom(struct RNG *rng, unsigned int seed);
int rng_getInt(struct RNG *rng); int BLI_rng_get_int(struct RNG *rng);
double rng_getDouble(struct RNG *rng); double BLI_rng_get_double(struct RNG *rng);
float rng_getFloat(struct RNG *rng); float BLI_rng_get_float(struct RNG *rng);
void rng_shuffleArray(struct RNG *rng, void *data, int elemSize, int numElems); void BLI_rng_shuffle_array(struct RNG *rng, void *data, int elemSize, int numElems);
/** Note that skipping is as slow as generating n numbers! */ /** Note that skipping is as slow as generating n numbers! */
void rng_skip(struct RNG *rng, int n); void BLI_rng_skip(struct RNG *rng, int n);
/** Seed the random number generator */ /** Seed the random number generator */
void BLI_srand(unsigned int seed); void BLI_srand(unsigned int seed);
@ -94,7 +94,4 @@ int BLI_thread_rand(int thread);
/** Allows up to BLENDER_MAX_THREADS threads to address */ /** Allows up to BLENDER_MAX_THREADS threads to address */
float BLI_thread_frand(int thread); float BLI_thread_frand(int thread);
#endif /* __BLI_RAND_H__ */
#endif

@ -64,51 +64,51 @@ struct RNG {
r_uint64 X; r_uint64 X;
}; };
RNG *rng_new(unsigned int seed) RNG *BLI_rng_new(unsigned int seed)
{ {
RNG *rng = MEM_mallocN(sizeof(*rng), "rng"); RNG *rng = MEM_mallocN(sizeof(*rng), "rng");
rng_seed(rng, seed); BLI_rng_seed(rng, seed);
return rng; return rng;
} }
void rng_free(RNG *rng) void BLI_rng_free(RNG *rng)
{ {
MEM_freeN(rng); MEM_freeN(rng);
} }
void rng_seed(RNG *rng, unsigned int seed) void BLI_rng_seed(RNG *rng, unsigned int seed)
{ {
rng->X = (((r_uint64) seed) << 16) | LOWSEED; rng->X = (((r_uint64) seed) << 16) | LOWSEED;
} }
void rng_srandom(RNG *rng, unsigned int seed) void BLI_rng_srandom(RNG *rng, unsigned int seed)
{ {
rng_seed(rng, seed + hash[seed & 255]); BLI_rng_seed(rng, seed + hash[seed & 255]);
seed = rng_getInt(rng); seed = BLI_rng_get_int(rng);
rng_seed(rng, seed + hash[seed & 255]); BLI_rng_seed(rng, seed + hash[seed & 255]);
seed = rng_getInt(rng); seed = BLI_rng_get_int(rng);
rng_seed(rng, seed + hash[seed & 255]); BLI_rng_seed(rng, seed + hash[seed & 255]);
} }
int rng_getInt(RNG *rng) int BLI_rng_get_int(RNG *rng)
{ {
rng->X = (MULTIPLIER * rng->X + ADDEND) & MASK; rng->X = (MULTIPLIER * rng->X + ADDEND) & MASK;
return (int) (rng->X >> 17); return (int) (rng->X >> 17);
} }
double rng_getDouble(RNG *rng) double BLI_rng_get_double(RNG *rng)
{ {
return (double) rng_getInt(rng) / 0x80000000; return (double) BLI_rng_get_int(rng) / 0x80000000;
} }
float rng_getFloat(RNG *rng) float BLI_rng_get_float(RNG *rng)
{ {
return (float) rng_getInt(rng) / 0x80000000; return (float) BLI_rng_get_int(rng) / 0x80000000;
} }
void rng_shuffleArray(RNG *rng, void *data, int elemSize, int numElems) void BLI_rng_shuffle_array(RNG *rng, void *data, int elemSize, int numElems)
{ {
int i = numElems; int i = numElems;
void *temp; void *temp;
@ -122,7 +122,7 @@ void rng_shuffleArray(RNG *rng, void *data, int elemSize, int numElems)
/* XXX Shouldn't it rather be "while (i--) {" ? /* XXX Shouldn't it rather be "while (i--) {" ?
* Else we have no guaranty first (0) element has a chance to be shuffled... --mont29 */ * Else we have no guaranty first (0) element has a chance to be shuffled... --mont29 */
while (--i) { while (--i) {
int j = rng_getInt(rng) % numElems; int j = BLI_rng_get_int(rng) % numElems;
if (i != j) { if (i != j) {
void *iElem = (unsigned char *)data + i * elemSize; void *iElem = (unsigned char *)data + i * elemSize;
void *jElem = (unsigned char *)data + j * elemSize; void *jElem = (unsigned char *)data + j * elemSize;
@ -135,12 +135,12 @@ void rng_shuffleArray(RNG *rng, void *data, int elemSize, int numElems)
free(temp); free(temp);
} }
void rng_skip(RNG *rng, int n) void BLI_rng_skip(RNG *rng, int n)
{ {
int i; int i;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
rng_getInt(rng); BLI_rng_get_int(rng);
} }
/***/ /***/
@ -150,28 +150,28 @@ static RNG theBLI_rng = {0};
/* note, this one creates periodical patterns */ /* note, this one creates periodical patterns */
void BLI_srand(unsigned int seed) void BLI_srand(unsigned int seed)
{ {
rng_seed(&theBLI_rng, seed); BLI_rng_seed(&theBLI_rng, seed);
} }
/* using hash table to create better seed */ /* using hash table to create better seed */
void BLI_srandom(unsigned int seed) void BLI_srandom(unsigned int seed)
{ {
rng_srandom(&theBLI_rng, seed); BLI_rng_srandom(&theBLI_rng, seed);
} }
int BLI_rand(void) int BLI_rand(void)
{ {
return rng_getInt(&theBLI_rng); return BLI_rng_get_int(&theBLI_rng);
} }
double BLI_drand(void) double BLI_drand(void)
{ {
return rng_getDouble(&theBLI_rng); return BLI_rng_get_double(&theBLI_rng);
} }
float BLI_frand(void) float BLI_frand(void)
{ {
return rng_getFloat(&theBLI_rng); return BLI_rng_get_float(&theBLI_rng);
} }
void BLI_fillrand(void *addr, int len) void BLI_fillrand(void *addr, int len)
@ -179,16 +179,16 @@ void BLI_fillrand(void *addr, int len)
RNG rng; RNG rng;
unsigned char *p = addr; unsigned char *p = addr;
rng_seed(&rng, (unsigned int) (PIL_check_seconds_timer() * 0x7FFFFFFF)); BLI_rng_seed(&rng, (unsigned int) (PIL_check_seconds_timer() * 0x7FFFFFFF));
while (len--) *p++ = rng_getInt(&rng) & 0xFF; while (len--) *p++ = BLI_rng_get_int(&rng) & 0xFF;
} }
void BLI_array_randomize(void *data, int elemSize, int numElems, unsigned int seed) void BLI_array_randomize(void *data, int elemSize, int numElems, unsigned int seed)
{ {
RNG rng; RNG rng;
rng_seed(&rng, seed); BLI_rng_seed(&rng, seed);
rng_shuffleArray(&rng, data, elemSize, numElems); BLI_rng_shuffle_array(&rng, data, elemSize, numElems);
} }
/* ********* for threaded random ************** */ /* ********* for threaded random ************** */
@ -200,20 +200,20 @@ void BLI_thread_srandom(int thread, unsigned int seed)
if (thread >= BLENDER_MAX_THREADS) if (thread >= BLENDER_MAX_THREADS)
thread = 0; thread = 0;
rng_seed(&rng_tab[thread], seed + hash[seed & 255]); BLI_rng_seed(&rng_tab[thread], seed + hash[seed & 255]);
seed = rng_getInt(&rng_tab[thread]); seed = BLI_rng_get_int(&rng_tab[thread]);
rng_seed(&rng_tab[thread], seed + hash[seed & 255]); BLI_rng_seed(&rng_tab[thread], seed + hash[seed & 255]);
seed = rng_getInt(&rng_tab[thread]); seed = BLI_rng_get_int(&rng_tab[thread]);
rng_seed(&rng_tab[thread], seed + hash[seed & 255]); BLI_rng_seed(&rng_tab[thread], seed + hash[seed & 255]);
} }
int BLI_thread_rand(int thread) int BLI_thread_rand(int thread)
{ {
return rng_getInt(&rng_tab[thread]); return BLI_rng_get_int(&rng_tab[thread]);
} }
float BLI_thread_frand(int thread) float BLI_thread_frand(int thread)
{ {
return rng_getFloat(&rng_tab[thread]); return BLI_rng_get_float(&rng_tab[thread]);
} }

@ -3306,7 +3306,7 @@ static void p_chart_stretch_minimize(PChart *chart, RNG *rng)
trusted_radius /= 2 * nedges; trusted_radius /= 2 * nedges;
random_angle = rng_getFloat(rng) * 2.0f * (float)M_PI; random_angle = BLI_rng_get_float(rng) * 2.0f * (float)M_PI;
dir[0] = trusted_radius * cosf(random_angle); dir[0] = trusted_radius * cosf(random_angle);
dir[1] = trusted_radius * sinf(random_angle); dir[1] = trusted_radius * sinf(random_angle);
@ -4285,7 +4285,7 @@ void param_stretch_begin(ParamHandle *handle)
param_assert(phandle->state == PHANDLE_STATE_CONSTRUCTED); param_assert(phandle->state == PHANDLE_STATE_CONSTRUCTED);
phandle->state = PHANDLE_STATE_STRETCH; phandle->state = PHANDLE_STATE_STRETCH;
phandle->rng = rng_new(31415926); phandle->rng = BLI_rng_new(31415926);
phandle->blend = 0.0f; phandle->blend = 0.0f;
for (i = 0; i < phandle->ncharts; i++) { for (i = 0; i < phandle->ncharts; i++) {
@ -4332,7 +4332,7 @@ void param_stretch_end(ParamHandle *handle)
param_assert(phandle->state == PHANDLE_STATE_STRETCH); param_assert(phandle->state == PHANDLE_STATE_STRETCH);
phandle->state = PHANDLE_STATE_CONSTRUCTED; phandle->state = PHANDLE_STATE_CONSTRUCTED;
rng_free(phandle->rng); BLI_rng_free(phandle->rng);
phandle->rng = NULL; phandle->rng = NULL;
} }

@ -1613,7 +1613,7 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
psys->flag |= PSYS_DRAWING; psys->flag |= PSYS_DRAWING;
rng= rng_new(psys->seed); rng= BLI_rng_new(psys->seed);
totpart=psys->totpart; totpart=psys->totpart;
@ -1763,7 +1763,7 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
/* 3. start creating renderable things */ /* 3. start creating renderable things */
for (a=0, pa=pars; a<totpart+totchild; a++, pa++, seed++) { for (a=0, pa=pars; a<totpart+totchild; a++, pa++, seed++) {
random = rng_getFloat(rng); random = BLI_rng_get_float(rng);
/* setup per particle individual stuff */ /* setup per particle individual stuff */
if (a<totpart) { if (a<totpart) {
if (pa->flag & PARS_UNEXIST) continue; if (pa->flag & PARS_UNEXIST) continue;
@ -2099,7 +2099,7 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
if (states) if (states)
MEM_freeN(states); MEM_freeN(states);
rng_free(rng); BLI_rng_free(rng);
psys->flag &= ~PSYS_DRAWING; psys->flag &= ~PSYS_DRAWING;