Some options used for "expert" x264 setup were removed from FFmpeg 0.11
[blender.git] / source / blender / blenkernel / intern / softbody.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) Blender Foundation
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/softbody.c
29  *  \ingroup bke
30  */
31
32
33 /*
34 ******
35 variables on the UI for now
36
37         float mediafrict;  friction to env
38         float nodemass;   softbody mass of *vertex*
39         float grav;        softbody amount of gravitaion to apply
40
41         float goalspring;  softbody goal springs
42         float goalfrict;   softbody goal springs friction
43         float mingoal;     quick limits for goal
44         float maxgoal;
45
46         float inspring;   softbody inner springs
47         float infrict;     softbody inner springs friction
48
49 *****
50 */
51
52
53 #include <math.h>
54 #include <stdlib.h>
55 #include <string.h>
56
57 #include "MEM_guardedalloc.h"
58
59 /* types */
60 #include "DNA_object_types.h"
61 #include "DNA_scene_types.h"
62 #include "DNA_lattice_types.h"
63 #include "DNA_curve_types.h"
64 #include "DNA_mesh_types.h"
65 #include "DNA_meshdata_types.h"
66
67 #include "BLI_math.h"
68 #include "BLI_utildefines.h"
69 #include "BLI_listbase.h"
70 #include "BLI_ghash.h"
71 #include "BLI_threads.h"
72
73 #include "BKE_curve.h"
74 #include "BKE_effect.h"
75 #include "BKE_global.h"
76 #include "BKE_modifier.h"
77 #include "BKE_softbody.h"
78 #include "BKE_DerivedMesh.h"
79 #include "BKE_pointcache.h"
80 #include "BKE_deform.h"
81 #include "BKE_mesh.h" 
82 //XXX #include  "BIF_editdeform.h"
83 //XXX #include  "BIF_graphics.h"
84 #include  "PIL_time.h"
85 // #include  "ONL_opennl.h" remove linking to ONL for now
86
87 /* callbacks for errors and interrupts and some goo */
88 static int (*SB_localInterruptCallBack)(void) = NULL;
89
90
91 /* ********** soft body engine ******* */
92
93 typedef enum {SB_EDGE=1, SB_BEND=2, SB_STIFFQUAD=3, SB_HANDLE=4} type_spring;
94
95 typedef struct BodySpring {
96         int v1, v2;
97         float len, cf, load;
98         float ext_force[3]; /* edges colliding and sailing */
99         type_spring springtype;
100         short flag;
101 } BodySpring;
102
103 typedef struct BodyFace {
104         int v1, v2, v3, v4;
105         float ext_force[3]; /* faces colliding */
106         short flag;
107 } BodyFace;
108
109 typedef struct ReferenceVert {
110         float pos[3]; /* position relative to com */
111         float mass;   /* node mass */
112 } ReferenceVert;
113
114 typedef struct ReferenceState {
115         float com[3]; /* center of mass*/
116         ReferenceVert *ivert; /* list of intial values */
117 } ReferenceState;
118
119
120 /*private scratch pad for caching and other data only needed when alive*/
121 typedef struct SBScratch {
122         GHash *colliderhash;
123         short needstobuildcollider;
124         short flag;
125         BodyFace *bodyface;
126         int totface;
127         float aabbmin[3], aabbmax[3];
128         ReferenceState Ref;
129 } SBScratch;
130
131 typedef struct  SB_thread_context {
132                 Scene *scene;
133                 Object *ob;
134                 float forcetime;
135                 float timenow;
136                 int ifirst;
137                 int ilast;
138                 ListBase *do_effector;
139                 int do_deflector;
140                 float fieldfactor;
141                 float windfactor;
142                 int nr;
143                 int tot;
144 } SB_thread_context;
145
146 #define NLF_BUILD  1
147 #define NLF_SOLVE  2
148
149 #define MID_PRESERVE 1
150
151 #define SOFTGOALSNAP  0.999f
152 /* if bp-> goal is above make it a *forced follow original* and skip all ODE stuff for this bp
153  * removes *unnecessary* stiffnes from ODE system
154  */
155 #define HEUNWARNLIMIT 1 /* 500 would be fine i think for detecting severe *stiff* stuff */
156
157
158 #define BSF_INTERSECT   1 /* edge intersects collider face */
159
160 /* private definitions for bodypoint states */
161 #define SBF_DOFUZZY          1 /* Bodypoint do fuzzy */
162 #define SBF_OUTOFCOLLISION   2 /* Bodypoint does not collide  */
163
164
165 #define BFF_INTERSECT   1 /* collider edge   intrudes face */
166 #define BFF_CLOSEVERT   2 /* collider vertex repulses face */
167
168
169 static float SoftHeunTol = 1.0f; /* humm .. this should be calculated from sb parameters and sizes */
170
171 /* local prototypes */
172 static void free_softbody_intern(SoftBody *sb);
173 /* aye this belongs to arith.c */
174 static void Vec3PlusStVec(float *v, float s, float *v1);
175
176 /*+++ frame based timing +++*/
177
178 /*physical unit of force is [kg * m / sec^2]*/
179
180 static float sb_grav_force_scale(Object *UNUSED(ob))
181 /* since unit of g is [m/sec^2] and F = mass * g we rescale unit mass of node to 1 gramm
182  * put it to a function here, so we can add user options later without touching simulation code
183  */
184 {
185         return (0.001f);
186 }
187
188 static float sb_fric_force_scale(Object *UNUSED(ob))
189 /* rescaling unit of drag [1 / sec] to somehow reasonable
190  * put it to a function here, so we can add user options later without touching simulation code
191  */
192 {
193         return (0.01f);
194 }
195
196 static float sb_time_scale(Object *ob)
197 /* defining the frames to *real* time relation */
198 {
199         SoftBody *sb= ob->soft; /* is supposed to be there */
200         if (sb) {
201                 return(sb->physics_speed);
202                 /*hrms .. this could be IPO as well :)
203                  estimated range [0.001 sluggish slug - 100.0 very fast (i hope ODE solver can handle that)]
204                  1 approx = a unit 1 pendulum at g = 9.8 [earth conditions]  has period 65 frames
205                  theory would give a 50 frames period .. so there must be something inaccurate .. looking for that (BM)
206                  */
207         }
208         return (1.0f);
209         /*
210         this would be frames/sec independent timing assuming 25 fps is default
211         but does not work very well with NLA
212                 return (25.0f/scene->r.frs_sec)
213         */
214 }
215 /*--- frame based timing ---*/
216
217 /* helper functions for everything is animatable jow_go_for2_5 +++++++*/
218 /* introducing them here, because i know: steps in properties  ( at frame timing )
219  * will cause unwanted responses of the softbody system (which does inter frame calculations )
220  * so first 'cure' would be: interpolate linear in time ..
221  * Q: why do i write this?
222  * A: because it happend once, that some eger coder 'streamlined' code to fail.
223  * We DO linear interpolation for goals .. and i think we should do on animated properties as well
224  */
225
226 /* animate sb->maxgoal, sb->mingoal */
227 static float _final_goal(Object *ob, BodyPoint *bp)/*jow_go_for2_5 */
228 {
229         float f = -1999.99f;
230         if (ob) {
231                 SoftBody *sb= ob->soft; /* is supposed to be there */
232                 if (!(ob->softflag & OB_SB_GOAL)) return (0.0f);
233                 if (sb&&bp) {
234                         if (bp->goal < 0.0f) return (0.0f);
235                         f = sb->mingoal + bp->goal*ABS(sb->maxgoal - sb->mingoal);
236                         f = pow(f, 4.0f);
237                         return (f);
238                 }
239         }
240         printf("_final_goal failed! sb or bp ==NULL\n");
241         return f; /*using crude but spot able values some times helps debuggin */
242 }
243
244 static float _final_mass(Object *ob, BodyPoint *bp)
245 {
246         if (ob) {
247                 SoftBody *sb= ob->soft; /* is supposed to be there */
248                 if (sb&&bp) {
249                         return(bp->mass*sb->nodemass);
250                 }
251         }
252         printf("_final_mass failed! sb or bp ==NULL\n");
253         return 1.0f;
254 }
255 /* helper functions for everything is animateble jow_go_for2_5 ------*/
256
257 /*+++ collider caching and dicing +++*/
258
259 /********************
260 for each target object/face the axis aligned bounding box (AABB) is stored
261 faces parallel to global axes
262 so only simple "value" in [min, max] ckecks are used
263 float operations still
264 */
265
266 /* just an ID here to reduce the prob for killing objects
267 ** ob->sumohandle points to we should not kill :)
268 */
269 static const int CCD_SAVETY = 190561;
270
271 typedef struct ccdf_minmax {
272         float minx, miny, minz, maxx, maxy, maxz;
273 } ccdf_minmax;
274
275
276
277 typedef struct ccd_Mesh {
278         int totvert, totface;
279         MVert *mvert;
280         MVert *mprevvert;
281         MFace *mface;
282         int savety;
283         ccdf_minmax *mima;
284         /* Axis Aligned Bounding Box AABB */
285         float bbmin[3];
286         float bbmax[3];
287 } ccd_Mesh;
288
289
290
291
292 static ccd_Mesh *ccd_mesh_make(Object *ob)
293 {
294         CollisionModifierData *cmd;
295         ccd_Mesh *pccd_M = NULL;
296         ccdf_minmax *mima = NULL;
297         MFace *mface=NULL;
298         float v[3], hull;
299         int i;
300
301         cmd =(CollisionModifierData *)modifiers_findByType(ob, eModifierType_Collision);
302
303         /* first some paranoia checks */
304         if (!cmd) return NULL;
305         if (!cmd->numverts || !cmd->numfaces) return NULL;
306
307         pccd_M = MEM_mallocN(sizeof(ccd_Mesh), "ccd_Mesh");
308         pccd_M->totvert = cmd->numverts;
309         pccd_M->totface = cmd->numfaces;
310         pccd_M->savety  = CCD_SAVETY;
311         pccd_M->bbmin[0]=pccd_M->bbmin[1]=pccd_M->bbmin[2]=1e30f;
312         pccd_M->bbmax[0]=pccd_M->bbmax[1]=pccd_M->bbmax[2]=-1e30f;
313         pccd_M->mprevvert=NULL;
314
315
316         /* blow it up with forcefield ranges */
317         hull = MAX2(ob->pd->pdef_sbift, ob->pd->pdef_sboft);
318
319         /* alloc and copy verts*/
320         pccd_M->mvert = MEM_dupallocN(cmd->xnew);
321         /* note that xnew coords are already in global space, */
322         /* determine the ortho BB */
323         for (i=0; i < pccd_M->totvert; i++) {
324                 /* evaluate limits */
325                 copy_v3_v3(v, pccd_M->mvert[i].co);
326                 pccd_M->bbmin[0] = MIN2(pccd_M->bbmin[0], v[0]-hull);
327                 pccd_M->bbmin[1] = MIN2(pccd_M->bbmin[1], v[1]-hull);
328                 pccd_M->bbmin[2] = MIN2(pccd_M->bbmin[2], v[2]-hull);
329
330                 pccd_M->bbmax[0] = MAX2(pccd_M->bbmax[0], v[0]+hull);
331                 pccd_M->bbmax[1] = MAX2(pccd_M->bbmax[1], v[1]+hull);
332                 pccd_M->bbmax[2] = MAX2(pccd_M->bbmax[2], v[2]+hull);
333
334         }
335         /* alloc and copy faces*/
336         pccd_M->mface = MEM_dupallocN(cmd->mfaces);
337
338         /* OBBs for idea1 */
339         pccd_M->mima = MEM_mallocN(sizeof(ccdf_minmax)*pccd_M->totface, "ccd_Mesh_Faces_mima");
340         mima  = pccd_M->mima;
341         mface = pccd_M->mface;
342
343
344         /* anyhoo we need to walk the list of faces and find OBB they live in */
345         for (i=0; i < pccd_M->totface; i++) {
346                 mima->minx=mima->miny=mima->minz=1e30f;
347                 mima->maxx=mima->maxy=mima->maxz=-1e30f;
348
349                 copy_v3_v3(v, pccd_M->mvert[mface->v1].co);
350                 mima->minx = MIN2(mima->minx, v[0]-hull);
351                 mima->miny = MIN2(mima->miny, v[1]-hull);
352                 mima->minz = MIN2(mima->minz, v[2]-hull);
353                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
354                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
355                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
356
357                 copy_v3_v3(v, pccd_M->mvert[mface->v2].co);
358                 mima->minx = MIN2(mima->minx, v[0]-hull);
359                 mima->miny = MIN2(mima->miny, v[1]-hull);
360                 mima->minz = MIN2(mima->minz, v[2]-hull);
361                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
362                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
363                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
364
365                 copy_v3_v3(v, pccd_M->mvert[mface->v3].co);
366                 mima->minx = MIN2(mima->minx, v[0]-hull);
367                 mima->miny = MIN2(mima->miny, v[1]-hull);
368                 mima->minz = MIN2(mima->minz, v[2]-hull);
369                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
370                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
371                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
372
373                 if (mface->v4) {
374                         copy_v3_v3(v, pccd_M->mvert[mface->v4].co);
375                 mima->minx = MIN2(mima->minx, v[0]-hull);
376                 mima->miny = MIN2(mima->miny, v[1]-hull);
377                 mima->minz = MIN2(mima->minz, v[2]-hull);
378                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
379                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
380                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
381                 }
382
383
384         mima++;
385         mface++;
386
387         }
388         return pccd_M;
389 }
390 static void ccd_mesh_update(Object *ob, ccd_Mesh *pccd_M)
391 {
392         CollisionModifierData *cmd;
393         ccdf_minmax *mima = NULL;
394         MFace *mface=NULL;
395         float v[3], hull;
396         int i;
397
398         cmd =(CollisionModifierData *)modifiers_findByType(ob, eModifierType_Collision);
399
400         /* first some paranoia checks */
401         if (!cmd) return;
402         if (!cmd->numverts || !cmd->numfaces) return;
403
404         if ((pccd_M->totvert != cmd->numverts) ||
405             (pccd_M->totface != cmd->numfaces))
406         {
407                 return;
408         }
409
410         pccd_M->bbmin[0]=pccd_M->bbmin[1]=pccd_M->bbmin[2]=1e30f;
411         pccd_M->bbmax[0]=pccd_M->bbmax[1]=pccd_M->bbmax[2]=-1e30f;
412
413
414         /* blow it up with forcefield ranges */
415         hull = MAX2(ob->pd->pdef_sbift, ob->pd->pdef_sboft);
416
417         /* rotate current to previous */
418         if (pccd_M->mprevvert) MEM_freeN(pccd_M->mprevvert);
419         pccd_M->mprevvert = pccd_M->mvert;
420         /* alloc and copy verts*/
421         pccd_M->mvert = MEM_dupallocN(cmd->xnew);
422         /* note that xnew coords are already in global space, */
423         /* determine the ortho BB */
424         for (i=0; i < pccd_M->totvert; i++) {
425                 /* evaluate limits */
426                 copy_v3_v3(v, pccd_M->mvert[i].co);
427                 pccd_M->bbmin[0] = MIN2(pccd_M->bbmin[0], v[0]-hull);
428                 pccd_M->bbmin[1] = MIN2(pccd_M->bbmin[1], v[1]-hull);
429                 pccd_M->bbmin[2] = MIN2(pccd_M->bbmin[2], v[2]-hull);
430
431                 pccd_M->bbmax[0] = MAX2(pccd_M->bbmax[0], v[0]+hull);
432                 pccd_M->bbmax[1] = MAX2(pccd_M->bbmax[1], v[1]+hull);
433                 pccd_M->bbmax[2] = MAX2(pccd_M->bbmax[2], v[2]+hull);
434
435                 /* evaluate limits */
436                 copy_v3_v3(v, pccd_M->mprevvert[i].co);
437                 pccd_M->bbmin[0] = MIN2(pccd_M->bbmin[0], v[0]-hull);
438                 pccd_M->bbmin[1] = MIN2(pccd_M->bbmin[1], v[1]-hull);
439                 pccd_M->bbmin[2] = MIN2(pccd_M->bbmin[2], v[2]-hull);
440
441                 pccd_M->bbmax[0] = MAX2(pccd_M->bbmax[0], v[0]+hull);
442                 pccd_M->bbmax[1] = MAX2(pccd_M->bbmax[1], v[1]+hull);
443                 pccd_M->bbmax[2] = MAX2(pccd_M->bbmax[2], v[2]+hull);
444
445         }
446
447         mima  = pccd_M->mima;
448         mface = pccd_M->mface;
449
450
451         /* anyhoo we need to walk the list of faces and find OBB they live in */
452         for (i=0; i < pccd_M->totface; i++) {
453                 mima->minx=mima->miny=mima->minz=1e30f;
454                 mima->maxx=mima->maxy=mima->maxz=-1e30f;
455
456                 copy_v3_v3(v, pccd_M->mvert[mface->v1].co);
457                 mima->minx = MIN2(mima->minx, v[0]-hull);
458                 mima->miny = MIN2(mima->miny, v[1]-hull);
459                 mima->minz = MIN2(mima->minz, v[2]-hull);
460                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
461                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
462                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
463
464                 copy_v3_v3(v, pccd_M->mvert[mface->v2].co);
465                 mima->minx = MIN2(mima->minx, v[0]-hull);
466                 mima->miny = MIN2(mima->miny, v[1]-hull);
467                 mima->minz = MIN2(mima->minz, v[2]-hull);
468                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
469                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
470                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
471
472                 copy_v3_v3(v, pccd_M->mvert[mface->v3].co);
473                 mima->minx = MIN2(mima->minx, v[0]-hull);
474                 mima->miny = MIN2(mima->miny, v[1]-hull);
475                 mima->minz = MIN2(mima->minz, v[2]-hull);
476                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
477                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
478                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
479
480                 if (mface->v4) {
481                         copy_v3_v3(v, pccd_M->mvert[mface->v4].co);
482                 mima->minx = MIN2(mima->minx, v[0]-hull);
483                 mima->miny = MIN2(mima->miny, v[1]-hull);
484                 mima->minz = MIN2(mima->minz, v[2]-hull);
485                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
486                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
487                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
488                 }
489
490
491                 copy_v3_v3(v, pccd_M->mprevvert[mface->v1].co);
492                 mima->minx = MIN2(mima->minx, v[0]-hull);
493                 mima->miny = MIN2(mima->miny, v[1]-hull);
494                 mima->minz = MIN2(mima->minz, v[2]-hull);
495                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
496                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
497                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
498
499                 copy_v3_v3(v, pccd_M->mprevvert[mface->v2].co);
500                 mima->minx = MIN2(mima->minx, v[0]-hull);
501                 mima->miny = MIN2(mima->miny, v[1]-hull);
502                 mima->minz = MIN2(mima->minz, v[2]-hull);
503                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
504                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
505                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
506
507                 copy_v3_v3(v, pccd_M->mprevvert[mface->v3].co);
508                 mima->minx = MIN2(mima->minx, v[0]-hull);
509                 mima->miny = MIN2(mima->miny, v[1]-hull);
510                 mima->minz = MIN2(mima->minz, v[2]-hull);
511                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
512                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
513                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
514
515                 if (mface->v4) {
516                         copy_v3_v3(v, pccd_M->mprevvert[mface->v4].co);
517                 mima->minx = MIN2(mima->minx, v[0]-hull);
518                 mima->miny = MIN2(mima->miny, v[1]-hull);
519                 mima->minz = MIN2(mima->minz, v[2]-hull);
520                 mima->maxx = MAX2(mima->maxx, v[0]+hull);
521                 mima->maxy = MAX2(mima->maxy, v[1]+hull);
522                 mima->maxz = MAX2(mima->maxz, v[2]+hull);
523                 }
524
525
526         mima++;
527         mface++;
528
529         }
530         return;
531 }
532
533 static void ccd_mesh_free(ccd_Mesh *ccdm)
534 {
535         if (ccdm && (ccdm->savety == CCD_SAVETY )) { /*make sure we're not nuking objects we don't know*/
536                 MEM_freeN(ccdm->mface);
537                 MEM_freeN(ccdm->mvert);
538                 if (ccdm->mprevvert) MEM_freeN(ccdm->mprevvert);
539                 MEM_freeN(ccdm->mima);
540                 MEM_freeN(ccdm);
541                 ccdm = NULL;
542         }
543 }
544
545 static void ccd_build_deflector_hash(Scene *scene, Object *vertexowner, GHash *hash)
546 {
547         Base *base= scene->base.first;
548         Object *ob;
549
550         if (!hash) return;
551         while (base) {
552                 /*Only proceed for mesh object in same layer */
553                 if (base->object->type==OB_MESH && (base->lay & vertexowner->lay)) {
554                         ob= base->object;
555                         if ((vertexowner) && (ob == vertexowner)) {
556                                 /* if vertexowner is given  we don't want to check collision with owner object */
557                                 base = base->next;
558                                 continue;
559                         }
560
561                         /*+++ only with deflecting set */
562                         if (ob->pd && ob->pd->deflect && BLI_ghash_lookup(hash, ob) == NULL) {
563                                 ccd_Mesh *ccdmesh = ccd_mesh_make(ob);
564                                 BLI_ghash_insert(hash, ob, ccdmesh);
565                         }/*--- only with deflecting set */
566
567                 }/* mesh && layer*/
568                 base = base->next;
569         } /* while (base) */
570 }
571
572 static void ccd_update_deflector_hash(Scene *scene, Object *vertexowner, GHash *hash)
573 {
574         Base *base= scene->base.first;
575         Object *ob;
576
577         if ((!hash) || (!vertexowner)) return;
578         while (base) {
579                 /*Only proceed for mesh object in same layer */
580                 if (base->object->type==OB_MESH && (base->lay & vertexowner->lay)) {
581                         ob= base->object;
582                         if (ob == vertexowner) {
583                                 /* if vertexowner is given  we don't want to check collision with owner object */
584                                 base = base->next;
585                                 continue;
586                         }
587
588                         /*+++ only with deflecting set */
589                         if (ob->pd && ob->pd->deflect) {
590                                 ccd_Mesh *ccdmesh = BLI_ghash_lookup(hash, ob);
591                                 if (ccdmesh)
592                                         ccd_mesh_update(ob, ccdmesh);
593                         }/*--- only with deflecting set */
594
595                 }/* mesh && layer*/
596                 base = base->next;
597         } /* while (base) */
598 }
599
600
601
602
603 /*--- collider caching and dicing ---*/
604
605
606 static int count_mesh_quads(Mesh *me)
607 {
608         int a, result = 0;
609         MFace *mface= me->mface;
610
611         if (mface) {
612                 for (a=me->totface; a>0; a--, mface++) {
613                         if (mface->v4) result++;
614                 }
615         }
616         return result;
617 }
618
619 static void add_mesh_quad_diag_springs(Object *ob)
620 {
621         Mesh *me= ob->data;
622         MFace *mface= me->mface;
623         /*BodyPoint *bp;*/ /*UNUSED*/
624         BodySpring *bs, *bs_new;
625         int a;
626
627         if (ob->soft) {
628                 int nofquads;
629                 //float s_shear = ob->soft->shearstiff*ob->soft->shearstiff;
630
631                 nofquads = count_mesh_quads(me);
632                 if (nofquads) {
633                         /* resize spring-array to hold additional quad springs */
634                         bs_new= MEM_callocN((ob->soft->totspring + nofquads *2 )*sizeof(BodySpring), "bodyspring");
635                         memcpy(bs_new, ob->soft->bspring, (ob->soft->totspring )*sizeof(BodySpring));
636
637                         if (ob->soft->bspring)
638                                 MEM_freeN(ob->soft->bspring); /* do this before reassigning the pointer  or have a 1st class memory leak */
639                         ob->soft->bspring = bs_new;
640
641                         /* fill the tail */
642                         a = 0;
643                         bs = bs_new+ob->soft->totspring;
644                         /*bp= ob->soft->bpoint; */ /*UNUSED*/
645                         if (mface ) {
646                                 for (a=me->totface; a>0; a--, mface++) {
647                                         if (mface->v4) {
648                                                 bs->v1= mface->v1;
649                                                 bs->v2= mface->v3;
650                                                 bs->springtype   = SB_STIFFQUAD;
651                                                 bs++;
652                                                 bs->v1= mface->v2;
653                                                 bs->v2= mface->v4;
654                                                 bs->springtype   = SB_STIFFQUAD;
655                                                 bs++;
656
657                                         }
658                                 }
659                         }
660
661                         /* now we can announce new springs */
662                         ob->soft->totspring += nofquads *2;
663                 }
664         }
665 }
666
667 static void add_2nd_order_roller(Object *ob, float UNUSED(stiffness), int *counter, int addsprings)
668 {
669         /*assume we have a softbody*/
670         SoftBody *sb= ob->soft; /* is supposed to be there */
671         BodyPoint *bp, *bpo;
672         BodySpring *bs, *bs2, *bs3= NULL;
673         int a, b, c, notthis= 0, v0;
674         if (!sb->bspring) {return;} /* we are 2nd order here so 1rst should have been build :) */
675         /* first run counting  second run adding */
676         *counter = 0;
677         if (addsprings) bs3 = ob->soft->bspring+ob->soft->totspring;
678         for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
679                 /*scan for neighborhood*/
680                 bpo = NULL;
681                 v0  = (sb->totpoint-a);
682                 for (b=bp->nofsprings;b>0;b--) {
683                         bs = sb->bspring + bp->springs[b-1];
684                         /*nasty thing here that springs have two ends
685                         so here we have to make sure we examine the other */
686                         if (v0 == bs->v1) {
687                                 bpo = sb->bpoint+bs->v2;
688                                 notthis = bs->v2;
689                         }
690                         else {
691                         if (v0 == bs->v2) {
692                                 bpo = sb->bpoint+bs->v1;
693                                 notthis = bs->v1;
694                         }
695                         else {printf("oops we should not get here -  add_2nd_order_springs");}
696                         }
697                         if (bpo) {/* so now we have a 2nd order humpdidump */
698                                 for (c=bpo->nofsprings;c>0;c--) {
699                                         bs2 = sb->bspring + bpo->springs[c-1];
700                                         if ((bs2->v1 != notthis) && (bs2->v1 > v0)) {
701                                                 (*counter)++;/*hit */
702                                                 if (addsprings) {
703                                                         bs3->v1= v0;
704                                                         bs3->v2= bs2->v1;
705                                                         bs3->springtype   = SB_BEND;
706                                                         bs3++;
707                                                 }
708                                         }
709                                         if ((bs2->v2 !=notthis)&&(bs2->v2 > v0)) {
710                                         (*counter)++;/*hit */
711                                                 if (addsprings) {
712                                                         bs3->v1= v0;
713                                                         bs3->v2= bs2->v2;
714                                                         bs3->springtype   = SB_BEND;
715                                                         bs3++;
716                                                 }
717
718                                         }
719                                 }
720
721                         }
722
723                 }
724                 /*scan for neighborhood done*/
725         }
726 }
727
728
729 static void add_2nd_order_springs(Object *ob, float stiffness)
730 {
731         int counter = 0;
732         BodySpring *bs_new;
733         stiffness *=stiffness;
734
735         add_2nd_order_roller(ob, stiffness, &counter, 0); /* counting */
736         if (counter) {
737                 /* resize spring-array to hold additional springs */
738                 bs_new= MEM_callocN((ob->soft->totspring + counter )*sizeof(BodySpring), "bodyspring");
739                 memcpy(bs_new, ob->soft->bspring, (ob->soft->totspring )*sizeof(BodySpring));
740
741                 if (ob->soft->bspring)
742                         MEM_freeN(ob->soft->bspring);
743                 ob->soft->bspring = bs_new;
744
745                 add_2nd_order_roller(ob, stiffness, &counter, 1); /* adding */
746                 ob->soft->totspring += counter;
747         }
748 }
749
750 static void add_bp_springlist(BodyPoint *bp, int springID)
751 {
752         int *newlist;
753
754         if (bp->springs == NULL) {
755                 bp->springs = MEM_callocN(sizeof(int), "bpsprings");
756                 bp->springs[0] = springID;
757                 bp->nofsprings = 1;
758         }
759         else {
760                 bp->nofsprings++;
761                 newlist = MEM_callocN(bp->nofsprings * sizeof(int), "bpsprings");
762                 memcpy(newlist, bp->springs, (bp->nofsprings-1)* sizeof(int));
763                 MEM_freeN(bp->springs);
764                 bp->springs = newlist;
765                 bp->springs[bp->nofsprings-1] = springID;
766         }
767 }
768
769 /* do this once when sb is build
770 it is O(N^2) so scanning for springs every iteration is too expensive
771 */
772 static void build_bps_springlist(Object *ob)
773 {
774         SoftBody *sb= ob->soft; /* is supposed to be there */
775         BodyPoint *bp;
776         BodySpring *bs;
777         int a, b;
778
779         if (sb==NULL) return; /* paranoya check */
780
781         for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
782                 /* throw away old list */
783                 if (bp->springs) {
784                         MEM_freeN(bp->springs);
785                         bp->springs=NULL;
786                 }
787                 /* scan for attached inner springs */
788                 for (b=sb->totspring, bs= sb->bspring; b>0; b--, bs++) {
789                         if (( (sb->totpoint-a) == bs->v1) ) {
790                                 add_bp_springlist(bp, sb->totspring -b);
791                         }
792                         if (( (sb->totpoint-a) == bs->v2) ) {
793                                 add_bp_springlist(bp, sb->totspring -b);
794                         }
795                 }/*for springs*/
796         }/*for bp*/
797 }
798
799 static void calculate_collision_balls(Object *ob)
800 {
801         SoftBody *sb= ob->soft; /* is supposed to be there */
802         BodyPoint *bp;
803         BodySpring *bs;
804         int a, b, akku_count;
805         float min, max, akku;
806
807         if (sb==NULL) return; /* paranoya check */
808
809         for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
810                 bp->colball=0;
811                 akku =0.0f;
812                 akku_count=0;
813                 min = 1e22f;
814                 max = -1e22f;
815                 /* first estimation based on attached */
816                 for (b=bp->nofsprings;b>0;b--) {
817                         bs = sb->bspring + bp->springs[b-1];
818                         if (bs->springtype == SB_EDGE) {
819                         akku += bs->len;
820                         akku_count++,
821                         min = MIN2(bs->len, min);
822                         max = MAX2(bs->len, max);
823                         }
824                 }
825
826                 if (akku_count > 0) {
827                         if (sb->sbc_mode == SBC_MODE_MANUAL) {
828                                 bp->colball=sb->colball;
829                         }
830                         if (sb->sbc_mode == SBC_MODE_AVG) {
831                                 bp->colball = akku/(float)akku_count*sb->colball;
832                         }
833                         if (sb->sbc_mode == SBC_MODE_MIN) {
834                                 bp->colball=min*sb->colball;
835                         }
836                         if (sb->sbc_mode == SBC_MODE_MAX) {
837                                 bp->colball=max*sb->colball;
838                         }
839                         if (sb->sbc_mode == SBC_MODE_AVGMINMAX) {
840                                 bp->colball = (min + max)/2.0f*sb->colball;
841                         }
842                 }
843                 else bp->colball=0;
844         }/*for bp*/
845 }
846
847
848 /* creates new softbody if didn't exist yet, makes new points and springs arrays */
849 static void renew_softbody(Scene *scene, Object *ob, int totpoint, int totspring)
850 {
851         SoftBody *sb;
852         int i;
853         short softflag;
854         if (ob->soft==NULL) ob->soft= sbNew(scene);
855         else free_softbody_intern(ob->soft);
856         sb= ob->soft;
857         softflag=ob->softflag;
858
859         if (totpoint) {
860                 sb->totpoint= totpoint;
861                 sb->totspring= totspring;
862
863                 sb->bpoint= MEM_mallocN(totpoint*sizeof(BodyPoint), "bodypoint");
864                 if (totspring)
865                         sb->bspring= MEM_mallocN(totspring*sizeof(BodySpring), "bodyspring");
866
867                         /* initialize BodyPoint array */
868                 for (i=0; i<totpoint; i++) {
869                         BodyPoint *bp = &sb->bpoint[i];
870
871
872                         /* hum as far as i see this is overridden by _final_goal() now jow_go_for2_5 */
873                         /* sadly breaks compatibility with older versions */
874                         /* but makes goals behave the same for meshes, lattices and curves */
875                         if (softflag & OB_SB_GOAL) {
876                                 bp->goal= sb->defgoal;
877                         }
878                         else {
879                                 bp->goal= 0.0f;
880                                 /* so this will definily be below SOFTGOALSNAP */
881                         }
882
883                         bp->nofsprings= 0;
884                         bp->springs= NULL;
885                         bp->choke = 0.0f;
886                         bp->choke2 = 0.0f;
887                         bp->frozen = 1.0f;
888                         bp->colball = 0.0f;
889                         bp->loc_flag = 0;
890                         bp->springweight = 1.0f;
891                         bp->mass = 1.0f;
892                 }
893         }
894 }
895
896 static void free_softbody_baked(SoftBody *sb)
897 {
898         SBVertex *key;
899         int k;
900
901         for (k=0; k<sb->totkey; k++) {
902                 key= *(sb->keys + k);
903                 if (key) MEM_freeN(key);
904         }
905         if (sb->keys) MEM_freeN(sb->keys);
906
907         sb->keys= NULL;
908         sb->totkey= 0;
909 }
910 static void free_scratch(SoftBody *sb)
911 {
912         if (sb->scratch) {
913                 /* todo make sure everything is cleaned up nicly */
914                 if (sb->scratch->colliderhash) {
915                         BLI_ghash_free(sb->scratch->colliderhash, NULL,
916                                         (GHashValFreeFP) ccd_mesh_free); /*this hoepfully will free all caches*/
917                         sb->scratch->colliderhash = NULL;
918                 }
919                 if (sb->scratch->bodyface) {
920                         MEM_freeN(sb->scratch->bodyface);
921                 }
922                 if (sb->scratch->Ref.ivert) {
923                         MEM_freeN(sb->scratch->Ref.ivert);
924                 }
925                 MEM_freeN(sb->scratch);
926                 sb->scratch = NULL;
927         }
928
929 }
930
931 /* only frees internal data */
932 static void free_softbody_intern(SoftBody *sb)
933 {
934         if (sb) {
935                 int a;
936                 BodyPoint *bp;
937
938                 if (sb->bpoint) {
939                         for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
940                                 /* free spring list */
941                                 if (bp->springs != NULL) {
942                                         MEM_freeN(bp->springs);
943                                 }
944                         }
945                         MEM_freeN(sb->bpoint);
946                 }
947
948                 if (sb->bspring) MEM_freeN(sb->bspring);
949
950                 sb->totpoint= sb->totspring= 0;
951                 sb->bpoint= NULL;
952                 sb->bspring= NULL;
953
954                 free_scratch(sb);
955                 free_softbody_baked(sb);
956         }
957 }
958
959
960 /* ************ dynamics ********** */
961
962 /* the most general (micro physics correct) way to do collision
963 ** (only needs the current particle position)
964 **
965 ** it actually checks if the particle intrudes a short range force field generated
966 ** by the faces of the target object and returns a force to drive the particel out
967 ** the strenght of the field grows exponetially if the particle is on the 'wrong' side of the face
968 ** 'wrong' side : projection to the face normal is negative (all referred to a vertex in the face)
969 **
970 ** flaw of this: 'fast' particles as well as 'fast' colliding faces
971 ** give a 'tunnel' effect such that the particle passes through the force field
972 ** without ever 'seeing' it
973 ** this is fully compliant to heisenberg: h >= fuzzy(location) * fuzzy(time)
974 ** besides our h is way larger than in QM because forces propagate way slower here
975 ** we have to deal with fuzzy(time) in the range of 1/25 seconds (typical frame rate)
976 ** yup collision targets are not known here any better
977 ** and 1/25 second is looong compared to real collision events
978 ** Q: why not use 'simple' collision here like bouncing back a particle
979 **   --> reverting is velocity on the face normal
980 ** A: because our particles are not alone here
981 **    and need to tell their neighbors exactly what happens via spring forces
982 ** unless sbObjectStep( .. ) is called on sub frame timing level
983 ** BTW that also questions the use of a 'implicit' solvers on softbodies
984 ** since that would only valid for 'slow' moving collision targets and dito particles
985 */
986
987 /* aye this belongs to arith.c */
988 static void Vec3PlusStVec(float *v, float s, float *v1)
989 {
990         v[0] += s*v1[0];
991         v[1] += s*v1[1];
992         v[2] += s*v1[2];
993 }
994
995 /* +++ dependency information functions*/
996
997 static int are_there_deflectors(Scene *scene, unsigned int layer)
998 {
999         Base *base;
1000
1001         for (base = scene->base.first; base; base= base->next) {
1002                 if ( (base->lay & layer) && base->object->pd) {
1003                         if (base->object->pd->deflect)
1004                                 return 1;
1005                 }
1006         }
1007         return 0;
1008 }
1009
1010 static int query_external_colliders(Scene *scene, Object *me)
1011 {
1012         return(are_there_deflectors(scene, me->lay));
1013 }
1014 /* --- dependency information functions*/
1015
1016
1017 /* +++ the aabb "force" section*/
1018 static int sb_detect_aabb_collisionCached(float UNUSED(force[3]), unsigned int UNUSED(par_layer), struct Object *vertexowner, float UNUSED(time))
1019 {
1020         Object *ob;
1021         SoftBody *sb=vertexowner->soft;
1022         GHash *hash;
1023         GHashIterator *ihash;
1024         float  aabbmin[3], aabbmax[3];
1025         int deflected=0;
1026 #if 0
1027         int a;
1028 #endif
1029
1030         if ((sb == NULL) || (sb->scratch ==NULL)) return 0;
1031         copy_v3_v3(aabbmin, sb->scratch->aabbmin);
1032         copy_v3_v3(aabbmax, sb->scratch->aabbmax);
1033
1034         hash  = vertexowner->soft->scratch->colliderhash;
1035         ihash = BLI_ghashIterator_new(hash);
1036         while (!BLI_ghashIterator_isDone(ihash) ) {
1037
1038                 ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
1039                 ob             = BLI_ghashIterator_getKey       (ihash);
1040                         /* only with deflecting set */
1041                         if (ob->pd && ob->pd->deflect) {
1042 #if 0                   /* UNUSED */
1043                                 MFace *mface= NULL;
1044                                 MVert *mvert= NULL;
1045                                 MVert *mprevvert= NULL;
1046                                 ccdf_minmax *mima= NULL;
1047 #endif
1048                                 if (ccdm) {
1049 #if 0                           /* UNUSED */
1050                                         mface= ccdm->mface;
1051                                         mvert= ccdm->mvert;
1052                                         mprevvert= ccdm->mprevvert;
1053                                         mima= ccdm->mima;
1054                                         a = ccdm->totface;
1055 #endif
1056                                         if ((aabbmax[0] < ccdm->bbmin[0]) ||
1057                                             (aabbmax[1] < ccdm->bbmin[1]) ||
1058                                             (aabbmax[2] < ccdm->bbmin[2]) ||
1059                                             (aabbmin[0] > ccdm->bbmax[0]) ||
1060                                             (aabbmin[1] > ccdm->bbmax[1]) ||
1061                                             (aabbmin[2] > ccdm->bbmax[2]) )
1062                                         {
1063                                                 /* boxes don't intersect */
1064                                                 BLI_ghashIterator_step(ihash);
1065                                                 continue;
1066                                         }
1067
1068                                         /* so now we have the 2 boxes overlapping */
1069                                         /* forces actually not used */
1070                                         deflected = 2;
1071
1072                                 }
1073                                 else {
1074                                         /*aye that should be cached*/
1075                                         printf("missing cache error\n");
1076                                         BLI_ghashIterator_step(ihash);
1077                                         continue;
1078                                 }
1079                         } /* if (ob->pd && ob->pd->deflect) */
1080                         BLI_ghashIterator_step(ihash);
1081         } /* while () */
1082         BLI_ghashIterator_free(ihash);
1083         return deflected;
1084 }
1085 /* --- the aabb section*/
1086
1087
1088 /* +++ the face external section*/
1089 static int sb_detect_face_pointCached(float face_v1[3], float face_v2[3], float face_v3[3], float *damp,
1090                                       float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner, float time)
1091 {
1092         Object *ob;
1093         GHash *hash;
1094         GHashIterator *ihash;
1095         float nv1[3], edge1[3], edge2[3], d_nvect[3], aabbmin[3], aabbmax[3];
1096         float facedist, outerfacethickness, tune = 10.f;
1097         int a, deflected=0;
1098
1099         aabbmin[0] = MIN3(face_v1[0], face_v2[0], face_v3[0]);
1100         aabbmin[1] = MIN3(face_v1[1], face_v2[1], face_v3[1]);
1101         aabbmin[2] = MIN3(face_v1[2], face_v2[2], face_v3[2]);
1102         aabbmax[0] = MAX3(face_v1[0], face_v2[0], face_v3[0]);
1103         aabbmax[1] = MAX3(face_v1[1], face_v2[1], face_v3[1]);
1104         aabbmax[2] = MAX3(face_v1[2], face_v2[2], face_v3[2]);
1105
1106         /* calculate face normal once again SIGH */
1107         sub_v3_v3v3(edge1, face_v1, face_v2);
1108         sub_v3_v3v3(edge2, face_v3, face_v2);
1109         cross_v3_v3v3(d_nvect, edge2, edge1);
1110         normalize_v3(d_nvect);
1111
1112
1113         hash  = vertexowner->soft->scratch->colliderhash;
1114         ihash = BLI_ghashIterator_new(hash);
1115         while (!BLI_ghashIterator_isDone(ihash) ) {
1116
1117                 ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
1118                 ob             = BLI_ghashIterator_getKey       (ihash);
1119                         /* only with deflecting set */
1120                         if (ob->pd && ob->pd->deflect) {
1121                                 MVert *mvert= NULL;
1122                                 MVert *mprevvert= NULL;
1123                                 if (ccdm) {
1124                                         mvert= ccdm->mvert;
1125                                         a    = ccdm->totvert;
1126                                         mprevvert= ccdm->mprevvert;
1127                                         outerfacethickness = ob->pd->pdef_sboft;
1128                                         if ((aabbmax[0] < ccdm->bbmin[0]) ||
1129                                             (aabbmax[1] < ccdm->bbmin[1]) ||
1130                                             (aabbmax[2] < ccdm->bbmin[2]) ||
1131                                             (aabbmin[0] > ccdm->bbmax[0]) ||
1132                                             (aabbmin[1] > ccdm->bbmax[1]) ||
1133                                             (aabbmin[2] > ccdm->bbmax[2]) )
1134                                         {
1135                                                 /* boxes don't intersect */
1136                                                 BLI_ghashIterator_step(ihash);
1137                                                 continue;
1138                                         }
1139
1140                                 }
1141                                 else {
1142                                         /*aye that should be cached*/
1143                                         printf("missing cache error\n");
1144                                         BLI_ghashIterator_step(ihash);
1145                                         continue;
1146                                 }
1147
1148
1149                                 /* use mesh*/
1150                                 if (mvert) {
1151                                         while (a) {
1152                                                 copy_v3_v3(nv1, mvert[a-1].co);
1153                                                 if (mprevvert) {
1154                                                         mul_v3_fl(nv1, time);
1155                                                         Vec3PlusStVec(nv1, (1.0f-time), mprevvert[a-1].co);
1156                                                 }
1157                                                 /* origin to face_v2*/
1158                                                 sub_v3_v3(nv1, face_v2);
1159                                                 facedist = dot_v3v3(nv1, d_nvect);
1160                                                 if (ABS(facedist)<outerfacethickness) {
1161                                                         if (isect_point_tri_prism_v3(nv1, face_v1, face_v2, face_v3) ) {
1162                                                                 float df;
1163                                                                 if (facedist > 0) {
1164                                                                         df = (outerfacethickness-facedist)/outerfacethickness;
1165                                                                 }
1166                                                                 else {
1167                                                                         df = (outerfacethickness+facedist)/outerfacethickness;
1168                                                                 }
1169
1170                                                                 *damp=df*tune*ob->pd->pdef_sbdamp;
1171
1172                                                                 df = 0.01f*exp(- 100.0f*df);
1173                                                                 Vec3PlusStVec(force, -df, d_nvect);
1174                                                                 deflected = 3;
1175                                                         }
1176                                                 }
1177                                                 a--;
1178                                         }/* while (a)*/
1179                                 } /* if (mvert) */
1180                         } /* if (ob->pd && ob->pd->deflect) */
1181                         BLI_ghashIterator_step(ihash);
1182         } /* while () */
1183         BLI_ghashIterator_free(ihash);
1184         return deflected;
1185 }
1186
1187
1188 static int sb_detect_face_collisionCached(float face_v1[3], float face_v2[3], float face_v3[3], float *damp,
1189                                           float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner, float time)
1190 {
1191         Object *ob;
1192         GHash *hash;
1193         GHashIterator *ihash;
1194         float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3], d_nvect[3], aabbmin[3], aabbmax[3];
1195         float t, tune = 10.0f;
1196         int a, deflected=0;
1197
1198         aabbmin[0] = MIN3(face_v1[0], face_v2[0], face_v3[0]);
1199         aabbmin[1] = MIN3(face_v1[1], face_v2[1], face_v3[1]);
1200         aabbmin[2] = MIN3(face_v1[2], face_v2[2], face_v3[2]);
1201         aabbmax[0] = MAX3(face_v1[0], face_v2[0], face_v3[0]);
1202         aabbmax[1] = MAX3(face_v1[1], face_v2[1], face_v3[1]);
1203         aabbmax[2] = MAX3(face_v1[2], face_v2[2], face_v3[2]);
1204
1205         hash  = vertexowner->soft->scratch->colliderhash;
1206         ihash = BLI_ghashIterator_new(hash);
1207         while (!BLI_ghashIterator_isDone(ihash) ) {
1208
1209                 ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
1210                 ob             = BLI_ghashIterator_getKey       (ihash);
1211                         /* only with deflecting set */
1212                         if (ob->pd && ob->pd->deflect) {
1213                                 MFace *mface= NULL;
1214                                 MVert *mvert= NULL;
1215                                 MVert *mprevvert= NULL;
1216                                 ccdf_minmax *mima= NULL;
1217                                 if (ccdm) {
1218                                         mface= ccdm->mface;
1219                                         mvert= ccdm->mvert;
1220                                         mprevvert= ccdm->mprevvert;
1221                                         mima= ccdm->mima;
1222                                         a = ccdm->totface;
1223
1224                                         if ((aabbmax[0] < ccdm->bbmin[0]) ||
1225                                             (aabbmax[1] < ccdm->bbmin[1]) ||
1226                                             (aabbmax[2] < ccdm->bbmin[2]) ||
1227                                             (aabbmin[0] > ccdm->bbmax[0]) ||
1228                                             (aabbmin[1] > ccdm->bbmax[1]) ||
1229                                             (aabbmin[2] > ccdm->bbmax[2]) )
1230                                         {
1231                                                 /* boxes don't intersect */
1232                                                 BLI_ghashIterator_step(ihash);
1233                                                 continue;
1234                                         }
1235
1236                                 }
1237                                 else {
1238                                         /*aye that should be cached*/
1239                                         printf("missing cache error\n");
1240                                         BLI_ghashIterator_step(ihash);
1241                                         continue;
1242                                 }
1243
1244
1245                                 /* use mesh*/
1246                                 while (a--) {
1247                                         if (
1248                                                 (aabbmax[0] < mima->minx) ||
1249                                                 (aabbmin[0] > mima->maxx) ||
1250                                                 (aabbmax[1] < mima->miny) ||
1251                                                 (aabbmin[1] > mima->maxy) ||
1252                                                 (aabbmax[2] < mima->minz) ||
1253                                                 (aabbmin[2] > mima->maxz)
1254                                                 ) {
1255                                                 mface++;
1256                                                 mima++;
1257                                                 continue;
1258                                         }
1259
1260
1261                                         if (mvert) {
1262
1263                                                 copy_v3_v3(nv1, mvert[mface->v1].co);
1264                                                 copy_v3_v3(nv2, mvert[mface->v2].co);
1265                                                 copy_v3_v3(nv3, mvert[mface->v3].co);
1266                                                 if (mface->v4) {
1267                                                         copy_v3_v3(nv4, mvert[mface->v4].co);
1268                                                 }
1269                                                 if (mprevvert) {
1270                                                         mul_v3_fl(nv1, time);
1271                                                         Vec3PlusStVec(nv1, (1.0f-time), mprevvert[mface->v1].co);
1272
1273                                                         mul_v3_fl(nv2, time);
1274                                                         Vec3PlusStVec(nv2, (1.0f-time), mprevvert[mface->v2].co);
1275
1276                                                         mul_v3_fl(nv3, time);
1277                                                         Vec3PlusStVec(nv3, (1.0f-time), mprevvert[mface->v3].co);
1278
1279                                                         if (mface->v4) {
1280                                                                 mul_v3_fl(nv4, time);
1281                                                                 Vec3PlusStVec(nv4, (1.0f-time), mprevvert[mface->v4].co);
1282                                                         }
1283                                                 }
1284                                         }
1285
1286                                         /* switch origin to be nv2*/
1287                                         sub_v3_v3v3(edge1, nv1, nv2);
1288                                         sub_v3_v3v3(edge2, nv3, nv2);
1289                                         cross_v3_v3v3(d_nvect, edge2, edge1);
1290                                         normalize_v3(d_nvect);
1291                                         if (
1292                                                 isect_line_tri_v3(nv1, nv2, face_v1, face_v2, face_v3, &t, NULL) ||
1293                                                 isect_line_tri_v3(nv2, nv3, face_v1, face_v2, face_v3, &t, NULL) ||
1294                                                 isect_line_tri_v3(nv3, nv1, face_v1, face_v2, face_v3, &t, NULL) ) {
1295                                                 Vec3PlusStVec(force, -0.5f, d_nvect);
1296                                                 *damp=tune*ob->pd->pdef_sbdamp;
1297                                                 deflected = 2;
1298                                         }
1299                                         if (mface->v4) { /* quad */
1300                                                 /* switch origin to be nv4 */
1301                                                 sub_v3_v3v3(edge1, nv3, nv4);
1302                                                 sub_v3_v3v3(edge2, nv1, nv4);
1303                                                 cross_v3_v3v3(d_nvect, edge2, edge1);
1304                                                 normalize_v3(d_nvect);
1305                                                 if (
1306                                                         /* isect_line_tri_v3(nv1, nv3, face_v1, face_v2, face_v3, &t, NULL) ||
1307                                                          we did that edge already */
1308                                                         isect_line_tri_v3(nv3, nv4, face_v1, face_v2, face_v3, &t, NULL) ||
1309                                                         isect_line_tri_v3(nv4, nv1, face_v1, face_v2, face_v3, &t, NULL) ) {
1310                                                         Vec3PlusStVec(force, -0.5f, d_nvect);
1311                                                         *damp=tune*ob->pd->pdef_sbdamp;
1312                                                         deflected = 2;
1313                                                 }
1314                                         }
1315                                         mface++;
1316                                         mima++;
1317                                 }/* while a */
1318                         } /* if (ob->pd && ob->pd->deflect) */
1319                         BLI_ghashIterator_step(ihash);
1320         } /* while () */
1321         BLI_ghashIterator_free(ihash);
1322         return deflected;
1323 }
1324
1325
1326
1327 static void scan_for_ext_face_forces(Object *ob, float timenow)
1328 {
1329         SoftBody *sb = ob->soft;
1330         BodyFace *bf;
1331         int a;
1332         float damp=0.0f, choke=1.0f;
1333         float tune = -10.0f;
1334         float feedback[3];
1335
1336         if (sb && sb->scratch->totface) {
1337
1338
1339                 bf = sb->scratch->bodyface;
1340                 for (a=0; a<sb->scratch->totface; a++, bf++) {
1341                         bf->ext_force[0]=bf->ext_force[1]=bf->ext_force[2]=0.0f;
1342 /*+++edges intruding*/
1343                         bf->flag &= ~BFF_INTERSECT;
1344                         feedback[0]=feedback[1]=feedback[2]=0.0f;
1345                         if (sb_detect_face_collisionCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v2].pos, sb->bpoint[bf->v3].pos,
1346                                 &damp,  feedback, ob->lay, ob, timenow)) {
1347                                 Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
1348                                 Vec3PlusStVec(sb->bpoint[bf->v2].force, tune, feedback);
1349                                 Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
1350 //                              Vec3PlusStVec(bf->ext_force, tune, feedback);
1351                                 bf->flag |= BFF_INTERSECT;
1352                                 choke = MIN2(MAX2(damp, choke), 1.0f);
1353                         }
1354
1355                         feedback[0]=feedback[1]=feedback[2]=0.0f;
1356                         if ((bf->v4) && (sb_detect_face_collisionCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v3].pos, sb->bpoint[bf->v4].pos,
1357                                 &damp,  feedback, ob->lay, ob, timenow))) {
1358                                 Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
1359                                 Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
1360                                 Vec3PlusStVec(sb->bpoint[bf->v4].force, tune, feedback);
1361 //                              Vec3PlusStVec(bf->ext_force, tune, feedback);
1362                                 bf->flag |= BFF_INTERSECT;
1363                                 choke = MIN2(MAX2(damp, choke), 1.0f);
1364                         }
1365 /*---edges intruding*/
1366
1367 /*+++ close vertices*/
1368                         if (( bf->flag & BFF_INTERSECT)==0) {
1369                                 bf->flag &= ~BFF_CLOSEVERT;
1370                                 tune = -1.0f;
1371                                 feedback[0]=feedback[1]=feedback[2]=0.0f;
1372                                 if (sb_detect_face_pointCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v2].pos, sb->bpoint[bf->v3].pos,
1373                                         &damp,  feedback, ob->lay, ob, timenow)) {
1374                                 Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
1375                                 Vec3PlusStVec(sb->bpoint[bf->v2].force, tune, feedback);
1376                                 Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
1377 //                                              Vec3PlusStVec(bf->ext_force, tune, feedback);
1378                                                 bf->flag |= BFF_CLOSEVERT;
1379                                                 choke = MIN2(MAX2(damp, choke), 1.0f);
1380                                 }
1381
1382                                 feedback[0]=feedback[1]=feedback[2]=0.0f;
1383                                 if ((bf->v4) && (sb_detect_face_pointCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v3].pos, sb->bpoint[bf->v4].pos,
1384                                         &damp,  feedback, ob->lay, ob, timenow))) {
1385                                 Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
1386                                 Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
1387                                 Vec3PlusStVec(sb->bpoint[bf->v4].force, tune, feedback);
1388 //                                              Vec3PlusStVec(bf->ext_force, tune, feedback);
1389                                                 bf->flag |= BFF_CLOSEVERT;
1390                                                 choke = MIN2(MAX2(damp, choke), 1.0f);
1391                                 }
1392                         }
1393 /*--- close vertices*/
1394                 }
1395                 bf = sb->scratch->bodyface;
1396                 for (a=0; a<sb->scratch->totface; a++, bf++) {
1397                         if (( bf->flag & BFF_INTERSECT) || ( bf->flag & BFF_CLOSEVERT)) {
1398                                 sb->bpoint[bf->v1].choke2=MAX2(sb->bpoint[bf->v1].choke2, choke);
1399                                 sb->bpoint[bf->v2].choke2=MAX2(sb->bpoint[bf->v2].choke2, choke);
1400                                 sb->bpoint[bf->v3].choke2=MAX2(sb->bpoint[bf->v3].choke2, choke);
1401                                 if (bf->v4) {
1402                                 sb->bpoint[bf->v2].choke2=MAX2(sb->bpoint[bf->v2].choke2, choke);
1403                                 }
1404                         }
1405                 }
1406         }
1407 }
1408
1409 /*  --- the face external section*/
1410
1411
1412 /* +++ the spring external section*/
1413
1414 static int sb_detect_edge_collisionCached(float edge_v1[3], float edge_v2[3], float *damp,
1415                                                                    float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner, float time)
1416 {
1417         Object *ob;
1418         GHash *hash;
1419         GHashIterator *ihash;
1420         float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3], d_nvect[3], aabbmin[3], aabbmax[3];
1421         float t, el;
1422         int a, deflected=0;
1423
1424         aabbmin[0] = MIN2(edge_v1[0], edge_v2[0]);
1425         aabbmin[1] = MIN2(edge_v1[1], edge_v2[1]);
1426         aabbmin[2] = MIN2(edge_v1[2], edge_v2[2]);
1427         aabbmax[0] = MAX2(edge_v1[0], edge_v2[0]);
1428         aabbmax[1] = MAX2(edge_v1[1], edge_v2[1]);
1429         aabbmax[2] = MAX2(edge_v1[2], edge_v2[2]);
1430
1431         el = len_v3v3(edge_v1, edge_v2);
1432
1433         hash  = vertexowner->soft->scratch->colliderhash;
1434         ihash = BLI_ghashIterator_new(hash);
1435         while (!BLI_ghashIterator_isDone(ihash) ) {
1436
1437                 ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
1438                 ob             = BLI_ghashIterator_getKey       (ihash);
1439                         /* only with deflecting set */
1440                         if (ob->pd && ob->pd->deflect) {
1441                                 MFace *mface= NULL;
1442                                 MVert *mvert= NULL;
1443                                 MVert *mprevvert= NULL;
1444                                 ccdf_minmax *mima= NULL;
1445                                 if (ccdm) {
1446                                         mface= ccdm->mface;
1447                                         mvert= ccdm->mvert;
1448                                         mprevvert= ccdm->mprevvert;
1449                                         mima= ccdm->mima;
1450                                         a = ccdm->totface;
1451
1452                                         if ((aabbmax[0] < ccdm->bbmin[0]) ||
1453                                             (aabbmax[1] < ccdm->bbmin[1]) ||
1454                                             (aabbmax[2] < ccdm->bbmin[2]) ||
1455                                             (aabbmin[0] > ccdm->bbmax[0]) ||
1456                                             (aabbmin[1] > ccdm->bbmax[1]) ||
1457                                             (aabbmin[2] > ccdm->bbmax[2]) )
1458                                         {
1459                                                 /* boxes don't intersect */
1460                                                 BLI_ghashIterator_step(ihash);
1461                                                 continue;
1462                                         }
1463
1464                                 }
1465                                 else {
1466                                         /*aye that should be cached*/
1467                                         printf("missing cache error\n");
1468                                         BLI_ghashIterator_step(ihash);
1469                                         continue;
1470                                 }
1471
1472
1473                                 /* use mesh*/
1474                                 while (a--) {
1475                                         if (
1476                                                 (aabbmax[0] < mima->minx) ||
1477                                                 (aabbmin[0] > mima->maxx) ||
1478                                                 (aabbmax[1] < mima->miny) ||
1479                                                 (aabbmin[1] > mima->maxy) ||
1480                                                 (aabbmax[2] < mima->minz) ||
1481                                                 (aabbmin[2] > mima->maxz)
1482                                                 ) {
1483                                                 mface++;
1484                                                 mima++;
1485                                                 continue;
1486                                         }
1487
1488
1489                                         if (mvert) {
1490
1491                                                 copy_v3_v3(nv1, mvert[mface->v1].co);
1492                                                 copy_v3_v3(nv2, mvert[mface->v2].co);
1493                                                 copy_v3_v3(nv3, mvert[mface->v3].co);
1494                                                 if (mface->v4) {
1495                                                         copy_v3_v3(nv4, mvert[mface->v4].co);
1496                                                 }
1497                                                 if (mprevvert) {
1498                                                         mul_v3_fl(nv1, time);
1499                                                         Vec3PlusStVec(nv1, (1.0f-time), mprevvert[mface->v1].co);
1500
1501                                                         mul_v3_fl(nv2, time);
1502                                                         Vec3PlusStVec(nv2, (1.0f-time), mprevvert[mface->v2].co);
1503
1504                                                         mul_v3_fl(nv3, time);
1505                                                         Vec3PlusStVec(nv3, (1.0f-time), mprevvert[mface->v3].co);
1506
1507                                                         if (mface->v4) {
1508                                                                 mul_v3_fl(nv4, time);
1509                                                                 Vec3PlusStVec(nv4, (1.0f-time), mprevvert[mface->v4].co);
1510                                                         }
1511                                                 }
1512                                         }
1513
1514                                         /* switch origin to be nv2*/
1515                                         sub_v3_v3v3(edge1, nv1, nv2);
1516                                         sub_v3_v3v3(edge2, nv3, nv2);
1517
1518                                         cross_v3_v3v3(d_nvect, edge2, edge1);
1519                                         normalize_v3(d_nvect);
1520                                         if ( isect_line_tri_v3(edge_v1, edge_v2, nv1, nv2, nv3, &t, NULL)) {
1521                                                 float v1[3], v2[3];
1522                                                 float intrusiondepth, i1, i2;
1523                                                 sub_v3_v3v3(v1, edge_v1, nv2);
1524                                                 sub_v3_v3v3(v2, edge_v2, nv2);
1525                                                 i1 = dot_v3v3(v1, d_nvect);
1526                                                 i2 = dot_v3v3(v2, d_nvect);
1527                                                 intrusiondepth = -MIN2(i1, i2)/el;
1528                                                 Vec3PlusStVec(force, intrusiondepth, d_nvect);
1529                                                 *damp=ob->pd->pdef_sbdamp;
1530                                                 deflected = 2;
1531                                         }
1532                                         if (mface->v4) { /* quad */
1533                                                 /* switch origin to be nv4 */
1534                                                 sub_v3_v3v3(edge1, nv3, nv4);
1535                                                 sub_v3_v3v3(edge2, nv1, nv4);
1536
1537                                                 cross_v3_v3v3(d_nvect, edge2, edge1);
1538                                                 normalize_v3(d_nvect);
1539                                                 if (isect_line_tri_v3( edge_v1, edge_v2, nv1, nv3, nv4, &t, NULL)) {
1540                                                         float v1[3], v2[3];
1541                                                         float intrusiondepth, i1, i2;
1542                                                         sub_v3_v3v3(v1, edge_v1, nv4);
1543                                                         sub_v3_v3v3(v2, edge_v2, nv4);
1544                                                 i1 = dot_v3v3(v1, d_nvect);
1545                                                 i2 = dot_v3v3(v2, d_nvect);
1546                                                 intrusiondepth = -MIN2(i1, i2)/el;
1547
1548
1549                                                         Vec3PlusStVec(force, intrusiondepth, d_nvect);
1550                                                         *damp=ob->pd->pdef_sbdamp;
1551                                                         deflected = 2;
1552                                                 }
1553                                         }
1554                                         mface++;
1555                                         mima++;
1556                                 }/* while a */
1557                         } /* if (ob->pd && ob->pd->deflect) */
1558                         BLI_ghashIterator_step(ihash);
1559         } /* while () */
1560         BLI_ghashIterator_free(ihash);
1561         return deflected;
1562 }
1563
1564 static void _scan_for_ext_spring_forces(Scene *scene, Object *ob, float timenow, int ifirst, int ilast, struct ListBase *do_effector)
1565 {
1566         SoftBody *sb = ob->soft;
1567         int a;
1568         float damp;
1569         float feedback[3];
1570
1571         if (sb && sb->totspring) {
1572                 for (a=ifirst; a<ilast; a++) {
1573                         BodySpring *bs = &sb->bspring[a];
1574                         bs->ext_force[0]=bs->ext_force[1]=bs->ext_force[2]=0.0f;
1575                         feedback[0]=feedback[1]=feedback[2]=0.0f;
1576                         bs->flag &= ~BSF_INTERSECT;
1577
1578                         if (bs->springtype == SB_EDGE) {
1579                                 /* +++ springs colliding */
1580                                 if (ob->softflag & OB_SB_EDGECOLL) {
1581                                         if ( sb_detect_edge_collisionCached (sb->bpoint[bs->v1].pos, sb->bpoint[bs->v2].pos,
1582                                                 &damp, feedback, ob->lay, ob, timenow)) {
1583                                                         add_v3_v3(bs->ext_force, feedback);
1584                                                         bs->flag |= BSF_INTERSECT;
1585                                                         //bs->cf=damp;
1586                                                         bs->cf=sb->choke*0.01f;
1587
1588                                         }
1589                                 }
1590                                 /* ---- springs colliding */
1591
1592                                 /* +++ springs seeing wind ... n stuff depending on their orientation*/
1593                                 /* note we don't use sb->mediafrict but use sb->aeroedge for magnitude of effect*/
1594                                 if (sb->aeroedge) {
1595                                         float vel[3], sp[3], pr[3], force[3];
1596                                         float f, windfactor  = 0.25f;
1597                                         /*see if we have wind*/
1598                                         if (do_effector) {
1599                                                 EffectedPoint epoint;
1600                                                 float speed[3]={0.0f, 0.0f, 0.0f};
1601                                                 float pos[3];
1602                                                 mid_v3_v3v3(pos, sb->bpoint[bs->v1].pos, sb->bpoint[bs->v2].pos);
1603                                                 mid_v3_v3v3(vel, sb->bpoint[bs->v1].vec, sb->bpoint[bs->v2].vec);
1604                                                 pd_point_from_soft(scene, pos, vel, -1, &epoint);
1605                                                 pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
1606
1607                                                 mul_v3_fl(speed, windfactor);
1608                                                 add_v3_v3(vel, speed);
1609                                         }
1610                                         /* media in rest */
1611                                         else {
1612                                                 add_v3_v3v3(vel, sb->bpoint[bs->v1].vec, sb->bpoint[bs->v2].vec);
1613                                         }
1614                                         f = normalize_v3(vel);
1615                                         f = -0.0001f*f*f*sb->aeroedge;
1616                                         /* (todo) add a nice angle dependent function done for now BUT */
1617                                         /* still there could be some nice drag/lift function, but who needs it */
1618
1619                                         sub_v3_v3v3(sp, sb->bpoint[bs->v1].pos, sb->bpoint[bs->v2].pos);
1620                                         project_v3_v3v3(pr, vel, sp);
1621                                         sub_v3_v3(vel, pr);
1622                                         normalize_v3(vel);
1623                                         if (ob->softflag & OB_SB_AERO_ANGLE) {
1624                                                 normalize_v3(sp);
1625                                                 Vec3PlusStVec(bs->ext_force, f*(1.0f-ABS(dot_v3v3(vel, sp))), vel);
1626                                         }
1627                                         else {
1628                                                 Vec3PlusStVec(bs->ext_force, f, vel); // to keep compatible with 2.45 release files
1629                                         }
1630                                 }
1631                                 /* --- springs seeing wind */
1632                         }
1633                 }
1634         }
1635 }
1636
1637
1638 static void scan_for_ext_spring_forces(Scene *scene, Object *ob, float timenow)
1639 {
1640         SoftBody *sb = ob->soft;
1641         ListBase *do_effector = NULL;
1642
1643         do_effector = pdInitEffectors(scene, ob, NULL, sb->effector_weights);
1644         _scan_for_ext_spring_forces(scene, ob, timenow, 0, sb->totspring, do_effector);
1645         pdEndEffectors(&do_effector);
1646 }
1647
1648 static void *exec_scan_for_ext_spring_forces(void *data)
1649 {
1650         SB_thread_context *pctx = (SB_thread_context*)data;
1651         _scan_for_ext_spring_forces(pctx->scene, pctx->ob, pctx->timenow, pctx->ifirst, pctx->ilast, pctx->do_effector);
1652         return NULL;
1653 }
1654
1655 static void sb_sfesf_threads_run(Scene *scene, struct Object *ob, float timenow, int totsprings, int *UNUSED(ptr_to_break_func(void)))
1656 {
1657         ListBase *do_effector = NULL;
1658         ListBase threads;
1659         SB_thread_context *sb_threads;
1660         int i, totthread, left, dec;
1661         int lowsprings =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */
1662
1663         do_effector= pdInitEffectors(scene, ob, NULL, ob->soft->effector_weights);
1664
1665         /* figure the number of threads while preventing pretty pointless threading overhead */
1666         if (scene->r.mode & R_FIXED_THREADS)
1667                 totthread= scene->r.threads;
1668         else
1669                 totthread= BLI_system_thread_count();
1670         /* what if we got zillions of CPUs running but less to spread*/
1671         while ((totsprings/totthread < lowsprings) && (totthread > 1)) {
1672                 totthread--;
1673         }
1674
1675         sb_threads= MEM_callocN(sizeof(SB_thread_context)*totthread, "SBSpringsThread");
1676         memset(sb_threads, 0, sizeof(SB_thread_context)*totthread);
1677         left = totsprings;
1678         dec = totsprings/totthread +1;
1679         for (i=0; i<totthread; i++) {
1680                 sb_threads[i].scene = scene;
1681                 sb_threads[i].ob = ob;
1682                 sb_threads[i].forcetime = 0.0; // not used here
1683                 sb_threads[i].timenow = timenow;
1684                 sb_threads[i].ilast   = left;
1685                 left = left - dec;
1686                 if (left >0) {
1687                         sb_threads[i].ifirst  = left;
1688                 }
1689                 else
1690                         sb_threads[i].ifirst  = 0;
1691                 sb_threads[i].do_effector = do_effector;
1692                 sb_threads[i].do_deflector = FALSE;// not used here
1693                 sb_threads[i].fieldfactor = 0.0f;// not used here
1694                 sb_threads[i].windfactor  = 0.0f;// not used here
1695                 sb_threads[i].nr= i;
1696                 sb_threads[i].tot= totthread;
1697         }
1698         if (totthread > 1) {
1699                 BLI_init_threads(&threads, exec_scan_for_ext_spring_forces, totthread);
1700
1701                 for (i=0; i<totthread; i++)
1702                         BLI_insert_thread(&threads, &sb_threads[i]);
1703
1704                 BLI_end_threads(&threads);
1705         }
1706         else
1707                 exec_scan_for_ext_spring_forces(&sb_threads[0]);
1708         /* clean up */
1709         MEM_freeN(sb_threads);
1710
1711         pdEndEffectors(&do_effector);
1712 }
1713
1714
1715 /* --- the spring external section*/
1716
1717 static int choose_winner(float*w, float* pos, float*a, float*b, float*c, float*ca, float*cb, float*cc)
1718 {
1719         float mindist, cp;
1720         int winner =1;
1721         mindist = ABS(dot_v3v3(pos, a));
1722
1723         cp = ABS(dot_v3v3(pos, b));
1724         if ( mindist < cp ) {
1725                 mindist = cp;
1726                 winner =2;
1727         }
1728
1729         cp = ABS(dot_v3v3(pos, c));
1730         if (mindist < cp ) {
1731                 mindist = cp;
1732                 winner =3;
1733         }
1734         switch (winner) {
1735                 case 1: copy_v3_v3(w, ca); break;
1736                 case 2: copy_v3_v3(w, cb); break;
1737                 case 3: copy_v3_v3(w, cc);
1738         }
1739         return(winner);
1740 }
1741
1742
1743
1744 static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3], float *damp,
1745                                                                          float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner,
1746                                                                          float time, float vel[3], float *intrusion)
1747 {
1748         Object *ob= NULL;
1749         GHash *hash;
1750         GHashIterator *ihash;
1751         float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3], d_nvect[3], dv1[3], ve[3], avel[3]={0.0, 0.0, 0.0},
1752               vv1[3], vv2[3], vv3[3], vv4[3], coledge[3]={0.0f, 0.0f, 0.0f}, mindistedge = 1000.0f,
1753               outerforceaccu[3], innerforceaccu[3],
1754               facedist, /* n_mag, */ /* UNUSED */ force_mag_norm, minx, miny, minz, maxx, maxy, maxz,
1755               innerfacethickness = -0.5f, outerfacethickness = 0.2f,
1756               ee = 5.0f, ff = 0.1f, fa=1;
1757         int a, deflected=0, cavel=0, ci=0;
1758 /* init */
1759         *intrusion = 0.0f;
1760         hash  = vertexowner->soft->scratch->colliderhash;
1761         ihash = BLI_ghashIterator_new(hash);
1762         outerforceaccu[0]=outerforceaccu[1]=outerforceaccu[2]=0.0f;
1763         innerforceaccu[0]=innerforceaccu[1]=innerforceaccu[2]=0.0f;
1764 /* go */
1765         while (!BLI_ghashIterator_isDone(ihash) ) {
1766
1767                 ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
1768                 ob             = BLI_ghashIterator_getKey       (ihash);
1769                         /* only with deflecting set */
1770                         if (ob->pd && ob->pd->deflect) {
1771                                 MFace *mface= NULL;
1772                                 MVert *mvert= NULL;
1773                                 MVert *mprevvert= NULL;
1774                                 ccdf_minmax *mima= NULL;
1775
1776                                 if (ccdm) {
1777                                         mface= ccdm->mface;
1778                                         mvert= ccdm->mvert;
1779                                         mprevvert= ccdm->mprevvert;
1780                                         mima= ccdm->mima;
1781                                         a = ccdm->totface;
1782
1783                                         minx = ccdm->bbmin[0];
1784                                         miny = ccdm->bbmin[1];
1785                                         minz = ccdm->bbmin[2];
1786
1787                                         maxx = ccdm->bbmax[0];
1788                                         maxy = ccdm->bbmax[1];
1789                                         maxz = ccdm->bbmax[2];
1790
1791                                         if ((opco[0] < minx) ||
1792                                             (opco[1] < miny) ||
1793                                             (opco[2] < minz) ||
1794                                             (opco[0] > maxx) ||
1795                                             (opco[1] > maxy) ||
1796                                             (opco[2] > maxz) )
1797                                         {
1798                                                 /* outside the padded boundbox --> collision object is too far away */
1799                                                 BLI_ghashIterator_step(ihash);
1800                                                 continue;
1801                                         }
1802                                 }
1803                                 else {
1804                                         /*aye that should be cached*/
1805                                         printf("missing cache error\n");
1806                                                 BLI_ghashIterator_step(ihash);
1807                                         continue;
1808                                 }
1809
1810                                 /* do object level stuff */
1811                                 /* need to have user control for that since it depends on model scale */
1812                                 innerfacethickness = -ob->pd->pdef_sbift;
1813                                 outerfacethickness =  ob->pd->pdef_sboft;
1814                                 fa = (ff*outerfacethickness-outerfacethickness);
1815                                 fa *= fa;
1816                                 fa = 1.0f/fa;
1817                                 avel[0]=avel[1]=avel[2]=0.0f;
1818                                 /* use mesh*/
1819                                 while (a--) {
1820                                         if (
1821                                                 (opco[0] < mima->minx) ||
1822                                                 (opco[0] > mima->maxx) ||
1823                                                 (opco[1] < mima->miny) ||
1824                                                 (opco[1] > mima->maxy) ||
1825                                                 (opco[2] < mima->minz) ||
1826                                                 (opco[2] > mima->maxz)
1827                                                 ) {
1828                                                         mface++;
1829                                                         mima++;
1830                                                         continue;
1831                                         }
1832
1833                                         if (mvert) {
1834
1835                                                 copy_v3_v3(nv1, mvert[mface->v1].co);
1836                                                 copy_v3_v3(nv2, mvert[mface->v2].co);
1837                                                 copy_v3_v3(nv3, mvert[mface->v3].co);
1838                                                 if (mface->v4) {
1839                                                         copy_v3_v3(nv4, mvert[mface->v4].co);
1840                                                 }
1841
1842                                                 if (mprevvert) {
1843                                                         /* grab the average speed of the collider vertices
1844                                                         before we spoil nvX
1845                                                         humm could be done once a SB steps but then we' need to store that too
1846                                                         since the AABB reduced propabitlty to get here drasticallly
1847                                                         it might be a nice tradeof CPU <--> memory
1848                                                         */
1849                                                         sub_v3_v3v3(vv1, nv1, mprevvert[mface->v1].co);
1850                                                         sub_v3_v3v3(vv2, nv2, mprevvert[mface->v2].co);
1851                                                         sub_v3_v3v3(vv3, nv3, mprevvert[mface->v3].co);
1852                                                         if (mface->v4) {
1853                                                                 sub_v3_v3v3(vv4, nv4, mprevvert[mface->v4].co);
1854                                                         }
1855
1856                                                         mul_v3_fl(nv1, time);
1857                                                         Vec3PlusStVec(nv1, (1.0f-time), mprevvert[mface->v1].co);
1858
1859                                                         mul_v3_fl(nv2, time);
1860                                                         Vec3PlusStVec(nv2, (1.0f-time), mprevvert[mface->v2].co);
1861
1862                                                         mul_v3_fl(nv3, time);
1863                                                         Vec3PlusStVec(nv3, (1.0f-time), mprevvert[mface->v3].co);
1864
1865                                                         if (mface->v4) {
1866                                                                 mul_v3_fl(nv4, time);
1867                                                                 Vec3PlusStVec(nv4, (1.0f-time), mprevvert[mface->v4].co);
1868                                                         }
1869                                                 }
1870                                         }
1871
1872                                         /* switch origin to be nv2*/
1873                                         sub_v3_v3v3(edge1, nv1, nv2);
1874                                         sub_v3_v3v3(edge2, nv3, nv2);
1875                                         sub_v3_v3v3(dv1, opco, nv2); /* abuse dv1 to have vertex in question at *origin* of triangle */
1876
1877                                         cross_v3_v3v3(d_nvect, edge2, edge1);
1878                                         /* n_mag = */ /* UNUSED */ normalize_v3(d_nvect);
1879                                         facedist = dot_v3v3(dv1, d_nvect);
1880                                         // so rules are
1881                                         //
1882
1883                                         if ((facedist > innerfacethickness) && (facedist < outerfacethickness)) {
1884                                                 if (isect_point_tri_prism_v3(opco, nv1, nv2, nv3) ) {
1885                                                         force_mag_norm =(float)exp(-ee*facedist);
1886                                                         if (facedist > outerfacethickness*ff)
1887                                                                 force_mag_norm =(float)force_mag_norm*fa*(facedist - outerfacethickness)*(facedist - outerfacethickness);
1888                                                         *damp=ob->pd->pdef_sbdamp;
1889                                                         if (facedist > 0.0f) {
1890                                                                 *damp *= (1.0f - facedist/outerfacethickness);
1891                                                                 Vec3PlusStVec(outerforceaccu, force_mag_norm, d_nvect);
1892                                                                 deflected = 3;
1893
1894                                                         }
1895                                                         else {
1896                                                                 Vec3PlusStVec(innerforceaccu, force_mag_norm, d_nvect);
1897                                                                 if (deflected < 2) deflected = 2;
1898                                                         }
1899                                                         if ((mprevvert) && (*damp > 0.0f)) {
1900                                                                 choose_winner(ve, opco, nv1, nv2, nv3, vv1, vv2, vv3);
1901                                                                 add_v3_v3(avel, ve);
1902                                                                 cavel ++;
1903                                                         }
1904                                                         *intrusion += facedist;
1905                                                         ci++;
1906                                                 }
1907                                         }
1908                                         if (mface->v4) { /* quad */
1909                                                 /* switch origin to be nv4 */
1910                                                 sub_v3_v3v3(edge1, nv3, nv4);
1911                                                 sub_v3_v3v3(edge2, nv1, nv4);
1912                                                 sub_v3_v3v3(dv1, opco, nv4); /* abuse dv1 to have vertex in question at *origin* of triangle */
1913
1914                                                 cross_v3_v3v3(d_nvect, edge2, edge1);
1915                                                 /* n_mag = */ /* UNUSED */ normalize_v3(d_nvect);
1916                                                 facedist = dot_v3v3(dv1, d_nvect);
1917
1918                                                 if ((facedist > innerfacethickness) && (facedist < outerfacethickness)) {
1919                                                         if (isect_point_tri_prism_v3(opco, nv1, nv3, nv4) ) {
1920                                                                 force_mag_norm =(float)exp(-ee*facedist);
1921                                                                 if (facedist > outerfacethickness*ff)
1922                                                                         force_mag_norm =(float)force_mag_norm*fa*(facedist - outerfacethickness)*(facedist - outerfacethickness);
1923                                                                 *damp=ob->pd->pdef_sbdamp;
1924                                                         if (facedist > 0.0f) {
1925                                                                 *damp *= (1.0f - facedist/outerfacethickness);
1926                                                                 Vec3PlusStVec(outerforceaccu, force_mag_norm, d_nvect);
1927                                                                 deflected = 3;
1928
1929                                                         }
1930                                                         else {
1931                                                                 Vec3PlusStVec(innerforceaccu, force_mag_norm, d_nvect);
1932                                                                 if (deflected < 2) deflected = 2;
1933                                                         }
1934
1935                                                                 if ((mprevvert) && (*damp > 0.0f)) {
1936                                                                         choose_winner(ve, opco, nv1, nv3, nv4, vv1, vv3, vv4);
1937                                                                         add_v3_v3(avel, ve);
1938                                                                         cavel ++;
1939                                                                 }
1940                                                                 *intrusion += facedist;
1941                                                                 ci++;
1942                                                         }
1943
1944                                                 }
1945                                                 if ((deflected < 2)&& (G.rt != 444)) { /* we did not hit a face until now */
1946                                                         /* see if 'outer' hits an edge */
1947                                                         float dist;
1948
1949                                                         closest_to_line_segment_v3(ve, opco, nv1, nv2);
1950                                                          sub_v3_v3v3(ve, opco, ve);
1951                                                         dist = normalize_v3(ve);
1952                                                         if ((dist < outerfacethickness)&&(dist < mindistedge )) {
1953                                                                 copy_v3_v3(coledge, ve);
1954                                                                 mindistedge = dist,
1955                                                                 deflected=1;
1956                                                         }
1957
1958                                                         closest_to_line_segment_v3(ve, opco, nv2, nv3);
1959                                                          sub_v3_v3v3(ve, opco, ve);
1960                                                         dist = normalize_v3(ve);
1961                                                         if ((dist < outerfacethickness)&&(dist < mindistedge )) {
1962                                                                 copy_v3_v3(coledge, ve);
1963                                                                 mindistedge = dist,
1964                                                                 deflected=1;
1965                                                         }
1966
1967                                                         closest_to_line_segment_v3(ve, opco, nv3, nv1);
1968                                                          sub_v3_v3v3(ve, opco, ve);
1969                                                         dist = normalize_v3(ve);
1970                                                         if ((dist < outerfacethickness)&&(dist < mindistedge )) {
1971                                                                 copy_v3_v3(coledge, ve);
1972                                                                 mindistedge = dist,
1973                                                                 deflected=1;
1974                                                         }
1975                                                         if (mface->v4) { /* quad */
1976                                                                 closest_to_line_segment_v3(ve, opco, nv3, nv4);
1977                                                                 sub_v3_v3v3(ve, opco, ve);
1978                                                                 dist = normalize_v3(ve);
1979                                                                 if ((dist < outerfacethickness)&&(dist < mindistedge )) {
1980                                                                         copy_v3_v3(coledge, ve);
1981                                                                         mindistedge = dist,
1982                                                                                 deflected=1;
1983                                                                 }
1984
1985                                                                 closest_to_line_segment_v3(ve, opco, nv1, nv4);
1986                                                                 sub_v3_v3v3(ve, opco, ve);
1987                                                                 dist = normalize_v3(ve);
1988                                                                 if ((dist < outerfacethickness)&&(dist < mindistedge )) {
1989                                                                         copy_v3_v3(coledge, ve);
1990                                                                         mindistedge = dist,
1991                                                                                 deflected=1;
1992                                                                 }
1993
1994                                                         }
1995
1996
1997                                                 }
1998                                         }
1999                                         mface++;
2000                                         mima++;
2001                                 }/* while a */
2002                         } /* if (ob->pd && ob->pd->deflect) */
2003                         BLI_ghashIterator_step(ihash);
2004         } /* while () */
2005
2006         if (deflected == 1) { // no face but 'outer' edge cylinder sees vert
2007                 force_mag_norm =(float)exp(-ee*mindistedge);
2008                 if (mindistedge > outerfacethickness*ff)
2009                         force_mag_norm =(float)force_mag_norm*fa*(mindistedge - outerfacethickness)*(mindistedge - outerfacethickness);
2010                 Vec3PlusStVec(force, force_mag_norm, coledge);
2011                 *damp=ob->pd->pdef_sbdamp;
2012                 if (mindistedge > 0.0f) {
2013                         *damp *= (1.0f - mindistedge/outerfacethickness);
2014                 }
2015
2016         }
2017         if (deflected == 2) { //  face inner detected
2018                 add_v3_v3(force, innerforceaccu);
2019         }
2020         if (deflected == 3) { //  face outer detected
2021                 add_v3_v3(force, outerforceaccu);
2022         }
2023
2024         BLI_ghashIterator_free(ihash);
2025         if (cavel) mul_v3_fl(avel, 1.0f/(float)cavel);
2026         copy_v3_v3(vel, avel);
2027         if (ci) *intrusion /= ci;
2028         if (deflected) {
2029                 normalize_v3_v3(facenormal, force);
2030         }
2031         return deflected;
2032 }
2033
2034
2035 /* sandbox to plug in various deflection algos */
2036 static int sb_deflect_face(Object *ob, float *actpos, float *facenormal, float *force, float *cf, float time, float *vel, float *intrusion)
2037 {
2038         float s_actpos[3];
2039         int deflected;
2040         copy_v3_v3(s_actpos, actpos);
2041         deflected= sb_detect_vertex_collisionCached(s_actpos, facenormal, cf, force, ob->lay, ob, time, vel, intrusion);
2042         //deflected= sb_detect_vertex_collisionCachedEx(s_actpos, facenormal, cf, force, ob->lay, ob, time, vel, intrusion);
2043         return(deflected);
2044 }
2045
2046 /* hiding this for now .. but the jacobian may pop up on other tasks .. so i'd like to keep it
2047 static void dfdx_spring(int ia, int ic, int op, float dir[3], float L, float len, float factor)
2048 {
2049         float m, delta_ij;
2050         int i, j;
2051         if (L < len) {
2052                 for (i=0;i<3;i++)
2053                         for (j=0;j<3;j++) {
2054                                 delta_ij = (i==j ? (1.0f): (0.0f));
2055                                 m=factor*(dir[i]*dir[j] + (1-L/len)*(delta_ij - dir[i]*dir[j]));
2056                                 nlMatrixAdd(ia+i, op+ic+j, m);
2057                         }
2058         }
2059         else {
2060                 for (i=0;i<3;i++)
2061                         for (j=0;j<3;j++) {
2062                                 m=factor*dir[i]*dir[j];
2063                                 nlMatrixAdd(ia+i, op+ic+j, m);
2064                         }
2065         }
2066 }
2067
2068
2069 static void dfdx_goal(int ia, int ic, int op, float factor)
2070 {
2071         int i;
2072         for (i=0;i<3;i++) nlMatrixAdd(ia+i, op+ic+i, factor);
2073 }
2074
2075 static void dfdv_goal(int ia, int ic, float factor)
2076 {
2077         int i;
2078         for (i=0;i<3;i++) nlMatrixAdd(ia+i, ic+i, factor);
2079 }
2080 */
2081 static void sb_spring_force(Object *ob, int bpi, BodySpring *bs, float iks, float UNUSED(forcetime), int nl_flags)
2082 {
2083         SoftBody *sb= ob->soft; /* is supposed to be there */
2084         BodyPoint  *bp1, *bp2;
2085
2086         float dir[3], dvel[3];
2087         float distance, forcefactor, kd, absvel, projvel, kw;
2088 #if 0   /* UNUSED */
2089         int ia, ic;
2090 #endif
2091         /* prepare depending on which side of the spring we are on */
2092         if (bpi == bs->v1) {
2093                 bp1 = &sb->bpoint[bs->v1];
2094                 bp2 = &sb->bpoint[bs->v2];
2095 #if 0   /* UNUSED */
2096                 ia =3*bs->v1;
2097                 ic =3*bs->v2;
2098 #endif
2099         }
2100         else if (bpi == bs->v2) {
2101                 bp1 = &sb->bpoint[bs->v2];
2102                 bp2 = &sb->bpoint[bs->v1];
2103 #if 0   /* UNUSED */
2104                 ia =3*bs->v2;
2105                 ic =3*bs->v1;
2106 #endif
2107         }
2108         else {
2109                 /* TODO make this debug option */
2110                 /**/
2111                 printf("bodypoint <bpi> is not attached to spring  <*bs> --> sb_spring_force()\n");
2112                 return;
2113         }
2114
2115         /* do bp1 <--> bp2 elastic */
2116         sub_v3_v3v3(dir, bp1->pos, bp2->pos);
2117         distance = normalize_v3(dir);
2118         if (bs->len < distance)
2119                 iks  = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
2120         else
2121                 iks  = 1.0f/(1.0f-sb->inpush)-1.0f ;/* inner spring constants function */
2122
2123         if (bs->len > 0.0f) /* check for degenerated springs */
2124                 forcefactor = iks/bs->len;
2125         else
2126                 forcefactor = iks;
2127         kw = (bp1->springweight+bp2->springweight)/2.0f;
2128         kw = kw * kw;
2129         kw = kw * kw;
2130         switch (bs->springtype) {
2131                 case SB_EDGE:
2132                 case SB_HANDLE:
2133                         forcefactor *=  kw;
2134                         break;
2135                 case SB_BEND:
2136                         forcefactor *=sb->secondspring*kw;
2137                         break;
2138                 case SB_STIFFQUAD:
2139                         forcefactor *=sb->shearstiff*sb->shearstiff* kw;
2140                         break;
2141                 default:
2142                         break;
2143         }
2144
2145
2146         Vec3PlusStVec(bp1->force, (bs->len - distance)*forcefactor, dir);
2147
2148         /* do bp1 <--> bp2 viscous */
2149         sub_v3_v3v3(dvel, bp1->vec, bp2->vec);
2150         kd = sb->infrict * sb_fric_force_scale(ob);
2151         absvel  = normalize_v3(dvel);
2152         projvel = dot_v3v3(dir, dvel);
2153         kd     *= absvel * projvel;
2154         Vec3PlusStVec(bp1->force, -kd, dir);
2155
2156         /* do jacobian stuff if needed */
2157         if (nl_flags & NLF_BUILD) {
2158                 //int op =3*sb->totpoint;
2159                 //float mvel = -forcetime*kd;
2160                 //float mpos = -forcetime*forcefactor;
2161                 /* depending on my pos */
2162                 // dfdx_spring(ia, ia, op, dir, bs->len, distance, -mpos);
2163                 /* depending on my vel */
2164                 // dfdv_goal(ia, ia, mvel); // well that ignores geometie
2165                 if (bp2->goal < SOFTGOALSNAP) { /* ommit this bp when it snaps */
2166                         /* depending on other pos */
2167                         // dfdx_spring(ia, ic, op, dir, bs->len, distance, mpos);
2168                         /* depending on other vel */
2169                         // dfdv_goal(ia, ia, -mvel); // well that ignores geometie
2170                 }
2171         }
2172 }
2173
2174
2175 /* since this is definitely the most CPU consuming task here .. try to spread it */
2176 /* core function _softbody_calc_forces_slice_in_a_thread */
2177 /* result is int to be able to flag user break */
2178 static int _softbody_calc_forces_slice_in_a_thread(Scene *scene, Object *ob, float forcetime, float timenow, int ifirst, int ilast, int *UNUSED(ptr_to_break_func(void)), ListBase *do_effector, int do_deflector, float fieldfactor, float windfactor)
2179 {
2180         float iks;
2181         int bb, do_selfcollision, do_springcollision, do_aero;
2182         int number_of_points_here = ilast - ifirst;
2183         SoftBody *sb= ob->soft; /* is supposed to be there */
2184         BodyPoint  *bp;
2185
2186         /* intitialize */
2187         if (sb) {
2188         /* check conditions for various options */
2189         /* +++ could be done on object level to squeeze out the last bits of it */
2190         do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF));
2191         do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL);
2192         do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES));
2193         /* --- could be done on object level to squeeze out the last bits of it */
2194         }
2195         else {
2196                 printf("Error expected a SB here\n");
2197                 return (999);
2198         }
2199
2200 /* debugerin */
2201         if (sb->totpoint < ifirst) {
2202                 printf("Aye 998");
2203                 return (998);
2204         }
2205 /* debugerin */
2206
2207
2208         bp = &sb->bpoint[ifirst];
2209         for (bb=number_of_points_here; bb>0; bb--, bp++) {
2210                 /* clear forces  accumulator */
2211                 bp->force[0]= bp->force[1]= bp->force[2]= 0.0;
2212                 /* naive ball self collision */
2213                 /* needs to be done if goal snaps or not */
2214                 if (do_selfcollision) {
2215                         int attached;
2216                         BodyPoint   *obp;
2217                         BodySpring *bs;
2218                         int c, b;
2219                         float velcenter[3], dvel[3], def[3];
2220                         float distance;
2221                         float compare;
2222                         float bstune = sb->ballstiff;
2223
2224                         for (c=sb->totpoint, obp= sb->bpoint; c>=ifirst+bb; c--, obp++) {
2225                                 compare = (obp->colball + bp->colball);
2226                                 sub_v3_v3v3(def, bp->pos, obp->pos);
2227                                 /* rather check the AABBoxes before ever calulating the real distance */
2228                                 /* mathematically it is completly nuts, but performance is pretty much (3) times faster */
2229                                 if ((ABS(def[0]) > compare) || (ABS(def[1]) > compare) || (ABS(def[2]) > compare)) continue;
2230                                 distance = normalize_v3(def);
2231                                 if (distance < compare ) {
2232                                         /* exclude body points attached with a spring */
2233                                         attached = 0;
2234                                         for (b=obp->nofsprings;b>0;b--) {
2235                                                 bs = sb->bspring + obp->springs[b-1];
2236                                                 if (( ilast-bb == bs->v2) || ( ilast-bb == bs->v1)) {
2237                                                         attached=1;
2238                                                         continue;}
2239                                         }
2240                                         if (!attached) {
2241                                                 float f = bstune / (distance) + bstune / (compare * compare) * distance - 2.0f * bstune / compare;
2242
2243                                                 mid_v3_v3v3(velcenter, bp->vec, obp->vec);
2244                                                 sub_v3_v3v3(dvel, velcenter, bp->vec);
2245                                                 mul_v3_fl(dvel, _final_mass(ob, bp));
2246
2247                                                 Vec3PlusStVec(bp->force, f*(1.0f-sb->balldamp), def);
2248                                                 Vec3PlusStVec(bp->force, sb->balldamp, dvel);
2249
2250                                                 /* exploit force(a, b) == -force(b, a) part2/2 */
2251                                                 sub_v3_v3v3(dvel, velcenter, obp->vec);
2252                                                 mul_v3_fl(dvel, _final_mass(ob, bp));
2253
2254                                                 Vec3PlusStVec(obp->force, sb->balldamp, dvel);
2255                                                 Vec3PlusStVec(obp->force, -f*(1.0f-sb->balldamp), def);
2256                                         }
2257                                 }
2258                         }
2259                 }
2260                 /* naive ball self collision done */
2261
2262                 if (_final_goal(ob, bp) < SOFTGOALSNAP) { /* ommit this bp when it snaps */
2263                         float auxvect[3];
2264                         float velgoal[3];
2265
2266                         /* do goal stuff */
2267                         if (ob->softflag & OB_SB_GOAL) {
2268                                 /* true elastic goal */
2269                                 float ks, kd;
2270                                 sub_v3_v3v3(auxvect, bp->pos, bp->origT);
2271                                 ks  = 1.0f / (1.0f - _final_goal(ob, bp) * sb->goalspring) - 1.0f;
2272                                 bp->force[0]+= -ks*(auxvect[0]);
2273                                 bp->force[1]+= -ks*(auxvect[1]);
2274                                 bp->force[2]+= -ks*(auxvect[2]);
2275
2276                                 /* calulate damping forces generated by goals*/
2277                                 sub_v3_v3v3(velgoal, bp->origS, bp->origE);
2278                                 kd =  sb->goalfrict * sb_fric_force_scale(ob);
2279                                 add_v3_v3v3(auxvect, velgoal, bp->vec);
2280
2281                                 if (forcetime > 0.0f) { /* make sure friction does not become rocket motor on time reversal */
2282                                         bp->force[0]-= kd * (auxvect[0]);
2283                                         bp->force[1]-= kd * (auxvect[1]);
2284                                         bp->force[2]-= kd * (auxvect[2]);
2285                                 }
2286                                 else {
2287                                         bp->force[0]-= kd * (velgoal[0] - bp->vec[0]);
2288                                         bp->force[1]-= kd * (velgoal[1] - bp->vec[1]);
2289                                         bp->force[2]-= kd * (velgoal[2] - bp->vec[2]);
2290                                 }
2291                         }
2292                         /* done goal stuff */
2293
2294                         /* gravitation */
2295                         if (sb && scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
2296                                 float gravity[3];
2297                                 copy_v3_v3(gravity, scene->physics_settings.gravity);
2298                                 mul_v3_fl(gravity, sb_grav_force_scale(ob)*_final_mass(ob, bp)*sb->effector_weights->global_gravity); /* individual mass of node here */
2299                                 add_v3_v3(bp->force, gravity);
2300                         }
2301
2302                         /* particle field & vortex */
2303                         if (do_effector) {
2304                                 EffectedPoint epoint;
2305                                 float kd;
2306                                 float force[3]= {0.0f, 0.0f, 0.0f};
2307                                 float speed[3]= {0.0f, 0.0f, 0.0f};
2308                                 float eval_sb_fric_force_scale = sb_fric_force_scale(ob); /* just for calling function once */
2309                                 pd_point_from_soft(scene, bp->pos, bp->vec, sb->bpoint-bp, &epoint);
2310                                 pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
2311
2312                                 /* apply forcefield*/
2313                                 mul_v3_fl(force, fieldfactor* eval_sb_fric_force_scale);
2314                                 add_v3_v3(bp->force, force);
2315
2316                                 /* BP friction in moving media */
2317                                 kd= sb->mediafrict* eval_sb_fric_force_scale;
2318                                 bp->force[0] -= kd * (bp->vec[0] + windfactor*speed[0]/eval_sb_fric_force_scale);
2319                                 bp->force[1] -= kd * (bp->vec[1] + windfactor*speed[1]/eval_sb_fric_force_scale);
2320                                 bp->force[2] -= kd * (bp->vec[2] + windfactor*speed[2]/eval_sb_fric_force_scale);
2321                                 /* now we'll have nice centrifugal effect for vortex */
2322
2323                         }
2324                         else {
2325                                 /* BP friction in media (not) moving*/
2326                                 float kd = sb->mediafrict* sb_fric_force_scale(ob);
2327                                 /* assume it to be proportional to actual velocity */
2328                                 bp->force[0]-= bp->vec[0]*kd;
2329                                 bp->force[1]-= bp->vec[1]*kd;
2330                                 bp->force[2]-= bp->vec[2]*kd;
2331                                 /* friction in media done */
2332                         }
2333                         /* +++cached collision targets */
2334                         bp->choke = 0.0f;
2335                         bp->choke2 = 0.0f;
2336                         bp->loc_flag &= ~SBF_DOFUZZY;
2337                         if (do_deflector && !(bp->loc_flag & SBF_OUTOFCOLLISION) ) {
2338                                 float cfforce[3], defforce[3] ={0.0f, 0.0f, 0.0f}, vel[3] = {0.0f, 0.0f, 0.0f}, facenormal[3], cf = 1.0f, intrusion;
2339                                 float kd = 1.0f;
2340
2341                                 if (sb_deflect_face(ob, bp->pos, facenormal, defforce, &cf, timenow, vel, &intrusion)) {
2342                                                 if (intrusion < 0.0f) {
2343                                                         sb->scratch->flag |= SBF_DOFUZZY;
2344                                                         bp->loc_flag |= SBF_DOFUZZY;
2345                                                         bp->choke = sb->choke*0.01f;
2346                                                 }
2347
2348                                                         sub_v3_v3v3(cfforce, bp->vec, vel);
2349                                                         Vec3PlusStVec(bp->force, -cf*50.0f, cfforce);
2350
2351                                         Vec3PlusStVec(bp->force, kd, defforce);
2352                                 }
2353
2354                         }
2355                         /* ---cached collision targets */
2356
2357                         /* +++springs */
2358                         iks  = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
2359                         if (ob->softflag & OB_SB_EDGES) {
2360                                 if (sb->bspring) { /* spring list exists at all ? */
2361                                         int b;
2362                                         BodySpring *bs;
2363                                         for (b=bp->nofsprings;b>0;b--) {
2364                                                 bs = sb->bspring + bp->springs[b-1];
2365                                                 if (do_springcollision || do_aero) {
2366                                                         add_v3_v3(bp->force, bs->ext_force);
2367                                                         if (bs->flag & BSF_INTERSECT)
2368                                                                 bp->choke = bs->cf;
2369
2370                                                 }
2371                                                 // sb_spring_force(Object *ob, int bpi, BodySpring *bs, float iks, float forcetime, int nl_flags)
2372                                                 sb_spring_force(ob, ilast-bb, bs, iks, forcetime, 0);
2373                                         }/* loop springs */
2374                                 }/* existing spring list */
2375                         }/*any edges*/
2376                         /* ---springs */
2377                 }/*omit on snap */
2378         }/*loop all bp's*/
2379         return 0; /*done fine*/
2380 }
2381
2382 static void *exec_softbody_calc_forces(void *data)
2383 {
2384         SB_thread_context *pctx = (SB_thread_context*)data;
2385         _softbody_calc_forces_slice_in_a_thread(pctx->scene, pctx->ob, pctx->forcetime, pctx->timenow, pctx->ifirst, pctx->ilast, NULL, pctx->do_effector, pctx->do_deflector, pctx->fieldfactor, pctx->windfactor);
2386         return NULL;
2387 }
2388
2389 static void sb_cf_threads_run(Scene *scene, Object *ob, float forcetime, float timenow, int totpoint, int *UNUSED(ptr_to_break_func(void)), struct ListBase *do_effector, int do_deflector, float fieldfactor, float windfactor)
2390 {
2391         ListBase threads;
2392         SB_thread_context *sb_threads;
2393         int i, totthread, left, dec;
2394         int lowpoints =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */
2395
2396         /* figure the number of threads while preventing pretty pointless threading overhead */
2397         if (scene->r.mode & R_FIXED_THREADS)
2398                 totthread= scene->r.threads;
2399         else
2400                 totthread= BLI_system_thread_count();
2401         /* what if we got zillions of CPUs running but less to spread*/
2402         while ((totpoint/totthread < lowpoints) && (totthread > 1)) {
2403                 totthread--;
2404         }
2405
2406         /* printf("sb_cf_threads_run spawning %d threads\n", totthread); */
2407
2408         sb_threads= MEM_callocN(sizeof(SB_thread_context)*totthread, "SBThread");
2409         memset(sb_threads, 0, sizeof(SB_thread_context)*totthread);
2410         left = totpoint;
2411         dec = totpoint/totthread +1;
2412         for (i=0; i<totthread; i++) {
2413                 sb_threads[i].scene = scene;
2414                 sb_threads[i].ob = ob;
2415                 sb_threads[i].forcetime = forcetime;
2416                 sb_threads[i].timenow = timenow;
2417                 sb_threads[i].ilast   = left;
2418                 left = left - dec;
2419                 if (left >0) {
2420                         sb_threads[i].ifirst  = left;
2421                 }
2422                 else
2423                         sb_threads[i].ifirst  = 0;
2424                 sb_threads[i].do_effector = do_effector;
2425                 sb_threads[i].do_deflector = do_deflector;
2426                 sb_threads[i].fieldfactor = fieldfactor;
2427                 sb_threads[i].windfactor  = windfactor;
2428                 sb_threads[i].nr= i;
2429                 sb_threads[i].tot= totthread;
2430         }
2431
2432
2433         if (totthread > 1) {
2434                 BLI_init_threads(&threads, exec_softbody_calc_forces, totthread);
2435
2436                 for (i=0; i<totthread; i++)
2437                         BLI_insert_thread(&threads, &sb_threads[i]);
2438
2439                 BLI_end_threads(&threads);
2440         }
2441         else
2442                 exec_softbody_calc_forces(&sb_threads[0]);
2443         /* clean up */
2444         MEM_freeN(sb_threads);
2445 }
2446
2447 static void softbody_calc_forcesEx(Scene *scene, Object *ob, float forcetime, float timenow, int UNUSED(nl_flags))
2448 {
2449 /* rule we never alter free variables :bp->vec bp->pos in here !
2450  * this will ruin adaptive stepsize AKA heun! (BM)
2451  */
2452         SoftBody *sb= ob->soft; /* is supposed to be there */
2453         /*BodyPoint *bproot;*/ /* UNUSED */
2454         ListBase *do_effector = NULL;
2455         /* float gravity; */ /* UNUSED */
2456         /* float iks; */
2457         float fieldfactor = -1.0f, windfactor  = 0.25;
2458         int   do_deflector /*, do_selfcollision*/, do_springcollision, do_aero;
2459
2460         /* gravity = sb->grav * sb_grav_force_scale(ob); */ /* UNUSED */
2461
2462         /* check conditions for various options */
2463         do_deflector= query_external_colliders(scene, ob);
2464         /* do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF)); */ /* UNUSED */
2465         do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL);
2466         do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES));
2467
2468         /* iks  = 1.0f/(1.0f-sb->inspring)-1.0f; */ /* inner spring constants function */ /* UNUSED */
2469         /* bproot= sb->bpoint; */ /* need this for proper spring addressing */ /* UNUSED */
2470
2471         if (do_springcollision || do_aero)
2472         sb_sfesf_threads_run(scene, ob, timenow, sb->totspring, NULL);
2473
2474         /* after spring scan because it uses Effoctors too */
2475         do_effector= pdInitEffectors(scene, ob, NULL, sb->effector_weights);
2476
2477         if (do_deflector) {
2478                 float defforce[3];
2479                 do_deflector = sb_detect_aabb_collisionCached(defforce, ob->lay, ob, timenow);
2480         }
2481
2482         sb_cf_threads_run(scene, ob, forcetime, timenow, sb->totpoint, NULL, do_effector, do_deflector, fieldfactor, windfactor);
2483
2484         /* finally add forces caused by face collision */
2485         if (ob->softflag & OB_SB_FACECOLL) scan_for_ext_face_forces(ob, timenow);
2486
2487         /* finish matrix and solve */
2488         pdEndEffectors(&do_effector);
2489 }
2490
2491
2492
2493
2494 static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, float timenow, int nl_flags)
2495 {
2496         /* redirection to the new threaded Version */
2497         if (!(G.rt & 0x10)) { // 16
2498                 softbody_calc_forcesEx(scene, ob, forcetime, timenow, nl_flags);
2499                 return;
2500         }
2501         else {
2502                 /* so the following will die  */
2503                 /* |||||||||||||||||||||||||| */
2504                 /* VVVVVVVVVVVVVVVVVVVVVVVVVV */
2505                 /*backward compatibility note:
2506                 fixing bug [17428] which forces adaptive step size to tiny steps
2507                 in some situations
2508                 .. keeping G.rt==17 0x11 option for old files 'needing' the bug*/
2509
2510                 /* rule we never alter free variables :bp->vec bp->pos in here !
2511                 * this will ruin adaptive stepsize AKA heun! (BM)
2512                 */
2513                 SoftBody *sb= ob->soft; /* is supposed to be there */
2514                 BodyPoint  *bp;
2515                 /* BodyPoint *bproot; */ /* UNUSED */
2516                 BodySpring *bs;
2517                 ListBase *do_effector = NULL;
2518                 float iks, ks, kd, gravity[3] = {0.0f, 0.0f, 0.0f};
2519                 float fieldfactor = -1.0f, windfactor  = 0.25f;
2520                 float tune = sb->ballstiff;
2521                 int a, b,  do_deflector, do_selfcollision, do_springcollision, do_aero;
2522
2523
2524                 /* jacobian
2525                 NLboolean success;
2526
2527                 if (nl_flags) {
2528                 nlBegin(NL_SYSTEM);
2529                 nlBegin(NL_MATRIX);
2530                 }
2531                 */
2532
2533
2534                 if (scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
2535                         copy_v3_v3(gravity, scene->physics_settings.gravity);
2536                         mul_v3_fl(gravity, sb_grav_force_scale(ob)*sb->effector_weights->global_gravity);
2537                 }
2538
2539                 /* check conditions for various options */
2540                 do_deflector= query_external_colliders(scene, ob);
2541                 do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF));
2542                 do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL);
2543                 do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES));
2544
2545                 iks  = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
2546                 /* bproot= sb->bpoint; */ /* need this for proper spring addressing */ /* UNUSED */
2547
2548                 if (do_springcollision || do_aero)  scan_for_ext_spring_forces(scene, ob, timenow);
2549                 /* after spring scan because it uses Effoctors too */
2550                 do_effector= pdInitEffectors(scene, ob, NULL, ob->soft->effector_weights);
2551
2552                 if (do_deflector) {
2553                         float defforce[3];
2554                         do_deflector = sb_detect_aabb_collisionCached(defforce, ob->lay, ob, timenow);
2555                 }
2556
2557                 for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
2558                         /* clear forces  accumulator */
2559                         bp->force[0]= bp->force[1]= bp->force[2]= 0.0;
2560                         if (nl_flags & NLF_BUILD) {
2561                                 //int ia =3*(sb->totpoint-a);
2562                                 //int op =3*sb->totpoint;
2563                                 /* dF/dV = v */
2564                                 /* jacobioan
2565                                 nlMatrixAdd(op+ia, ia, -forcetime);
2566                                 nlMatrixAdd(op+ia+1, ia+1, -forcetime);
2567                                 nlMatrixAdd(op+ia+2, ia+2, -forcetime);
2568
2569                                 nlMatrixAdd(ia, ia, 1);
2570                                 nlMatrixAdd(ia+1, ia+1, 1);
2571                                 nlMatrixAdd(ia+2, ia+2, 1);
2572
2573                                 nlMatrixAdd(op+ia, op+ia, 1);
2574                                 nlMatrixAdd(op+ia+1, op+ia+1, 1);
2575                                 nlMatrixAdd(op+ia+2, op+ia+2, 1);
2576                                 */
2577
2578
2579                         }
2580
2581                         /* naive ball self collision */
2582                         /* needs to be done if goal snaps or not */
2583                         if (do_selfcollision) {
2584                                 int attached;
2585                                 BodyPoint   *obp;
2586                                 int c, b;
2587                                 float velcenter[3], dvel[3], def[3];
2588                                 float distance;
2589                                 float compare;
2590
2591                                 for (c=sb->totpoint, obp= sb->bpoint; c>=a; c--, obp++) {
2592
2593                                         //if ((bp->octantflag & obp->octantflag) == 0) continue;
2594
2595                                         compare = (obp->colball + bp->colball);
2596                                         sub_v3_v3v3(def, bp->pos, obp->pos);
2597
2598                                         /* rather check the AABBoxes before ever calulating the real distance */
2599                                         /* mathematically it is completly nuts, but performance is pretty much (3) times faster */
2600                                         if ((ABS(def[0]) > compare) || (ABS(def[1]) > compare) || (ABS(def[2]) > compare)) continue;
2601
2602                                         distance = normalize_v3(def);
2603                                         if (distance < compare ) {
2604                                                 /* exclude body points attached with a spring */
2605                                                 attached = 0;
2606                                                 for (b=obp->nofsprings;b>0;b--) {
2607                                                         bs = sb->bspring + obp->springs[b-1];
2608                                                         if (( sb->totpoint-a == bs->v2) || ( sb->totpoint-a == bs->v1)) {
2609                                                                 attached=1;
2610                                                                 continue;}
2611                                                 }
2612                                                 if (!attached) {
2613                                                         float f = tune / (distance) + tune / (compare * compare) * distance - 2.0f * tune/compare;
2614
2615                                                         mid_v3_v3v3(velcenter, bp->vec, obp->vec);
2616                                                         sub_v3_v3v3(dvel, velcenter, bp->vec);
2617                                                         mul_v3_fl(dvel, _final_mass(ob, bp));
2618
2619                                                         Vec3PlusStVec(bp->force, f*(1.0f-sb->balldamp), def);
2620                                                         Vec3PlusStVec(bp->force, sb->balldamp, dvel);
2621
2622                                                         if (nl_flags & NLF_BUILD) {
2623                                                                 //int ia =3*(sb->totpoint-a);
2624                                                                 //int ic =3*(sb->totpoint-c);
2625                                                                 //int op =3*sb->totpoint;
2626                                                                 //float mvel = forcetime*sb->nodemass*sb->balldamp;
2627                                                                 //float mpos = forcetime*tune*(1.0f-sb->balldamp);
2628                                                                 /*some quick and dirty entries to the jacobian*/
2629                                                                 //dfdx_goal(ia, ia, op, mpos);
2630                                                                 //dfdv_goal(ia, ia, mvel);
2631                                                                 /* exploit force(a, b) == -force(b, a) part1/2 */
2632                                                                 //dfdx_goal(ic, ic, op, mpos);
2633                                                                 //dfdv_goal(ic, ic, mvel);
2634
2635
2636                                                                 /*TODO sit down an X-out the true jacobian entries*/
2637                                                                 /*well does not make to much sense because the eigenvalues
2638                                                                 of the jacobian go negative; and negative eigenvalues
2639                                                                 on a complex iterative system z(n+1)=A * z(n)
2640                                                                 give imaginary roots in the charcateristic polynom
2641                                                                 --> solutions that to z(t)=u(t)* exp ( i omega t) --> oscilations we don't want here
2642                                                                 where u(t) is a unknown amplitude function (worst case rising fast)
2643                                                                 */
2644                                                         }
2645
2646                                                         /* exploit force(a, b) == -force(b, a) part2/2 */
2647                                                         sub_v3_v3v3(dvel, velcenter, obp->vec);
2648                                                         mul_v3_fl(dvel, (_final_mass(ob, bp)+_final_mass(ob, obp))/2.0f);
2649
2650                                                         Vec3PlusStVec(obp->force, sb->balldamp, dvel);
2651                                                         Vec3PlusStVec(obp->force, -f*(1.0f-sb->balldamp), def);
2652
2653
2654                                                 }
2655                                         }
2656                                 }
2657                         }
2658                         /* naive ball self collision done */
2659
2660                         if (_final_goal(ob, bp) < SOFTGOALSNAP) { /* ommit this bp when it snaps */
2661                                 float auxvect[3];
2662                                 float velgoal[3];
2663
2664                                 /* do goal stuff */
2665                                 if (ob->softflag & OB_SB_GOAL) {
2666                                         /* true elastic goal */
2667                                         sub_v3_v3v3(auxvect, bp->pos, bp->origT);
2668                                         ks  = 1.0f / (1.0f- _final_goal(ob, bp) * sb->goalspring) - 1.0f;
2669                                         bp->force[0]+= -ks*(auxvect[0]);
2670                                         bp->force[1]+= -ks*(auxvect[1]);
2671                                         bp->force[2]+= -ks*(auxvect[2]);
2672
2673                                         if (nl_flags & NLF_BUILD) {
2674                                                 //int ia =3*(sb->totpoint-a);
2675                                                 //int op =3*(sb->totpoint);
2676                                                 /* depending on my pos */
2677                                                 //dfdx_goal(ia, ia, op, ks*forcetime);
2678                                         }
2679
2680
2681                                         /* calulate damping forces generated by goals*/
2682                                         sub_v3_v3v3(velgoal, bp->origS, bp->origE);
2683                                         kd = sb->goalfrict * sb_fric_force_scale(ob);
2684                                         add_v3_v3v3(auxvect, velgoal, bp->vec);
2685
2686                                         if (forcetime > 0.0f) { /* make sure friction does not become rocket motor on time reversal */
2687                                                 bp->force[0]-= kd * (auxvect[0]);
2688                                                 bp->force[1]-= kd * (auxvect[1]);
2689                                                 bp->force[2]-= kd * (auxvect[2]);
2690                                                 if (nl_flags & NLF_BUILD) {
2691                                                         //int ia =3*(sb->totpoint-a);
2692                                                         normalize_v3(auxvect);
2693                                                         /* depending on my vel */
2694                                                         //dfdv_goal(ia, ia, kd*forcetime);
2695                                                 }
2696
2697                                         }
2698                                         else {
2699                                                 bp->force[0]-= kd * (velgoal[0] - bp->vec[0]);
2700                                                 bp->force[1]-= kd * (velgoal[1] - bp->vec[1]);
2701                                                 bp->force[2]-= kd * (velgoal[2] - bp->vec[2]);
2702                                         }
2703                                 }
2704                                 /* done goal stuff */
2705
2706
2707                                 /* gravitation */
2708                                 madd_v3_v3fl(bp->force, gravity, _final_mass(ob, bp)); /* individual mass of node here */
2709
2710
2711                                 /* particle field & vortex */
2712                                 if (do_effector) {
2713                                         EffectedPoint epoint;
2714                                         float force[3]= {0.0f, 0.0f, 0.0f};
2715                                         float speed[3]= {0.0f, 0.0f, 0.0f};
2716                                         float eval_sb_fric_force_scale = sb_fric_force_scale(ob); /* just for calling function once */
2717                                         pd_point_from_soft(scene, bp->pos, bp->vec, sb->bpoint-bp, &epoint);
2718                                         pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
2719
2720                                         /* apply forcefield*/
2721                                         mul_v3_fl(force, fieldfactor* eval_sb_fric_force_scale);
2722                                         add_v3_v3(bp->force, force);
2723
2724                                         /* BP friction in moving media */
2725                                         kd= sb->mediafrict* eval_sb_fric_force_scale;
2726                                         bp->force[0] -= kd * (bp->vec[0] + windfactor*speed[0]/eval_sb_fric_force_scale);
2727                                         bp->force[1] -= kd * (bp->vec[1] + windfactor*speed[1]/eval_sb_fric_force_scale);
2728                                         bp->force[2] -= kd * (bp->v