Fix T51296: UVs not working for hair emitted from vertices
[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                 for (int i = 0; i < ctx->dm->getNumTessFaces(ctx->dm); i++, mface++) {
444                         if (ELEM(pa->num, mface->v1, mface->v2, mface->v3, mface->v4)) {
445                                 unsigned int *vert = &mface->v1;
446
447                                 for (int j = 0; j < 4; j++, vert++) {
448                                         if (*vert == pa->num) {
449                                                 pa->fuv[j] = 1.0f;
450                                                 break;
451                                         }
452                                 }
453
454                                 break;
455                         }
456                 }
457         }
458         
459 #if ONLY_WORKING_WITH_PA_VERTS
460         if (ctx->tree) {
461                 KDTreeNearest ptn[3];
462                 int w, maxw;
463                 
464                 psys_particle_on_dm(ctx->dm,from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co1,0,0,0,orco1,0);
465                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco1, 1, 1);
466                 maxw = BLI_kdtree_find_nearest_n(ctx->tree,orco1,ptn,3);
467                 
468                 for (w=0; w<maxw; w++) {
469                         pa->verts[w]=ptn->num;
470                 }
471         }
472 #endif
473         
474         if (rng_skip_tot > 0) /* should never be below zero */
475                 BLI_rng_skip(thread->rng, rng_skip_tot);
476 }
477
478 static void distribute_from_faces_exec(ParticleTask *thread, ParticleData *pa, int p) {
479         ParticleThreadContext *ctx= thread->ctx;
480         DerivedMesh *dm= ctx->dm;
481         float randu, randv;
482         int distr= ctx->distr;
483         int i;
484         int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
485
486         MFace *mface;
487         
488         pa->num = i = ctx->index[p];
489         mface = dm->getTessFaceData(dm,i,CD_MFACE);
490         
491         switch (distr) {
492                 case PART_DISTR_JIT:
493                         if (ctx->jitlevel == 1) {
494                                 if (mface->v4)
495                                         psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
496                                 else
497                                         psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
498                         }
499                         else {
500                                 float offset = fmod(ctx->jitoff[i] + (float)p, (float)ctx->jitlevel);
501                                 if (!isnan(offset)) {
502                                         psys_uv_to_w(ctx->jit[2*(int)offset], ctx->jit[2*(int)offset+1], mface->v4, pa->fuv);
503                                 }
504                         }
505                         break;
506                 case PART_DISTR_RAND:
507                         randu= BLI_rng_get_float(thread->rng);
508                         randv= BLI_rng_get_float(thread->rng);
509                         rng_skip_tot -= 2;
510                         
511                         psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
512                         break;
513         }
514         pa->foffset= 0.0f;
515         
516         if (rng_skip_tot > 0) /* should never be below zero */
517                 BLI_rng_skip(thread->rng, rng_skip_tot);
518 }
519
520 static void distribute_from_volume_exec(ParticleTask *thread, ParticleData *pa, int p) {
521         ParticleThreadContext *ctx= thread->ctx;
522         DerivedMesh *dm= ctx->dm;
523         float *v1, *v2, *v3, *v4, nor[3], co[3];
524         float cur_d, min_d, randu, randv;
525         int distr= ctx->distr;
526         int i, intersect, tot;
527         int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
528         
529         MFace *mface;
530         MVert *mvert=dm->getVertDataArray(dm,CD_MVERT);
531         
532         pa->num = i = ctx->index[p];
533         mface = dm->getTessFaceData(dm,i,CD_MFACE);
534         
535         switch (distr) {
536                 case PART_DISTR_JIT:
537                         if (ctx->jitlevel == 1) {
538                                 if (mface->v4)
539                                         psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
540                                 else
541                                         psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
542                         }
543                         else {
544                                 float offset = fmod(ctx->jitoff[i] + (float)p, (float)ctx->jitlevel);
545                                 if (!isnan(offset)) {
546                                         psys_uv_to_w(ctx->jit[2*(int)offset], ctx->jit[2*(int)offset+1], mface->v4, pa->fuv);
547                                 }
548                         }
549                         break;
550                 case PART_DISTR_RAND:
551                         randu= BLI_rng_get_float(thread->rng);
552                         randv= BLI_rng_get_float(thread->rng);
553                         rng_skip_tot -= 2;
554                         
555                         psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
556                         break;
557         }
558         pa->foffset= 0.0f;
559         
560         /* experimental */
561         tot=dm->getNumTessFaces(dm);
562         
563         psys_interpolate_face(mvert,mface,0,0,pa->fuv,co,nor,0,0,0,0);
564         
565         normalize_v3(nor);
566         negate_v3(nor);
567         
568         min_d=FLT_MAX;
569         intersect=0;
570         
571         for (i=0,mface=dm->getTessFaceDataArray(dm,CD_MFACE); i<tot; i++,mface++) {
572                 if (i==pa->num) continue;
573                 
574                 v1=mvert[mface->v1].co;
575                 v2=mvert[mface->v2].co;
576                 v3=mvert[mface->v3].co;
577                 
578                 if (isect_ray_tri_v3(co, nor, v2, v3, v1, &cur_d, NULL)) {
579                         if (cur_d<min_d) {
580                                 min_d=cur_d;
581                                 pa->foffset=cur_d*0.5f; /* to the middle of volume */
582                                 intersect=1;
583                         }
584                 }
585                 if (mface->v4) {
586                         v4=mvert[mface->v4].co;
587                         
588                         if (isect_ray_tri_v3(co, nor, v4, v1, v3, &cur_d, NULL)) {
589                                 if (cur_d<min_d) {
590                                         min_d=cur_d;
591                                         pa->foffset=cur_d*0.5f; /* to the middle of volume */
592                                         intersect=1;
593                                 }
594                         }
595                 }
596         }
597         if (intersect==0)
598                 pa->foffset=0.0;
599         else {
600                 switch (distr) {
601                         case PART_DISTR_JIT:
602                                 pa->foffset *= ctx->jit[p % (2 * ctx->jitlevel)];
603                                 break;
604                         case PART_DISTR_RAND:
605                                 pa->foffset *= BLI_frand();
606                                 break;
607                 }
608         }
609         
610         if (rng_skip_tot > 0) /* should never be below zero */
611                 BLI_rng_skip(thread->rng, rng_skip_tot);
612 }
613
614 static void distribute_children_exec(ParticleTask *thread, ChildParticle *cpa, int p) {
615         ParticleThreadContext *ctx= thread->ctx;
616         Object *ob= ctx->sim.ob;
617         DerivedMesh *dm= ctx->dm;
618         float orco1[3], co1[3], nor1[3];
619         float randu, randv;
620         int cfrom= ctx->cfrom;
621         int i;
622         int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
623         
624         MFace *mf;
625         
626         if (ctx->index[p] < 0) {
627                 cpa->num=0;
628                 cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3]=0.0f;
629                 cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0;
630                 return;
631         }
632         
633         mf= dm->getTessFaceData(dm, ctx->index[p], CD_MFACE);
634         
635         randu= BLI_rng_get_float(thread->rng);
636         randv= BLI_rng_get_float(thread->rng);
637         rng_skip_tot -= 2;
638         
639         psys_uv_to_w(randu, randv, mf->v4, cpa->fuv);
640         
641         cpa->num = ctx->index[p];
642         
643         if (ctx->tree) {
644                 KDTreeNearest ptn[10];
645                 int w,maxw;//, do_seams;
646                 float maxd /*, mind,dd */, totw= 0.0f;
647                 int parent[10];
648                 float pweight[10];
649                 
650                 psys_particle_on_dm(dm,cfrom,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co1,nor1,NULL,NULL,orco1,NULL);
651                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco1, 1, 1);
652                 maxw = BLI_kdtree_find_nearest_n(ctx->tree,orco1,ptn,3);
653                 
654                 maxd=ptn[maxw-1].dist;
655                 /* mind=ptn[0].dist; */ /* UNUSED */
656                 
657                 /* the weights here could be done better */
658                 for (w=0; w<maxw; w++) {
659                         parent[w]=ptn[w].index;
660                         pweight[w]=(float)pow(2.0,(double)(-6.0f*ptn[w].dist/maxd));
661                 }
662                 for (;w<10; w++) {
663                         parent[w]=-1;
664                         pweight[w]=0.0f;
665                 }
666                 
667                 for (w=0,i=0; w<maxw && i<4; w++) {
668                         if (parent[w]>=0) {
669                                 cpa->pa[i]=parent[w];
670                                 cpa->w[i]=pweight[w];
671                                 totw+=pweight[w];
672                                 i++;
673                         }
674                 }
675                 for (;i<4; i++) {
676                         cpa->pa[i]=-1;
677                         cpa->w[i]=0.0f;
678                 }
679                 
680                 if (totw > 0.0f) {
681                         for (w = 0; w < 4; w++) {
682                                 cpa->w[w] /= totw;
683                         }
684                 }
685                 
686                 cpa->parent=cpa->pa[0];
687         }
688
689         if (rng_skip_tot > 0) /* should never be below zero */
690                 BLI_rng_skip(thread->rng, rng_skip_tot);
691 }
692
693 static void exec_distribute_parent(TaskPool * __restrict UNUSED(pool), void *taskdata, int UNUSED(threadid))
694 {
695         ParticleTask *task = taskdata;
696         ParticleSystem *psys= task->ctx->sim.psys;
697         ParticleData *pa;
698         int p;
699
700         BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP * task->begin);
701         
702         pa= psys->particles + task->begin;
703         switch (psys->part->from) {
704                 case PART_FROM_FACE:
705                         for (p = task->begin; p < task->end; ++p, ++pa)
706                                 distribute_from_faces_exec(task, pa, p);
707                         break;
708                 case PART_FROM_VOLUME:
709                         for (p = task->begin; p < task->end; ++p, ++pa)
710                                 distribute_from_volume_exec(task, pa, p);
711                         break;
712                 case PART_FROM_VERT:
713                         for (p = task->begin; p < task->end; ++p, ++pa)
714                                 distribute_from_verts_exec(task, pa, p);
715                         break;
716         }
717 }
718
719 static void exec_distribute_child(TaskPool * __restrict UNUSED(pool), void *taskdata, int UNUSED(threadid))
720 {
721         ParticleTask *task = taskdata;
722         ParticleSystem *psys = task->ctx->sim.psys;
723         ChildParticle *cpa;
724         int p;
725         
726         /* RNG skipping at the beginning */
727         cpa = psys->child;
728         for (p = 0; p < task->begin; ++p, ++cpa) {
729                 if (task->ctx->skip) /* simplification skip */
730                         BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP * task->ctx->skip[p]);
731                 
732                 BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP);
733         }
734                 
735         for (; p < task->end; ++p, ++cpa) {
736                 if (task->ctx->skip) /* simplification skip */
737                         BLI_rng_skip(task->rng, PSYS_RND_DIST_SKIP * task->ctx->skip[p]);
738                 
739                 distribute_children_exec(task, cpa, p);
740         }
741 }
742
743 static int distribute_compare_orig_index(const void *p1, const void *p2, void *user_data)
744 {
745         int *orig_index = (int *) user_data;
746         int index1 = orig_index[*(const int *)p1];
747         int index2 = orig_index[*(const int *)p2];
748
749         if (index1 < index2)
750                 return -1;
751         else if (index1 == index2) {
752                 /* this pointer comparison appears to make qsort stable for glibc,
753                  * and apparently on solaris too, makes the renders reproducible */
754                 if (p1 < p2)
755                         return -1;
756                 else if (p1 == p2)
757                         return 0;
758                 else
759                         return 1;
760         }
761         else
762                 return 1;
763 }
764
765 static void distribute_invalid(Scene *scene, ParticleSystem *psys, int from)
766 {
767         if (from == PART_FROM_CHILD) {
768                 ChildParticle *cpa;
769                 int p, totchild = psys_get_tot_child(scene, psys);
770
771                 if (psys->child && totchild) {
772                         for (p=0,cpa=psys->child; p<totchild; p++,cpa++) {
773                                 cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3] = 0.0;
774                                 cpa->foffset= 0.0f;
775                                 cpa->parent=0;
776                                 cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0;
777                                 cpa->num= -1;
778                         }
779                 }
780         }
781         else {
782                 PARTICLE_P;
783                 LOOP_PARTICLES {
784                         pa->fuv[0] = pa->fuv[1] = pa->fuv[2] = pa->fuv[3] = 0.0;
785                         pa->foffset= 0.0f;
786                         pa->num= -1;
787                 }
788         }
789 }
790
791 /* Creates a distribution of coordinates on a DerivedMesh       */
792 /* This is to denote functionality that does not yet work with mesh - only derived mesh */
793 static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, ParticleSimulationData *sim, int from)
794 {
795         Scene *scene = sim->scene;
796         DerivedMesh *finaldm = sim->psmd->dm_final;
797         Object *ob = sim->ob;
798         ParticleSystem *psys= sim->psys;
799         ParticleData *pa=0, *tpars= 0;
800         ParticleSettings *part;
801         ParticleSeam *seams= 0;
802         KDTree *tree=0;
803         DerivedMesh *dm= NULL;
804         float *jit= NULL;
805         int i, p=0;
806         int cfrom=0;
807         int totelem=0, totpart, *particle_element=0, children=0, totseam=0;
808         int jitlevel= 1, distr;
809         float *element_weight=NULL,*jitter_offset=NULL, *vweight=NULL;
810         float cur, maxweight=0.0, tweight, totweight, inv_totweight, co[3], nor[3], orco[3];
811         
812         if (ELEM(NULL, ob, psys, psys->part))
813                 return 0;
814         
815         part=psys->part;
816         totpart=psys->totpart;
817         if (totpart==0)
818                 return 0;
819         
820         if (!finaldm->deformedOnly && !finaldm->getTessFaceDataArray(finaldm, CD_ORIGINDEX)) {
821                 printf("Can't create particles with the current modifier stack, disable destructive modifiers\n");
822 // XXX          error("Can't paint with the current modifier stack, disable destructive modifiers");
823                 return 0;
824         }
825         
826         /* XXX This distribution code is totally broken in case from == PART_FROM_CHILD, it's always using finaldm
827          *     even if use_modifier_stack is unset... But making things consistent here break all existing edited
828          *     hair systems, so better wait for complete rewrite.
829          */
830
831         psys_thread_context_init(ctx, sim);
832         
833         /* First handle special cases */
834         if (from == PART_FROM_CHILD) {
835                 /* Simple children */
836                 if (part->childtype != PART_CHILD_FACES) {
837                         BLI_srandom(31415926 + psys->seed + psys->child_seed);
838                         distribute_simple_children(scene, ob, finaldm, sim->psmd->dm_deformed, psys);
839                         return 0;
840                 }
841         }
842         else {
843                 /* Grid distribution */
844                 if (part->distr==PART_DISTR_GRID && from != PART_FROM_VERT) {
845                         BLI_srandom(31415926 + psys->seed);
846
847                         if (psys->part->use_modifier_stack) {
848                                 dm = finaldm;
849                         }
850                         else {
851                                 dm = CDDM_from_mesh((Mesh*)ob->data);
852                         }
853                         DM_ensure_tessface(dm);
854
855                         distribute_grid(dm,psys);
856
857                         if (dm != finaldm) {
858                                 dm->release(dm);
859                         }
860
861                         return 0;
862                 }
863         }
864         
865         /* Create trees and original coordinates if needed */
866         if (from == PART_FROM_CHILD) {
867                 distr=PART_DISTR_RAND;
868                 BLI_srandom(31415926 + psys->seed + psys->child_seed);
869                 dm= finaldm;
870
871                 /* BMESH ONLY */
872                 DM_ensure_tessface(dm);
873
874                 children=1;
875
876                 tree=BLI_kdtree_new(totpart);
877
878                 for (p=0,pa=psys->particles; p<totpart; p++,pa++) {
879                         psys_particle_on_dm(dm,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,nor,0,0,orco,NULL);
880                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco, 1, 1);
881                         BLI_kdtree_insert(tree, p, orco);
882                 }
883
884                 BLI_kdtree_balance(tree);
885
886                 totpart = psys_get_tot_child(scene, psys);
887                 cfrom = from = PART_FROM_FACE;
888         }
889         else {
890                 distr = part->distr;
891                 BLI_srandom(31415926 + psys->seed);
892                 
893                 if (psys->part->use_modifier_stack)
894                         dm = finaldm;
895                 else
896                         dm= CDDM_from_mesh((Mesh*)ob->data);
897
898                 /* BMESH ONLY, for verts we don't care about tessfaces */
899                 if (from != PART_FROM_VERT) {
900                         DM_ensure_tessface(dm);
901                 }
902
903                 /* we need orco for consistent distributions */
904                 if (!CustomData_has_layer(&dm->vertData, CD_ORCO))
905                         DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, BKE_mesh_orco_verts_get(ob));
906
907                 if (from == PART_FROM_VERT) {
908                         MVert *mv= dm->getVertDataArray(dm, CD_MVERT);
909                         float (*orcodata)[3] = dm->getVertDataArray(dm, CD_ORCO);
910                         int totvert = dm->getNumVerts(dm);
911
912                         tree=BLI_kdtree_new(totvert);
913
914                         for (p=0; p<totvert; p++) {
915                                 if (orcodata) {
916                                         copy_v3_v3(co,orcodata[p]);
917                                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co, 1, 1);
918                                 }
919                                 else
920                                         copy_v3_v3(co,mv[p].co);
921                                 BLI_kdtree_insert(tree, p, co);
922                         }
923
924                         BLI_kdtree_balance(tree);
925                 }
926         }
927
928         /* Get total number of emission elements and allocate needed arrays */
929         totelem = (from == PART_FROM_VERT) ? dm->getNumVerts(dm) : dm->getNumTessFaces(dm);
930
931         if (totelem == 0) {
932                 distribute_invalid(scene, psys, children ? PART_FROM_CHILD : 0);
933
934                 if (G.debug & G_DEBUG)
935                         fprintf(stderr,"Particle distribution error: Nothing to emit from!\n");
936
937                 if (dm != finaldm) dm->release(dm);
938
939                 BLI_kdtree_free(tree);
940
941                 return 0;
942         }
943
944         element_weight  = MEM_callocN(sizeof(float)*totelem, "particle_distribution_weights");
945         particle_element= MEM_callocN(sizeof(int)*totpart, "particle_distribution_indexes");
946         jitter_offset   = MEM_callocN(sizeof(float)*totelem, "particle_distribution_jitoff");
947
948         /* Calculate weights from face areas */
949         if ((part->flag&PART_EDISTR || children) && from != PART_FROM_VERT) {
950                 MVert *v1, *v2, *v3, *v4;
951                 float totarea=0.f, co1[3], co2[3], co3[3], co4[3];
952                 float (*orcodata)[3];
953                 
954                 orcodata= dm->getVertDataArray(dm, CD_ORCO);
955
956                 for (i=0; i<totelem; i++) {
957                         MFace *mf=dm->getTessFaceData(dm,i,CD_MFACE);
958
959                         if (orcodata) {
960                                 copy_v3_v3(co1, orcodata[mf->v1]);
961                                 copy_v3_v3(co2, orcodata[mf->v2]);
962                                 copy_v3_v3(co3, orcodata[mf->v3]);
963                                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co1, 1, 1);
964                                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co2, 1, 1);
965                                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co3, 1, 1);
966                                 if (mf->v4) {
967                                         copy_v3_v3(co4, orcodata[mf->v4]);
968                                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co4, 1, 1);
969                                 }
970                         }
971                         else {
972                                 v1= (MVert*)dm->getVertData(dm,mf->v1,CD_MVERT);
973                                 v2= (MVert*)dm->getVertData(dm,mf->v2,CD_MVERT);
974                                 v3= (MVert*)dm->getVertData(dm,mf->v3,CD_MVERT);
975                                 copy_v3_v3(co1, v1->co);
976                                 copy_v3_v3(co2, v2->co);
977                                 copy_v3_v3(co3, v3->co);
978                                 if (mf->v4) {
979                                         v4= (MVert*)dm->getVertData(dm,mf->v4,CD_MVERT);
980                                         copy_v3_v3(co4, v4->co);
981                                 }
982                         }
983
984                         cur = mf->v4 ? area_quad_v3(co1, co2, co3, co4) : area_tri_v3(co1, co2, co3);
985                         
986                         if (cur > maxweight)
987                                 maxweight = cur;
988
989                         element_weight[i] = cur;
990                         totarea += cur;
991                 }
992
993                 for (i=0; i<totelem; i++)
994                         element_weight[i] /= totarea;
995
996                 maxweight /= totarea;
997         }
998         else {
999                 float min=1.0f/(float)(MIN2(totelem,totpart));
1000                 for (i=0; i<totelem; i++)
1001                         element_weight[i]=min;
1002                 maxweight=min;
1003         }
1004
1005         /* Calculate weights from vgroup */
1006         vweight = psys_cache_vgroup(dm,psys,PSYS_VG_DENSITY);
1007
1008         if (vweight) {
1009                 if (from==PART_FROM_VERT) {
1010                         for (i=0;i<totelem; i++)
1011                                 element_weight[i]*=vweight[i];
1012                 }
1013                 else { /* PART_FROM_FACE / PART_FROM_VOLUME */
1014                         for (i=0;i<totelem; i++) {
1015                                 MFace *mf=dm->getTessFaceData(dm,i,CD_MFACE);
1016                                 tweight = vweight[mf->v1] + vweight[mf->v2] + vweight[mf->v3];
1017                                 
1018                                 if (mf->v4) {
1019                                         tweight += vweight[mf->v4];
1020                                         tweight /= 4.0f;
1021                                 }
1022                                 else {
1023                                         tweight /= 3.0f;
1024                                 }
1025
1026                                 element_weight[i]*=tweight;
1027                         }
1028                 }
1029                 MEM_freeN(vweight);
1030         }
1031
1032         /* Calculate total weight of all elements */
1033         int totmapped = 0;
1034         totweight = 0.0f;
1035         for (i = 0; i < totelem; i++) {
1036                 if (element_weight[i] > 0.0f) {
1037                         totmapped++;
1038                         totweight += element_weight[i];
1039                 }
1040         }
1041
1042         if (totmapped == 0) {
1043                 /* We are not allowed to distribute particles anywhere... */
1044                 return 0;
1045         }
1046
1047         inv_totweight = 1.0f / totweight;
1048
1049         /* Calculate cumulative weights.
1050          * We remove all null-weighted elements from element_sum, and create a new mapping
1051          * 'activ'_elem_index -> orig_elem_index.
1052          * This simplifies greatly the filtering of zero-weighted items - and can be much more efficient
1053          * especially in random case (reducing a lot the size of binary-searched array)...
1054          */
1055         float *element_sum = MEM_mallocN(sizeof(*element_sum) * totmapped, __func__);
1056         int *element_map = MEM_mallocN(sizeof(*element_map) * totmapped, __func__);
1057         int i_mapped = 0;
1058
1059         for (i = 0; i < totelem && element_weight[i] == 0.0f; i++);
1060         element_sum[i_mapped] = element_weight[i] * inv_totweight;
1061         element_map[i_mapped] = i;
1062         i_mapped++;
1063         for (i++; i < totelem; i++) {
1064                 if (element_weight[i] > 0.0f) {
1065                         element_sum[i_mapped] = element_sum[i_mapped - 1] + element_weight[i] * inv_totweight;
1066                         /* Skip elements which weight is so small that it does not affect the sum. */
1067                         if (element_sum[i_mapped] > element_sum[i_mapped - 1]) {
1068                                 element_map[i_mapped] = i;
1069                                 i_mapped++;
1070                         }
1071                 }
1072         }
1073         totmapped = i_mapped;
1074
1075         /* Finally assign elements to particles */
1076         if ((part->flag & PART_TRAND) || (part->simplify_flag & PART_SIMPLIFY_ENABLE)) {
1077                 for (p = 0; p < totpart; p++) {
1078                         /* In theory element_sum[totmapped - 1] should be 1.0,
1079                          * but due to float errors this is not necessarily always true, so scale pos accordingly. */
1080                         const float pos = BLI_frand() * element_sum[totmapped - 1];
1081                         const int eidx = distribute_binary_search(element_sum, totmapped, pos);
1082                         particle_element[p] = element_map[eidx];
1083                         BLI_assert(pos <= element_sum[eidx]);
1084                         BLI_assert(eidx ? (pos > element_sum[eidx - 1]) : (pos >= 0.0f));
1085                         jitter_offset[particle_element[p]] = pos;
1086                 }
1087         }
1088         else {
1089                 double step, pos;
1090                 
1091                 step = (totpart < 2) ? 0.5 : 1.0 / (double)totpart;
1092                 /* This is to address tricky issues with vertex-emitting when user tries (and expects) exact 1-1 vert/part
1093                  * distribution (see T47983 and its two example files). It allows us to consider pos as
1094                  * 'midpoint between v and v+1' (or 'p and p+1', depending whether we have more vertices than particles or not),
1095                  * and avoid stumbling over float imprecisions in element_sum. */
1096                 if (from == PART_FROM_VERT) {
1097                         pos = (totpart < totmapped) ? 0.5 / (double)totmapped : step * 0.5;  /* We choose the smaller step. */
1098                 }
1099                 else {
1100                         pos = 0.0;
1101                 }
1102
1103                 for (i = 0, p = 0; p < totpart; p++, pos += step) {
1104                         for ( ; (i < totmapped - 1) && (pos > (double)element_sum[i]); i++);
1105
1106                         particle_element[p] = element_map[i];
1107
1108                         jitter_offset[particle_element[p]] = pos;
1109                 }
1110         }
1111
1112         MEM_freeN(element_sum);
1113         MEM_freeN(element_map);
1114
1115         /* For hair, sort by origindex (allows optimization's in rendering), */
1116         /* however with virtual parents the children need to be in random order. */
1117         if (part->type == PART_HAIR && !(part->childtype==PART_CHILD_FACES && part->parents != 0.0f)) {
1118                 int *orig_index = NULL;
1119
1120                 if (from == PART_FROM_VERT) {
1121                         if (dm->numVertData)
1122                                 orig_index = dm->getVertDataArray(dm, CD_ORIGINDEX);
1123                 }
1124                 else {
1125                         if (dm->numTessFaceData)
1126                                 orig_index = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
1127                 }
1128
1129                 if (orig_index) {
1130                         BLI_qsort_r(particle_element, totpart, sizeof(int), distribute_compare_orig_index, orig_index);
1131                 }
1132         }
1133
1134         /* Create jittering if needed */
1135         if (distr==PART_DISTR_JIT && ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
1136                 jitlevel= part->userjit;
1137                 
1138                 if (jitlevel == 0) {
1139                         jitlevel= totpart/totelem;
1140                         if (part->flag & PART_EDISTR) jitlevel*= 2;     /* looks better in general, not very scietific */
1141                         if (jitlevel<3) jitlevel= 3;
1142                 }
1143                 
1144                 jit= MEM_callocN((2+ jitlevel*2)*sizeof(float), "jit");
1145
1146                 /* for small amounts of particles we use regular jitter since it looks
1147                  * a bit better, for larger amounts we switch to hammersley sequence 
1148                  * because it is much faster */
1149                 if (jitlevel < 25)
1150                         init_mv_jit(jit, jitlevel, psys->seed, part->jitfac);
1151                 else
1152                         hammersley_create(jit, jitlevel+1, psys->seed, part->jitfac);
1153                 BLI_array_randomize(jit, 2*sizeof(float), jitlevel, psys->seed); /* for custom jit or even distribution */
1154         }
1155
1156         /* Setup things for threaded distribution */
1157         ctx->tree= tree;
1158         ctx->seams= seams;
1159         ctx->totseam= totseam;
1160         ctx->sim.psys= psys;
1161         ctx->index= particle_element;
1162         ctx->jit= jit;
1163         ctx->jitlevel= jitlevel;
1164         ctx->jitoff= jitter_offset;
1165         ctx->weight= element_weight;
1166         ctx->maxweight= maxweight;
1167         ctx->cfrom= cfrom;
1168         ctx->distr= distr;
1169         ctx->dm= dm;
1170         ctx->tpars= tpars;
1171
1172         if (children) {
1173                 totpart= psys_render_simplify_distribution(ctx, totpart);
1174                 alloc_child_particles(psys, totpart);
1175         }
1176
1177         return 1;
1178 }
1179
1180 static void psys_task_init_distribute(ParticleTask *task, ParticleSimulationData *sim)
1181 {
1182         /* init random number generator */
1183         int seed = 31415926 + sim->psys->seed;
1184         
1185         task->rng = BLI_rng_new(seed);
1186 }
1187
1188 static void distribute_particles_on_dm(ParticleSimulationData *sim, int from)
1189 {
1190         TaskScheduler *task_scheduler;
1191         TaskPool *task_pool;
1192         ParticleThreadContext ctx;
1193         ParticleTask *tasks;
1194         DerivedMesh *finaldm = sim->psmd->dm_final;
1195         int i, totpart, numtasks;
1196         
1197         /* create a task pool for distribution tasks */
1198         if (!psys_thread_context_init_distribute(&ctx, sim, from))
1199                 return;
1200         
1201         task_scheduler = BLI_task_scheduler_get();
1202         task_pool = BLI_task_pool_create(task_scheduler, &ctx);
1203         
1204         totpart = (from == PART_FROM_CHILD ? sim->psys->totchild : sim->psys->totpart);
1205         psys_tasks_create(&ctx, 0, totpart, &tasks, &numtasks);
1206         for (i = 0; i < numtasks; ++i) {
1207                 ParticleTask *task = &tasks[i];
1208                 
1209                 psys_task_init_distribute(task, sim);
1210                 if (from == PART_FROM_CHILD)
1211                         BLI_task_pool_push(task_pool, exec_distribute_child, task, false, TASK_PRIORITY_LOW);
1212                 else
1213                         BLI_task_pool_push(task_pool, exec_distribute_parent, task, false, TASK_PRIORITY_LOW);
1214         }
1215         BLI_task_pool_work_and_wait(task_pool);
1216         
1217         BLI_task_pool_free(task_pool);
1218         
1219         psys_calc_dmcache(sim->ob, finaldm, sim->psmd->dm_deformed, sim->psys);
1220         
1221         if (ctx.dm != finaldm)
1222                 ctx.dm->release(ctx.dm);
1223         
1224         psys_tasks_free(tasks, numtasks);
1225         
1226         psys_thread_context_free(&ctx);
1227 }
1228
1229 /* ready for future use, to emit particles without geometry */
1230 static void distribute_particles_on_shape(ParticleSimulationData *sim, int UNUSED(from))
1231 {
1232         distribute_invalid(sim->scene, sim->psys, 0);
1233
1234         fprintf(stderr,"Shape emission not yet possible!\n");
1235 }
1236
1237 void distribute_particles(ParticleSimulationData *sim, int from)
1238 {
1239         PARTICLE_PSMD;
1240         int distr_error=0;
1241
1242         if (psmd) {
1243                 if (psmd->dm_final)
1244                         distribute_particles_on_dm(sim, from);
1245                 else
1246                         distr_error=1;
1247         }
1248         else
1249                 distribute_particles_on_shape(sim, from);
1250
1251         if (distr_error) {
1252                 distribute_invalid(sim->scene, sim->psys, from);
1253
1254                 fprintf(stderr,"Particle distribution error!\n");
1255         }
1256 }
1257
1258 /* ======== Simplify ======== */
1259
1260 static float psys_render_viewport_falloff(double rate, float dist, float width)
1261 {
1262         return pow(rate, dist / width);
1263 }
1264
1265 static float psys_render_projected_area(ParticleSystem *psys, const float center[3], float area, double vprate, float *viewport)
1266 {
1267         ParticleRenderData *data = psys->renderdata;
1268         float co[4], view[3], ortho1[3], ortho2[3], w, dx, dy, radius;
1269         
1270         /* transform to view space */
1271         copy_v3_v3(co, center);
1272         co[3] = 1.0f;
1273         mul_m4_v4(data->viewmat, co);
1274         
1275         /* compute two vectors orthogonal to view vector */
1276         normalize_v3_v3(view, co);
1277         ortho_basis_v3v3_v3(ortho1, ortho2, view);
1278
1279         /* compute on screen minification */
1280         w = co[2] * data->winmat[2][3] + data->winmat[3][3];
1281         dx = data->winx * ortho2[0] * data->winmat[0][0];
1282         dy = data->winy * ortho2[1] * data->winmat[1][1];
1283         w = sqrtf(dx * dx + dy * dy) / w;
1284
1285         /* w squared because we are working with area */
1286         area = area * w * w;
1287
1288         /* viewport of the screen test */
1289
1290         /* project point on screen */
1291         mul_m4_v4(data->winmat, co);
1292         if (co[3] != 0.0f) {
1293                 co[0] = 0.5f * data->winx * (1.0f + co[0] / co[3]);
1294                 co[1] = 0.5f * data->winy * (1.0f + co[1] / co[3]);
1295         }
1296
1297         /* screen space radius */
1298         radius = sqrtf(area / (float)M_PI);
1299
1300         /* make smaller using fallof once over screen edge */
1301         *viewport = 1.0f;
1302
1303         if (co[0] + radius < 0.0f)
1304                 *viewport *= psys_render_viewport_falloff(vprate, -(co[0] + radius), data->winx);
1305         else if (co[0] - radius > data->winx)
1306                 *viewport *= psys_render_viewport_falloff(vprate, (co[0] - radius) - data->winx, data->winx);
1307
1308         if (co[1] + radius < 0.0f)
1309                 *viewport *= psys_render_viewport_falloff(vprate, -(co[1] + radius), data->winy);
1310         else if (co[1] - radius > data->winy)
1311                 *viewport *= psys_render_viewport_falloff(vprate, (co[1] - radius) - data->winy, data->winy);
1312         
1313         return area;
1314 }
1315
1316 /* BMESH_TODO, for orig face data, we need to use MPoly */
1317 static int psys_render_simplify_distribution(ParticleThreadContext *ctx, int tot)
1318 {
1319         DerivedMesh *dm = ctx->dm;
1320         Mesh *me = (Mesh *)(ctx->sim.ob->data);
1321         MFace *mf, *mface;
1322         MVert *mvert;
1323         ParticleRenderData *data;
1324         ParticleRenderElem *elems, *elem;
1325         ParticleSettings *part = ctx->sim.psys->part;
1326         float *facearea, (*facecenter)[3], size[3], fac, powrate, scaleclamp;
1327         float co1[3], co2[3], co3[3], co4[3], lambda, arearatio, t, area, viewport;
1328         double vprate;
1329         int *facetotvert;
1330         int a, b, totorigface, totface, newtot, skipped;
1331
1332         /* double lookup */
1333         const int *index_mf_to_mpoly;
1334         const int *index_mp_to_orig;
1335
1336         if (part->ren_as != PART_DRAW_PATH || !(part->draw & PART_DRAW_REN_STRAND))
1337                 return tot;
1338         if (!ctx->sim.psys->renderdata)
1339                 return tot;
1340
1341         data = ctx->sim.psys->renderdata;
1342         if (data->timeoffset)
1343                 return 0;
1344         if (!(part->simplify_flag & PART_SIMPLIFY_ENABLE))
1345                 return tot;
1346
1347         mvert = dm->getVertArray(dm);
1348         mface = dm->getTessFaceArray(dm);
1349         totface = dm->getNumTessFaces(dm);
1350         totorigface = me->totpoly;
1351
1352         if (totface == 0 || totorigface == 0)
1353                 return tot;
1354
1355         index_mf_to_mpoly = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
1356         index_mp_to_orig  = dm->getPolyDataArray(dm, CD_ORIGINDEX);
1357         if (index_mf_to_mpoly == NULL) {
1358                 index_mp_to_orig = NULL;
1359         }
1360
1361         facearea = MEM_callocN(sizeof(float) * totorigface, "SimplifyFaceArea");
1362         facecenter = MEM_callocN(sizeof(float[3]) * totorigface, "SimplifyFaceCenter");
1363         facetotvert = MEM_callocN(sizeof(int) * totorigface, "SimplifyFaceArea");
1364         elems = MEM_callocN(sizeof(ParticleRenderElem) * totorigface, "SimplifyFaceElem");
1365
1366         if (data->elems)
1367                 MEM_freeN(data->elems);
1368
1369         data->do_simplify = true;
1370         data->elems = elems;
1371         data->index_mf_to_mpoly = index_mf_to_mpoly;
1372         data->index_mp_to_orig  = index_mp_to_orig;
1373
1374         /* compute number of children per original face */
1375         for (a = 0; a < tot; a++) {
1376                 b = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, ctx->index[a]) : ctx->index[a];
1377                 if (b != ORIGINDEX_NONE) {
1378                         elems[b].totchild++;
1379                 }
1380         }
1381
1382         /* compute areas and centers of original faces */
1383         for (mf = mface, a = 0; a < totface; a++, mf++) {
1384                 b = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, a) : a;
1385
1386                 if (b != ORIGINDEX_NONE) {
1387                         copy_v3_v3(co1, mvert[mf->v1].co);
1388                         copy_v3_v3(co2, mvert[mf->v2].co);
1389                         copy_v3_v3(co3, mvert[mf->v3].co);
1390
1391                         add_v3_v3(facecenter[b], co1);
1392                         add_v3_v3(facecenter[b], co2);
1393                         add_v3_v3(facecenter[b], co3);
1394
1395                         if (mf->v4) {
1396                                 copy_v3_v3(co4, mvert[mf->v4].co);
1397                                 add_v3_v3(facecenter[b], co4);
1398                                 facearea[b] += area_quad_v3(co1, co2, co3, co4);
1399                                 facetotvert[b] += 4;
1400                         }
1401                         else {
1402                                 facearea[b] += area_tri_v3(co1, co2, co3);
1403                                 facetotvert[b] += 3;
1404                         }
1405                 }
1406         }
1407
1408         for (a = 0; a < totorigface; a++)
1409                 if (facetotvert[a] > 0)
1410                         mul_v3_fl(facecenter[a], 1.0f / facetotvert[a]);
1411
1412         /* for conversion from BU area / pixel area to reference screen size */
1413         BKE_mesh_texspace_get(me, 0, 0, size);
1414         fac = ((size[0] + size[1] + size[2]) / 3.0f) / part->simplify_refsize;
1415         fac = fac * fac;
1416
1417         powrate = log(0.5f) / log(part->simplify_rate * 0.5f);
1418         if (part->simplify_flag & PART_SIMPLIFY_VIEWPORT)
1419                 vprate = pow(1.0f - part->simplify_viewport, 5.0);
1420         else
1421                 vprate = 1.0;
1422
1423         /* set simplification parameters per original face */
1424         for (a = 0, elem = elems; a < totorigface; a++, elem++) {
1425                 area = psys_render_projected_area(ctx->sim.psys, facecenter[a], facearea[a], vprate, &viewport);
1426                 arearatio = fac * area / facearea[a];
1427
1428                 if ((arearatio < 1.0f || viewport < 1.0f) && elem->totchild) {
1429                         /* lambda is percentage of elements to keep */
1430                         lambda = (arearatio < 1.0f) ? powf(arearatio, powrate) : 1.0f;
1431                         lambda *= viewport;
1432
1433                         lambda = MAX2(lambda, 1.0f / elem->totchild);
1434
1435                         /* compute transition region */
1436                         t = part->simplify_transition;
1437                         elem->t = (lambda - t < 0.0f) ? lambda : (lambda + t > 1.0f) ? 1.0f - lambda : t;
1438                         elem->reduce = 1;
1439
1440                         /* scale at end and beginning of the transition region */
1441                         elem->scalemax = (lambda + t < 1.0f) ? 1.0f / lambda : 1.0f / (1.0f - elem->t * elem->t / t);
1442                         elem->scalemin = (lambda + t < 1.0f) ? 0.0f : elem->scalemax * (1.0f - elem->t / t);
1443
1444                         elem->scalemin = sqrtf(elem->scalemin);
1445                         elem->scalemax = sqrtf(elem->scalemax);
1446
1447                         /* clamp scaling */
1448                         scaleclamp = (float)min_ii(elem->totchild, 10);
1449                         elem->scalemin = MIN2(scaleclamp, elem->scalemin);
1450                         elem->scalemax = MIN2(scaleclamp, elem->scalemax);
1451
1452                         /* extend lambda to include transition */
1453                         lambda = lambda + elem->t;
1454                         if (lambda > 1.0f)
1455                                 lambda = 1.0f;
1456                 }
1457                 else {
1458                         lambda = arearatio;
1459
1460                         elem->scalemax = 1.0f; //sqrt(lambda);
1461                         elem->scalemin = 1.0f; //sqrt(lambda);
1462                         elem->reduce = 0;
1463                 }
1464
1465                 elem->lambda = lambda;
1466                 elem->scalemin = sqrtf(elem->scalemin);
1467                 elem->scalemax = sqrtf(elem->scalemax);
1468                 elem->curchild = 0;
1469         }
1470
1471         MEM_freeN(facearea);
1472         MEM_freeN(facecenter);
1473         MEM_freeN(facetotvert);
1474
1475         /* move indices and set random number skipping */
1476         ctx->skip = MEM_callocN(sizeof(int) * tot, "SimplificationSkip");
1477
1478         skipped = 0;
1479         for (a = 0, newtot = 0; a < tot; a++) {
1480                 b = (index_mf_to_mpoly) ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, ctx->index[a]) : ctx->index[a];
1481
1482                 if (b != ORIGINDEX_NONE) {
1483                         if (elems[b].curchild++ < ceil(elems[b].lambda * elems[b].totchild)) {
1484                                 ctx->index[newtot] = ctx->index[a];
1485                                 ctx->skip[newtot] = skipped;
1486                                 skipped = 0;
1487                                 newtot++;
1488                         }
1489                         else skipped++;
1490                 }
1491                 else skipped++;
1492         }
1493
1494         for (a = 0, elem = elems; a < totorigface; a++, elem++)
1495                 elem->curchild = 0;
1496
1497         return newtot;
1498 }