9ae53293397a7e366193e1963f0aae1756b8a37b
[blender.git] / source / blender / blenkernel / intern / implicit.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/implicit.c
29  *  \ingroup bke
30  */
31
32 #include "implicit.h"
33
34 #ifdef IMPLICIT_SOLVER_BLENDER
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_scene_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_object_force.h"
41 #include "DNA_meshdata_types.h"
42 #include "DNA_texture_types.h"
43
44 #include "BLI_math.h"
45 #include "BLI_linklist.h"
46 #include "BLI_utildefines.h"
47
48 #include "BKE_cloth.h"
49 #include "BKE_collision.h"
50 #include "BKE_effect.h"
51 #include "BKE_global.h"
52
53 #ifdef __GNUC__
54 #  pragma GCC diagnostic ignored "-Wtype-limits"
55 #endif
56
57 #ifdef _OPENMP
58 #  define CLOTH_OPENMP_LIMIT 512
59 #endif
60
61 #if 0  /* debug timing */
62 #ifdef _WIN32
63 #include <windows.h>
64 static LARGE_INTEGER _itstart, _itend;
65 static LARGE_INTEGER ifreq;
66 static void itstart(void)
67 {
68         static int first = 1;
69         if (first) {
70                 QueryPerformanceFrequency(&ifreq);
71                 first = 0;
72         }
73         QueryPerformanceCounter(&_itstart);
74 }
75 static void itend(void)
76 {
77         QueryPerformanceCounter(&_itend);
78 }
79 double itval(void)
80 {
81         return ((double)_itend.QuadPart -
82                         (double)_itstart.QuadPart)/((double)ifreq.QuadPart);
83 }
84 #else
85 #include <sys/time.h>
86 // intrinsics need better compile flag checking
87 // #include <xmmintrin.h>
88 // #include <pmmintrin.h>
89 // #include <pthread.h>
90
91 static struct timeval _itstart, _itend;
92 static struct timezone itz;
93 static void itstart(void)
94 {
95         gettimeofday(&_itstart, &itz);
96 }
97 static void itend(void)
98 {
99         gettimeofday(&_itend, &itz);
100 }
101 static double itval(void)
102 {
103         double t1, t2;
104         t1 =  (double)_itstart.tv_sec + (double)_itstart.tv_usec/(1000*1000);
105         t2 =  (double)_itend.tv_sec + (double)_itend.tv_usec/(1000*1000);
106         return t2-t1;
107 }
108 #endif
109 #endif  /* debug timing */
110
111 static float I[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
112 static float ZERO[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
113
114 /* ==== hash functions for debugging ==== */
115 static unsigned int hash_int_2d(unsigned int kx, unsigned int ky)
116 {
117 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
118
119         unsigned int a, b, c;
120
121         a = b = c = 0xdeadbeef + (2 << 2) + 13;
122         a += kx;
123         b += ky;
124
125         c ^= b; c -= rot(b,14);
126         a ^= c; a -= rot(c,11);
127         b ^= a; b -= rot(a,25);
128         c ^= b; c -= rot(b,16);
129         a ^= c; a -= rot(c,4);
130         b ^= a; b -= rot(a,14);
131         c ^= b; c -= rot(b,24);
132
133         return c;
134
135 #undef rot
136 }
137
138 static int hash_vertex(int type, int vertex)
139 {
140         return hash_int_2d((unsigned int)type, (unsigned int)vertex);
141 }
142
143 static int hash_collpair(int type, CollPair *collpair)
144 {
145         return hash_int_2d((unsigned int)type, hash_int_2d((unsigned int)collpair->face1, (unsigned int)collpair->face2));
146 }
147 /* ================ */
148
149 /*
150 #define C99
151 #ifdef C99
152 #defineDO_INLINE inline 
153 #else 
154 #defineDO_INLINE static 
155 #endif
156 */
157 struct Cloth;
158
159 //////////////////////////////////////////
160 /* fast vector / matrix library, enhancements are welcome :) -dg */
161 /////////////////////////////////////////
162
163 /* DEFINITIONS */
164 typedef float lfVector[3];
165 typedef struct fmatrix3x3 {
166         float m[3][3]; /* 3x3 matrix */
167         unsigned int c, r; /* column and row number */
168         /* int pinned; // is this vertex allowed to move? */
169         float n1, n2, n3; /* three normal vectors for collision constrains */
170         unsigned int vcount; /* vertex count */
171         unsigned int scount; /* spring count */ 
172 } fmatrix3x3;
173
174 ///////////////////////////
175 // float[3] vector
176 ///////////////////////////
177 /* simple vector code */
178 /* STATUS: verified */
179 DO_INLINE void mul_fvector_S(float to[3], float from[3], float scalar)
180 {
181         to[0] = from[0] * scalar;
182         to[1] = from[1] * scalar;
183         to[2] = from[2] * scalar;
184 }
185 /* simple v^T * v product ("outer product") */
186 /* STATUS: HAS TO BE verified (*should* work) */
187 DO_INLINE void mul_fvectorT_fvector(float to[3][3], float vectorA[3], float vectorB[3])
188 {
189         mul_fvector_S(to[0], vectorB, vectorA[0]);
190         mul_fvector_S(to[1], vectorB, vectorA[1]);
191         mul_fvector_S(to[2], vectorB, vectorA[2]);
192 }
193 /* simple v^T * v product with scalar ("outer product") */
194 /* STATUS: HAS TO BE verified (*should* work) */
195 DO_INLINE void mul_fvectorT_fvectorS(float to[3][3], float vectorA[3], float vectorB[3], float aS)
196 {       
197         mul_fvectorT_fvector(to, vectorA, vectorB);
198         
199         mul_fvector_S(to[0], to[0], aS);
200         mul_fvector_S(to[1], to[1], aS);
201         mul_fvector_S(to[2], to[2], aS);
202 }
203
204 #if 0
205 /* printf vector[3] on console: for debug output */
206 static void print_fvector(float m3[3])
207 {
208         printf("%f\n%f\n%f\n\n", m3[0], m3[1], m3[2]);
209 }
210
211 ///////////////////////////
212 // long float vector float (*)[3]
213 ///////////////////////////
214 /* print long vector on console: for debug output */
215 DO_INLINE void print_lfvector(float (*fLongVector)[3], unsigned int verts)
216 {
217         unsigned int i = 0;
218         for (i = 0; i < verts; i++) {
219                 print_fvector(fLongVector[i]);
220         }
221 }
222 #endif
223
224 /* create long vector */
225 DO_INLINE lfVector *create_lfvector(unsigned int verts)
226 {
227         /* TODO: check if memory allocation was successful */
228         return  (lfVector *)MEM_callocN(verts * sizeof(lfVector), "cloth_implicit_alloc_vector");
229         // return (lfVector *)cloth_aligned_malloc(&MEMORY_BASE, verts * sizeof(lfVector));
230 }
231 /* delete long vector */
232 DO_INLINE void del_lfvector(float (*fLongVector)[3])
233 {
234         if (fLongVector != NULL) {
235                 MEM_freeN(fLongVector);
236                 // cloth_aligned_free(&MEMORY_BASE, fLongVector);
237         }
238 }
239 /* copy long vector */
240 DO_INLINE void cp_lfvector(float (*to)[3], float (*from)[3], unsigned int verts)
241 {
242         memcpy(to, from, verts * sizeof(lfVector));
243 }
244 /* init long vector with float[3] */
245 DO_INLINE void init_lfvector(float (*fLongVector)[3], float vector[3], unsigned int verts)
246 {
247         unsigned int i = 0;
248         for (i = 0; i < verts; i++) {
249                 copy_v3_v3(fLongVector[i], vector);
250         }
251 }
252 /* zero long vector with float[3] */
253 DO_INLINE void zero_lfvector(float (*to)[3], unsigned int verts)
254 {
255         memset(to, 0.0f, verts * sizeof(lfVector));
256 }
257 /* multiply long vector with scalar*/
258 DO_INLINE void mul_lfvectorS(float (*to)[3], float (*fLongVector)[3], float scalar, unsigned int verts)
259 {
260         unsigned int i = 0;
261
262         for (i = 0; i < verts; i++) {
263                 mul_fvector_S(to[i], fLongVector[i], scalar);
264         }
265 }
266 /* multiply long vector with scalar*/
267 /* A -= B * float */
268 DO_INLINE void submul_lfvectorS(float (*to)[3], float (*fLongVector)[3], float scalar, unsigned int verts)
269 {
270         unsigned int i = 0;
271         for (i = 0; i < verts; i++) {
272                 VECSUBMUL(to[i], fLongVector[i], scalar);
273         }
274 }
275 /* dot product for big vector */
276 DO_INLINE float dot_lfvector(float (*fLongVectorA)[3], float (*fLongVectorB)[3], unsigned int verts)
277 {
278         long i = 0;
279         float temp = 0.0;
280 // XXX brecht, disabled this for now (first schedule line was already disabled),
281 // due to non-commutative nature of floating point ops this makes the sim give
282 // different results each time you run it!
283 // schedule(guided, 2)
284 //#pragma omp parallel for reduction(+: temp) if (verts > CLOTH_OPENMP_LIMIT)
285         for (i = 0; i < (long)verts; i++) {
286                 temp += dot_v3v3(fLongVectorA[i], fLongVectorB[i]);
287         }
288         return temp;
289 }
290 /* A = B + C  --> for big vector */
291 DO_INLINE void add_lfvector_lfvector(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], unsigned int verts)
292 {
293         unsigned int i = 0;
294
295         for (i = 0; i < verts; i++) {
296                 VECADD(to[i], fLongVectorA[i], fLongVectorB[i]);
297         }
298
299 }
300 /* A = B + C * float --> for big vector */
301 DO_INLINE void add_lfvector_lfvectorS(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], float bS, unsigned int verts)
302 {
303         unsigned int i = 0;
304
305         for (i = 0; i < verts; i++) {
306                 VECADDS(to[i], fLongVectorA[i], fLongVectorB[i], bS);
307
308         }
309 }
310 /* A = B * float + C * float --> for big vector */
311 DO_INLINE void add_lfvectorS_lfvectorS(float (*to)[3], float (*fLongVectorA)[3], float aS, float (*fLongVectorB)[3], float bS, unsigned int verts)
312 {
313         unsigned int i = 0;
314
315         for (i = 0; i < verts; i++) {
316                 VECADDSS(to[i], fLongVectorA[i], aS, fLongVectorB[i], bS);
317         }
318 }
319 /* A = B - C * float --> for big vector */
320 DO_INLINE void sub_lfvector_lfvectorS(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], float bS, unsigned int verts)
321 {
322         unsigned int i = 0;
323         for (i = 0; i < verts; i++) {
324                 VECSUBS(to[i], fLongVectorA[i], fLongVectorB[i], bS);
325         }
326
327 }
328 /* A = B - C --> for big vector */
329 DO_INLINE void sub_lfvector_lfvector(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], unsigned int verts)
330 {
331         unsigned int i = 0;
332
333         for (i = 0; i < verts; i++) {
334                 sub_v3_v3v3(to[i], fLongVectorA[i], fLongVectorB[i]);
335         }
336
337 }
338 ///////////////////////////
339 // 3x3 matrix
340 ///////////////////////////
341 #if 0
342 /* printf 3x3 matrix on console: for debug output */
343 static void print_fmatrix(float m3[3][3])
344 {
345         printf("%f\t%f\t%f\n", m3[0][0], m3[0][1], m3[0][2]);
346         printf("%f\t%f\t%f\n", m3[1][0], m3[1][1], m3[1][2]);
347         printf("%f\t%f\t%f\n\n", m3[2][0], m3[2][1], m3[2][2]);
348 }
349
350 static void print_sparse_matrix(fmatrix3x3 *m)
351 {
352         if (m) {
353                 unsigned int i;
354                 for (i = 0; i < m[0].vcount + m[0].scount; i++) {
355                         printf("%d:\n", i);
356                         print_fmatrix(m[i].m);
357                 }
358         }
359 }
360 #endif
361
362 static void print_lvector(lfVector *v, int numverts)
363 {
364         int i;
365         for (i = 0; i < numverts; ++i) {
366                 if (i > 0)
367                         printf("\n");
368                 
369                 printf("%f,\n", v[i][0]);
370                 printf("%f,\n", v[i][1]);
371                 printf("%f,\n", v[i][2]);
372         }
373 }
374
375 static void print_bfmatrix(fmatrix3x3 *m)
376 {
377         int tot = m[0].vcount + m[0].scount;
378         int size = m[0].vcount * 3;
379         float *t = MEM_callocN(sizeof(float) * size*size, "bfmatrix");
380         int q, i, j;
381         
382         for (q = 0; q < tot; ++q) {
383                 int k = 3 * m[q].r;
384                 int l = 3 * m[q].c;
385                 
386                 for (j = 0; j < 3; ++j) {
387                         for (i = 0; i < 3; ++i) {
388 //                              if (t[k + i + (l + j) * size] != 0.0f) {
389 //                                      printf("warning: overwriting value at %d, %d\n", m[q].r, m[q].c);
390 //                              }
391                                 if (k == l) {
392                                         t[k + i + (k + j) * size] += m[q].m[i][j];
393                                 }
394                                 else {
395                                         t[k + i + (l + j) * size] += m[q].m[i][j];
396                                         t[l + j + (k + i) * size] += m[q].m[j][i];
397                                 }
398                         }
399                 }
400         }
401         
402         for (j = 0; j < size; ++j) {
403                 if (j > 0 && j % 3 == 0)
404                         printf("\n");
405                 
406                 for (i = 0; i < size; ++i) {
407                         if (i > 0 && i % 3 == 0)
408                                 printf("  ");
409                         
410                         implicit_print_matrix_elem(t[i + j * size]);
411                 }
412                 printf("\n");
413         }
414         
415         MEM_freeN(t);
416 }
417
418 /* copy 3x3 matrix */
419 DO_INLINE void cp_fmatrix(float to[3][3], float from[3][3])
420 {
421         // memcpy(to, from, sizeof (float) * 9);
422         copy_v3_v3(to[0], from[0]);
423         copy_v3_v3(to[1], from[1]);
424         copy_v3_v3(to[2], from[2]);
425 }
426
427 /* copy 3x3 matrix */
428 DO_INLINE void initdiag_fmatrixS(float to[3][3], float aS)
429 {
430         cp_fmatrix(to, ZERO);
431         
432         to[0][0] = aS;
433         to[1][1] = aS;
434         to[2][2] = aS;
435 }
436
437 #if 0
438 /* calculate determinant of 3x3 matrix */
439 DO_INLINE float det_fmatrix(float m[3][3])
440 {
441         return  m[0][0]*m[1][1]*m[2][2] + m[1][0]*m[2][1]*m[0][2] + m[0][1]*m[1][2]*m[2][0] 
442                         -m[0][0]*m[1][2]*m[2][1] - m[0][1]*m[1][0]*m[2][2] - m[2][0]*m[1][1]*m[0][2];
443 }
444
445 DO_INLINE void inverse_fmatrix(float to[3][3], float from[3][3])
446 {
447         unsigned int i, j;
448         float d;
449
450         if ((d=det_fmatrix(from)) == 0) {
451                 printf("can't build inverse");
452                 exit(0);
453         }
454         for (i=0;i<3;i++) {
455                 for (j=0;j<3;j++) {
456                         int i1=(i+1)%3;
457                         int i2=(i+2)%3;
458                         int j1=(j+1)%3;
459                         int j2=(j+2)%3;
460                         // reverse indexs i&j to take transpose
461                         to[j][i] = (from[i1][j1]*from[i2][j2]-from[i1][j2]*from[i2][j1])/d;
462                         /*
463                         if (i==j)
464                         to[i][j] = 1.0f / from[i][j];
465                         else
466                         to[i][j] = 0;
467                         */
468                 }
469         }
470
471 }
472 #endif
473
474 /* 3x3 matrix multiplied by a scalar */
475 /* STATUS: verified */
476 DO_INLINE void mul_fmatrix_S(float matrix[3][3], float scalar)
477 {
478         mul_fvector_S(matrix[0], matrix[0], scalar);
479         mul_fvector_S(matrix[1], matrix[1], scalar);
480         mul_fvector_S(matrix[2], matrix[2], scalar);
481 }
482
483 /* a vector multiplied by a 3x3 matrix */
484 /* STATUS: verified */
485 DO_INLINE void mul_fvector_fmatrix(float *to, float *from, float matrix[3][3])
486 {
487         to[0] = matrix[0][0]*from[0] + matrix[1][0]*from[1] + matrix[2][0]*from[2];
488         to[1] = matrix[0][1]*from[0] + matrix[1][1]*from[1] + matrix[2][1]*from[2];
489         to[2] = matrix[0][2]*from[0] + matrix[1][2]*from[1] + matrix[2][2]*from[2];
490 }
491
492 /* 3x3 matrix multiplied by a vector */
493 /* STATUS: verified */
494 DO_INLINE void mul_fmatrix_fvector(float *to, float matrix[3][3], float from[3])
495 {
496         to[0] = dot_v3v3(matrix[0], from);
497         to[1] = dot_v3v3(matrix[1], from);
498         to[2] = dot_v3v3(matrix[2], from);
499 }
500 /* 3x3 matrix addition with 3x3 matrix */
501 DO_INLINE void add_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3])
502 {
503         VECADD(to[0], matrixA[0], matrixB[0]);
504         VECADD(to[1], matrixA[1], matrixB[1]);
505         VECADD(to[2], matrixA[2], matrixB[2]);
506 }
507 /* A -= B*x + C*y (3x3 matrix sub-addition with 3x3 matrix) */
508 DO_INLINE void subadd_fmatrixS_fmatrixS(float to[3][3], float matrixA[3][3], float aS, float matrixB[3][3], float bS)
509 {
510         VECSUBADDSS(to[0], matrixA[0], aS, matrixB[0], bS);
511         VECSUBADDSS(to[1], matrixA[1], aS, matrixB[1], bS);
512         VECSUBADDSS(to[2], matrixA[2], aS, matrixB[2], bS);
513 }
514 /* A = B - C (3x3 matrix subtraction with 3x3 matrix) */
515 DO_INLINE void sub_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3])
516 {
517         sub_v3_v3v3(to[0], matrixA[0], matrixB[0]);
518         sub_v3_v3v3(to[1], matrixA[1], matrixB[1]);
519         sub_v3_v3v3(to[2], matrixA[2], matrixB[2]);
520 }
521 /////////////////////////////////////////////////////////////////
522 // special functions
523 /////////////////////////////////////////////////////////////////
524 /* 3x3 matrix multiplied+added by a vector */
525 /* STATUS: verified */
526 DO_INLINE void muladd_fmatrix_fvector(float to[3], float matrix[3][3], float from[3])
527 {
528         to[0] += dot_v3v3(matrix[0], from);
529         to[1] += dot_v3v3(matrix[1], from);
530         to[2] += dot_v3v3(matrix[2], from);
531 }
532 /////////////////////////////////////////////////////////////////
533
534 ///////////////////////////
535 // SPARSE SYMMETRIC big matrix with 3x3 matrix entries
536 ///////////////////////////
537 /* printf a big matrix on console: for debug output */
538 #if 0
539 static void print_bfmatrix(fmatrix3x3 *m3)
540 {
541         unsigned int i = 0;
542
543         for (i = 0; i < m3[0].vcount + m3[0].scount; i++)
544         {
545                 print_fmatrix(m3[i].m);
546         }
547 }
548 #endif
549
550 /* create big matrix */
551 DO_INLINE fmatrix3x3 *create_bfmatrix(unsigned int verts, unsigned int springs)
552 {
553         // TODO: check if memory allocation was successful */
554         fmatrix3x3 *temp = (fmatrix3x3 *)MEM_callocN(sizeof(fmatrix3x3) * (verts + springs), "cloth_implicit_alloc_matrix");
555         temp[0].vcount = verts;
556         temp[0].scount = springs;
557         return temp;
558 }
559 /* delete big matrix */
560 DO_INLINE void del_bfmatrix(fmatrix3x3 *matrix)
561 {
562         if (matrix != NULL) {
563                 MEM_freeN(matrix);
564         }
565 }
566
567 /* copy big matrix */
568 DO_INLINE void cp_bfmatrix(fmatrix3x3 *to, fmatrix3x3 *from)
569 {
570         // TODO bounds checking
571         memcpy(to, from, sizeof(fmatrix3x3) * (from[0].vcount+from[0].scount));
572 }
573
574 /* init big matrix */
575 // slow in parallel
576 DO_INLINE void init_bfmatrix(fmatrix3x3 *matrix, float m3[3][3])
577 {
578         unsigned int i;
579
580         for (i = 0; i < matrix[0].vcount+matrix[0].scount; i++) {
581                 cp_fmatrix(matrix[i].m, m3); 
582         }
583 }
584
585 /* init the diagonal of big matrix */
586 // slow in parallel
587 DO_INLINE void initdiag_bfmatrix(fmatrix3x3 *matrix, float m3[3][3])
588 {
589         unsigned int i, j;
590         float tmatrix[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
591
592         for (i = 0; i < matrix[0].vcount; i++) {
593                 cp_fmatrix(matrix[i].m, m3); 
594         }
595         for (j = matrix[0].vcount; j < matrix[0].vcount+matrix[0].scount; j++) {
596                 cp_fmatrix(matrix[j].m, tmatrix); 
597         }
598 }
599
600 /* SPARSE SYMMETRIC multiply big matrix with long vector*/
601 /* STATUS: verified */
602 DO_INLINE void mul_bfmatrix_lfvector( float (*to)[3], fmatrix3x3 *from, lfVector *fLongVector)
603 {
604         unsigned int i = 0;
605         unsigned int vcount = from[0].vcount;
606         lfVector *temp = create_lfvector(vcount);
607         
608         zero_lfvector(to, vcount);
609
610 #pragma omp parallel sections private(i) if (vcount > CLOTH_OPENMP_LIMIT)
611         {
612 #pragma omp section
613                 {
614                         for (i = from[0].vcount; i < from[0].vcount+from[0].scount; i++) {
615                                 muladd_fmatrix_fvector(to[from[i].c], from[i].m, fLongVector[from[i].r]);
616                         }
617                 }
618 #pragma omp section
619                 {
620                         for (i = 0; i < from[0].vcount+from[0].scount; i++) {
621                                 muladd_fmatrix_fvector(temp[from[i].r], from[i].m, fLongVector[from[i].c]);
622                         }
623                 }
624         }
625         add_lfvector_lfvector(to, to, temp, from[0].vcount);
626         
627         del_lfvector(temp);
628         
629         
630 }
631
632 /* SPARSE SYMMETRIC sub big matrix with big matrix*/
633 /* A -= B * float + C * float --> for big matrix */
634 /* VERIFIED */
635 DO_INLINE void subadd_bfmatrixS_bfmatrixS( fmatrix3x3 *to, fmatrix3x3 *from, float aS,  fmatrix3x3 *matrix, float bS)
636 {
637         unsigned int i = 0;
638
639         /* process diagonal elements */
640         for (i = 0; i < matrix[0].vcount+matrix[0].scount; i++) {
641                 subadd_fmatrixS_fmatrixS(to[i].m, from[i].m, aS, matrix[i].m, bS);
642         }
643
644 }
645
646 ///////////////////////////////////////////////////////////////////
647 // simulator start
648 ///////////////////////////////////////////////////////////////////
649 typedef struct RootTransform {
650         float loc[3];
651         float rot[3][3];
652         
653         float vel[3];
654         float omega[3];
655         
656         float acc[3];
657         float domega_dt[3];
658 } RootTransform;
659
660 typedef struct Implicit_Data  {
661         /* inputs */
662         fmatrix3x3 *bigI;                       /* identity (constant) */
663         fmatrix3x3 *M;                          /* masses */
664         lfVector *F;                            /* forces */
665         fmatrix3x3 *dFdV, *dFdX;        /* force jacobians */
666         RootTransform *root;            /* root transforms */
667         
668         /* motion state data */
669         lfVector *X, *Xnew;                     /* positions */
670         lfVector *V, *Vnew;                     /* velocities */
671         
672         /* internal solver data */
673         lfVector *B;                            /* B for A*dV = B */
674         fmatrix3x3 *A;                          /* A for A*dV = B */
675         
676         lfVector *dV;                           /* velocity change (solution of A*dV = B) */
677         lfVector *z;                            /* target velocity in constrained directions */
678         fmatrix3x3 *S;                          /* filtering matrix for constraints */
679         fmatrix3x3 *P, *Pinv;           /* pre-conditioning matrix */
680 } Implicit_Data;
681
682 int implicit_init(Object *UNUSED(ob), ClothModifierData *clmd)
683 {
684         unsigned int i = 0;
685         Cloth *cloth = NULL;
686         ClothVertex *verts = NULL;
687         ClothSpring *spring = NULL;
688         Implicit_Data *id = NULL;
689         LinkNode *search = NULL;
690         
691         if (G.debug_value > 0)
692                 printf("implicit_init\n");
693
694         // init memory guard
695         // BLI_listbase_clear(&MEMORY_BASE);
696
697         cloth = (Cloth *)clmd->clothObject;
698         verts = cloth->verts;
699
700         // create implicit base
701         id = (Implicit_Data *)MEM_callocN(sizeof(Implicit_Data), "implicit vecmat");
702         cloth->implicit = id;
703
704         /* process diagonal elements */
705         id->A = create_bfmatrix(cloth->numverts, cloth->numsprings);
706         id->dFdV = create_bfmatrix(cloth->numverts, cloth->numsprings);
707         id->dFdX = create_bfmatrix(cloth->numverts, cloth->numsprings);
708         id->S = create_bfmatrix(cloth->numverts, 0);
709         id->Pinv = create_bfmatrix(cloth->numverts, cloth->numsprings);
710         id->P = create_bfmatrix(cloth->numverts, cloth->numsprings);
711         id->bigI = create_bfmatrix(cloth->numverts, cloth->numsprings); // TODO 0 springs
712         id->M = create_bfmatrix(cloth->numverts, cloth->numsprings);
713         id->X = create_lfvector(cloth->numverts);
714         id->Xnew = create_lfvector(cloth->numverts);
715         id->V = create_lfvector(cloth->numverts);
716         id->Vnew = create_lfvector(cloth->numverts);
717         id->F = create_lfvector(cloth->numverts);
718         id->B = create_lfvector(cloth->numverts);
719         id->dV = create_lfvector(cloth->numverts);
720         id->z = create_lfvector(cloth->numverts);
721
722         id->root = MEM_callocN(sizeof(RootTransform) * cloth->numverts, "root transforms");
723
724         for (i = 0; i < cloth->numverts; i++) {
725                 id->A[i].r = id->A[i].c = id->dFdV[i].r = id->dFdV[i].c = id->dFdX[i].r = id->dFdX[i].c = id->P[i].c = id->P[i].r = id->Pinv[i].c = id->Pinv[i].r = id->bigI[i].c = id->bigI[i].r = id->M[i].r = id->M[i].c = i;
726                 
727                 initdiag_fmatrixS(id->M[i].m, verts[i].mass);
728         }
729
730         // init springs 
731         search = cloth->springs;
732         for (i = 0; i < cloth->numsprings; i++) {
733                 spring = search->link;
734                 
735                 // dFdV_start[i].r = big_I[i].r = big_zero[i].r = 
736                 id->A[i+cloth->numverts].r = id->dFdV[i+cloth->numverts].r = id->dFdX[i+cloth->numverts].r = 
737                                 id->P[i+cloth->numverts].r = id->Pinv[i+cloth->numverts].r = id->bigI[i+cloth->numverts].r = id->M[i+cloth->numverts].r = spring->ij;
738
739                 // dFdV_start[i].c = big_I[i].c = big_zero[i].c = 
740                 id->A[i+cloth->numverts].c = id->dFdV[i+cloth->numverts].c = id->dFdX[i+cloth->numverts].c = 
741                                 id->P[i+cloth->numverts].c = id->Pinv[i+cloth->numverts].c = id->bigI[i+cloth->numverts].c = id->M[i+cloth->numverts].c = spring->kl;
742
743                 spring->matrix_index = i + cloth->numverts;
744                 
745                 search = search->next;
746         }
747         
748         initdiag_bfmatrix(id->bigI, I);
749
750         for (i = 0; i < cloth->numverts; i++) {
751                 copy_v3_v3(id->X[i], verts[i].x);
752         }
753
754         return 1;
755 }
756
757 int     implicit_free(ClothModifierData *clmd)
758 {
759         Implicit_Data *id;
760         Cloth *cloth;
761         cloth = (Cloth *)clmd->clothObject;
762
763         if (cloth) {
764                 id = cloth->implicit;
765
766                 if (id) {
767                         del_bfmatrix(id->A);
768                         del_bfmatrix(id->dFdV);
769                         del_bfmatrix(id->dFdX);
770                         del_bfmatrix(id->S);
771                         del_bfmatrix(id->P);
772                         del_bfmatrix(id->Pinv);
773                         del_bfmatrix(id->bigI);
774                         del_bfmatrix(id->M);
775
776                         del_lfvector(id->X);
777                         del_lfvector(id->Xnew);
778                         del_lfvector(id->V);
779                         del_lfvector(id->Vnew);
780                         del_lfvector(id->F);
781                         del_lfvector(id->B);
782                         del_lfvector(id->dV);
783                         del_lfvector(id->z);
784
785                         MEM_freeN(id->root);
786
787                         MEM_freeN(id);
788                 }
789         }
790
791         return 1;
792 }
793
794 /* ==== Transformation of Moving Reference Frame ====
795  *   x_world, v_world, f_world, a_world, dfdx_world, dfdv_world : state variables in world space
796  *   x_root, v_root, f_root, a_root, dfdx_root, dfdv_root       : state variables in root space
797  *   
798  *   x0 : translation of the root frame (hair root location)
799  *   v0 : linear velocity of the root frame
800  *   a0 : acceleration of the root frame
801  *   R : rotation matrix of the root frame
802  *   w : angular velocity of the root frame
803  *   dwdt : angular acceleration of the root frame
804  */
805
806 /* x_root = R^T * x_world */
807 BLI_INLINE void loc_world_to_root(float r[3], const float v[3], const RootTransform *root)
808 {
809 #ifdef CLOTH_ROOT_FRAME
810         sub_v3_v3v3(r, v, root->loc);
811         mul_transposed_m3_v3((float (*)[3])root->rot, r);
812 #else
813         copy_v3_v3(r, v);
814         (void)root;
815 #endif
816 }
817
818 /* x_world = R * x_root */
819 BLI_INLINE void loc_root_to_world(float r[3], const float v[3], const RootTransform *root)
820 {
821 #ifdef CLOTH_ROOT_FRAME
822         copy_v3_v3(r, v);
823         mul_m3_v3((float (*)[3])root->rot, r);
824         add_v3_v3(r, root->loc);
825 #else
826         copy_v3_v3(r, v);
827         (void)root;
828 #endif
829 }
830
831 /* v_root = cross(w, x_root) + R^T*(v_world - v0) */
832 BLI_INLINE void vel_world_to_root(float r[3], const float x_root[3], const float v[3], const RootTransform *root)
833 {
834 #ifdef CLOTH_ROOT_FRAME
835         float angvel[3];
836         cross_v3_v3v3(angvel, root->omega, x_root);
837         
838         sub_v3_v3v3(r, v, root->vel);
839         mul_transposed_m3_v3((float (*)[3])root->rot, r);
840         add_v3_v3(r, angvel);
841 #else
842         copy_v3_v3(r, v);
843         (void)x_root;
844         (void)root;
845 #endif
846 }
847
848 /* v_world = R*(v_root - cross(w, x_root)) + v0 */
849 BLI_INLINE void vel_root_to_world(float r[3], const float x_root[3], const float v[3], const RootTransform *root)
850 {
851 #ifdef CLOTH_ROOT_FRAME
852         float angvel[3];
853         cross_v3_v3v3(angvel, root->omega, x_root);
854         
855         sub_v3_v3v3(r, v, angvel);
856         mul_m3_v3((float (*)[3])root->rot, r);
857         add_v3_v3(r, root->vel);
858 #else
859         copy_v3_v3(r, v);
860         (void)x_root;
861         (void)root;
862 #endif
863 }
864
865 /* a_root = -cross(dwdt, x_root) - 2*cross(w, v_root) - cross(w, cross(w, x_root)) + R^T*(a_world - a0) */
866 BLI_INLINE void force_world_to_root(float r[3], const float x_root[3], const float v_root[3], const float force[3], float mass, const RootTransform *root)
867 {
868 #ifdef CLOTH_ROOT_FRAME
869         float euler[3], coriolis[3], centrifugal[3], rotvel[3];
870         
871         cross_v3_v3v3(euler, root->domega_dt, x_root);
872         cross_v3_v3v3(coriolis, root->omega, v_root);
873         mul_v3_fl(coriolis, 2.0f);
874         cross_v3_v3v3(rotvel, root->omega, x_root);
875         cross_v3_v3v3(centrifugal, root->omega, rotvel);
876         
877         madd_v3_v3v3fl(r, force, root->acc, mass);
878         mul_transposed_m3_v3((float (*)[3])root->rot, r);
879         madd_v3_v3fl(r, euler, mass);
880         madd_v3_v3fl(r, coriolis, mass);
881         madd_v3_v3fl(r, centrifugal, mass);
882 #else
883         copy_v3_v3(r, force);
884         (void)x_root;
885         (void)v_root;
886         (void)mass;
887         (void)root;
888 #endif
889 }
890
891 /* a_world = R*[ a_root + cross(dwdt, x_root) + 2*cross(w, v_root) + cross(w, cross(w, x_root)) ] + a0 */
892 BLI_INLINE void force_root_to_world(float r[3], const float x_root[3], const float v_root[3], const float force[3], float mass, const RootTransform *root)
893 {
894 #ifdef CLOTH_ROOT_FRAME
895         float euler[3], coriolis[3], centrifugal[3], rotvel[3];
896         
897         cross_v3_v3v3(euler, root->domega_dt, x_root);
898         cross_v3_v3v3(coriolis, root->omega, v_root);
899         mul_v3_fl(coriolis, 2.0f);
900         cross_v3_v3v3(rotvel, root->omega, x_root);
901         cross_v3_v3v3(centrifugal, root->omega, rotvel);
902         
903         madd_v3_v3v3fl(r, force, euler, mass);
904         madd_v3_v3fl(r, coriolis, mass);
905         madd_v3_v3fl(r, centrifugal, mass);
906         mul_m3_v3((float (*)[3])root->rot, r);
907         madd_v3_v3fl(r, root->acc, mass);
908 #else
909         copy_v3_v3(r, force);
910         (void)x_root;
911         (void)v_root;
912         (void)mass;
913         (void)root;
914 #endif
915 }
916
917 BLI_INLINE void acc_world_to_root(float r[3], const float x_root[3], const float v_root[3], const float acc[3], const RootTransform *root)
918 {
919         force_world_to_root(r, x_root, v_root, acc, 1.0f, root);
920 }
921
922 BLI_INLINE void acc_root_to_world(float r[3], const float x_root[3], const float v_root[3], const float acc[3], const RootTransform *root)
923 {
924         force_root_to_world(r, x_root, v_root, acc, 1.0f, root);
925 }
926
927 BLI_INLINE void cross_m3_v3m3(float r[3][3], const float v[3], float m[3][3])
928 {
929         cross_v3_v3v3(r[0], v, m[0]);
930         cross_v3_v3v3(r[1], v, m[1]);
931         cross_v3_v3v3(r[2], v, m[2]);
932 }
933
934 BLI_INLINE void cross_v3_identity(float r[3][3], const float v[3])
935 {
936         r[0][0] = 0.0f;         r[1][0] = v[2];         r[2][0] = -v[1];
937         r[0][1] = -v[2];        r[1][1] = 0.0f;         r[2][1] = v[0];
938         r[0][2] = v[1];         r[1][2] = -v[0];        r[2][2] = 0.0f;
939 }
940
941 /* dfdx_root = m*[ -cross(dwdt, I) - cross(w, cross(w, I)) ] + R^T*(dfdx_world) */
942 BLI_INLINE void dfdx_world_to_root(float m[3][3], float dfdx[3][3], float mass, const RootTransform *root)
943 {
944 #ifdef CLOTH_ROOT_FRAME
945         float t[3][3], u[3][3];
946         
947         copy_m3_m3(t, (float (*)[3])root->rot);
948         transpose_m3(t);
949         mul_m3_m3m3(m, t, dfdx);
950         
951         cross_v3_identity(t, root->domega_dt);
952         mul_m3_fl(t, mass);
953         sub_m3_m3m3(m, m, t);
954         
955         cross_v3_identity(u, root->omega);
956         cross_m3_v3m3(t, root->omega, u);
957         mul_m3_fl(t, mass);
958         sub_m3_m3m3(m, m, t);
959 #else
960         copy_m3_m3(m, dfdx);
961         (void)mass;
962         (void)root;
963 #endif
964 }
965
966 /* dfdx_world = R*(dfdx_root + m*[ cross(dwdt, I) + cross(w, cross(w, I)) ]) */
967 BLI_INLINE void dfdx_root_to_world(float m[3][3], float dfdx[3][3], float mass, const RootTransform *root)
968 {
969 #ifdef CLOTH_ROOT_FRAME
970         float t[3][3], u[3][3];
971         
972         cross_v3_identity(t, root->domega_dt);
973         mul_m3_fl(t, mass);
974         add_m3_m3m3(m, dfdx, t);
975         
976         cross_v3_identity(u, root->omega);
977         cross_m3_v3m3(t, root->omega, u);
978         mul_m3_fl(t, mass);
979         add_m3_m3m3(m, m, t);
980         
981         mul_m3_m3m3(m, (float (*)[3])root->rot, m);
982 #else
983         copy_m3_m3(m, dfdx);
984         (void)mass;
985         (void)root;
986 #endif
987 }
988
989 /* dfdv_root = -2*m*cross(w, I) + R^T*(dfdv_world) */
990 BLI_INLINE void dfdv_world_to_root(float m[3][3], float dfdv[3][3], float mass, const RootTransform *root)
991 {
992 #ifdef CLOTH_ROOT_FRAME
993         float t[3][3];
994         
995         copy_m3_m3(t, (float (*)[3])root->rot);
996         transpose_m3(t);
997         mul_m3_m3m3(m, t, dfdv);
998         
999         cross_v3_identity(t, root->omega);
1000         mul_m3_fl(t, 2.0f*mass);
1001         sub_m3_m3m3(m, m, t);
1002 #else
1003         copy_m3_m3(m, dfdv);
1004         (void)mass;
1005         (void)root;
1006 #endif
1007 }
1008
1009 /* dfdv_world = R*(dfdv_root + 2*m*cross(w, I)) */
1010 BLI_INLINE void dfdv_root_to_world(float m[3][3], float dfdv[3][3], float mass, const RootTransform *root)
1011 {
1012 #ifdef CLOTH_ROOT_FRAME
1013         float t[3][3];
1014         
1015         cross_v3_identity(t, root->omega);
1016         mul_m3_fl(t, 2.0f*mass);
1017         add_m3_m3m3(m, dfdv, t);
1018         
1019         mul_m3_m3m3(m, (float (*)[3])root->rot, m);
1020 #else
1021         copy_m3_m3(m, dfdv);
1022         (void)mass;
1023         (void)root;
1024 #endif
1025 }
1026
1027 /* ================================ */
1028
1029 DO_INLINE float fb(float length, float L)
1030 {
1031         float x = length / L;
1032         float xx = x * x;
1033         float xxx = xx * x;
1034         float xxxx = xxx * x;
1035         return (-11.541f * xxxx + 34.193f * xxx - 39.083f * xx + 23.116f * x - 9.713f);
1036 }
1037
1038 DO_INLINE float fbderiv(float length, float L)
1039 {
1040         float x = length/L;
1041         float xx = x * x;
1042         float xxx = xx * x;
1043         return (-46.164f * xxx + 102.579f * xx - 78.166f * x + 23.116f);
1044 }
1045
1046 DO_INLINE float fbstar(float length, float L, float kb, float cb)
1047 {
1048         float tempfb_fl = kb * fb(length, L);
1049         float fbstar_fl = cb * (length - L);
1050         
1051         if (tempfb_fl < fbstar_fl)
1052                 return fbstar_fl;
1053         else
1054                 return tempfb_fl;
1055 }
1056
1057 // function to calculae bending spring force (taken from Choi & Co)
1058 DO_INLINE float fbstar_jacobi(float length, float L, float kb, float cb)
1059 {
1060         float tempfb_fl = kb * fb(length, L);
1061         float fbstar_fl = cb * (length - L);
1062
1063         if (tempfb_fl < fbstar_fl) {
1064                 return cb;
1065         }
1066         else {
1067                 return kb * fbderiv(length, L);
1068         }
1069 }
1070
1071 DO_INLINE void filter(lfVector *V, fmatrix3x3 *S)
1072 {
1073         unsigned int i=0;
1074
1075         for (i = 0; i < S[0].vcount; i++) {
1076                 mul_m3_v3(S[i].m, V[S[i].r]);
1077         }
1078 }
1079
1080 #if 0 /* this version of the CG algorithm does not work very well with partial constraints (where S has non-zero elements) */
1081 static int  cg_filtered(lfVector *ldV, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S)
1082 {
1083         // Solves for unknown X in equation AX=B
1084         unsigned int conjgrad_loopcount=0, conjgrad_looplimit=100;
1085         float conjgrad_epsilon=0.0001f /* , conjgrad_lasterror=0 */ /* UNUSED */;
1086         lfVector *q, *d, *tmp, *r; 
1087         float s, starget, a, s_prev;
1088         unsigned int numverts = lA[0].vcount;
1089         q = create_lfvector(numverts);
1090         d = create_lfvector(numverts);
1091         tmp = create_lfvector(numverts);
1092         r = create_lfvector(numverts);
1093
1094         // zero_lfvector(ldV, CLOTHPARTICLES);
1095         filter(ldV, S);
1096
1097         add_lfvector_lfvector(ldV, ldV, z, numverts);
1098
1099         // r = B - Mul(tmp, A, X);    // just use B if X known to be zero
1100         cp_lfvector(r, lB, numverts);
1101         mul_bfmatrix_lfvector(tmp, lA, ldV);
1102         sub_lfvector_lfvector(r, r, tmp, numverts);
1103
1104         filter(r, S);
1105
1106         cp_lfvector(d, r, numverts);
1107
1108         s = dot_lfvector(r, r, numverts);
1109         starget = s * sqrtf(conjgrad_epsilon);
1110
1111         while (s>starget && conjgrad_loopcount < conjgrad_looplimit) {
1112                 // Mul(q, A, d); // q = A*d;
1113                 mul_bfmatrix_lfvector(q, lA, d);
1114
1115                 filter(q, S);
1116
1117                 a = s/dot_lfvector(d, q, numverts);
1118
1119                 // X = X + d*a;
1120                 add_lfvector_lfvectorS(ldV, ldV, d, a, numverts);
1121
1122                 // r = r - q*a;
1123                 sub_lfvector_lfvectorS(r, r, q, a, numverts);
1124
1125                 s_prev = s;
1126                 s = dot_lfvector(r, r, numverts);
1127
1128                 //d = r+d*(s/s_prev);
1129                 add_lfvector_lfvectorS(d, r, d, (s/s_prev), numverts);
1130
1131                 filter(d, S);
1132
1133                 conjgrad_loopcount++;
1134         }
1135         /* conjgrad_lasterror = s; */ /* UNUSED */
1136
1137         del_lfvector(q);
1138         del_lfvector(d);
1139         del_lfvector(tmp);
1140         del_lfvector(r);
1141         // printf("W/O conjgrad_loopcount: %d\n", conjgrad_loopcount);
1142
1143         return conjgrad_loopcount<conjgrad_looplimit;  // true means we reached desired accuracy in given time - ie stable
1144 }
1145 #endif
1146
1147 static int cg_filtered(lfVector *ldV, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S)
1148 {
1149         // Solves for unknown X in equation AX=B
1150         unsigned int conjgrad_loopcount=0, conjgrad_looplimit=100;
1151         float conjgrad_epsilon=0.01f;
1152         
1153         unsigned int numverts = lA[0].vcount;
1154         lfVector *fB = create_lfvector(numverts);
1155         lfVector *AdV = create_lfvector(numverts);
1156         lfVector *r = create_lfvector(numverts);
1157         lfVector *c = create_lfvector(numverts);
1158         lfVector *q = create_lfvector(numverts);
1159         lfVector *s = create_lfvector(numverts);
1160         float delta_new, delta_old, delta_target, alpha;
1161         
1162         cp_lfvector(ldV, z, numverts);
1163         
1164         /* d0 = filter(B)^T * P * filter(B) */
1165         cp_lfvector(fB, lB, numverts);
1166         filter(fB, S);
1167         delta_target = conjgrad_epsilon*conjgrad_epsilon * dot_lfvector(fB, fB, numverts);
1168         
1169         /* r = filter(B - A * dV) */
1170         mul_bfmatrix_lfvector(AdV, lA, ldV);
1171         sub_lfvector_lfvector(r, lB, AdV, numverts);
1172         filter(r, S);
1173         
1174         /* c = filter(P^-1 * r) */
1175         cp_lfvector(c, r, numverts);
1176         filter(c, S);
1177         
1178         /* delta = r^T * c */
1179         delta_new = dot_lfvector(r, c, numverts);
1180         
1181 #ifdef IMPLICIT_PRINT_SOLVER_INPUT_OUTPUT
1182         printf("==== A ====\n");
1183         print_bfmatrix(lA);
1184         printf("==== z ====\n");
1185         print_lvector(z, numverts);
1186         printf("==== B ====\n");
1187         print_lvector(lB, numverts);
1188         printf("==== S ====\n");
1189         print_bfmatrix(S);
1190 #endif
1191         
1192         while (delta_new > delta_target && conjgrad_loopcount < conjgrad_looplimit) {
1193                 mul_bfmatrix_lfvector(q, lA, c);
1194                 filter(q, S);
1195                 
1196                 alpha = delta_new / dot_lfvector(c, q, numverts);
1197                 
1198                 add_lfvector_lfvectorS(ldV, ldV, c, alpha, numverts);
1199                 
1200                 add_lfvector_lfvectorS(r, r, q, -alpha, numverts);
1201                 
1202                 /* s = P^-1 * r */
1203                 cp_lfvector(s, r, numverts);
1204                 delta_old = delta_new;
1205                 delta_new = dot_lfvector(r, s, numverts);
1206                 
1207                 add_lfvector_lfvectorS(c, s, c, delta_new / delta_old, numverts);
1208                 filter(c, S);
1209                 
1210                 conjgrad_loopcount++;
1211         }
1212
1213 #ifdef IMPLICIT_PRINT_SOLVER_INPUT_OUTPUT
1214         printf("==== dV ====\n");
1215         print_lvector(ldV, numverts);
1216         printf("========\n");
1217 #endif
1218         
1219         del_lfvector(fB);
1220         del_lfvector(AdV);
1221         del_lfvector(r);
1222         del_lfvector(c);
1223         del_lfvector(q);
1224         del_lfvector(s);
1225         // printf("W/O conjgrad_loopcount: %d\n", conjgrad_loopcount);
1226
1227         return conjgrad_loopcount < conjgrad_looplimit;  // true means we reached desired accuracy in given time - ie stable
1228 }
1229
1230 #if 0
1231 // block diagonalizer
1232 DO_INLINE void BuildPPinv(fmatrix3x3 *lA, fmatrix3x3 *P, fmatrix3x3 *Pinv)
1233 {
1234         unsigned int i = 0;
1235         
1236         // Take only the diagonal blocks of A
1237 // #pragma omp parallel for private(i) if (lA[0].vcount > CLOTH_OPENMP_LIMIT)
1238         for (i = 0; i<lA[0].vcount; i++) {
1239                 // block diagonalizer
1240                 cp_fmatrix(P[i].m, lA[i].m);
1241                 inverse_fmatrix(Pinv[i].m, P[i].m);
1242                 
1243         }
1244 }
1245 /*
1246 // version 1.3
1247 static int cg_filtered_pre(lfVector *dv, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S, fmatrix3x3 *P, fmatrix3x3 *Pinv)
1248 {
1249         unsigned int numverts = lA[0].vcount, iterations = 0, conjgrad_looplimit=100;
1250         float delta0 = 0, deltaNew = 0, deltaOld = 0, alpha = 0;
1251         float conjgrad_epsilon=0.0001; // 0.2 is dt for steps=5
1252         lfVector *r = create_lfvector(numverts);
1253         lfVector *p = create_lfvector(numverts);
1254         lfVector *s = create_lfvector(numverts);
1255         lfVector *h = create_lfvector(numverts);
1256         
1257         BuildPPinv(lA, P, Pinv);
1258         
1259         filter(dv, S);
1260         add_lfvector_lfvector(dv, dv, z, numverts);
1261         
1262         mul_bfmatrix_lfvector(r, lA, dv);
1263         sub_lfvector_lfvector(r, lB, r, numverts);
1264         filter(r, S);
1265         
1266         mul_prevfmatrix_lfvector(p, Pinv, r);
1267         filter(p, S);
1268         
1269         deltaNew = dot_lfvector(r, p, numverts);
1270         
1271         delta0 = deltaNew * sqrt(conjgrad_epsilon);
1272         
1273         // itstart();
1274         
1275         while ((deltaNew > delta0) && (iterations < conjgrad_looplimit))
1276         {
1277                 iterations++;
1278                 
1279                 mul_bfmatrix_lfvector(s, lA, p);
1280                 filter(s, S);
1281                 
1282                 alpha = deltaNew / dot_lfvector(p, s, numverts);
1283                 
1284                 add_lfvector_lfvectorS(dv, dv, p, alpha, numverts);
1285                 
1286                 add_lfvector_lfvectorS(r, r, s, -alpha, numverts);
1287                 
1288                 mul_prevfmatrix_lfvector(h, Pinv, r);
1289                 filter(h, S);
1290                 
1291                 deltaOld = deltaNew;
1292                 
1293                 deltaNew = dot_lfvector(r, h, numverts);
1294                 
1295                 add_lfvector_lfvectorS(p, h, p, deltaNew / deltaOld, numverts);
1296                 
1297                 filter(p, S);
1298                 
1299         }
1300         
1301         // itend();
1302         // printf("cg_filtered_pre time: %f\n", (float)itval());
1303         
1304         del_lfvector(h);
1305         del_lfvector(s);
1306         del_lfvector(p);
1307         del_lfvector(r);
1308         
1309         printf("iterations: %d\n", iterations);
1310         
1311         return iterations<conjgrad_looplimit;
1312 }
1313 */
1314 // version 1.4
1315 static int cg_filtered_pre(lfVector *dv, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S, fmatrix3x3 *P, fmatrix3x3 *Pinv, fmatrix3x3 *bigI)
1316 {
1317         unsigned int numverts = lA[0].vcount, iterations = 0, conjgrad_looplimit=100;
1318         float delta0 = 0, deltaNew = 0, deltaOld = 0, alpha = 0, tol = 0;
1319         lfVector *r = create_lfvector(numverts);
1320         lfVector *p = create_lfvector(numverts);
1321         lfVector *s = create_lfvector(numverts);
1322         lfVector *h = create_lfvector(numverts);
1323         lfVector *bhat = create_lfvector(numverts);
1324         lfVector *btemp = create_lfvector(numverts);
1325         
1326         BuildPPinv(lA, P, Pinv);
1327         
1328         initdiag_bfmatrix(bigI, I);
1329         sub_bfmatrix_Smatrix(bigI, bigI, S);
1330         
1331         // x = Sx_0+(I-S)z
1332         filter(dv, S);
1333         add_lfvector_lfvector(dv, dv, z, numverts);
1334         
1335         // b_hat = S(b-A(I-S)z)
1336         mul_bfmatrix_lfvector(r, lA, z);
1337         mul_bfmatrix_lfvector(bhat, bigI, r);
1338         sub_lfvector_lfvector(bhat, lB, bhat, numverts);
1339         
1340         // r = S(b-Ax)
1341         mul_bfmatrix_lfvector(r, lA, dv);
1342         sub_lfvector_lfvector(r, lB, r, numverts);
1343         filter(r, S);
1344         
1345         // p = SP^-1r
1346         mul_prevfmatrix_lfvector(p, Pinv, r);
1347         filter(p, S);
1348         
1349         // delta0 = bhat^TP^-1bhat
1350         mul_prevfmatrix_lfvector(btemp, Pinv, bhat);
1351         delta0 = dot_lfvector(bhat, btemp, numverts);
1352         
1353         // deltaNew = r^TP
1354         deltaNew = dot_lfvector(r, p, numverts);
1355         
1356         /*
1357         filter(dv, S);
1358         add_lfvector_lfvector(dv, dv, z, numverts);
1359         
1360         mul_bfmatrix_lfvector(r, lA, dv);
1361         sub_lfvector_lfvector(r, lB, r, numverts);
1362         filter(r, S);
1363         
1364         mul_prevfmatrix_lfvector(p, Pinv, r);
1365         filter(p, S);
1366         
1367         deltaNew = dot_lfvector(r, p, numverts);
1368         
1369         delta0 = deltaNew * sqrt(conjgrad_epsilon);
1370         */
1371         
1372         // itstart();
1373         
1374         tol = (0.01*0.2);
1375         
1376         while ((deltaNew > delta0*tol*tol) && (iterations < conjgrad_looplimit))
1377         {
1378                 iterations++;
1379                 
1380                 mul_bfmatrix_lfvector(s, lA, p);
1381                 filter(s, S);
1382                 
1383                 alpha = deltaNew / dot_lfvector(p, s, numverts);
1384                 
1385                 add_lfvector_lfvectorS(dv, dv, p, alpha, numverts);
1386                 
1387                 add_lfvector_lfvectorS(r, r, s, -alpha, numverts);
1388                 
1389                 mul_prevfmatrix_lfvector(h, Pinv, r);
1390                 filter(h, S);
1391                 
1392                 deltaOld = deltaNew;
1393                 
1394                 deltaNew = dot_lfvector(r, h, numverts);
1395                 
1396                 add_lfvector_lfvectorS(p, h, p, deltaNew / deltaOld, numverts);
1397                 
1398                 filter(p, S);
1399                 
1400         }
1401         
1402         // itend();
1403         // printf("cg_filtered_pre time: %f\n", (float)itval());
1404         
1405         del_lfvector(btemp);
1406         del_lfvector(bhat);
1407         del_lfvector(h);
1408         del_lfvector(s);
1409         del_lfvector(p);
1410         del_lfvector(r);
1411         
1412         // printf("iterations: %d\n", iterations);
1413         
1414         return iterations<conjgrad_looplimit;
1415 }
1416 #endif
1417
1418 DO_INLINE void dfdx_spring_type2(float to[3][3], float dir[3], float length, float L, float k, float cb)
1419 {
1420         // return  outerprod(dir, dir)*fbstar_jacobi(length, L, k, cb);
1421         mul_fvectorT_fvectorS(to, dir, dir, fbstar_jacobi(length, L, k, cb));
1422 }
1423
1424 DO_INLINE void dfdv_damp(float to[3][3], float dir[3], float damping)
1425 {
1426         // derivative of force wrt velocity.  
1427         mul_fvectorT_fvectorS(to, dir, dir, damping);
1428         
1429 }
1430
1431 DO_INLINE void dfdx_spring(float to[3][3],  float dir[3], float length, float L, float k)
1432 {
1433         // dir is unit length direction, rest is spring's restlength, k is spring constant.
1434         //return  ( (I-outerprod(dir, dir))*Min(1.0f, rest/length) - I) * -k;
1435         mul_fvectorT_fvector(to, dir, dir);
1436         sub_fmatrix_fmatrix(to, I, to);
1437
1438         mul_fmatrix_S(to, (L/length)); 
1439         sub_fmatrix_fmatrix(to, to, I);
1440         mul_fmatrix_S(to, -k);
1441 }
1442
1443 DO_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s, lfVector *UNUSED(lF), lfVector *X, lfVector *V, fmatrix3x3 *UNUSED(dFdV), fmatrix3x3 *UNUSED(dFdX), float time)
1444 {
1445         Cloth *cloth = clmd->clothObject;
1446         ClothVertex *verts = cloth->verts;
1447         float extent[3];
1448         float length = 0, dot = 0;
1449         float dir[3] = {0, 0, 0};
1450         float vel[3];
1451         float k = 0.0f;
1452         float L = s->restlen;
1453         float cb; /* = clmd->sim_parms->structural; */ /*UNUSED*/
1454
1455         float nullf[3] = {0, 0, 0};
1456         float stretch_force[3] = {0, 0, 0};
1457         float bending_force[3] = {0, 0, 0};
1458         float damping_force[3] = {0, 0, 0};
1459         float nulldfdx[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
1460         
1461         float scaling = 0.0;
1462
1463         int no_compress = clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_NO_SPRING_COMPRESS;
1464         
1465         copy_v3_v3(s->f, nullf);
1466         cp_fmatrix(s->dfdx, nulldfdx);
1467         cp_fmatrix(s->dfdv, nulldfdx);
1468
1469         // calculate elonglation
1470         sub_v3_v3v3(extent, X[s->kl], X[s->ij]);
1471         sub_v3_v3v3(vel, V[s->kl], V[s->ij]);
1472         dot = dot_v3v3(extent, extent);
1473         length = sqrtf(dot);
1474         
1475         s->flags &= ~CLOTH_SPRING_FLAG_NEEDED;
1476         
1477         if (length > ALMOST_ZERO) {
1478                 /*
1479                 if (length>L)
1480                 {
1481                 if ((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED) &&
1482                     ((((length-L)*100.0f/L) > clmd->sim_parms->maxspringlen))) // cut spring!
1483                 {
1484                 s->flags |= CSPRING_FLAG_DEACTIVATE;
1485                 return;
1486         }
1487         }
1488                 */
1489                 mul_fvector_S(dir, extent, 1.0f/length);
1490         }
1491         else {
1492                 mul_fvector_S(dir, extent, 0.0f);
1493         }
1494         
1495         // calculate force of structural + shear springs
1496         if ((s->type & CLOTH_SPRING_TYPE_STRUCTURAL) || (s->type & CLOTH_SPRING_TYPE_SHEAR) || (s->type & CLOTH_SPRING_TYPE_SEWING) ) {
1497 #ifdef CLOTH_FORCE_SPRING_STRUCTURAL
1498                 if (length > L || no_compress) {
1499                         s->flags |= CLOTH_SPRING_FLAG_NEEDED;
1500                         
1501                         k = clmd->sim_parms->structural;
1502
1503                         scaling = k + s->stiffness * fabsf(clmd->sim_parms->max_struct - k);
1504
1505                         k = scaling / (clmd->sim_parms->avg_spring_len + FLT_EPSILON);
1506
1507                         // TODO: verify, half verified (couldn't see error)
1508                         if (s->type & CLOTH_SPRING_TYPE_SEWING) {
1509                                 // sewing springs usually have a large distance at first so clamp the force so we don't get tunnelling through colission objects
1510                                 float force = k*(length-L);
1511                                 if (force > clmd->sim_parms->max_sewing) {
1512                                         force = clmd->sim_parms->max_sewing;
1513                                 }
1514                                 mul_fvector_S(stretch_force, dir, force);
1515                         }
1516                         else {
1517                                 mul_fvector_S(stretch_force, dir, k * (length - L));
1518                         }
1519
1520                         VECADD(s->f, s->f, stretch_force);
1521
1522                         // Ascher & Boxman, p.21: Damping only during elonglation
1523                         // something wrong with it...
1524                         mul_fvector_S(damping_force, dir, clmd->sim_parms->Cdis * dot_v3v3(vel, dir));
1525                         VECADD(s->f, s->f, damping_force);
1526                         
1527                         /* VERIFIED */
1528                         dfdx_spring(s->dfdx, dir, length, L, k);
1529                         
1530                         /* VERIFIED */
1531                         dfdv_damp(s->dfdv, dir, clmd->sim_parms->Cdis);
1532                         
1533                 }
1534 #endif
1535         }
1536         else if (s->type & CLOTH_SPRING_TYPE_GOAL) {
1537 #ifdef CLOTH_FORCE_SPRING_GOAL
1538                 float tvect[3];
1539                 
1540                 s->flags |= CLOTH_SPRING_FLAG_NEEDED;
1541                 
1542                 // current_position = xold + t * (newposition - xold)
1543                 sub_v3_v3v3(tvect, verts[s->ij].xconst, verts[s->ij].xold);
1544                 mul_fvector_S(tvect, tvect, time);
1545                 VECADD(tvect, tvect, verts[s->ij].xold);
1546
1547                 sub_v3_v3v3(extent, X[s->ij], tvect);
1548                 
1549                 // SEE MSG BELOW (these are UNUSED)
1550                 // dot = dot_v3v3(extent, extent);
1551                 // length = sqrt(dot);
1552                 
1553                 k = clmd->sim_parms->goalspring;
1554                 
1555                 scaling = k + s->stiffness * fabsf(clmd->sim_parms->max_struct - k);
1556                         
1557                 k = verts [s->ij].goal * scaling / (clmd->sim_parms->avg_spring_len + FLT_EPSILON);
1558                 
1559                 VECADDS(s->f, s->f, extent, -k);
1560                 
1561                 mul_fvector_S(damping_force, dir, clmd->sim_parms->goalfrict * 0.01f * dot_v3v3(vel, dir));
1562                 VECADD(s->f, s->f, damping_force);
1563                 
1564                 // HERE IS THE PROBLEM!!!!
1565                 // dfdx_spring(s->dfdx, dir, length, 0.0, k);
1566                 // dfdv_damp(s->dfdv, dir, MIN2(1.0, (clmd->sim_parms->goalfrict/100.0)));
1567 #endif
1568         }
1569         else {  /* calculate force of bending springs */
1570 #ifdef CLOTH_FORCE_SPRING_BEND
1571                 if (length < L) {
1572                         s->flags |= CLOTH_SPRING_FLAG_NEEDED;
1573                         
1574                         k = clmd->sim_parms->bending;
1575                         
1576                         scaling = k + s->stiffness * fabsf(clmd->sim_parms->max_bend - k);
1577                         cb = k = scaling / (20.0f * (clmd->sim_parms->avg_spring_len + FLT_EPSILON));
1578
1579                         mul_fvector_S(bending_force, dir, fbstar(length, L, k, cb));
1580                         VECADD(s->f, s->f, bending_force);
1581
1582                         dfdx_spring_type2(s->dfdx, dir, length, L, k, cb);
1583                 }
1584 #endif
1585         }
1586 }
1587
1588 DO_INLINE void cloth_apply_spring_force(ClothModifierData *UNUSED(clmd), ClothSpring *s, lfVector *lF, lfVector *UNUSED(X), lfVector *UNUSED(V), fmatrix3x3 *dFdV, fmatrix3x3 *dFdX)
1589 {
1590         if (s->flags & CLOTH_SPRING_FLAG_NEEDED) {
1591                 if (!(s->type & CLOTH_SPRING_TYPE_BENDING)) {
1592                         sub_fmatrix_fmatrix(dFdV[s->ij].m, dFdV[s->ij].m, s->dfdv);
1593                         sub_fmatrix_fmatrix(dFdV[s->kl].m, dFdV[s->kl].m, s->dfdv);
1594                         add_fmatrix_fmatrix(dFdV[s->matrix_index].m, dFdV[s->matrix_index].m, s->dfdv);
1595                 }
1596
1597                 VECADD(lF[s->ij], lF[s->ij], s->f);
1598                 
1599                 if (!(s->type & CLOTH_SPRING_TYPE_GOAL))
1600                         sub_v3_v3v3(lF[s->kl], lF[s->kl], s->f);
1601                 
1602                 sub_fmatrix_fmatrix(dFdX[s->kl].m, dFdX[s->kl].m, s->dfdx);
1603                 sub_fmatrix_fmatrix(dFdX[s->ij].m, dFdX[s->ij].m, s->dfdx);
1604                 add_fmatrix_fmatrix(dFdX[s->matrix_index].m, dFdX[s->matrix_index].m, s->dfdx);
1605         }
1606 }
1607
1608
1609 static void CalcFloat( float *v1, float *v2, float *v3, float *n)
1610 {
1611         float n1[3], n2[3];
1612
1613         n1[0] = v1[0]-v2[0];
1614         n2[0] = v2[0]-v3[0];
1615         n1[1] = v1[1]-v2[1];
1616         n2[1] = v2[1]-v3[1];
1617         n1[2] = v1[2]-v2[2];
1618         n2[2] = v2[2]-v3[2];
1619         n[0] = n1[1]*n2[2]-n1[2]*n2[1];
1620         n[1] = n1[2]*n2[0]-n1[0]*n2[2];
1621         n[2] = n1[0]*n2[1]-n1[1]*n2[0];
1622 }
1623
1624 static void CalcFloat4( float *v1, float *v2, float *v3, float *v4, float *n)
1625 {
1626         /* real cross! */
1627         float n1[3], n2[3];
1628
1629         n1[0] = v1[0]-v3[0];
1630         n1[1] = v1[1]-v3[1];
1631         n1[2] = v1[2]-v3[2];
1632
1633         n2[0] = v2[0]-v4[0];
1634         n2[1] = v2[1]-v4[1];
1635         n2[2] = v2[2]-v4[2];
1636
1637         n[0] = n1[1]*n2[2]-n1[2]*n2[1];
1638         n[1] = n1[2]*n2[0]-n1[0]*n2[2];
1639         n[2] = n1[0]*n2[1]-n1[1]*n2[0];
1640 }
1641
1642 static float calculateVertexWindForce(const float wind[3], const float vertexnormal[3])
1643 {
1644         return dot_v3v3(wind, vertexnormal);
1645 }
1646
1647 /* ================ Volumetric Hair Interaction ================
1648  * adapted from
1649  *      Volumetric Methods for Simulation and Rendering of Hair
1650  *      by Lena Petrovic, Mark Henne and John Anderson
1651  *      Pixar Technical Memo #06-08, Pixar Animation Studios
1652  */
1653
1654 /* Note about array indexing:
1655  * Generally the arrays here are one-dimensional.
1656  * The relation between 3D indices and the array offset is
1657  *   offset = x + res_x * y + res_y * z
1658  */
1659
1660 /* TODO: This is an initial implementation and should be made much better in due time.
1661  * What should at least be implemented is a grid size parameter and a smoothing kernel
1662  * for bigger grids.
1663  */
1664
1665 /* 10x10x10 grid gives nice initial results */
1666 static const int hair_grid_res = 10;
1667
1668 static int hair_grid_size(int res)
1669 {
1670         return res * res * res;
1671 }
1672
1673 BLI_INLINE void hair_grid_get_scale(int res, const float gmin[3], const float gmax[3], float scale[3])
1674 {
1675         sub_v3_v3v3(scale, gmax, gmin);
1676         mul_v3_fl(scale, 1.0f / (res-1));
1677 }
1678
1679 typedef struct HairGridVert {
1680         float velocity[3];
1681         float density;
1682 } HairGridVert;
1683
1684 #define HAIR_GRID_INDEX_AXIS(vec, res, gmin, scale, axis) ( min_ii( max_ii( (int)((vec[axis] - gmin[axis]) / scale[axis]), 0), res-2 ) )
1685
1686 BLI_INLINE int hair_grid_offset(const float vec[3], int res, const float gmin[3], const float scale[3])
1687 {
1688         int i, j, k;
1689         i = HAIR_GRID_INDEX_AXIS(vec, res, gmin, scale, 0);
1690         j = HAIR_GRID_INDEX_AXIS(vec, res, gmin, scale, 1);
1691         k = HAIR_GRID_INDEX_AXIS(vec, res, gmin, scale, 2);
1692         return i + (j + k*res)*res;
1693 }
1694
1695 BLI_INLINE int hair_grid_interp_weights(int res, const float gmin[3], const float scale[3], const float vec[3], float uvw[3])
1696 {
1697         int i, j, k, offset;
1698         
1699         i = HAIR_GRID_INDEX_AXIS(vec, res, gmin, scale, 0);
1700         j = HAIR_GRID_INDEX_AXIS(vec, res, gmin, scale, 1);
1701         k = HAIR_GRID_INDEX_AXIS(vec, res, gmin, scale, 2);
1702         offset = i + (j + k*res)*res;
1703         
1704         uvw[0] = (vec[0] - gmin[0]) / scale[0] - (float)i;
1705         uvw[1] = (vec[1] - gmin[1]) / scale[1] - (float)j;
1706         uvw[2] = (vec[2] - gmin[2]) / scale[2] - (float)k;
1707         
1708         return offset;
1709 }
1710
1711 BLI_INLINE void hair_grid_interpolate(const HairGridVert *grid, int res, const float gmin[3], const float scale[3], const float vec[3],
1712                                       float *density, float velocity[3], float density_gradient[3])
1713 {
1714         HairGridVert data[8];
1715         float uvw[3], muvw[3];
1716         int res2 = res * res;
1717         int offset;
1718         
1719         offset = hair_grid_interp_weights(res, gmin, scale, vec, uvw);
1720         muvw[0] = 1.0f - uvw[0];
1721         muvw[1] = 1.0f - uvw[1];
1722         muvw[2] = 1.0f - uvw[2];
1723         
1724         data[0] = grid[offset           ];
1725         data[1] = grid[offset         +1];
1726         data[2] = grid[offset     +res  ];
1727         data[3] = grid[offset     +res+1];
1728         data[4] = grid[offset+res2      ];
1729         data[5] = grid[offset+res2    +1];
1730         data[6] = grid[offset+res2+res  ];
1731         data[7] = grid[offset+res2+res+1];
1732         
1733         if (density) {
1734                 *density = muvw[2]*( muvw[1]*( muvw[0]*data[0].density + uvw[0]*data[1].density )   +
1735                                       uvw[1]*( muvw[0]*data[2].density + uvw[0]*data[3].density ) ) +
1736                             uvw[2]*( muvw[1]*( muvw[0]*data[4].density + uvw[0]*data[5].density )   +
1737                                       uvw[1]*( muvw[0]*data[6].density + uvw[0]*data[7].density ) );
1738         }
1739         if (velocity) {
1740                 int k;
1741                 for (k = 0; k < 3; ++k) {
1742                         velocity[k] = muvw[2]*( muvw[1]*( muvw[0]*data[0].velocity[k] + uvw[0]*data[1].velocity[k] )   +
1743                                                  uvw[1]*( muvw[0]*data[2].velocity[k] + uvw[0]*data[3].velocity[k] ) ) +
1744                                        uvw[2]*( muvw[1]*( muvw[0]*data[4].velocity[k] + uvw[0]*data[5].velocity[k] )   +
1745                                                  uvw[1]*( muvw[0]*data[6].velocity[k] + uvw[0]*data[7].velocity[k] ) );
1746                 }
1747         }
1748         if (density_gradient) {
1749                 density_gradient[0] = muvw[1] * muvw[2] * ( data[0].density - data[1].density ) +
1750                                        uvw[1] * muvw[2] * ( data[2].density - data[3].density ) +
1751                                       muvw[1] *  uvw[2] * ( data[4].density - data[5].density ) +
1752                                        uvw[1] *  uvw[2] * ( data[6].density - data[7].density );
1753                 
1754                 density_gradient[1] = muvw[2] * muvw[0] * ( data[0].density - data[2].density ) +
1755                                        uvw[2] * muvw[0] * ( data[4].density - data[6].density ) +
1756                                       muvw[2] *  uvw[0] * ( data[1].density - data[3].density ) +
1757                                        uvw[2] *  uvw[0] * ( data[5].density - data[7].density );
1758                 
1759                 density_gradient[2] = muvw[2] * muvw[0] * ( data[0].density - data[4].density ) +
1760                                        uvw[2] * muvw[0] * ( data[1].density - data[5].density ) +
1761                                       muvw[2] *  uvw[0] * ( data[2].density - data[6].density ) +
1762                                        uvw[2] *  uvw[0] * ( data[3].density - data[7].density );
1763         }
1764 }
1765
1766 static void hair_velocity_smoothing(const HairGridVert *hairgrid, const float gmin[3], const float scale[3], float smoothfac,
1767                                     lfVector *lF, lfVector *lX, lfVector *lV, unsigned int numverts)
1768 {
1769         int v;
1770         /* calculate forces */
1771         for (v = 0; v < numverts; v++) {
1772                 float density, velocity[3];
1773                 
1774                 hair_grid_interpolate(hairgrid, hair_grid_res, gmin, scale, lX[v], &density, velocity, NULL);
1775                 
1776                 sub_v3_v3(velocity, lV[v]);
1777                 madd_v3_v3fl(lF[v], velocity, smoothfac);
1778         }
1779 }
1780
1781 static void hair_velocity_collision(const HairGridVert *collgrid, const float gmin[3], const float scale[3], float collfac,
1782                                     lfVector *lF, lfVector *lX, lfVector *lV, unsigned int numverts)
1783 {
1784         int v;
1785         /* calculate forces */
1786         for (v = 0; v < numverts; v++) {
1787                 int offset = hair_grid_offset(lX[v], hair_grid_res, gmin, scale);
1788                 
1789                 if (collgrid[offset].density > 0.0f) {
1790                         lF[v][0] += collfac * (collgrid[offset].velocity[0] - lV[v][0]);
1791                         lF[v][1] += collfac * (collgrid[offset].velocity[1] - lV[v][1]);
1792                         lF[v][2] += collfac * (collgrid[offset].velocity[2] - lV[v][2]);
1793                 }
1794         }
1795 }
1796
1797 static void hair_pressure_force(const HairGridVert *hairgrid, const float gmin[3], const float scale[3], float pressurefac, float minpressure,
1798                                 lfVector *lF, lfVector *lX, unsigned int numverts)
1799 {
1800         int v;
1801         
1802         /* calculate forces */
1803         for (v = 0; v < numverts; v++) {
1804                 float density, gradient[3], gradlen;
1805                 
1806                 hair_grid_interpolate(hairgrid, hair_grid_res, gmin, scale, lX[v], &density, NULL, gradient);
1807                 
1808                 gradlen = normalize_v3(gradient) - minpressure;
1809                 if (gradlen < 0.0f)
1810                         continue;
1811                 mul_v3_fl(gradient, gradlen);
1812                 
1813                 madd_v3_v3fl(lF[v], gradient, pressurefac);
1814         }
1815 }
1816
1817 static void hair_volume_get_boundbox(lfVector *lX, unsigned int numverts, float gmin[3], float gmax[3])
1818 {
1819         int i;
1820         
1821         INIT_MINMAX(gmin, gmax);
1822         for (i = 0; i < numverts; i++)
1823                 DO_MINMAX(lX[i], gmin, gmax);
1824 }
1825
1826 BLI_INLINE bool hair_grid_point_valid(const float vec[3], float gmin[3], float gmax[3])
1827 {
1828         return !(vec[0] < gmin[0] || vec[1] < gmin[1] || vec[2] < gmin[2] ||
1829                  vec[0] > gmax[0] || vec[1] > gmax[1] || vec[2] > gmax[2]);
1830 }
1831
1832 BLI_INLINE float dist_tent_v3f3(const float a[3], float x, float y, float z)
1833 {
1834         float w = (1.0f - fabsf(a[0] - x)) * (1.0f - fabsf(a[1] - y)) * (1.0f - fabsf(a[2] - z));
1835         return w;
1836 }
1837
1838 /* returns the grid array offset as well to avoid redundant calculation */
1839 static int hair_grid_weights(int res, const float gmin[3], const float scale[3], const float vec[3], float weights[8])
1840 {
1841         int i, j, k, offset;
1842         float uvw[3];
1843         
1844         i = HAIR_GRID_INDEX_AXIS(vec, res, gmin, scale, 0);
1845         j = HAIR_GRID_INDEX_AXIS(vec, res, gmin, scale, 1);
1846         k = HAIR_GRID_INDEX_AXIS(vec, res, gmin, scale, 2);
1847         offset = i + (j + k*res)*res;
1848         
1849         uvw[0] = (vec[0] - gmin[0]) / scale[0];
1850         uvw[1] = (vec[1] - gmin[1]) / scale[1];
1851         uvw[2] = (vec[2] - gmin[2]) / scale[2];
1852         
1853         weights[0] = dist_tent_v3f3(uvw, (float)i    , (float)j    , (float)k    );
1854         weights[1] = dist_tent_v3f3(uvw, (float)(i+1), (float)j    , (float)k    );
1855         weights[2] = dist_tent_v3f3(uvw, (float)i    , (float)(j+1), (float)k    );
1856         weights[3] = dist_tent_v3f3(uvw, (float)(i+1), (float)(j+1), (float)k    );
1857         weights[4] = dist_tent_v3f3(uvw, (float)i    , (float)j    , (float)(k+1));
1858         weights[5] = dist_tent_v3f3(uvw, (float)(i+1), (float)j    , (float)(k+1));
1859         weights[6] = dist_tent_v3f3(uvw, (float)i    , (float)(j+1), (float)(k+1));
1860         weights[7] = dist_tent_v3f3(uvw, (float)(i+1), (float)(j+1), (float)(k+1));
1861         
1862         return offset;
1863 }
1864
1865 static HairGridVert *hair_volume_create_hair_grid(ClothModifierData *clmd, lfVector *lX, lfVector *lV, unsigned int numverts)
1866 {
1867         int res = hair_grid_res;
1868         int size = hair_grid_size(res);
1869         HairGridVert *hairgrid;
1870         float gmin[3], gmax[3], scale[3];
1871         /* 2.0f is an experimental value that seems to give good results */
1872         float smoothfac = 2.0f * clmd->sim_parms->velocity_smooth;
1873         unsigned int    v = 0;
1874         int                 i = 0;
1875
1876         hair_volume_get_boundbox(lX, numverts, gmin, gmax);
1877         hair_grid_get_scale(res, gmin, gmax, scale);
1878
1879         hairgrid = MEM_mallocN(sizeof(HairGridVert) * size, "hair voxel data");
1880
1881         /* initialize grid */
1882         for (i = 0; i < size; ++i) {
1883                 zero_v3(hairgrid[i].velocity);
1884                 hairgrid[i].density = 0.0f;
1885         }
1886
1887         /* gather velocities & density */
1888         if (smoothfac > 0.0f) {
1889                 for (v = 0; v < numverts; v++) {
1890                         float *V = lV[v];
1891                         float weights[8];
1892                         int di, dj, dk;
1893                         int offset;
1894                         
1895                         if (!hair_grid_point_valid(lX[v], gmin, gmax))
1896                                 continue;
1897                         
1898                         offset = hair_grid_weights(res, gmin, scale, lX[v], weights);
1899                         
1900                         for (di = 0; di < 2; ++di) {
1901                                 for (dj = 0; dj < 2; ++dj) {
1902                                         for (dk = 0; dk < 2; ++dk) {
1903                                                 int voffset = offset + di + (dj + dk*res)*res;
1904                                                 int iw = di + dj*2 + dk*4;
1905                                                 
1906                                                 hairgrid[voffset].density += weights[iw];
1907                                                 madd_v3_v3fl(hairgrid[voffset].velocity, V, weights[iw]);
1908                                         }
1909                                 }
1910                         }
1911                 }
1912         }
1913
1914         /* divide velocity with density */
1915         for (i = 0; i < size; i++) {
1916                 float density = hairgrid[i].density;
1917                 if (density > 0.0f)
1918                         mul_v3_fl(hairgrid[i].velocity, 1.0f/density);
1919         }
1920         
1921         return hairgrid;
1922 }
1923
1924
1925 static HairGridVert *hair_volume_create_collision_grid(ClothModifierData *clmd, lfVector *lX, unsigned int numverts)
1926 {
1927         int res = hair_grid_res;
1928         int size = hair_grid_size(res);
1929         HairGridVert *collgrid;
1930         ListBase *colliders;
1931         ColliderCache *col = NULL;
1932         float gmin[3], gmax[3], scale[3];
1933         /* 2.0f is an experimental value that seems to give good results */
1934         float collfac = 2.0f * clmd->sim_parms->collider_friction;
1935         unsigned int    v = 0;
1936         int                 i = 0;
1937
1938         hair_volume_get_boundbox(lX, numverts, gmin, gmax);
1939         hair_grid_get_scale(res, gmin, gmax, scale);
1940
1941         collgrid = MEM_mallocN(sizeof(HairGridVert) * size, "hair collider voxel data");
1942
1943         /* initialize grid */
1944         for (i = 0; i < size; ++i) {
1945                 zero_v3(collgrid[i].velocity);
1946                 collgrid[i].density = 0.0f;
1947         }
1948
1949         /* gather colliders */
1950         colliders = get_collider_cache(clmd->scene, NULL, NULL);
1951         if (colliders && collfac > 0.0f) {
1952                 for (col = colliders->first; col; col = col->next) {
1953                         MVert *loc0 = col->collmd->x;
1954                         MVert *loc1 = col->collmd->xnew;
1955                         float vel[3];
1956                         float weights[8];
1957                         int di, dj, dk;
1958                         
1959                         for (v=0; v < col->collmd->numverts; v++, loc0++, loc1++) {
1960                                 int offset;
1961                                 
1962                                 if (!hair_grid_point_valid(loc1->co, gmin, gmax))
1963                                         continue;
1964                                 
1965                                 offset = hair_grid_weights(res, gmin, scale, lX[v], weights);
1966                                 
1967                                 sub_v3_v3v3(vel, loc1->co, loc0->co);
1968                                 
1969                                 for (di = 0; di < 2; ++di) {
1970                                         for (dj = 0; dj < 2; ++dj) {
1971                                                 for (dk = 0; dk < 2; ++dk) {
1972                                                         int voffset = offset + di + (dj + dk*res)*res;
1973                                                         int iw = di + dj*2 + dk*4;
1974                                                         
1975                                                         collgrid[voffset].density += weights[iw];
1976                                                         madd_v3_v3fl(collgrid[voffset].velocity, vel, weights[iw]);
1977                                                 }
1978                                         }
1979                                 }
1980                         }
1981                 }
1982         }
1983         free_collider_cache(&colliders);
1984
1985         /* divide velocity with density */
1986         for (i = 0; i < size; i++) {
1987                 float density = collgrid[i].density;
1988                 if (density > 0.0f)
1989                         mul_v3_fl(collgrid[i].velocity, 1.0f/density);
1990         }
1991         
1992         return collgrid;
1993 }
1994
1995 static void hair_volume_forces(ClothModifierData *clmd, lfVector *lF, lfVector *lX, lfVector *lV, unsigned int numverts)
1996 {
1997         HairGridVert *hairgrid, *collgrid;
1998         float gmin[3], gmax[3], scale[3];
1999         /* 2.0f is an experimental value that seems to give good results */
2000         float smoothfac = 2.0f * clmd->sim_parms->velocity_smooth;
2001         float collfac = 2.0f * clmd->sim_parms->collider_friction;
2002         float pressfac = clmd->sim_parms->pressure;
2003         float minpress = clmd->sim_parms->pressure_threshold;
2004         
2005         if (smoothfac <= 0.0f && collfac <= 0.0f && pressfac <= 0.0f)
2006                 return;
2007         
2008         hair_volume_get_boundbox(lX, numverts, gmin, gmax);
2009         hair_grid_get_scale(hair_grid_res, gmin, gmax, scale);
2010         
2011         hairgrid = hair_volume_create_hair_grid(clmd, lX, lV, numverts);
2012         collgrid = hair_volume_create_collision_grid(clmd, lX, numverts);
2013         
2014         hair_velocity_smoothing(hairgrid, gmin, scale, smoothfac, lF, lX, lV, numverts);
2015         hair_velocity_collision(collgrid, gmin, scale, collfac, lF, lX, lV, numverts);
2016         hair_pressure_force(hairgrid, gmin, scale, pressfac, minpress, lF, lX, numverts);
2017         
2018         MEM_freeN(hairgrid);
2019         MEM_freeN(collgrid);
2020 }
2021
2022 bool implicit_hair_volume_get_texture_data(Object *UNUSED(ob), ClothModifierData *clmd, ListBase *UNUSED(effectors), VoxelData *vd)
2023 {
2024         lfVector *lX, *lV;
2025         HairGridVert *hairgrid/*, *collgrid*/;
2026         int numverts;
2027         int totres, i;
2028         int depth;
2029
2030         if (!clmd->clothObject || !clmd->clothObject->implicit)
2031                 return false;
2032
2033         lX = clmd->clothObject->implicit->X;
2034         lV = clmd->clothObject->implicit->V;
2035         numverts = clmd->clothObject->numverts;
2036
2037         hairgrid = hair_volume_create_hair_grid(clmd, lX, lV, numverts);
2038 //      collgrid = hair_volume_create_collision_grid(clmd, lX, numverts);
2039
2040         vd->resol[0] = hair_grid_res;
2041         vd->resol[1] = hair_grid_res;
2042         vd->resol[2] = hair_grid_res;
2043         
2044         totres = hair_grid_size(hair_grid_res);
2045         
2046         if (vd->hair_type == TEX_VD_HAIRVELOCITY) {
2047                 depth = 4;
2048                 vd->data_type = TEX_VD_RGBA_PREMUL;
2049         }
2050         else {
2051                 depth = 1;
2052                 vd->data_type = TEX_VD_INTENSITY;
2053         }
2054         
2055         if (totres > 0) {
2056                 vd->dataset = (float *)MEM_mapallocN(sizeof(float) * depth * (totres), "hair volume texture data");
2057                 
2058                 for (i = 0; i < totres; ++i) {
2059                         switch (vd->hair_type) {
2060                                 case TEX_VD_HAIRDENSITY:
2061                                         vd->dataset[i] = hairgrid[i].density;
2062                                         break;
2063                                 
2064                                 case TEX_VD_HAIRRESTDENSITY:
2065                                         vd->dataset[i] = 0.0f; // TODO
2066                                         break;
2067                                 
2068                                 case TEX_VD_HAIRVELOCITY:
2069                                         vd->dataset[i + 0*totres] = hairgrid[i].velocity[0];
2070                                         vd->dataset[i + 1*totres] = hairgrid[i].velocity[1];
2071                                         vd->dataset[i + 2*totres] = hairgrid[i].velocity[2];
2072                                         vd->dataset[i + 3*totres] = len_v3(hairgrid[i].velocity);
2073                                         break;
2074                                 
2075                                 case TEX_VD_HAIRENERGY:
2076                                         vd->dataset[i] = 0.0f; // TODO
2077                                         break;
2078                         }
2079                 }
2080         }
2081         else {
2082                 vd->dataset = NULL;
2083         }
2084         
2085         MEM_freeN(hairgrid);
2086 //      MEM_freeN(collgrid);
2087         
2088         return true;
2089 }
2090
2091 /* ================================ */
2092
2093 static bool collision_response(ClothModifierData *clmd, CollisionModifierData *collmd, CollPair *collpair, float restitution, float r_impulse[3])
2094 {
2095         Cloth *cloth = clmd->clothObject;
2096         int index = collpair->ap1;
2097         bool result = false;
2098         
2099         float v1[3], v2_old[3], v2_new[3], v_rel_old[3], v_rel_new[3];
2100         float epsilon2 = BLI_bvhtree_getepsilon(collmd->bvhtree);
2101
2102         float margin_distance = collpair->distance - epsilon2;
2103         float mag_v_rel;
2104         
2105         zero_v3(r_impulse);
2106         
2107         if (margin_distance > 0.0f)
2108                 return false; /* XXX tested before already? */
2109         
2110         /* only handle static collisions here */
2111         if ( collpair->flag & COLLISION_IN_FUTURE )
2112                 return false;
2113         
2114         /* velocity */
2115         copy_v3_v3(v1, cloth->verts[index].v);
2116         collision_get_collider_velocity(v2_old, v2_new, collmd, collpair);
2117         /* relative velocity = velocity of the cloth point relative to the collider */
2118         sub_v3_v3v3(v_rel_old, v1, v2_old);
2119         sub_v3_v3v3(v_rel_new, v1, v2_new);
2120         /* normal component of the relative velocity */
2121         mag_v_rel = dot_v3v3(v_rel_old, collpair->normal);
2122         
2123         /* only valid when moving toward the collider */
2124         if (mag_v_rel < -ALMOST_ZERO) {
2125                 float v_nor_old, v_nor_new;
2126                 float v_tan_old[3], v_tan_new[3];
2127                 float bounce, repulse;
2128                 
2129                 /* Collision response based on
2130                  * "Simulating Complex Hair with Robust Collision Handling" (Choe, Choi, Ko, ACM SIGGRAPH 2005)
2131                  * http://graphics.snu.ac.kr/publications/2005-choe-HairSim/Choe_2005_SCA.pdf
2132                  */
2133                 
2134                 v_nor_old = mag_v_rel;
2135                 v_nor_new = dot_v3v3(v_rel_new, collpair->normal);
2136                 
2137                 madd_v3_v3v3fl(v_tan_old, v_rel_old, collpair->normal, -v_nor_old);
2138                 madd_v3_v3v3fl(v_tan_new, v_rel_new, collpair->normal, -v_nor_new);
2139                 
2140                 /* TODO repulsion forces can easily destabilize the system,
2141                  * have to clamp them or construct a linear spring instead
2142                  */
2143 //              repulse = -margin_distance / dt + dot_v3v3(v1, collpair->normal);
2144                 repulse = 0.0f;
2145                 
2146                 if (margin_distance < -epsilon2) {
2147                         bounce = -(v_nor_new + v_nor_old * restitution);
2148                         mul_v3_v3fl(r_impulse, collpair->normal, max_ff(repulse, bounce));
2149                 }
2150                 else {
2151                         bounce = 0.0f;
2152                         mul_v3_v3fl(r_impulse, collpair->normal, repulse);
2153                 }
2154                 
2155                 result = true;
2156         }
2157         
2158         return result;
2159 }
2160
2161 /* Init constraint matrix
2162  * This is part of the modified CG method suggested by Baraff/Witkin in
2163  * "Large Steps in Cloth Simulation" (Siggraph 1998)
2164  */
2165 static void setup_constraint_matrix(ClothModifierData *clmd, ColliderContacts *contacts, int totcolliders, lfVector *X, lfVector *V, fmatrix3x3 *S, lfVector *z, float dt)
2166 {
2167         Cloth *cloth = clmd->clothObject;
2168         ClothVertex *verts = cloth->verts;
2169         int numverts = cloth->numverts;
2170         RootTransform *roots = cloth->implicit->root;
2171         int i, j, v;
2172
2173         for (v = 0; v < numverts; v++) {
2174                 S[v].c = S[v].r = v;
2175                 if (verts[v].flags & CLOTH_VERT_FLAG_PINNED) {
2176                         /* pinned vertex constraints */
2177                         zero_v3(z[v]); /* velocity is defined externally */
2178                         zero_m3(S[v].m);
2179                 }
2180                 else {
2181                         /* free vertex */
2182                         zero_v3(z[v]);
2183                         unit_m3(S[v].m);
2184                 }
2185                 
2186                 verts[v].impulse_count = 0;
2187         }
2188
2189         for (i = 0; i < totcolliders; ++i) {
2190                 ColliderContacts *ct = &contacts[i];
2191                 for (j = 0; j < ct->totcollisions; ++j) {
2192                         CollPair *collpair = &ct->collisions[j];
2193 //                      float restitution = (1.0f - clmd->coll_parms->damping) * (1.0f - ct->ob->pd->pdef_sbdamp);
2194                         float restitution = 0.0f;
2195                         int v = collpair->face1;
2196                         float cnor[3], cmat[3][3];
2197                         float impulse[3];
2198                         
2199                         /* pinned verts handled separately */
2200                         if (verts[v].flags & CLOTH_VERT_FLAG_PINNED)
2201                                 continue;
2202                         
2203                         /* calculate collision response */
2204                         if (!collision_response(clmd, ct->collmd, collpair, restitution, impulse))
2205                                 continue;
2206                         
2207                         vel_world_to_root(impulse, X[v], impulse, &roots[v]);
2208                         add_v3_v3(z[v], impulse);
2209                         ++verts[v].impulse_count;
2210                         if (verts[v].impulse_count > 1)
2211                                 continue;
2212                         
2213                         /* modify S to enforce velocity constraint in normal direction */
2214                         copy_v3_v3(cnor, collpair->normal);
2215                         mul_transposed_m3_v3(roots[v].rot, cnor);
2216                         mul_fvectorT_fvector(cmat, cnor, cnor);
2217                         
2218                         sub_m3_m3m3(S[v].m, I, cmat);
2219                         
2220                         BKE_sim_debug_data_add_dot(clmd->debug_data, collpair->pa, 0, 1, 0, "collision", hash_collpair(936, collpair));
2221                         BKE_sim_debug_data_add_dot(clmd->debug_data, collpair->pb, 1, 0, 0, "collision", hash_collpair(937, collpair));
2222                         BKE_sim_debug_data_add_line(clmd->debug_data, collpair->pa, collpair->pb, 0.7, 0.7, 0.7, "collision", hash_collpair(938, collpair));
2223                         
2224                         { /* DEBUG */
2225 //                              float nor[3];
2226 //                              mul_v3_v3fl(nor, collpair->normal, collpair->distance);
2227 //                              BKE_sim_debug_data_add_vector(clmd->debug_data, collpair->pb, nor, 1, 1, 0, "collision", hash_collpair(939, collpair));
2228                                 BKE_sim_debug_data_add_vector(clmd->debug_data, collpair->pb, impulse, 1, 1, 0, "collision", hash_collpair(940, collpair));
2229 //                              BKE_sim_debug_data_add_vector(clmd->debug_data, collpair->pb, collpair->normal, 1, 1, 0, "collision", hash_collpair(941, collpair));
2230                         }
2231                 }
2232         }
2233 }
2234
2235 static void cloth_calc_force(ClothModifierData *clmd, float UNUSED(frame), lfVector *lF, lfVector *lX, lfVector *lV, fmatrix3x3 *dFdV, fmatrix3x3 *dFdX, ListBase *effectors, float time, fmatrix3x3 *M)
2236 {
2237         /* Collect forces and derivatives:  F, dFdX, dFdV */
2238         Cloth           *cloth          = clmd->clothObject;
2239         ClothVertex *verts = cloth->verts;
2240         RootTransform *roots = cloth->implicit->root;
2241         unsigned int i  = 0;
2242         float           drag    = clmd->sim_parms->Cvi * 0.01f; /* viscosity of air scaled in percent */
2243         float           gravity[3] = {0.0f, 0.0f, 0.0f};
2244         MFace           *mfaces         = cloth->mfaces;
2245         unsigned int numverts = cloth->numverts;
2246         LinkNode *search;
2247         lfVector *winvec;
2248         EffectedPoint epoint;
2249         
2250         /* initialize forces to zero */
2251         zero_lfvector(lF, numverts);
2252         init_bfmatrix(dFdX, ZERO);
2253         init_bfmatrix(dFdV, ZERO);
2254
2255 #ifdef CLOTH_FORCE_GRAVITY
2256         /* global acceleration (gravitation) */
2257         if (clmd->scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) {
2258                 copy_v3_v3(gravity, clmd->scene->physics_settings.gravity);
2259                 mul_fvector_S(gravity, gravity, 0.001f * clmd->sim_parms->effector_weights->global_gravity); /* scale gravity force */
2260         }
2261         /* multiply lF with mass matrix
2262          * force = mass * acceleration (in this case: gravity)
2263          */
2264         for (i = 0; i < numverts; i++) {
2265                 float g[3];
2266                 acc_world_to_root(g, lX[i], lV[i], gravity, &roots[i]);
2267                 mul_m3_v3(M[i].m, g);
2268                 add_v3_v3(lF[i], g);
2269         }
2270 #else
2271         zero_lfvector(lF, numverts);
2272 #endif
2273
2274         hair_volume_forces(clmd, lF, lX, lV, numverts);
2275
2276 #ifdef CLOTH_FORCE_DRAG
2277         /* set dFdX jacobi matrix diagonal entries to -spring_air */ 
2278         for (i = 0; i < numverts; i++) {
2279                 dFdV[i].m[0][0] -= drag;
2280                 dFdV[i].m[1][1] -= drag;
2281                 dFdV[i].m[2][2] -= drag;
2282         }
2283         submul_lfvectorS(lF, lV, drag, numverts);
2284         for (i = 0; i < numverts; i++) {
2285 #if 1
2286                 float tmp[3][3];
2287                 
2288                 /* NB: uses root space velocity, no need to transform */
2289                 madd_v3_v3fl(lF[i], lV[i], -drag);
2290                 
2291                 copy_m3_m3(tmp, I);
2292                 mul_m3_fl(tmp, -drag);
2293                 add_m3_m3m3(dFdV[i].m, dFdV[i].m, tmp);
2294 #else
2295                 float f[3], tmp[3][3], drag_dfdv[3][3], t[3];
2296                 
2297                 mul_v3_v3fl(f, lV[i], -drag);
2298                 force_world_to_root(t, lX[i], lV[i], f, verts[i].mass, &roots[i]);
2299                 add_v3_v3(lF[i], t);
2300                 
2301                 copy_m3_m3(drag_dfdv, I);
2302                 mul_m3_fl(drag_dfdv, -drag);
2303                 dfdv_world_to_root(tmp, drag_dfdv, verts[i].mass, &roots[i]);
2304                 add_m3_m3m3(dFdV[i].m, dFdV[i].m, tmp);
2305 #endif
2306         }
2307 #endif
2308         
2309         /* handle external forces like wind */
2310         if (effectors) {
2311                 // 0 = force, 1 = normalized force
2312                 winvec = create_lfvector(cloth->numverts);
2313                 
2314                 if (!winvec)
2315                         printf("winvec: out of memory in implicit.c\n");
2316                 
2317                 // precalculate wind forces
2318                 for (i = 0; i < cloth->numverts; i++) {
2319                         pd_point_from_loc(clmd->scene, (float*)lX[i], (float*)lV[i], i, &epoint);
2320                         pdDoEffectors(effectors, NULL, clmd->sim_parms->effector_weights, &epoint, winvec[i], NULL);
2321                 }
2322                 
2323                 for (i = 0; i < cloth->numfaces; i++) {
2324                         float trinormal[3] = {0, 0, 0}; // normalized triangle normal
2325                         float triunnormal[3] = {0, 0, 0}; // not-normalized-triangle normal
2326                         float tmp[3] = {0, 0, 0};
2327                         float factor = (mfaces[i].v4) ? 0.25 : 1.0 / 3.0;
2328                         factor *= 0.02f;
2329                         
2330                         // calculate face normal
2331                         if (mfaces[i].v4)
2332                                 CalcFloat4(lX[mfaces[i].v1], lX[mfaces[i].v2], lX[mfaces[i].v3], lX[mfaces[i].v4], triunnormal);
2333                         else
2334                                 CalcFloat(lX[mfaces[i].v1], lX[mfaces[i].v2], lX[mfaces[i].v3], triunnormal);
2335
2336                         normalize_v3_v3(trinormal, triunnormal);
2337                         
2338                         // add wind from v1
2339                         copy_v3_v3(tmp, trinormal);
2340                         mul_v3_fl(tmp, calculateVertexWindForce(winvec[mfaces[i].v1], triunnormal));
2341                         VECADDS(lF[mfaces[i].v1], lF[mfaces[i].v1], tmp, factor);
2342                         
2343                         // add wind from v2
2344                         copy_v3_v3(tmp, trinormal);
2345                         mul_v3_fl(tmp, calculateVertexWindForce(winvec[mfaces[i].v2], triunnormal));
2346                         VECADDS(lF[mfaces[i].v2], lF[mfaces[i].v2], tmp, factor);
2347                         
2348                         // add wind from v3
2349                         copy_v3_v3(tmp, trinormal);
2350                         mul_v3_fl(tmp, calculateVertexWindForce(winvec[mfaces[i].v3], triunnormal));
2351                         VECADDS(lF[mfaces[i].v3], lF[mfaces[i].v3], tmp, factor);
2352                         
2353                         // add wind from v4
2354                         if (mfaces[i].v4) {
2355                                 copy_v3_v3(tmp, trinormal);
2356                                 mul_v3_fl(tmp, calculateVertexWindForce(winvec[mfaces[i].v4], triunnormal));
2357                                 VECADDS(lF[mfaces[i].v4], lF[mfaces[i].v4], tmp, factor);
2358                         }
2359                 }
2360
2361                 /* Hair has only edges */
2362                 if (cloth->numfaces == 0) {
2363                         ClothSpring *spring;
2364                         float edgevec[3] = {0, 0, 0}; //edge vector
2365                         float edgeunnormal[3] = {0, 0, 0}; // not-normalized-edge normal
2366                         float tmp[3] = {0, 0, 0};
2367                         float factor = 0.01;
2368
2369                         search = cloth->springs;
2370                         while (search) {
2371                                 spring = search->link;
2372                                 
2373                                 if (spring->type == CLOTH_SPRING_TYPE_STRUCTURAL) {
2374                                         sub_v3_v3v3(edgevec, (float*)lX[spring->ij], (float*)lX[spring->kl]);
2375
2376                                         project_v3_v3v3(tmp, winvec[spring->ij], edgevec);
2377                                         sub_v3_v3v3(edgeunnormal, winvec[spring->ij], tmp);
2378                                         /* hair doesn't stretch too much so we can use restlen pretty safely */
2379                                         VECADDS(lF[spring->ij], lF[spring->ij], edgeunnormal, spring->restlen * factor);
2380
2381                                         project_v3_v3v3(tmp, winvec[spring->kl], edgevec);
2382                                         sub_v3_v3v3(edgeunnormal, winvec[spring->kl], tmp);
2383                                         VECADDS(lF[spring->kl], lF[spring->kl], edgeunnormal, spring->restlen * factor);
2384                                 }
2385
2386                                 search = search->next;
2387                         }
2388                 }
2389
2390                 del_lfvector(winvec);
2391         }
2392                 
2393         // calculate spring forces
2394         search = cloth->springs;
2395         while (search) {
2396                 // only handle active springs
2397                 ClothSpring *spring = search->link;
2398                 if (!(spring->flags & CLOTH_SPRING_FLAG_DEACTIVATE))
2399                         cloth_calc_spring_force(clmd, search->link, lF, lX, lV, dFdV, dFdX, time);
2400
2401                 search = search->next;
2402         }
2403         
2404         // apply spring forces
2405         search = cloth->springs;
2406         while (search) {
2407                 // only handle active springs
2408                 ClothSpring *spring = search->link;
2409                 if (!(spring->flags & CLOTH_SPRING_FLAG_DEACTIVATE))
2410                         cloth_apply_spring_force(clmd, search->link, lF, lX, lV, dFdV, dFdX);
2411                 search = search->next;
2412         }
2413         // printf("\n");
2414 }
2415
2416 static bool simulate_implicit_euler(Implicit_Data *id, float dt)
2417 {
2418         unsigned int numverts = id->dFdV[0].vcount;
2419         bool ok;
2420
2421         lfVector *dFdXmV = create_lfvector(numverts);
2422         zero_lfvector(id->dV, numverts);
2423
2424         cp_bfmatrix(id->A, id->M);
2425
2426         subadd_bfmatrixS_bfmatrixS(id->A, id->dFdV, dt, id->dFdX, (dt*dt));
2427
2428         mul_bfmatrix_lfvector(dFdXmV, id->dFdX, id->V);
2429
2430         add_lfvectorS_lfvectorS(id->B, id->F, dt, dFdXmV, (dt*dt), numverts);
2431
2432         // itstart();
2433
2434         ok = cg_filtered(id->dV, id->A, id->B, id->z, id->S); /* conjugate gradient algorithm to solve Ax=b */
2435         // cg_filtered_pre(id->dV, id->A, id->B, id->z, id->S, id->P, id->Pinv, id->bigI);
2436
2437         // itend();
2438         // printf("cg_filtered calc time: %f\n", (float)itval());
2439
2440         // advance velocities
2441         add_lfvector_lfvector(id->Vnew, id->V, id->dV, numverts);
2442
2443         del_lfvector(dFdXmV);
2444         
2445         return ok;
2446 }
2447
2448 /* computes where the cloth would be if it were subject to perfectly stiff edges
2449  * (edge distance constraints) in a lagrangian solver.  then add forces to help
2450  * guide the implicit solver to that state.  this function is called after
2451  * collisions*/
2452 static int UNUSED_FUNCTION(cloth_calc_helper_forces)(Object *UNUSED(ob), ClothModifierData *clmd, float (*initial_cos)[3], float UNUSED(step), float dt)
2453 {
2454         Cloth *cloth= clmd->clothObject;
2455         float (*cos)[3] = MEM_callocN(sizeof(float)*3*cloth->numverts, "cos cloth_calc_helper_forces");
2456         float *masses = MEM_callocN(sizeof(float)*cloth->numverts, "cos cloth_calc_helper_forces");
2457         LinkNode *node;
2458         ClothSpring *spring;
2459         ClothVertex *cv;
2460         int i, steps;
2461         
2462         cv = cloth->verts;
2463         for (i=0; i<cloth->numverts; i++, cv++) {
2464                 copy_v3_v3(cos[i], cv->tx);
2465                 
2466                 if (cv->goal == 1.0f || len_squared_v3v3(initial_cos[i], cv->tx) != 0.0f) {
2467                         masses[i] = 1e+10;
2468                 }
2469                 else {
2470                         masses[i] = cv->mass;
2471                 }
2472         }
2473         
2474         steps = 55;
2475         for (i=0; i<steps; i++) {
2476                 for (node=cloth->springs; node; node=node->next) {
2477                         /* ClothVertex *cv1, *cv2; */ /* UNUSED */
2478                         int v1, v2;
2479                         float len, c, l, vec[3];
2480                         
2481                         spring = node->link;
2482                         if (spring->type != CLOTH_SPRING_TYPE_STRUCTURAL && spring->type != CLOTH_SPRING_TYPE_SHEAR) 
2483                                 continue;
2484                         
2485                         v1 = spring->ij; v2 = spring->kl;
2486                         /* cv1 = cloth->verts + v1; */ /* UNUSED */
2487                         /* cv2 = cloth->verts + v2; */ /* UNUSED */
2488                         len = len_v3v3(cos[v1], cos[v2]);
2489                         
2490                         sub_v3_v3v3(vec, cos[v1], cos[v2]);
2491                         normalize_v3(vec);
2492                         
2493                         c = (len - spring->restlen);
2494                         if (c == 0.0f)
2495                                 continue;
2496                         
2497                         l = c / ((1.0f / masses[v1]) + (1.0f / masses[v2]));
2498                         
2499                         mul_v3_fl(vec, -(1.0f / masses[v1]) * l);
2500                         add_v3_v3(cos[v1], vec);
2501         
2502                         sub_v3_v3v3(vec, cos[v2], cos[v1]);
2503                         normalize_v3(vec);
2504                         
2505                         mul_v3_fl(vec, -(1.0f / masses[v2]) * l);
2506                         add_v3_v3(cos[v2], vec);
2507                 }
2508         }
2509         
2510         cv = cloth->verts;
2511         for (i=0; i<cloth->numverts; i++, cv++) {
2512                 float vec[3];
2513                 
2514                 /*compute forces*/
2515                 sub_v3_v3v3(vec, cos[i], cv->tx);
2516                 mul_v3_fl(vec, cv->mass*dt*20.0f);
2517                 add_v3_v3(cv->tv, vec);
2518                 //copy_v3_v3(cv->tx, cos[i]);
2519         }
2520         
2521         MEM_freeN(cos);
2522         MEM_freeN(masses);
2523         
2524         return 1;
2525 }
2526
2527 int implicit_solver(Object *ob, float frame, ClothModifierData *clmd, ListBase *effectors)
2528 {
2529         unsigned int i=0;
2530         float step=0.0f, tf=clmd->sim_parms->timescale;
2531         Cloth *cloth = clmd->clothObject;
2532         ClothVertex *verts = cloth->verts/*, *cv*/;
2533         unsigned int numverts = cloth->numverts;
2534         float dt = clmd->sim_parms->timescale / clmd->sim_parms->stepsPerFrame;
2535         float spf = (float)clmd->sim_parms->stepsPerFrame / clmd->sim_parms->timescale;
2536         /*float (*initial_cos)[3] = MEM_callocN(sizeof(float)*3*cloth->numverts, "initial_cos implicit.c");*/ /* UNUSED */
2537         Implicit_Data *id = cloth->implicit;
2538         ColliderContacts *contacts = NULL;
2539         int totcolliders = 0;
2540         
2541         BKE_sim_debug_data_clear_category(clmd->debug_data, "collision");
2542         
2543         if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) { /* do goal stuff */
2544                 for (i = 0; i < numverts; i++) {
2545                         // update velocities with constrained velocities from pinned verts
2546                         if (verts [i].flags & CLOTH_VERT_FLAG_PINNED) {
2547                                 float v[3];
2548                                 sub_v3_v3v3(v, verts[i].xconst, verts[i].xold);
2549                                 // mul_v3_fl(id->V[i], clmd->sim_parms->stepsPerFrame);
2550                                 vel_world_to_root(id->V[i], id->X[i], v, &id->root[i]);
2551                         }
2552                 }
2553         }
2554         
2555         if (clmd->debug_data) {
2556                 for (i = 0; i < numverts; i++) {
2557                         BKE_sim_debug_data_add_dot(clmd->debug_data, verts[i].x, 1.0f, 0.1f, 1.0f, "points", hash_vertex(583, i));
2558                 }
2559         }
2560         
2561         while (step < tf) {
2562                 
2563                 /* copy velocities for collision */
2564                 for (i = 0; i < numverts; i++) {
2565                         vel_root_to_world(verts[i].tv, id->X[i], id->V[i], &id->root[i]);
2566                         copy_v3_v3(verts[i].v, verts[i].tv);
2567                 }
2568                 
2569                 /* determine contact points */
2570                 if (clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_ENABLED) {
2571                         if (clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_POINTS) {
2572                                 cloth_find_point_contacts(ob, clmd, 0.0f, tf, &contacts, &totcolliders);
2573                         }
2574                 }
2575                 
2576                 /* setup vertex constraints for pinned vertices and contacts */
2577                 setup_constraint_matrix(clmd, contacts, totcolliders, id->X, id->V, id->S, id->z, dt);
2578                 
2579                 // damping velocity for artistic reasons
2580                 mul_lfvectorS(id->V, id->V, clmd->sim_parms->vel_damping, numverts);
2581                 
2582                 // calculate forces
2583                 cloth_calc_force(clmd, frame, id->F, id->X, id->V, id->dFdV, id->dFdX, effectors, step, id->M);
2584                 
2585                 // calculate new velocity
2586                 simulate_implicit_euler(id, dt);
2587                 
2588                 // advance positions
2589                 add_lfvector_lfvectorS(id->Xnew, id->X, id->Vnew, dt, numverts);
2590                 
2591                 /* move pinned verts to correct position */
2592                 for (i = 0; i < numverts; i++) {
2593                         if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) {
2594                                 if (verts[i].flags & CLOTH_VERT_FLAG_PINNED) {
2595                                         float x[3];
2596                                         interp_v3_v3v3(x, verts[i].xold, verts[i].xconst, step + dt);
2597                                         loc_world_to_root(id->Xnew[i], x, &id->root[i]);
2598                                 }
2599                         }
2600                         
2601                         loc_root_to_world(verts[i].txold, id->X[i], &id->root[i]);
2602                         
2603                         if (!(verts[i].flags & CLOTH_VERT_FLAG_PINNED) && i > 0) {
2604                                 BKE_sim_debug_data_add_line(clmd->debug_data, id->X[i], id->X[i-1], 0.6, 0.3, 0.3, "hair", hash_vertex(4892, i));
2605                                 BKE_sim_debug_data_add_line(clmd->debug_data, id->Xnew[i], id->Xnew[i-1], 1, 0.5, 0.5, "hair", hash_vertex(4893, i));
2606                         }
2607                         BKE_sim_debug_data_add_vector(clmd->debug_data, id->X[i], id->V[i], 0, 0, 1, "velocity", hash_vertex(3158, i));
2608                 }
2609                 
2610                 /* free contact points */
2611                 if (contacts) {
2612                         cloth_free_contacts(contacts, totcolliders);
2613                 }
2614
2615 #if 0
2616                 if (clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_ENABLED) {
2617                         bool do_extra_solve = false;
2618                         
2619                         // collisions 
2620                         // itstart();
2621                         
2622                         // update verts to current positions
2623                         for (i = 0; i < numverts; i++) {
2624                                 copy_v3_v3(verts[i].tx, id->Xnew[i]);
2625                                 
2626                                 sub_v3_v3v3(verts[i].tv, verts[i].tx, verts[i].txold);
2627                                 copy_v3_v3(verts[i].v, verts[i].tv);
2628                         }
2629                         
2630                         /* unused */
2631                         /*for (i=0, cv=cloth->verts; i<cloth->numverts; i++, cv++) {
2632                                 copy_v3_v3(initial_cos[i], cv->tx);
2633                         }*/
2634                         
2635                         if (clmd->clothObject->bvhtree) {
2636                                 // call collision function
2637                                 // TODO: check if "step" or "step+dt" is correct - dg
2638                                 do_extra_solve = cloth_bvh_objcollision(ob, clmd, step/clmd->sim_parms->timescale, dt/clmd->sim_parms->timescale);
2639                         }
2640                         else if (clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_POINTS) {
2641                                 do_extra_solve = cloth_points_objcollision(ob, clmd, step/clmd->sim_parms->timescale, dt/clmd->sim_parms->timescale);
2642                         }
2643                         
2644                         // copy corrected positions back to simulation
2645                         for (i = 0; i < numverts; i++) {
2646                                 // correct velocity again, just to be sure we had to change it due to adaptive collisions
2647                                 sub_v3_v3v3(verts[i].tv, verts[i].tx, id->X[i]);
2648                         }
2649                         
2650                         /* unused */
2651                         /*if (do_extra_solve)
2652                                 cloth_calc_helper_forces(ob, clmd, initial_cos, step/clmd->sim_parms->timescale, dt/clmd->sim_parms->timescale);*/
2653                         
2654                         if (do_extra_solve) {
2655                                 for (i = 0; i < numverts; i++) {                                
2656                                         if ((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) && (verts [i].flags & CLOTH_VERT_FLAG_PINNED))
2657                                                 continue;
2658                                         
2659                                         copy_v3_v3(id->Xnew[i], verts[i].tx);
2660                                         copy_v3_v3(id->Vnew[i], verts[i].tv);
2661                                         mul_v3_fl(id->Vnew[i], spf);
2662                                 }
2663                         }
2664                         
2665                         // X = Xnew;
2666                         cp_lfvector(id->X, id->Xnew, numverts);
2667                         
2668                         // if there were collisions, advance the velocity from v_n+1/2 to v_n+1
2669                         
2670                         if (do_extra_solve) {
2671                                 // V = Vnew;
2672                                 cp_lfvector(id->V, id->Vnew, numverts);
2673                                 
2674                                 // calculate 
2675                                 cloth_calc_force(clmd, frame, id->F, id->X, id->V, id->dFdV, id->dFdX, effectors, step+dt, id->M);
2676                                 
2677                                 simulate_implicit_euler(id, dt / 2.0f);
2678                         }
2679                 }
2680                 else {
2681                         // X = Xnew;
2682                         cp_lfvector(id->X, id->Xnew, numverts);
2683                 }
2684                 
2685                 // itend();
2686                 // printf("collision time: %f\n", (float)itval());
2687 #else
2688                 // X = Xnew;
2689                 cp_lfvector(id->X, id->Xnew, numverts);
2690 #endif
2691                 
2692                 // V = Vnew;
2693                 cp_lfvector(id->V, id->Vnew, numverts);
2694                 
2695                 step += dt;
2696         }
2697         
2698         for (i = 0; i < numverts; i++) {
2699                 if ((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) && (verts [i].flags & CLOTH_VERT_FLAG_PINNED)) {
2700                         copy_v3_v3(verts[i].txold, verts[i].xconst); // TODO: test --> should be .x
2701                         copy_v3_v3(verts[i].x, verts[i].xconst);
2702                         
2703                         vel_root_to_world(verts[i].v, id->X[i], id->V[i], &id->root[i]);
2704                 }
2705                 else {
2706                         loc_root_to_world(verts[i].txold, id->X[i], &id->root[i]);
2707                         copy_v3_v3(verts[i].x, verts[i].txold);
2708                         
2709                         vel_root_to_world(verts[i].v, id->X[i], id->V[i], &id->root[i]);
2710                 }
2711         }
2712         
2713         /* unused */
2714         /*MEM_freeN(initial_cos);*/
2715         
2716         return 1;
2717 }
2718
2719 void implicit_set_positions(ClothModifierData *clmd)
2720 {
2721         Cloth *cloth = clmd->clothObject;
2722         ClothVertex *verts = cloth->verts;
2723         unsigned int numverts = cloth->numverts, i;
2724         ClothHairRoot *cloth_roots = clmd->roots;
2725         Implicit_Data *id = cloth->implicit;
2726         
2727         for (i = 0; i < numverts; i++) {
2728 #ifdef CLOTH_ROOT_FRAME
2729                 copy_v3_v3(id->root[i].loc, cloth_roots[i].loc);
2730                 copy_m3_m3(id->root[i].rot, cloth_roots[i].rot);
2731 #else
2732                 zero_v3(id->root[i].loc);
2733                 unit_m3(id->root[i].rot);
2734                 (void)cloth_roots;
2735 #endif
2736                 
2737                 loc_world_to_root(id->X[i], verts[i].x, &id->root[i]);
2738                 vel_world_to_root(id->V[i], id->X[i], verts[i].v, &id->root[i]);
2739         }
2740         if (G.debug_value > 0)
2741                 printf("implicit_set_positions\n");
2742 }
2743
2744 #endif /* IMPLICIT_SOLVER_BLENDER */