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