Code cleanup: remove unused includes
[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_pointcache.h"
79 #include "BKE_deform.h"
80 #include "BKE_mesh.h"
81 #include "BKE_scene.h"
82
83 #include  "PIL_time.h"
84 // #include  "ONL_opennl.h" remove linking to ONL for now
85
86 /* callbacks for errors and interrupts and some goo */
87 static int (*SB_localInterruptCallBack)(void) = NULL;
88
89
90 /* ********** soft body engine ******* */
91
92 typedef enum {SB_EDGE=1, SB_BEND=2, SB_STIFFQUAD=3, SB_HANDLE=4} type_spring;
93
94 typedef struct BodySpring {
95         int v1, v2;
96         float len, cf, load;
97         float ext_force[3]; /* edges colliding and sailing */
98         type_spring springtype;
99         short flag;
100 } BodySpring;
101
102 typedef struct BodyFace {
103         int v1, v2, v3, v4;
104         float ext_force[3]; /* faces colliding */
105         short flag;
106 } BodyFace;
107
108 typedef struct ReferenceVert {
109         float pos[3]; /* position relative to com */
110         float mass;   /* node mass */
111 } ReferenceVert;
112
113 typedef struct ReferenceState {
114         float com[3]; /* center of mass*/
115         ReferenceVert *ivert; /* list of intial values */
116 } ReferenceState;
117
118
119 /*private scratch pad for caching and other data only needed when alive*/
120 typedef struct SBScratch {
121         GHash *colliderhash;
122         short needstobuildcollider;
123         short flag;
124         BodyFace *bodyface;
125         int totface;
126         float aabbmin[3], aabbmax[3];
127         ReferenceState Ref;
128 } SBScratch;
129
130 typedef struct  SB_thread_context {
131                 Scene *scene;
132                 Object *ob;
133                 float forcetime;
134                 float timenow;
135                 int ifirst;
136                 int ilast;
137                 ListBase *do_effector;
138                 int do_deflector;
139                 float fieldfactor;
140                 float windfactor;
141                 int nr;
142                 int tot;
143 } SB_thread_context;
144
145 #define NLF_BUILD  1
146 #if 0
147 #  define NLF_SOLVE  2
148 #endif
149
150 #define MID_PRESERVE 1
151
152 #define SOFTGOALSNAP  0.999f
153 /* if bp-> goal is above make it a *forced follow original* and skip all ODE stuff for this bp
154  * removes *unnecessary* stiffness from ODE system
155  */
156 #define HEUNWARNLIMIT 1 /* 500 would be fine i think for detecting severe *stiff* stuff */
157
158
159 #define BSF_INTERSECT   1 /* edge intersects collider face */
160
161 /* private definitions for bodypoint states */
162 #define SBF_DOFUZZY          1 /* Bodypoint do fuzzy */
163 #define SBF_OUTOFCOLLISION   2 /* Bodypoint does not collide  */
164
165
166 #define BFF_INTERSECT   1 /* collider edge   intrudes face */
167 #define BFF_CLOSEVERT   2 /* collider vertex repulses face */
168
169
170 static float SoftHeunTol = 1.0f; /* humm .. this should be calculated from sb parameters and sizes */
171
172 /* local prototypes */
173 static void free_softbody_intern(SoftBody *sb);
174 /* aye this belongs to arith.c */
175 static void Vec3PlusStVec(float *v, float s, float *v1);
176
177 /*+++ frame based timing +++*/
178
179 /*physical unit of force is [kg * m / sec^2]*/
180
181 static float sb_grav_force_scale(Object *UNUSED(ob))
182 /* since unit of g is [m/sec^2] and F = mass * g we rescale unit mass of node to 1 gramm
183  * put it to a function here, so we can add user options later without touching simulation code
184  */
185 {
186         return (0.001f);
187 }
188
189 static float sb_fric_force_scale(Object *UNUSED(ob))
190 /* rescaling unit of drag [1 / sec] to somehow reasonable
191  * put it to a function here, so we can add user options later without touching simulation code
192  */
193 {
194         return (0.01f);
195 }
196
197 static float sb_time_scale(Object *ob)
198 /* defining the frames to *real* time relation */
199 {
200         SoftBody *sb= ob->soft; /* is supposed to be there */
201         if (sb) {
202                 return(sb->physics_speed);
203                 /* hrms .. this could be IPO as well :)
204                  * estimated range [0.001 sluggish slug - 100.0 very fast (i hope ODE solver can handle that)]
205                  * 1 approx = a unit 1 pendulum at g = 9.8 [earth conditions]  has period 65 frames
206                  * theory would give a 50 frames period .. so there must be something inaccurate .. looking for that (BM)
207                  */
208         }
209         return (1.0f);
210         /*
211          * this would be frames/sec independent timing assuming 25 fps is default
212          * but does not work very well with NLA
213          * return (25.0f/scene->r.frs_sec)
214          */
215 }
216 /*--- frame based timing ---*/
217
218 /* helper functions for everything is animatable jow_go_for2_5 +++++++*/
219 /* introducing them here, because i know: steps in properties  ( at frame timing )
220  * will cause unwanted responses of the softbody system (which does inter frame calculations )
221  * so first 'cure' would be: interpolate linear in time ..
222  * Q: why do i write this?
223  * A: because it happend once, that some eger coder 'streamlined' code to fail.
224  * We DO linear interpolation for goals .. and i think we should do on animated properties as well
225  */
226
227 /* animate sb->maxgoal, sb->mingoal */
228 static float _final_goal(Object *ob, BodyPoint *bp)/*jow_go_for2_5 */
229 {
230         float f = -1999.99f;
231         if (ob) {
232                 SoftBody *sb= ob->soft; /* is supposed to be there */
233                 if (!(ob->softflag & OB_SB_GOAL)) return (0.0f);
234                 if (sb&&bp) {
235                         if (bp->goal < 0.0f) return (0.0f);
236                         f = sb->mingoal + bp->goal * fabsf(sb->maxgoal - sb->mingoal);
237                         f = pow(f, 4.0f);
238                         return (f);
239                 }
240         }
241         printf("_final_goal failed! sb or bp ==NULL\n");
242         return f; /*using crude but spot able values some times helps debuggin */
243 }
244
245 static float _final_mass(Object *ob, BodyPoint *bp)
246 {
247         if (ob) {
248                 SoftBody *sb= ob->soft; /* is supposed to be there */
249                 if (sb&&bp) {
250                         return(bp->mass*sb->nodemass);
251                 }
252         }
253         printf("_final_mass failed! sb or bp ==NULL\n");
254         return 1.0f;
255 }
256 /* helper functions for everything is animateble jow_go_for2_5 ------*/
257
258 /*+++ collider caching and dicing +++*/
259
260 /********************
261 for each target object/face the axis aligned bounding box (AABB) is stored
262 faces parallel to global axes
263 so only simple "value" in [min, max] ckecks are used
264 float operations still
265 */
266
267 /* just an ID here to reduce the prob for killing objects
268 ** ob->sumohandle points to we should not kill :)
269 */
270 static const int CCD_SAVETY = 190561;
271
272 typedef struct ccdf_minmax {
273         float minx, miny, minz, maxx, maxy, maxz;
274 } ccdf_minmax;
275
276
277
278 typedef struct ccd_Mesh {
279         int totvert, totface;
280         MVert *mvert;
281         MVert *mprevvert;
282         MFace *mface;
283         int savety;
284         ccdf_minmax *mima;
285         /* Axis Aligned Bounding Box AABB */
286         float bbmin[3];
287         float bbmax[3];
288 } ccd_Mesh;
289
290
291
292
293 static ccd_Mesh *ccd_mesh_make(Object *ob)
294 {
295         CollisionModifierData *cmd;
296         ccd_Mesh *pccd_M = NULL;
297         ccdf_minmax *mima = NULL;
298         MFace *mface=NULL;
299         float v[3], hull;
300         int i;
301
302         cmd =(CollisionModifierData *)modifiers_findByType(ob, eModifierType_Collision);
303
304         /* first some paranoia checks */
305         if (!cmd) return NULL;
306         if (!cmd->numverts || !cmd->numfaces) return NULL;
307
308         pccd_M = MEM_mallocN(sizeof(ccd_Mesh), "ccd_Mesh");
309         pccd_M->totvert = cmd->numverts;
310         pccd_M->totface = cmd->numfaces;
311         pccd_M->savety  = CCD_SAVETY;
312         pccd_M->bbmin[0]=pccd_M->bbmin[1]=pccd_M->bbmin[2]=1e30f;
313         pccd_M->bbmax[0]=pccd_M->bbmax[1]=pccd_M->bbmax[2]=-1e30f;
314         pccd_M->mprevvert=NULL;
315
316
317         /* blow it up with forcefield ranges */
318         hull = max_ff(ob->pd->pdef_sbift, ob->pd->pdef_sboft);
319
320         /* alloc and copy verts*/
321         pccd_M->mvert = MEM_dupallocN(cmd->xnew);
322         /* note that xnew coords are already in global space, */
323         /* determine the ortho BB */
324         for (i=0; i < pccd_M->totvert; i++) {
325                 /* evaluate limits */
326                 copy_v3_v3(v, pccd_M->mvert[i].co);
327                 pccd_M->bbmin[0] = min_ff(pccd_M->bbmin[0], v[0] - hull);
328                 pccd_M->bbmin[1] = min_ff(pccd_M->bbmin[1], v[1] - hull);
329                 pccd_M->bbmin[2] = min_ff(pccd_M->bbmin[2], v[2] - hull);
330
331                 pccd_M->bbmax[0] = max_ff(pccd_M->bbmax[0], v[0] + hull);
332                 pccd_M->bbmax[1] = max_ff(pccd_M->bbmax[1], v[1] + hull);
333                 pccd_M->bbmax[2] = max_ff(pccd_M->bbmax[2], v[2] + hull);
334
335         }
336         /* alloc and copy faces*/
337         pccd_M->mface = MEM_dupallocN(cmd->mfaces);
338
339         /* OBBs for idea1 */
340         pccd_M->mima = MEM_mallocN(sizeof(ccdf_minmax)*pccd_M->totface, "ccd_Mesh_Faces_mima");
341         mima  = pccd_M->mima;
342         mface = pccd_M->mface;
343
344
345         /* anyhoo we need to walk the list of faces and find OBB they live in */
346         for (i=0; i < pccd_M->totface; i++) {
347                 mima->minx=mima->miny=mima->minz=1e30f;
348                 mima->maxx=mima->maxy=mima->maxz=-1e30f;
349
350                 copy_v3_v3(v, pccd_M->mvert[mface->v1].co);
351                 mima->minx = min_ff(mima->minx, v[0] - hull);
352                 mima->miny = min_ff(mima->miny, v[1] - hull);
353                 mima->minz = min_ff(mima->minz, v[2] - hull);
354                 mima->maxx = max_ff(mima->maxx, v[0] + hull);
355                 mima->maxy = max_ff(mima->maxy, v[1] + hull);
356                 mima->maxz = max_ff(mima->maxz, v[2] + hull);
357
358                 copy_v3_v3(v, pccd_M->mvert[mface->v2].co);
359                 mima->minx = min_ff(mima->minx, v[0] - hull);
360                 mima->miny = min_ff(mima->miny, v[1] - hull);
361                 mima->minz = min_ff(mima->minz, v[2] - hull);
362                 mima->maxx = max_ff(mima->maxx, v[0] + hull);
363                 mima->maxy = max_ff(mima->maxy, v[1] + hull);
364                 mima->maxz = max_ff(mima->maxz, v[2] + hull);
365
366                 copy_v3_v3(v, pccd_M->mvert[mface->v3].co);
367                 mima->minx = min_ff(mima->minx, v[0] - hull);
368                 mima->miny = min_ff(mima->miny, v[1] - hull);
369                 mima->minz = min_ff(mima->minz, v[2] - hull);
370                 mima->maxx = max_ff(mima->maxx, v[0] + hull);
371                 mima->maxy = max_ff(mima->maxy, v[1] + hull);
372                 mima->maxz = max_ff(mima->maxz, v[2] + hull);
373
374                 if (mface->v4) {
375                         copy_v3_v3(v, pccd_M->mvert[mface->v4].co);
376                         mima->minx = min_ff(mima->minx, v[0] - hull);
377                         mima->miny = min_ff(mima->miny, v[1] - hull);
378                         mima->minz = min_ff(mima->minz, v[2] - hull);
379                         mima->maxx = max_ff(mima->maxx, v[0] + hull);
380                         mima->maxy = max_ff(mima->maxy, v[1] + hull);
381                         mima->maxz = max_ff(mima->maxz, v[2] + hull);
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 = max_ff(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] = min_ff(pccd_M->bbmin[0], v[0] - hull);
428                 pccd_M->bbmin[1] = min_ff(pccd_M->bbmin[1], v[1] - hull);
429                 pccd_M->bbmin[2] = min_ff(pccd_M->bbmin[2], v[2] - hull);
430
431                 pccd_M->bbmax[0] = max_ff(pccd_M->bbmax[0], v[0] + hull);
432                 pccd_M->bbmax[1] = max_ff(pccd_M->bbmax[1], v[1] + hull);
433                 pccd_M->bbmax[2] = max_ff(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] = min_ff(pccd_M->bbmin[0], v[0] - hull);
438                 pccd_M->bbmin[1] = min_ff(pccd_M->bbmin[1], v[1] - hull);
439                 pccd_M->bbmin[2] = min_ff(pccd_M->bbmin[2], v[2] - hull);
440
441                 pccd_M->bbmax[0] = max_ff(pccd_M->bbmax[0], v[0] + hull);
442                 pccd_M->bbmax[1] = max_ff(pccd_M->bbmax[1], v[1] + hull);
443                 pccd_M->bbmax[2] = max_ff(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 = min_ff(mima->minx, v[0] - hull);
458                 mima->miny = min_ff(mima->miny, v[1] - hull);
459                 mima->minz = min_ff(mima->minz, v[2] - hull);
460                 mima->maxx = max_ff(mima->maxx, v[0] + hull);
461                 mima->maxy = max_ff(mima->maxy, v[1] + hull);
462                 mima->maxz = max_ff(mima->maxz, v[2] + hull);
463
464                 copy_v3_v3(v, pccd_M->mvert[mface->v2].co);
465                 mima->minx = min_ff(mima->minx, v[0] - hull);
466                 mima->miny = min_ff(mima->miny, v[1] - hull);
467                 mima->minz = min_ff(mima->minz, v[2] - hull);
468                 mima->maxx = max_ff(mima->maxx, v[0] + hull);
469                 mima->maxy = max_ff(mima->maxy, v[1] + hull);
470                 mima->maxz = max_ff(mima->maxz, v[2] + hull);
471
472                 copy_v3_v3(v, pccd_M->mvert[mface->v3].co);
473                 mima->minx = min_ff(mima->minx, v[0] - hull);
474                 mima->miny = min_ff(mima->miny, v[1] - hull);
475                 mima->minz = min_ff(mima->minz, v[2] - hull);
476                 mima->maxx = max_ff(mima->maxx, v[0] + hull);
477                 mima->maxy = max_ff(mima->maxy, v[1] + hull);
478                 mima->maxz = max_ff(mima->maxz, v[2] + hull);
479
480                 if (mface->v4) {
481                         copy_v3_v3(v, pccd_M->mvert[mface->v4].co);
482                         mima->minx = min_ff(mima->minx, v[0] - hull);
483                         mima->miny = min_ff(mima->miny, v[1] - hull);
484                         mima->minz = min_ff(mima->minz, v[2] - hull);
485                         mima->maxx = max_ff(mima->maxx, v[0] + hull);
486                         mima->maxy = max_ff(mima->maxy, v[1] + hull);
487                         mima->maxz = max_ff(mima->maxz, v[2] + hull);
488                 }
489
490
491                 copy_v3_v3(v, pccd_M->mprevvert[mface->v1].co);
492                 mima->minx = min_ff(mima->minx, v[0] - hull);
493                 mima->miny = min_ff(mima->miny, v[1] - hull);
494                 mima->minz = min_ff(mima->minz, v[2] - hull);
495                 mima->maxx = max_ff(mima->maxx, v[0] + hull);
496                 mima->maxy = max_ff(mima->maxy, v[1] + hull);
497                 mima->maxz = max_ff(mima->maxz, v[2] + hull);
498
499                 copy_v3_v3(v, pccd_M->mprevvert[mface->v2].co);
500                 mima->minx = min_ff(mima->minx, v[0] - hull);
501                 mima->miny = min_ff(mima->miny, v[1] - hull);
502                 mima->minz = min_ff(mima->minz, v[2] - hull);
503                 mima->maxx = max_ff(mima->maxx, v[0] + hull);
504                 mima->maxy = max_ff(mima->maxy, v[1] + hull);
505                 mima->maxz = max_ff(mima->maxz, v[2] + hull);
506
507                 copy_v3_v3(v, pccd_M->mprevvert[mface->v3].co);
508                 mima->minx = min_ff(mima->minx, v[0] - hull);
509                 mima->miny = min_ff(mima->miny, v[1] - hull);
510                 mima->minz = min_ff(mima->minz, v[2] - hull);
511                 mima->maxx = max_ff(mima->maxx, v[0] + hull);
512                 mima->maxy = max_ff(mima->maxy, v[1] + hull);
513                 mima->maxz = max_ff(mima->maxz, v[2] + hull);
514
515                 if (mface->v4) {
516                         copy_v3_v3(v, pccd_M->mprevvert[mface->v4].co);
517                         mima->minx = min_ff(mima->minx, v[0] - hull);
518                         mima->miny = min_ff(mima->miny, v[1] - hull);
519                         mima->minz = min_ff(mima->minz, v[2] - hull);
520                         mima->maxx = max_ff(mima->maxx, v[0] + hull);
521                         mima->maxy = max_ff(mima->maxy, v[1] + hull);
522                         mima->maxz = max_ff(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 = min_ff(bs->len, min);
822                                 max = max_ff(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_done(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] = min_fff(face_v1[0], face_v2[0], face_v3[0]);
1100         aabbmin[1] = min_fff(face_v1[1], face_v2[1], face_v3[1]);
1101         aabbmin[2] = min_fff(face_v1[2], face_v2[2], face_v3[2]);
1102         aabbmax[0] = max_fff(face_v1[0], face_v2[0], face_v3[0]);
1103         aabbmax[1] = max_fff(face_v1[1], face_v2[1], face_v3[1]);
1104         aabbmax[2] = max_fff(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_done(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 * expf(-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] = min_fff(face_v1[0], face_v2[0], face_v3[0]);
1199         aabbmin[1] = min_fff(face_v1[1], face_v2[1], face_v3[1]);
1200         aabbmin[2] = min_fff(face_v1[2], face_v2[2], face_v3[2]);
1201         aabbmax[0] = max_fff(face_v1[0], face_v2[0], face_v3[0]);
1202         aabbmax[1] = max_fff(face_v1[1], face_v2[1], face_v3[1]);
1203         aabbmax[2] = max_fff(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_done(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                         {
1348                                 Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
1349                                 Vec3PlusStVec(sb->bpoint[bf->v2].force, tune, feedback);
1350                                 Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
1351 //                              Vec3PlusStVec(bf->ext_force, tune, feedback);
1352                                 bf->flag |= BFF_INTERSECT;
1353                                 choke = min_ff(max_ff(damp, choke), 1.0f);
1354                         }
1355
1356                         feedback[0]=feedback[1]=feedback[2]=0.0f;
1357                         if ((bf->v4) && (sb_detect_face_collisionCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v3].pos, sb->bpoint[bf->v4].pos,
1358                                                                         &damp, feedback, ob->lay, ob, timenow)))
1359                         {
1360                                 Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
1361                                 Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
1362                                 Vec3PlusStVec(sb->bpoint[bf->v4].force, tune, feedback);
1363 //                              Vec3PlusStVec(bf->ext_force, tune, feedback);
1364                                 bf->flag |= BFF_INTERSECT;
1365                                 choke = min_ff(max_ff(damp, choke), 1.0f);
1366                         }
1367 /*---edges intruding*/
1368
1369 /*+++ close vertices*/
1370                         if (( bf->flag & BFF_INTERSECT)==0) {
1371                                 bf->flag &= ~BFF_CLOSEVERT;
1372                                 tune = -1.0f;
1373                                 feedback[0]=feedback[1]=feedback[2]=0.0f;
1374                                 if (sb_detect_face_pointCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v2].pos, sb->bpoint[bf->v3].pos,
1375                                         &damp,  feedback, ob->lay, ob, timenow))
1376                                 {
1377                                         Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
1378                                         Vec3PlusStVec(sb->bpoint[bf->v2].force, tune, feedback);
1379                                         Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
1380 //                                      Vec3PlusStVec(bf->ext_force, tune, feedback);
1381                                         bf->flag |= BFF_CLOSEVERT;
1382                                         choke = min_ff(max_ff(damp, choke), 1.0f);
1383                                 }
1384
1385                                 feedback[0]=feedback[1]=feedback[2]=0.0f;
1386                                 if ((bf->v4) && (sb_detect_face_pointCached(sb->bpoint[bf->v1].pos, sb->bpoint[bf->v3].pos, sb->bpoint[bf->v4].pos,
1387                                                                             &damp, feedback, ob->lay, ob, timenow)))
1388                                 {
1389                                         Vec3PlusStVec(sb->bpoint[bf->v1].force, tune, feedback);
1390                                         Vec3PlusStVec(sb->bpoint[bf->v3].force, tune, feedback);
1391                                         Vec3PlusStVec(sb->bpoint[bf->v4].force, tune, feedback);
1392 //                                      Vec3PlusStVec(bf->ext_force, tune, feedback);
1393                                         bf->flag |= BFF_CLOSEVERT;
1394                                         choke = min_ff(max_ff(damp, choke), 1.0f);
1395                                 }
1396                         }
1397 /*--- close vertices*/
1398                 }
1399                 bf = sb->scratch->bodyface;
1400                 for (a=0; a<sb->scratch->totface; a++, bf++) {
1401                         if (( bf->flag & BFF_INTERSECT) || ( bf->flag & BFF_CLOSEVERT)) {
1402                                 sb->bpoint[bf->v1].choke2 = max_ff(sb->bpoint[bf->v1].choke2, choke);
1403                                 sb->bpoint[bf->v2].choke2 = max_ff(sb->bpoint[bf->v2].choke2, choke);
1404                                 sb->bpoint[bf->v3].choke2 = max_ff(sb->bpoint[bf->v3].choke2, choke);
1405                                 if (bf->v4) {
1406                                         sb->bpoint[bf->v2].choke2 = max_ff(sb->bpoint[bf->v2].choke2, choke);
1407                                 }
1408                         }
1409                 }
1410         }
1411 }
1412
1413 /*  --- the face external section*/
1414
1415
1416 /* +++ the spring external section*/
1417
1418 static int sb_detect_edge_collisionCached(float edge_v1[3], float edge_v2[3], float *damp,
1419                                                                    float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner, float time)
1420 {
1421         Object *ob;
1422         GHash *hash;
1423         GHashIterator *ihash;
1424         float nv1[3], nv2[3], nv3[3], nv4[3], edge1[3], edge2[3], d_nvect[3], aabbmin[3], aabbmax[3];
1425         float t, el;
1426         int a, deflected=0;
1427
1428         minmax_v3v3_v3(aabbmin, aabbmax, edge_v1);
1429         minmax_v3v3_v3(aabbmin, aabbmax, edge_v2);
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_done(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                                         {
1484                                                 mface++;
1485                                                 mima++;
1486                                                 continue;
1487                                         }
1488
1489
1490                                         if (mvert) {
1491
1492                                                 copy_v3_v3(nv1, mvert[mface->v1].co);
1493                                                 copy_v3_v3(nv2, mvert[mface->v2].co);
1494                                                 copy_v3_v3(nv3, mvert[mface->v3].co);
1495                                                 if (mface->v4) {
1496                                                         copy_v3_v3(nv4, mvert[mface->v4].co);
1497                                                 }
1498                                                 if (mprevvert) {
1499                                                         mul_v3_fl(nv1, time);
1500                                                         Vec3PlusStVec(nv1, (1.0f-time), mprevvert[mface->v1].co);
1501
1502                                                         mul_v3_fl(nv2, time);
1503                                                         Vec3PlusStVec(nv2, (1.0f-time), mprevvert[mface->v2].co);
1504
1505                                                         mul_v3_fl(nv3, time);
1506                                                         Vec3PlusStVec(nv3, (1.0f-time), mprevvert[mface->v3].co);
1507
1508                                                         if (mface->v4) {
1509                                                                 mul_v3_fl(nv4, time);
1510                                                                 Vec3PlusStVec(nv4, (1.0f-time), mprevvert[mface->v4].co);
1511                                                         }
1512                                                 }
1513                                         }
1514
1515                                         /* switch origin to be nv2*/
1516                                         sub_v3_v3v3(edge1, nv1, nv2);
1517                                         sub_v3_v3v3(edge2, nv3, nv2);
1518
1519                                         cross_v3_v3v3(d_nvect, edge2, edge1);
1520                                         normalize_v3(d_nvect);
1521                                         if ( isect_line_tri_v3(edge_v1, edge_v2, nv1, nv2, nv3, &t, NULL)) {
1522                                                 float v1[3], v2[3];
1523                                                 float intrusiondepth, i1, i2;
1524                                                 sub_v3_v3v3(v1, edge_v1, nv2);
1525                                                 sub_v3_v3v3(v2, edge_v2, nv2);
1526                                                 i1 = dot_v3v3(v1, d_nvect);
1527                                                 i2 = dot_v3v3(v2, d_nvect);
1528                                                 intrusiondepth = -min_ff(i1, i2) / el;
1529                                                 Vec3PlusStVec(force, intrusiondepth, d_nvect);
1530                                                 *damp=ob->pd->pdef_sbdamp;
1531                                                 deflected = 2;
1532                                         }
1533                                         if (mface->v4) { /* quad */
1534                                                 /* switch origin to be nv4 */
1535                                                 sub_v3_v3v3(edge1, nv3, nv4);
1536                                                 sub_v3_v3v3(edge2, nv1, nv4);
1537
1538                                                 cross_v3_v3v3(d_nvect, edge2, edge1);
1539                                                 normalize_v3(d_nvect);
1540                                                 if (isect_line_tri_v3( edge_v1, edge_v2, nv1, nv3, nv4, &t, NULL)) {
1541                                                         float v1[3], v2[3];
1542                                                         float intrusiondepth, i1, i2;
1543                                                         sub_v3_v3v3(v1, edge_v1, nv4);
1544                                                         sub_v3_v3v3(v2, edge_v2, nv4);
1545                                                         i1 = dot_v3v3(v1, d_nvect);
1546                                                         i2 = dot_v3v3(v2, d_nvect);
1547                                                         intrusiondepth = -min_ff(i1, i2) / el;
1548
1549
1550                                                         Vec3PlusStVec(force, intrusiondepth, d_nvect);
1551                                                         *damp=ob->pd->pdef_sbdamp;
1552                                                         deflected = 2;
1553                                                 }
1554                                         }
1555                                         mface++;
1556                                         mima++;
1557                                 }/* while a */
1558                         } /* if (ob->pd && ob->pd->deflect) */
1559                         BLI_ghashIterator_step(ihash);
1560         } /* while () */
1561         BLI_ghashIterator_free(ihash);
1562         return deflected;
1563 }
1564
1565 static void _scan_for_ext_spring_forces(Scene *scene, Object *ob, float timenow, int ifirst, int ilast, struct ListBase *do_effector)
1566 {
1567         SoftBody *sb = ob->soft;
1568         int a;
1569         float damp;
1570         float feedback[3];
1571
1572         if (sb && sb->totspring) {
1573                 for (a=ifirst; a<ilast; a++) {
1574                         BodySpring *bs = &sb->bspring[a];
1575                         bs->ext_force[0]=bs->ext_force[1]=bs->ext_force[2]=0.0f;
1576                         feedback[0]=feedback[1]=feedback[2]=0.0f;
1577                         bs->flag &= ~BSF_INTERSECT;
1578
1579                         if (bs->springtype == SB_EDGE) {
1580                                 /* +++ springs colliding */
1581                                 if (ob->softflag & OB_SB_EDGECOLL) {
1582                                         if ( sb_detect_edge_collisionCached (sb->bpoint[bs->v1].pos, sb->bpoint[bs->v2].pos,
1583                                                 &damp, feedback, ob->lay, ob, timenow)) {
1584                                                         add_v3_v3(bs->ext_force, feedback);
1585                                                         bs->flag |= BSF_INTERSECT;
1586                                                         //bs->cf=damp;
1587                                                         bs->cf=sb->choke*0.01f;
1588
1589                                         }
1590                                 }
1591                                 /* ---- springs colliding */
1592
1593                                 /* +++ springs seeing wind ... n stuff depending on their orientation*/
1594                                 /* note we don't use sb->mediafrict but use sb->aeroedge for magnitude of effect*/
1595                                 if (sb->aeroedge) {
1596                                         float vel[3], sp[3], pr[3], force[3];
1597                                         float f, windfactor  = 0.25f;
1598                                         /*see if we have wind*/
1599                                         if (do_effector) {
1600                                                 EffectedPoint epoint;
1601                                                 float speed[3] = {0.0f, 0.0f, 0.0f};
1602                                                 float pos[3];
1603                                                 mid_v3_v3v3(pos, sb->bpoint[bs->v1].pos, sb->bpoint[bs->v2].pos);
1604                                                 mid_v3_v3v3(vel, sb->bpoint[bs->v1].vec, sb->bpoint[bs->v2].vec);
1605                                                 pd_point_from_soft(scene, pos, vel, -1, &epoint);
1606                                                 pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
1607
1608                                                 mul_v3_fl(speed, windfactor);
1609                                                 add_v3_v3(vel, speed);
1610                                         }
1611                                         /* media in rest */
1612                                         else {
1613                                                 add_v3_v3v3(vel, sb->bpoint[bs->v1].vec, sb->bpoint[bs->v2].vec);
1614                                         }
1615                                         f = normalize_v3(vel);
1616                                         f = -0.0001f*f*f*sb->aeroedge;
1617                                         /* (todo) add a nice angle dependent function done for now BUT */
1618                                         /* still there could be some nice drag/lift function, but who needs it */
1619
1620                                         sub_v3_v3v3(sp, sb->bpoint[bs->v1].pos, sb->bpoint[bs->v2].pos);
1621                                         project_v3_v3v3(pr, vel, sp);
1622                                         sub_v3_v3(vel, pr);
1623                                         normalize_v3(vel);
1624                                         if (ob->softflag & OB_SB_AERO_ANGLE) {
1625                                                 normalize_v3(sp);
1626                                                 Vec3PlusStVec(bs->ext_force, f * (1.0f - fabsf(dot_v3v3(vel, sp))), vel);
1627                                         }
1628                                         else {
1629                                                 Vec3PlusStVec(bs->ext_force, f, vel); // to keep compatible with 2.45 release files
1630                                         }
1631                                 }
1632                                 /* --- springs seeing wind */
1633                         }
1634                 }
1635         }
1636 }
1637
1638
1639 static void scan_for_ext_spring_forces(Scene *scene, Object *ob, float timenow)
1640 {
1641         SoftBody *sb = ob->soft;
1642         ListBase *do_effector = NULL;
1643
1644         do_effector = pdInitEffectors(scene, ob, NULL, sb->effector_weights, true);
1645         _scan_for_ext_spring_forces(scene, ob, timenow, 0, sb->totspring, do_effector);
1646         pdEndEffectors(&do_effector);
1647 }
1648
1649 static void *exec_scan_for_ext_spring_forces(void *data)
1650 {
1651         SB_thread_context *pctx = (SB_thread_context*)data;
1652         _scan_for_ext_spring_forces(pctx->scene, pctx->ob, pctx->timenow, pctx->ifirst, pctx->ilast, pctx->do_effector);
1653         return NULL;
1654 }
1655
1656 static void sb_sfesf_threads_run(Scene *scene, struct Object *ob, float timenow, int totsprings, int *UNUSED(ptr_to_break_func(void)))
1657 {
1658         ListBase *do_effector = NULL;
1659         ListBase threads;
1660         SB_thread_context *sb_threads;
1661         int i, totthread, left, dec;
1662         int lowsprings =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */
1663
1664         do_effector= pdInitEffectors(scene, ob, NULL, ob->soft->effector_weights, true);
1665
1666         /* figure the number of threads while preventing pretty pointless threading overhead */
1667         totthread= BKE_scene_num_threads(scene);
1668         /* what if we got zillions of CPUs running but less to spread*/
1669         while ((totsprings/totthread < lowsprings) && (totthread > 1)) {
1670                 totthread--;
1671         }
1672
1673         sb_threads= MEM_callocN(sizeof(SB_thread_context)*totthread, "SBSpringsThread");
1674         memset(sb_threads, 0, sizeof(SB_thread_context)*totthread);
1675         left = totsprings;
1676         dec = totsprings/totthread +1;
1677         for (i=0; i<totthread; i++) {
1678                 sb_threads[i].scene = scene;
1679                 sb_threads[i].ob = ob;
1680                 sb_threads[i].forcetime = 0.0; // not used here
1681                 sb_threads[i].timenow = timenow;
1682                 sb_threads[i].ilast   = left;
1683                 left = left - dec;
1684                 if (left >0) {
1685                         sb_threads[i].ifirst  = left;
1686                 }
1687                 else
1688                         sb_threads[i].ifirst  = 0;
1689                 sb_threads[i].do_effector = do_effector;
1690                 sb_threads[i].do_deflector = false;// not used here
1691                 sb_threads[i].fieldfactor = 0.0f;// not used here
1692                 sb_threads[i].windfactor  = 0.0f;// not used here
1693                 sb_threads[i].nr= i;
1694                 sb_threads[i].tot= totthread;
1695         }
1696         if (totthread > 1) {
1697                 BLI_init_threads(&threads, exec_scan_for_ext_spring_forces, totthread);
1698
1699                 for (i=0; i<totthread; i++)
1700                         BLI_insert_thread(&threads, &sb_threads[i]);
1701
1702                 BLI_end_threads(&threads);
1703         }
1704         else
1705                 exec_scan_for_ext_spring_forces(&sb_threads[0]);
1706         /* clean up */
1707         MEM_freeN(sb_threads);
1708
1709         pdEndEffectors(&do_effector);
1710 }
1711
1712
1713 /* --- the spring external section*/
1714
1715 static int choose_winner(float*w, float* pos, float*a, float*b, float*c, float*ca, float*cb, float*cc)
1716 {
1717         float mindist, cp;
1718         int winner =1;
1719         mindist = fabsf(dot_v3v3(pos, a));
1720
1721         cp = fabsf(dot_v3v3(pos, b));
1722         if ( mindist < cp ) {
1723                 mindist = cp;
1724                 winner =2;
1725         }
1726
1727         cp = fabsf(dot_v3v3(pos, c));
1728         if (mindist < cp ) {
1729                 mindist = cp;
1730                 winner =3;
1731         }
1732         switch (winner) {
1733                 case 1: copy_v3_v3(w, ca); break;
1734                 case 2: copy_v3_v3(w, cb); break;
1735                 case 3: copy_v3_v3(w, cc);
1736         }
1737         return(winner);
1738 }
1739
1740
1741
1742 static int sb_detect_vertex_collisionCached(float opco[3], float facenormal[3], float *damp,
1743                                                                          float force[3], unsigned int UNUSED(par_layer), struct Object *vertexowner,
1744                                                                          float time, float vel[3], float *intrusion)
1745 {
1746         Object *ob= NULL;
1747         GHash *hash;
1748         GHashIterator *ihash;
1749         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},
1750               vv1[3], vv2[3], vv3[3], vv4[3], coledge[3] = {0.0f, 0.0f, 0.0f}, mindistedge = 1000.0f,
1751               outerforceaccu[3], innerforceaccu[3],
1752               facedist, /* n_mag, */ /* UNUSED */ force_mag_norm, minx, miny, minz, maxx, maxy, maxz,
1753               innerfacethickness = -0.5f, outerfacethickness = 0.2f,
1754               ee = 5.0f, ff = 0.1f, fa=1;
1755         int a, deflected=0, cavel=0, ci=0;
1756 /* init */
1757         *intrusion = 0.0f;
1758         hash  = vertexowner->soft->scratch->colliderhash;
1759         ihash = BLI_ghashIterator_new(hash);
1760         outerforceaccu[0]=outerforceaccu[1]=outerforceaccu[2]=0.0f;
1761         innerforceaccu[0]=innerforceaccu[1]=innerforceaccu[2]=0.0f;
1762 /* go */
1763         while (!BLI_ghashIterator_done(ihash)) {
1764
1765                 ccd_Mesh *ccdm = BLI_ghashIterator_getValue     (ihash);
1766                 ob             = BLI_ghashIterator_getKey       (ihash);
1767                         /* only with deflecting set */
1768                         if (ob->pd && ob->pd->deflect) {
1769                                 MFace *mface= NULL;
1770                                 MVert *mvert= NULL;
1771                                 MVert *mprevvert= NULL;
1772                                 ccdf_minmax *mima= NULL;
1773
1774                                 if (ccdm) {
1775                                         mface= ccdm->mface;
1776                                         mvert= ccdm->mvert;
1777                                         mprevvert= ccdm->mprevvert;
1778                                         mima= ccdm->mima;
1779                                         a = ccdm->totface;
1780
1781                                         minx = ccdm->bbmin[0];
1782                                         miny = ccdm->bbmin[1];
1783                                         minz = ccdm->bbmin[2];
1784
1785                                         maxx = ccdm->bbmax[0];
1786                                         maxy = ccdm->bbmax[1];
1787                                         maxz = ccdm->bbmax[2];
1788
1789                                         if ((opco[0] < minx) ||
1790                                             (opco[1] < miny) ||
1791                                             (opco[2] < minz) ||
1792                                             (opco[0] > maxx) ||
1793                                             (opco[1] > maxy) ||
1794                                             (opco[2] > maxz) )
1795                                         {
1796                                                 /* outside the padded boundbox --> collision object is too far away */
1797                                                 BLI_ghashIterator_step(ihash);
1798                                                 continue;
1799                                         }
1800                                 }
1801                                 else {
1802                                         /*aye that should be cached*/
1803                                         printf("missing cache error\n");
1804                                                 BLI_ghashIterator_step(ihash);
1805                                         continue;
1806                                 }
1807
1808                                 /* do object level stuff */
1809                                 /* need to have user control for that since it depends on model scale */
1810                                 innerfacethickness = -ob->pd->pdef_sbift;
1811                                 outerfacethickness =  ob->pd->pdef_sboft;
1812                                 fa = (ff*outerfacethickness-outerfacethickness);
1813                                 fa *= fa;
1814                                 fa = 1.0f/fa;
1815                                 avel[0]=avel[1]=avel[2]=0.0f;
1816                                 /* use mesh*/
1817                                 while (a--) {
1818                                         if (
1819                                                 (opco[0] < mima->minx) ||
1820                                                 (opco[0] > mima->maxx) ||
1821                                                 (opco[1] < mima->miny) ||
1822                                                 (opco[1] > mima->maxy) ||
1823                                                 (opco[2] < mima->minz) ||
1824                                                 (opco[2] > mima->maxz)
1825                                                 ) {
1826                                                         mface++;
1827                                                         mima++;
1828                                                         continue;
1829                                         }
1830
1831                                         if (mvert) {
1832
1833                                                 copy_v3_v3(nv1, mvert[mface->v1].co);
1834                                                 copy_v3_v3(nv2, mvert[mface->v2].co);
1835                                                 copy_v3_v3(nv3, mvert[mface->v3].co);
1836                                                 if (mface->v4) {
1837                                                         copy_v3_v3(nv4, mvert[mface->v4].co);
1838                                                 }
1839
1840                                                 if (mprevvert) {
1841                                                         /* grab the average speed of the collider vertices
1842                                                         before we spoil nvX
1843                                                         humm could be done once a SB steps but then we' need to store that too
1844                                                         since the AABB reduced propabitlty to get here drasticallly
1845                                                         it might be a nice tradeof CPU <--> memory
1846                                                         */
1847                                                         sub_v3_v3v3(vv1, nv1, mprevvert[mface->v1].co);
1848                                                         sub_v3_v3v3(vv2, nv2, mprevvert[mface->v2].co);
1849                                                         sub_v3_v3v3(vv3, nv3, mprevvert[mface->v3].co);
1850                                                         if (mface->v4) {
1851                                                                 sub_v3_v3v3(vv4, nv4, mprevvert[mface->v4].co);
1852                                                         }
1853
1854                                                         mul_v3_fl(nv1, time);
1855                                                         Vec3PlusStVec(nv1, (1.0f-time), mprevvert[mface->v1].co);
1856
1857                                                         mul_v3_fl(nv2, time);
1858                                                         Vec3PlusStVec(nv2, (1.0f-time), mprevvert[mface->v2].co);
1859
1860                                                         mul_v3_fl(nv3, time);
1861                                                         Vec3PlusStVec(nv3, (1.0f-time), mprevvert[mface->v3].co);
1862
1863                                                         if (mface->v4) {
1864                                                                 mul_v3_fl(nv4, time);
1865                                                                 Vec3PlusStVec(nv4, (1.0f-time), mprevvert[mface->v4].co);
1866                                                         }
1867                                                 }
1868                                         }
1869
1870                                         /* switch origin to be nv2*/
1871                                         sub_v3_v3v3(edge1, nv1, nv2);
1872                                         sub_v3_v3v3(edge2, nv3, nv2);
1873                                         sub_v3_v3v3(dv1, opco, nv2); /* abuse dv1 to have vertex in question at *origin* of triangle */
1874
1875                                         cross_v3_v3v3(d_nvect, edge2, edge1);
1876                                         /* n_mag = */ /* UNUSED */ normalize_v3(d_nvect);
1877                                         facedist = dot_v3v3(dv1, d_nvect);
1878                                         // so rules are
1879                                         //
1880
1881                                         if ((facedist > innerfacethickness) && (facedist < outerfacethickness)) {
1882                                                 if (isect_point_tri_prism_v3(opco, nv1, nv2, nv3) ) {
1883                                                         force_mag_norm =(float)exp(-ee*facedist);
1884                                                         if (facedist > outerfacethickness*ff)
1885                                                                 force_mag_norm =(float)force_mag_norm*fa*(facedist - outerfacethickness)*(facedist - outerfacethickness);
1886                                                         *damp=ob->pd->pdef_sbdamp;
1887                                                         if (facedist > 0.0f) {
1888                                                                 *damp *= (1.0f - facedist/outerfacethickness);
1889                                                                 Vec3PlusStVec(outerforceaccu, force_mag_norm, d_nvect);
1890                                                                 deflected = 3;
1891
1892                                                         }
1893                                                         else {
1894                                                                 Vec3PlusStVec(innerforceaccu, force_mag_norm, d_nvect);
1895                                                                 if (deflected < 2) deflected = 2;
1896                                                         }
1897                                                         if ((mprevvert) && (*damp > 0.0f)) {
1898                                                                 choose_winner(ve, opco, nv1, nv2, nv3, vv1, vv2, vv3);
1899                                                                 add_v3_v3(avel, ve);
1900                                                                 cavel ++;
1901                                                         }
1902                                                         *intrusion += facedist;
1903                                                         ci++;
1904                                                 }
1905                                         }
1906                                         if (mface->v4) { /* quad */
1907                                                 /* switch origin to be nv4 */
1908                                                 sub_v3_v3v3(edge1, nv3, nv4);
1909                                                 sub_v3_v3v3(edge2, nv1, nv4);
1910                                                 sub_v3_v3v3(dv1, opco, nv4); /* abuse dv1 to have vertex in question at *origin* of triangle */
1911
1912                                                 cross_v3_v3v3(d_nvect, edge2, edge1);
1913                                                 /* n_mag = */ /* UNUSED */ normalize_v3(d_nvect);
1914                                                 facedist = dot_v3v3(dv1, d_nvect);
1915
1916                                                 if ((facedist > innerfacethickness) && (facedist < outerfacethickness)) {
1917                                                         if (isect_point_tri_prism_v3(opco, nv1, nv3, nv4) ) {
1918                                                                 force_mag_norm =(float)exp(-ee*facedist);
1919                                                                 if (facedist > outerfacethickness*ff)
1920                                                                         force_mag_norm =(float)force_mag_norm*fa*(facedist - outerfacethickness)*(facedist - outerfacethickness);
1921                                                                 *damp=ob->pd->pdef_sbdamp;
1922                                                         if (facedist > 0.0f) {
1923                                                                 *damp *= (1.0f - facedist/outerfacethickness);
1924                                                                 Vec3PlusStVec(outerforceaccu, force_mag_norm, d_nvect);
1925                                                                 deflected = 3;
1926
1927                                                         }
1928                                                         else {
1929                                                                 Vec3PlusStVec(innerforceaccu, force_mag_norm, d_nvect);
1930                                                                 if (deflected < 2) deflected = 2;
1931                                                         }
1932
1933                                                                 if ((mprevvert) && (*damp > 0.0f)) {
1934                                                                         choose_winner(ve, opco, nv1, nv3, nv4, vv1, vv3, vv4);
1935                                                                         add_v3_v3(avel, ve);
1936                                                                         cavel ++;
1937                                                                 }
1938                                                                 *intrusion += facedist;
1939                                                                 ci++;
1940                                                         }
1941
1942                                                 }
1943                                                 if ((deflected < 2)&& (G.debug_value != 444)) { /* we did not hit a face until now */
1944                                                         /* see if 'outer' hits an edge */
1945                                                         float dist;
1946
1947                                                         closest_to_line_segment_v3(ve, opco, nv1, nv2);
1948                                                          sub_v3_v3v3(ve, opco, ve);
1949                                                         dist = normalize_v3(ve);
1950                                                         if ((dist < outerfacethickness)&&(dist < mindistedge )) {
1951                                                                 copy_v3_v3(coledge, ve);
1952                                                                 mindistedge = dist,
1953                                                                 deflected=1;
1954                                                         }
1955
1956                                                         closest_to_line_segment_v3(ve, opco, nv2, nv3);
1957                                                         sub_v3_v3v3(ve, opco, ve);
1958                                                         dist = normalize_v3(ve);
1959                                                         if ((dist < outerfacethickness)&&(dist < mindistedge )) {
1960                                                                 copy_v3_v3(coledge, ve);
1961                                                                 mindistedge = dist,
1962                                                                 deflected=1;
1963                                                         }
1964
1965                                                         closest_to_line_segment_v3(ve, opco, nv3, nv1);
1966                                                         sub_v3_v3v3(ve, opco, ve);
1967                                                         dist = normalize_v3(ve);
1968                                                         if ((dist < outerfacethickness)&&(dist < mindistedge )) {
1969                                                                 copy_v3_v3(coledge, ve);
1970                                                                 mindistedge = dist,
1971                                                                 deflected=1;
1972                                                         }
1973                                                         if (mface->v4) { /* quad */
1974                                                                 closest_to_line_segment_v3(ve, opco, nv3, nv4);
1975                                                                 sub_v3_v3v3(ve, opco, ve);
1976                                                                 dist = normalize_v3(ve);
1977                                                                 if ((dist < outerfacethickness)&&(dist < mindistedge )) {
1978                                                                         copy_v3_v3(coledge, ve);
1979                                                                         mindistedge = dist,
1980                                                                                 deflected=1;
1981                                                                 }
1982
1983                                                                 closest_to_line_segment_v3(ve, opco, nv1, nv4);
1984                                                                 sub_v3_v3v3(ve, opco, ve);
1985                                                                 dist = normalize_v3(ve);
1986                                                                 if ((dist < outerfacethickness)&&(dist < mindistedge )) {
1987                                                                         copy_v3_v3(coledge, ve);
1988                                                                         mindistedge = dist,
1989                                                                                 deflected=1;
1990                                                                 }
1991
1992                                                         }
1993
1994
1995                                                 }
1996                                         }
1997                                         mface++;
1998                                         mima++;
1999                                 }/* while a */
2000                         } /* if (ob->pd && ob->pd->deflect) */
2001                         BLI_ghashIterator_step(ihash);
2002         } /* while () */
2003
2004         if (deflected == 1) { // no face but 'outer' edge cylinder sees vert
2005                 force_mag_norm =(float)exp(-ee*mindistedge);
2006                 if (mindistedge > outerfacethickness*ff)
2007                         force_mag_norm =(float)force_mag_norm*fa*(mindistedge - outerfacethickness)*(mindistedge - outerfacethickness);
2008                 Vec3PlusStVec(force, force_mag_norm, coledge);
2009                 *damp=ob->pd->pdef_sbdamp;
2010                 if (mindistedge > 0.0f) {
2011                         *damp *= (1.0f - mindistedge/outerfacethickness);
2012                 }
2013
2014         }
2015         if (deflected == 2) { //  face inner detected
2016                 add_v3_v3(force, innerforceaccu);
2017         }
2018         if (deflected == 3) { //  face outer detected
2019                 add_v3_v3(force, outerforceaccu);
2020         }
2021
2022         BLI_ghashIterator_free(ihash);
2023         if (cavel) mul_v3_fl(avel, 1.0f/(float)cavel);
2024         copy_v3_v3(vel, avel);
2025         if (ci) *intrusion /= ci;
2026         if (deflected) {
2027                 normalize_v3_v3(facenormal, force);
2028         }
2029         return deflected;
2030 }
2031
2032
2033 /* sandbox to plug in various deflection algos */
2034 static int sb_deflect_face(Object *ob, float *actpos, float *facenormal, float *force, float *cf, float time, float *vel, float *intrusion)
2035 {
2036         float s_actpos[3];
2037         int deflected;
2038         copy_v3_v3(s_actpos, actpos);
2039         deflected= sb_detect_vertex_collisionCached(s_actpos, facenormal, cf, force, ob->lay, ob, time, vel, intrusion);
2040         //deflected= sb_detect_vertex_collisionCachedEx(s_actpos, facenormal, cf, force, ob->lay, ob, time, vel, intrusion);
2041         return(deflected);
2042 }
2043
2044 /* hiding this for now .. but the jacobian may pop up on other tasks .. so i'd like to keep it
2045 static void dfdx_spring(int ia, int ic, int op, float dir[3], float L, float len, float factor)
2046 {
2047         float m, delta_ij;
2048         int i, j;
2049         if (L < len) {
2050                 for (i=0;i<3;i++)
2051                         for (j=0;j<3;j++) {
2052                                 delta_ij = (i==j ? (1.0f): (0.0f));
2053                                 m=factor*(dir[i]*dir[j] + (1-L/len)*(delta_ij - dir[i]*dir[j]));
2054                                 nlMatrixAdd(ia+i, op+ic+j, m);
2055                         }
2056         }
2057         else {
2058                 for (i=0;i<3;i++)
2059                         for (j=0;j<3;j++) {
2060                                 m=factor*dir[i]*dir[j];
2061                                 nlMatrixAdd(ia+i, op+ic+j, m);
2062                         }
2063         }
2064 }
2065
2066
2067 static void dfdx_goal(int ia, int ic, int op, float factor)
2068 {
2069         int i;
2070         for (i=0;i<3;i++) nlMatrixAdd(ia+i, op+ic+i, factor);
2071 }
2072
2073 static void dfdv_goal(int ia, int ic, float factor)
2074 {
2075         int i;
2076         for (i=0;i<3;i++) nlMatrixAdd(ia+i, ic+i, factor);
2077 }
2078 */
2079 static void sb_spring_force(Object *ob, int bpi, BodySpring *bs, float iks, float UNUSED(forcetime), int nl_flags)
2080 {
2081         SoftBody *sb= ob->soft; /* is supposed to be there */
2082         BodyPoint  *bp1, *bp2;
2083
2084         float dir[3], dvel[3];
2085         float distance, forcefactor, kd, absvel, projvel, kw;
2086 #if 0   /* UNUSED */
2087         int ia, ic;
2088 #endif
2089         /* prepare depending on which side of the spring we are on */
2090         if (bpi == bs->v1) {
2091                 bp1 = &sb->bpoint[bs->v1];
2092                 bp2 = &sb->bpoint[bs->v2];
2093 #if 0   /* UNUSED */
2094                 ia =3*bs->v1;
2095                 ic =3*bs->v2;
2096 #endif
2097         }
2098         else if (bpi == bs->v2) {
2099                 bp1 = &sb->bpoint[bs->v2];
2100                 bp2 = &sb->bpoint[bs->v1];
2101 #if 0   /* UNUSED */
2102                 ia =3*bs->v2;
2103                 ic =3*bs->v1;
2104 #endif
2105         }
2106         else {
2107                 /* TODO make this debug option */
2108                 /**/
2109                 printf("bodypoint <bpi> is not attached to spring  <*bs> --> sb_spring_force()\n");
2110                 return;
2111         }
2112
2113         /* do bp1 <--> bp2 elastic */
2114         sub_v3_v3v3(dir, bp1->pos, bp2->pos);
2115         distance = normalize_v3(dir);
2116         if (bs->len < distance)
2117                 iks  = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
2118         else
2119                 iks  = 1.0f/(1.0f-sb->inpush)-1.0f ;/* inner spring constants function */
2120
2121         if (bs->len > 0.0f) /* check for degenerated springs */
2122                 forcefactor = iks/bs->len;
2123         else
2124                 forcefactor = iks;
2125         kw = (bp1->springweight+bp2->springweight)/2.0f;
2126         kw = kw * kw;
2127         kw = kw * kw;
2128         switch (bs->springtype) {
2129                 case SB_EDGE:
2130                 case SB_HANDLE:
2131                         forcefactor *=  kw;
2132                         break;
2133                 case SB_BEND:
2134                         forcefactor *=sb->secondspring*kw;
2135                         break;
2136                 case SB_STIFFQUAD:
2137                         forcefactor *=sb->shearstiff*sb->shearstiff* kw;
2138                         break;
2139                 default:
2140                         break;
2141         }
2142
2143
2144         Vec3PlusStVec(bp1->force, (bs->len - distance)*forcefactor, dir);
2145
2146         /* do bp1 <--> bp2 viscous */
2147         sub_v3_v3v3(dvel, bp1->vec, bp2->vec);
2148         kd = sb->infrict * sb_fric_force_scale(ob);
2149         absvel  = normalize_v3(dvel);
2150         projvel = dot_v3v3(dir, dvel);
2151         kd     *= absvel * projvel;
2152         Vec3PlusStVec(bp1->force, -kd, dir);
2153
2154         /* do jacobian stuff if needed */
2155         if (nl_flags & NLF_BUILD) {
2156                 //int op =3*sb->totpoint;
2157                 //float mvel = -forcetime*kd;
2158                 //float mpos = -forcetime*forcefactor;
2159                 /* depending on my pos */
2160                 // dfdx_spring(ia, ia, op, dir, bs->len, distance, -mpos);
2161                 /* depending on my vel */
2162                 // dfdv_goal(ia, ia, mvel); // well that ignores geometie
2163                 if (bp2->goal < SOFTGOALSNAP) { /* ommit this bp when it snaps */
2164                         /* depending on other pos */
2165                         // dfdx_spring(ia, ic, op, dir, bs->len, distance, mpos);
2166                         /* depending on other vel */
2167                         // dfdv_goal(ia, ia, -mvel); // well that ignores geometie
2168                 }
2169         }
2170 }
2171
2172
2173 /* since this is definitely the most CPU consuming task here .. try to spread it */
2174 /* core function _softbody_calc_forces_slice_in_a_thread */
2175 /* result is int to be able to flag user break */
2176 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)
2177 {
2178         float iks;
2179         int bb, do_selfcollision, do_springcollision, do_aero;
2180         int number_of_points_here = ilast - ifirst;
2181         SoftBody *sb= ob->soft; /* is supposed to be there */
2182         BodyPoint  *bp;
2183
2184         /* intitialize */
2185         if (sb) {
2186         /* check conditions for various options */
2187         /* +++ could be done on object level to squeeze out the last bits of it */
2188         do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF));
2189         do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL);
2190         do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES));
2191         /* --- could be done on object level to squeeze out the last bits of it */
2192         }
2193         else {
2194                 printf("Error expected a SB here\n");
2195                 return (999);
2196         }
2197
2198 /* debugerin */
2199         if (sb->totpoint < ifirst) {
2200                 printf("Aye 998");
2201                 return (998);
2202         }
2203 /* debugerin */
2204
2205
2206         bp = &sb->bpoint[ifirst];
2207         for (bb=number_of_points_here; bb>0; bb--, bp++) {
2208                 /* clear forces  accumulator */
2209                 bp->force[0] = bp->force[1] = bp->force[2] = 0.0;
2210                 /* naive ball self collision */
2211                 /* needs to be done if goal snaps or not */
2212                 if (do_selfcollision) {
2213                         int attached;
2214                         BodyPoint   *obp;
2215                         BodySpring *bs;
2216                         int c, b;
2217                         float velcenter[3], dvel[3], def[3];
2218                         float distance;
2219                         float compare;
2220                         float bstune = sb->ballstiff;
2221
2222                         for (c=sb->totpoint, obp= sb->bpoint; c>=ifirst+bb; c--, obp++) {
2223                                 compare = (obp->colball + bp->colball);
2224                                 sub_v3_v3v3(def, bp->pos, obp->pos);
2225                                 /* rather check the AABBoxes before ever calulating the real distance */
2226                                 /* mathematically it is completly nuts, but performance is pretty much (3) times faster */
2227                                 if ((ABS(def[0]) > compare) || (ABS(def[1]) > compare) || (ABS(def[2]) > compare)) continue;
2228                                 distance = normalize_v3(def);
2229                                 if (distance < compare ) {
2230                                         /* exclude body points attached with a spring */
2231                                         attached = 0;
2232                                         for (b=obp->nofsprings;b>0;b--) {
2233                                                 bs = sb->bspring + obp->springs[b-1];
2234                                                 if (( ilast-bb == bs->v2) || ( ilast-bb == bs->v1)) {
2235                                                         attached=1;
2236                                                         continue;}
2237                                         }
2238                                         if (!attached) {
2239                                                 float f = bstune / (distance) + bstune / (compare * compare) * distance - 2.0f * bstune / compare;
2240
2241                                                 mid_v3_v3v3(velcenter, bp->vec, obp->vec);
2242                                                 sub_v3_v3v3(dvel, velcenter, bp->vec);
2243                                                 mul_v3_fl(dvel, _final_mass(ob, bp));
2244
2245                                                 Vec3PlusStVec(bp->force, f*(1.0f-sb->balldamp), def);
2246                                                 Vec3PlusStVec(bp->force, sb->balldamp, dvel);
2247
2248                                                 /* exploit force(a, b) == -force(b, a) part2/2 */
2249                                                 sub_v3_v3v3(dvel, velcenter, obp->vec);
2250                                                 mul_v3_fl(dvel, _final_mass(ob, bp));
2251
2252                                                 Vec3PlusStVec(obp->force, sb->balldamp, dvel);
2253                                                 Vec3PlusStVec(obp->force, -f*(1.0f-sb->balldamp), def);
2254                                         }
2255                                 }
2256                         }
2257                 }
2258                 /* naive ball self collision done */
2259
2260                 if (_final_goal(ob, bp) < SOFTGOALSNAP) { /* ommit this bp when it snaps */
2261                         float auxvect[3];
2262                         float velgoal[3];
2263
2264                         /* do goal stuff */
2265                         if (ob->softflag & OB_SB_GOAL) {
2266                                 /* true elastic goal */
2267                                 float ks, kd;
2268                                 sub_v3_v3v3(auxvect, bp->pos, bp->origT);
2269                                 ks  = 1.0f / (1.0f - _final_goal(ob, bp) * sb->goalspring) - 1.0f;
2270                                 bp->force[0]+= -ks*(auxvect[0]);
2271                                 bp->force[1]+= -ks*(auxvect[1]);
2272                                 bp->force[2]+= -ks*(auxvect[2]);
2273
2274                                 /* calulate damping forces generated by goals*/
2275                                 sub_v3_v3v3(velgoal, bp->origS, bp->origE);
2276                                 kd =  sb->goalfrict * sb_fric_force_scale(ob);
2277                                 add_v3_v3v3(auxvect, velgoal, bp->vec);
2278
2279                                 if (forcetime > 0.0f) { /* make sure friction does not become rocket motor on time reversal */
2280                                         bp->force[0]-= kd * (auxvect[0]);
2281                                         bp->force[1]-= kd * (auxvect[1]);
2282                                         bp->force[2]-= kd * (auxvect[2]);
2283                                 }
2284                                 else {
2285                                         bp->force[0]-= kd * (velgoal[0] - bp->vec[0]);
2286                                         bp->force[1]-= kd * (velgoal[1] - bp->vec[1]);
2287                                         bp->force[2]-= kd * (velgoal[2] - bp->vec[2]);
2288                                 }
2289                         }
2290                         /* done goal stuff */
2291
2292                         /* gravitation */
2293                         if (scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
2294                                 float gravity[3];
2295                                 copy_v3_v3(gravity, scene->physics_settings.gravity);
2296                                 mul_v3_fl(gravity, sb_grav_force_scale(ob)*_final_mass(ob, bp)*sb->effector_weights->global_gravity); /* individual mass of node here */
2297                                 add_v3_v3(bp->force, gravity);
2298                         }
2299
2300                         /* particle field & vortex */
2301                         if (do_effector) {
2302                                 EffectedPoint epoint;
2303                                 float kd;
2304                                 float force[3] = {0.0f, 0.0f, 0.0f};
2305                                 float speed[3] = {0.0f, 0.0f, 0.0f};
2306                                 float eval_sb_fric_force_scale = sb_fric_force_scale(ob); /* just for calling function once */
2307                                 pd_point_from_soft(scene, bp->pos, bp->vec, sb->bpoint-bp, &epoint);
2308                                 pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
2309
2310                                 /* apply forcefield*/
2311                                 mul_v3_fl(force, fieldfactor* eval_sb_fric_force_scale);
2312                                 add_v3_v3(bp->force, force);
2313
2314                                 /* BP friction in moving media */
2315                                 kd= sb->mediafrict* eval_sb_fric_force_scale;
2316                                 bp->force[0] -= kd * (bp->vec[0] + windfactor*speed[0]/eval_sb_fric_force_scale);
2317                                 bp->force[1] -= kd * (bp->vec[1] + windfactor*speed[1]/eval_sb_fric_force_scale);
2318                                 bp->force[2] -= kd * (bp->vec[2] + windfactor*speed[2]/eval_sb_fric_force_scale);
2319                                 /* now we'll have nice centrifugal effect for vortex */
2320
2321                         }
2322                         else {
2323                                 /* BP friction in media (not) moving*/
2324                                 float kd = sb->mediafrict* sb_fric_force_scale(ob);
2325                                 /* assume it to be proportional to actual velocity */
2326                                 bp->force[0]-= bp->vec[0]*kd;
2327                                 bp->force[1]-= bp->vec[1]*kd;
2328                                 bp->force[2]-= bp->vec[2]*kd;
2329                                 /* friction in media done */
2330                         }
2331                         /* +++cached collision targets */
2332                         bp->choke = 0.0f;
2333                         bp->choke2 = 0.0f;
2334                         bp->loc_flag &= ~SBF_DOFUZZY;
2335                         if (do_deflector && !(bp->loc_flag & SBF_OUTOFCOLLISION) ) {
2336                                 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;
2337                                 float kd = 1.0f;
2338
2339                                 if (sb_deflect_face(ob, bp->pos, facenormal, defforce, &cf, timenow, vel, &intrusion)) {
2340                                                 if (intrusion < 0.0f) {
2341                                                         sb->scratch->flag |= SBF_DOFUZZY;
2342                                                         bp->loc_flag |= SBF_DOFUZZY;
2343                                                         bp->choke = sb->choke*0.01f;
2344                                                 }
2345
2346                                                         sub_v3_v3v3(cfforce, bp->vec, vel);
2347                                                         Vec3PlusStVec(bp->force, -cf*50.0f, cfforce);
2348
2349                                         Vec3PlusStVec(bp->force, kd, defforce);
2350                                 }
2351
2352                         }
2353                         /* ---cached collision targets */
2354
2355                         /* +++springs */
2356                         iks  = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
2357                         if (ob->softflag & OB_SB_EDGES) {
2358                                 if (sb->bspring) { /* spring list exists at all ? */
2359                                         int b;
2360                                         BodySpring *bs;
2361                                         for (b=bp->nofsprings;b>0;b--) {
2362                                                 bs = sb->bspring + bp->springs[b-1];
2363                                                 if (do_springcollision || do_aero) {
2364                                                         add_v3_v3(bp->force, bs->ext_force);
2365                                                         if (bs->flag & BSF_INTERSECT)
2366                                                                 bp->choke = bs->cf;
2367
2368                                                 }
2369                                                 // sb_spring_force(Object *ob, int bpi, BodySpring *bs, float iks, float forcetime, int nl_flags)
2370                                                 sb_spring_force(ob, ilast-bb, bs, iks, forcetime, 0);
2371                                         }/* loop springs */
2372                                 }/* existing spring list */
2373                         }/*any edges*/
2374                         /* ---springs */
2375                 }/*omit on snap */
2376         }/*loop all bp's*/
2377         return 0; /*done fine*/
2378 }
2379
2380 static void *exec_softbody_calc_forces(void *data)
2381 {
2382         SB_thread_context *pctx = (SB_thread_context*)data;
2383         _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);
2384         return NULL;
2385 }
2386
2387 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)
2388 {
2389         ListBase threads;
2390         SB_thread_context *sb_threads;
2391         int i, totthread, left, dec;
2392         int lowpoints =100; /* wild guess .. may increase with better thread management 'above' or even be UI option sb->spawn_cf_threads_nopts */
2393
2394         /* figure the number of threads while preventing pretty pointless threading overhead */
2395         totthread= BKE_scene_num_threads(scene);
2396         /* what if we got zillions of CPUs running but less to spread*/
2397         while ((totpoint/totthread < lowpoints) && (totthread > 1)) {
2398                 totthread--;
2399         }
2400
2401         /* printf("sb_cf_threads_run spawning %d threads\n", totthread); */
2402
2403         sb_threads= MEM_callocN(sizeof(SB_thread_context)*totthread, "SBThread");
2404         memset(sb_threads, 0, sizeof(SB_thread_context)*totthread);
2405         left = totpoint;
2406         dec = totpoint/totthread +1;
2407         for (i=0; i<totthread; i++) {
2408                 sb_threads[i].scene = scene;
2409                 sb_threads[i].ob = ob;
2410                 sb_threads[i].forcetime = forcetime;
2411                 sb_threads[i].timenow = timenow;
2412                 sb_threads[i].ilast   = left;
2413                 left = left - dec;
2414                 if (left >0) {
2415                         sb_threads[i].ifirst  = left;
2416                 }
2417                 else
2418                         sb_threads[i].ifirst  = 0;
2419                 sb_threads[i].do_effector = do_effector;
2420                 sb_threads[i].do_deflector = do_deflector;
2421                 sb_threads[i].fieldfactor = fieldfactor;
2422                 sb_threads[i].windfactor  = windfactor;
2423                 sb_threads[i].nr= i;
2424                 sb_threads[i].tot= totthread;
2425         }
2426
2427
2428         if (totthread > 1) {
2429                 BLI_init_threads(&threads, exec_softbody_calc_forces, totthread);
2430
2431                 for (i=0; i<totthread; i++)
2432                         BLI_insert_thread(&threads, &sb_threads[i]);
2433
2434                 BLI_end_threads(&threads);
2435         }
2436         else
2437                 exec_softbody_calc_forces(&sb_threads[0]);
2438         /* clean up */
2439         MEM_freeN(sb_threads);
2440 }
2441
2442 static void softbody_calc_forcesEx(Scene *scene, Object *ob, float forcetime, float timenow, int UNUSED(nl_flags))
2443 {
2444 /* rule we never alter free variables :bp->vec bp->pos in here !
2445  * this will ruin adaptive stepsize AKA heun! (BM)
2446  */
2447         SoftBody *sb= ob->soft; /* is supposed to be there */
2448         /*BodyPoint *bproot;*/ /* UNUSED */
2449         ListBase *do_effector = NULL;
2450         /* float gravity; */ /* UNUSED */
2451         /* float iks; */
2452         float fieldfactor = -1.0f, windfactor  = 0.25;
2453         int   do_deflector /*, do_selfcollision*/, do_springcollision, do_aero;
2454
2455         /* gravity = sb->grav * sb_grav_force_scale(ob); */ /* UNUSED */
2456
2457         /* check conditions for various options */
2458         do_deflector= query_external_colliders(scene, ob);
2459         /* do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF)); */ /* UNUSED */
2460         do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL);
2461         do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES));
2462
2463         /* iks  = 1.0f/(1.0f-sb->inspring)-1.0f; */ /* inner spring constants function */ /* UNUSED */
2464         /* bproot= sb->bpoint; */ /* need this for proper spring addressing */ /* UNUSED */
2465
2466         if (do_springcollision || do_aero)
2467         sb_sfesf_threads_run(scene, ob, timenow, sb->totspring, NULL);
2468
2469         /* after spring scan because it uses Effoctors too */
2470         do_effector= pdInitEffectors(scene, ob, NULL, sb->effector_weights, true);
2471
2472         if (do_deflector) {
2473                 float defforce[3];
2474                 do_deflector = sb_detect_aabb_collisionCached(defforce, ob->lay, ob, timenow);
2475         }
2476
2477         sb_cf_threads_run(scene, ob, forcetime, timenow, sb->totpoint, NULL, do_effector, do_deflector, fieldfactor, windfactor);
2478
2479         /* finally add forces caused by face collision */
2480         if (ob->softflag & OB_SB_FACECOLL) scan_for_ext_face_forces(ob, timenow);
2481
2482         /* finish matrix and solve */
2483         pdEndEffectors(&do_effector);
2484 }
2485
2486
2487
2488
2489 static void softbody_calc_forces(Scene *scene, Object *ob, float forcetime, float timenow, int nl_flags)
2490 {
2491         /* redirection to the new threaded Version */
2492         if (!(G.debug_value & 0x10)) { // 16
2493                 softbody_calc_forcesEx(scene, ob, forcetime, timenow, nl_flags);
2494                 return;
2495         }
2496         else {
2497                 /* so the following will die  */
2498                 /* |||||||||||||||||||||||||| */
2499                 /* VVVVVVVVVVVVVVVVVVVVVVVVVV */
2500                 /*backward compatibility note:
2501                 fixing bug [17428] which forces adaptive step size to tiny steps
2502                 in some situations
2503                 .. keeping G.debug_value==17 0x11 option for old files 'needing' the bug*/
2504
2505                 /* rule we never alter free variables :bp->vec bp->pos in here !
2506                 * this will ruin adaptive stepsize AKA heun! (BM)
2507                 */
2508                 SoftBody *sb= ob->soft; /* is supposed to be there */
2509                 BodyPoint  *bp;
2510                 /* BodyPoint *bproot; */ /* UNUSED */
2511                 BodySpring *bs;
2512                 ListBase *do_effector = NULL;
2513                 float iks, ks, kd, gravity[3] = {0.0f, 0.0f, 0.0f};
2514                 float fieldfactor = -1.0f, windfactor  = 0.25f;
2515                 float tune = sb->ballstiff;
2516                 int a, b,  do_deflector, do_selfcollision, do_springcollision, do_aero;
2517
2518
2519                 /* jacobian
2520                 NLboolean success;
2521
2522                 if (nl_flags) {
2523                 nlBegin(NL_SYSTEM);
2524                 nlBegin(NL_MATRIX);
2525                 }
2526                 */
2527
2528
2529                 if (scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
2530                         copy_v3_v3(gravity, scene->physics_settings.gravity);
2531                         mul_v3_fl(gravity, sb_grav_force_scale(ob)*sb->effector_weights->global_gravity);
2532                 }
2533
2534                 /* check conditions for various options */
2535                 do_deflector= query_external_colliders(scene, ob);
2536                 do_selfcollision=((ob->softflag & OB_SB_EDGES) && (sb->bspring)&& (ob->softflag & OB_SB_SELF));
2537                 do_springcollision=do_deflector && (ob->softflag & OB_SB_EDGES) &&(ob->softflag & OB_SB_EDGECOLL);
2538                 do_aero=((sb->aeroedge)&& (ob->softflag & OB_SB_EDGES));
2539
2540                 iks  = 1.0f/(1.0f-sb->inspring)-1.0f ;/* inner spring constants function */
2541                 /* bproot= sb->bpoint; */ /* need this for proper spring addressing */ /* UNUSED */
2542
2543                 if (do_springcollision || do_aero)  scan_for_ext_spring_forces(scene, ob, timenow);
2544                 /* after spring scan because it uses Effoctors too */
2545                 do_effector= pdInitEffectors(scene, ob, NULL, ob->soft->effector_weights, true);
2546
2547                 if (do_deflector) {
2548                         float defforce[3];
2549                         do_deflector = sb_detect_aabb_collisionCached(defforce, ob->lay, ob, timenow);
2550                 }
2551
2552                 for (a=sb->totpoint, bp= sb->bpoint; a>0; a--, bp++) {
2553                         /* clear forces  accumulator */
2554                         bp->force[0] = bp->force[1] = bp->force[2] = 0.0;
2555                         if (nl_flags & NLF_BUILD) {
2556                                 //int ia =3*(sb->totpoint-a);
2557                                 //int op =3*sb->totpoint;
2558                                 /* dF/dV = v */
2559                                 /* jacobioan
2560                                 nlMatrixAdd(op+ia, ia, -forcetime);
2561                                 nlMatrixAdd(op+ia+1, ia+1, -forcetime);
2562                                 nlMatrixAdd(op+ia+2, ia+2, -forcetime);
2563
2564                                 nlMatrixAdd(ia, ia, 1);
2565                                 nlMatrixAdd(ia+1, ia+1, 1);
2566                                 nlMatrixAdd(ia+2, ia+2, 1);
2567
2568                                 nlMatrixAdd(op+ia, op+ia, 1);
2569                                 nlMatrixAdd(op+ia+1, op+ia+1, 1);
2570                                 nlMatrixAdd(op+ia+2, op+ia+2, 1);
2571                                 */
2572
2573
2574                         }
2575
2576                         /* naive ball self collision */
2577                         /* needs to be done if goal snaps or not */
2578                         if (do_selfcollision) {
2579                                 int attached;
2580                                 BodyPoint   *obp;
2581                                 int c, b;
2582                                 float velcenter[3], dvel[3], def[3];
2583                                 float distance;
2584                                 float compare;
2585
2586                                 for (c=sb->totpoint, obp= sb->bpoint; c>=a; c--, obp++) {
2587
2588                                         //if ((bp->octantflag & obp->octantflag) == 0) continue;
2589
2590                                         compare = (obp->colball + bp->colball);
2591                                         sub_v3_v3v3(def, bp->pos, obp->pos);
2592
2593                                         /* rather check the AABBoxes before ever calulating the real distance */
2594                                         /* mathematically it is completly nuts, but performance is pretty much (3) times faster */
2595                                         if ((ABS(def[0]) > compare) || (ABS(def[1]) > compare) || (ABS(def[2]) > compare)) continue;
2596
2597                                         distance = normalize_v3(def);
2598                                         if (distance < compare ) {
2599                                                 /* exclude body points attached with a spring */
2600                                                 attached = 0;
2601                                                 for (b=obp->nofsprings;b>0;b--) {
2602                                                         bs = sb->bspring + obp->springs[b-1];
2603                                                         if (( sb->totpoint-a == bs->v2) || ( sb->totpoint-a == bs->v1)) {
2604                                                                 attached=1;
2605                                                                 continue;}
2606                                                 }
2607                                                 if (!attached) {
2608                                                         float f = tune / (distance) + tune / (compare * compare) * distance - 2.0f * tune/compare;
2609
2610                                                         mid_v3_v3v3(velcenter, bp->vec, obp->vec);
2611                                                         sub_v3_v3v3(dvel, velcenter, bp->vec);
2612                                                         mul_v3_fl(dvel, _final_mass(ob, bp));
2613
2614                                                         Vec3PlusStVec(bp->force, f*(1.0f-sb->balldamp), def);
2615                                                         Vec3PlusStVec(bp->force, sb->balldamp, dvel);
2616
2617                                                         if (nl_flags & NLF_BUILD) {
2618                                                                 //int ia =3*(sb->totpoint-a);
2619                                                                 //int ic =3*(sb->totpoint-c);
2620                                                                 //int op =3*sb->totpoint;
2621                                                                 //float mvel = forcetime*sb->nodemass*sb->balldamp;
2622                                                                 //float mpos = forcetime*tune*(1.0f-sb->balldamp);
2623                                                                 /*some quick and dirty entries to the jacobian*/
2624                                                                 //dfdx_goal(ia, ia, op, mpos);
2625                                                                 //dfdv_goal(ia, ia, mvel);
2626                                                                 /* exploit force(a, b) == -force(b, a) part1/2 */
2627                                                                 //dfdx_goal(ic, ic, op, mpos);
2628                                                                 //dfdv_goal(ic, ic, mvel);
2629
2630
2631                                                                 /*TODO sit down an X-out the true jacobian entries*/
2632                                                                 /*well does not make to much sense because the eigenvalues
2633                                                                 of the jacobian go negative; and negative eigenvalues
2634                                                                 on a complex iterative system z(n+1)=A * z(n)
2635                                                                 give imaginary roots in the charcateristic polynom
2636                                                                 --> solutions that to z(t)=u(t)* exp ( i omega t) --> oscilations we don't want here
2637                                                                 where u(t) is a unknown amplitude function (worst case rising fast)
2638                                                                 */
2639                                                         }
2640
2641                                                         /* exploit force(a, b) == -force(b, a) part2/2 */
2642                                                         sub_v3_v3v3(dvel, velcenter, obp->vec);
2643                                                         mul_v3_fl(dvel, (_final_mass(ob, bp)+_final_mass(ob, obp))/2.0f);
2644
2645                                                         Vec3PlusStVec(obp->force, sb->balldamp, dvel);
2646                                                         Vec3PlusStVec(obp->force, -f*(1.0f-sb->balldamp), def);
2647
2648
2649                                                 }
2650                                         }
2651                                 }
2652                         }
2653                         /* naive ball self collision done */
2654
2655                         if (_final_goal(ob, bp) < SOFTGOALSNAP) { /* ommit this bp when it snaps */
2656                                 float auxvect[3];
2657                                 float velgoal[3];
2658
2659                                 /* do goal stuff */
2660                                 if (ob->softflag & OB_SB_GOAL) {
2661                                         /* true elastic goal */
2662                                         sub_v3_v3v3(auxvect, bp->pos, bp->origT);
2663                                         ks  = 1.0f / (1.0f- _final_goal(ob, bp) * sb->goalspring) - 1.0f;
2664                                         bp->force[0]+= -ks*(auxvect[0]);
2665                                         bp->force[1]+= -ks*(auxvect[1]);
2666                                         bp->force[2]+= -ks*(auxvect[2]);
2667
2668                                         if (nl_flags & NLF_BUILD) {
2669                                                 //int ia =3*(sb->totpoint-a);
2670                                                 //int op =3*(sb->totpoint);
2671                                                 /* depending on my pos */
2672                                                 //dfdx_goal(ia, ia, op, ks*forcetime);
2673                                         }
2674
2675
2676                                         /* calulate damping forces generated by goals*/
2677                                         sub_v3_v3v3(velgoal, bp->origS, bp->origE);
2678                                         kd = sb->goalfrict * sb_fric_force_scale(ob);
2679                                         add_v3_v3v3(auxvect, velgoal, bp->vec);
2680
2681                                         if (forcetime > 0.0f) { /* make sure friction does not become rocket motor on time reversal */
2682                                                 bp->force[0]-= kd * (auxvect[0]);
2683                                                 bp->force[1]-= kd * (auxvect[1]);
2684                                                 bp->force[2]-= kd * (auxvect[2]);
2685                                                 if (nl_flags & NLF_BUILD) {
2686                                                         //int ia =3*(sb->totpoint-a);
2687                                                         normalize_v3(auxvect);
2688                                                         /* depending on my vel */
2689                                                         //dfdv_goal(ia, ia, kd*forcetime);
2690                                                 }
2691
2692                                         }
2693                                         else {
2694                                                 bp->force[0]-= kd * (velgoal[0] - bp->vec[0]);
2695                                                 bp->force[1]-= kd * (velgoal[1] - bp->vec[1]);
2696                                                 bp->force[2]-= kd * (velgoal[2] - bp->vec[2]);
2697                                         }
2698                                 }
2699                                 /* done goal stuff */
2700
2701
2702                                 /* gravitation */
2703                                 madd_v3_v3fl(bp->force, gravity, _final_mass(ob, bp)); /* individual mass of node here */
2704
2705
2706                                 /* particle field & vortex */
2707                                 if (do_effector) {
2708                                         EffectedPoint epoint;
2709                                         float force[3] = {0.0f, 0.0f, 0.0f};
2710                                         float speed[3] = {0.0f, 0.0f, 0.0f};
2711                                         float eval_sb_fric_force_scale = sb_fric_force_scale(ob); /* just for calling function once */
2712                                         pd_point_from_soft(scene, bp->pos, bp->vec, sb->bpoint-bp, &epoint);
2713                                         pdDoEffectors(do_effector, NULL, sb->effector_weights, &epoint, force, speed);
2714
2715                                         /* apply forcefield*/
2716                                         mul_v3_fl(force, fieldfactor* eval_sb_fric_force_scale);
2717                                         add_v3_v3(bp->force, force);
2718
2719                                         /* BP friction in moving media */