2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2007 by Janne Karhu.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): Raul Fernandez Hernandez (Farsthary),
27 * ***** END GPL LICENSE BLOCK *****
30 /** \file blender/blenkernel/intern/particle_distribute.c
36 #include "MEM_guardedalloc.h"
38 #include "BLI_utildefines.h"
39 #include "BLI_jitter_2d.h"
40 #include "BLI_kdtree.h"
42 #include "BLI_math_geom.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_modifier_types.h"
50 #include "DNA_particle_types.h"
51 #include "DNA_scene_types.h"
53 #include "BKE_cdderivedmesh.h"
54 #include "BKE_DerivedMesh.h"
55 #include "BKE_global.h"
57 #include "BKE_object.h"
58 #include "BKE_particle.h"
60 static int psys_render_simplify_distribution(ParticleThreadContext *ctx, int tot);
62 static void alloc_child_particles(ParticleSystem *psys, int tot)
65 /* only re-allocate if we have to */
66 if (psys->part->childtype && psys->totchild == tot) {
67 memset(psys->child, 0, tot*sizeof(ChildParticle));
71 MEM_freeN(psys->child);
76 if (psys->part->childtype) {
79 psys->child= MEM_callocN(psys->totchild*sizeof(ChildParticle), "child_particles");
83 static void distribute_simple_children(Scene *scene, Object *ob, DerivedMesh *finaldm, DerivedMesh *deformdm, ParticleSystem *psys)
85 ChildParticle *cpa = NULL;
87 int child_nbr= psys_get_child_number(scene, psys);
88 int totpart= psys_get_tot_child(scene, psys);
90 alloc_child_particles(psys, totpart);
93 for (i=0; i<child_nbr; i++) {
94 for (p=0; p<psys->totpart; p++,cpa++) {
98 /* create even spherical distribution inside unit sphere */
99 while (length>=1.0f) {
100 cpa->fuv[0]=2.0f*BLI_frand()-1.0f;
101 cpa->fuv[1]=2.0f*BLI_frand()-1.0f;
102 cpa->fuv[2]=2.0f*BLI_frand()-1.0f;
103 length=len_v3(cpa->fuv);
109 /* dmcache must be updated for parent particles if children from faces is used */
110 psys_calc_dmcache(ob, finaldm, deformdm, psys);
112 static void distribute_grid(DerivedMesh *dm, ParticleSystem *psys)
114 ParticleData *pa=NULL;
115 float min[3], max[3], delta[3], d;
116 MVert *mv, *mvert = dm->getVertDataArray(dm,0);
117 int totvert=dm->getNumVerts(dm), from=psys->part->from;
118 int i, j, k, p, res=psys->part->grid_res, size[3], axis;
120 /* find bounding box of dm */
123 copy_v3_v3(min, mv->co);
124 copy_v3_v3(max, mv->co);
126 for (i = 1; i < totvert; i++, mv++) {
127 minmax_v3v3_v3(min, max, mv->co);
135 sub_v3_v3v3(delta, max, min);
137 /* determine major axis */
138 axis = axis_dominant_v3_single(delta);
140 d = delta[axis]/(float)res;
143 size[(axis+1)%3] = (int)ceil(delta[(axis+1)%3]/d);
144 size[(axis+2)%3] = (int)ceil(delta[(axis+2)%3]/d);
146 /* float errors grrr.. */
147 size[(axis+1)%3] = MIN2(size[(axis+1)%3],res);
148 size[(axis+2)%3] = MIN2(size[(axis+2)%3],res);
150 size[0] = MAX2(size[0], 1);
151 size[1] = MAX2(size[1], 1);
152 size[2] = MAX2(size[2], 1);
154 /* no full offset for flat/thin objects */
155 min[0]+= d < delta[0] ? d/2.f : delta[0]/2.f;
156 min[1]+= d < delta[1] ? d/2.f : delta[1]/2.f;
157 min[2]+= d < delta[2] ? d/2.f : delta[2]/2.f;
159 for (i=0,p=0,pa=psys->particles; i<res; i++) {
160 for (j=0; j<res; j++) {
161 for (k=0; k<res; k++,p++,pa++) {
162 pa->fuv[0] = min[0] + (float)i*d;
163 pa->fuv[1] = min[1] + (float)j*d;
164 pa->fuv[2] = min[2] + (float)k*d;
165 pa->flag |= PARS_UNEXIST;
166 pa->hair_index = 0; /* abused in volume calculation */
171 /* enable particles near verts/edges/faces/inside surface */
172 if (from==PART_FROM_VERT) {
181 for (i=0,mv=mvert; i<totvert; i++,mv++) {
182 sub_v3_v3v3(vec,mv->co,min);
186 pa[((int)(vec[0] * (size[0] - 1)) * res +
187 (int)(vec[1] * (size[1] - 1))) * res +
188 (int)(vec[2] * (size[2] - 1))].flag &= ~PARS_UNEXIST;
191 else if (ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
192 float co1[3], co2[3];
194 MFace *mface= NULL, *mface_array;
195 float v1[3], v2[3], v3[3], v4[4], lambda;
196 int a, a1, a2, a0mul, a1mul, a2mul, totface;
197 int amax= from==PART_FROM_FACE ? 3 : 1;
199 totface=dm->getNumTessFaces(dm);
200 mface=mface_array=dm->getTessFaceDataArray(dm,CD_MFACE);
202 for (a=0; a<amax; a++) {
203 if (a==0) { a0mul=res*res; a1mul=res; a2mul=1; }
204 else if (a==1) { a0mul=res; a1mul=1; a2mul=res*res; }
205 else { a0mul=1; a1mul=res*res; a2mul=res; }
207 for (a1=0; a1<size[(a+1)%3]; a1++) {
208 for (a2=0; a2<size[(a+2)%3]; a2++) {
211 pa = psys->particles + a1*a1mul + a2*a2mul;
212 copy_v3_v3(co1, pa->fuv);
213 co1[a] -= d < delta[a] ? d/2.f : delta[a]/2.f;
214 copy_v3_v3(co2, co1);
215 co2[a] += delta[a] + 0.001f*d;
218 struct IsectRayPrecalc isect_precalc;
219 float ray_direction[3];
220 sub_v3_v3v3(ray_direction, co2, co1);
221 isect_ray_tri_watertight_v3_precalc(&isect_precalc, ray_direction);
223 /* lets intersect the faces */
224 for (i=0; i<totface; i++,mface++) {
225 copy_v3_v3(v1, mvert[mface->v1].co);
226 copy_v3_v3(v2, mvert[mface->v2].co);
227 copy_v3_v3(v3, mvert[mface->v3].co);
229 bool intersects_tri = isect_ray_tri_watertight_v3(co1,
233 if (intersects_tri) {
234 if (from==PART_FROM_FACE)
235 (pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST;
236 else /* store number of intersections */
237 (pa+(int)(lambda*size[a])*a0mul)->hair_index++;
240 if (mface->v4 && (!intersects_tri || from==PART_FROM_VOLUME)) {
241 copy_v3_v3(v4, mvert[mface->v4].co);
243 if (isect_ray_tri_watertight_v3(co1,
247 if (from==PART_FROM_FACE)
248 (pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST;
250 (pa+(int)(lambda*size[a])*a0mul)->hair_index++;
255 if (from==PART_FROM_VOLUME) {
256 int in=pa->hair_index%2;
257 if (in) pa->hair_index++;
258 for (i=0; i<size[0]; i++) {
259 if (in || (pa+i*a0mul)->hair_index%2)
260 (pa+i*a0mul)->flag &= ~PARS_UNEXIST;
261 /* odd intersections == in->out / out->in */
262 /* even intersections -> in stays same */
263 in=(in + (pa+i*a0mul)->hair_index) % 2;
271 if (psys->part->flag & PART_GRID_HEXAGONAL) {
272 for (i=0,p=0,pa=psys->particles; i<res; i++) {
273 for (j=0; j<res; j++) {
274 for (k=0; k<res; k++,p++,pa++) {
287 if (psys->part->flag & PART_GRID_INVERT) {
288 for (i=0; i<size[0]; i++) {
289 for (j=0; j<size[1]; j++) {
290 pa=psys->particles + res*(i*res + j);
291 for (k=0; k<size[2]; k++, pa++) {
292 pa->flag ^= PARS_UNEXIST;
298 if (psys->part->grid_rand > 0.f) {
299 float rfac = d * psys->part->grid_rand;
300 for (p=0,pa=psys->particles; p<psys->totpart; p++,pa++) {
301 if (pa->flag & PARS_UNEXIST)
304 pa->fuv[0] += rfac * (psys_frand(psys, p + 31) - 0.5f);
305 pa->fuv[1] += rfac * (psys_frand(psys, p + 32) - 0.5f);
306 pa->fuv[2] += rfac * (psys_frand(psys, p + 33) - 0.5f);
311 /* modified copy from rayshade.c */
312 static void hammersley_create(float *out, int n, int seed, float amount)
315 double p, t, offs[2];
318 rng = BLI_rng_new(31415926 + n + seed);
319 offs[0] = BLI_rng_get_double(rng) + (double)amount;
320 offs[1] = BLI_rng_get_double(rng) + (double)amount;
323 for (k = 0; k < n; k++) {
325 for (p = 0.5, kk = k; kk; p *= 0.5, kk >>= 1)
326 if (kk & 1) /* kk mod 2 = 1 */
329 out[2*k + 0] = fmod((double)k/(double)n + offs[0], 1.0);
330 out[2*k + 1] = fmod(t + offs[1], 1.0);
334 /* almost exact copy of BLI_jitter_init */
335 static void init_mv_jit(float *jit, int num, int seed2, float amount)
338 float *jit2, x, rad1, rad2, rad3;
343 rad1= (float)(1.0f/sqrtf((float)num));
344 rad2= (float)(1.0f/((float)num));
345 rad3= (float)sqrtf((float)num)/((float)num);
347 rng = BLI_rng_new(31415926 + num + seed2);
350 for (i=0; i<num2; i+=2) {
352 jit[i] = x + amount*rad1*(0.5f - BLI_rng_get_float(rng));
353 jit[i+1] = i/(2.0f*num) + amount*rad1*(0.5f - BLI_rng_get_float(rng));
355 jit[i]-= (float)floor(jit[i]);
356 jit[i+1]-= (float)floor(jit[i+1]);
359 x -= (float)floor(x);
362 jit2= MEM_mallocN(12 + 2*sizeof(float)*num, "initjit");
364 for (i=0 ; i<4 ; i++) {
365 BLI_jitterate1((float (*)[2])jit, (float (*)[2])jit2, num, rad1);
366 BLI_jitterate1((float (*)[2])jit, (float (*)[2])jit2, num, rad1);
367 BLI_jitterate2((float (*)[2])jit, (float (*)[2])jit2, num, rad2);
373 static void psys_uv_to_w(float u, float v, int quad, float *w)
375 float vert[4][3], co[3];
384 vert[0][0] = 0.0f; vert[0][1] = 0.0f; vert[0][2] = 0.0f;
385 vert[1][0] = 1.0f; vert[1][1] = 0.0f; vert[1][2] = 0.0f;
386 vert[2][0] = 1.0f; vert[2][1] = 1.0f; vert[2][2] = 0.0f;
393 vert[3][0] = 0.0f; vert[3][1] = 1.0f; vert[3][2] = 0.0f;
394 interp_weights_poly_v3( w,vert, 4, co);
397 interp_weights_poly_v3( w,vert, 3, co);
402 /* Find the index in "sum" array before "value" is crossed. */
403 static int distribute_binary_search(float *sum, int n, float value)
405 int mid, low = 0, high = n - 1;
410 if (sum[low] >= value)
413 if (sum[high - 1] < value)
417 mid = (low + high) / 2;
419 if ((sum[mid] >= value) && (sum[mid - 1] < value))
422 if (sum[mid] > value) {
433 /* the max number if calls to rng_* funcs within psys_thread_distribute_particle
434 * be sure to keep up to date if this changes */
435 #define PSYS_RND_DIST_SKIP 2
437 /* note: this function must be thread safe, for from == PART_FROM_CHILD */
438 #define ONLY_WORKING_WITH_PA_VERTS 0
439 static void distribute_from_verts_exec(ParticleTask *thread, ParticleData *pa, int p)
441 ParticleThreadContext *ctx= thread->ctx;
444 mface = ctx->dm->getTessFaceDataArray(ctx->dm, CD_MFACE);
446 int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
448 /* TODO_PARTICLE - use original index */
449 pa->num = ctx->index[p];
453 if (pa->num != DMCACHE_NOTFOUND && pa->num < ctx->dm->getNumVerts(ctx->dm)) {
455 /* This finds the first face to contain the emitting vertex,
456 * this is not ideal, but is mostly fine as UV seams generally
457 * map to equal-colored parts of a texture */
458 for (int i = 0; i < ctx->dm->getNumTessFaces(ctx->dm); i++, mface++) {
459 if (ELEM(pa->num, mface->v1, mface->v2, mface->v3, mface->v4)) {
460 unsigned int *vert = &mface->v1;
462 for (int j = 0; j < 4; j++, vert++) {
463 if (*vert == pa->num) {
474 #if ONLY_WORKING_WITH_PA_VERTS
476 KDTreeNearest ptn[3];
479 psys_particle_on_dm(ctx->dm,from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co1,0,0,0,orco1,0);
480 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco1, 1, 1);
481 maxw = BLI_kdtree_find_nearest_n(ctx->tree,orco1,ptn,3);
483 for (w=0; w<maxw; w++) {
484 pa->verts[w]=ptn->num;
489 if (rng_skip_tot > 0) /* should never be below zero */
490 BLI_rng_skip(thread->rng, rng_skip_tot);
493 static void distribute_from_faces_exec(ParticleTask *thread, ParticleData *pa, int p) {
494 ParticleThreadContext *ctx= thread->ctx;
495 DerivedMesh *dm= ctx->dm;
497 int distr= ctx->distr;
499 int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
503 pa->num = i = ctx->index[p];
504 mface = dm->getTessFaceData(dm,i,CD_MFACE);
508 if (ctx->jitlevel == 1) {
510 psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
512 psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
515 float offset = fmod(ctx->jitoff[i] + (float)p, (float)ctx->jitlevel);
516 if (!isnan(offset)) {
517 psys_uv_to_w(ctx->jit[2*(int)offset], ctx->jit[2*(int)offset+1], mface->v4, pa->fuv);
521 case PART_DISTR_RAND:
522 randu= BLI_rng_get_float(thread->rng);
523 randv= BLI_rng_get_float(thread->rng);
526 psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
531 if (rng_skip_tot > 0) /* should never be below zero */
532 BLI_rng_skip(thread->rng, rng_skip_tot);
535 static void distribute_from_volume_exec(ParticleTask *thread, ParticleData *pa, int p) {
536 ParticleThreadContext *ctx= thread->ctx;
537 DerivedMesh *dm= ctx->dm;
538 float *v1, *v2, *v3, *v4, nor[3], co[3];
539 float cur_d, min_d, randu, randv;
540 int distr= ctx->distr;
541 int i, intersect, tot;
542 int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
545 MVert *mvert=dm->getVertDataArray(dm,CD_MVERT);
547 pa->num = i = ctx->index[p];
548 mface = dm->getTessFaceData(dm,i,CD_MFACE);
552 if (ctx->jitlevel == 1) {
554 psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
556 psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
559 float offset = fmod(ctx->jitoff[i] + (float)p, (float)ctx->jitlevel);
560 if (!isnan(offset)) {
561 psys_uv_to_w(ctx->jit[2*(int)offset], ctx->jit[2*(int)offset+1], mface->v4, pa->fuv);
565 case PART_DISTR_RAND:
566 randu= BLI_rng_get_float(thread->rng);
567 randv= BLI_rng_get_float(thread->rng);
570 psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
576 tot=dm->getNumTessFaces(dm);
578 psys_interpolate_face(mvert,mface,0,0,pa->fuv,co,nor,0,0,0,0);
586 for (i=0,mface=dm->getTessFaceDataArray(dm,CD_MFACE); i<tot; i++,mface++) {
587 if (i==pa->num) continue;
589 v1=mvert[mface->v1].co;
590 v2=mvert[mface->v2].co;
591 v3=mvert[mface->v3].co;
593 if (isect_ray_tri_v3(co, nor, v2, v3, v1, &cur_d, NULL)) {
596 pa->foffset=cur_d*0.5f; /* to the middle of volume */
601 v4=mvert[mface->v4].co;
603 if (isect_ray_tri_v3(co, nor, v4, v1, v3, &cur_d, NULL)) {
606 pa->foffset=cur_d*0.5f; /* to the middle of volume */
617 pa->foffset *= ctx->jit[p % (2 * ctx->jitlevel)];
619 case PART_DISTR_RAND:
620 pa->foffset *= BLI_frand();
625 if (rng_skip_tot > 0) /* should never be below zero */
626 BLI_rng_skip(thread->rng, rng_skip_tot);
629 static void distribute_children_exec(ParticleTask *thread, ChildParticle *cpa, int p) {
630 ParticleThreadContext *ctx= thread->ctx;
631 Object *ob= ctx->sim.ob;
632 DerivedMesh *dm= ctx->dm;
633 float orco1[3], co1[3], nor1[3];
635 int cfrom= ctx->cfrom;
637 int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
641 if (ctx->index[p] < 0) {
643 cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3]=0.0f;
644 cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0;
648 mf= dm->getTessFaceData(dm, ctx->index[p], CD_MFACE);
650 randu= BLI_rng_get_float(thread->rng);
651 randv= BLI_rng_get_float(thread->rng);
654 psys_uv_to_w(randu, randv, mf->v4, cpa->fuv);
656 cpa->num = ctx->index[p];
659 KDTreeNearest ptn[10];
660 int w,maxw;//, do_seams;
661 float maxd /*, mind,dd */, totw= 0.0f;
665 psys_particle_on_dm(dm,cfrom,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co1,nor1,NULL,NULL,orco1,NULL);
666 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco1, 1, 1);
667 maxw = BLI_kdtree_find_nearest_n(ctx->tree,orco1,ptn,3);
669 maxd=ptn[maxw-1].dist;
670 /* mind=ptn[0].dist; */ /* UNUSED */
672 /* the weights here could be done better */
673 for (w=0; w<maxw; w++) {
674 parent[w]=ptn[w].index;
675 pweight[w]=(float)pow(2.0,(double)(-6.0f*ptn[w].dist/maxd));
682 for (w=0,i=0; w<maxw && i<4; w++) {
684 cpa->pa[i]=parent[w];
685 cpa->w[i]=pweight[w];
696 for (w = 0; w < 4; w++) {
701 cpa->parent=cpa->pa[0];
704 if (rng_skip_tot > 0) /* should never be below zero */
705 BLI_rng_skip(thread->rng, rng_skip_tot);
708 static void exec_distribute_parent(TaskPool * __restrict UNUSED(pool), void *taskdata, int UNUSED(threadid))
710 ParticleTask *task = taskdata;
711 ParticleSystem *psys= task->ctx->sim.psys;
715 BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP * task->begin);
717 pa= psys->particles + task->begin;
718 switch (psys->part->from) {
720 for (p = task->begin; p < task->end; ++p, ++pa)
721 distribute_from_faces_exec(task, pa, p);
723 case PART_FROM_VOLUME:
724 for (p = task->begin; p < task->end; ++p, ++pa)
725 distribute_from_volume_exec(task, pa, p);
728 for (p = task->begin; p < task->end; ++p, ++pa)
729 distribute_from_verts_exec(task, pa, p);
734 static void exec_distribute_child(TaskPool * __restrict UNUSED(pool), void *taskdata, int UNUSED(threadid))
736 ParticleTask *task = taskdata;
737 ParticleSystem *psys = task->ctx->sim.psys;
741 /* RNG skipping at the beginning */
743 for (p = 0; p < task->begin; ++p, ++cpa) {
744 if (task->ctx->skip) /* simplification skip */
745 BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP * task->ctx->skip[p]);
747 BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP);
750 for (; p < task->end; ++p, ++cpa) {
751 if (task->ctx->skip) /* simplification skip */
752 BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP * task->ctx->skip[p]);
754 distribute_children_exec(task, cpa, p);
758 static int distribute_compare_orig_index(const void *p1, const void *p2, void *user_data)
760 int *orig_index = (int *) user_data;
761 int index1 = orig_index[*(const int *)p1];
762 int index2 = orig_index[*(const int *)p2];
766 else if (index1 == index2) {
767 /* this pointer comparison appears to make qsort stable for glibc,
768 * and apparently on solaris too, makes the renders reproducible */
780 static void distribute_invalid(Scene *scene, ParticleSystem *psys, int from)
782 if (from == PART_FROM_CHILD) {
784 int p, totchild = psys_get_tot_child(scene, psys);
786 if (psys->child && totchild) {
787 for (p=0,cpa=psys->child; p<totchild; p++,cpa++) {
788 cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3] = 0.0;
791 cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0;
799 pa->fuv[0] = pa->fuv[1] = pa->fuv[2] = pa->fuv[3] = 0.0;
806 /* Creates a distribution of coordinates on a DerivedMesh */
807 /* This is to denote functionality that does not yet work with mesh - only derived mesh */
808 static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, ParticleSimulationData *sim, int from)
810 Scene *scene = sim->scene;
811 DerivedMesh *finaldm = sim->psmd->dm_final;
812 Object *ob = sim->ob;
813 ParticleSystem *psys= sim->psys;
814 ParticleData *pa=0, *tpars= 0;
815 ParticleSettings *part;
816 ParticleSeam *seams= 0;
818 DerivedMesh *dm= NULL;
822 int totelem=0, totpart, *particle_element=0, children=0, totseam=0;
823 int jitlevel= 1, distr;
824 float *element_weight=NULL,*jitter_offset=NULL, *vweight=NULL;
825 float cur, maxweight=0.0, tweight, totweight, inv_totweight, co[3], nor[3], orco[3];
827 if (ELEM(NULL, ob, psys, psys->part))
831 totpart=psys->totpart;
835 if (!finaldm->deformedOnly && !finaldm->getTessFaceDataArray(finaldm, CD_ORIGINDEX)) {
836 printf("Can't create particles with the current modifier stack, disable destructive modifiers\n");
837 // XXX error("Can't paint with the current modifier stack, disable destructive modifiers");
841 /* XXX This distribution code is totally broken in case from == PART_FROM_CHILD, it's always using finaldm
842 * even if use_modifier_stack is unset... But making things consistent here break all existing edited
843 * hair systems, so better wait for complete rewrite.
846 psys_thread_context_init(ctx, sim);
848 /* First handle special cases */
849 if (from == PART_FROM_CHILD) {
850 /* Simple children */
851 if (part->childtype != PART_CHILD_FACES) {
852 BLI_srandom(31415926 + psys->seed + psys->child_seed);
853 distribute_simple_children(scene, ob, finaldm, sim->psmd->dm_deformed, psys);
858 /* Grid distribution */
859 if (part->distr==PART_DISTR_GRID && from != PART_FROM_VERT) {
860 BLI_srandom(31415926 + psys->seed);
862 if (psys->part->use_modifier_stack) {
866 dm = CDDM_from_mesh((Mesh*)ob->data);
868 DM_ensure_tessface(dm);
870 distribute_grid(dm,psys);
880 /* Create trees and original coordinates if needed */
881 if (from == PART_FROM_CHILD) {
882 distr=PART_DISTR_RAND;
883 BLI_srandom(31415926 + psys->seed + psys->child_seed);
887 DM_ensure_tessface(dm);
891 tree=BLI_kdtree_new(totpart);
893 for (p=0,pa=psys->particles; p<totpart; p++,pa++) {
894 psys_particle_on_dm(dm,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,nor,0,0,orco,NULL);
895 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco, 1, 1);
896 BLI_kdtree_insert(tree, p, orco);
899 BLI_kdtree_balance(tree);
901 totpart = psys_get_tot_child(scene, psys);
902 cfrom = from = PART_FROM_FACE;
906 BLI_srandom(31415926 + psys->seed);
908 if (psys->part->use_modifier_stack)
911 dm= CDDM_from_mesh((Mesh*)ob->data);
913 DM_ensure_tessface(dm);
915 /* we need orco for consistent distributions */
916 if (!CustomData_has_layer(&dm->vertData, CD_ORCO))
917 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, BKE_mesh_orco_verts_get(ob));
919 if (from == PART_FROM_VERT) {
920 MVert *mv= dm->getVertDataArray(dm, CD_MVERT);
921 float (*orcodata)[3] = dm->getVertDataArray(dm, CD_ORCO);
922 int totvert = dm->getNumVerts(dm);
924 tree=BLI_kdtree_new(totvert);
926 for (p=0; p<totvert; p++) {
928 copy_v3_v3(co,orcodata[p]);
929 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co, 1, 1);
932 copy_v3_v3(co,mv[p].co);
933 BLI_kdtree_insert(tree, p, co);
936 BLI_kdtree_balance(tree);
940 /* Get total number of emission elements and allocate needed arrays */
941 totelem = (from == PART_FROM_VERT) ? dm->getNumVerts(dm) : dm->getNumTessFaces(dm);
944 distribute_invalid(scene, psys, children ? PART_FROM_CHILD : 0);
946 if (G.debug & G_DEBUG)
947 fprintf(stderr,"Particle distribution error: Nothing to emit from!\n");
949 if (dm != finaldm) dm->release(dm);
951 BLI_kdtree_free(tree);
956 element_weight = MEM_callocN(sizeof(float)*totelem, "particle_distribution_weights");
957 particle_element= MEM_callocN(sizeof(int)*totpart, "particle_distribution_indexes");
958 jitter_offset = MEM_callocN(sizeof(float)*totelem, "particle_distribution_jitoff");
960 /* Calculate weights from face areas */
961 if ((part->flag&PART_EDISTR || children) && from != PART_FROM_VERT) {
962 MVert *v1, *v2, *v3, *v4;
963 float totarea=0.f, co1[3], co2[3], co3[3], co4[3];
964 float (*orcodata)[3];
966 orcodata= dm->getVertDataArray(dm, CD_ORCO);
968 for (i=0; i<totelem; i++) {
969 MFace *mf=dm->getTessFaceData(dm,i,CD_MFACE);
972 copy_v3_v3(co1, orcodata[mf->v1]);
973 copy_v3_v3(co2, orcodata[mf->v2]);
974 copy_v3_v3(co3, orcodata[mf->v3]);
975 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co1, 1, 1);
976 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co2, 1, 1);
977 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co3, 1, 1);
979 copy_v3_v3(co4, orcodata[mf->v4]);
980 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co4, 1, 1);
984 v1= (MVert*)dm->getVertData(dm,mf->v1,CD_MVERT);
985 v2= (MVert*)dm->getVertData(dm,mf->v2,CD_MVERT);
986 v3= (MVert*)dm->getVertData(dm,mf->v3,CD_MVERT);
987 copy_v3_v3(co1, v1->co);
988 copy_v3_v3(co2, v2->co);
989 copy_v3_v3(co3, v3->co);
991 v4= (MVert*)dm->getVertData(dm,mf->v4,CD_MVERT);
992 copy_v3_v3(co4, v4->co);
996 cur = mf->v4 ? area_quad_v3(co1, co2, co3, co4) : area_tri_v3(co1, co2, co3);
1001 element_weight[i] = cur;
1005 for (i=0; i<totelem; i++)
1006 element_weight[i] /= totarea;
1008 maxweight /= totarea;
1011 float min=1.0f/(float)(MIN2(totelem,totpart));
1012 for (i=0; i<totelem; i++)
1013 element_weight[i]=min;
1017 /* Calculate weights from vgroup */
1018 vweight = psys_cache_vgroup(dm,psys,PSYS_VG_DENSITY);
1021 if (from==PART_FROM_VERT) {
1022 for (i=0;i<totelem; i++)
1023 element_weight[i]*=vweight[i];
1025 else { /* PART_FROM_FACE / PART_FROM_VOLUME */
1026 for (i=0;i<totelem; i++) {
1027 MFace *mf=dm->getTessFaceData(dm,i,CD_MFACE);
1028 tweight = vweight[mf->v1] + vweight[mf->v2] + vweight[mf->v3];
1031 tweight += vweight[mf->v4];
1038 element_weight[i]*=tweight;
1044 /* Calculate total weight of all elements */
1047 for (i = 0; i < totelem; i++) {
1048 if (element_weight[i] > 0.0f) {
1050 totweight += element_weight[i];
1054 if (totmapped == 0) {
1055 /* We are not allowed to distribute particles anywhere... */
1059 inv_totweight = 1.0f / totweight;
1061 /* Calculate cumulative weights.
1062 * We remove all null-weighted elements from element_sum, and create a new mapping
1063 * 'activ'_elem_index -> orig_elem_index.
1064 * This simplifies greatly the filtering of zero-weighted items - and can be much more efficient
1065 * especially in random case (reducing a lot the size of binary-searched array)...
1067 float *element_sum = MEM_mallocN(sizeof(*element_sum) * totmapped, __func__);
1068 int *element_map = MEM_mallocN(sizeof(*element_map) * totmapped, __func__);
1071 for (i = 0; i < totelem && element_weight[i] == 0.0f; i++);
1072 element_sum[i_mapped] = element_weight[i] * inv_totweight;
1073 element_map[i_mapped] = i;
1075 for (i++; i < totelem; i++) {
1076 if (element_weight[i] > 0.0f) {
1077 element_sum[i_mapped] = element_sum[i_mapped - 1] + element_weight[i] * inv_totweight;
1078 /* Skip elements which weight is so small that it does not affect the sum. */
1079 if (element_sum[i_mapped] > element_sum[i_mapped - 1]) {
1080 element_map[i_mapped] = i;
1085 totmapped = i_mapped;
1087 /* Finally assign elements to particles */
1088 if ((part->flag & PART_TRAND) || (part->simplify_flag & PART_SIMPLIFY_ENABLE)) {
1089 for (p = 0; p < totpart; p++) {
1090 /* In theory element_sum[totmapped - 1] should be 1.0,
1091 * but due to float errors this is not necessarily always true, so scale pos accordingly. */
1092 const float pos = BLI_frand() * element_sum[totmapped - 1];
1093 const int eidx = distribute_binary_search(element_sum, totmapped, pos);
1094 particle_element[p] = element_map[eidx];
1095 BLI_assert(pos <= element_sum[eidx]);
1096 BLI_assert(eidx ? (pos > element_sum[eidx - 1]) : (pos >= 0.0f));
1097 jitter_offset[particle_element[p]] = pos;
1103 step = (totpart < 2) ? 0.5 : 1.0 / (double)totpart;
1104 /* This is to address tricky issues with vertex-emitting when user tries (and expects) exact 1-1 vert/part
1105 * distribution (see T47983 and its two example files). It allows us to consider pos as
1106 * 'midpoint between v and v+1' (or 'p and p+1', depending whether we have more vertices than particles or not),
1107 * and avoid stumbling over float imprecisions in element_sum.
1108 * Note: moved face and volume distribution to this as well (instead of starting at zero),
1109 * for the same reasons, see T52682. */
1110 pos = (totpart < totmapped) ? 0.5 / (double)totmapped : step * 0.5; /* We choose the smaller step. */
1112 for (i = 0, p = 0; p < totpart; p++, pos += step) {
1113 for ( ; (i < totmapped - 1) && (pos > (double)element_sum[i]); i++);
1115 particle_element[p] = element_map[i];
1117 jitter_offset[particle_element[p]] = pos;
1121 MEM_freeN(element_sum);
1122 MEM_freeN(element_map);
1124 /* For hair, sort by origindex (allows optimization's in rendering), */
1125 /* however with virtual parents the children need to be in random order. */
1126 if (part->type == PART_HAIR && !(part->childtype==PART_CHILD_FACES && part->parents != 0.0f)) {
1127 int *orig_index = NULL;
1129 if (from == PART_FROM_VERT) {
1130 if (dm->numVertData)
1131 orig_index = dm->getVertDataArray(dm, CD_ORIGINDEX);
1134 if (dm->numTessFaceData)
1135 orig_index = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
1139 BLI_qsort_r(particle_element, totpart, sizeof(int), distribute_compare_orig_index, orig_index);
1143 /* Create jittering if needed */
1144 if (distr==PART_DISTR_JIT && ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
1145 jitlevel= part->userjit;
1147 if (jitlevel == 0) {
1148 jitlevel= totpart/totelem;
1149 if (part->flag & PART_EDISTR) jitlevel*= 2; /* looks better in general, not very scientific */
1150 if (jitlevel<3) jitlevel= 3;
1153 jit= MEM_callocN((2+ jitlevel*2)*sizeof(float), "jit");
1155 /* for small amounts of particles we use regular jitter since it looks
1156 * a bit better, for larger amounts we switch to hammersley sequence
1157 * because it is much faster */
1159 init_mv_jit(jit, jitlevel, psys->seed, part->jitfac);
1161 hammersley_create(jit, jitlevel+1, psys->seed, part->jitfac);
1162 BLI_array_randomize(jit, 2*sizeof(float), jitlevel, psys->seed); /* for custom jit or even distribution */
1165 /* Setup things for threaded distribution */
1168 ctx->totseam= totseam;
1169 ctx->sim.psys= psys;
1170 ctx->index= particle_element;
1172 ctx->jitlevel= jitlevel;
1173 ctx->jitoff= jitter_offset;
1174 ctx->weight= element_weight;
1175 ctx->maxweight= maxweight;
1182 totpart= psys_render_simplify_distribution(ctx, totpart);
1183 alloc_child_particles(psys, totpart);
1189 static void psys_task_init_distribute(ParticleTask *task, ParticleSimulationData *sim)
1191 /* init random number generator */
1192 int seed = 31415926 + sim->psys->seed;
1194 task->rng = BLI_rng_new(seed);
1197 static void distribute_particles_on_dm(ParticleSimulationData *sim, int from)
1199 TaskScheduler *task_scheduler;
1200 TaskPool *task_pool;
1201 ParticleThreadContext ctx;
1202 ParticleTask *tasks;
1203 DerivedMesh *finaldm = sim->psmd->dm_final;
1204 int i, totpart, numtasks;
1206 /* create a task pool for distribution tasks */
1207 if (!psys_thread_context_init_distribute(&ctx, sim, from))
1210 task_scheduler = BLI_task_scheduler_get();
1211 task_pool = BLI_task_pool_create(task_scheduler, &ctx);
1213 totpart = (from == PART_FROM_CHILD ? sim->psys->totchild : sim->psys->totpart);
1214 psys_tasks_create(&ctx, 0, totpart, &tasks, &numtasks);
1215 for (i = 0; i < numtasks; ++i) {
1216 ParticleTask *task = &tasks[i];
1218 psys_task_init_distribute(task, sim);
1219 if (from == PART_FROM_CHILD)
1220 BLI_task_pool_push(task_pool, exec_distribute_child, task, false, TASK_PRIORITY_LOW);
1222 BLI_task_pool_push(task_pool, exec_distribute_parent, task, false, TASK_PRIORITY_LOW);
1224 BLI_task_pool_work_and_wait(task_pool);
1226 BLI_task_pool_free(task_pool);
1228 psys_calc_dmcache(sim->ob, finaldm, sim->psmd->dm_deformed, sim->psys);
1230 if (ctx.dm != finaldm)
1231 ctx.dm->release(ctx.dm);
1233 psys_tasks_free(tasks, numtasks);
1235 psys_thread_context_free(&ctx);
1238 /* ready for future use, to emit particles without geometry */
1239 static void distribute_particles_on_shape(ParticleSimulationData *sim, int UNUSED(from))
1241 distribute_invalid(sim->scene, sim->psys, 0);
1243 fprintf(stderr,"Shape emission not yet possible!\n");
1246 void distribute_particles(ParticleSimulationData *sim, int from)
1253 distribute_particles_on_dm(sim, from);
1258 distribute_particles_on_shape(sim, from);
1261 distribute_invalid(sim->scene, sim->psys, from);
1263 fprintf(stderr,"Particle distribution error!\n");
1267 /* ======== Simplify ======== */
1269 static float psys_render_viewport_falloff(double rate, float dist, float width)
1271 return pow(rate, dist / width);
1274 static float psys_render_projected_area(ParticleSystem *psys, const float center[3], float area, double vprate, float *viewport)
1276 ParticleRenderData *data = psys->renderdata;
1277 float co[4], view[3], ortho1[3], ortho2[3], w, dx, dy, radius;
1279 /* transform to view space */
1280 copy_v3_v3(co, center);
1282 mul_m4_v4(data->viewmat, co);
1284 /* compute two vectors orthogonal to view vector */
1285 normalize_v3_v3(view, co);
1286 ortho_basis_v3v3_v3(ortho1, ortho2, view);
1288 /* compute on screen minification */
1289 w = co[2] * data->winmat[2][3] + data->winmat[3][3];
1290 dx = data->winx * ortho2[0] * data->winmat[0][0];
1291 dy = data->winy * ortho2[1] * data->winmat[1][1];
1292 w = sqrtf(dx * dx + dy * dy) / w;
1294 /* w squared because we are working with area */
1295 area = area * w * w;
1297 /* viewport of the screen test */
1299 /* project point on screen */
1300 mul_m4_v4(data->winmat, co);
1301 if (co[3] != 0.0f) {
1302 co[0] = 0.5f * data->winx * (1.0f + co[0] / co[3]);
1303 co[1] = 0.5f * data->winy * (1.0f + co[1] / co[3]);
1306 /* screen space radius */
1307 radius = sqrtf(area / (float)M_PI);
1309 /* make smaller using fallof once over screen edge */
1312 if (co[0] + radius < 0.0f)
1313 *viewport *= psys_render_viewport_falloff(vprate, -(co[0] + radius), data->winx);
1314 else if (co[0] - radius > data->winx)
1315 *viewport *= psys_render_viewport_falloff(vprate, (co[0] - radius) - data->winx, data->winx);
1317 if (co[1] + radius < 0.0f)
1318 *viewport *= psys_render_viewport_falloff(vprate, -(co[1] + radius), data->winy);
1319 else if (co[1] - radius > data->winy)
1320 *viewport *= psys_render_viewport_falloff(vprate, (co[1] - radius) - data->winy, data->winy);
1325 /* BMESH_TODO, for orig face data, we need to use MPoly */
1326 static int psys_render_simplify_distribution(ParticleThreadContext *ctx, int tot)
1328 DerivedMesh *dm = ctx->dm;
1329 Mesh *me = (Mesh *)(ctx->sim.ob->data);
1332 ParticleRenderData *data;
1333 ParticleRenderElem *elems, *elem;
1334 ParticleSettings *part = ctx->sim.psys->part;
1335 float *facearea, (*facecenter)[3], size[3], fac, powrate, scaleclamp;
1336 float co1[3], co2[3], co3[3], co4[3], lambda, arearatio, t, area, viewport;
1339 int a, b, totorigface, totface, newtot, skipped;
1342 const int *index_mf_to_mpoly;
1343 const int *index_mp_to_orig;
1345 if (part->ren_as != PART_DRAW_PATH || !(part->draw & PART_DRAW_REN_STRAND))
1347 if (!ctx->sim.psys->renderdata)
1350 data = ctx->sim.psys->renderdata;
1351 if (data->timeoffset)
1353 if (!(part->simplify_flag & PART_SIMPLIFY_ENABLE))
1356 mvert = dm->getVertArray(dm);
1357 mface = dm->getTessFaceArray(dm);
1358 totface = dm->getNumTessFaces(dm);
1359 totorigface = me->totpoly;
1361 if (totface == 0 || totorigface == 0)
1364 index_mf_to_mpoly = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
1365 index_mp_to_orig = dm->getPolyDataArray(dm, CD_ORIGINDEX);
1366 if (index_mf_to_mpoly == NULL) {
1367 index_mp_to_orig = NULL;
1370 facearea = MEM_callocN(sizeof(float) * totorigface, "SimplifyFaceArea");
1371 facecenter = MEM_callocN(sizeof(float[3]) * totorigface, "SimplifyFaceCenter");
1372 facetotvert = MEM_callocN(sizeof(int) * totorigface, "SimplifyFaceArea");
1373 elems = MEM_callocN(sizeof(ParticleRenderElem) * totorigface, "SimplifyFaceElem");
1376 MEM_freeN(data->elems);
1378 data->do_simplify = true;
1379 data->elems = elems;
1380 data->index_mf_to_mpoly = index_mf_to_mpoly;
1381 data->index_mp_to_orig = index_mp_to_orig;
1383 /* compute number of children per original face */
1384 for (a = 0; a < tot; a++) {
1385 b = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, ctx->index[a]) : ctx->index[a];
1386 if (b != ORIGINDEX_NONE) {
1387 elems[b].totchild++;
1391 /* compute areas and centers of original faces */
1392 for (mf = mface, a = 0; a < totface; a++, mf++) {
1393 b = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, a) : a;
1395 if (b != ORIGINDEX_NONE) {
1396 copy_v3_v3(co1, mvert[mf->v1].co);
1397 copy_v3_v3(co2, mvert[mf->v2].co);
1398 copy_v3_v3(co3, mvert[mf->v3].co);
1400 add_v3_v3(facecenter[b], co1);
1401 add_v3_v3(facecenter[b], co2);
1402 add_v3_v3(facecenter[b], co3);
1405 copy_v3_v3(co4, mvert[mf->v4].co);
1406 add_v3_v3(facecenter[b], co4);
1407 facearea[b] += area_quad_v3(co1, co2, co3, co4);
1408 facetotvert[b] += 4;
1411 facearea[b] += area_tri_v3(co1, co2, co3);
1412 facetotvert[b] += 3;
1417 for (a = 0; a < totorigface; a++)
1418 if (facetotvert[a] > 0)
1419 mul_v3_fl(facecenter[a], 1.0f / facetotvert[a]);
1421 /* for conversion from BU area / pixel area to reference screen size */
1422 BKE_mesh_texspace_get(me, 0, 0, size);
1423 fac = ((size[0] + size[1] + size[2]) / 3.0f) / part->simplify_refsize;
1426 powrate = log(0.5f) / log(part->simplify_rate * 0.5f);
1427 if (part->simplify_flag & PART_SIMPLIFY_VIEWPORT)
1428 vprate = pow(1.0f - part->simplify_viewport, 5.0);
1432 /* set simplification parameters per original face */
1433 for (a = 0, elem = elems; a < totorigface; a++, elem++) {
1434 area = psys_render_projected_area(ctx->sim.psys, facecenter[a], facearea[a], vprate, &viewport);
1435 arearatio = fac * area / facearea[a];
1437 if ((arearatio < 1.0f || viewport < 1.0f) && elem->totchild) {
1438 /* lambda is percentage of elements to keep */
1439 lambda = (arearatio < 1.0f) ? powf(arearatio, powrate) : 1.0f;
1442 lambda = MAX2(lambda, 1.0f / elem->totchild);
1444 /* compute transition region */
1445 t = part->simplify_transition;
1446 elem->t = (lambda - t < 0.0f) ? lambda : (lambda + t > 1.0f) ? 1.0f - lambda : t;
1449 /* scale at end and beginning of the transition region */
1450 elem->scalemax = (lambda + t < 1.0f) ? 1.0f / lambda : 1.0f / (1.0f - elem->t * elem->t / t);
1451 elem->scalemin = (lambda + t < 1.0f) ? 0.0f : elem->scalemax * (1.0f - elem->t / t);
1453 elem->scalemin = sqrtf(elem->scalemin);
1454 elem->scalemax = sqrtf(elem->scalemax);
1457 scaleclamp = (float)min_ii(elem->totchild, 10);
1458 elem->scalemin = MIN2(scaleclamp, elem->scalemin);
1459 elem->scalemax = MIN2(scaleclamp, elem->scalemax);
1461 /* extend lambda to include transition */
1462 lambda = lambda + elem->t;
1469 elem->scalemax = 1.0f; //sqrt(lambda);
1470 elem->scalemin = 1.0f; //sqrt(lambda);
1474 elem->lambda = lambda;
1475 elem->scalemin = sqrtf(elem->scalemin);
1476 elem->scalemax = sqrtf(elem->scalemax);
1480 MEM_freeN(facearea);
1481 MEM_freeN(facecenter);
1482 MEM_freeN(facetotvert);
1484 /* move indices and set random number skipping */
1485 ctx->skip = MEM_callocN(sizeof(int) * tot, "SimplificationSkip");
1488 for (a = 0, newtot = 0; a < tot; a++) {
1489 b = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, ctx->index[a]) : ctx->index[a];
1491 if (b != ORIGINDEX_NONE) {
1492 if (elems[b].curchild++ < ceil(elems[b].lambda * elems[b].totchild)) {
1493 ctx->index[newtot] = ctx->index[a];
1494 ctx->skip[newtot] = skipped;
1503 for (a = 0, elem = elems; a < totorigface; a++, elem++)