void BLI_jitter_init(float (*jitarr)[2], int num) { float (*jit2)[2]; float num_fl, num_fl_sqrt; float x, rad1, rad2, rad3; RNG *rng; int i; if (num == 0) { return; } num_fl = (float)num; num_fl_sqrt = sqrtf(num_fl); jit2 = MEM_mallocN(12 + (unsigned int)num * sizeof(float[2]), "initjit"); rad1 = 1.0f / num_fl_sqrt; rad2 = 1.0f / num_fl; rad3 = num_fl_sqrt / num_fl; rng = BLI_rng_new(31415926 + (unsigned int)num); x = 0; for (i = 0; i < num; i++) { jitarr[i][0] = x + rad1 * (float)(0.5 - BLI_rng_get_double(rng)); jitarr[i][1] = (float)i / num_fl + rad1 * (float)(0.5 - BLI_rng_get_double(rng)); x += rad3; x -= floorf(x); } BLI_rng_free(rng); for (i = 0; i < 24; i++) { BLI_jitterate1(jitarr, jit2, num, rad1); BLI_jitterate1(jitarr, jit2, num, rad1); BLI_jitterate2(jitarr, jit2, num, rad2); } MEM_freeN(jit2); /* finally, move jittertab to be centered around (0, 0) */ for (i = 0; i < num; i++) { jitarr[i][0] -= 0.5f; jitarr[i][1] -= 0.5f; } }
static bool object_rand_transverts( TransVertStore *tvs, const float offset, const float uniform, const float normal_factor, const unsigned int seed) { bool use_normal = (normal_factor != 0.0f); struct RNG *rng; TransVert *tv; int a; if (!tvs || !(tvs->transverts)) { return false; } rng = BLI_rng_new(seed); tv = tvs->transverts; for (a = 0; a < tvs->transverts_tot; a++, tv++) { const float t = max_ff(0.0f, uniform + ((1.0f - uniform) * BLI_rng_get_float(rng))); float vec[3]; BLI_rng_get_float_unit_v3(rng, vec); if (use_normal && (tv->flag & TX_VERT_USE_NORMAL)) { float no[3]; /* avoid >90d rotation to align with normal */ if (dot_v3v3(vec, tv->normal) < 0.0f) { negate_v3_v3(no, tv->normal); } else { copy_v3_v3(no, tv->normal); } interp_v3_v3v3_slerp_safe(vec, vec, no, normal_factor); } madd_v3_v3fl(tv->loc, vec, offset * t); } BLI_rng_free(rng); return true; }
/* modified copy from rayshade.c */ static void hammersley_create(float *out, int n, int seed, float amount) { RNG *rng; double p, t, offs[2]; int k, kk; rng = BLI_rng_new(31415926 + n + seed); offs[0] = BLI_rng_get_double(rng) + (double)amount; offs[1] = BLI_rng_get_double(rng) + (double)amount; BLI_rng_free(rng); for (k = 0; k < n; k++) { t = 0; for (p = 0.5, kk = k; kk; p *= 0.5, kk >>= 1) if (kk & 1) /* kk mod 2 = 1 */ t += p; out[2*k + 0] = fmod((double)k/(double)n + offs[0], 1.0); out[2*k + 1] = fmod(t + offs[1], 1.0); } }
static void precalculate_effector(EffectorCache *eff) { unsigned int cfra = (unsigned int)(eff->scene->r.cfra >= 0 ? eff->scene->r.cfra : -eff->scene->r.cfra); if (!eff->pd->rng) eff->pd->rng = BLI_rng_new(eff->pd->seed + cfra); else BLI_rng_srandom(eff->pd->rng, eff->pd->seed + cfra); if (eff->pd->forcefield == PFIELD_GUIDE && eff->ob->type==OB_CURVE) { Curve *cu= eff->ob->data; if (cu->flag & CU_PATH) { if (eff->ob->curve_cache == NULL || eff->ob->curve_cache->path==NULL || eff->ob->curve_cache->path->data==NULL) BKE_displist_make_curveTypes(eff->scene, eff->ob, 0); if (eff->ob->curve_cache->path && eff->ob->curve_cache->path->data) { where_on_path(eff->ob, 0.0, eff->guide_loc, eff->guide_dir, NULL, &eff->guide_radius, NULL); mul_m4_v3(eff->ob->obmat, eff->guide_loc); mul_mat3_m4_v3(eff->ob->obmat, eff->guide_dir); } } } else if (eff->pd->shape == PFIELD_SHAPE_SURFACE) { eff->surmd = (SurfaceModifierData *)modifiers_findByType( eff->ob, eModifierType_Surface ); if (eff->ob->type == OB_CURVE) eff->flag |= PE_USE_NORMAL_DATA; } else if (eff->psys) psys_update_particle_tree(eff->psys, eff->scene->r.cfra); /* Store object velocity */ if (eff->ob) { float old_vel[3]; BKE_object_where_is_calc_time(eff->scene, eff->ob, cfra - 1.0f); copy_v3_v3(old_vel, eff->ob->obmat[3]); BKE_object_where_is_calc_time(eff->scene, eff->ob, cfra); sub_v3_v3v3(eff->velocity, eff->ob->obmat[3], old_vel); } }
Noise::Noise(long seed) { /* Use Blender RNG for repeatable results across platforms. */ RNG *rng = BLI_rng_new(seed); int i, j, k; for (i = 0; i < _NOISE_B; i++) { p[i] = i; g1[i] = (float)((BLI_rng_get_int(rng) % (_NOISE_B + _NOISE_B)) - _NOISE_B) / _NOISE_B; for (j = 0; j < 2; j++) g2[i][j] = (float)((BLI_rng_get_int(rng) % (_NOISE_B + _NOISE_B)) - _NOISE_B) / _NOISE_B; normalize2(g2[i]); for (j = 0; j < 3; j++) g3[i][j] = (float)((BLI_rng_get_int(rng) % (_NOISE_B + _NOISE_B)) - _NOISE_B) / _NOISE_B; normalize3(g3[i]); } while (--i) { k = p[i]; p[i] = p[j = BLI_rng_get_int(rng) % _NOISE_B]; p[j] = k; } for (i = 0; i < _NOISE_B + 2; i++) { p[_NOISE_B + i] = p[i]; g1[_NOISE_B + i] = g1[i]; for (j = 0; j < 2; j++) g2[_NOISE_B + i][j] = g2[i][j]; for (j = 0; j < 3; j++) g3[_NOISE_B + i][j] = g3[i][j]; } BLI_rng_free(rng); }
/* almost exact copy of BLI_jitter_init */ static void init_mv_jit(float *jit, int num, int seed2, float amount) { RNG *rng; float *jit2, x, rad1, rad2, rad3; int i, num2; if (num==0) return; rad1= (float)(1.0f/sqrtf((float)num)); rad2= (float)(1.0f/((float)num)); rad3= (float)sqrtf((float)num)/((float)num); rng = BLI_rng_new(31415926 + num + seed2); x= 0; num2 = 2 * num; for (i=0; i<num2; i+=2) { jit[i] = x + amount*rad1*(0.5f - BLI_rng_get_float(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+1]-= (float)floor(jit[i+1]); x+= rad3; x -= (float)floor(x); } jit2= MEM_mallocN(12 + 2*sizeof(float)*num, "initjit"); for (i=0 ; i<4 ; i++) { BLI_jitterate1((float (*)[2])jit, (float (*)[2])jit2, num, rad1); BLI_jitterate1((float (*)[2])jit, (float (*)[2])jit2, num, rad1); BLI_jitterate2((float (*)[2])jit, (float (*)[2])jit2, num, rad2); } MEM_freeN(jit2); BLI_rng_free(rng); }
void BKE_brush_system_init(void) { brush_rng = BLI_rng_new(0); BLI_rng_srandom(brush_rng, 31415682); }
static void gp_stroke_path_animation(bContext *C, ReportList *reports, Curve *cu, tGpTimingData *gtd) { Scene *scene = CTX_data_scene(C); bAction *act; FCurve *fcu; PointerRNA ptr; PropertyRNA *prop = NULL; int nbr_gaps = 0, i; if (gtd->mode == GP_STROKECONVERT_TIMING_NONE) return; /* gap_duration and gap_randomness are in frames, but we need seconds!!! */ gtd->gap_duration = FRA2TIME(gtd->gap_duration); gtd->gap_randomness = FRA2TIME(gtd->gap_randomness); /* Enable path! */ cu->flag |= CU_PATH; cu->pathlen = gtd->frame_range; /* Get RNA pointer to read/write path time values */ RNA_id_pointer_create((ID *)cu, &ptr); prop = RNA_struct_find_property(&ptr, "eval_time"); /* Ensure we have an F-Curve to add keyframes to */ act = verify_adt_action((ID *)cu, true); fcu = verify_fcurve(act, NULL, &ptr, "eval_time", 0, true); if (G.debug & G_DEBUG) { printf("%s: tot len: %f\t\ttot time: %f\n", __func__, gtd->tot_dist, gtd->tot_time); for (i = 0; i < gtd->num_points; i++) { printf("\tpoint %d:\t\tlen: %f\t\ttime: %f\n", i, gtd->dists[i], gtd->times[i]); } } if (gtd->mode == GP_STROKECONVERT_TIMING_LINEAR) { float cfra; /* Linear extrapolation! */ fcu->extend = FCURVE_EXTRAPOLATE_LINEAR; cu->ctime = 0.0f; cfra = (float)gtd->start_frame; insert_keyframe_direct(reports, ptr, prop, fcu, cfra, BEZT_KEYTYPE_KEYFRAME, INSERTKEY_FAST); cu->ctime = cu->pathlen; if (gtd->realtime) { cfra += (float)TIME2FRA(gtd->tot_time); /* Seconds to frames */ } else { cfra = (float)gtd->end_frame; } insert_keyframe_direct(reports, ptr, prop, fcu, cfra, BEZT_KEYTYPE_KEYFRAME, INSERTKEY_FAST); } else { /* Use actual recorded timing! */ RNG *rng = BLI_rng_new(0); float time_range; /* CustomGaps specific */ float tot_gaps_time = 0.0f; /* Pre-process gaps, in case we don't want to keep their original timing */ if (gtd->mode == GP_STROKECONVERT_TIMING_CUSTOMGAP) { gp_stroke_path_animation_preprocess_gaps(gtd, rng, &nbr_gaps, &tot_gaps_time); } if (gtd->realtime) { time_range = (float)TIME2FRA(gtd->tot_time); /* Seconds to frames */ } else { time_range = (float)(gtd->end_frame - gtd->start_frame); } if (G.debug & G_DEBUG) { printf("GP Stroke Path Conversion: Starting keying!\n"); } gp_stroke_path_animation_add_keyframes(reports, ptr, prop, fcu, cu, gtd, rng, time_range, nbr_gaps, tot_gaps_time); BLI_rng_free(rng); } /* As we used INSERTKEY_FAST mode, we need to recompute all curve's handles now */ calchandles_fcurve(fcu); if (G.debug & G_DEBUG) { printf("%s: \ntot len: %f\t\ttot time: %f\n", __func__, gtd->tot_dist, gtd->tot_time); for (i = 0; i < gtd->num_points; i++) { printf("\tpoint %d:\t\tlen: %f\t\ttime: %f\n", i, gtd->dists[i], gtd->times[i]); } printf("\n\n"); } WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL); /* send updates */ DAG_id_tag_update(&cu->id, 0); }
void BKE_init_ocean(struct Ocean *o, int M, int N, float Lx, float Lz, float V, float l, float A, float w, float damp, float alignment, float depth, float time, short do_height_field, short do_chop, short do_normals, short do_jacobian, int seed) { RNG *rng; int i, j, ii; BLI_rw_mutex_lock(&o->oceanmutex, THREAD_LOCK_WRITE); o->_M = M; o->_N = N; o->_V = V; o->_l = l; o->_A = A; o->_w = w; o->_damp_reflections = 1.0f - damp; o->_wind_alignment = alignment; o->_depth = depth; o->_Lx = Lx; o->_Lz = Lz; o->_wx = cos(w); o->_wz = -sin(w); /* wave direction */ o->_L = V * V / GRAVITY; /* largest wave for a given velocity V */ o->time = time; o->_do_disp_y = do_height_field; o->_do_normals = do_normals; o->_do_chop = do_chop; o->_do_jacobian = do_jacobian; o->_k = (float *) MEM_mallocN(M * (1 + N / 2) * sizeof(float), "ocean_k"); o->_h0 = (fftw_complex *) MEM_mallocN(M * N * sizeof(fftw_complex), "ocean_h0"); o->_h0_minus = (fftw_complex *) MEM_mallocN(M * N * sizeof(fftw_complex), "ocean_h0_minus"); o->_kx = (float *) MEM_mallocN(o->_M * sizeof(float), "ocean_kx"); o->_kz = (float *) MEM_mallocN(o->_N * sizeof(float), "ocean_kz"); /* make this robust in the face of erroneous usage */ if (o->_Lx == 0.0f) o->_Lx = 0.001f; if (o->_Lz == 0.0f) o->_Lz = 0.001f; /* the +ve components and DC */ for (i = 0; i <= o->_M / 2; ++i) o->_kx[i] = 2.0f * (float)M_PI * i / o->_Lx; /* the -ve components */ for (i = o->_M - 1, ii = 0; i > o->_M / 2; --i, ++ii) o->_kx[i] = -2.0f * (float)M_PI * ii / o->_Lx; /* the +ve components and DC */ for (i = 0; i <= o->_N / 2; ++i) o->_kz[i] = 2.0f * (float)M_PI * i / o->_Lz; /* the -ve components */ for (i = o->_N - 1, ii = 0; i > o->_N / 2; --i, ++ii) o->_kz[i] = -2.0f * (float)M_PI * ii / o->_Lz; /* pre-calculate the k matrix */ for (i = 0; i < o->_M; ++i) for (j = 0; j <= o->_N / 2; ++j) o->_k[i * (1 + o->_N / 2) + j] = sqrt(o->_kx[i] * o->_kx[i] + o->_kz[j] * o->_kz[j]); /*srand(seed);*/ rng = BLI_rng_new(seed); for (i = 0; i < o->_M; ++i) { for (j = 0; j < o->_N; ++j) { float r1 = gaussRand(rng); float r2 = gaussRand(rng); fftw_complex r1r2; init_complex(r1r2, r1, r2); mul_complex_f(o->_h0[i * o->_N + j], r1r2, (float)(sqrt(Ph(o, o->_kx[i], o->_kz[j]) / 2.0f))); mul_complex_f(o->_h0_minus[i * o->_N + j], r1r2, (float)(sqrt(Ph(o, -o->_kx[i], -o->_kz[j]) / 2.0f))); } } o->_fft_in = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in"); o->_htilda = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_htilda"); BLI_lock_thread(LOCK_FFTW); if (o->_do_disp_y) { o->_disp_y = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_disp_y"); o->_disp_y_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in, o->_disp_y, FFTW_ESTIMATE); } if (o->_do_normals) { o->_fft_in_nx = (fftw_complex *) MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_nx"); o->_fft_in_nz = (fftw_complex *) MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_nz"); o->_N_x = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_N_x"); /* o->_N_y = (float *) fftwf_malloc(o->_M * o->_N * sizeof(float)); (MEM01) */ o->_N_z = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_N_z"); o->_N_x_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_nx, o->_N_x, FFTW_ESTIMATE); o->_N_z_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_nz, o->_N_z, FFTW_ESTIMATE); } if (o->_do_chop) { o->_fft_in_x = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_x"); o->_fft_in_z = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_z"); o->_disp_x = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_disp_x"); o->_disp_z = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_disp_z"); o->_disp_x_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_x, o->_disp_x, FFTW_ESTIMATE); o->_disp_z_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_z, o->_disp_z, FFTW_ESTIMATE); } if (o->_do_jacobian) { o->_fft_in_jxx = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_jxx"); o->_fft_in_jzz = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_jzz"); o->_fft_in_jxz = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_jxz"); o->_Jxx = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_Jxx"); o->_Jzz = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_Jzz"); o->_Jxz = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_Jxz"); o->_Jxx_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_jxx, o->_Jxx, FFTW_ESTIMATE); o->_Jzz_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_jzz, o->_Jzz, FFTW_ESTIMATE); o->_Jxz_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_jxz, o->_Jxz, FFTW_ESTIMATE); } BLI_unlock_thread(LOCK_FFTW); BLI_rw_mutex_unlock(&o->oceanmutex); set_height_normalize_factor(o); BLI_rng_free(rng); }