Target calculation for local non-straight rest shapes.
[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 ELEM(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 && origindex_final < totelem) {
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 = axis_dominant_v3_single(delta);
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)sqrtf((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(const void *p1, const void *p2, void *user_data)
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 (ELEM(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), distribute_compare_orig_index, 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         psys_get_texture(sim, pa, &ptex, PAMAP_INIT, 0.f);
1550         
1551         switch (part->type) {
1552         case PART_EMITTER:
1553                 if (ptex.exist < psys_frand(psys, p+125))
1554                         pa->flag |= PARS_UNEXIST;
1555                 pa->time = part->sta + (part->end - part->sta)*ptex.time;
1556                 break;
1557         case PART_HAIR:
1558                 if (ptex.exist < psys_frand(psys, p+125))
1559                         pa->flag |= PARS_UNEXIST;
1560                 pa->time = 0.f;
1561                 break;
1562         case PART_FLUID:
1563                 break;
1564         }
1565 }
1566
1567 /* set particle parameters that don't change during particle's life */
1568 void initialize_particle(ParticleSimulationData *sim, ParticleData *pa)
1569 {
1570         ParticleSettings *part = sim->psys->part;
1571         float birth_time = (float)(pa - sim->psys->particles) / (float)sim->psys->totpart;
1572         
1573         pa->flag &= ~PARS_UNEXIST;
1574         pa->time = part->sta + (part->end - part->sta) * birth_time;
1575
1576         pa->hair_index = 0;
1577         /* we can't reset to -1 anymore since we've figured out correct index in distribute_particles */
1578         /* usage other than straight after distribute has to handle this index by itself - jahka*/
1579         //pa->num_dmcache = DMCACHE_NOTFOUND; /* assume we don't have a derived mesh face */
1580 }
1581 static void initialize_all_particles(ParticleSimulationData *sim)
1582 {
1583         ParticleSystem *psys = sim->psys;
1584         PARTICLE_P;
1585
1586         psys->totunexist = 0;
1587
1588         LOOP_PARTICLES {
1589                 if ((pa->flag & PARS_UNEXIST)==0)
1590                         initialize_particle(sim, pa);
1591
1592                 if (pa->flag & PARS_UNEXIST)
1593                         psys->totunexist++;
1594         }
1595
1596         /* Free unexisting particles. */
1597         if (psys->totpart && psys->totunexist == psys->totpart) {
1598                 if (psys->particles->boid)
1599                         MEM_freeN(psys->particles->boid);
1600
1601                 MEM_freeN(psys->particles);
1602                 psys->particles = NULL;
1603                 psys->totpart = psys->totunexist = 0;
1604         }
1605
1606         if (psys->totunexist) {
1607                 int newtotpart = psys->totpart - psys->totunexist;
1608                 ParticleData *npa, *newpars;
1609                 
1610                 npa = newpars = MEM_callocN(newtotpart * sizeof(ParticleData), "particles");
1611
1612                 for (p=0, pa=psys->particles; p<newtotpart; p++, pa++, npa++) {
1613                         while (pa->flag & PARS_UNEXIST)
1614                                 pa++;
1615
1616                         memcpy(npa, pa, sizeof(ParticleData));
1617                 }
1618
1619                 if (psys->particles->boid)
1620                         MEM_freeN(psys->particles->boid);
1621                 MEM_freeN(psys->particles);
1622                 psys->particles = newpars;
1623                 psys->totpart -= psys->totunexist;
1624
1625                 if (psys->particles->boid) {
1626                         BoidParticle *newboids = MEM_callocN(psys->totpart * sizeof(BoidParticle), "boid particles");
1627
1628                         LOOP_PARTICLES
1629                                 pa->boid = newboids++;
1630
1631                 }
1632         }
1633 }
1634
1635 static void get_angular_velocity_vector(short avemode, ParticleKey *state, float vec[3])
1636 {
1637         switch (avemode) {
1638                 case PART_AVE_VELOCITY:
1639                         copy_v3_v3(vec, state->vel);
1640                         break;
1641                 case PART_AVE_HORIZONTAL:
1642                 {
1643                         float zvec[3];
1644                         zvec[0] = zvec[1] = 0;
1645                         zvec[2] = 1.f;
1646                         cross_v3_v3v3(vec, state->vel, zvec);
1647                         break;
1648                 }
1649                 case PART_AVE_VERTICAL:
1650                 {
1651                         float zvec[3], temp[3];
1652                         zvec[0] = zvec[1] = 0;
1653                         zvec[2] = 1.f;
1654                         cross_v3_v3v3(temp, state->vel, zvec);
1655                         cross_v3_v3v3(vec, temp, state->vel);
1656                         break;
1657                 }
1658                 case PART_AVE_GLOBAL_X:
1659                         vec[0] = 1.f;
1660                         vec[1] = vec[2] = 0;
1661                         break;
1662                 case PART_AVE_GLOBAL_Y:
1663                         vec[1] = 1.f;
1664                         vec[0] = vec[2] = 0;
1665                         break;
1666                 case PART_AVE_GLOBAL_Z:
1667                         vec[2] = 1.f;
1668                         vec[0] = vec[1] = 0;
1669                         break;
1670         }
1671 }
1672
1673 void psys_get_birth_coords(ParticleSimulationData *sim, ParticleData *pa, ParticleKey *state, float dtime, float cfra)
1674 {
1675         Object *ob = sim->ob;
1676         ParticleSystem *psys = sim->psys;
1677         ParticleSettings *part = psys->part;
1678         ParticleTexture ptex;
1679         float fac, phasefac, nor[3] = {0,0,0},loc[3],vel[3] = {0.0,0.0,0.0},rot[4],q2[4];
1680         float r_vel[3],r_ave[3],r_rot[4],vec[3],p_vel[3] = {0.0,0.0,0.0};
1681         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};
1682         float q_phase[4];
1683
1684         const bool use_boids = ((part->phystype == PART_PHYS_BOIDS) &&
1685                                 (pa->boid != NULL));
1686         const bool use_tangents = ((use_boids == false) &&
1687                                    ((part->tanfac != 0.0f) || (part->rotmode == PART_ROT_NOR_TAN)));
1688
1689         int p = pa - psys->particles;
1690
1691         /* get birth location from object               */
1692         if (use_tangents)
1693                 psys_particle_on_emitter(sim->psmd, part->from,pa->num, pa->num_dmcache, pa->fuv,pa->foffset,loc,nor,utan,vtan,0,0);
1694         else
1695                 psys_particle_on_emitter(sim->psmd, part->from,pa->num, pa->num_dmcache, pa->fuv,pa->foffset,loc,nor,0,0,0,0);
1696                 
1697         /* get possible textural influence */
1698         psys_get_texture(sim, pa, &ptex, PAMAP_IVEL, cfra);
1699
1700         /* particles live in global space so    */
1701         /* let's convert:                                               */
1702         /* -location                                                    */
1703         mul_m4_v3(ob->obmat, loc);
1704                 
1705         /* -normal                                                              */
1706         mul_mat3_m4_v3(ob->obmat, nor);
1707         normalize_v3(nor);
1708
1709         /* -tangent                                                             */
1710         if (use_tangents) {
1711                 //float phase=vg_rot?2.0f*(psys_particle_value_from_verts(sim->psmd->dm,part->from,pa,vg_rot)-0.5f):0.0f;
1712                 float phase=0.0f;
1713                 mul_v3_fl(vtan,-cosf((float)M_PI*(part->tanphase+phase)));
1714                 fac= -sinf((float)M_PI*(part->tanphase+phase));
1715                 madd_v3_v3fl(vtan, utan, fac);
1716
1717                 mul_mat3_m4_v3(ob->obmat,vtan);
1718
1719                 copy_v3_v3(utan, nor);
1720                 mul_v3_fl(utan,dot_v3v3(vtan,nor));
1721                 sub_v3_v3(vtan, utan);
1722                         
1723                 normalize_v3(vtan);
1724         }
1725                 
1726
1727         /* -velocity (boids need this even if there's no random velocity) */
1728         if (part->randfac != 0.0f || (part->phystype==PART_PHYS_BOIDS && pa->boid)) {
1729                 r_vel[0] = 2.0f * (psys_frand(psys, p + 10) - 0.5f);
1730                 r_vel[1] = 2.0f * (psys_frand(psys, p + 11) - 0.5f);
1731                 r_vel[2] = 2.0f * (psys_frand(psys, p + 12) - 0.5f);
1732
1733                 mul_mat3_m4_v3(ob->obmat, r_vel);
1734                 normalize_v3(r_vel);
1735         }
1736
1737         /* -angular velocity                                    */
1738         if (part->avemode==PART_AVE_RAND) {
1739                 r_ave[0] = 2.0f * (psys_frand(psys, p + 13) - 0.5f);
1740                 r_ave[1] = 2.0f * (psys_frand(psys, p + 14) - 0.5f);
1741                 r_ave[2] = 2.0f * (psys_frand(psys, p + 15) - 0.5f);
1742
1743                 mul_mat3_m4_v3(ob->obmat,r_ave);
1744                 normalize_v3(r_ave);
1745         }
1746                 
1747         /* -rotation                                                    */
1748         if (part->randrotfac != 0.0f) {
1749                 r_rot[0] = 2.0f * (psys_frand(psys, p + 16) - 0.5f);
1750                 r_rot[1] = 2.0f * (psys_frand(psys, p + 17) - 0.5f);
1751                 r_rot[2] = 2.0f * (psys_frand(psys, p + 18) - 0.5f);
1752                 r_rot[3] = 2.0f * (psys_frand(psys, p + 19) - 0.5f);
1753                 normalize_qt(r_rot);
1754
1755                 mat4_to_quat(rot,ob->obmat);
1756                 mul_qt_qtqt(r_rot,r_rot,rot);
1757         }
1758
1759         if (use_boids) {
1760                 float dvec[3], q[4], mat[3][3];
1761
1762                 copy_v3_v3(state->co,loc);
1763
1764                 /* boids don't get any initial velocity  */
1765                 zero_v3(state->vel);
1766
1767                 /* boids store direction in ave */
1768                 if (fabsf(nor[2])==1.0f) {
1769                         sub_v3_v3v3(state->ave, loc, ob->obmat[3]);
1770                         normalize_v3(state->ave);
1771                 }
1772                 else {
1773                         copy_v3_v3(state->ave, nor);
1774                 }
1775
1776                 /* calculate rotation matrix */
1777                 project_v3_v3v3(dvec, r_vel, state->ave);
1778                 sub_v3_v3v3(mat[0], state->ave, dvec);
1779                 normalize_v3(mat[0]);
1780                 negate_v3_v3(mat[2], r_vel);
1781                 normalize_v3(mat[2]);
1782                 cross_v3_v3v3(mat[1], mat[2], mat[0]);
1783                 
1784                 /* apply rotation */
1785                 mat3_to_quat_is_ok( q,mat);
1786                 copy_qt_qt(state->rot, q);
1787         }
1788         else {
1789                 /* conversion done so now we apply new: */
1790                 /* -velocity from:                                              */
1791
1792                 /*              *reactions                                              */
1793                 if (dtime > 0.f) {
1794                         sub_v3_v3v3(vel, pa->state.vel, pa->prev_state.vel);
1795                 }
1796
1797                 /*              *emitter velocity                               */
1798                 if (dtime != 0.f && part->obfac != 0.f) {
1799                         sub_v3_v3v3(vel, loc, state->co);
1800                         mul_v3_fl(vel, part->obfac/dtime);
1801                 }
1802                 
1803                 /*              *emitter normal                                 */
1804                 if (part->normfac != 0.f)
1805                         madd_v3_v3fl(vel, nor, part->normfac);
1806                 
1807                 /*              *emitter tangent                                */
1808                 if (sim->psmd && part->tanfac != 0.f)
1809                         madd_v3_v3fl(vel, vtan, part->tanfac);
1810
1811                 /*              *emitter object orientation             */
1812                 if (part->ob_vel[0] != 0.f) {
1813                         normalize_v3_v3(vec, ob->obmat[0]);
1814                         madd_v3_v3fl(vel, vec, part->ob_vel[0]);
1815                 }
1816                 if (part->ob_vel[1] != 0.f) {
1817                         normalize_v3_v3(vec, ob->obmat[1]);
1818                         madd_v3_v3fl(vel, vec, part->ob_vel[1]);
1819                 }
1820                 if (part->ob_vel[2] != 0.f) {
1821                         normalize_v3_v3(vec, ob->obmat[2]);
1822                         madd_v3_v3fl(vel, vec, part->ob_vel[2]);
1823                 }
1824
1825                 /*              *texture                                                */
1826                 /* TODO */
1827
1828                 /*              *random                                                 */
1829                 if (part->randfac != 0.f)
1830                         madd_v3_v3fl(vel, r_vel, part->randfac);
1831
1832                 /*              *particle                                               */
1833                 if (part->partfac != 0.f)
1834                         madd_v3_v3fl(vel, p_vel, part->partfac);
1835                 
1836                 mul_v3_v3fl(state->vel, vel, ptex.ivel);
1837
1838                 /* -location from emitter                               */
1839                 copy_v3_v3(state->co,loc);
1840
1841                 /* -rotation                                                    */
1842                 unit_qt(state->rot);
1843
1844                 if (part->rotmode) {
1845                         bool use_global_space;
1846
1847                         /* create vector into which rotation is aligned */
1848                         switch (part->rotmode) {
1849                                 case PART_ROT_NOR:
1850                                 case PART_ROT_NOR_TAN:
1851                                         copy_v3_v3(rot_vec, nor);
1852                                         use_global_space = false;
1853                                         break;
1854                                 case PART_ROT_VEL:
1855                                         copy_v3_v3(rot_vec, vel);
1856                                         use_global_space = true;
1857                                         break;
1858                                 case PART_ROT_GLOB_X:
1859                                 case PART_ROT_GLOB_Y:
1860                                 case PART_ROT_GLOB_Z:
1861                                         rot_vec[part->rotmode - PART_ROT_GLOB_X] = 1.0f;
1862                                         use_global_space = true;
1863                                         break;
1864                                 case PART_ROT_OB_X:
1865                                 case PART_ROT_OB_Y:
1866                                 case PART_ROT_OB_Z:
1867                                         copy_v3_v3(rot_vec, ob->obmat[part->rotmode - PART_ROT_OB_X]);
1868                                         use_global_space = false;
1869                                         break;
1870                                 default:
1871                                         use_global_space = true;
1872                                         break;
1873                         }
1874                         
1875                         /* create rotation quat */
1876
1877
1878                         if (use_global_space) {
1879                                 negate_v3(rot_vec);
1880                                 vec_to_quat(q2, rot_vec, OB_POSX, OB_POSZ);
1881
1882                                 /* randomize rotation quat */
1883                                 if (part->randrotfac != 0.0f) {
1884                                         interp_qt_qtqt(rot, q2, r_rot, part->randrotfac);
1885                                 }
1886                                 else {
1887                                         copy_qt_qt(rot, q2);
1888                                 }
1889                         }
1890                         else {
1891                                 /* calculate rotation in local-space */
1892                                 float q_obmat[4];
1893                                 float q_imat[4];
1894
1895                                 mat4_to_quat(q_obmat, ob->obmat);
1896                                 invert_qt_qt(q_imat, q_obmat);
1897
1898
1899                                 if (part->rotmode != PART_ROT_NOR_TAN) {
1900                                         float rot_vec_local[3];
1901
1902                                         /* rot_vec */
1903                                         negate_v3(rot_vec);
1904                                         copy_v3_v3(rot_vec_local, rot_vec);
1905                                         mul_qt_v3(q_imat, rot_vec_local);
1906                                         normalize_v3(rot_vec_local);
1907
1908                                         vec_to_quat(q2, rot_vec_local, OB_POSX, OB_POSZ);
1909                                 }
1910                                 else {
1911                                         /* (part->rotmode == PART_ROT_NOR_TAN) */
1912                                         float tmat[3][3];
1913
1914                                         /* note: utan_local is not taken from 'utan', we calculate from rot_vec/vtan */
1915                                         /* note: it looks like rotation phase may be applied twice (once with vtan, again below)
1916                                          * however this isn't the case - campbell */
1917                                         float *rot_vec_local = tmat[0];
1918                                         float *vtan_local    = tmat[1];
1919                                         float *utan_local    = tmat[2];
1920
1921                                         /* use tangents */
1922                                         BLI_assert(use_tangents == true);
1923
1924                                         /* rot_vec */
1925                                         copy_v3_v3(rot_vec_local, rot_vec);
1926                                         mul_qt_v3(q_imat, rot_vec_local);
1927
1928                                         /* vtan_local */
1929                                         copy_v3_v3(vtan_local, vtan);  /* flips, cant use */
1930                                         mul_qt_v3(q_imat, vtan_local);
1931
1932                                         /* ensure orthogonal matrix (rot_vec aligned) */
1933                                         cross_v3_v3v3(utan_local, vtan_local, rot_vec_local);
1934                                         cross_v3_v3v3(vtan_local, utan_local, rot_vec_local);
1935
1936                                         /* note: no need to normalize */
1937                                         mat3_to_quat(q2, tmat);
1938                                 }
1939
1940                                 /* randomize rotation quat */
1941                                 if (part->randrotfac != 0.0f) {
1942                                         mul_qt_qtqt(r_rot, r_rot, q_imat);
1943                                         interp_qt_qtqt(rot, q2, r_rot, part->randrotfac);
1944                                 }
1945                                 else {
1946                                         copy_qt_qt(rot, q2);
1947                                 }
1948
1949                                 mul_qt_qtqt(rot, q_obmat, rot);
1950                         }
1951
1952                         /* rotation phase */
1953                         phasefac = part->phasefac;
1954                         if (part->randphasefac != 0.0f)
1955                                 phasefac += part->randphasefac * psys_frand(psys, p + 20);
1956                         axis_angle_to_quat( q_phase,x_vec, phasefac*(float)M_PI);
1957
1958                         /* combine base rotation & phase */
1959                         mul_qt_qtqt(state->rot, rot, q_phase);
1960                 }
1961
1962                 /* -angular velocity                                    */
1963
1964                 zero_v3(state->ave);
1965
1966                 if (part->avemode) {
1967                         if (part->avemode == PART_AVE_RAND)
1968                                 copy_v3_v3(state->ave, r_ave);
1969                         else
1970                                 get_angular_velocity_vector(part->avemode, state, state->ave);
1971
1972                         normalize_v3(state->ave);
1973                         mul_v3_fl(state->ave, part->avefac);
1974                 }
1975         }
1976 }
1977
1978 /* recursively evaluate emitter parent anim at cfra */
1979 static void evaluate_emitter_anim(Scene *scene, Object *ob, float cfra)
1980 {
1981         if (ob->parent)
1982                 evaluate_emitter_anim(scene, ob->parent, cfra);
1983         
1984         /* we have to force RECALC_ANIM here since where_is_objec_time only does drivers */
1985         BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, cfra, ADT_RECALC_ANIM);
1986         BKE_object_where_is_calc_time(scene, ob, cfra);
1987 }
1988
1989 /* sets particle to the emitter surface with initial velocity & rotation */
1990 void reset_particle(ParticleSimulationData *sim, ParticleData *pa, float dtime, float cfra)
1991 {
1992         ParticleSystem *psys = sim->psys;
1993         ParticleSettings *part;
1994         ParticleTexture ptex;
1995         int p = pa - psys->particles;
1996         part=psys->part;
1997         
1998         /* get precise emitter matrix if particle is born */
1999         if (part->type!=PART_HAIR && dtime > 0.f && pa->time < cfra && pa->time >= sim->psys->cfra) {
2000                 evaluate_emitter_anim(sim->scene, sim->ob, pa->time);
2001
2002                 psys->flag |= PSYS_OB_ANIM_RESTORE;
2003         }
2004
2005         psys_get_birth_coords(sim, pa, &pa->state, dtime, cfra);
2006
2007         /* Initialize particle settings which depends on texture.
2008          *
2009          * We could only do it now because we'll need to know coordinate
2010          * before sampling the texture.
2011          */
2012         initialize_particle_texture(sim, pa, p);
2013
2014         if (part->phystype==PART_PHYS_BOIDS && pa->boid) {
2015                 BoidParticle *bpa = pa->boid;
2016
2017                 /* and gravity in r_ve */
2018                 bpa->gravity[0] = bpa->gravity[1] = 0.0f;
2019                 bpa->gravity[2] = -1.0f;
2020                 if ((sim->scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) &&
2021                     (sim->scene->physics_settings.gravity[2] != 0.0f))
2022                 {
2023                         bpa->gravity[2] = sim->scene->physics_settings.gravity[2];
2024                 }
2025
2026                 bpa->data.health = part->boids->health;
2027                 bpa->data.mode = eBoidMode_InAir;
2028                 bpa->data.state_id = ((BoidState*)part->boids->states.first)->id;
2029                 bpa->data.acc[0]=bpa->data.acc[1]=bpa->data.acc[2]=0.0f;
2030         }
2031
2032         if (part->type == PART_HAIR) {
2033                 pa->lifetime = 100.0f;
2034         }
2035         else {
2036                 /* initialize the lifetime, in case the texture coordinates
2037                  * are from Particles/Strands, which would cause undefined values
2038                  */
2039                 pa->lifetime = part->lifetime * (1.0f - part->randlife * psys_frand(psys, p + 21));
2040                 pa->dietime = pa->time + pa->lifetime;
2041
2042                 /* get possible textural influence */
2043                 psys_get_texture(sim, pa, &ptex, PAMAP_LIFE, cfra);
2044
2045                 pa->lifetime = part->lifetime * ptex.life;
2046
2047                 if (part->randlife != 0.0f)
2048                         pa->lifetime *= 1.0f - part->randlife * psys_frand(psys, p + 21);
2049         }
2050
2051         pa->dietime = pa->time + pa->lifetime;
2052
2053         if (sim->psys->pointcache && sim->psys->pointcache->flag & PTCACHE_BAKED &&
2054                 sim->psys->pointcache->mem_cache.first) {
2055                 float dietime = psys_get_dietime_from_cache(sim->psys->pointcache, p);
2056                 pa->dietime = MIN2(pa->dietime, dietime);
2057         }
2058
2059         if (pa->time > cfra)
2060                 pa->alive = PARS_UNBORN;
2061         else if (pa->dietime <= cfra)
2062                 pa->alive = PARS_DEAD;
2063         else
2064                 pa->alive = PARS_ALIVE;
2065
2066         pa->state.time = cfra;
2067 }
2068 static void reset_all_particles(ParticleSimulationData *sim, float dtime, float cfra, int from)
2069 {
2070         ParticleData *pa;
2071         int p, totpart=sim->psys->totpart;
2072         
2073         for (p=from, pa=sim->psys->particles+from; p<totpart; p++, pa++)
2074                 reset_particle(sim, pa, dtime, cfra);
2075 }
2076 /************************************************/
2077 /*                      Particle targets                                        */
2078 /************************************************/
2079 ParticleSystem *psys_get_target_system(Object *ob, ParticleTarget *pt)
2080 {
2081         ParticleSystem *psys = NULL;
2082
2083         if (pt->ob == NULL || pt->ob == ob)
2084                 psys = BLI_findlink(&ob->particlesystem, pt->psys-1);
2085         else
2086                 psys = BLI_findlink(&pt->ob->particlesystem, pt->psys-1);
2087
2088         if (psys)
2089                 pt->flag |= PTARGET_VALID;
2090         else
2091                 pt->flag &= ~PTARGET_VALID;
2092
2093         return psys;
2094 }
2095 /************************************************/
2096 /*                      Keyed particles                                         */
2097 /************************************************/
2098 /* Counts valid keyed targets */
2099 void psys_count_keyed_targets(ParticleSimulationData *sim)
2100 {
2101         ParticleSystem *psys = sim->psys, *kpsys;
2102         ParticleTarget *pt = psys->targets.first;
2103         int keys_valid = 1;
2104         psys->totkeyed = 0;
2105
2106         for (; pt; pt=pt->next) {
2107                 kpsys = psys_get_target_system(sim->ob, pt);
2108
2109                 if (kpsys && kpsys->totpart) {
2110                         psys->totkeyed += keys_valid;
2111                         if (psys->flag & PSYS_KEYED_TIMING && pt->duration != 0.0f)
2112                                 psys->totkeyed += 1;
2113                 }
2114                 else {
2115                         keys_valid = 0;
2116                 }
2117         }
2118
2119         psys->totkeyed *= psys->flag & PSYS_KEYED_TIMING ? 1 : psys->part->keyed_loops;
2120 }
2121
2122 static void set_keyed_keys(ParticleSimulationData *sim)
2123 {
2124         ParticleSystem *psys = sim->psys;
2125         ParticleSimulationData ksim= {0};
2126         ParticleTarget *pt;
2127         PARTICLE_P;
2128         ParticleKey *key;
2129         int totpart = psys->totpart, k, totkeys = psys->totkeyed;
2130         int keyed_flag = 0;
2131
2132         ksim.scene= sim->scene;
2133         
2134         /* no proper targets so let's clear and bail out */
2135         if (psys->totkeyed==0) {
2136                 free_keyed_keys(psys);
2137                 psys->flag &= ~PSYS_KEYED;
2138                 return;
2139         }
2140
2141         if (totpart && psys->particles->totkey != totkeys) {
2142                 free_keyed_keys(psys);
2143                 
2144                 key = MEM_callocN(totpart*totkeys*sizeof(ParticleKey), "Keyed keys");
2145                 
2146                 LOOP_PARTICLES {
2147                         pa->keys = key;
2148                         pa->totkey = totkeys;
2149                         key += totkeys;
2150                 }
2151         }
2152         
2153         psys->flag &= ~PSYS_KEYED;
2154
2155
2156         pt = psys->targets.first;
2157         for (k=0; k<totkeys; k++) {
2158                 ksim.ob = pt->ob ? pt->ob : sim->ob;
2159                 ksim.psys = BLI_findlink(&ksim.ob->particlesystem, pt->psys - 1);
2160                 keyed_flag = (ksim.psys->flag & PSYS_KEYED);
2161                 ksim.psys->flag &= ~PSYS_KEYED;
2162
2163                 LOOP_PARTICLES {
2164                         key = pa->keys + k;
2165                         key->time = -1.0; /* use current time */
2166
2167                         psys_get_particle_state(&ksim, p%ksim.psys->totpart, key, 1);
2168
2169                         if (psys->flag & PSYS_KEYED_TIMING) {
2170                                 key->time = pa->time + pt->time;
2171                                 if (pt->duration != 0.0f && k+1 < totkeys) {
2172                                         copy_particle_key(key+1, key, 1);
2173                                         (key+1)->time = pa->time + pt->time + pt->duration;
2174                                 }
2175                         }
2176                         else if (totkeys > 1)
2177                                 key->time = pa->time + (float)k / (float)(totkeys - 1) * pa->lifetime;
2178                         else
2179                                 key->time = pa->time;
2180                 }
2181
2182                 if (psys->flag & PSYS_KEYED_TIMING && pt->duration!=0.0f)
2183                         k++;
2184
2185                 ksim.psys->flag |= keyed_flag;
2186
2187                 pt = (pt->next && pt->next->flag & PTARGET_VALID) ? pt->next : psys->targets.first;
2188         }
2189
2190         psys->flag |= PSYS_KEYED;
2191 }
2192
2193 /************************************************/
2194 /*                      Point Cache                                                     */
2195 /************************************************/
2196 void psys_make_temp_pointcache(Object *ob, ParticleSystem *psys)
2197 {
2198         PointCache *cache = psys->pointcache;
2199
2200         if (cache->flag & PTCACHE_DISK_CACHE && BLI_listbase_is_empty(&cache->mem_cache)) {
2201                 PTCacheID pid;
2202                 BKE_ptcache_id_from_particles(&pid, ob, psys);
2203                 cache->flag &= ~PTCACHE_DISK_CACHE;
2204                 BKE_ptcache_disk_to_mem(&pid);
2205                 cache->flag |= PTCACHE_DISK_CACHE;
2206         }
2207 }
2208 static void psys_clear_temp_pointcache(ParticleSystem *psys)
2209 {
2210         if (psys->pointcache->flag & PTCACHE_DISK_CACHE)
2211                 BKE_ptcache_free_mem(&psys->pointcache->mem_cache);
2212 }
2213 void psys_get_pointcache_start_end(Scene *scene, ParticleSystem *psys, int *sfra, int *efra)
2214 {
2215         ParticleSettings *part = psys->part;
2216
2217         *sfra = MAX2(1, (int)part->sta);
2218         *efra = MIN2((int)(part->end + part->lifetime + 1.0f), scene->r.efra);
2219 }
2220
2221 /************************************************/
2222 /*                      Effectors                                                       */
2223 /************************************************/
2224 static void psys_update_particle_bvhtree(ParticleSystem *psys, float cfra)
2225 {
2226         if (psys) {
2227                 PARTICLE_P;
2228                 int totpart = 0;
2229                 bool need_rebuild;
2230
2231                 BLI_rw_mutex_lock(&psys_bvhtree_rwlock, THREAD_LOCK_READ);
2232                 need_rebuild = !psys->bvhtree || psys->bvhtree_frame != cfra;
2233                 BLI_rw_mutex_unlock(&psys_bvhtree_rwlock);
2234                 
2235                 if (need_rebuild) {
2236                         LOOP_SHOWN_PARTICLES {
2237                                 totpart++;
2238                         }
2239                         
2240                         BLI_rw_mutex_lock(&psys_bvhtree_rwlock, THREAD_LOCK_WRITE);
2241                         
2242                         BLI_bvhtree_free(psys->bvhtree);
2243                         psys->bvhtree = BLI_bvhtree_new(totpart, 0.0, 4, 6);
2244                         
2245                         LOOP_SHOWN_PARTICLES {
2246                                 if (pa->alive == PARS_ALIVE) {
2247                                         if (pa->state.time == cfra)
2248                                                 BLI_bvhtree_insert(psys->bvhtree, p, pa->prev_state.co, 1);
2249                                         else
2250                                                 BLI_bvhtree_insert(psys->bvhtree, p, pa->state.co, 1);
2251                                 }
2252                         }
2253                         BLI_bvhtree_balance(psys->bvhtree);
2254                         
2255                         psys->bvhtree_frame = cfra;
2256                         
2257                         BLI_rw_mutex_unlock(&psys_bvhtree_rwlock);
2258                 }
2259         }
2260 }
2261 void psys_update_particle_tree(ParticleSystem *psys, float cfra)
2262 {
2263         if (psys) {
2264                 PARTICLE_P;
2265                 int totpart = 0;
2266
2267                 if (!psys->tree || psys->tree_frame != cfra) {
2268                         LOOP_SHOWN_PARTICLES {
2269                                 totpart++;
2270                         }
2271
2272                         BLI_kdtree_free(psys->tree);
2273                         psys->tree = BLI_kdtree_new(psys->totpart);
2274
2275                         LOOP_SHOWN_PARTICLES {
2276                                 if (pa->alive == PARS_ALIVE) {
2277                                         if (pa->state.time == cfra)
2278                                                 BLI_kdtree_insert(psys->tree, p, pa->prev_state.co);
2279                                         else
2280                                                 BLI_kdtree_insert(psys->tree, p, pa->state.co);
2281                                 }
2282                         }
2283                         BLI_kdtree_balance(psys->tree);
2284
2285                         psys->tree_frame = cfra;
2286                 }
2287         }
2288 }
2289
2290 static void psys_update_effectors(ParticleSimulationData *sim)
2291 {
2292         pdEndEffectors(&sim->psys->effectors);
2293         sim->psys->effectors = pdInitEffectors(sim->scene, sim->ob, sim->psys,
2294                                                sim->psys->part->effector_weights, true);
2295         precalc_guides(sim, sim->psys->effectors);
2296 }
2297
2298 static void integrate_particle(ParticleSettings *part, ParticleData *pa, float dtime, float *external_acceleration,
2299                                void (*force_func)(void *forcedata, ParticleKey *state, float *force, float *impulse),
2300                                void *forcedata)
2301 {
2302 #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}}
2303
2304         ParticleKey states[5];
2305         float force[3], acceleration[3], impulse[3], dx[4][3] = ZERO_F43, dv[4][3] = ZERO_F43, oldpos[3];
2306         float pa_mass= (part->flag & PART_SIZEMASS ? part->mass * pa->size : part->mass);
2307         int i, steps=1;
2308         int integrator = part->integrator;
2309
2310 #undef ZERO_F43
2311
2312         copy_v3_v3(oldpos, pa->state.co);
2313
2314         /* Verlet integration behaves strangely with moving emitters, so do first step with euler. */
2315         if (pa->prev_state.time < 0.f && integrator == PART_INT_VERLET)
2316                 integrator = PART_INT_EULER;
2317
2318         switch (integrator) {
2319                 case PART_INT_EULER:
2320                         steps=1;
2321                         break;
2322                 case PART_INT_MIDPOINT:
2323                         steps=2;
2324                         break;
2325                 case PART_INT_RK4:
2326                         steps=4;
2327                         break;
2328                 case PART_INT_VERLET:
2329                         steps=1;
2330                         break;
2331         }
2332
2333         for (i=0; i<steps; i++) {
2334                 copy_particle_key(states + i, &pa->state, 1);
2335         }
2336
2337         states->time = 0.f;
2338
2339         for (i=0; i<steps; i++) {
2340                 zero_v3(force);
2341                 zero_v3(impulse);
2342
2343                 force_func(forcedata, states+i, force, impulse);
2344
2345                 /* force to acceleration*/
2346                 mul_v3_v3fl(acceleration, force, 1.0f/pa_mass);
2347
2348                 if (external_acceleration)
2349                         add_v3_v3(acceleration, external_acceleration);
2350                 
2351                 /* calculate next state */
2352                 add_v3_v3(states[i].vel, impulse);
2353
2354                 switch (integrator) {
2355                         case PART_INT_EULER:
2356                                 madd_v3_v3v3fl(pa->state.co, states->co, states->vel, dtime);
2357                                 madd_v3_v3v3fl(pa->state.vel, states->vel, acceleration, dtime);
2358                                 break;
2359                         case PART_INT_MIDPOINT:
2360                                 if (i==0) {
2361                                         madd_v3_v3v3fl(states[1].co, states->co, states->vel, dtime*0.5f);
2362                                         madd_v3_v3v3fl(states[1].vel, states->vel, acceleration, dtime*0.5f);
2363                                         states[1].time = dtime*0.5f;
2364                                         /*fra=sim->psys->cfra+0.5f*dfra;*/
2365                                 }
2366                                 else {
2367                                         madd_v3_v3v3fl(pa->state.co, states->co, states[1].vel, dtime);
2368                                         madd_v3_v3v3fl(pa->state.vel, states->vel, acceleration, dtime);
2369                                 }
2370                                 break;
2371                         case PART_INT_RK4:
2372                                 switch (i) {
2373                                         case 0:
2374                                                 copy_v3_v3(dx[0], states->vel);
2375                                                 mul_v3_fl(dx[0], dtime);
2376                                                 copy_v3_v3(dv[0], acceleration);
2377                                                 mul_v3_fl(dv[0], dtime);
2378
2379                                                 madd_v3_v3v3fl(states[1].co, states->co, dx[0], 0.5f);
2380                                                 madd_v3_v3v3fl(states[1].vel, states->vel, dv[0], 0.5f);
2381                                                 states[1].time = dtime*0.5f;
2382                                                 /*fra=sim->psys->cfra+0.5f*dfra;*/
2383                                                 break;
2384                                         case 1:
2385                                                 madd_v3_v3v3fl(dx[1], states->vel, dv[0], 0.5f);
2386                                                 mul_v3_fl(dx[1], dtime);
2387                                                 copy_v3_v3(dv[1], acceleration);
2388                                                 mul_v3_fl(dv[1], dtime);
2389
2390                                                 madd_v3_v3v3fl(states[2].co, states->co, dx[1], 0.5f);
2391                                                 madd_v3_v3v3fl(states[2].vel, states->vel, dv[1], 0.5f);
2392                                                 states[2].time = dtime*0.5f;
2393                                                 break;
2394                                         case 2:
2395                                                 madd_v3_v3v3fl(dx[2], states->vel, dv[1], 0.5f);
2396                                                 mul_v3_fl(dx[2], dtime);
2397                                                 copy_v3_v3(dv[2], acceleration);
2398                                                 mul_v3_fl(dv[2], dtime);
2399
2400                                                 add_v3_v3v3(states[3].co, states->co, dx[2]);
2401                                                 add_v3_v3v3(states[3].vel, states->vel, dv[2]);
2402                                                 states[3].time = dtime;
2403                                                 /*fra=cfra;*/
2404                                                 break;
2405                                         case 3:
2406                                                 add_v3_v3v3(dx[3], states->vel, dv[2]);
2407                                                 mul_v3_fl(dx[3], dtime);
2408                                                 copy_v3_v3(dv[3], acceleration);
2409                                                 mul_v3_fl(dv[3], dtime);
2410
2411                                                 madd_v3_v3v3fl(pa->state.co, states->co, dx[0], 1.0f/6.0f);
2412                                                 madd_v3_v3fl(pa->state.co, dx[1], 1.0f/3.0f);
2413                                                 madd_v3_v3fl(pa->state.co, dx[2], 1.0f/3.0f);
2414                                                 madd_v3_v3fl(pa->state.co, dx[3], 1.0f/6.0f);
2415
2416                                                 madd_v3_v3v3fl(pa->state.vel, states->vel, dv[0], 1.0f/6.0f);
2417                                                 madd_v3_v3fl(pa->state.vel, dv[1], 1.0f/3.0f);
2418                                                 madd_v3_v3fl(pa->state.vel, dv[2], 1.0f/3.0f);
2419                                                 madd_v3_v3fl(pa->state.vel, dv[3], 1.0f/6.0f);
2420                                 }
2421                                 break;
2422                         case PART_INT_VERLET:   /* Verlet integration */
2423                                 madd_v3_v3v3fl(pa->state.vel, pa->prev_state.vel, acceleration, dtime);
2424                                 madd_v3_v3v3fl(pa->state.co, pa->prev_state.co, pa->state.vel, dtime);
2425
2426                                 sub_v3_v3v3(pa->state.vel, pa->state.co, oldpos);
2427                                 mul_v3_fl(pa->state.vel, 1.0f/dtime);
2428                                 break;
2429                 }
2430         }
2431 }
2432
2433 /*********************************************************************************************************
2434  *                    SPH fluid physics 
2435  *
2436  * In theory, there could be unlimited implementation of SPH simulators
2437  *
2438  * This code uses in some parts adapted algorithms from the pseudo code as outlined in the Research paper:
2439  *
2440  * Titled: Particle-based Viscoelastic Fluid Simulation.
2441  * Authors: Simon Clavet, Philippe Beaudoin and Pierre Poulin
2442  * Website: http://www.iro.umontreal.ca/labs/infographie/papers/Clavet-2005-PVFS/
2443  *
2444  * Presented at Siggraph, (2005)
2445  *
2446  * ********************************************************************************************************/
2447 #define PSYS_FLUID_SPRINGS_INITIAL_SIZE 256
2448 static ParticleSpring *sph_spring_add(ParticleSystem *psys, ParticleSpring *spring)
2449 {
2450         /* Are more refs required? */
2451         if (psys->alloc_fluidsprings == 0 || psys->fluid_springs == NULL) {
2452                 psys->alloc_fluidsprings = PSYS_FLUID_SPRINGS_INITIAL_SIZE;
2453                 psys->fluid_springs = (ParticleSpring*)MEM_callocN(psys->alloc_fluidsprings * sizeof(ParticleSpring), "Particle Fluid Springs");
2454         }
2455         else if (psys->tot_fluidsprings == psys->alloc_fluidsprings) {
2456                 /* Double the number of refs allocated */
2457                 psys->alloc_fluidsprings *= 2;
2458                 psys->fluid_springs = (ParticleSpring*)MEM_reallocN(psys->fluid_springs, psys->alloc_fluidsprings * sizeof(ParticleSpring));
2459         }
2460
2461         memcpy(psys->fluid_springs + psys->tot_fluidsprings, spring, sizeof(ParticleSpring));
2462         psys->tot_fluidsprings++;
2463
2464         return psys->fluid_springs + psys->tot_fluidsprings - 1;
2465 }
2466 static void sph_spring_delete(ParticleSystem *psys, int j)
2467 {
2468         if (j != psys->tot_fluidsprings - 1)
2469                 psys->fluid_springs[j] = psys->fluid_springs[psys->tot_fluidsprings - 1];
2470
2471         psys->tot_fluidsprings--;
2472
2473         if (psys->tot_fluidsprings < psys->alloc_fluidsprings/2 && psys->alloc_fluidsprings > PSYS_FLUID_SPRINGS_INITIAL_SIZE) {
2474                 psys->alloc_fluidsprings /= 2;
2475                 psys->fluid_springs = (ParticleSpring*)MEM_reallocN(psys->fluid_springs,  psys->alloc_fluidsprings * sizeof(ParticleSpring));
2476         }
2477 }
2478 static void sph_springs_modify(ParticleSystem *psys, float dtime)
2479 {
2480         SPHFluidSettings *fluid = psys->part->fluid;
2481         ParticleData *pa1, *pa2;
2482         ParticleSpring *spring = psys->fluid_springs;
2483         
2484         float h, d, Rij[3], rij, Lij;
2485         int i;
2486
2487         float yield_ratio = fluid->yield_ratio;
2488         float plasticity = fluid->plasticity_constant;
2489         /* scale things according to dtime */
2490         float timefix = 25.f * dtime;
2491
2492         if ((fluid->flag & SPH_VISCOELASTIC_SPRINGS)==0 || fluid->spring_k == 0.f)
2493                 return;
2494
2495         /* Loop through the springs */
2496         for (i=0; i<psys->tot_fluidsprings; i++, spring++) {
2497                 pa1 = psys->particles + spring->particle_index[0];
2498                 pa2 = psys->particles + spring->particle_index[1];
2499
2500                 sub_v3_v3v3(Rij, pa2->prev_state.co, pa1->prev_state.co);
2501                 rij = normalize_v3(Rij);
2502
2503                 /* adjust rest length */
2504                 Lij = spring->rest_length;
2505                 d = yield_ratio * timefix * Lij;
2506
2507                 if (rij > Lij + d) // Stretch
2508                         spring->rest_length += plasticity * (rij - Lij - d) * timefix;
2509                 else if (rij < Lij - d) // Compress
2510                         spring->rest_length -= plasticity * (Lij - d - rij) * timefix;
2511
2512                 h = 4.f*pa1->size;
2513
2514                 if (spring->rest_length > h)
2515                         spring->delete_flag = 1;
2516         }
2517
2518         /* Loop through springs backwaqrds - for efficient delete function */
2519         for (i=psys->tot_fluidsprings-1; i >= 0; i--) {
2520                 if (psys->fluid_springs[i].delete_flag)
2521                         sph_spring_delete(psys, i);
2522         }
2523 }
2524 static EdgeHash *sph_springhash_build(ParticleSystem *psys)
2525 {
2526         EdgeHash *springhash = NULL;
2527         ParticleSpring *spring;
2528         int i = 0;
2529
2530         springhash = BLI_edgehash_new_ex(__func__, psys->tot_fluidsprings);
2531
2532         for (i=0, spring=psys->fluid_springs; i<psys->tot_fluidsprings; i++, spring++)
2533                 BLI_edgehash_insert(springhash, spring->particle_index[0], spring->particle_index[1], SET_INT_IN_POINTER(i+1));
2534
2535         return springhash;
2536 }
2537
2538 #define SPH_NEIGHBORS 512
2539 typedef struct SPHNeighbor {
2540         ParticleSystem *psys;
2541         int index;
2542 } SPHNeighbor;
2543
2544 typedef struct SPHRangeData {
2545         SPHNeighbor neighbors[SPH_NEIGHBORS];
2546         int tot_neighbors;
2547
2548         float* data;
2549
2550         ParticleSystem *npsys;
2551         ParticleData *pa;
2552
2553         float h;
2554         float mass;
2555         float massfac;
2556         int use_size;
2557 } SPHRangeData;
2558
2559 static void sph_evaluate_func(BVHTree *tree, ParticleSystem **psys, float co[3], SPHRangeData *pfr, float interaction_radius, BVHTree_RangeQuery callback)
2560 {
2561         int i;
2562
2563         pfr->tot_neighbors = 0;
2564
2565         for (i=0; i < 10 && psys[i]; i++) {
2566                 pfr->npsys    = psys[i];
2567                 pfr->massfac  = psys[i]->part->mass / pfr->mass;
2568                 pfr->use_size = psys[i]->part->flag & PART_SIZEMASS;
2569
2570                 if (tree) {
2571                         BLI_bvhtree_range_query(tree, co, interaction_radius, callback, pfr);
2572                         break;
2573                 }
2574                 else {
2575                         BLI_rw_mutex_lock(&psys_bvhtree_rwlock, THREAD_LOCK_READ);
2576                         
2577                         BLI_bvhtree_range_query(psys[i]->bvhtree, co, interaction_radius, callback, pfr);
2578                         
2579                         BLI_rw_mutex_unlock(&psys_bvhtree_rwlock);
2580                 }
2581         }
2582 }
2583 static void sph_density_accum_cb(void *userdata, int index, float squared_dist)
2584 {
2585         SPHRangeData *pfr = (SPHRangeData *)userdata;
2586         ParticleData *npa = pfr->npsys->particles + index;
2587         float q;
2588         float dist;
2589
2590         if (npa == pfr->pa || squared_dist < FLT_EPSILON)
2591                 return;
2592
2593         /* Ugh! One particle has too many neighbors! If some aren't taken into
2594          * account, the forces will be biased by the tree search order. This
2595          * effectively adds enery to the system, and results in a churning motion.
2596          * But, we have to stop somewhere, and it's not the end of the world.
2597          *  - jahka and z0r
2598          */
2599         if (pfr->tot_neighbors >= SPH_NEIGHBORS)
2600                 return;
2601
2602         pfr->neighbors[pfr->tot_neighbors].index = index;
2603         pfr->neighbors[pfr->tot_neighbors].psys = pfr->npsys;
2604         pfr->tot_neighbors++;
2605
2606         dist = sqrtf(squared_dist);
2607         q = (1.f - dist/pfr->h) * pfr->massfac;
2608
2609         if (pfr->use_size)
2610                 q *= npa->size;
2611
2612         pfr->data[0] += q*q;
2613         pfr->data[1] += q*q*q;
2614 }
2615
2616 /*
2617  * Find the Courant number for an SPH particle (used for adaptive time step).
2618  */
2619 static void sph_particle_courant(SPHData *sphdata, SPHRangeData *pfr)
2620 {
2621         ParticleData *pa, *npa;
2622         int i;
2623         float flow[3], offset[3], dist;
2624
2625         zero_v3(flow);
2626
2627         dist = 0.0f;
2628         if (pfr->tot_neighbors > 0) {
2629                 pa = pfr->pa;
2630                 for (i=0; i < pfr->tot_neighbors; i++) {
2631                         npa = pfr->neighbors[i].psys->particles + pfr->neighbors[i].index;
2632                         sub_v3_v3v3(offset, pa->prev_state.co, npa->prev_state.co);
2633                         dist += len_v3(offset);
2634                         add_v3_v3(flow, npa->prev_state.vel);
2635                 }
2636                 dist += sphdata->psys[0]->part->fluid->radius; // TODO: remove this? - z0r
2637                 sphdata->element_size = dist / pfr->tot_neighbors;
2638                 mul_v3_v3fl(sphdata->flow, flow, 1.0f / pfr->tot_neighbors);
2639         }
2640         else {
2641                 sphdata->element_size = FLT_MAX;
2642                 copy_v3_v3(sphdata->flow, flow);
2643         }
2644 }
2645 static void sph_force_cb(void *sphdata_v, ParticleKey *state, float *force, float *UNUSED(impulse))
2646 {
2647         SPHData *sphdata = (SPHData *)sphdata_v;
2648         ParticleSystem **psys = sphdata->psys;
2649         ParticleData *pa = sphdata->pa;
2650         SPHFluidSettings *fluid = psys[0]->part->fluid;
2651         ParticleSpring *spring = NULL;
2652         SPHRangeData pfr;
2653         SPHNeighbor *pfn;
2654         float *gravity = sphdata->gravity;
2655         EdgeHash *springhash = sphdata->eh;
2656
2657         float q, u, rij, dv[3];
2658         float pressure, near_pressure;
2659
2660         float visc = fluid->viscosity_omega;
2661         float stiff_visc = fluid->viscosity_beta * (fluid->flag & SPH_FAC_VISCOSITY ? fluid->viscosity_omega : 1.f);
2662
2663         float inv_mass = 1.0f / sphdata->mass;
2664         float spring_constant = fluid->spring_k;
2665
2666         /* 4.0 seems to be a pretty good value */
2667         float interaction_radius = fluid->radius * (fluid->flag & SPH_FAC_RADIUS ? 4.0f * pa->size : 1.0f);
2668         float h = interaction_radius * sphdata->hfac;
2669         float rest_density = fluid->rest_density * (fluid->flag & SPH_FAC_DENSITY ? 4.77f : 1.f); /* 4.77 is an experimentally determined density factor */
2670         float rest_length = fluid->rest_length * (fluid->flag & SPH_FAC_REST_LENGTH ? 2.588f * pa->size : 1.f);
2671
2672         float stiffness = fluid->stiffness_k;
2673         float stiffness_near_fac = fluid->stiffness_knear * (fluid->flag & SPH_FAC_REPULSION ? fluid->stiffness_k : 1.f);
2674
2675         ParticleData *npa;
2676         float vec[3];
2677         float vel[3];
2678         float co[3];
2679         float data[2];
2680         float density, near_density;
2681
2682         int i, spring_index, index = pa - psys[0]->particles;
2683
2684         data[0] = data[1] = 0;
2685         pfr.data = data;
2686         pfr.h = h;
2687         pfr.pa = pa;
2688         pfr.mass = sphdata->mass;
2689
2690         sph_evaluate_func( NULL, psys, state->co, &pfr, interaction_radius, sph_density_accum_cb);
2691
2692         density = data[0];
2693         near_density = data[1];
2694
2695         pressure =  stiffness * (density - rest_density);
2696         near_pressure = stiffness_near_fac * near_density;
2697
2698         pfn = pfr.neighbors;
2699         for (i=0; i<pfr.tot_neighbors; i++, pfn++) {
2700                 npa = pfn->psys->particles + pfn->index;
2701
2702                 madd_v3_v3v3fl(co, npa->prev_state.co, npa->prev_state.vel, state->time);
2703
2704                 sub_v3_v3v3(vec, co, state->co);
2705                 rij = normalize_v3(vec);
2706
2707                 q = (1.f - rij/h) * pfn->psys->part->mass * inv_mass;
2708
2709                 if (pfn->psys->part->flag & PART_SIZEMASS)
2710                         q *= npa->size;
2711
2712                 copy_v3_v3(vel, npa->prev_state.vel);
2713
2714                 /* Double Density Relaxation */
2715                 madd_v3_v3fl(force, vec, -(pressure + near_pressure*q)*q);
2716
2717                 /* Viscosity */
2718                 if (visc > 0.f  || stiff_visc > 0.f) {
2719                         sub_v3_v3v3(dv, vel, state->vel);
2720                         u = dot_v3v3(vec, dv);
2721
2722                         if (u < 0.f && visc > 0.f)
2723                                 madd_v3_v3fl(force, vec, 0.5f * q * visc * u );
2724
2725                         if (u > 0.f && stiff_visc > 0.f)
2726                                 madd_v3_v3fl(force, vec, 0.5f * q * stiff_visc * u );
2727                 }
2728
2729                 if (spring_constant > 0.f) {
2730                         /* Viscoelastic spring force */
2731                         if (pfn->psys == psys[0] && fluid->flag & SPH_VISCOELASTIC_SPRINGS && springhash) {
2732                                 /* BLI_edgehash_lookup appears to be thread-safe. - z0r */
2733                                 spring_index = GET_INT_FROM_POINTER(BLI_edgehash_lookup(springhash, index, pfn->index));
2734
2735                                 if (spring_index) {
2736                                         spring = psys[0]->fluid_springs + spring_index - 1;
2737
2738                                         madd_v3_v3fl(force, vec, -10.f * spring_constant * (1.f - rij/h) * (spring->rest_length - rij));
2739                                 }
2740                                 else if (fluid->spring_frames == 0 || (pa->prev_state.time-pa->time) <= fluid->spring_frames) {
2741                                         ParticleSpring temp_spring;
2742                                         temp_spring.particle_index[0] = index;
2743                                         temp_spring.particle_index[1] = pfn->index;
2744                                         temp_spring.rest_length = (fluid->flag & SPH_CURRENT_REST_LENGTH) ? rij : rest_length;
2745                                         temp_spring.delete_flag = 0;
2746
2747                                         /* sph_spring_add is not thread-safe. - z0r */
2748 #pragma omp critical
2749                                         sph_spring_add(psys[0], &temp_spring);
2750                                 }
2751                         }
2752                         else {/* PART_SPRING_HOOKES - Hooke's spring force */
2753                                 madd_v3_v3fl(force, vec, -10.f * spring_constant * (1.f - rij/h) * (rest_length - rij));
2754                         }
2755                 }
2756         }
2757         
2758         /* Artificial buoyancy force in negative gravity direction  */
2759         if (fluid->buoyancy > 0.f && gravity)
2760                 madd_v3_v3fl(force, gravity, fluid->buoyancy * (density-rest_density));
2761
2762         if (sphdata->pass == 0 && psys[0]->part->time_flag & PART_TIME_AUTOSF)
2763                 sph_particle_courant(sphdata, &pfr);
2764         sphdata->pass++;
2765 }
2766
2767 static void sphclassical_density_accum_cb(void *userdata, int index, float UNUSED(squared_dist))
2768 {
2769         SPHRangeData *pfr = (SPHRangeData *)userdata;
2770         ParticleData *npa = pfr->npsys->particles + index;
2771         float q;
2772         float qfac = 21.0f / (256.f * (float)M_PI);
2773         float rij, rij_h;
2774         float vec[3];
2775
2776         /* Exclude particles that are more than 2h away. Can't use squared_dist here
2777          * because it is not accurate enough. Use current state, i.e. the output of
2778          * basic_integrate() - z0r */
2779         sub_v3_v3v3(vec, npa->state.co, pfr->pa->state.co);
2780         rij = len_v3(vec);
2781         rij_h = rij / pfr->h;
2782         if (rij_h > 2.0f)
2783                 return;
2784
2785         /* Smoothing factor. Utilise the Wendland kernel. gnuplot:
2786          *     q1(x) = (2.0 - x)**4 * ( 1.0 + 2.0 * x)
2787          *     plot [0:2] q1(x) */
2788         q  = qfac / pow3f(pfr->h) * pow4f(2.0f - rij_h) * ( 1.0f + 2.0f * rij_h);
2789         q *= pfr->npsys->part->mass;
2790
2791         if (pfr->use_size)
2792                 q *= pfr->pa->size;
2793
2794         pfr->data[0] += q;
2795         pfr->data[1] += q / npa->sphdensity;
2796 }
2797
2798 static void sphclassical_neighbour_accum_cb(void *userdata, int index, float UNUSED(squared_dist))
2799 {
2800         SPHRangeData *pfr = (SPHRangeData *)userdata;
2801         ParticleData *npa = pfr->npsys->particles + index;
2802         float rij, rij_h;
2803         float vec[3];
2804
2805         if (pfr->tot_neighbors >= SPH_NEIGHBORS)
2806                 return;
2807
2808         /* Exclude particles that are more than 2h away. Can't use squared_dist here
2809          * because it is not accurate enough. Use current state, i.e. the output of
2810          * basic_integrate() - z0r */
2811         sub_v3_v3v3(vec, npa->state.co, pfr->pa->state.co);
2812         rij = len_v3(vec);
2813         rij_h = rij / pfr->h;
2814         if (rij_h > 2.0f)
2815                 return;
2816
2817         pfr->neighbors[pfr->tot_neighbors].index = index;
2818         pfr->neighbors[pfr->tot_neighbors].psys = pfr->npsys;
2819         pfr->tot_neighbors++;
2820 }
2821 static void sphclassical_force_cb(void *sphdata_v, ParticleKey *state, float *force, float *UNUSED(impulse))
2822 {
2823         SPHData *sphdata = (SPHData *)sphdata_v;
2824         ParticleSystem **psys = sphdata->psys;
2825         ParticleData *pa = sphdata->pa;
2826         SPHFluidSettings *fluid = psys[0]->part->fluid;
2827         SPHRangeData pfr;
2828         SPHNeighbor *pfn;
2829         float *gravity = sphdata->gravity;
2830
2831         float dq, u, rij, dv[3];
2832         float pressure, npressure;
2833
2834         float visc = fluid->viscosity_omega;
2835
2836         float interaction_radius;
2837         float h, hinv;
2838         /* 4.77 is an experimentally determined density factor */
2839         float rest_density = fluid->rest_density * (fluid->flag & SPH_FAC_DENSITY ? 4.77f : 1.0f);
2840
2841         // Use speed of sound squared
2842         float stiffness = pow2f(fluid->stiffness_k);
2843
2844         ParticleData *npa;
2845         float vec[3];
2846         float co[3];
2847         float pressureTerm;
2848
2849         int i;
2850
2851         float qfac2 = 42.0f / (256.0f * (float)M_PI);
2852         float rij_h;
2853
2854         /* 4.0 here is to be consistent with previous formulation/interface */
2855         interaction_radius = fluid->radius * (fluid->flag & SPH_FAC_RADIUS ? 4.0f * pa->size : 1.0f);
2856         h = interaction_radius * sphdata->hfac;
2857         hinv = 1.0f / h;
2858
2859         pfr.h = h;
2860         pfr.pa = pa;
2861
2862         sph_evaluate_func(NULL, psys, state->co, &pfr, interaction_radius, sphclassical_neighbour_accum_cb);
2863         pressure =  stiffness * (pow7f(pa->sphdensity / rest_density) - 1.0f);
2864
2865         /* multiply by mass so that we return a force, not accel */
2866         qfac2 *= sphdata->mass / pow3f(pfr.h);
2867
2868         pfn = pfr.neighbors;
2869         for (i = 0; i < pfr.tot_neighbors; i++, pfn++) {
2870                 npa = pfn->psys->particles + pfn->index;
2871                 if (npa == pa) {
2872                         /* we do not contribute to ourselves */
2873                         continue;
2874                 }
2875
2876                 /* Find vector to neighbor. Exclude particles that are more than 2h
2877                  * away. Can't use current state here because it may have changed on
2878                  * another thread - so do own mini integration. Unlike basic_integrate,
2879                  * SPH integration depends on neighboring particles. - z0r */
2880                 madd_v3_v3v3fl(co, npa->prev_state.co, npa->prev_state.vel, state->time);
2881                 sub_v3_v3v3(vec, co, state->co);
2882                 rij = normalize_v3(vec);
2883                 rij_h = rij / pfr.h;
2884                 if (rij_h > 2.0f)
2885                         continue;
2886
2887                 npressure = stiffness * (pow7f(npa->sphdensity / rest_density) - 1.0f);
2888
2889                 /* First derivative of smoothing factor. Utilise the Wendland kernel.
2890                  * gnuplot:
2891                  *     q2(x) = 2.0 * (2.0 - x)**4 - 4.0 * (2.0 - x)**3 * (1.0 + 2.0 * x)
2892                  *     plot [0:2] q2(x)
2893                  * Particles > 2h away are excluded above. */
2894                 dq = qfac2 * (2.0f * pow4f(2.0f - rij_h) - 4.0f * pow3f(2.0f - rij_h) * (1.0f + 2.0f * rij_h)  );
2895
2896                 if (pfn->psys->part->flag & PART_SIZEMASS)
2897                         dq *= npa->size;
2898
2899                 pressureTerm = pressure / pow2f(pa->sphdensity) + npressure / pow2f(npa->sphdensity);
2900
2901                 /* Note that 'minus' is removed, because vec = vecBA, not vecAB.
2902                  * This applies to the viscosity calculation below, too. */
2903                 madd_v3_v3fl(force, vec, pressureTerm * dq);