Code comments regarding last commit (forgotten to add before)
[blender.git] / source / blender / blenkernel / intern / particle_distribute.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2007 by Janne Karhu.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Raul Fernandez Hernandez (Farsthary),
24  *                 Stephen Swhitehorn,
25  *                 Lukas Toenne
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/blenkernel/intern/particle_distribute.c
31  *  \ingroup bke
32  */
33
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_utildefines.h"
39 #include "BLI_jitter.h"
40 #include "BLI_kdtree.h"
41 #include "BLI_math.h"
42 #include "BLI_rand.h"
43 #include "BLI_sort.h"
44 #include "BLI_task.h"
45
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_modifier_types.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_scene_types.h"
51
52 #include "BKE_cdderivedmesh.h"
53 #include "BKE_DerivedMesh.h"
54 #include "BKE_global.h"
55 #include "BKE_mesh.h"
56 #include "BKE_object.h"
57 #include "BKE_particle.h"
58
59 static int psys_render_simplify_distribution(ParticleThreadContext *ctx, int tot);
60
61 static void alloc_child_particles(ParticleSystem *psys, int tot)
62 {
63         if (psys->child) {
64                 /* only re-allocate if we have to */
65                 if (psys->part->childtype && psys->totchild == tot) {
66                         memset(psys->child, 0, tot*sizeof(ChildParticle));
67                         return;
68                 }
69
70                 MEM_freeN(psys->child);
71                 psys->child=NULL;
72                 psys->totchild=0;
73         }
74
75         if (psys->part->childtype) {
76                 psys->totchild= tot;
77                 if (psys->totchild)
78                         psys->child= MEM_callocN(psys->totchild*sizeof(ChildParticle), "child_particles");
79         }
80 }
81
82 static void distribute_simple_children(Scene *scene, Object *ob, DerivedMesh *finaldm, DerivedMesh *deformdm, ParticleSystem *psys)
83 {
84         ChildParticle *cpa = NULL;
85         int i, p;
86         int child_nbr= psys_get_child_number(scene, psys);
87         int totpart= psys_get_tot_child(scene, psys);
88
89         alloc_child_particles(psys, totpart);
90
91         cpa = psys->child;
92         for (i=0; i<child_nbr; i++) {
93                 for (p=0; p<psys->totpart; p++,cpa++) {
94                         float length=2.0;
95                         cpa->parent=p;
96                                         
97                         /* create even spherical distribution inside unit sphere */
98                         while (length>=1.0f) {
99                                 cpa->fuv[0]=2.0f*BLI_frand()-1.0f;
100                                 cpa->fuv[1]=2.0f*BLI_frand()-1.0f;
101                                 cpa->fuv[2]=2.0f*BLI_frand()-1.0f;
102                                 length=len_v3(cpa->fuv);
103                         }
104
105                         cpa->num=-1;
106                 }
107         }
108         /* dmcache must be updated for parent particles if children from faces is used */
109         psys_calc_dmcache(ob, finaldm, deformdm, psys);
110 }
111 static void distribute_grid(DerivedMesh *dm, ParticleSystem *psys)
112 {
113         ParticleData *pa=NULL;
114         float min[3], max[3], delta[3], d;
115         MVert *mv, *mvert = dm->getVertDataArray(dm,0);
116         int totvert=dm->getNumVerts(dm), from=psys->part->from;
117         int i, j, k, p, res=psys->part->grid_res, size[3], axis;
118
119         /* find bounding box of dm */
120         if (totvert > 0) {
121                 mv=mvert;
122                 copy_v3_v3(min, mv->co);
123                 copy_v3_v3(max, mv->co);
124                 mv++;
125                 for (i = 1; i < totvert; i++, mv++) {
126                         minmax_v3v3_v3(min, max, mv->co);
127                 }
128         }
129         else {
130                 zero_v3(min);
131                 zero_v3(max);
132         }
133
134         sub_v3_v3v3(delta, max, min);
135
136         /* determine major axis */
137         axis = axis_dominant_v3_single(delta);
138          
139         d = delta[axis]/(float)res;
140
141         size[axis] = res;
142         size[(axis+1)%3] = (int)ceil(delta[(axis+1)%3]/d);
143         size[(axis+2)%3] = (int)ceil(delta[(axis+2)%3]/d);
144
145         /* float errors grrr.. */
146         size[(axis+1)%3] = MIN2(size[(axis+1)%3],res);
147         size[(axis+2)%3] = MIN2(size[(axis+2)%3],res);
148
149         size[0] = MAX2(size[0], 1);
150         size[1] = MAX2(size[1], 1);
151         size[2] = MAX2(size[2], 1);
152
153         /* no full offset for flat/thin objects */
154         min[0]+= d < delta[0] ? d/2.f : delta[0]/2.f;
155         min[1]+= d < delta[1] ? d/2.f : delta[1]/2.f;
156         min[2]+= d < delta[2] ? d/2.f : delta[2]/2.f;
157
158         for (i=0,p=0,pa=psys->particles; i<res; i++) {
159                 for (j=0; j<res; j++) {
160                         for (k=0; k<res; k++,p++,pa++) {
161                                 pa->fuv[0] = min[0] + (float)i*d;
162                                 pa->fuv[1] = min[1] + (float)j*d;
163                                 pa->fuv[2] = min[2] + (float)k*d;
164                                 pa->flag |= PARS_UNEXIST;
165                                 pa->hair_index = 0; /* abused in volume calculation */
166                         }
167                 }
168         }
169
170         /* enable particles near verts/edges/faces/inside surface */
171         if (from==PART_FROM_VERT) {
172                 float vec[3];
173
174                 pa=psys->particles;
175
176                 min[0] -= d/2.0f;
177                 min[1] -= d/2.0f;
178                 min[2] -= d/2.0f;
179
180                 for (i=0,mv=mvert; i<totvert; i++,mv++) {
181                         sub_v3_v3v3(vec,mv->co,min);
182                         vec[0]/=delta[0];
183                         vec[1]/=delta[1];
184                         vec[2]/=delta[2];
185                         pa[((int)(vec[0] * (size[0] - 1))  * res +
186                             (int)(vec[1] * (size[1] - 1))) * res +
187                             (int)(vec[2] * (size[2] - 1))].flag &= ~PARS_UNEXIST;
188                 }
189         }
190         else if (ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
191                 float co1[3], co2[3];
192
193                 MFace *mface= NULL, *mface_array;
194                 float v1[3], v2[3], v3[3], v4[4], lambda;
195                 int a, a1, a2, a0mul, a1mul, a2mul, totface;
196                 int amax= from==PART_FROM_FACE ? 3 : 1;
197
198                 totface=dm->getNumTessFaces(dm);
199                 mface=mface_array=dm->getTessFaceDataArray(dm,CD_MFACE);
200                 
201                 for (a=0; a<amax; a++) {
202                         if (a==0) { a0mul=res*res; a1mul=res; a2mul=1; }
203                         else if (a==1) { a0mul=res; a1mul=1; a2mul=res*res; }
204                         else { a0mul=1; a1mul=res*res; a2mul=res; }
205
206                         for (a1=0; a1<size[(a+1)%3]; a1++) {
207                                 for (a2=0; a2<size[(a+2)%3]; a2++) {
208                                         mface= mface_array;
209
210                                         pa = psys->particles + a1*a1mul + a2*a2mul;
211                                         copy_v3_v3(co1, pa->fuv);
212                                         co1[a] -= d < delta[a] ? d/2.f : delta[a]/2.f;
213                                         copy_v3_v3(co2, co1);
214                                         co2[a] += delta[a] + 0.001f*d;
215                                         co1[a] -= 0.001f*d;
216                                         
217                                         /* lets intersect the faces */
218                                         for (i=0; i<totface; i++,mface++) {
219                                                 copy_v3_v3(v1, mvert[mface->v1].co);
220                                                 copy_v3_v3(v2, mvert[mface->v2].co);
221                                                 copy_v3_v3(v3, mvert[mface->v3].co);
222
223                                                 bool intersects_tri = isect_axial_line_segment_tri_v3(a, co1, co2, v2, v3, v1, &lambda);
224                                                 if (intersects_tri) {
225                                                         if (from==PART_FROM_FACE)
226                                                                 (pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST;
227                                                         else /* store number of intersections */
228                                                                 (pa+(int)(lambda*size[a])*a0mul)->hair_index++;
229                                                 }
230
231                                                 if (mface->v4 && (!intersects_tri || from==PART_FROM_VOLUME)) {
232                                                         copy_v3_v3(v4, mvert[mface->v4].co);
233
234                                                         if (isect_axial_line_segment_tri_v3(a, co1, co2, v4, v1, v3, &lambda)) {
235                                                                 if (from==PART_FROM_FACE)
236                                                                         (pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST;
237                                                                 else
238                                                                         (pa+(int)(lambda*size[a])*a0mul)->hair_index++;
239                                                         }
240                                                 }
241                                         }
242
243                                         if (from==PART_FROM_VOLUME) {
244                                                 int in=pa->hair_index%2;
245                                                 if (in) pa->hair_index++;
246                                                 for (i=0; i<size[0]; i++) {
247                                                         if (in || (pa+i*a0mul)->hair_index%2)
248                                                                 (pa+i*a0mul)->flag &= ~PARS_UNEXIST;
249                                                         /* odd intersections == in->out / out->in */
250                                                         /* even intersections -> in stays same */
251                                                         in=(in + (pa+i*a0mul)->hair_index) % 2;
252                                                 }
253                                         }
254                                 }
255                         }
256                 }
257         }
258
259         if (psys->part->flag & PART_GRID_HEXAGONAL) {
260                 for (i=0,p=0,pa=psys->particles; i<res; i++) {
261                         for (j=0; j<res; j++) {
262                                 for (k=0; k<res; k++,p++,pa++) {
263                                         if (j%2)
264                                                 pa->fuv[0] += d/2.f;
265
266                                         if (k%2) {
267                                                 pa->fuv[0] += d/2.f;
268                                                 pa->fuv[1] += d/2.f;
269                                         }
270                                 }
271                         }
272                 }
273         }
274
275         if (psys->part->flag & PART_GRID_INVERT) {
276                 for (i=0; i<size[0]; i++) {
277                         for (j=0; j<size[1]; j++) {
278                                 pa=psys->particles + res*(i*res + j);
279                                 for (k=0; k<size[2]; k++, pa++) {
280                                         pa->flag ^= PARS_UNEXIST;
281                                 }
282                         }
283                 }
284         }
285
286         if (psys->part->grid_rand > 0.f) {
287                 float rfac = d * psys->part->grid_rand;
288                 for (p=0,pa=psys->particles; p<psys->totpart; p++,pa++) {
289                         if (pa->flag & PARS_UNEXIST)
290                                 continue;
291
292                         pa->fuv[0] += rfac * (psys_frand(psys, p + 31) - 0.5f);
293                         pa->fuv[1] += rfac * (psys_frand(psys, p + 32) - 0.5f);
294                         pa->fuv[2] += rfac * (psys_frand(psys, p + 33) - 0.5f);
295                 }
296         }
297 }
298
299 /* modified copy from rayshade.c */
300 static void hammersley_create(float *out, int n, int seed, float amount)
301 {
302         RNG *rng;
303         double p, t, offs[2];
304         int k, kk;
305
306         rng = BLI_rng_new(31415926 + n + seed);
307         offs[0] = BLI_rng_get_double(rng) + (double)amount;
308         offs[1] = BLI_rng_get_double(rng) + (double)amount;
309         BLI_rng_free(rng);
310
311         for (k = 0; k < n; k++) {
312                 t = 0;
313                 for (p = 0.5, kk = k; kk; p *= 0.5, kk >>= 1)
314                         if (kk & 1) /* kk mod 2 = 1 */
315                                 t += p;
316
317                 out[2*k + 0] = fmod((double)k/(double)n + offs[0], 1.0);
318                 out[2*k + 1] = fmod(t + offs[1], 1.0);
319         }
320 }
321
322 /* almost exact copy of BLI_jitter_init */
323 static void init_mv_jit(float *jit, int num, int seed2, float amount)
324 {
325         RNG *rng;
326         float *jit2, x, rad1, rad2, rad3;
327         int i, num2;
328
329         if (num==0) return;
330
331         rad1= (float)(1.0f/sqrtf((float)num));
332         rad2= (float)(1.0f/((float)num));
333         rad3= (float)sqrtf((float)num)/((float)num);
334
335         rng = BLI_rng_new(31415926 + num + seed2);
336         x= 0;
337         num2 = 2 * num;
338         for (i=0; i<num2; i+=2) {
339         
340                 jit[i] = x + amount*rad1*(0.5f - BLI_rng_get_float(rng));
341                 jit[i+1] = i/(2.0f*num) + amount*rad1*(0.5f - BLI_rng_get_float(rng));
342                 
343                 jit[i]-= (float)floor(jit[i]);
344                 jit[i+1]-= (float)floor(jit[i+1]);
345                 
346                 x+= rad3;
347                 x -= (float)floor(x);
348         }
349
350         jit2= MEM_mallocN(12 + 2*sizeof(float)*num, "initjit");
351
352         for (i=0 ; i<4 ; i++) {
353                 BLI_jitterate1((float (*)[2])jit, (float (*)[2])jit2, num, rad1);
354                 BLI_jitterate1((float (*)[2])jit, (float (*)[2])jit2, num, rad1);
355                 BLI_jitterate2((float (*)[2])jit, (float (*)[2])jit2, num, rad2);
356         }
357         MEM_freeN(jit2);
358         BLI_rng_free(rng);
359 }
360
361 static void psys_uv_to_w(float u, float v, int quad, float *w)
362 {
363         float vert[4][3], co[3];
364
365         if (!quad) {
366                 if (u+v > 1.0f)
367                         v= 1.0f-v;
368                 else
369                         u= 1.0f-u;
370         }
371
372         vert[0][0] = 0.0f; vert[0][1] = 0.0f; vert[0][2] = 0.0f;
373         vert[1][0] = 1.0f; vert[1][1] = 0.0f; vert[1][2] = 0.0f;
374         vert[2][0] = 1.0f; vert[2][1] = 1.0f; vert[2][2] = 0.0f;
375
376         co[0] = u;
377         co[1] = v;
378         co[2] = 0.0f;
379
380         if (quad) {
381                 vert[3][0] = 0.0f; vert[3][1] = 1.0f; vert[3][2] = 0.0f;
382                 interp_weights_poly_v3( w,vert, 4, co);
383         }
384         else {
385                 interp_weights_poly_v3( w,vert, 3, co);
386                 w[3] = 0.0f;
387         }
388 }
389
390 /* Find the index in "sum" array before "value" is crossed. */
391 static int distribute_binary_search(float *sum, int n, float value)
392 {
393         int mid, low = 0, high = n - 1;
394
395         if (high == low)
396                 return low;
397
398         if (sum[low] >= value)
399                 return low;
400
401         if (sum[high - 1] < value)
402                 return high;
403
404         while (low < high) {
405                 mid = (low + high) / 2;
406                 
407                 if ((sum[mid] >= value) && (sum[mid - 1] < value))
408                         return mid;
409                 
410                 if (sum[mid] > value) {
411                         high = mid - 1;
412                 }
413                 else {
414                         low = mid + 1;
415                 }
416         }
417
418         return low;
419 }
420
421 /* the max number if calls to rng_* funcs within psys_thread_distribute_particle
422  * be sure to keep up to date if this changes */
423 #define PSYS_RND_DIST_SKIP 2
424
425 /* note: this function must be thread safe, for from == PART_FROM_CHILD */
426 #define ONLY_WORKING_WITH_PA_VERTS 0
427 static void distribute_from_verts_exec(ParticleTask *thread, ParticleData *pa, int p)
428 {
429         ParticleThreadContext *ctx= thread->ctx;
430         MFace *mface;
431
432         DM_ensure_tessface(ctx->dm);
433         mface = ctx->dm->getTessFaceDataArray(ctx->dm, CD_MFACE);
434
435         int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
436
437         /* TODO_PARTICLE - use original index */
438         pa->num = ctx->index[p];
439
440         zero_v4(pa->fuv);
441
442         if (pa->num != DMCACHE_NOTFOUND && pa->num < ctx->dm->getNumVerts(ctx->dm)) {
443
444                 /* This finds the first face to contain the emitting vertex,
445                  * this is not ideal, but is mostly fine as UV seams generally
446                  * map to equal-colored parts of a texture */
447                 for (int i = 0; i < ctx->dm->getNumTessFaces(ctx->dm); i++, mface++) {
448                         if (ELEM(pa->num, mface->v1, mface->v2, mface->v3, mface->v4)) {
449                                 unsigned int *vert = &mface->v1;
450
451                                 for (int j = 0; j < 4; j++, vert++) {
452                                         if (*vert == pa->num) {
453                                                 pa->fuv[j] = 1.0f;
454                                                 break;
455                                         }
456                                 }
457
458                                 break;
459                         }
460                 }
461         }
462         
463 #if ONLY_WORKING_WITH_PA_VERTS
464         if (ctx->tree) {
465                 KDTreeNearest ptn[3];
466                 int w, maxw;
467                 
468                 psys_particle_on_dm(ctx->dm,from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co1,0,0,0,orco1,0);
469                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco1, 1, 1);
470                 maxw = BLI_kdtree_find_nearest_n(ctx->tree,orco1,ptn,3);
471                 
472                 for (w=0; w<maxw; w++) {
473                         pa->verts[w]=ptn->num;
474                 }
475         }
476 #endif
477         
478         if (rng_skip_tot > 0) /* should never be below zero */
479                 BLI_rng_skip(thread->rng, rng_skip_tot);
480 }
481
482 static void distribute_from_faces_exec(ParticleTask *thread, ParticleData *pa, int p) {
483         ParticleThreadContext *ctx= thread->ctx;
484         DerivedMesh *dm= ctx->dm;
485         float randu, randv;
486         int distr= ctx->distr;
487         int i;
488         int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
489
490         MFace *mface;
491         
492         pa->num = i = ctx->index[p];
493         mface = dm->getTessFaceData(dm,i,CD_MFACE);
494         
495         switch (distr) {
496                 case PART_DISTR_JIT:
497                         if (ctx->jitlevel == 1) {
498                                 if (mface->v4)
499                                         psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
500                                 else
501                                         psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
502                         }
503                         else {
504                                 float offset = fmod(ctx->jitoff[i] + (float)p, (float)ctx->jitlevel);
505                                 if (!isnan(offset)) {
506                                         psys_uv_to_w(ctx->jit[2*(int)offset], ctx->jit[2*(int)offset+1], mface->v4, pa->fuv);
507                                 }
508                         }
509                         break;
510                 case PART_DISTR_RAND:
511                         randu= BLI_rng_get_float(thread->rng);
512                         randv= BLI_rng_get_float(thread->rng);
513                         rng_skip_tot -= 2;
514                         
515                         psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
516                         break;
517         }
518         pa->foffset= 0.0f;
519         
520         if (rng_skip_tot > 0) /* should never be below zero */
521                 BLI_rng_skip(thread->rng, rng_skip_tot);
522 }
523
524 static void distribute_from_volume_exec(ParticleTask *thread, ParticleData *pa, int p) {
525         ParticleThreadContext *ctx= thread->ctx;
526         DerivedMesh *dm= ctx->dm;
527         float *v1, *v2, *v3, *v4, nor[3], co[3];
528         float cur_d, min_d, randu, randv;
529         int distr= ctx->distr;
530         int i, intersect, tot;
531         int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
532         
533         MFace *mface;
534         MVert *mvert=dm->getVertDataArray(dm,CD_MVERT);
535         
536         pa->num = i = ctx->index[p];
537         mface = dm->getTessFaceData(dm,i,CD_MFACE);
538         
539         switch (distr) {
540                 case PART_DISTR_JIT:
541                         if (ctx->jitlevel == 1) {
542                                 if (mface->v4)
543                                         psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
544                                 else
545                                         psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
546                         }
547                         else {
548                                 float offset = fmod(ctx->jitoff[i] + (float)p, (float)ctx->jitlevel);
549                                 if (!isnan(offset)) {
550                                         psys_uv_to_w(ctx->jit[2*(int)offset], ctx->jit[2*(int)offset+1], mface->v4, pa->fuv);
551                                 }
552                         }
553                         break;
554                 case PART_DISTR_RAND:
555                         randu= BLI_rng_get_float(thread->rng);
556                         randv= BLI_rng_get_float(thread->rng);
557                         rng_skip_tot -= 2;
558                         
559                         psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
560                         break;
561         }
562         pa->foffset= 0.0f;
563         
564         /* experimental */
565         tot=dm->getNumTessFaces(dm);
566         
567         psys_interpolate_face(mvert,mface,0,0,pa->fuv,co,nor,0,0,0,0);
568         
569         normalize_v3(nor);
570         negate_v3(nor);
571         
572         min_d=FLT_MAX;
573         intersect=0;
574         
575         for (i=0,mface=dm->getTessFaceDataArray(dm,CD_MFACE); i<tot; i++,mface++) {
576                 if (i==pa->num) continue;
577                 
578                 v1=mvert[mface->v1].co;
579                 v2=mvert[mface->v2].co;
580                 v3=mvert[mface->v3].co;
581                 
582                 if (isect_ray_tri_v3(co, nor, v2, v3, v1, &cur_d, NULL)) {
583                         if (cur_d<min_d) {
584                                 min_d=cur_d;
585                                 pa->foffset=cur_d*0.5f; /* to the middle of volume */
586                                 intersect=1;
587                         }
588                 }
589                 if (mface->v4) {
590                         v4=mvert[mface->v4].co;
591                         
592                         if (isect_ray_tri_v3(co, nor, v4, v1, v3, &cur_d, NULL)) {
593                                 if (cur_d<min_d) {
594                                         min_d=cur_d;
595                                         pa->foffset=cur_d*0.5f; /* to the middle of volume */
596                                         intersect=1;
597                                 }
598                         }
599                 }
600         }
601         if (intersect==0)
602                 pa->foffset=0.0;
603         else {
604                 switch (distr) {
605                         case PART_DISTR_JIT:
606                                 pa->foffset *= ctx->jit[p % (2 * ctx->jitlevel)];
607                                 break;
608                         case PART_DISTR_RAND:
609                                 pa->foffset *= BLI_frand();
610                                 break;
611                 }
612         }
613         
614         if (rng_skip_tot > 0) /* should never be below zero */
615                 BLI_rng_skip(thread->rng, rng_skip_tot);
616 }
617
618 static void distribute_children_exec(ParticleTask *thread, ChildParticle *cpa, int p) {
619         ParticleThreadContext *ctx= thread->ctx;
620         Object *ob= ctx->sim.ob;
621         DerivedMesh *dm= ctx->dm;
622         float orco1[3], co1[3], nor1[3];
623         float randu, randv;
624         int cfrom= ctx->cfrom;
625         int i;
626         int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
627         
628         MFace *mf;
629         
630         if (ctx->index[p] < 0) {
631                 cpa->num=0;
632                 cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3]=0.0f;
633                 cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0;
634                 return;
635         }
636         
637         mf= dm->getTessFaceData(dm, ctx->index[p], CD_MFACE);
638         
639         randu= BLI_rng_get_float(thread->rng);
640         randv= BLI_rng_get_float(thread->rng);
641         rng_skip_tot -= 2;
642         
643         psys_uv_to_w(randu, randv, mf->v4, cpa->fuv);
644         
645         cpa->num = ctx->index[p];
646         
647         if (ctx->tree) {
648                 KDTreeNearest ptn[10];
649                 int w,maxw;//, do_seams;
650                 float maxd /*, mind,dd */, totw= 0.0f;
651                 int parent[10];
652                 float pweight[10];
653                 
654                 psys_particle_on_dm(dm,cfrom,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co1,nor1,NULL,NULL,orco1,NULL);
655                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco1, 1, 1);
656                 maxw = BLI_kdtree_find_nearest_n(ctx->tree,orco1,ptn,3);
657                 
658                 maxd=ptn[maxw-1].dist;
659                 /* mind=ptn[0].dist; */ /* UNUSED */
660                 
661                 /* the weights here could be done better */
662                 for (w=0; w<maxw; w++) {
663                         parent[w]=ptn[w].index;
664                         pweight[w]=(float)pow(2.0,(double)(-6.0f*ptn[w].dist/maxd));
665                 }
666                 for (;w<10; w++) {
667                         parent[w]=-1;
668                         pweight[w]=0.0f;
669                 }
670                 
671                 for (w=0,i=0; w<maxw && i<4; w++) {
672                         if (parent[w]>=0) {
673                                 cpa->pa[i]=parent[w];
674                                 cpa->w[i]=pweight[w];
675                                 totw+=pweight[w];
676                                 i++;
677                         }
678                 }
679                 for (;i<4; i++) {
680                         cpa->pa[i]=-1;
681                         cpa->w[i]=0.0f;
682                 }
683                 
684                 if (totw > 0.0f) {
685                         for (w = 0; w < 4; w++) {
686                                 cpa->w[w] /= totw;
687                         }
688                 }
689                 
690                 cpa->parent=cpa->pa[0];
691         }
692
693         if (rng_skip_tot > 0) /* should never be below zero */
694                 BLI_rng_skip(thread->rng, rng_skip_tot);
695 }
696
697 static void exec_distribute_parent(TaskPool * __restrict UNUSED(pool), void *taskdata, int UNUSED(threadid))
698 {
699         ParticleTask *task = taskdata;
700         ParticleSystem *psys= task->ctx->sim.psys;
701         ParticleData *pa;
702         int p;
703
704         BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP * task->begin);
705         
706         pa= psys->particles + task->begin;
707         switch (psys->part->from) {
708                 case PART_FROM_FACE:
709                         for (p = task->begin; p < task->end; ++p, ++pa)
710                                 distribute_from_faces_exec(task, pa, p);
711                         break;
712                 case PART_FROM_VOLUME:
713                         for (p = task->begin; p < task->end; ++p, ++pa)
714                                 distribute_from_volume_exec(task, pa, p);
715                         break;
716                 case PART_FROM_VERT:
717                         for (p = task->begin; p < task->end; ++p, ++pa)
718                                 distribute_from_verts_exec(task, pa, p);
719                         break;
720         }
721 }
722
723 static void exec_distribute_child(TaskPool * __restrict UNUSED(pool), void *taskdata, int UNUSED(threadid))
724 {
725         ParticleTask *task = taskdata;
726         ParticleSystem *psys = task->ctx->sim.psys;
727         ChildParticle *cpa;
728         int p;
729         
730         /* RNG skipping at the beginning */
731         cpa = psys->child;
732         for (p = 0; p < task->begin; ++p, ++cpa) {
733                 if (task->ctx->skip) /* simplification skip */
734                         BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP * task->ctx->skip[p]);
735                 
736                 BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP);
737         }
738                 
739         for (; p < task->end; ++p, ++cpa) {
740                 if (task->ctx->skip) /* simplification skip */
741                         BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP * task->ctx->skip[p]);
742                 
743                 distribute_children_exec(task, cpa, p);
744         }
745 }
746
747 static int distribute_compare_orig_index(const void *p1, const void *p2, void *user_data)
748 {
749         int *orig_index = (int *) user_data;
750         int index1 = orig_index[*(const int *)p1];
751         int index2 = orig_index[*(const int *)p2];
752
753         if (index1 < index2)
754                 return -1;
755         else if (index1 == index2) {
756                 /* this pointer comparison appears to make qsort stable for glibc,
757                  * and apparently on solaris too, makes the renders reproducible */
758                 if (p1 < p2)
759                         return -1;
760                 else if (p1 == p2)
761                         return 0;
762                 else
763                         return 1;
764         }
765         else
766                 return 1;
767 }
768
769 static void distribute_invalid(Scene *scene, ParticleSystem *psys, int from)
770 {
771         if (from == PART_FROM_CHILD) {
772                 ChildParticle *cpa;
773                 int p, totchild = psys_get_tot_child(scene, psys);
774
775                 if (psys->child && totchild) {
776                         for (p=0,cpa=psys->child; p<totchild; p++,cpa++) {
777                                 cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3] = 0.0;
778                                 cpa->foffset= 0.0f;
779                                 cpa->parent=0;
780                                 cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0;
781                                 cpa->num= -1;
782                         }
783                 }
784         }
785         else {
786                 PARTICLE_P;
787                 LOOP_PARTICLES {
788                         pa->fuv[0] = pa->fuv[1] = pa->fuv[2] = pa->fuv[3] = 0.0;
789                         pa->foffset= 0.0f;
790                         pa->num= -1;
791                 }
792         }
793 }
794
795 /* Creates a distribution of coordinates on a DerivedMesh       */
796 /* This is to denote functionality that does not yet work with mesh - only derived mesh */
797 static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, ParticleSimulationData *sim, int from)
798 {
799         Scene *scene = sim->scene;
800         DerivedMesh *finaldm = sim->psmd->dm_final;
801         Object *ob = sim->ob;
802         ParticleSystem *psys= sim->psys;
803         ParticleData *pa=0, *tpars= 0;
804         ParticleSettings *part;
805         ParticleSeam *seams= 0;
806         KDTree *tree=0;
807         DerivedMesh *dm= NULL;
808         float *jit= NULL;
809         int i, p=0;
810         int cfrom=0;
811         int totelem=0, totpart, *particle_element=0, children=0, totseam=0;
812         int jitlevel= 1, distr;
813         float *element_weight=NULL,*jitter_offset=NULL, *vweight=NULL;
814         float cur, maxweight=0.0, tweight, totweight, inv_totweight, co[3], nor[3], orco[3];
815         
816         if (ELEM(NULL, ob, psys, psys->part))
817                 return 0;
818         
819         part=psys->part;
820         totpart=psys->totpart;
821         if (totpart==0)
822                 return 0;
823         
824         if (!finaldm->deformedOnly && !finaldm->getTessFaceDataArray(finaldm, CD_ORIGINDEX)) {
825                 printf("Can't create particles with the current modifier stack, disable destructive modifiers\n");
826 // XXX          error("Can't paint with the current modifier stack, disable destructive modifiers");
827                 return 0;
828         }
829         
830         /* XXX This distribution code is totally broken in case from == PART_FROM_CHILD, it's always using finaldm
831          *     even if use_modifier_stack is unset... But making things consistent here break all existing edited
832          *     hair systems, so better wait for complete rewrite.
833          */
834
835         psys_thread_context_init(ctx, sim);
836         
837         /* First handle special cases */
838         if (from == PART_FROM_CHILD) {
839                 /* Simple children */
840                 if (part->childtype != PART_CHILD_FACES) {
841                         BLI_srandom(31415926 + psys->seed + psys->child_seed);
842                         distribute_simple_children(scene, ob, finaldm, sim->psmd->dm_deformed, psys);
843                         return 0;
844                 }
845         }
846         else {
847                 /* Grid distribution */
848                 if (part->distr==PART_DISTR_GRID && from != PART_FROM_VERT) {
849                         BLI_srandom(31415926 + psys->seed);
850
851                         if (psys->part->use_modifier_stack) {
852                                 dm = finaldm;
853                         }
854                         else {
855                                 dm = CDDM_from_mesh((Mesh*)ob->data);
856                         }
857                         DM_ensure_tessface(dm);
858
859                         distribute_grid(dm,psys);
860
861                         if (dm != finaldm) {
862                                 dm->release(dm);
863                         }
864
865                         return 0;
866                 }
867         }
868         
869         /* Create trees and original coordinates if needed */
870         if (from == PART_FROM_CHILD) {
871                 distr=PART_DISTR_RAND;
872                 BLI_srandom(31415926 + psys->seed + psys->child_seed);
873                 dm= finaldm;
874
875                 /* BMESH ONLY */
876                 DM_ensure_tessface(dm);
877
878                 children=1;
879
880                 tree=BLI_kdtree_new(totpart);
881
882                 for (p=0,pa=psys->particles; p<totpart; p++,pa++) {
883                         psys_particle_on_dm(dm,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,nor,0,0,orco,NULL);
884                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco, 1, 1);
885                         BLI_kdtree_insert(tree, p, orco);
886                 }
887
888                 BLI_kdtree_balance(tree);
889
890                 totpart = psys_get_tot_child(scene, psys);
891                 cfrom = from = PART_FROM_FACE;
892         }
893         else {
894                 distr = part->distr;
895                 BLI_srandom(31415926 + psys->seed);
896                 
897                 if (psys->part->use_modifier_stack)
898                         dm = finaldm;
899                 else
900                         dm= CDDM_from_mesh((Mesh*)ob->data);
901
902                 /* BMESH ONLY, for verts we don't care about tessfaces */
903                 if (from != PART_FROM_VERT) {
904                         DM_ensure_tessface(dm);
905                 }
906
907                 /* we need orco for consistent distributions */
908                 if (!CustomData_has_layer(&dm->vertData, CD_ORCO))
909                         DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, BKE_mesh_orco_verts_get(ob));
910
911                 if (from == PART_FROM_VERT) {
912                         MVert *mv= dm->getVertDataArray(dm, CD_MVERT);
913                         float (*orcodata)[3] = dm->getVertDataArray(dm, CD_ORCO);
914                         int totvert = dm->getNumVerts(dm);
915
916                         tree=BLI_kdtree_new(totvert);
917
918                         for (p=0; p<totvert; p++) {
919                                 if (orcodata) {
920                                         copy_v3_v3(co,orcodata[p]);
921                                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co, 1, 1);
922                                 }
923                                 else
924                                         copy_v3_v3(co,mv[p].co);
925                                 BLI_kdtree_insert(tree, p, co);
926                         }
927
928                         BLI_kdtree_balance(tree);
929                 }
930         }
931
932         /* Get total number of emission elements and allocate needed arrays */
933         totelem = (from == PART_FROM_VERT) ? dm->getNumVerts(dm) : dm->getNumTessFaces(dm);
934
935         if (totelem == 0) {
936                 distribute_invalid(scene, psys, children ? PART_FROM_CHILD : 0);
937
938                 if (G.debug & G_DEBUG)
939                         fprintf(stderr,"Particle distribution error: Nothing to emit from!\n");
940
941                 if (dm != finaldm) dm->release(dm);
942
943                 BLI_kdtree_free(tree);
944
945                 return 0;
946         }
947
948         element_weight  = MEM_callocN(sizeof(float)*totelem, "particle_distribution_weights");
949         particle_element= MEM_callocN(sizeof(int)*totpart, "particle_distribution_indexes");
950         jitter_offset   = MEM_callocN(sizeof(float)*totelem, "particle_distribution_jitoff");
951
952         /* Calculate weights from face areas */
953         if ((part->flag&PART_EDISTR || children) && from != PART_FROM_VERT) {
954                 MVert *v1, *v2, *v3, *v4;
955                 float totarea=0.f, co1[3], co2[3], co3[3], co4[3];
956                 float (*orcodata)[3];
957                 
958                 orcodata= dm->getVertDataArray(dm, CD_ORCO);
959
960                 for (i=0; i<totelem; i++) {
961                         MFace *mf=dm->getTessFaceData(dm,i,CD_MFACE);
962
963                         if (orcodata) {
964                                 copy_v3_v3(co1, orcodata[mf->v1]);
965                                 copy_v3_v3(co2, orcodata[mf->v2]);
966                                 copy_v3_v3(co3, orcodata[mf->v3]);
967                                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co1, 1, 1);
968                                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co2, 1, 1);
969                                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co3, 1, 1);
970                                 if (mf->v4) {
971                                         copy_v3_v3(co4, orcodata[mf->v4]);
972                                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co4, 1, 1);
973                                 }
974                         }
975                         else {
976                                 v1= (MVert*)dm->getVertData(dm,mf->v1,CD_MVERT);
977                                 v2= (MVert*)dm->getVertData(dm,mf->v2,CD_MVERT);
978                                 v3= (MVert*)dm->getVertData(dm,mf->v3,CD_MVERT);
979                                 copy_v3_v3(co1, v1->co);
980                                 copy_v3_v3(co2, v2->co);
981                                 copy_v3_v3(co3, v3->co);
982                                 if (mf->v4) {
983                                         v4= (MVert*)dm->getVertData(dm,mf->v4,CD_MVERT);
984                                         copy_v3_v3(co4, v4->co);
985                                 }
986                         }
987
988                         cur = mf->v4 ? area_quad_v3(co1, co2, co3, co4) : area_tri_v3(co1, co2, co3);
989                         
990                         if (cur > maxweight)
991                                 maxweight = cur;
992
993                         element_weight[i] = cur;
994                         totarea += cur;
995                 }
996
997                 for (i=0; i<totelem; i++)
998                         element_weight[i] /= totarea;
999
1000                 maxweight /= totarea;
1001         }
1002         else {
1003                 float min=1.0f/(float)(MIN2(totelem,totpart));
1004                 for (i=0; i<totelem; i++)
1005                         element_weight[i]=min;
1006                 maxweight=min;
1007         }
1008
1009         /* Calculate weights from vgroup */
1010         vweight = psys_cache_vgroup(dm,psys,PSYS_VG_DENSITY);
1011
1012         if (vweight) {
1013                 if (from==PART_FROM_VERT) {
1014                         for (i=0;i<totelem; i++)
1015                                 element_weight[i]*=vweight[i];
1016                 }
1017                 else { /* PART_FROM_FACE / PART_FROM_VOLUME */
1018                         for (i=0;i<totelem; i++) {
1019                                 MFace *mf=dm->getTessFaceData(dm,i,CD_MFACE);
1020                                 tweight = vweight[mf->v1] + vweight[mf->v2] + vweight[mf->v3];
1021                                 
1022                                 if (mf->v4) {
1023                                         tweight += vweight[mf->v4];
1024                                         tweight /= 4.0f;
1025                                 }
1026                                 else {
1027                                         tweight /= 3.0f;
1028                                 }
1029
1030                                 element_weight[i]*=tweight;
1031                         }
1032                 }
1033                 MEM_freeN(vweight);
1034         }
1035
1036         /* Calculate total weight of all elements */
1037         int totmapped = 0;
1038         totweight = 0.0f;
1039         for (i = 0; i < totelem; i++) {
1040                 if (element_weight[i] > 0.0f) {
1041                         totmapped++;
1042                         totweight += element_weight[i];
1043                 }
1044         }
1045
1046         if (totmapped == 0) {
1047                 /* We are not allowed to distribute particles anywhere... */
1048                 return 0;
1049         }
1050
1051         inv_totweight = 1.0f / totweight;
1052
1053         /* Calculate cumulative weights.
1054          * We remove all null-weighted elements from element_sum, and create a new mapping
1055          * 'activ'_elem_index -> orig_elem_index.
1056          * This simplifies greatly the filtering of zero-weighted items - and can be much more efficient
1057          * especially in random case (reducing a lot the size of binary-searched array)...
1058          */
1059         float *element_sum = MEM_mallocN(sizeof(*element_sum) * totmapped, __func__);
1060         int *element_map = MEM_mallocN(sizeof(*element_map) * totmapped, __func__);
1061         int i_mapped = 0;
1062
1063         for (i = 0; i < totelem && element_weight[i] == 0.0f; i++);
1064         element_sum[i_mapped] = element_weight[i] * inv_totweight;
1065         element_map[i_mapped] = i;
1066         i_mapped++;
1067         for (i++; i < totelem; i++) {
1068                 if (element_weight[i] > 0.0f) {
1069                         element_sum[i_mapped] = element_sum[i_mapped - 1] + element_weight[i] * inv_totweight;
1070                         /* Skip elements which weight is so small that it does not affect the sum. */
1071                         if (element_sum[i_mapped] > element_sum[i_mapped - 1]) {
1072                                 element_map[i_mapped] = i;
1073                                 i_mapped++;
1074                         }
1075                 }
1076         }
1077         totmapped = i_mapped;
1078
1079         /* Finally assign elements to particles */
1080         if ((part->flag & PART_TRAND) || (part->simplify_flag & PART_SIMPLIFY_ENABLE)) {
1081                 for (p = 0; p < totpart; p++) {
1082                         /* In theory element_sum[totmapped - 1] should be 1.0,
1083                          * but due to float errors this is not necessarily always true, so scale pos accordingly. */
1084                         const float pos = BLI_frand() * element_sum[totmapped - 1];
1085                         const int eidx = distribute_binary_search(element_sum, totmapped, pos);
1086                         particle_element[p] = element_map[eidx];
1087                         BLI_assert(pos <= element_sum[eidx]);
1088                         BLI_assert(eidx ? (pos > element_sum[eidx - 1]) : (pos >= 0.0f));
1089                         jitter_offset[particle_element[p]] = pos;
1090                 }
1091         }
1092         else {
1093                 double step, pos;
1094                 
1095                 step = (totpart < 2) ? 0.5 : 1.0 / (double)totpart;
1096                 /* This is to address tricky issues with vertex-emitting when user tries (and expects) exact 1-1 vert/part
1097                  * distribution (see T47983 and its two example files). It allows us to consider pos as
1098                  * 'midpoint between v and v+1' (or 'p and p+1', depending whether we have more vertices than particles or not),
1099                  * and avoid stumbling over float imprecisions in element_sum. */
1100                 if (from == PART_FROM_VERT) {
1101                         pos = (totpart < totmapped) ? 0.5 / (double)totmapped : step * 0.5;  /* We choose the smaller step. */
1102                 }
1103                 else {
1104                         pos = 0.0;
1105                 }
1106
1107                 for (i = 0, p = 0; p < totpart; p++, pos += step) {
1108                         for ( ; (i < totmapped - 1) && (pos > (double)element_sum[i]); i++);
1109
1110                         particle_element[p] = element_map[i];
1111
1112                         jitter_offset[particle_element[p]] = pos;
1113                 }
1114         }
1115
1116         MEM_freeN(element_sum);
1117         MEM_freeN(element_map);
1118
1119         /* For hair, sort by origindex (allows optimization's in rendering), */
1120         /* however with virtual parents the children need to be in random order. */
1121         if (part->type == PART_HAIR && !(part->childtype==PART_CHILD_FACES && part->parents != 0.0f)) {
1122                 int *orig_index = NULL;
1123
1124                 if (from == PART_FROM_VERT) {
1125                         if (dm->numVertData)
1126                                 orig_index = dm->getVertDataArray(dm, CD_ORIGINDEX);
1127                 }
1128                 else {
1129                         if (dm->numTessFaceData)
1130                                 orig_index = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
1131                 }
1132
1133                 if (orig_index) {
1134                         BLI_qsort_r(particle_element, totpart, sizeof(int), distribute_compare_orig_index, orig_index);
1135                 }
1136         }
1137
1138         /* Create jittering if needed */
1139         if (distr==PART_DISTR_JIT && ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
1140                 jitlevel= part->userjit;
1141                 
1142                 if (jitlevel == 0) {
1143                         jitlevel= totpart/totelem;
1144                         if (part->flag & PART_EDISTR) jitlevel*= 2;     /* looks better in general, not very scietific */
1145                         if (jitlevel<3) jitlevel= 3;
1146                 }
1147                 
1148                 jit= MEM_callocN((2+ jitlevel*2)*sizeof(float), "jit");
1149
1150                 /* for small amounts of particles we use regular jitter since it looks
1151                  * a bit better, for larger amounts we switch to hammersley sequence 
1152                  * because it is much faster */
1153                 if (jitlevel < 25)
1154                         init_mv_jit(jit, jitlevel, psys->seed, part->jitfac);
1155                 else
1156                         hammersley_create(jit, jitlevel+1, psys->seed, part->jitfac);
1157                 BLI_array_randomize(jit, 2*sizeof(float), jitlevel, psys->seed); /* for custom jit or even distribution */
1158         }
1159
1160         /* Setup things for threaded distribution */
1161         ctx->tree= tree;
1162         ctx->seams= seams;
1163         ctx->totseam= totseam;
1164         ctx->sim.psys= psys;
1165         ctx->index= particle_element;
1166         ctx->jit= jit;
1167         ctx->jitlevel= jitlevel;
1168         ctx->jitoff= jitter_offset;
1169         ctx->weight= element_weight;
1170         ctx->maxweight= maxweight;
1171         ctx->cfrom= cfrom;
1172         ctx->distr= distr;
1173         ctx->dm= dm;
1174         ctx->tpars= tpars;
1175
1176         if (children) {
1177                 totpart= psys_render_simplify_distribution(ctx, totpart);
1178                 alloc_child_particles(psys, totpart);
1179         }
1180
1181         return 1;
1182 }
1183
1184 static void psys_task_init_distribute(ParticleTask *task, ParticleSimulationData *sim)
1185 {
1186         /* init random number generator */
1187         int seed = 31415926 + sim->psys->seed;
1188         
1189         task->rng = BLI_rng_new(seed);
1190 }
1191
1192 static void distribute_particles_on_dm(ParticleSimulationData *sim, int from)
1193 {
1194         TaskScheduler *task_scheduler;
1195         TaskPool *task_pool;
1196         ParticleThreadContext ctx;
1197         ParticleTask *tasks;
1198         DerivedMesh *finaldm = sim->psmd->dm_final;
1199         int i, totpart, numtasks;
1200         
1201         /* create a task pool for distribution tasks */
1202         if (!psys_thread_context_init_distribute(&ctx, sim, from))
1203                 return;
1204         
1205         task_scheduler = BLI_task_scheduler_get();
1206         task_pool = BLI_task_pool_create(task_scheduler, &ctx);
1207         
1208         totpart = (from == PART_FROM_CHILD ? sim->psys->totchild : sim->psys->totpart);
1209         psys_tasks_create(&ctx, 0, totpart, &tasks, &numtasks);
1210         for (i = 0; i < numtasks; ++i) {
1211                 ParticleTask *task = &tasks[i];
1212                 
1213                 psys_task_init_distribute(task, sim);
1214                 if (from == PART_FROM_CHILD)
1215                         BLI_task_pool_push(task_pool, exec_distribute_child, task, false, TASK_PRIORITY_LOW);
1216                 else
1217                         BLI_task_pool_push(task_pool, exec_distribute_parent, task, false, TASK_PRIORITY_LOW);
1218         }
1219         BLI_task_pool_work_and_wait(task_pool);
1220         
1221         BLI_task_pool_free(task_pool);
1222         
1223         psys_calc_dmcache(sim->ob, finaldm, sim->psmd->dm_deformed, sim->psys);
1224         
1225         if (ctx.dm != finaldm)
1226                 ctx.dm->release(ctx.dm);
1227         
1228         psys_tasks_free(tasks, numtasks);
1229         
1230         psys_thread_context_free(&ctx);
1231 }
1232
1233 /* ready for future use, to emit particles without geometry */
1234 static void distribute_particles_on_shape(ParticleSimulationData *sim, int UNUSED(from))
1235 {
1236         distribute_invalid(sim->scene, sim->psys, 0);
1237
1238         fprintf(stderr,"Shape emission not yet possible!\n");
1239 }
1240
1241 void distribute_particles(ParticleSimulationData *sim, int from)
1242 {
1243         PARTICLE_PSMD;
1244         int distr_error=0;
1245
1246         if (psmd) {
1247                 if (psmd->dm_final)
1248                         distribute_particles_on_dm(sim, from);
1249                 else
1250                         distr_error=1;
1251         }
1252         else
1253                 distribute_particles_on_shape(sim, from);
1254
1255         if (distr_error) {
1256                 distribute_invalid(sim->scene, sim->psys, from);
1257
1258                 fprintf(stderr,"Particle distribution error!\n");
1259         }
1260 }
1261
1262 /* ======== Simplify ======== */
1263
1264 static float psys_render_viewport_falloff(double rate, float dist, float width)
1265 {
1266         return pow(rate, dist / width);
1267 }
1268
1269 static float psys_render_projected_area(ParticleSystem *psys, const float center[3], float area, double vprate, float *viewport)
1270 {
1271         ParticleRenderData *data = psys->renderdata;
1272         float co[4], view[3], ortho1[3], ortho2[3], w, dx, dy, radius;
1273         
1274         /* transform to view space */
1275         copy_v3_v3(co, center);
1276         co[3] = 1.0f;
1277         mul_m4_v4(data->viewmat, co);
1278         
1279         /* compute two vectors orthogonal to view vector */
1280         normalize_v3_v3(view, co);
1281         ortho_basis_v3v3_v3(ortho1, ortho2, view);
1282
1283         /* compute on screen minification */
1284         w = co[2] * data->winmat[2][3] + data->winmat[3][3];
1285         dx = data->winx * ortho2[0] * data->winmat[0][0];
1286         dy = data->winy * ortho2[1] * data->winmat[1][1];
1287         w = sqrtf(dx * dx + dy * dy) / w;
1288
1289         /* w squared because we are working with area */
1290         area = area * w * w;
1291
1292         /* viewport of the screen test */
1293
1294         /* project point on screen */
1295         mul_m4_v4(data->winmat, co);
1296         if (co[3] != 0.0f) {
1297                 co[0] = 0.5f * data->winx * (1.0f + co[0] / co[3]);
1298                 co[1] = 0.5f * data->winy * (1.0f + co[1] / co[3]);
1299         }
1300
1301         /* screen space radius */
1302         radius = sqrtf(area / (float)M_PI);
1303
1304         /* make smaller using fallof once over screen edge */
1305         *viewport = 1.0f;
1306
1307         if (co[0] + radius < 0.0f)
1308                 *viewport *= psys_render_viewport_falloff(vprate, -(co[0] + radius), data->winx);
1309         else if (co[0] - radius > data->winx)
1310                 *viewport *= psys_render_viewport_falloff(vprate, (co[0] - radius) - data->winx, data->winx);
1311
1312         if (co[1] + radius < 0.0f)
1313                 *viewport *= psys_render_viewport_falloff(vprate, -(co[1] + radius), data->winy);
1314         else if (co[1] - radius > data->winy)
1315                 *viewport *= psys_render_viewport_falloff(vprate, (co[1] - radius) - data->winy, data->winy);
1316         
1317         return area;
1318 }
1319
1320 /* BMESH_TODO, for orig face data, we need to use MPoly */
1321 static int psys_render_simplify_distribution(ParticleThreadContext *ctx, int tot)
1322 {
1323         DerivedMesh *dm = ctx->dm;
1324         Mesh *me = (Mesh *)(ctx->sim.ob->data);
1325         MFace *mf, *mface;
1326         MVert *mvert;
1327         ParticleRenderData *data;
1328         ParticleRenderElem *elems, *elem;
1329         ParticleSettings *part = ctx->sim.psys->part;
1330         float *facearea, (*facecenter)[3], size[3], fac, powrate, scaleclamp;
1331         float co1[3], co2[3], co3[3], co4[3], lambda, arearatio, t, area, viewport;
1332         double vprate;
1333         int *facetotvert;
1334         int a, b, totorigface, totface, newtot, skipped;
1335
1336         /* double lookup */
1337         const int *index_mf_to_mpoly;
1338         const int *index_mp_to_orig;
1339
1340         if (part->ren_as != PART_DRAW_PATH || !(part->draw & PART_DRAW_REN_STRAND))
1341                 return tot;
1342         if (!ctx->sim.psys->renderdata)
1343                 return tot;
1344
1345         data = ctx->sim.psys->renderdata;
1346         if (data->timeoffset)
1347                 return 0;
1348         if (!(part->simplify_flag & PART_SIMPLIFY_ENABLE))
1349                 return tot;
1350
1351         mvert = dm->getVertArray(dm);
1352         mface = dm->getTessFaceArray(dm);
1353         totface = dm->getNumTessFaces(dm);
1354         totorigface = me->totpoly;
1355
1356         if (totface == 0 || totorigface == 0)
1357                 return tot;
1358
1359         index_mf_to_mpoly = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
1360         index_mp_to_orig  = dm->getPolyDataArray(dm, CD_ORIGINDEX);
1361         if (index_mf_to_mpoly == NULL) {
1362                 index_mp_to_orig = NULL;
1363         }
1364
1365         facearea = MEM_callocN(sizeof(float) * totorigface, "SimplifyFaceArea");
1366         facecenter = MEM_callocN(sizeof(float[3]) * totorigface, "SimplifyFaceCenter");
1367         facetotvert = MEM_callocN(sizeof(int) * totorigface, "SimplifyFaceArea");
1368         elems = MEM_callocN(sizeof(ParticleRenderElem) * totorigface, "SimplifyFaceElem");
1369
1370         if (data->elems)
1371                 MEM_freeN(data->elems);
1372
1373         data->do_simplify = true;
1374         data->elems = elems;
1375         data->index_mf_to_mpoly = index_mf_to_mpoly;
1376         data->index_mp_to_orig  = index_mp_to_orig;
1377
1378         /* compute number of children per original face */
1379         for (a = 0; a < tot; a++) {
1380                 b = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, ctx->index[a]) : ctx->index[a];
1381                 if (b != ORIGINDEX_NONE) {
1382                         elems[b].totchild++;
1383                 }
1384         }
1385
1386         /* compute areas and centers of original faces */
1387         for (mf = mface, a = 0; a < totface; a++, mf++) {
1388                 b = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, a) : a;
1389
1390                 if (b != ORIGINDEX_NONE) {
1391                         copy_v3_v3(co1, mvert[mf->v1].co);
1392                         copy_v3_v3(co2, mvert[mf->v2].co);
1393                         copy_v3_v3(co3, mvert[mf->v3].co);
1394
1395                         add_v3_v3(facecenter[b], co1);
1396                         add_v3_v3(facecenter[b], co2);
1397                         add_v3_v3(facecenter[b], co3);
1398
1399                         if (mf->v4) {
1400                                 copy_v3_v3(co4, mvert[mf->v4].co);
1401                                 add_v3_v3(facecenter[b], co4);
1402                                 facearea[b] += area_quad_v3(co1, co2, co3, co4);
1403                                 facetotvert[b] += 4;
1404                         }
1405                         else {
1406                                 facearea[b] += area_tri_v3(co1, co2, co3);
1407                                 facetotvert[b] += 3;
1408                         }
1409                 }
1410         }
1411
1412         for (a = 0; a < totorigface; a++)
1413                 if (facetotvert[a] > 0)
1414                         mul_v3_fl(facecenter[a], 1.0f / facetotvert[a]);
1415
1416         /* for conversion from BU area / pixel area to reference screen size */
1417         BKE_mesh_texspace_get(me, 0, 0, size);
1418         fac = ((size[0] + size[1] + size[2]) / 3.0f) / part->simplify_refsize;
1419         fac = fac * fac;
1420
1421         powrate = log(0.5f) / log(part->simplify_rate * 0.5f);
1422         if (part->simplify_flag & PART_SIMPLIFY_VIEWPORT)
1423                 vprate = pow(1.0f - part->simplify_viewport, 5.0);
1424         else
1425                 vprate = 1.0;
1426
1427         /* set simplification parameters per original face */
1428         for (a = 0, elem = elems; a < totorigface; a++, elem++) {
1429                 area = psys_render_projected_area(ctx->sim.psys, facecenter[a], facearea[a], vprate, &viewport);
1430                 arearatio = fac * area / facearea[a];
1431
1432                 if ((arearatio < 1.0f || viewport < 1.0f) && elem->totchild) {
1433                         /* lambda is percentage of elements to keep */
1434                         lambda = (arearatio < 1.0f) ? powf(arearatio, powrate) : 1.0f;
1435                         lambda *= viewport;
1436
1437                         lambda = MAX2(lambda, 1.0f / elem->totchild);
1438
1439                         /* compute transition region */
1440                         t = part->simplify_transition;
1441                         elem->t = (lambda - t < 0.0f) ? lambda : (lambda + t > 1.0f) ? 1.0f - lambda : t;
1442                         elem->reduce = 1;
1443
1444                         /* scale at end and beginning of the transition region */
1445                         elem->scalemax = (lambda + t < 1.0f) ? 1.0f / lambda : 1.0f / (1.0f - elem->t * elem->t / t);
1446                         elem->scalemin = (lambda + t < 1.0f) ? 0.0f : elem->scalemax * (1.0f - elem->t / t);
1447
1448                         elem->scalemin = sqrtf(elem->scalemin);
1449                         elem->scalemax = sqrtf(elem->scalemax);
1450
1451                         /* clamp scaling */
1452                         scaleclamp = (float)min_ii(elem->totchild, 10);
1453                         elem->scalemin = MIN2(scaleclamp, elem->scalemin);
1454                         elem->scalemax = MIN2(scaleclamp, elem->scalemax);
1455
1456                         /* extend lambda to include transition */
1457                         lambda = lambda + elem->t;
1458                         if (lambda > 1.0f)
1459                                 lambda = 1.0f;
1460                 }
1461                 else {
1462                         lambda = arearatio;
1463
1464                         elem->scalemax = 1.0f; //sqrt(lambda);
1465                         elem->scalemin = 1.0f; //sqrt(lambda);
1466                         elem->reduce = 0;
1467                 }
1468
1469                 elem->lambda = lambda;
1470                 elem->scalemin = sqrtf(elem->scalemin);
1471                 elem->scalemax = sqrtf(elem->scalemax);
1472                 elem->curchild = 0;
1473         }
1474
1475         MEM_freeN(facearea);
1476         MEM_freeN(facecenter);
1477         MEM_freeN(facetotvert);
1478
1479         /* move indices and set random number skipping */
1480         ctx->skip = MEM_callocN(sizeof(int) * tot, "SimplificationSkip");
1481
1482         skipped = 0;
1483         for (a = 0, newtot = 0; a < tot; a++) {
1484                 b = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, ctx->index[a]) : ctx->index[a];
1485
1486                 if (b != ORIGINDEX_NONE) {
1487                         if (elems[b].curchild++ < ceil(elems[b].lambda * elems[b].totchild)) {
1488                                 ctx->index[newtot] = ctx->index[a];
1489                                 ctx->skip[newtot] = skipped;
1490                                 skipped = 0;
1491                                 newtot++;
1492                         }
1493                         else skipped++;
1494                 }
1495                 else skipped++;
1496         }
1497
1498         for (a = 0, elem = elems; a < totorigface; a++, elem++)
1499                 elem->curchild = 0;
1500
1501         return newtot;
1502 }