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