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