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