Fix T39686: nused omp firstprivate variable sphdata in particles
[blender-staging.git] / source / blender / blenkernel / intern / particle_system.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), Stephen Swhitehorn.
24  *
25  * Adaptive time step
26  * Classical SPH
27  * Copyright 2011-2012 AutoCRC
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 /** \file blender/blenkernel/intern/particle_system.c
33  *  \ingroup bke
34  */
35
36
37 #include <stddef.h>
38
39 #include <stdlib.h>
40 #include <math.h>
41 #include <string.h>
42
43 #ifdef _OPENMP
44 #include <omp.h>
45 #endif
46
47 #include "MEM_guardedalloc.h"
48
49 #include "DNA_anim_types.h"
50 #include "DNA_boid_types.h"
51 #include "DNA_particle_types.h"
52 #include "DNA_mesh_types.h"
53 #include "DNA_meshdata_types.h"
54 #include "DNA_modifier_types.h"
55 #include "DNA_object_force.h"
56 #include "DNA_object_types.h"
57 #include "DNA_curve_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_texture_types.h"
60 #include "DNA_listBase.h"
61
62 #include "BLI_utildefines.h"
63 #include "BLI_edgehash.h"
64 #include "BLI_rand.h"
65 #include "BLI_jitter.h"
66 #include "BLI_math.h"
67 #include "BLI_blenlib.h"
68 #include "BLI_kdtree.h"
69 #include "BLI_kdopbvh.h"
70 #include "BLI_sort.h"
71 #include "BLI_threads.h"
72 #include "BLI_linklist.h"
73
74 #include "BKE_animsys.h"
75 #include "BKE_boids.h"
76 #include "BKE_cdderivedmesh.h"
77 #include "BKE_collision.h"
78 #include "BKE_effect.h"
79 #include "BKE_particle.h"
80 #include "BKE_global.h"
81
82 #include "BKE_DerivedMesh.h"
83 #include "BKE_object.h"
84 #include "BKE_material.h"
85 #include "BKE_cloth.h"
86 #include "BKE_lattice.h"
87 #include "BKE_pointcache.h"
88 #include "BKE_mesh.h"
89 #include "BKE_modifier.h"
90 #include "BKE_scene.h"
91 #include "BKE_bvhutils.h"
92
93 #include "PIL_time.h"
94
95 #include "RE_shader_ext.h"
96
97 /* fluid sim particle import */
98 #ifdef WITH_MOD_FLUID
99 #include "DNA_object_fluidsim.h"
100 #include "LBM_fluidsim.h"
101 #include <zlib.h>
102 #include <string.h>
103
104 #endif // WITH_MOD_FLUID
105
106 static ThreadRWMutex psys_bvhtree_rwlock = BLI_RWLOCK_INITIALIZER;
107
108 /************************************************/
109 /*                      Reacting to system events                       */
110 /************************************************/
111
112 static int particles_are_dynamic(ParticleSystem *psys)
113 {
114         if (psys->pointcache->flag & PTCACHE_BAKED)
115                 return 0;
116
117         if (psys->part->type == PART_HAIR)
118                 return psys->flag & PSYS_HAIR_DYNAMICS;
119         else
120                 return ELEM3(psys->part->phystype, PART_PHYS_NEWTON, PART_PHYS_BOIDS, PART_PHYS_FLUID);
121 }
122
123 float psys_get_current_display_percentage(ParticleSystem *psys)
124 {
125         ParticleSettings *part=psys->part;
126
127         if ((psys->renderdata && !particles_are_dynamic(psys)) ||  /* non-dynamic particles can be rendered fully */
128             (part->child_nbr && part->childtype)  ||    /* display percentage applies to children */
129             (psys->pointcache->flag & PTCACHE_BAKING))  /* baking is always done with full amount */
130         {
131                 return 1.0f;
132         }
133
134         return psys->part->disp/100.0f;
135 }
136
137 static int tot_particles(ParticleSystem *psys, PTCacheID *pid)
138 {
139         if (pid && psys->pointcache->flag & PTCACHE_EXTERNAL)
140                 return pid->cache->totpoint;
141         else if (psys->part->distr == PART_DISTR_GRID && psys->part->from != PART_FROM_VERT)
142                 return psys->part->grid_res * psys->part->grid_res * psys->part->grid_res - psys->totunexist;
143         else
144                 return psys->part->totpart - psys->totunexist;
145 }
146
147 void psys_reset(ParticleSystem *psys, int mode)
148 {
149         PARTICLE_P;
150
151         if (ELEM(mode, PSYS_RESET_ALL, PSYS_RESET_DEPSGRAPH)) {
152                 if (mode == PSYS_RESET_ALL || !(psys->flag & PSYS_EDITED)) {
153                         /* don't free if not absolutely necessary */
154                         if (psys->totpart != tot_particles(psys, NULL)) {
155                                 psys_free_particles(psys);
156                                 psys->totpart= 0;
157                         }
158
159                         psys->totkeyed= 0;
160                         psys->flag &= ~(PSYS_HAIR_DONE|PSYS_KEYED);
161
162                         if (psys->edit && psys->free_edit) {
163                                 psys->free_edit(psys->edit);
164                                 psys->edit = NULL;
165                                 psys->free_edit = NULL;
166                         }
167                 }
168         }
169         else if (mode == PSYS_RESET_CACHE_MISS) {
170                 /* set all particles to be skipped */
171                 LOOP_PARTICLES
172                         pa->flag |= PARS_NO_DISP;
173         }
174
175         /* reset children */
176         if (psys->child) {
177                 MEM_freeN(psys->child);
178                 psys->child= NULL;
179         }
180
181         psys->totchild= 0;
182
183         /* reset path cache */
184         psys_free_path_cache(psys, psys->edit);
185
186         /* reset point cache */
187         BKE_ptcache_invalidate(psys->pointcache);
188
189         if (psys->fluid_springs) {
190                 MEM_freeN(psys->fluid_springs);
191                 psys->fluid_springs = NULL;
192         }
193
194         psys->tot_fluidsprings = psys->alloc_fluidsprings = 0;
195 }
196
197 static void realloc_particles(ParticleSimulationData *sim, int new_totpart)
198 {
199         ParticleSystem *psys = sim->psys;
200         ParticleSettings *part = psys->part;
201         ParticleData *newpars = NULL;
202         BoidParticle *newboids = NULL;
203         PARTICLE_P;
204         int totpart, totsaved = 0;
205
206         if (new_totpart<0) {
207                 if ((part->distr == PART_DISTR_GRID) && (part->from != PART_FROM_VERT)) {
208                         totpart= part->grid_res;
209                         totpart*=totpart*totpart;
210                 }
211                 else
212                         totpart=part->totpart;
213         }
214         else
215                 totpart=new_totpart;
216
217         if (totpart != psys->totpart) {
218                 if (psys->edit && psys->free_edit) {
219                         psys->free_edit(psys->edit);
220                         psys->edit = NULL;
221                         psys->free_edit = NULL;
222                 }
223
224                 if (totpart) {
225                         newpars= MEM_callocN(totpart*sizeof(ParticleData), "particles");
226                         if (newpars == NULL)
227                                 return;
228
229                         if (psys->part->phystype == PART_PHYS_BOIDS) {
230                                 newboids= MEM_callocN(totpart*sizeof(BoidParticle), "boid particles");
231
232                                 if (newboids == NULL) {
233                                          /* allocation error! */
234                                         if (newpars)
235                                                 MEM_freeN(newpars);
236                                         return;
237                                 }
238                         }
239                 }
240         
241                 if (psys->particles) {
242                         totsaved=MIN2(psys->totpart,totpart);
243                         /*save old pars*/
244                         if (totsaved) {
245                                 memcpy(newpars,psys->particles,totsaved*sizeof(ParticleData));
246
247                                 if (psys->particles->boid)
248                                         memcpy(newboids, psys->particles->boid, totsaved*sizeof(BoidParticle));
249                         }
250
251                         if (psys->particles->keys)
252                                 MEM_freeN(psys->particles->keys);
253
254                         if (psys->particles->boid)
255                                 MEM_freeN(psys->particles->boid);
256
257                         for (p=0, pa=newpars; p<totsaved; p++, pa++) {
258                                 if (pa->keys) {
259                                         pa->keys= NULL;
260                                         pa->totkey= 0;
261                                 }
262                         }
263
264                         for (p=totsaved, pa=psys->particles+totsaved; p<psys->totpart; p++, pa++)
265                                 if (pa->hair) MEM_freeN(pa->hair);
266
267                         MEM_freeN(psys->particles);
268                         psys_free_pdd(psys);
269                 }
270                 
271                 psys->particles=newpars;
272                 psys->totpart=totpart;
273
274                 if (newboids) {
275                         LOOP_PARTICLES
276                                 pa->boid = newboids++;
277                 }
278         }
279
280         if (psys->child) {
281                 MEM_freeN(psys->child);
282                 psys->child=NULL;
283                 psys->totchild=0;
284         }
285 }
286
287 static int get_psys_child_number(struct Scene *scene, ParticleSystem *psys)
288 {
289         int nbr;
290
291         if (!psys->part->childtype)
292                 return 0;
293
294         if (psys->renderdata)
295                 nbr= psys->part->ren_child_nbr;
296         else
297                 nbr= psys->part->child_nbr;
298
299         return get_render_child_particle_number(&scene->r, nbr);
300 }
301
302 static int get_psys_tot_child(struct Scene *scene, ParticleSystem *psys)
303 {
304         return psys->totpart*get_psys_child_number(scene, psys);
305 }
306
307 static void alloc_child_particles(ParticleSystem *psys, int tot)
308 {
309         if (psys->child) {
310                 /* only re-allocate if we have to */
311                 if (psys->part->childtype && psys->totchild == tot) {
312                         memset(psys->child, 0, tot*sizeof(ChildParticle));
313                         return;
314                 }
315
316                 MEM_freeN(psys->child);
317                 psys->child=NULL;
318                 psys->totchild=0;
319         }
320
321         if (psys->part->childtype) {
322                 psys->totchild= tot;
323                 if (psys->totchild)
324                         psys->child= MEM_callocN(psys->totchild*sizeof(ChildParticle), "child_particles");
325         }
326 }
327
328 /************************************************/
329 /*                      Distribution                                            */
330 /************************************************/
331
332 void psys_calc_dmcache(Object *ob, DerivedMesh *dm, ParticleSystem *psys)
333 {
334         /* use for building derived mesh mapping info:
335          *
336          * node: the allocated links - total derived mesh element count 
337          * nodearray: the array of nodes aligned with the base mesh's elements, so
338          *            each original elements can reference its derived elements
339          */
340         Mesh *me= (Mesh*)ob->data;
341         bool use_modifier_stack= psys->part->use_modifier_stack;
342         PARTICLE_P;
343         
344         /* CACHE LOCATIONS */
345         if (!dm->deformedOnly) {
346                 /* Will use later to speed up subsurf/derivedmesh */
347                 LinkNode *node, *nodedmelem, **nodearray;
348                 int totdmelem, totelem, i, *origindex, *origindex_poly = NULL;
349
350                 if (psys->part->from == PART_FROM_VERT) {
351                         totdmelem= dm->getNumVerts(dm);
352
353                         if (use_modifier_stack) {
354                                 totelem= totdmelem;
355                                 origindex= NULL;
356                         }
357                         else {
358                                 totelem= me->totvert;
359                                 origindex= dm->getVertDataArray(dm, CD_ORIGINDEX);
360                         }
361                 }
362                 else { /* FROM_FACE/FROM_VOLUME */
363                         totdmelem= dm->getNumTessFaces(dm);
364
365                         if (use_modifier_stack) {
366                                 totelem= totdmelem;
367                                 origindex= NULL;
368                                 origindex_poly= NULL;
369                         }
370                         else {
371                                 totelem= me->totpoly;
372                                 origindex= dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
373
374                                 /* for face lookups we need the poly origindex too */
375                                 origindex_poly= dm->getPolyDataArray(dm, CD_ORIGINDEX);
376                                 if (origindex_poly == NULL) {
377                                         origindex= NULL;
378                                 }
379                         }
380                 }
381         
382                 nodedmelem= MEM_callocN(sizeof(LinkNode)*totdmelem, "psys node elems");
383                 nodearray= MEM_callocN(sizeof(LinkNode *)*totelem, "psys node array");
384                 
385                 for (i=0, node=nodedmelem; i<totdmelem; i++, node++) {
386                         int origindex_final;
387                         node->link = SET_INT_IN_POINTER(i);
388
389                         /* may be vertex or face origindex */
390                         if (use_modifier_stack) {
391                                 origindex_final = i;
392                         }
393                         else {
394                                 origindex_final = origindex ? origindex[i] : ORIGINDEX_NONE;
395
396                                 /* if we have a poly source, do an index lookup */
397                                 if (origindex_poly && origindex_final != ORIGINDEX_NONE) {
398                                         origindex_final = origindex_poly[origindex_final];
399                                 }
400                         }
401
402                         if (origindex_final != ORIGINDEX_NONE) {
403                                 if (nodearray[origindex_final]) {
404                                         /* prepend */
405                                         node->next = nodearray[origindex_final];
406                                         nodearray[origindex_final] = node;
407                                 }
408                                 else {
409                                         nodearray[origindex_final] = node;
410                                 }
411                         }
412                 }
413                 
414                 /* cache the verts/faces! */
415                 LOOP_PARTICLES {
416                         if (pa->num < 0) {
417                                 pa->num_dmcache = DMCACHE_NOTFOUND;
418                                 continue;
419                         }
420
421                         if (use_modifier_stack) {
422                                 if (pa->num < totelem)
423                                         pa->num_dmcache = DMCACHE_ISCHILD;
424                                 else
425                                         pa->num_dmcache = DMCACHE_NOTFOUND;
426                         }
427                         else {
428                                 if (psys->part->from == PART_FROM_VERT) {
429                                         if (pa->num < totelem && nodearray[pa->num])
430                                                 pa->num_dmcache= GET_INT_FROM_POINTER(nodearray[pa->num]->link);
431                                         else
432                                                 pa->num_dmcache = DMCACHE_NOTFOUND;
433                                 }
434                                 else { /* FROM_FACE/FROM_VOLUME */
435                                         /* Note that sometimes the pa->num is over the nodearray size, this is bad, maybe there is a better place to fix this,
436                                          * but for now passing NULL is OK. every face will be searched for the particle so its slower - Campbell */
437                                         pa->num_dmcache= psys_particle_dm_face_lookup(ob, dm, pa->num, pa->fuv, pa->num < totelem ? nodearray[pa->num] : NULL);
438                                 }
439                         }
440                 }
441
442                 MEM_freeN(nodearray);
443                 MEM_freeN(nodedmelem);
444         }
445         else {
446                 /* TODO PARTICLE, make the following line unnecessary, each function
447                  * should know to use the num or num_dmcache, set the num_dmcache to
448                  * an invalid value, just in case */
449                 
450                 LOOP_PARTICLES
451                         pa->num_dmcache = DMCACHE_NOTFOUND;
452         }
453 }
454
455 static void distribute_simple_children(Scene *scene, Object *ob, DerivedMesh *finaldm, ParticleSystem *psys)
456 {
457         ChildParticle *cpa = NULL;
458         int i, p;
459         int child_nbr= get_psys_child_number(scene, psys);
460         int totpart= get_psys_tot_child(scene, psys);
461
462         alloc_child_particles(psys, totpart);
463
464         cpa = psys->child;
465         for (i=0; i<child_nbr; i++) {
466                 for (p=0; p<psys->totpart; p++,cpa++) {
467                         float length=2.0;
468                         cpa->parent=p;
469                                         
470                         /* create even spherical distribution inside unit sphere */
471                         while (length>=1.0f) {
472                                 cpa->fuv[0]=2.0f*BLI_frand()-1.0f;
473                                 cpa->fuv[1]=2.0f*BLI_frand()-1.0f;
474                                 cpa->fuv[2]=2.0f*BLI_frand()-1.0f;
475                                 length=len_v3(cpa->fuv);
476                         }
477
478                         cpa->num=-1;
479                 }
480         }
481         /* dmcache must be updated for parent particles if children from faces is used */
482         psys_calc_dmcache(ob, finaldm, psys);
483 }
484 static void distribute_grid(DerivedMesh *dm, ParticleSystem *psys)
485 {
486         ParticleData *pa=NULL;
487         float min[3], max[3], delta[3], d;
488         MVert *mv, *mvert = dm->getVertDataArray(dm,0);
489         int totvert=dm->getNumVerts(dm), from=psys->part->from;
490         int i, j, k, p, res=psys->part->grid_res, size[3], axis;
491
492         /* find bounding box of dm */
493         if (totvert > 0) {
494                 mv=mvert;
495                 copy_v3_v3(min, mv->co);
496                 copy_v3_v3(max, mv->co);
497                 mv++;
498                 for (i = 1; i < totvert; i++, mv++) {
499                         minmax_v3v3_v3(min, max, mv->co);
500                 }
501         }
502         else {
503                 zero_v3(min);
504                 zero_v3(max);
505         }
506
507         sub_v3_v3v3(delta, max, min);
508
509         /* determine major axis */
510         axis = (delta[0]>=delta[1]) ? 0 : ((delta[1]>=delta[2]) ? 1 : 2);
511          
512         d = delta[axis]/(float)res;
513
514         size[axis] = res;
515         size[(axis+1)%3] = (int)ceil(delta[(axis+1)%3]/d);
516         size[(axis+2)%3] = (int)ceil(delta[(axis+2)%3]/d);
517
518         /* float errors grrr.. */
519         size[(axis+1)%3] = MIN2(size[(axis+1)%3],res);
520         size[(axis+2)%3] = MIN2(size[(axis+2)%3],res);
521
522         size[0] = MAX2(size[0], 1);
523         size[1] = MAX2(size[1], 1);
524         size[2] = MAX2(size[2], 1);
525
526         /* no full offset for flat/thin objects */
527         min[0]+= d < delta[0] ? d/2.f : delta[0]/2.f;
528         min[1]+= d < delta[1] ? d/2.f : delta[1]/2.f;
529         min[2]+= d < delta[2] ? d/2.f : delta[2]/2.f;
530
531         for (i=0,p=0,pa=psys->particles; i<res; i++) {
532                 for (j=0; j<res; j++) {
533                         for (k=0; k<res; k++,p++,pa++) {
534                                 pa->fuv[0] = min[0] + (float)i*d;
535                                 pa->fuv[1] = min[1] + (float)j*d;
536                                 pa->fuv[2] = min[2] + (float)k*d;
537                                 pa->flag |= PARS_UNEXIST;
538                                 pa->hair_index = 0; /* abused in volume calculation */
539                         }
540                 }
541         }
542
543         /* enable particles near verts/edges/faces/inside surface */
544         if (from==PART_FROM_VERT) {
545                 float vec[3];
546
547                 pa=psys->particles;
548
549                 min[0] -= d/2.0f;
550                 min[1] -= d/2.0f;
551                 min[2] -= d/2.0f;
552
553                 for (i=0,mv=mvert; i<totvert; i++,mv++) {
554                         sub_v3_v3v3(vec,mv->co,min);
555                         vec[0]/=delta[0];
556                         vec[1]/=delta[1];
557                         vec[2]/=delta[2];
558                         pa[((int)(vec[0] * (size[0] - 1))  * res +
559                             (int)(vec[1] * (size[1] - 1))) * res +
560                             (int)(vec[2] * (size[2] - 1))].flag &= ~PARS_UNEXIST;
561                 }
562         }
563         else if (ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
564                 float co1[3], co2[3];
565
566                 MFace *mface= NULL, *mface_array;
567                 float v1[3], v2[3], v3[3], v4[4], lambda;
568                 int a, a1, a2, a0mul, a1mul, a2mul, totface;
569                 int amax= from==PART_FROM_FACE ? 3 : 1;
570
571                 totface=dm->getNumTessFaces(dm);
572                 mface=mface_array=dm->getTessFaceDataArray(dm,CD_MFACE);
573                 
574                 for (a=0; a<amax; a++) {
575                         if (a==0) { a0mul=res*res; a1mul=res; a2mul=1; }
576                         else if (a==1) { a0mul=res; a1mul=1; a2mul=res*res; }
577                         else { a0mul=1; a1mul=res*res; a2mul=res; }
578
579                         for (a1=0; a1<size[(a+1)%3]; a1++) {
580                                 for (a2=0; a2<size[(a+2)%3]; a2++) {
581                                         mface= mface_array;
582
583                                         pa = psys->particles + a1*a1mul + a2*a2mul;
584                                         copy_v3_v3(co1, pa->fuv);
585                                         co1[a] -= d < delta[a] ? d/2.f : delta[a]/2.f;
586                                         copy_v3_v3(co2, co1);
587                                         co2[a] += delta[a] + 0.001f*d;
588                                         co1[a] -= 0.001f*d;
589                                         
590                                         /* lets intersect the faces */
591                                         for (i=0; i<totface; i++,mface++) {
592                                                 copy_v3_v3(v1, mvert[mface->v1].co);
593                                                 copy_v3_v3(v2, mvert[mface->v2].co);
594                                                 copy_v3_v3(v3, mvert[mface->v3].co);
595
596                                                 if (isect_axial_line_tri_v3(a, co1, co2, v2, v3, v1, &lambda)) {
597                                                         if (from==PART_FROM_FACE)
598                                                                 (pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST;
599                                                         else /* store number of intersections */
600                                                                 (pa+(int)(lambda*size[a])*a0mul)->hair_index++;
601                                                 }
602                                                 else if (mface->v4) {
603                                                         copy_v3_v3(v4, mvert[mface->v4].co);
604
605                                                         if (isect_axial_line_tri_v3(a, co1, co2, v4, v1, v3, &lambda)) {
606                                                                 if (from==PART_FROM_FACE)
607                                                                         (pa+(int)(lambda*size[a])*a0mul)->flag &= ~PARS_UNEXIST;
608                                                                 else
609                                                                         (pa+(int)(lambda*size[a])*a0mul)->hair_index++;
610                                                         }
611                                                 }
612                                         }
613
614                                         if (from==PART_FROM_VOLUME) {
615                                                 int in=pa->hair_index%2;
616                                                 if (in) pa->hair_index++;
617                                                 for (i=0; i<size[0]; i++) {
618                                                         if (in || (pa+i*a0mul)->hair_index%2)
619                                                                 (pa+i*a0mul)->flag &= ~PARS_UNEXIST;
620                                                         /* odd intersections == in->out / out->in */
621                                                         /* even intersections -> in stays same */
622                                                         in=(in + (pa+i*a0mul)->hair_index) % 2;
623                                                 }
624                                         }
625                                 }
626                         }
627                 }
628         }
629
630         if (psys->part->flag & PART_GRID_HEXAGONAL) {
631                 for (i=0,p=0,pa=psys->particles; i<res; i++) {
632                         for (j=0; j<res; j++) {
633                                 for (k=0; k<res; k++,p++,pa++) {
634                                         if (j%2)
635                                                 pa->fuv[0] += d/2.f;
636
637                                         if (k%2) {
638                                                 pa->fuv[0] += d/2.f;
639                                                 pa->fuv[1] += d/2.f;
640                                         }
641                                 }
642                         }
643                 }
644         }
645
646         if (psys->part->flag & PART_GRID_INVERT) {
647                 for (i=0; i<size[0]; i++) {
648                         for (j=0; j<size[1]; j++) {
649                                 pa=psys->particles + res*(i*res + j);
650                                 for (k=0; k<size[2]; k++, pa++) {
651                                         pa->flag ^= PARS_UNEXIST;
652                                 }
653                         }
654                 }
655         }
656
657         if (psys->part->grid_rand > 0.f) {
658                 float rfac = d * psys->part->grid_rand;
659                 for (p=0,pa=psys->particles; p<psys->totpart; p++,pa++) {
660                         if (pa->flag & PARS_UNEXIST)
661                                 continue;
662
663                         pa->fuv[0] += rfac * (psys_frand(psys, p + 31) - 0.5f);
664                         pa->fuv[1] += rfac * (psys_frand(psys, p + 32) - 0.5f);
665                         pa->fuv[2] += rfac * (psys_frand(psys, p + 33) - 0.5f);
666                 }
667         }
668 }
669
670 /* modified copy from rayshade.c */
671 static void hammersley_create(float *out, int n, int seed, float amount)
672 {
673         RNG *rng;
674         double p, t, offs[2];
675         int k, kk;
676
677         rng = BLI_rng_new(31415926 + n + seed);
678         offs[0] = BLI_rng_get_double(rng) + (double)amount;
679         offs[1] = BLI_rng_get_double(rng) + (double)amount;
680         BLI_rng_free(rng);
681
682         for (k = 0; k < n; k++) {
683                 t = 0;
684                 for (p = 0.5, kk = k; kk; p *= 0.5, kk >>= 1)
685                         if (kk & 1) /* kk mod 2 = 1 */
686                                 t += p;
687
688                 out[2*k + 0] = fmod((double)k/(double)n + offs[0], 1.0);
689                 out[2*k + 1] = fmod(t + offs[1], 1.0);
690         }
691 }
692
693 /* almost exact copy of BLI_jitter_init */
694 static void init_mv_jit(float *jit, int num, int seed2, float amount)
695 {
696         RNG *rng;
697         float *jit2, x, rad1, rad2, rad3;
698         int i, num2;
699
700         if (num==0) return;
701
702         rad1= (float)(1.0f/sqrtf((float)num));
703         rad2= (float)(1.0f/((float)num));
704         rad3= (float)sqrt((float)num)/((float)num);
705
706         rng = BLI_rng_new(31415926 + num + seed2);
707         x= 0;
708                 num2 = 2 * num;
709         for (i=0; i<num2; i+=2) {
710         
711                 jit[i] = x + amount*rad1*(0.5f - BLI_rng_get_float(rng));
712                 jit[i+1] = i/(2.0f*num) + amount*rad1*(0.5f - BLI_rng_get_float(rng));
713                 
714                 jit[i]-= (float)floor(jit[i]);
715                 jit[i+1]-= (float)floor(jit[i+1]);
716                 
717                 x+= rad3;
718                 x -= (float)floor(x);
719         }
720
721         jit2= MEM_mallocN(12 + 2*sizeof(float)*num, "initjit");
722
723         for (i=0 ; i<4 ; i++) {
724                 BLI_jitterate1((float (*)[2])jit, (float (*)[2])jit2, num, rad1);
725                 BLI_jitterate1((float (*)[2])jit, (float (*)[2])jit2, num, rad1);
726                 BLI_jitterate2((float (*)[2])jit, (float (*)[2])jit2, num, rad2);
727         }
728         MEM_freeN(jit2);
729         BLI_rng_free(rng);
730 }
731
732 static void psys_uv_to_w(float u, float v, int quad, float *w)
733 {
734         float vert[4][3], co[3];
735
736         if (!quad) {
737                 if (u+v > 1.0f)
738                         v= 1.0f-v;
739                 else
740                         u= 1.0f-u;
741         }
742
743         vert[0][0] = 0.0f; vert[0][1] = 0.0f; vert[0][2] = 0.0f;
744         vert[1][0] = 1.0f; vert[1][1] = 0.0f; vert[1][2] = 0.0f;
745         vert[2][0] = 1.0f; vert[2][1] = 1.0f; vert[2][2] = 0.0f;
746
747         co[0] = u;
748         co[1] = v;
749         co[2] = 0.0f;
750
751         if (quad) {
752                 vert[3][0] = 0.0f; vert[3][1] = 1.0f; vert[3][2] = 0.0f;
753                 interp_weights_poly_v3( w,vert, 4, co);
754         }
755         else {
756                 interp_weights_poly_v3( w,vert, 3, co);
757                 w[3] = 0.0f;
758         }
759 }
760
761 /* Find the index in "sum" array before "value" is crossed. */
762 static int distribute_binary_search(float *sum, int n, float value)
763 {
764         int mid, low=0, high=n;
765
766         if (value == 0.f)
767                 return 0;
768
769         while (low <= high) {
770                 mid= (low + high)/2;
771                 
772                 if (sum[mid] < value && value <= sum[mid+1])
773                         return mid;
774                 
775                 if (sum[mid] >= value)
776                         high= mid - 1;
777                 else if (sum[mid] < value)
778                         low= mid + 1;
779                 else
780                         return mid;
781         }
782
783         return low;
784 }
785
786 /* the max number if calls to rng_* funcs within psys_thread_distribute_particle
787  * be sure to keep up to date if this changes */
788 #define PSYS_RND_DIST_SKIP 2
789
790 /* note: this function must be thread safe, for from == PART_FROM_CHILD */
791 #define ONLY_WORKING_WITH_PA_VERTS 0
792 static void distribute_threads_exec(ParticleThread *thread, ParticleData *pa, ChildParticle *cpa, int p)
793 {
794         ParticleThreadContext *ctx= thread->ctx;
795         Object *ob= ctx->sim.ob;
796         DerivedMesh *dm= ctx->dm;
797         float *v1, *v2, *v3, *v4, nor[3], orco1[3], co1[3], co2[3], nor1[3];
798         float cur_d, min_d, randu, randv;
799         int from= ctx->from;
800         int cfrom= ctx->cfrom;
801         int distr= ctx->distr;
802         int i, intersect, tot;
803         int rng_skip_tot= PSYS_RND_DIST_SKIP; /* count how many rng_* calls wont need skipping */
804
805         if (from == PART_FROM_VERT) {
806                 /* TODO_PARTICLE - use original index */
807                 pa->num= ctx->index[p];
808                 pa->fuv[0] = 1.0f;
809                 pa->fuv[1] = pa->fuv[2] = pa->fuv[3] = 0.0;
810
811 #if ONLY_WORKING_WITH_PA_VERTS
812                 if (ctx->tree) {
813                         KDTreeNearest ptn[3];
814                         int w, maxw;
815
816                         psys_particle_on_dm(ctx->dm,from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co1,0,0,0,orco1,0);
817                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco1, 1, 1);
818                         maxw = BLI_kdtree_find_nearest_n(ctx->tree,orco1,ptn,3);
819
820                         for (w=0; w<maxw; w++) {
821                                 pa->verts[w]=ptn->num;
822                         }
823                 }
824 #endif
825         }
826         else if (from == PART_FROM_FACE || from == PART_FROM_VOLUME) {
827                 MFace *mface;
828
829                 pa->num = i = ctx->index[p];
830                 mface = dm->getTessFaceData(dm,i,CD_MFACE);
831                 
832                 switch (distr) {
833                 case PART_DISTR_JIT:
834                         if (ctx->jitlevel == 1) {
835                                 if (mface->v4)
836                                         psys_uv_to_w(0.5f, 0.5f, mface->v4, pa->fuv);
837                                 else
838                                         psys_uv_to_w(1.0f / 3.0f, 1.0f / 3.0f, mface->v4, pa->fuv);
839                         }
840                         else {
841                                 ctx->jitoff[i] = fmod(ctx->jitoff[i],(float)ctx->jitlevel);
842                                 if (!isnan(ctx->jitoff[i])) {
843                                         psys_uv_to_w(ctx->jit[2*(int)ctx->jitoff[i]], ctx->jit[2*(int)ctx->jitoff[i]+1], mface->v4, pa->fuv);
844                                         ctx->jitoff[i]++;
845                                 }
846                         }
847                         break;
848                 case PART_DISTR_RAND:
849                         randu= BLI_rng_get_float(thread->rng);
850                         randv= BLI_rng_get_float(thread->rng);
851                         rng_skip_tot -= 2;
852
853                         psys_uv_to_w(randu, randv, mface->v4, pa->fuv);
854                         break;
855                 }
856                 pa->foffset= 0.0f;
857                 
858                 /* experimental */
859                 if (from==PART_FROM_VOLUME) {
860                         MVert *mvert=dm->getVertDataArray(dm,CD_MVERT);
861
862                         tot=dm->getNumTessFaces(dm);
863
864                         psys_interpolate_face(mvert,mface,0,0,pa->fuv,co1,nor,0,0,0,0);
865
866                         normalize_v3(nor);
867                         mul_v3_fl(nor,-100.0);
868
869                         add_v3_v3v3(co2,co1,nor);
870
871                         min_d=2.0;
872                         intersect=0;
873
874                         for (i=0,mface=dm->getTessFaceDataArray(dm,CD_MFACE); i<tot; i++,mface++) {
875                                 if (i==pa->num) continue;
876
877                                 v1=mvert[mface->v1].co;
878                                 v2=mvert[mface->v2].co;
879                                 v3=mvert[mface->v3].co;
880
881                                 if (isect_line_tri_v3(co1, co2, v2, v3, v1, &cur_d, 0)) {
882                                         if (cur_d<min_d) {
883                                                 min_d=cur_d;
884                                                 pa->foffset=cur_d*50.0f; /* to the middle of volume */
885                                                 intersect=1;
886                                         }
887                                 }
888                                 if (mface->v4) {
889                                         v4=mvert[mface->v4].co;
890
891                                         if (isect_line_tri_v3(co1, co2, v4, v1, v3, &cur_d, 0)) {
892                                                 if (cur_d<min_d) {
893                                                         min_d=cur_d;
894                                                         pa->foffset=cur_d*50.0f; /* to the middle of volume */
895                                                         intersect=1;
896                                                 }
897                                         }
898                                 }
899                         }
900                         if (intersect==0)
901                                 pa->foffset=0.0;
902                         else {
903                                 switch (distr) {
904                                         case PART_DISTR_JIT:
905                                                 pa->foffset *= ctx->jit[p % (2 * ctx->jitlevel)];
906                                                 break;
907                                         case PART_DISTR_RAND:
908                                                 pa->foffset *= BLI_frand();
909                                                 break;
910                                 }
911                         }
912                 }
913         }
914         else if (from == PART_FROM_CHILD) {
915                 MFace *mf;
916
917                 if (ctx->index[p] < 0) {
918                         cpa->num=0;
919                         cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3]=0.0f;
920                         cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0;
921                         return;
922                 }
923
924                 mf= dm->getTessFaceData(dm, ctx->index[p], CD_MFACE);
925
926                 randu= BLI_rng_get_float(thread->rng);
927                 randv= BLI_rng_get_float(thread->rng);
928                 rng_skip_tot -= 2;
929
930                 psys_uv_to_w(randu, randv, mf->v4, cpa->fuv);
931
932                 cpa->num = ctx->index[p];
933
934                 if (ctx->tree) {
935                         KDTreeNearest ptn[10];
936                         int w,maxw;//, do_seams;
937                         float maxd /*, mind,dd */, totw= 0.0f;
938                         int parent[10];
939                         float pweight[10];
940
941                         psys_particle_on_dm(dm,cfrom,cpa->num,DMCACHE_ISCHILD,cpa->fuv,cpa->foffset,co1,nor1,NULL,NULL,orco1,NULL);
942                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco1, 1, 1);
943                         maxw = BLI_kdtree_find_nearest_n(ctx->tree,orco1,ptn,3);
944
945                         maxd=ptn[maxw-1].dist;
946                         /* mind=ptn[0].dist; */ /* UNUSED */
947                         
948                         /* the weights here could be done better */
949                         for (w=0; w<maxw; w++) {
950                                 parent[w]=ptn[w].index;
951                                 pweight[w]=(float)pow(2.0,(double)(-6.0f*ptn[w].dist/maxd));
952                         }
953                         for (;w<10; w++) {
954                                 parent[w]=-1;
955                                 pweight[w]=0.0f;
956                         }
957
958                         for (w=0,i=0; w<maxw && i<4; w++) {
959                                 if (parent[w]>=0) {
960                                         cpa->pa[i]=parent[w];
961                                         cpa->w[i]=pweight[w];
962                                         totw+=pweight[w];
963                                         i++;
964                                 }
965                         }
966                         for (;i<4; i++) {
967                                 cpa->pa[i]=-1;
968                                 cpa->w[i]=0.0f;
969                         }
970
971                         if (totw>0.0f) for (w=0; w<4; w++)
972                                 cpa->w[w]/=totw;
973
974                         cpa->parent=cpa->pa[0];
975                 }
976         }
977
978         if (rng_skip_tot > 0) /* should never be below zero */
979                 BLI_rng_skip(thread->rng, rng_skip_tot);
980 }
981
982 static void *distribute_threads_exec_cb(void *data)
983 {
984         ParticleThread *thread= (ParticleThread*)data;
985         ParticleSystem *psys= thread->ctx->sim.psys;
986         ParticleData *pa;
987         ChildParticle *cpa;
988         int p, totpart;
989
990         if (thread->ctx->from == PART_FROM_CHILD) {
991                 totpart= psys->totchild;
992                 cpa= psys->child;
993
994                 for (p=0; p<totpart; p++, cpa++) {
995                         if (thread->ctx->skip) /* simplification skip */
996                                 BLI_rng_skip(thread->rng, PSYS_RND_DIST_SKIP * thread->ctx->skip[p]);
997
998                         if ((p+thread->num) % thread->tot == 0)
999                                 distribute_threads_exec(thread, NULL, cpa, p);
1000                         else /* thread skip */
1001                                 BLI_rng_skip(thread->rng, PSYS_RND_DIST_SKIP);
1002                 }
1003         }
1004         else {
1005                 totpart= psys->totpart;
1006                 pa= psys->particles + thread->num;
1007                 for (p=thread->num; p<totpart; p+=thread->tot, pa+=thread->tot)
1008                         distribute_threads_exec(thread, pa, NULL, p);
1009         }
1010
1011         return 0;
1012 }
1013
1014 static int distribute_compare_orig_index(void *user_data, const void *p1, const void *p2)
1015 {
1016         int *orig_index = (int *) user_data;
1017         int index1 = orig_index[*(const int *)p1];
1018         int index2 = orig_index[*(const int *)p2];
1019
1020         if (index1 < index2)
1021                 return -1;
1022         else if (index1 == index2) {
1023                 /* this pointer comparison appears to make qsort stable for glibc,
1024                  * and apparently on solaris too, makes the renders reproducible */
1025                 if (p1 < p2)
1026                         return -1;
1027                 else if (p1 == p2)
1028                         return 0;
1029                 else
1030                         return 1;
1031         }
1032         else
1033                 return 1;
1034 }
1035
1036 static void distribute_invalid(Scene *scene, ParticleSystem *psys, int from)
1037 {
1038         if (from == PART_FROM_CHILD) {
1039                 ChildParticle *cpa;
1040                 int p, totchild = get_psys_tot_child(scene, psys);
1041
1042                 if (psys->child && totchild) {
1043                         for (p=0,cpa=psys->child; p<totchild; p++,cpa++) {
1044                                 cpa->fuv[0]=cpa->fuv[1]=cpa->fuv[2]=cpa->fuv[3] = 0.0;
1045                                 cpa->foffset= 0.0f;
1046                                 cpa->parent=0;
1047                                 cpa->pa[0]=cpa->pa[1]=cpa->pa[2]=cpa->pa[3]=0;
1048                                 cpa->num= -1;
1049                         }
1050                 }
1051         }
1052         else {
1053                 PARTICLE_P;
1054                 LOOP_PARTICLES {
1055                         pa->fuv[0] = pa->fuv[1] = pa->fuv[2] = pa->fuv[3] = 0.0;
1056                         pa->foffset= 0.0f;
1057                         pa->num= -1;
1058                 }
1059         }
1060 }
1061
1062 /* Creates a distribution of coordinates on a DerivedMesh       */
1063 /* This is to denote functionality that does not yet work with mesh - only derived mesh */
1064 static int distribute_threads_init_data(ParticleThread *threads, Scene *scene, DerivedMesh *finaldm, int from)
1065 {
1066         ParticleThreadContext *ctx= threads[0].ctx;
1067         Object *ob= ctx->sim.ob;
1068         ParticleSystem *psys= ctx->sim.psys;
1069         ParticleData *pa=0, *tpars= 0;
1070         ParticleSettings *part;
1071         ParticleSeam *seams= 0;
1072         KDTree *tree=0;
1073         DerivedMesh *dm= NULL;
1074         float *jit= NULL;
1075         int i, seed, p=0, totthread= threads[0].tot;
1076         int cfrom=0;
1077         int totelem=0, totpart, *particle_element=0, children=0, totseam=0;
1078         int jitlevel= 1, distr;
1079         float *element_weight=NULL,*element_sum=NULL,*jitter_offset=NULL, *vweight=NULL;
1080         float cur, maxweight=0.0, tweight, totweight, inv_totweight, co[3], nor[3], orco[3];
1081         
1082         if (ELEM3(NULL, ob, psys, psys->part))
1083                 return 0;
1084
1085         part=psys->part;
1086         totpart=psys->totpart;
1087         if (totpart==0)
1088                 return 0;
1089
1090         if (!finaldm->deformedOnly && !finaldm->getTessFaceDataArray(finaldm, CD_ORIGINDEX)) {
1091                 printf("Can't create particles with the current modifier stack, disable destructive modifiers\n");
1092 // XXX          error("Can't paint with the current modifier stack, disable destructive modifiers");
1093                 return 0;
1094         }
1095
1096         /* First handle special cases */
1097         if (from == PART_FROM_CHILD) {
1098                 /* Simple children */
1099                 if (part->childtype != PART_CHILD_FACES) {
1100                         BLI_srandom(31415926 + psys->seed + psys->child_seed);
1101                         distribute_simple_children(scene, ob, finaldm, psys);
1102                         return 0;
1103                 }
1104         }
1105         else {
1106                 /* Grid distribution */
1107                 if (part->distr==PART_DISTR_GRID && from != PART_FROM_VERT) {
1108                         BLI_srandom(31415926 + psys->seed);
1109                         dm= CDDM_from_mesh((Mesh*)ob->data);
1110                         DM_ensure_tessface(dm);
1111                         distribute_grid(dm,psys);
1112                         dm->release(dm);
1113                         return 0;
1114                 }
1115         }
1116         
1117         /* Create trees and original coordinates if needed */
1118         if (from == PART_FROM_CHILD) {
1119                 distr=PART_DISTR_RAND;
1120                 BLI_srandom(31415926 + psys->seed + psys->child_seed);
1121                 dm= finaldm;
1122
1123                 /* BMESH ONLY */
1124                 DM_ensure_tessface(dm);
1125
1126                 children=1;
1127
1128                 tree=BLI_kdtree_new(totpart);
1129
1130                 for (p=0,pa=psys->particles; p<totpart; p++,pa++) {
1131                         psys_particle_on_dm(dm,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,nor,0,0,orco,NULL);
1132                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &orco, 1, 1);
1133                         BLI_kdtree_insert(tree, p, orco);
1134                 }
1135
1136                 BLI_kdtree_balance(tree);
1137
1138                 totpart = get_psys_tot_child(scene, psys);
1139                 cfrom = from = PART_FROM_FACE;
1140         }
1141         else {
1142                 distr = part->distr;
1143                 BLI_srandom(31415926 + psys->seed);
1144                 
1145                 if (psys->part->use_modifier_stack)
1146                         dm = finaldm;
1147                 else
1148                         dm= CDDM_from_mesh((Mesh*)ob->data);
1149
1150                 /* BMESH ONLY, for verts we don't care about tessfaces */
1151                 if (from != PART_FROM_VERT) {
1152                         DM_ensure_tessface(dm);
1153                 }
1154
1155                 /* we need orco for consistent distributions */
1156                 if (!CustomData_has_layer(&dm->vertData, CD_ORCO))
1157                         DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, BKE_mesh_orco_verts_get(ob));
1158
1159                 if (from == PART_FROM_VERT) {
1160                         MVert *mv= dm->getVertDataArray(dm, CD_MVERT);
1161                         float (*orcodata)[3] = dm->getVertDataArray(dm, CD_ORCO);
1162                         int totvert = dm->getNumVerts(dm);
1163
1164                         tree=BLI_kdtree_new(totvert);
1165
1166                         for (p=0; p<totvert; p++) {
1167                                 if (orcodata) {
1168                                         copy_v3_v3(co,orcodata[p]);
1169                                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co, 1, 1);
1170                                 }
1171                                 else
1172                                         copy_v3_v3(co,mv[p].co);
1173                                 BLI_kdtree_insert(tree, p, co);
1174                         }
1175
1176                         BLI_kdtree_balance(tree);
1177                 }
1178         }
1179
1180         /* Get total number of emission elements and allocate needed arrays */
1181         totelem = (from == PART_FROM_VERT) ? dm->getNumVerts(dm) : dm->getNumTessFaces(dm);
1182
1183         if (totelem == 0) {
1184                 distribute_invalid(scene, psys, children ? PART_FROM_CHILD : 0);
1185
1186                 if (G.debug & G_DEBUG)
1187                         fprintf(stderr,"Particle distribution error: Nothing to emit from!\n");
1188
1189                 if (dm != finaldm) dm->release(dm);
1190
1191                 BLI_kdtree_free(tree);
1192
1193                 return 0;
1194         }
1195
1196         element_weight  = MEM_callocN(sizeof(float)*totelem, "particle_distribution_weights");
1197         particle_element= MEM_callocN(sizeof(int)*totpart, "particle_distribution_indexes");
1198         element_sum             = MEM_callocN(sizeof(float)*(totelem+1), "particle_distribution_sum");
1199         jitter_offset   = MEM_callocN(sizeof(float)*totelem, "particle_distribution_jitoff");
1200
1201         /* Calculate weights from face areas */
1202         if ((part->flag&PART_EDISTR || children) && from != PART_FROM_VERT) {
1203                 MVert *v1, *v2, *v3, *v4;
1204                 float totarea=0.f, co1[3], co2[3], co3[3], co4[3];
1205                 float (*orcodata)[3];
1206                 
1207                 orcodata= dm->getVertDataArray(dm, CD_ORCO);
1208
1209                 for (i=0; i<totelem; i++) {
1210                         MFace *mf=dm->getTessFaceData(dm,i,CD_MFACE);
1211
1212                         if (orcodata) {
1213                                 copy_v3_v3(co1, orcodata[mf->v1]);
1214                                 copy_v3_v3(co2, orcodata[mf->v2]);
1215                                 copy_v3_v3(co3, orcodata[mf->v3]);
1216                                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co1, 1, 1);
1217                                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co2, 1, 1);
1218                                 BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co3, 1, 1);
1219                                 if (mf->v4) {
1220                                         copy_v3_v3(co4, orcodata[mf->v4]);
1221                                         BKE_mesh_orco_verts_transform((Mesh*)ob->data, &co4, 1, 1);
1222                                 }
1223                         }
1224                         else {
1225                                 v1= (MVert*)dm->getVertData(dm,mf->v1,CD_MVERT);
1226                                 v2= (MVert*)dm->getVertData(dm,mf->v2,CD_MVERT);
1227                                 v3= (MVert*)dm->getVertData(dm,mf->v3,CD_MVERT);
1228                                 copy_v3_v3(co1, v1->co);
1229                                 copy_v3_v3(co2, v2->co);
1230                                 copy_v3_v3(co3, v3->co);
1231                                 if (mf->v4) {
1232                                         v4= (MVert*)dm->getVertData(dm,mf->v4,CD_MVERT);
1233                                         copy_v3_v3(co4, v4->co);
1234                                 }
1235                         }
1236
1237                         cur = mf->v4 ? area_quad_v3(co1, co2, co3, co4) : area_tri_v3(co1, co2, co3);
1238                         
1239                         if (cur > maxweight)
1240                                 maxweight = cur;
1241
1242                         element_weight[i] = cur;
1243                         totarea += cur;
1244                 }
1245
1246                 for (i=0; i<totelem; i++)
1247                         element_weight[i] /= totarea;
1248
1249                 maxweight /= totarea;
1250         }
1251         else {
1252                 float min=1.0f/(float)(MIN2(totelem,totpart));
1253                 for (i=0; i<totelem; i++)
1254                         element_weight[i]=min;
1255                 maxweight=min;
1256         }
1257
1258         /* Calculate weights from vgroup */
1259         vweight = psys_cache_vgroup(dm,psys,PSYS_VG_DENSITY);
1260
1261         if (vweight) {
1262                 if (from==PART_FROM_VERT) {
1263                         for (i=0;i<totelem; i++)
1264                                 element_weight[i]*=vweight[i];
1265                 }
1266                 else { /* PART_FROM_FACE / PART_FROM_VOLUME */
1267                         for (i=0;i<totelem; i++) {
1268                                 MFace *mf=dm->getTessFaceData(dm,i,CD_MFACE);
1269                                 tweight = vweight[mf->v1] + vweight[mf->v2] + vweight[mf->v3];
1270                                 
1271                                 if (mf->v4) {
1272                                         tweight += vweight[mf->v4];
1273                                         tweight /= 4.0f;
1274                                 }
1275                                 else {
1276                                         tweight /= 3.0f;
1277                                 }
1278
1279                                 element_weight[i]*=tweight;
1280                         }
1281                 }
1282                 MEM_freeN(vweight);
1283         }
1284
1285         /* Calculate total weight of all elements */
1286         totweight= 0.0f;
1287         for (i=0;i<totelem; i++)
1288                 totweight += element_weight[i];
1289
1290         inv_totweight = (totweight > 0.f ? 1.f/totweight : 0.f);
1291
1292         /* Calculate cumulative weights */
1293         element_sum[0] = 0.0f;
1294         for (i=0; i<totelem; i++)
1295                 element_sum[i+1] = element_sum[i] + element_weight[i] * inv_totweight;
1296         
1297         /* Finally assign elements to particles */
1298         if ((part->flag&PART_TRAND) || (part->simplify_flag&PART_SIMPLIFY_ENABLE)) {
1299                 float pos;
1300
1301                 for (p=0; p<totpart; p++) {
1302                         /* In theory element_sum[totelem] should be 1.0, but due to float errors this is not necessarily always true, so scale pos accordingly. */
1303                         pos= BLI_frand() * element_sum[totelem];
1304                         particle_element[p] = distribute_binary_search(element_sum, totelem, pos);
1305                         particle_element[p] = MIN2(totelem-1, particle_element[p]);
1306                         jitter_offset[particle_element[p]] = pos;
1307                 }
1308         }
1309         else {
1310                 double step, pos;
1311                 
1312                 step= (totpart < 2) ? 0.5 : 1.0/(double)totpart;
1313                 pos= 1e-6; /* tiny offset to avoid zero weight face */
1314                 i= 0;
1315
1316                 for (p=0; p<totpart; p++, pos+=step) {
1317                         while ((i < totelem) && (pos > (double)element_sum[i + 1]))
1318                                 i++;
1319
1320                         particle_element[p] = MIN2(totelem-1, i);
1321
1322                         /* avoid zero weight face */
1323                         if (p == totpart-1 && element_weight[particle_element[p]] == 0.0f)
1324                                 particle_element[p] = particle_element[p-1];
1325
1326                         jitter_offset[particle_element[p]] = pos;
1327                 }
1328         }
1329
1330         MEM_freeN(element_sum);
1331
1332         /* For hair, sort by origindex (allows optimization's in rendering), */
1333         /* however with virtual parents the children need to be in random order. */
1334         if (part->type == PART_HAIR && !(part->childtype==PART_CHILD_FACES && part->parents!=0.0f)) {
1335                 int *orig_index = NULL;
1336
1337                 if (from == PART_FROM_VERT) {
1338                         if (dm->numVertData)
1339                                 orig_index = dm->getVertDataArray(dm, CD_ORIGINDEX);
1340                 }
1341                 else {
1342                         if (dm->numTessFaceData)
1343                                 orig_index = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
1344                 }
1345
1346                 if (orig_index) {
1347                         BLI_qsort_r(particle_element, totpart, sizeof(int), orig_index, distribute_compare_orig_index);
1348                 }
1349         }
1350
1351         /* Create jittering if needed */
1352         if (distr==PART_DISTR_JIT && ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
1353                 jitlevel= part->userjit;
1354                 
1355                 if (jitlevel == 0) {
1356                         jitlevel= totpart/totelem;
1357                         if (part->flag & PART_EDISTR) jitlevel*= 2;     /* looks better in general, not very scietific */
1358                         if (jitlevel<3) jitlevel= 3;
1359                 }
1360                 
1361                 jit= MEM_callocN((2+ jitlevel*2)*sizeof(float), "jit");
1362
1363                 /* for small amounts of particles we use regular jitter since it looks
1364                  * a bit better, for larger amounts we switch to hammersley sequence 
1365                  * because it is much faster */
1366                 if (jitlevel < 25)
1367                         init_mv_jit(jit, jitlevel, psys->seed, part->jitfac);
1368                 else
1369                         hammersley_create(jit, jitlevel+1, psys->seed, part->jitfac);
1370                 BLI_array_randomize(jit, 2*sizeof(float), jitlevel, psys->seed); /* for custom jit or even distribution */
1371         }
1372
1373         /* Setup things for threaded distribution */
1374         ctx->tree= tree;
1375         ctx->seams= seams;
1376         ctx->totseam= totseam;
1377         ctx->sim.psys= psys;
1378         ctx->index= particle_element;
1379         ctx->jit= jit;
1380         ctx->jitlevel= jitlevel;
1381         ctx->jitoff= jitter_offset;
1382         ctx->weight= element_weight;
1383         ctx->maxweight= maxweight;
1384         ctx->from= (children) ? PART_FROM_CHILD : from;
1385         ctx->cfrom= cfrom;
1386         ctx->distr= distr;
1387         ctx->dm= dm;
1388         ctx->tpars= tpars;
1389
1390         if (children) {
1391                 totpart= psys_render_simplify_distribution(ctx, totpart);
1392                 alloc_child_particles(psys, totpart);
1393         }
1394
1395         if (!children || psys->totchild < 10000)
1396                 totthread= 1;
1397         
1398         seed= 31415926 + ctx->sim.psys->seed;
1399         for (i=0; i<totthread; i++) {
1400                 threads[i].rng= BLI_rng_new(seed);
1401                 threads[i].tot= totthread;
1402         }
1403
1404         return 1;
1405 }
1406
1407 static void distribute_particles_on_dm(ParticleSimulationData *sim, int from)
1408 {
1409         DerivedMesh *finaldm = sim->psmd->dm;
1410         ListBase threads;
1411         ParticleThread *pthreads;
1412         ParticleThreadContext *ctx;
1413         int i, totthread;
1414
1415         pthreads= psys_threads_create(sim);
1416
1417         if (!distribute_threads_init_data(pthreads, sim->scene, finaldm, from)) {
1418                 psys_threads_free(pthreads);
1419                 return;
1420         }
1421
1422         totthread= pthreads[0].tot;
1423         if (totthread > 1) {
1424                 BLI_init_threads(&threads, distribute_threads_exec_cb, totthread);
1425
1426                 for (i=0; i<totthread; i++)
1427                         BLI_insert_thread(&threads, &pthreads[i]);
1428
1429                 BLI_end_threads(&threads);
1430         }
1431         else
1432                 distribute_threads_exec_cb(&pthreads[0]);
1433
1434         psys_calc_dmcache(sim->ob, finaldm, sim->psys);
1435
1436         ctx= pthreads[0].ctx;
1437         if (ctx->dm != finaldm)
1438                 ctx->dm->release(ctx->dm);
1439
1440         psys_threads_free(pthreads);
1441 }
1442
1443 /* ready for future use, to emit particles without geometry */
1444 static void distribute_particles_on_shape(ParticleSimulationData *sim, int UNUSED(from))
1445 {
1446         distribute_invalid(sim->scene, sim->psys, 0);
1447
1448         fprintf(stderr,"Shape emission not yet possible!\n");
1449 }
1450
1451 static void distribute_particles(ParticleSimulationData *sim, int from)
1452 {
1453         PARTICLE_PSMD;
1454         int distr_error=0;
1455
1456         if (psmd) {
1457                 if (psmd->dm)
1458                         distribute_particles_on_dm(sim, from);
1459                 else
1460                         distr_error=1;
1461         }
1462         else
1463                 distribute_particles_on_shape(sim, from);
1464
1465         if (distr_error) {
1466                 distribute_invalid(sim->scene, sim->psys, from);
1467
1468                 fprintf(stderr,"Particle distribution error!\n");
1469         }
1470 }
1471
1472 /* threaded child particle distribution and path caching */
1473 ParticleThread *psys_threads_create(ParticleSimulationData *sim)
1474 {
1475         ParticleThread *threads;
1476         ParticleThreadContext *ctx;
1477         int i, totthread = BKE_scene_num_threads(sim->scene);
1478         
1479         threads= MEM_callocN(sizeof(ParticleThread)*totthread, "ParticleThread");
1480         ctx= MEM_callocN(sizeof(ParticleThreadContext), "ParticleThreadContext");
1481
1482         ctx->sim = *sim;
1483         ctx->dm= ctx->sim.psmd->dm;
1484         ctx->ma= give_current_material(sim->ob, sim->psys->part->omat);
1485
1486         memset(threads, 0, sizeof(ParticleThread)*totthread);
1487
1488         for (i=0; i<totthread; i++) {
1489                 threads[i].ctx= ctx;
1490                 threads[i].num= i;
1491                 threads[i].tot= totthread;
1492         }
1493
1494         return threads;
1495 }
1496
1497 void psys_threads_free(ParticleThread *threads)
1498 {
1499         ParticleThreadContext *ctx= threads[0].ctx;
1500         int i, totthread= threads[0].tot;
1501
1502         /* path caching */
1503         if (ctx->vg_length)
1504                 MEM_freeN(ctx->vg_length);
1505         if (ctx->vg_clump)
1506                 MEM_freeN(ctx->vg_clump);
1507         if (ctx->vg_kink)
1508                 MEM_freeN(ctx->vg_kink);
1509         if (ctx->vg_rough1)
1510                 MEM_freeN(ctx->vg_rough1);
1511         if (ctx->vg_rough2)
1512                 MEM_freeN(ctx->vg_rough2);
1513         if (ctx->vg_roughe)
1514                 MEM_freeN(ctx->vg_roughe);
1515
1516         if (ctx->sim.psys->lattice_deform_data) {
1517                 end_latt_deform(ctx->sim.psys->lattice_deform_data);
1518                 ctx->sim.psys->lattice_deform_data = NULL;
1519         }
1520
1521         /* distribution */
1522         if (ctx->jit) MEM_freeN(ctx->jit);
1523         if (ctx->jitoff) MEM_freeN(ctx->jitoff);
1524         if (ctx->weight) MEM_freeN(ctx->weight);
1525         if (ctx->index) MEM_freeN(ctx->index);
1526         if (ctx->skip) MEM_freeN(ctx->skip);
1527         if (ctx->seams) MEM_freeN(ctx->seams);
1528         //if (ctx->vertpart) MEM_freeN(ctx->vertpart);
1529         BLI_kdtree_free(ctx->tree);
1530
1531         /* threads */
1532         for (i=0; i<totthread; i++) {
1533                 if (threads[i].rng)
1534                         BLI_rng_free(threads[i].rng);
1535                 if (threads[i].rng_path)
1536                         BLI_rng_free(threads[i].rng_path);
1537         }
1538
1539         MEM_freeN(ctx);
1540         MEM_freeN(threads);
1541 }
1542
1543 static void initialize_particle_texture(ParticleSimulationData *sim, ParticleData *pa, int p)
1544 {
1545         ParticleSystem *psys = sim->psys;
1546         ParticleSettings *part = psys->part;
1547         ParticleTexture ptex;
1548
1549         if (part->type != PART_FLUID) {
1550                 psys_get_texture(sim, pa, &ptex, PAMAP_INIT, 0.f);
1551
1552                 if (ptex.exist < psys_frand(psys, p+125))
1553                         pa->flag |= PARS_UNEXIST;
1554
1555                 pa->time = (part->type == PART_HAIR) ? 0.f : part->sta + (part->end - part->sta)*ptex.time;
1556         }
1557 }
1558
1559 /* set particle parameters that don't change during particle's life */
1560 void initialize_particle(ParticleData *pa)
1561 {
1562         pa->flag &= ~PARS_UNEXIST;
1563
1564         pa->hair_index = 0;
1565         /* we can't reset to -1 anymore since we've figured out correct index in distribute_particles */
1566         /* usage other than straight after distribute has to handle this index by itself - jahka*/
1567         //pa->num_dmcache = DMCACHE_NOTFOUND; /* assume we don't have a derived mesh face */
1568 }
1569 static void initialize_all_particles(ParticleSimulationData *sim)
1570 {
1571         ParticleSystem *psys = sim->psys;
1572         PARTICLE_P;
1573
1574         psys->totunexist = 0;
1575
1576         LOOP_PARTICLES {
1577                 if ((pa->flag & PARS_UNEXIST)==0)
1578                         initialize_particle(pa);
1579
1580                 if (pa->flag & PARS_UNEXIST)
1581                         psys->totunexist++;
1582         }
1583
1584         /* Free unexisting particles. */
1585         if (psys->totpart && psys->totunexist == psys->totpart) {
1586                 if (psys->particles->boid)
1587                         MEM_freeN(psys->particles->boid);
1588
1589                 MEM_freeN(psys->particles);
1590                 psys->particles = NULL;
1591                 psys->totpart = psys->totunexist = 0;
1592         }
1593
1594         if (psys->totunexist) {
1595                 int newtotpart = psys->totpart - psys->totunexist;
1596                 ParticleData *npa, *newpars;
1597                 
1598                 npa = newpars = MEM_callocN(newtotpart * sizeof(ParticleData), "particles");
1599
1600                 for (p=0, pa=psys->particles; p<newtotpart; p++, pa++, npa++) {
1601                         while (pa->flag & PARS_UNEXIST)
1602                                 pa++;
1603
1604                         memcpy(npa, pa, sizeof(ParticleData));
1605                 }
1606
1607                 if (psys->particles->boid)
1608                         MEM_freeN(psys->particles->boid);
1609                 MEM_freeN(psys->particles);
1610                 psys->particles = newpars;
1611                 psys->totpart -= psys->totunexist;
1612
1613                 if (psys->particles->boid) {
1614                         BoidParticle *newboids = MEM_callocN(psys->totpart * sizeof(BoidParticle), "boid particles");
1615
1616                         LOOP_PARTICLES
1617                                 pa->boid = newboids++;
1618
1619                 }
1620         }
1621 }
1622
1623 static void get_angular_velocity_vector(short avemode, ParticleKey *state, float vec[3])
1624 {
1625         switch (avemode) {
1626                 case PART_AVE_VELOCITY:
1627                         copy_v3_v3(vec, state->vel);
1628                         break;
1629                 case PART_AVE_HORIZONTAL:
1630                 {
1631                         float zvec[3];
1632                         zvec[0] = zvec[1] = 0;
1633                         zvec[2] = 1.f;
1634                         cross_v3_v3v3(vec, state->vel, zvec);
1635                         break;
1636                 }
1637                 case PART_AVE_VERTICAL:
1638                 {
1639                         float zvec[3], temp[3];
1640                         zvec[0] = zvec[1] = 0;
1641                         zvec[2] = 1.f;
1642                         cross_v3_v3v3(temp, state->vel, zvec);
1643                         cross_v3_v3v3(vec, temp, state->vel);
1644                         break;
1645                 }
1646                 case PART_AVE_GLOBAL_X:
1647                         vec[0] = 1.f;
1648                         vec[1] = vec[2] = 0;
1649                         break;
1650                 case PART_AVE_GLOBAL_Y:
1651                         vec[1] = 1.f;
1652                         vec[0] = vec[2] = 0;
1653                         break;
1654                 case PART_AVE_GLOBAL_Z:
1655                         vec[2] = 1.f;
1656                         vec[0] = vec[1] = 0;
1657                         break;
1658         }
1659 }
1660
1661 void psys_get_birth_coordinates(ParticleSimulationData *sim, ParticleData *pa, ParticleKey *state, float dtime, float cfra)
1662 {
1663         Object *ob = sim->ob;
1664         ParticleSystem *psys = sim->psys;
1665         ParticleSettings *part = psys->part;
1666         ParticleTexture ptex;
1667         float fac, phasefac, nor[3] = {0,0,0},loc[3],vel[3] = {0.0,0.0,0.0},rot[4],q2[4];
1668         float r_vel[3],r_ave[3],r_rot[4],vec[3],p_vel[3] = {0.0,0.0,0.0};
1669         float x_vec[3] = {1.0,0.0,0.0}, utan[3] = {0.0,1.0,0.0}, vtan[3] = {0.0,0.0,1.0}, rot_vec[3] = {0.0,0.0,0.0};
1670         float q_phase[4];
1671
1672         const bool use_boids = ((part->phystype == PART_PHYS_BOIDS) &&
1673                                 (pa->boid != NULL));
1674         const bool use_tangents = ((use_boids == false) &&
1675                                    ((part->tanfac != 0.0f) || (part->rotmode == PART_ROT_NOR_TAN)));
1676
1677         int p = pa - psys->particles;
1678
1679         /* get birth location from object               */
1680         if (use_tangents)
1681                 psys_particle_on_emitter(sim->psmd, part->from,pa->num, pa->num_dmcache, pa->fuv,pa->foffset,loc,nor,utan,vtan,0,0);
1682         else
1683                 psys_particle_on_emitter(sim->psmd, part->from,pa->num, pa->num_dmcache, pa->fuv,pa->foffset,loc,nor,0,0,0,0);
1684                 
1685         /* get possible textural influence */
1686         psys_get_texture(sim, pa, &ptex, PAMAP_IVEL, cfra);
1687
1688         /* particles live in global space so    */
1689         /* let's convert:                                               */
1690         /* -location                                                    */
1691         mul_m4_v3(ob->obmat, loc);
1692                 
1693         /* -normal                                                              */
1694         mul_mat3_m4_v3(ob->obmat, nor);
1695         normalize_v3(nor);
1696
1697         /* -tangent                                                             */
1698         if (use_tangents) {
1699                 //float phase=vg_rot?2.0f*(psys_particle_value_from_verts(sim->psmd->dm,part->from,pa,vg_rot)-0.5f):0.0f;
1700                 float phase=0.0f;
1701                 mul_v3_fl(vtan,-cosf((float)M_PI*(part->tanphase+phase)));
1702                 fac= -sinf((float)M_PI*(part->tanphase+phase));
1703                 madd_v3_v3fl(vtan, utan, fac);
1704
1705                 mul_mat3_m4_v3(ob->obmat,vtan);
1706
1707                 copy_v3_v3(utan, nor);
1708                 mul_v3_fl(utan,dot_v3v3(vtan,nor));
1709                 sub_v3_v3(vtan, utan);
1710                         
1711                 normalize_v3(vtan);
1712         }
1713                 
1714
1715         /* -velocity (boids need this even if there's no random velocity) */
1716         if (part->randfac != 0.0f || (part->phystype==PART_PHYS_BOIDS && pa->boid)) {
1717                 r_vel[0] = 2.0f * (psys_frand(psys, p + 10) - 0.5f);
1718                 r_vel[1] = 2.0f * (psys_frand(psys, p + 11) - 0.5f);
1719                 r_vel[2] = 2.0f * (psys_frand(psys, p + 12) - 0.5f);
1720
1721                 mul_mat3_m4_v3(ob->obmat, r_vel);
1722                 normalize_v3(r_vel);
1723         }
1724
1725         /* -angular velocity                                    */
1726         if (part->avemode==PART_AVE_RAND) {
1727                 r_ave[0] = 2.0f * (psys_frand(psys, p + 13) - 0.5f);
1728                 r_ave[1] = 2.0f * (psys_frand(psys, p + 14) - 0.5f);
1729                 r_ave[2] = 2.0f * (psys_frand(psys, p + 15) - 0.5f);
1730
1731                 mul_mat3_m4_v3(ob->obmat,r_ave);
1732                 normalize_v3(r_ave);
1733         }
1734                 
1735         /* -rotation                                                    */
1736         if (part->randrotfac != 0.0f) {
1737                 r_rot[0] = 2.0f * (psys_frand(psys, p + 16) - 0.5f);
1738                 r_rot[1] = 2.0f * (psys_frand(psys, p + 17) - 0.5f);
1739                 r_rot[2] = 2.0f * (psys_frand(psys, p + 18) - 0.5f);
1740                 r_rot[3] = 2.0f * (psys_frand(psys, p + 19) - 0.5f);
1741                 normalize_qt(r_rot);
1742
1743                 mat4_to_quat(rot,ob->obmat);
1744                 mul_qt_qtqt(r_rot,r_rot,rot);
1745         }
1746
1747         if (use_boids) {
1748                 float dvec[3], q[4], mat[3][3];
1749
1750                 copy_v3_v3(state->co,loc);
1751
1752                 /* boids don't get any initial velocity  */
1753                 zero_v3(state->vel);
1754
1755                 /* boids store direction in ave */
1756                 if (fabsf(nor[2])==1.0f) {
1757                         sub_v3_v3v3(state->ave, loc, ob->obmat[3]);
1758                         normalize_v3(state->ave);
1759                 }
1760                 else {
1761                         copy_v3_v3(state->ave, nor);
1762                 }
1763
1764                 /* calculate rotation matrix */
1765                 project_v3_v3v3(dvec, r_vel, state->ave);
1766                 sub_v3_v3v3(mat[0], state->ave, dvec);
1767                 normalize_v3(mat[0]);
1768                 negate_v3_v3(mat[2], r_vel);
1769                 normalize_v3(mat[2]);
1770                 cross_v3_v3v3(mat[1], mat[2], mat[0]);
1771                 
1772                 /* apply rotation */
1773                 mat3_to_quat_is_ok( q,mat);
1774                 copy_qt_qt(state->rot, q);
1775         }
1776         else {
1777                 /* conversion done so now we apply new: */
1778                 /* -velocity from:                                              */
1779
1780                 /*              *reactions                                              */
1781                 if (dtime > 0.f) {
1782                         sub_v3_v3v3(vel, pa->state.vel, pa->prev_state.vel);
1783                 }
1784
1785                 /*              *emitter velocity                               */
1786                 if (dtime != 0.f && part->obfac != 0.f) {
1787                         sub_v3_v3v3(vel, loc, state->co);
1788                         mul_v3_fl(vel, part->obfac/dtime);
1789                 }
1790                 
1791                 /*              *emitter normal                                 */
1792                 if (part->normfac != 0.f)
1793                         madd_v3_v3fl(vel, nor, part->normfac);
1794                 
1795                 /*              *emitter tangent                                */
1796                 if (sim->psmd && part->tanfac != 0.f)
1797                         madd_v3_v3fl(vel, vtan, part->tanfac);
1798
1799                 /*              *emitter object orientation             */
1800                 if (part->ob_vel[0] != 0.f) {
1801                         normalize_v3_v3(vec, ob->obmat[0]);
1802                         madd_v3_v3fl(vel, vec, part->ob_vel[0]);
1803                 }
1804                 if (part->ob_vel[1] != 0.f) {
1805                         normalize_v3_v3(vec, ob->obmat[1]);
1806                         madd_v3_v3fl(vel, vec, part->ob_vel[1]);
1807                 }
1808                 if (part->ob_vel[2] != 0.f) {
1809                         normalize_v3_v3(vec, ob->obmat[2]);
1810                         madd_v3_v3fl(vel, vec, part->ob_vel[2]);
1811                 }
1812
1813                 /*              *texture                                                */
1814                 /* TODO */
1815
1816                 /*              *random                                                 */
1817                 if (part->randfac != 0.f)
1818                         madd_v3_v3fl(vel, r_vel, part->randfac);
1819
1820                 /*              *particle                                               */
1821                 if (part->partfac != 0.f)
1822                         madd_v3_v3fl(vel, p_vel, part->partfac);
1823                 
1824                 mul_v3_v3fl(state->vel, vel, ptex.ivel);
1825
1826                 /* -location from emitter                               */
1827                 copy_v3_v3(state->co,loc);
1828
1829                 /* -rotation                                                    */
1830                 unit_qt(state->rot);
1831
1832                 if (part->rotmode) {
1833                         bool use_global_space;
1834
1835                         /* create vector into which rotation is aligned */
1836                         switch (part->rotmode) {
1837                                 case PART_ROT_NOR:
1838                                 case PART_ROT_NOR_TAN:
1839                                         copy_v3_v3(rot_vec, nor);
1840                                         use_global_space = false;
1841                                         break;
1842                                 case PART_ROT_VEL:
1843                                         copy_v3_v3(rot_vec, vel);
1844                                         use_global_space = true;
1845                                         break;
1846                                 case PART_ROT_GLOB_X:
1847                                 case PART_ROT_GLOB_Y:
1848                                 case PART_ROT_GLOB_Z:
1849                                         rot_vec[part->rotmode - PART_ROT_GLOB_X] = 1.0f;
1850                                         use_global_space = true;
1851                                         break;
1852                                 case PART_ROT_OB_X:
1853                                 case PART_ROT_OB_Y:
1854                                 case PART_ROT_OB_Z:
1855                                         copy_v3_v3(rot_vec, ob->obmat[part->rotmode - PART_ROT_OB_X]);
1856                                         use_global_space = false;
1857                                         break;
1858                                 default:
1859                                         use_global_space = true;
1860                                         break;
1861                         }
1862                         
1863                         /* create rotation quat */
1864
1865
1866                         if (use_global_space) {
1867                                 negate_v3(rot_vec);
1868                                 vec_to_quat(q2, rot_vec, OB_POSX, OB_POSZ);
1869
1870                                 /* randomize rotation quat */
1871                                 if (part->randrotfac != 0.0f) {
1872                                         interp_qt_qtqt(rot, q2, r_rot, part->randrotfac);
1873                                 }
1874                                 else {
1875                                         copy_qt_qt(rot, q2);
1876                                 }
1877                         }
1878                         else {
1879                                 /* calculate rotation in local-space */
1880                                 float q_obmat[4];
1881                                 float q_imat[4];
1882
1883                                 mat4_to_quat(q_obmat, ob->obmat);
1884                                 invert_qt_qt(q_imat, q_obmat);
1885
1886
1887                                 if (part->rotmode != PART_ROT_NOR_TAN) {
1888                                         float rot_vec_local[3];
1889
1890                                         /* rot_vec */
1891                                         negate_v3(rot_vec);
1892                                         copy_v3_v3(rot_vec_local, rot_vec);
1893                                         mul_qt_v3(q_imat, rot_vec_local);
1894                                         normalize_v3(rot_vec_local);
1895
1896                                         vec_to_quat(q2, rot_vec_local, OB_POSX, OB_POSZ);
1897                                 }
1898                                 else {
1899                                         /* (part->rotmode == PART_ROT_NOR_TAN) */
1900                                         float tmat[3][3];
1901
1902                                         /* note: utan_local is not taken from 'utan', we calculate from rot_vec/vtan */
1903                                         /* note: it looks like rotation phase may be applied twice (once with vtan, again below)
1904                                          * however this isn't the case - campbell */
1905                                         float *rot_vec_local = tmat[0];
1906                                         float *vtan_local    = tmat[1];
1907                                         float *utan_local    = tmat[2];
1908
1909                                         /* use tangents */
1910                                         BLI_assert(use_tangents == true);
1911
1912                                         /* rot_vec */
1913                                         copy_v3_v3(rot_vec_local, rot_vec);
1914                                         mul_qt_v3(q_imat, rot_vec_local);
1915
1916                                         /* vtan_local */
1917                                         copy_v3_v3(vtan_local, vtan);  /* flips, cant use */
1918                                         mul_qt_v3(q_imat, vtan_local);
1919
1920                                         /* ensure orthogonal matrix (rot_vec aligned) */
1921                                         cross_v3_v3v3(utan_local, vtan_local, rot_vec_local);
1922                                         cross_v3_v3v3(vtan_local, utan_local, rot_vec_local);
1923
1924                                         /* note: no need to normalize */
1925                                         mat3_to_quat(q2, tmat);
1926                                 }
1927
1928                                 /* randomize rotation quat */
1929                                 if (part->randrotfac != 0.0f) {
1930                                         mul_qt_qtqt(r_rot, r_rot, q_imat);
1931                                         interp_qt_qtqt(rot, q2, r_rot, part->randrotfac);
1932                                 }
1933                                 else {
1934                                         copy_qt_qt(rot, q2);
1935                                 }
1936
1937                                 mul_qt_qtqt(rot, q_obmat, rot);
1938                         }
1939
1940                         /* rotation phase */
1941                         phasefac = part->phasefac;
1942                         if (part->randphasefac != 0.0f)
1943                                 phasefac += part->randphasefac * psys_frand(psys, p + 20);
1944                         axis_angle_to_quat( q_phase,x_vec, phasefac*(float)M_PI);
1945
1946                         /* combine base rotation & phase */
1947                         mul_qt_qtqt(state->rot, rot, q_phase);
1948                 }
1949
1950                 /* -angular velocity                                    */
1951
1952                 zero_v3(state->ave);
1953
1954                 if (part->avemode) {
1955                         if (part->avemode == PART_AVE_RAND)
1956                                 copy_v3_v3(state->ave, r_ave);
1957                         else
1958                                 get_angular_velocity_vector(part->avemode, state, state->ave);
1959
1960                         normalize_v3(state->ave);
1961                         mul_v3_fl(state->ave, part->avefac);
1962                 }
1963         }
1964 }
1965
1966 /* recursively evaluate emitter parent anim at cfra */
1967 static void evaluate_emitter_anim(Scene *scene, Object *ob, float cfra)
1968 {
1969         if (ob->parent)
1970                 evaluate_emitter_anim(scene, ob->parent, cfra);
1971         
1972         /* we have to force RECALC_ANIM here since where_is_objec_time only does drivers */
1973         BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, cfra, ADT_RECALC_ANIM);
1974         BKE_object_where_is_calc_time(scene, ob, cfra);
1975 }
1976
1977 /* sets particle to the emitter surface with initial velocity & rotation */
1978 void reset_particle(ParticleSimulationData *sim, ParticleData *pa, float dtime, float cfra)
1979 {
1980         ParticleSystem *psys = sim->psys;
1981         ParticleSettings *part;
1982         ParticleTexture ptex;
1983         int p = pa - psys->particles;
1984         part=psys->part;
1985         
1986         /* get precise emitter matrix if particle is born */
1987         if (part->type!=PART_HAIR && dtime > 0.f && pa->time < cfra && pa->time >= sim->psys->cfra) {
1988                 evaluate_emitter_anim(sim->scene, sim->ob, pa->time);
1989
1990                 psys->flag |= PSYS_OB_ANIM_RESTORE;
1991         }
1992
1993         psys_get_birth_coordinates(sim, pa, &pa->state, dtime, cfra);
1994
1995         /* Initialize particle settings which depends on texture.
1996          *
1997          * We could only do it now because we'll need to know coordinate
1998          * before sampling the texture.
1999          */
2000         initialize_particle_texture(sim, pa, p);
2001
2002         if (part->phystype==PART_PHYS_BOIDS && pa->boid) {
2003                 BoidParticle *bpa = pa->boid;
2004
2005                 /* and gravity in r_ve */
2006                 bpa->gravity[0] = bpa->gravity[1] = 0.0f;
2007                 bpa->gravity[2] = -1.0f;
2008                 if ((sim->scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) &&
2009                     (sim->scene->physics_settings.gravity[2] != 0.0f))
2010                 {
2011                         bpa->gravity[2] = sim->scene->physics_settings.gravity[2];
2012                 }
2013
2014                 bpa->data.health = part->boids->health;
2015                 bpa->data.mode = eBoidMode_InAir;
2016                 bpa->data.state_id = ((BoidState*)part->boids->states.first)->id;
2017                 bpa->data.acc[0]=bpa->data.acc[1]=bpa->data.acc[2]=0.0f;
2018         }
2019
2020         if (part->type == PART_HAIR) {
2021                 pa->lifetime = 100.0f;
2022         }
2023         else {
2024                 /* initialize the lifetime, in case the texture coordinates
2025                  * are from Particles/Strands, which would cause undefined values
2026                  */
2027                 pa->lifetime = part->lifetime * (1.0f - part->randlife * psys_frand(psys, p + 21));
2028                 pa->dietime = pa->time + pa->lifetime;
2029
2030                 /* get possible textural influence */
2031                 psys_get_texture(sim, pa, &ptex, PAMAP_LIFE, cfra);
2032
2033                 pa->lifetime = part->lifetime * ptex.life;
2034
2035                 if (part->randlife != 0.0f)
2036                         pa->lifetime *= 1.0f - part->randlife * psys_frand(psys, p + 21);
2037         }
2038
2039         pa->dietime = pa->time + pa->lifetime;
2040
2041         if (sim->psys->pointcache && sim->psys->pointcache->flag & PTCACHE_BAKED &&
2042                 sim->psys->pointcache->mem_cache.first) {
2043                 float dietime = psys_get_dietime_from_cache(sim->psys->pointcache, p);
2044                 pa->dietime = MIN2(pa->dietime, dietime);
2045         }
2046
2047         if (pa->time > cfra)
2048                 pa->alive = PARS_UNBORN;
2049         else if (pa->dietime <= cfra)
2050                 pa->alive = PARS_DEAD;
2051         else
2052                 pa->alive = PARS_ALIVE;
2053
2054         pa->state.time = cfra;
2055 }
2056 static void reset_all_particles(ParticleSimulationData *sim, float dtime, float cfra, int from)
2057 {
2058         ParticleData *pa;
2059         int p, totpart=sim->psys->totpart;
2060         
2061         for (p=from, pa=sim->psys->particles+from; p<totpart; p++, pa++)
2062                 reset_particle(sim, pa, dtime, cfra);
2063 }
2064 /************************************************/
2065 /*                      Particle targets                                        */
2066 /************************************************/
2067 ParticleSystem *psys_get_target_system(Object *ob, ParticleTarget *pt)
2068 {
2069         ParticleSystem *psys = NULL;
2070
2071         if (pt->ob == NULL || pt->ob == ob)
2072                 psys = BLI_findlink(&ob->particlesystem, pt->psys-1);
2073         else
2074                 psys = BLI_findlink(&pt->ob->particlesystem, pt->psys-1);
2075
2076         if (psys)
2077                 pt->flag |= PTARGET_VALID;
2078         else
2079                 pt->flag &= ~PTARGET_VALID;
2080
2081         return psys;
2082 }
2083 /************************************************/
2084 /*                      Keyed particles                                         */
2085 /************************************************/
2086 /* Counts valid keyed targets */
2087 void psys_count_keyed_targets(ParticleSimulationData *sim)
2088 {
2089         ParticleSystem *psys = sim->psys, *kpsys;
2090         ParticleTarget *pt = psys->targets.first;
2091         int keys_valid = 1;
2092         psys->totkeyed = 0;
2093
2094         for (; pt; pt=pt->next) {
2095                 kpsys = psys_get_target_system(sim->ob, pt);
2096
2097                 if (kpsys && kpsys->totpart) {
2098                         psys->totkeyed += keys_valid;
2099                         if (psys->flag & PSYS_KEYED_TIMING && pt->duration != 0.0f)
2100                                 psys->totkeyed += 1;
2101                 }
2102                 else {
2103                         keys_valid = 0;
2104                 }
2105         }
2106
2107         psys->totkeyed *= psys->flag & PSYS_KEYED_TIMING ? 1 : psys->part->keyed_loops;
2108 }
2109
2110 static void set_keyed_keys(ParticleSimulationData *sim)
2111 {
2112         ParticleSystem *psys = sim->psys;
2113         ParticleSimulationData ksim= {0};
2114         ParticleTarget *pt;
2115         PARTICLE_P;
2116         ParticleKey *key;
2117         int totpart = psys->totpart, k, totkeys = psys->totkeyed;
2118         int keyed_flag = 0;
2119
2120         ksim.scene= sim->scene;
2121         
2122         /* no proper targets so let's clear and bail out */
2123         if (psys->totkeyed==0) {
2124                 free_keyed_keys(psys);
2125                 psys->flag &= ~PSYS_KEYED;
2126                 return;
2127         }
2128
2129         if (totpart && psys->particles->totkey != totkeys) {
2130                 free_keyed_keys(psys);
2131                 
2132                 key = MEM_callocN(totpart*totkeys*sizeof(ParticleKey), "Keyed keys");
2133                 
2134                 LOOP_PARTICLES {
2135                         pa->keys = key;
2136                         pa->totkey = totkeys;
2137                         key += totkeys;
2138                 }
2139         }
2140         
2141         psys->flag &= ~PSYS_KEYED;
2142
2143
2144         pt = psys->targets.first;
2145         for (k=0; k<totkeys; k++) {
2146                 ksim.ob = pt->ob ? pt->ob : sim->ob;
2147                 ksim.psys = BLI_findlink(&ksim.ob->particlesystem, pt->psys - 1);
2148                 keyed_flag = (ksim.psys->flag & PSYS_KEYED);
2149                 ksim.psys->flag &= ~PSYS_KEYED;
2150
2151                 LOOP_PARTICLES {
2152                         key = pa->keys + k;
2153                         key->time = -1.0; /* use current time */
2154
2155                         psys_get_particle_state(&ksim, p%ksim.psys->totpart, key, 1);
2156
2157                         if (psys->flag & PSYS_KEYED_TIMING) {
2158                                 key->time = pa->time + pt->time;
2159                                 if (pt->duration != 0.0f && k+1 < totkeys) {
2160                                         copy_particle_key(key+1, key, 1);
2161                                         (key+1)->time = pa->time + pt->time + pt->duration;
2162                                 }
2163                         }
2164                         else if (totkeys > 1)
2165                                 key->time = pa->time + (float)k / (float)(totkeys - 1) * pa->lifetime;
2166                         else
2167                                 key->time = pa->time;
2168                 }
2169
2170                 if (psys->flag & PSYS_KEYED_TIMING && pt->duration!=0.0f)
2171                         k++;
2172
2173                 ksim.psys->flag |= keyed_flag;
2174
2175                 pt = (pt->next && pt->next->flag & PTARGET_VALID) ? pt->next : psys->targets.first;
2176         }
2177
2178         psys->flag |= PSYS_KEYED;
2179 }
2180
2181 /************************************************/
2182 /*                      Point Cache                                                     */
2183 /************************************************/
2184 void psys_make_temp_pointcache(Object *ob, ParticleSystem *psys)
2185 {
2186         PointCache *cache = psys->pointcache;
2187
2188         if (cache->flag & PTCACHE_DISK_CACHE && BLI_listbase_is_empty(&cache->mem_cache)) {
2189                 PTCacheID pid;
2190                 BKE_ptcache_id_from_particles(&pid, ob, psys);
2191                 cache->flag &= ~PTCACHE_DISK_CACHE;
2192                 BKE_ptcache_disk_to_mem(&pid);
2193                 cache->flag |= PTCACHE_DISK_CACHE;
2194         }
2195 }
2196 static void psys_clear_temp_pointcache(ParticleSystem *psys)
2197 {
2198         if (psys->pointcache->flag & PTCACHE_DISK_CACHE)
2199                 BKE_ptcache_free_mem(&psys->pointcache->mem_cache);
2200 }
2201 void psys_get_pointcache_start_end(Scene *scene, ParticleSystem *psys, int *sfra, int *efra)
2202 {
2203         ParticleSettings *part = psys->part;
2204
2205         *sfra = MAX2(1, (int)part->sta);
2206         *efra = MIN2((int)(part->end + part->lifetime + 1.0f), scene->r.efra);
2207 }
2208
2209 /************************************************/
2210 /*                      Effectors                                                       */
2211 /************************************************/
2212 static void psys_update_particle_bvhtree(ParticleSystem *psys, float cfra)
2213 {
2214         if (psys) {
2215                 PARTICLE_P;
2216                 int totpart = 0;
2217                 bool need_rebuild;
2218
2219                 BLI_rw_mutex_lock(&psys_bvhtree_rwlock, THREAD_LOCK_READ);
2220                 need_rebuild = !psys->bvhtree || psys->bvhtree_frame != cfra;
2221                 BLI_rw_mutex_unlock(&psys_bvhtree_rwlock);
2222                 
2223                 if (need_rebuild) {
2224                         LOOP_SHOWN_PARTICLES {
2225                                 totpart++;
2226                         }
2227                         
2228                         BLI_rw_mutex_lock(&psys_bvhtree_rwlock, THREAD_LOCK_WRITE);
2229                         
2230                         BLI_bvhtree_free(psys->bvhtree);
2231                         psys->bvhtree = BLI_bvhtree_new(totpart, 0.0, 4, 6);
2232                         
2233                         LOOP_SHOWN_PARTICLES {
2234                                 if (pa->alive == PARS_ALIVE) {
2235                                         if (pa->state.time == cfra)
2236                                                 BLI_bvhtree_insert(psys->bvhtree, p, pa->prev_state.co, 1);
2237                                         else
2238                                                 BLI_bvhtree_insert(psys->bvhtree, p, pa->state.co, 1);
2239                                 }
2240                         }
2241                         BLI_bvhtree_balance(psys->bvhtree);
2242                         
2243                         psys->bvhtree_frame = cfra;
2244                         
2245                         BLI_rw_mutex_unlock(&psys_bvhtree_rwlock);
2246                 }
2247         }
2248 }
2249 void psys_update_particle_tree(ParticleSystem *psys, float cfra)
2250 {
2251         if (psys) {
2252                 PARTICLE_P;
2253                 int totpart = 0;
2254
2255                 if (!psys->tree || psys->tree_frame != cfra) {
2256                         LOOP_SHOWN_PARTICLES {
2257                                 totpart++;
2258                         }
2259
2260                         BLI_kdtree_free(psys->tree);
2261                         psys->tree = BLI_kdtree_new(psys->totpart);
2262
2263                         LOOP_SHOWN_PARTICLES {
2264                                 if (pa->alive == PARS_ALIVE) {
2265                                         if (pa->state.time == cfra)
2266                                                 BLI_kdtree_insert(psys->tree, p, pa->prev_state.co);
2267                                         else
2268                                                 BLI_kdtree_insert(psys->tree, p, pa->state.co);
2269                                 }
2270                         }
2271                         BLI_kdtree_balance(psys->tree);
2272
2273                         psys->tree_frame = cfra;
2274                 }
2275         }
2276 }
2277
2278 static void psys_update_effectors(ParticleSimulationData *sim)
2279 {
2280         pdEndEffectors(&sim->psys->effectors);
2281         sim->psys->effectors = pdInitEffectors(sim->scene, sim->ob, sim->psys,
2282                                                sim->psys->part->effector_weights, true);
2283         precalc_guides(sim, sim->psys->effectors);
2284 }
2285
2286 static void integrate_particle(ParticleSettings *part, ParticleData *pa, float dtime, float *external_acceleration,
2287                                void (*force_func)(void *forcedata, ParticleKey *state, float *force, float *impulse),
2288                                void *forcedata)
2289 {
2290 #define ZERO_F43 {{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}}
2291
2292         ParticleKey states[5];
2293         float force[3], acceleration[3], impulse[3], dx[4][3] = ZERO_F43, dv[4][3] = ZERO_F43, oldpos[3];
2294         float pa_mass= (part->flag & PART_SIZEMASS ? part->mass * pa->size : part->mass);
2295         int i, steps=1;
2296         int integrator = part->integrator;
2297
2298 #undef ZERO_F43
2299
2300         copy_v3_v3(oldpos, pa->state.co);
2301
2302         /* Verlet integration behaves strangely with moving emitters, so do first step with euler. */
2303         if (pa->prev_state.time < 0.f && integrator == PART_INT_VERLET)
2304                 integrator = PART_INT_EULER;
2305
2306         switch (integrator) {
2307                 case PART_INT_EULER:
2308                         steps=1;
2309                         break;
2310                 case PART_INT_MIDPOINT:
2311                         steps=2;
2312                         break;
2313                 case PART_INT_RK4:
2314                         steps=4;
2315                         break;
2316                 case PART_INT_VERLET:
2317                         steps=1;
2318                         break;
2319         }
2320
2321         for (i=0; i<steps; i++) {
2322                 copy_particle_key(states + i, &pa->state, 1);
2323         }
2324
2325         states->time = 0.f;
2326
2327         for (i=0; i<steps; i++) {
2328                 zero_v3(force);
2329                 zero_v3(impulse);
2330
2331                 force_func(forcedata, states+i, force, impulse);
2332
2333                 /* force to acceleration*/
2334                 mul_v3_v3fl(acceleration, force, 1.0f/pa_mass);
2335
2336                 if (external_acceleration)
2337                         add_v3_v3(acceleration, external_acceleration);
2338                 
2339                 /* calculate next state */
2340                 add_v3_v3(states[i].vel, impulse);
2341
2342                 switch (integrator) {
2343                         case PART_INT_EULER:
2344                                 madd_v3_v3v3fl(pa->state.co, states->co, states->vel, dtime);
2345                                 madd_v3_v3v3fl(pa->state.vel, states->vel, acceleration, dtime);
2346                                 break;
2347                         case PART_INT_MIDPOINT:
2348                                 if (i==0) {
2349                                         madd_v3_v3v3fl(states[1].co, states->co, states->vel, dtime*0.5f);
2350                                         madd_v3_v3v3fl(states[1].vel, states->vel, acceleration, dtime*0.5f);
2351                                         states[1].time = dtime*0.5f;
2352                                         /*fra=sim->psys->cfra+0.5f*dfra;*/
2353                                 }
2354                                 else {
2355                                         madd_v3_v3v3fl(pa->state.co, states->co, states[1].vel, dtime);
2356                                         madd_v3_v3v3fl(pa->state.vel, states->vel, acceleration, dtime);
2357                                 }
2358                                 break;
2359                         case PART_INT_RK4:
2360                                 switch (i) {
2361                                         case 0:
2362                                                 copy_v3_v3(dx[0], states->vel);
2363                                                 mul_v3_fl(dx[0], dtime);
2364                                                 copy_v3_v3(dv[0], acceleration);
2365                                                 mul_v3_fl(dv[0], dtime);
2366
2367                                                 madd_v3_v3v3fl(states[1].co, states->co, dx[0], 0.5f);
2368                                                 madd_v3_v3v3fl(states[1].vel, states->vel, dv[0], 0.5f);
2369                                                 states[1].time = dtime*0.5f;
2370                                                 /*fra=sim->psys->cfra+0.5f*dfra;*/
2371                                                 break;
2372                                         case 1:
2373                                                 madd_v3_v3v3fl(dx[1], states->vel, dv[0], 0.5f);
2374                                                 mul_v3_fl(dx[1], dtime);
2375                                                 copy_v3_v3(dv[1], acceleration);
2376                                                 mul_v3_fl(dv[1], dtime);
2377
2378                                                 madd_v3_v3v3fl(states[2].co, states->co, dx[1], 0.5f);
2379                                                 madd_v3_v3v3fl(states[2].vel, states->vel, dv[1], 0.5f);
2380                                                 states[2].time = dtime*0.5f;
2381                                                 break;
2382                                         case 2:
2383                                                 madd_v3_v3v3fl(dx[2], states->vel, dv[1], 0.5f);
2384                                                 mul_v3_fl(dx[2], dtime);
2385                                                 copy_v3_v3(dv[2], acceleration);
2386                                                 mul_v3_fl(dv[2], dtime);
2387
2388                                                 add_v3_v3v3(states[3].co, states->co, dx[2]);
2389                                                 add_v3_v3v3(states[3].vel, states->vel, dv[2]);
2390                                                 states[3].time = dtime;
2391                                                 /*fra=cfra;*/
2392                                                 break;
2393                                         case 3:
2394                                                 add_v3_v3v3(dx[3], states->vel, dv[2]);
2395                                                 mul_v3_fl(dx[3], dtime);
2396                                                 copy_v3_v3(dv[3], acceleration);
2397                                                 mul_v3_fl(dv[3], dtime);
2398
2399                                                 madd_v3_v3v3fl(pa->state.co, states->co, dx[0], 1.0f/6.0f);
2400                                                 madd_v3_v3fl(pa->state.co, dx[1], 1.0f/3.0f);
2401                                                 madd_v3_v3fl(pa->state.co, dx[2], 1.0f/3.0f);
2402                                                 madd_v3_v3fl(pa->state.co, dx[3], 1.0f/6.0f);
2403
2404                                                 madd_v3_v3v3fl(pa->state.vel, states->vel, dv[0], 1.0f/6.0f);
2405                                                 madd_v3_v3fl(pa->state.vel, dv[1], 1.0f/3.0f);
2406                                                 madd_v3_v3fl(pa->state.vel, dv[2], 1.0f/3.0f);
2407                                                 madd_v3_v3fl(pa->state.vel, dv[3], 1.0f/6.0f);
2408                                 }
2409                                 break;
2410                         case PART_INT_VERLET:   /* Verlet integration */
2411                                 madd_v3_v3v3fl(pa->state.vel, pa->prev_state.vel, acceleration, dtime);
2412                                 madd_v3_v3v3fl(pa->state.co, pa->prev_state.co, pa->state.vel, dtime);
2413
2414                                 sub_v3_v3v3(pa->state.vel, pa->state.co, oldpos);
2415                                 mul_v3_fl(pa->state.vel, 1.0f/dtime);
2416                                 break;
2417                 }
2418         }
2419 }
2420
2421 /*********************************************************************************************************
2422  *                    SPH fluid physics 
2423  *
2424  * In theory, there could be unlimited implementation of SPH simulators
2425  *
2426  * This code uses in some parts adapted algorithms from the pseudo code as outlined in the Research paper:
2427  *
2428  * Titled: Particle-based Viscoelastic Fluid Simulation.
2429  * Authors: Simon Clavet, Philippe Beaudoin and Pierre Poulin
2430  * Website: http://www.iro.umontreal.ca/labs/infographie/papers/Clavet-2005-PVFS/
2431  *
2432  * Presented at Siggraph, (2005)
2433  *
2434  * ********************************************************************************************************/
2435 #define PSYS_FLUID_SPRINGS_INITIAL_SIZE 256
2436 static ParticleSpring *sph_spring_add(ParticleSystem *psys, ParticleSpring *spring)
2437 {
2438         /* Are more refs required? */
2439         if (psys->alloc_fluidsprings == 0 || psys->fluid_springs == NULL) {
2440                 psys->alloc_fluidsprings = PSYS_FLUID_SPRINGS_INITIAL_SIZE;
2441                 psys->fluid_springs = (ParticleSpring*)MEM_callocN(psys->alloc_fluidsprings * sizeof(ParticleSpring), "Particle Fluid Springs");
2442         }
2443         else if (psys->tot_fluidsprings == psys->alloc_fluidsprings) {
2444                 /* Double the number of refs allocated */
2445                 psys->alloc_fluidsprings *= 2;
2446                 psys->fluid_springs = (ParticleSpring*)MEM_reallocN(psys->fluid_springs, psys->alloc_fluidsprings * sizeof(ParticleSpring));
2447         }
2448
2449         memcpy(psys->fluid_springs + psys->tot_fluidsprings, spring, sizeof(ParticleSpring));
2450         psys->tot_fluidsprings++;
2451
2452         return psys->fluid_springs + psys->tot_fluidsprings - 1;
2453 }
2454 static void sph_spring_delete(ParticleSystem *psys, int j)
2455 {
2456         if (j != psys->tot_fluidsprings - 1)
2457                 psys->fluid_springs[j] = psys->fluid_springs[psys->tot_fluidsprings - 1];
2458
2459         psys->tot_fluidsprings--;
2460
2461         if (psys->tot_fluidsprings < psys->alloc_fluidsprings/2 && psys->alloc_fluidsprings > PSYS_FLUID_SPRINGS_INITIAL_SIZE) {
2462                 psys->alloc_fluidsprings /= 2;
2463                 psys->fluid_springs = (ParticleSpring*)MEM_reallocN(psys->fluid_springs,  psys->alloc_fluidsprings * sizeof(ParticleSpring));
2464         }
2465 }
2466 static void sph_springs_modify(ParticleSystem *psys, float dtime)
2467 {
2468         SPHFluidSettings *fluid = psys->part->fluid;
2469         ParticleData *pa1, *pa2;
2470         ParticleSpring *spring = psys->fluid_springs;
2471         
2472         float h, d, Rij[3], rij, Lij;
2473         int i;
2474
2475         float yield_ratio = fluid->yield_ratio;
2476         float plasticity = fluid->plasticity_constant;
2477         /* scale things according to dtime */
2478         float timefix = 25.f * dtime;
2479
2480         if ((fluid->flag & SPH_VISCOELASTIC_SPRINGS)==0 || fluid->spring_k == 0.f)
2481                 return;
2482
2483         /* Loop through the springs */
2484         for (i=0; i<psys->tot_fluidsprings; i++, spring++) {
2485                 pa1 = psys->particles + spring->particle_index[0];
2486                 pa2 = psys->particles + spring->particle_index[1];
2487
2488                 sub_v3_v3v3(Rij, pa2->prev_state.co, pa1->prev_state.co);
2489                 rij = normalize_v3(Rij);
2490
2491                 /* adjust rest length */
2492                 Lij = spring->rest_length;
2493                 d = yield_ratio * timefix * Lij;
2494
2495                 if (rij > Lij + d) // Stretch
2496                         spring->rest_length += plasticity * (rij - Lij - d) * timefix;
2497                 else if (rij < Lij - d) // Compress
2498                         spring->rest_length -= plasticity * (Lij - d - rij) * timefix;
2499
2500                 h = 4.f*pa1->size;
2501
2502                 if (spring->rest_length > h)
2503                         spring->delete_flag = 1;
2504         }
2505
2506         /* Loop through springs backwaqrds - for efficient delete function */
2507         for (i=psys->tot_fluidsprings-1; i >= 0; i--) {
2508                 if (psys->fluid_springs[i].delete_flag)
2509                         sph_spring_delete(psys, i);
2510         }
2511 }
2512 static EdgeHash *sph_springhash_build(ParticleSystem *psys)
2513 {
2514         EdgeHash *springhash = NULL;
2515         ParticleSpring *spring;
2516         int i = 0;
2517
2518         springhash = BLI_edgehash_new_ex(__func__, psys->tot_fluidsprings);
2519
2520         for (i=0, spring=psys->fluid_springs; i<psys->tot_fluidsprings; i++, spring++)
2521                 BLI_edgehash_insert(springhash, spring->particle_index[0], spring->particle_index[1], SET_INT_IN_POINTER(i+1));
2522
2523         return springhash;
2524 }
2525
2526 #define SPH_NEIGHBORS 512
2527 typedef struct SPHNeighbor {
2528         ParticleSystem *psys;
2529         int index;
2530 } SPHNeighbor;
2531
2532 typedef struct SPHRangeData {
2533         SPHNeighbor neighbors[SPH_NEIGHBORS];
2534         int tot_neighbors;
2535
2536         float* data;
2537
2538         ParticleSystem *npsys;
2539         ParticleData *pa;
2540
2541         float h;
2542         float mass;
2543         float massfac;
2544         int use_size;
2545 } SPHRangeData;
2546
2547 static void sph_evaluate_func(BVHTree *tree, ParticleSystem **psys, float co[3], SPHRangeData *pfr, float interaction_radius, BVHTree_RangeQuery callback)
2548 {
2549         int i;
2550
2551         pfr->tot_neighbors = 0;
2552
2553         for (i=0; i < 10 && psys[i]; i++) {
2554                 pfr->npsys    = psys[i];
2555                 pfr->massfac  = psys[i]->part->mass / pfr->mass;
2556                 pfr->use_size = psys[i]->part->flag & PART_SIZEMASS;
2557
2558                 if (tree) {
2559                         BLI_bvhtree_range_query(tree, co, interaction_radius, callback, pfr);
2560                         break;
2561                 }
2562                 else {
2563                         BLI_rw_mutex_lock(&psys_bvhtree_rwlock, THREAD_LOCK_READ);
2564                         
2565                         BLI_bvhtree_range_query(psys[i]->bvhtree, co, interaction_radius, callback, pfr);
2566                         
2567                         BLI_rw_mutex_unlock(&psys_bvhtree_rwlock);
2568                 }
2569         }
2570 }
2571 static void sph_density_accum_cb(void *userdata, int index, float squared_dist)
2572 {
2573         SPHRangeData *pfr = (SPHRangeData *)userdata;
2574         ParticleData *npa = pfr->npsys->particles + index;
2575         float q;
2576         float dist;
2577
2578         if (npa == pfr->pa || squared_dist < FLT_EPSILON)
2579                 return;
2580
2581         /* Ugh! One particle has too many neighbors! If some aren't taken into
2582          * account, the forces will be biased by the tree search order. This
2583          * effectively adds enery to the system, and results in a churning motion.
2584          * But, we have to stop somewhere, and it's not the end of the world.
2585          *  - jahka and z0r
2586          */
2587         if (pfr->tot_neighbors >= SPH_NEIGHBORS)
2588                 return;
2589
2590         pfr->neighbors[pfr->tot_neighbors].index = index;
2591         pfr->neighbors[pfr->tot_neighbors].psys = pfr->npsys;
2592         pfr->tot_neighbors++;
2593
2594         dist = sqrtf(squared_dist);
2595         q = (1.f - dist/pfr->h) * pfr->massfac;
2596
2597         if (pfr->use_size)
2598                 q *= npa->size;
2599
2600         pfr->data[0] += q*q;
2601         pfr->data[1] += q*q*q;
2602 }
2603
2604 /*
2605  * Find the Courant number for an SPH particle (used for adaptive time step).
2606  */
2607 static void sph_particle_courant(SPHData *sphdata, SPHRangeData *pfr)
2608 {
2609         ParticleData *pa, *npa;
2610         int i;
2611         float flow[3], offset[3], dist;
2612
2613         zero_v3(flow);
2614
2615         dist = 0.0f;
2616         if (pfr->tot_neighbors > 0) {
2617                 pa = pfr->pa;
2618                 for (i=0; i < pfr->tot_neighbors; i++) {
2619                         npa = pfr->neighbors[i].psys->particles + pfr->neighbors[i].index;
2620                         sub_v3_v3v3(offset, pa->prev_state.co, npa->prev_state.co);
2621                         dist += len_v3(offset);
2622                         add_v3_v3(flow, npa->prev_state.vel);
2623                 }
2624                 dist += sphdata->psys[0]->part->fluid->radius; // TODO: remove this? - z0r
2625                 sphdata->element_size = dist / pfr->tot_neighbors;
2626                 mul_v3_v3fl(sphdata->flow, flow, 1.0f / pfr->tot_neighbors);
2627         }
2628         else {
2629                 sphdata->element_size = MAXFLOAT;
2630                 copy_v3_v3(sphdata->flow, flow);
2631         }
2632 }
2633 static void sph_force_cb(void *sphdata_v, ParticleKey *state, float *force, float *UNUSED(impulse))
2634 {
2635         SPHData *sphdata = (SPHData *)sphdata_v;
2636         ParticleSystem **psys = sphdata->psys;
2637         ParticleData *pa = sphdata->pa;
2638         SPHFluidSettings *fluid = psys[0]->part->fluid;
2639         ParticleSpring *spring = NULL;
2640         SPHRangeData pfr;
2641         SPHNeighbor *pfn;
2642         float *gravity = sphdata->gravity;
2643         EdgeHash *springhash = sphdata->eh;
2644
2645         float q, u, rij, dv[3];
2646         float pressure, near_pressure;
2647
2648         float visc = fluid->viscosity_omega;
2649         float stiff_visc = fluid->viscosity_beta * (fluid->flag & SPH_FAC_VISCOSITY ? fluid->viscosity_omega : 1.f);
2650
2651         float inv_mass = 1.0f / sphdata->mass;
2652         float spring_constant = fluid->spring_k;
2653
2654         /* 4.0 seems to be a pretty good value */
2655         float interaction_radius = fluid->radius * (fluid->flag & SPH_FAC_RADIUS ? 4.0f * pa->size : 1.0f);
2656         float h = interaction_radius * sphdata->hfac;
2657         float rest_density = fluid->rest_density * (fluid->flag & SPH_FAC_DENSITY ? 4.77f : 1.f); /* 4.77 is an experimentally determined density factor */
2658         float rest_length = fluid->rest_length * (fluid->flag & SPH_FAC_REST_LENGTH ? 2.588f * pa->size : 1.f);
2659
2660         float stiffness = fluid->stiffness_k;
2661         float stiffness_near_fac = fluid->stiffness_knear * (fluid->flag & SPH_FAC_REPULSION ? fluid->stiffness_k : 1.f);
2662
2663         ParticleData *npa;
2664         float vec[3];
2665         float vel[3];
2666         float co[3];
2667         float data[2];
2668         float density, near_density;
2669
2670         int i, spring_index, index = pa - psys[0]->particles;
2671
2672         data[0] = data[1] = 0;
2673         pfr.data = data;
2674         pfr.h = h;
2675         pfr.pa = pa;
2676         pfr.mass = sphdata->mass;
2677
2678         sph_evaluate_func( NULL, psys, state->co, &pfr, interaction_radius, sph_density_accum_cb);
2679
2680         density = data[0];
2681         near_density = data[1];
2682
2683         pressure =  stiffness * (density - rest_density);
2684         near_pressure = stiffness_near_fac * near_density;
2685
2686         pfn = pfr.neighbors;
2687         for (i=0; i<pfr.tot_neighbors; i++, pfn++) {
2688                 npa = pfn->psys->particles + pfn->index;
2689
2690                 madd_v3_v3v3fl(co, npa->prev_state.co, npa->prev_state.vel, state->time);
2691
2692                 sub_v3_v3v3(vec, co, state->co);
2693                 rij = normalize_v3(vec);
2694
2695                 q = (1.f - rij/h) * pfn->psys->part->mass * inv_mass;
2696
2697                 if (pfn->psys->part->flag & PART_SIZEMASS)
2698                         q *= npa->size;
2699
2700                 copy_v3_v3(vel, npa->prev_state.vel);
2701
2702                 /* Double Density Relaxation */
2703                 madd_v3_v3fl(force, vec, -(pressure + near_pressure*q)*q);
2704
2705                 /* Viscosity */
2706                 if (visc > 0.f  || stiff_visc > 0.f) {
2707                         sub_v3_v3v3(dv, vel, state->vel);
2708                         u = dot_v3v3(vec, dv);
2709
2710                         if (u < 0.f && visc > 0.f)
2711                                 madd_v3_v3fl(force, vec, 0.5f * q * visc * u );
2712
2713                         if (u > 0.f && stiff_visc > 0.f)
2714                                 madd_v3_v3fl(force, vec, 0.5f * q * stiff_visc * u );
2715                 }
2716
2717                 if (spring_constant > 0.f) {
2718                         /* Viscoelastic spring force */
2719                         if (pfn->psys == psys[0] && fluid->flag & SPH_VISCOELASTIC_SPRINGS && springhash) {
2720                                 /* BLI_edgehash_lookup appears to be thread-safe. - z0r */
2721                                 spring_index = GET_INT_FROM_POINTER(BLI_edgehash_lookup(springhash, index, pfn->index));
2722
2723                                 if (spring_index) {
2724                                         spring = psys[0]->fluid_springs + spring_index - 1;
2725
2726                                         madd_v3_v3fl(force, vec, -10.f * spring_constant * (1.f - rij/h) * (spring->rest_length - rij));
2727                                 }
2728                                 else if (fluid->spring_frames == 0 || (pa->prev_state.time-pa->time) <= fluid->spring_frames) {
2729                                         ParticleSpring temp_spring;
2730                                         temp_spring.particle_index[0] = index;
2731                                         temp_spring.particle_index[1] = pfn->index;
2732                                         temp_spring.rest_length = (fluid->flag & SPH_CURRENT_REST_LENGTH) ? rij : rest_length;
2733                                         temp_spring.delete_flag = 0;
2734
2735                                         /* sph_spring_add is not thread-safe. - z0r */
2736 #pragma omp critical
2737                                         sph_spring_add(psys[0], &temp_spring);
2738                                 }
2739                         }
2740                         else {/* PART_SPRING_HOOKES - Hooke's spring force */
2741                                 madd_v3_v3fl(force, vec, -10.f * spring_constant * (1.f - rij/h) * (rest_length - rij));
2742                         }
2743                 }
2744         }
2745         
2746         /* Artificial buoyancy force in negative gravity direction  */
2747         if (fluid->buoyancy > 0.f && gravity)
2748                 madd_v3_v3fl(force, gravity, fluid->buoyancy * (density-rest_density));
2749
2750         if (sphdata->pass == 0 && psys[0]->part->time_flag & PART_TIME_AUTOSF)
2751                 sph_particle_courant(sphdata, &pfr);
2752         sphdata->pass++;
2753 }
2754
2755 /* powf is really slow for raising to integer powers. */
2756 MINLINE float pow2(float x)
2757 {
2758         return x * x;
2759 }
2760 MINLINE float pow3(float x)
2761 {
2762         return pow2(x) * x;
2763 }
2764 MINLINE float pow4(float x)
2765 {
2766         return pow2(pow2(x));
2767 }
2768 MINLINE float pow7(float x)
2769 {
2770         return pow2(pow3(x)) * x;
2771 }
2772
2773 static void sphclassical_density_accum_cb(void *userdata, int index, float UNUSED(squared_dist))
2774 {
2775         SPHRangeData *pfr = (SPHRangeData *)userdata;
2776         ParticleData *npa = pfr->npsys->particles + index;
2777         float q;
2778         float qfac = 21.0f / (256.f * (float)M_PI);
2779         float rij, rij_h;
2780         float vec[3];
2781
2782         /* Exclude particles that are more than 2h away. Can't use squared_dist here
2783          * because it is not accurate enough. Use current state, i.e. the output of
2784          * basic_integrate() - z0r */
2785         sub_v3_v3v3(vec, npa->state.co, pfr->pa->state.co);
2786         rij = len_v3(vec);
2787         rij_h = rij / pfr->h;
2788         if (rij_h > 2.0f)
2789                 return;
2790
2791         /* Smoothing factor. Utilise the Wendland kernel. gnuplot:
2792          *     q1(x) = (2.0 - x)**4 * ( 1.0 + 2.0 * x)
2793          *     plot [0:2] q1(x) */
2794         q  = qfac / pow3(pfr->h) * pow4(2.0f - rij_h) * ( 1.0f + 2.0f * rij_h);
2795         q *= pfr->npsys->part->mass;
2796
2797         if (pfr->use_size)
2798                 q *= pfr->pa->size;
2799
2800         pfr->data[0] += q;
2801         pfr->data[1] += q / npa->sphdensity;
2802 }
2803
2804 static void sphclassical_neighbour_accum_cb(void *userdata, int index, float UNUSED(squared_dist))
2805 {
2806         SPHRangeData *pfr = (SPHRangeData *)userdata;
2807         ParticleData *npa = pfr->npsys->particles + index;
2808         float rij, rij_h;
2809         float vec[3];
2810
2811         if (pfr->tot_neighbors >= SPH_NEIGHBORS)
2812                 return;
2813
2814         /* Exclude particles that are more than 2h away. Can't use squared_dist here
2815          * because it is not accurate enough. Use current state, i.e. the output of
2816          * basic_integrate() - z0r */
2817         sub_v3_v3v3(vec, npa->state.co, pfr->pa->state.co);
2818         rij = len_v3(vec);
2819         rij_h = rij / pfr->h;
2820         if (rij_h > 2.0f)
2821                 return;
2822
2823         pfr->neighbors[pfr->tot_neighbors].index = index;
2824         pfr->neighbors[pfr->tot_neighbors].psys = pfr->npsys;
2825         pfr->tot_neighbors++;
2826 }
2827 static void sphclassical_force_cb(void *sphdata_v, ParticleKey *state, float *force, float *UNUSED(impulse))
2828 {
2829         SPHData *sphdata = (SPHData *)sphdata_v;
2830         ParticleSystem **psys = sphdata->psys;
2831         ParticleData *pa = sphdata->pa;
2832         SPHFluidSettings *fluid = psys[0]->part->fluid;
2833         SPHRangeData pfr;
2834         SPHNeighbor *pfn;
2835         float *gravity = sphdata->gravity;
2836
2837         float dq, u, rij, dv[3];
2838         float pressure, npressure;
2839
2840         float visc = fluid->viscosity_omega;
2841
2842         float interaction_radius;
2843         float h, hinv;
2844         /* 4.77 is an experimentally determined density factor */
2845         float rest_density = fluid->rest_density * (fluid->flag & SPH_FAC_DENSITY ? 4.77f : 1.0f);
2846
2847         // Use speed of sound squared
2848         float stiffness = pow2(fluid->stiffness_k);
2849
2850         ParticleData *npa;
2851         float vec[3];
2852         float co[3];
2853         float pressureTerm;
2854
2855         int i;
2856
2857         float qfac2 = 42.0f / (256.0f * (float)M_PI);
2858         float rij_h;
2859
2860         /* 4.0 here is to be consistent with previous formulation/interface */
2861         interaction_radius = fluid->radius * (fluid->flag & SPH_FAC_RADIUS ? 4.0f * pa->size : 1.0f);
2862         h = interaction_radius * sphdata->hfac;
2863         hinv = 1.0f / h;
2864
2865         pfr.h = h;
2866         pfr.pa = pa;
2867
2868         sph_evaluate_func(NULL, psys, state->co, &pfr, interaction_radius, sphclassical_neighbour_accum_cb);
2869         pressure =  stiffness * (pow7(pa->sphdensity / rest_density) - 1.0f);
2870
2871         /* multiply by mass so that we return a force, not accel */
2872         qfac2 *= sphdata->mass / pow3(pfr.h);
2873
2874         pfn = pfr.neighbors;
2875         for (i = 0; i < pfr.tot_neighbors; i++, pfn++) {
2876                 npa = pfn->psys->particles + pfn->index;
2877                 if (npa == pa) {
2878                         /* we do not contribute to ourselves */
2879                         continue;
2880                 }
2881
2882                 /* Find vector to neighbor. Exclude particles that are more than 2h
2883                  * away. Can't use current state here because it may have changed on
2884                  * another thread - so do own mini integration. Unlike basic_integrate,
2885                  * SPH integration depends on neighboring particles. - z0r */
2886                 madd_v3_v3v3fl(co, npa->prev_state.co, npa->prev_state.vel, state->time);
2887                 sub_v3_v3v3(vec, co, state->co);
2888                 rij = normalize_v3(vec);
2889                 rij_h = rij / pfr.h;
2890                 if (rij_h > 2.0f)
2891                         continue;
2892
2893                 npressure = stiffness * (pow7(npa->sphdensity / rest_density) - 1.0f);
2894
2895                 /* First derivative of smoothing factor. Utilise the Wendland kernel.
2896                  * gnuplot:
2897                  *     q2(x) = 2.0 * (2.0 - x)**4 - 4.0 * (2.0 - x)**3 * (1.0 + 2.0 * x)
2898                  *     plot [0:2] q2(x)
2899                  * Particles > 2h away are excluded above. */
2900                 dq = qfac2 * (2.0f * pow4(2.0f - rij_h) - 4.0f * pow3(2.0f - rij_h) * (1.0f + 2.0f * rij_h)  );
2901
2902                 if (pfn->psys->part->flag & PART_SIZEMASS)
2903                         dq *= npa->size;
2904
2905                 pressureTerm = pressure / pow2(pa->sphdensity) + npressure / pow2(npa->sphdensity);
2906
2907                 /* Note that 'minus' is removed, because vec = vecBA, not vecAB.
2908                  * Thi