4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) Blender Foundation
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
36 #include "MEM_guardedalloc.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_object_force.h"
41 #include "DNA_cloth_types.h"
42 #include "DNA_key_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_lattice_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_modifier_types.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_arithb.h"
51 #include "BLI_threads.h"
52 #include "BKE_curve.h"
53 #include "BKE_displist.h"
54 #include "BKE_effect.h"
55 #include "BKE_global.h"
57 #include "BKE_object.h"
58 #include "BKE_cloth.h"
59 #include "BKE_modifier.h"
60 #include "BKE_utildefines.h"
61 #include "BKE_global.h"
62 #include "BIF_editdeform.h"
67 static LARGE_INTEGER _itstart, _itend;
68 static LARGE_INTEGER ifreq;
73 QueryPerformanceFrequency(&ifreq);
76 QueryPerformanceCounter(&_itstart);
80 QueryPerformanceCounter(&_itend);
84 return ((double)_itend.QuadPart -
85 (double)_itstart.QuadPart)/((double)ifreq.QuadPart);
89 // intrinsics need better compile flag checking
90 // #include <xmmintrin.h>
91 // #include <pmmintrin.h>
92 // #include <pthread.h>
94 static struct timeval _itstart, _itend;
95 static struct timezone itz;
98 gettimeofday(&_itstart, &itz);
102 gettimeofday(&_itend,&itz);
107 t1 = (double)_itstart.tv_sec + (double)_itstart.tv_usec/(1000*1000);
108 t2 = (double)_itend.tv_sec + (double)_itend.tv_usec/(1000*1000);
115 #defineDO_INLINE inline
117 #defineDO_INLINE static
122 //////////////////////////////////////////
123 /* fast vector / matrix library, enhancements are welcome :) -dg */
124 /////////////////////////////////////////
127 typedef float lfVector[3];
128 typedef struct fmatrix3x3 {
129 float m[3][3]; /* 4x4 matrix */
130 unsigned int c,r; /* column and row number */
131 int pinned; /* is this vertex allowed to move? */
132 float n1,n2,n3; /* three normal vectors for collision constrains */
133 unsigned int vcount; /* vertex count */
134 unsigned int scount; /* spring count */
137 ///////////////////////////
139 ///////////////////////////
140 /* simple vector code */
141 /* STATUS: verified */
142 DO_INLINE void mul_fvector_S(float to[3], float from[3], float scalar)
144 to[0] = from[0] * scalar;
145 to[1] = from[1] * scalar;
146 to[2] = from[2] * scalar;
148 /* simple cross product */
149 /* STATUS: verified */
150 DO_INLINE void cross_fvector(float to[3], float vectorA[3], float vectorB[3])
152 to[0] = vectorA[1] * vectorB[2] - vectorA[2] * vectorB[1];
153 to[1] = vectorA[2] * vectorB[0] - vectorA[0] * vectorB[2];
154 to[2] = vectorA[0] * vectorB[1] - vectorA[1] * vectorB[0];
156 /* simple v^T * v product ("outer product") */
157 /* STATUS: HAS TO BE verified (*should* work) */
158 DO_INLINE void mul_fvectorT_fvector(float to[3][3], float vectorA[3], float vectorB[3])
160 mul_fvector_S(to[0], vectorB, vectorA[0]);
161 mul_fvector_S(to[1], vectorB, vectorA[1]);
162 mul_fvector_S(to[2], vectorB, vectorA[2]);
164 /* simple v^T * v product with scalar ("outer product") */
165 /* STATUS: HAS TO BE verified (*should* work) */
166 DO_INLINE void mul_fvectorT_fvectorS(float to[3][3], float vectorA[3], float vectorB[3], float aS)
168 mul_fvector_S(to[0], vectorB, vectorA[0]* aS);
169 mul_fvector_S(to[1], vectorB, vectorA[1]* aS);
170 mul_fvector_S(to[2], vectorB, vectorA[2]* aS);
173 /* printf vector[3] on console: for debug output */
174 void print_fvector(float m3[3])
176 printf("%f\n%f\n%f\n\n",m3[0],m3[1],m3[2]);
179 ///////////////////////////
180 // long float vector float (*)[3]
181 ///////////////////////////
182 /* print long vector on console: for debug output */
183 DO_INLINE void print_lfvector(float (*fLongVector)[3], unsigned int verts)
186 for(i = 0; i < verts; i++)
188 print_fvector(fLongVector[i]);
191 /* create long vector */
192 DO_INLINE lfVector *create_lfvector(unsigned int verts)
194 // TODO: check if memory allocation was successfull */
195 return (lfVector *)MEM_callocN (verts * sizeof(lfVector), "cloth_implicit_alloc_vector");
196 // return (lfVector *)cloth_aligned_malloc(&MEMORY_BASE, verts * sizeof(lfVector));
198 /* delete long vector */
199 DO_INLINE void del_lfvector(float (*fLongVector)[3])
201 if (fLongVector != NULL)
203 MEM_freeN (fLongVector);
204 // cloth_aligned_free(&MEMORY_BASE, fLongVector);
207 /* copy long vector */
208 DO_INLINE void cp_lfvector(float (*to)[3], float (*from)[3], unsigned int verts)
210 memcpy(to, from, verts * sizeof(lfVector));
212 /* init long vector with float[3] */
213 DO_INLINE void init_lfvector(float (*fLongVector)[3], float vector[3], unsigned int verts)
216 for(i = 0; i < verts; i++)
218 VECCOPY(fLongVector[i], vector);
221 /* zero long vector with float[3] */
222 DO_INLINE void zero_lfvector(float (*to)[3], unsigned int verts)
224 memset(to, 0.0f, verts * sizeof(lfVector));
226 /* multiply long vector with scalar*/
227 DO_INLINE void mul_lfvectorS(float (*to)[3], float (*fLongVector)[3], float scalar, unsigned int verts)
231 for(i = 0; i < verts; i++)
233 mul_fvector_S(to[i], fLongVector[i], scalar);
236 /* multiply long vector with scalar*/
238 DO_INLINE void submul_lfvectorS(float (*to)[3], float (*fLongVector)[3], float scalar, unsigned int verts)
241 for(i = 0; i < verts; i++)
243 VECSUBMUL(to[i], fLongVector[i], scalar);
246 /* dot product for big vector */
247 DO_INLINE float dot_lfvector(float (*fLongVectorA)[3], float (*fLongVectorB)[3], unsigned int verts)
251 // schedule(guided, 2)
252 #pragma omp parallel for reduction(+: temp)
253 for(i = 0; i < verts; i++)
255 temp += INPR(fLongVectorA[i], fLongVectorB[i]);
259 /* A = B + C --> for big vector */
260 DO_INLINE void add_lfvector_lfvector(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], unsigned int verts)
264 for(i = 0; i < verts; i++)
266 VECADD(to[i], fLongVectorA[i], fLongVectorB[i]);
270 /* A = B + C * float --> for big vector */
271 DO_INLINE void add_lfvector_lfvectorS(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], float bS, unsigned int verts)
275 for(i = 0; i < verts; i++)
277 VECADDS(to[i], fLongVectorA[i], fLongVectorB[i], bS);
281 /* A = B * float + C * float --> for big vector */
282 DO_INLINE void add_lfvectorS_lfvectorS(float (*to)[3], float (*fLongVectorA)[3], float aS, float (*fLongVectorB)[3], float bS, unsigned int verts)
286 for(i = 0; i < verts; i++)
288 VECADDSS(to[i], fLongVectorA[i], aS, fLongVectorB[i], bS);
291 /* A = B - C * float --> for big vector */
292 DO_INLINE void sub_lfvector_lfvectorS(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], float bS, unsigned int verts)
295 for(i = 0; i < verts; i++)
297 VECSUBS(to[i], fLongVectorA[i], fLongVectorB[i], bS);
301 /* A = B - C --> for big vector */
302 DO_INLINE void sub_lfvector_lfvector(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], unsigned int verts)
306 for(i = 0; i < verts; i++)
308 VECSUB(to[i], fLongVectorA[i], fLongVectorB[i]);
312 ///////////////////////////
314 ///////////////////////////
315 /* printf 4x4 matrix on console: for debug output */
316 void print_fmatrix(float m3[3][3])
318 printf("%f\t%f\t%f\n",m3[0][0],m3[0][1],m3[0][2]);
319 printf("%f\t%f\t%f\n",m3[1][0],m3[1][1],m3[1][2]);
320 printf("%f\t%f\t%f\n\n",m3[2][0],m3[2][1],m3[2][2]);
322 /* copy 4x4 matrix */
323 DO_INLINE void cp_fmatrix(float to[3][3], float from[3][3])
325 // memcpy(to, from, sizeof (float) * 9);
326 VECCOPY(to[0], from[0]);
327 VECCOPY(to[1], from[1]);
328 VECCOPY(to[2], from[2]);
330 /* calculate determinant of 4x4 matrix */
331 DO_INLINE float det_fmatrix(float m[3][3])
333 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]
334 -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];
336 DO_INLINE void inverse_fmatrix(float to[3][3], float from[3][3])
341 if((d=det_fmatrix(from))==0)
343 printf("can't build inverse");
354 // reverse indexs i&j to take transpose
355 to[j][i] = (from[i1][j1]*from[i2][j2]-from[i1][j2]*from[i2][j1])/d;
358 to[i][j] = 1.0f / from[i][j];
367 /* 4x4 matrix multiplied by a scalar */
368 /* STATUS: verified */
369 DO_INLINE void mul_fmatrix_S(float matrix[3][3], float scalar)
371 mul_fvector_S(matrix[0], matrix[0],scalar);
372 mul_fvector_S(matrix[1], matrix[1],scalar);
373 mul_fvector_S(matrix[2], matrix[2],scalar);
376 /* a vector multiplied by a 4x4 matrix */
377 /* STATUS: verified */
378 DO_INLINE void mul_fvector_fmatrix(float *to, float *from, float matrix[3][3])
380 to[0] = matrix[0][0]*from[0] + matrix[1][0]*from[1] + matrix[2][0]*from[2];
381 to[1] = matrix[0][1]*from[0] + matrix[1][1]*from[1] + matrix[2][1]*from[2];
382 to[2] = matrix[0][2]*from[0] + matrix[1][2]*from[1] + matrix[2][2]*from[2];
385 /* 4x4 matrix multiplied by a vector */
386 /* STATUS: verified */
387 DO_INLINE void mul_fmatrix_fvector(float *to, float matrix[3][3], float *from)
389 to[0] = INPR(matrix[0],from);
390 to[1] = INPR(matrix[1],from);
391 to[2] = INPR(matrix[2],from);
393 /* 4x4 matrix multiplied by a 4x4 matrix */
394 /* STATUS: verified */
395 DO_INLINE void mul_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3])
397 mul_fvector_fmatrix(to[0], matrixA[0],matrixB);
398 mul_fvector_fmatrix(to[1], matrixA[1],matrixB);
399 mul_fvector_fmatrix(to[2], matrixA[2],matrixB);
401 /* 4x4 matrix addition with 4x4 matrix */
402 DO_INLINE void add_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3])
404 VECADD(to[0], matrixA[0], matrixB[0]);
405 VECADD(to[1], matrixA[1], matrixB[1]);
406 VECADD(to[2], matrixA[2], matrixB[2]);
408 /* 4x4 matrix add-addition with 4x4 matrix */
409 DO_INLINE void addadd_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3])
411 VECADDADD(to[0], matrixA[0], matrixB[0]);
412 VECADDADD(to[1], matrixA[1], matrixB[1]);
413 VECADDADD(to[2], matrixA[2], matrixB[2]);
415 /* 4x4 matrix sub-addition with 4x4 matrix */
416 DO_INLINE void addsub_fmatrixS_fmatrixS(float to[3][3], float matrixA[3][3], float aS, float matrixB[3][3], float bS)
418 VECADDSUBSS(to[0], matrixA[0], aS, matrixB[0], bS);
419 VECADDSUBSS(to[1], matrixA[1], aS, matrixB[1], bS);
420 VECADDSUBSS(to[2], matrixA[2], aS, matrixB[2], bS);
422 /* A -= B + C (4x4 matrix sub-addition with 4x4 matrix) */
423 DO_INLINE void subadd_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3])
425 VECSUBADD(to[0], matrixA[0], matrixB[0]);
426 VECSUBADD(to[1], matrixA[1], matrixB[1]);
427 VECSUBADD(to[2], matrixA[2], matrixB[2]);
429 /* A -= B*x + C*y (4x4 matrix sub-addition with 4x4 matrix) */
430 DO_INLINE void subadd_fmatrixS_fmatrixS(float to[3][3], float matrixA[3][3], float aS, float matrixB[3][3], float bS)
432 VECSUBADDSS(to[0], matrixA[0], aS, matrixB[0], bS);
433 VECSUBADDSS(to[1], matrixA[1], aS, matrixB[1], bS);
434 VECSUBADDSS(to[2], matrixA[2], aS, matrixB[2], bS);
436 /* A = B - C (4x4 matrix subtraction with 4x4 matrix) */
437 DO_INLINE void sub_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3])
439 VECSUB(to[0], matrixA[0], matrixB[0]);
440 VECSUB(to[1], matrixA[1], matrixB[1]);
441 VECSUB(to[2], matrixA[2], matrixB[2]);
443 /* A += B - C (4x4 matrix add-subtraction with 4x4 matrix) */
444 DO_INLINE void addsub_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3])
446 VECADDSUB(to[0], matrixA[0], matrixB[0]);
447 VECADDSUB(to[1], matrixA[1], matrixB[1]);
448 VECADDSUB(to[2], matrixA[2], matrixB[2]);
450 /////////////////////////////////////////////////////////////////
452 /////////////////////////////////////////////////////////////////
453 /* a vector multiplied and added to/by a 4x4 matrix */
454 DO_INLINE void muladd_fvector_fmatrix(float to[3], float from[3], float matrix[3][3])
456 to[0] += matrix[0][0]*from[0] + matrix[1][0]*from[1] + matrix[2][0]*from[2];
457 to[1] += matrix[0][1]*from[0] + matrix[1][1]*from[1] + matrix[2][1]*from[2];
458 to[2] += matrix[0][2]*from[0] + matrix[1][2]*from[1] + matrix[2][2]*from[2];
460 /* 4x4 matrix multiplied and added to/by a 4x4 matrix and added to another 4x4 matrix */
461 DO_INLINE void muladd_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3])
463 muladd_fvector_fmatrix(to[0], matrixA[0],matrixB);
464 muladd_fvector_fmatrix(to[1], matrixA[1],matrixB);
465 muladd_fvector_fmatrix(to[2], matrixA[2],matrixB);
467 /* a vector multiplied and sub'd to/by a 4x4 matrix */
468 DO_INLINE void mulsub_fvector_fmatrix(float to[3], float from[3], float matrix[3][3])
470 to[0] -= matrix[0][0]*from[0] + matrix[1][0]*from[1] + matrix[2][0]*from[2];
471 to[1] -= matrix[0][1]*from[0] + matrix[1][1]*from[1] + matrix[2][1]*from[2];
472 to[2] -= matrix[0][2]*from[0] + matrix[1][2]*from[1] + matrix[2][2]*from[2];
474 /* 4x4 matrix multiplied and sub'd to/by a 4x4 matrix and added to another 4x4 matrix */
475 DO_INLINE void mulsub_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3])
477 mulsub_fvector_fmatrix(to[0], matrixA[0],matrixB);
478 mulsub_fvector_fmatrix(to[1], matrixA[1],matrixB);
479 mulsub_fvector_fmatrix(to[2], matrixA[2],matrixB);
481 /* 4x4 matrix multiplied+added by a vector */
482 /* STATUS: verified */
483 DO_INLINE void muladd_fmatrix_fvector(float to[3], float matrix[3][3], float from[3])
485 to[0] += INPR(matrix[0],from);
486 to[1] += INPR(matrix[1],from);
487 to[2] += INPR(matrix[2],from);
489 /* 4x4 matrix multiplied+sub'ed by a vector */
490 DO_INLINE void mulsub_fmatrix_fvector(float to[3], float matrix[3][3], float from[3])
492 to[0] -= INPR(matrix[0],from);
493 to[1] -= INPR(matrix[1],from);
494 to[2] -= INPR(matrix[2],from);
496 /////////////////////////////////////////////////////////////////
498 ///////////////////////////
499 // SPARSE SYMMETRIC big matrix with 4x4 matrix entries
500 ///////////////////////////
501 /* printf a big matrix on console: for debug output */
502 void print_bfmatrix(fmatrix3x3 *m3)
506 for(i = 0; i < m3[0].vcount + m3[0].scount; i++)
508 print_fmatrix(m3[i].m);
511 /* create big matrix */
512 DO_INLINE fmatrix3x3 *create_bfmatrix(unsigned int verts, unsigned int springs)
514 // TODO: check if memory allocation was successfull */
515 fmatrix3x3 *temp = (fmatrix3x3 *)MEM_callocN (sizeof (fmatrix3x3) * (verts + springs), "cloth_implicit_alloc_matrix");
516 temp[0].vcount = verts;
517 temp[0].scount = springs;
520 /* delete big matrix */
521 DO_INLINE void del_bfmatrix(fmatrix3x3 *matrix)
528 /* copy big matrix */
529 DO_INLINE void cp_bfmatrix(fmatrix3x3 *to, fmatrix3x3 *from)
531 // TODO bounds checking
532 memcpy(to, from, sizeof(fmatrix3x3) * (from[0].vcount+from[0].scount) );
534 /* init the diagonal of big matrix */
536 DO_INLINE void initdiag_bfmatrix(fmatrix3x3 *matrix, float m3[3][3])
539 float tmatrix[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
541 for(i = 0; i < matrix[0].vcount; i++)
543 cp_fmatrix(matrix[i].m, m3);
545 for(j = matrix[0].vcount; j < matrix[0].vcount+matrix[0].scount; j++)
547 cp_fmatrix(matrix[j].m, tmatrix);
550 /* init big matrix */
551 DO_INLINE void init_bfmatrix(fmatrix3x3 *matrix, float m3[3][3])
555 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++)
557 cp_fmatrix(matrix[i].m, m3);
560 /* multiply big matrix with scalar*/
561 DO_INLINE void mul_bfmatrix_S(fmatrix3x3 *matrix, float scalar)
564 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++)
566 mul_fmatrix_S(matrix[i].m, scalar);
570 /* SPARSE SYMMETRIC multiply big matrix with long vector*/
571 /* STATUS: verified */
572 DO_INLINE void mul_bfmatrix_lfvector( float (*to)[3], fmatrix3x3 *from, lfVector *fLongVector)
575 lfVector *temp = create_lfvector(from[0].vcount);
577 zero_lfvector(to, from[0].vcount);
579 #pragma omp parallel sections private(i)
583 for(i = from[0].vcount; i < from[0].vcount+from[0].scount; i++)
585 muladd_fmatrix_fvector(to[from[i].c], from[i].m, fLongVector[from[i].r]);
590 for(i = 0; i < from[0].vcount+from[0].scount; i++)
592 muladd_fmatrix_fvector(temp[from[i].r], from[i].m, fLongVector[from[i].c]);
596 add_lfvector_lfvector(to, to, temp, from[0].vcount);
603 /* SPARSE SYMMETRIC multiply big matrix with long vector (for diagonal preconditioner) */
604 /* STATUS: verified */
605 DO_INLINE void mul_prevfmatrix_lfvector( float (*to)[3], fmatrix3x3 *from, lfVector *fLongVector)
609 for(i = 0; i < from[0].vcount; i++)
611 mul_fmatrix_fvector(to[from[i].r], from[i].m, fLongVector[from[i].c]);
615 /* SPARSE SYMMETRIC add big matrix with big matrix: A = B + C*/
616 DO_INLINE void add_bfmatrix_bfmatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix)
620 /* process diagonal elements */
621 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++)
623 add_fmatrix_fmatrix(to[i].m, from[i].m, matrix[i].m);
627 /* SPARSE SYMMETRIC add big matrix with big matrix: A += B + C */
628 DO_INLINE void addadd_bfmatrix_bfmatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix)
632 /* process diagonal elements */
633 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++)
635 addadd_fmatrix_fmatrix(to[i].m, from[i].m, matrix[i].m);
639 /* SPARSE SYMMETRIC subadd big matrix with big matrix: A -= B + C */
640 DO_INLINE void subadd_bfmatrix_bfmatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix)
644 /* process diagonal elements */
645 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++)
647 subadd_fmatrix_fmatrix(to[i].m, from[i].m, matrix[i].m);
651 /* A = B - C (SPARSE SYMMETRIC sub big matrix with big matrix) */
652 DO_INLINE void sub_bfmatrix_bfmatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix)
656 /* process diagonal elements */
657 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++)
659 sub_fmatrix_fmatrix(to[i].m, from[i].m, matrix[i].m);
663 /* SPARSE SYMMETRIC sub big matrix with big matrix S (special constraint matrix with limited entries) */
664 DO_INLINE void sub_bfmatrix_Smatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix)
668 /* process diagonal elements */
669 for(i = 0; i < matrix[0].vcount; i++)
671 sub_fmatrix_fmatrix(to[matrix[i].c].m, from[matrix[i].c].m, matrix[i].m);
675 /* A += B - C (SPARSE SYMMETRIC addsub big matrix with big matrix) */
676 DO_INLINE void addsub_bfmatrix_bfmatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix)
680 /* process diagonal elements */
681 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++)
683 addsub_fmatrix_fmatrix(to[i].m, from[i].m, matrix[i].m);
687 /* SPARSE SYMMETRIC sub big matrix with big matrix*/
688 /* A -= B * float + C * float --> for big matrix */
690 DO_INLINE void subadd_bfmatrixS_bfmatrixS( fmatrix3x3 *to, fmatrix3x3 *from, float aS, fmatrix3x3 *matrix, float bS)
694 /* process diagonal elements */
695 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++)
697 subadd_fmatrixS_fmatrixS(to[i].m, from[i].m, aS, matrix[i].m, bS);
702 ///////////////////////////////////////////////////////////////////
704 ///////////////////////////////////////////////////////////////////
705 static float I[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
706 static float ZERO[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};
707 typedef struct Implicit_Data
709 lfVector *X, *V, *Xnew, *Vnew, *olddV, *F, *B, *dV, *z;
710 fmatrix3x3 *A, *dFdV, *dFdX, *S, *P, *Pinv, *bigI;
713 int implicit_init (Object *ob, ClothModifierData *clmd)
716 unsigned int pinned = 0;
718 ClothVertex *verts = NULL;
719 ClothSpring *spring = NULL;
720 Implicit_Data *id = NULL;
721 LinkNode *search = NULL;
724 printf("implicit_init\n");
727 // MEMORY_BASE.first = MEMORY_BASE.last = NULL;
729 cloth = (Cloth *)clmd->clothObject;
730 verts = cloth->verts;
732 // create implicit base
733 id = (Implicit_Data *)MEM_callocN (sizeof(Implicit_Data), "implicit vecmat");
734 cloth->implicit = id;
736 /* process diagonal elements */
737 id->A = create_bfmatrix(cloth->numverts, cloth->numsprings);
738 id->dFdV = create_bfmatrix(cloth->numverts, cloth->numsprings);
739 id->dFdX = create_bfmatrix(cloth->numverts, cloth->numsprings);
740 id->S = create_bfmatrix(cloth->numverts, 0);
741 id->Pinv = create_bfmatrix(cloth->numverts, cloth->numsprings);
742 id->P = create_bfmatrix(cloth->numverts, cloth->numsprings);
743 id->bigI = create_bfmatrix(cloth->numverts, cloth->numsprings); // TODO 0 springs
744 id->X = create_lfvector(cloth->numverts);
745 id->Xnew = create_lfvector(cloth->numverts);
746 id->V = create_lfvector(cloth->numverts);
747 id->Vnew = create_lfvector(cloth->numverts);
748 id->olddV = create_lfvector(cloth->numverts);
749 zero_lfvector(id->olddV, cloth->numverts);
750 id->F = create_lfvector(cloth->numverts);
751 id->B = create_lfvector(cloth->numverts);
752 id->dV = create_lfvector(cloth->numverts);
753 id->z = create_lfvector(cloth->numverts);
755 for(i=0;i<cloth->numverts;i++)
757 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 = i;
759 if(verts [i].goal >= SOFTGOALSNAP)
761 id->S[pinned].pinned = 1;
762 id->S[pinned].c = id->S[pinned].r = i;
767 // S is special and needs specific vcount and scount
768 id->S[0].vcount = pinned; id->S[0].scount = 0;
771 search = cloth->springs;
772 for(i=0;i<cloth->numsprings;i++)
774 spring = search->link;
776 // dFdV_start[i].r = big_I[i].r = big_zero[i].r =
777 id->A[i+cloth->numverts].r = id->dFdV[i+cloth->numverts].r = id->dFdX[i+cloth->numverts].r =
778 id->P[i+cloth->numverts].r = id->Pinv[i+cloth->numverts].r = id->bigI[i+cloth->numverts].r = spring->ij;
780 // dFdV_start[i].c = big_I[i].c = big_zero[i].c =
781 id->A[i+cloth->numverts].c = id->dFdV[i+cloth->numverts].c = id->dFdX[i+cloth->numverts].c =
782 id->P[i+cloth->numverts].c = id->Pinv[i+cloth->numverts].c = id->bigI[i+cloth->numverts].c = spring->kl;
784 spring->matrix_index = i + cloth->numverts;
786 search = search->next;
789 for(i = 0; i < cloth->numverts; i++)
791 VECCOPY(id->X[i], verts[i].x);
796 int implicit_free (ClothModifierData *clmd)
800 cloth = (Cloth *)clmd->clothObject;
804 id = cloth->implicit;
809 del_bfmatrix(id->dFdV);
810 del_bfmatrix(id->dFdX);
813 del_bfmatrix(id->Pinv);
814 del_bfmatrix(id->bigI);
817 del_lfvector(id->Xnew);
819 del_lfvector(id->Vnew);
820 del_lfvector(id->olddV);
823 del_lfvector(id->dV);
833 DO_INLINE float fb(float length, float L)
836 return (-11.541f*pow(x,4)+34.193f*pow(x,3)-39.083f*pow(x,2)+23.116f*x-9.713f);
839 DO_INLINE float fbderiv(float length, float L)
843 return (-46.164f*pow(x,3)+102.579f*pow(x,2)-78.166f*x+23.116f);
846 DO_INLINE float fbstar(float length, float L, float kb, float cb)
848 float tempfb = kb * fb(length, L);
850 float fbstar = cb * (length - L);
858 // function to calculae bending spring force (taken from Choi & Co)
859 DO_INLINE float fbstar_jacobi(float length, float L, float kb, float cb)
861 float tempfb = kb * fb(length, L);
862 float fbstar = cb * (length - L);
870 return kb * fbderiv(length, L);
874 DO_INLINE void filter(lfVector *V, fmatrix3x3 *S)
878 for(i=0;i<S[0].vcount;i++)
880 mul_fvector_fmatrix(V[S[i].r], V[S[i].r], S[i].m);
884 int cg_filtered(lfVector *ldV, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S)
886 // Solves for unknown X in equation AX=B
887 unsigned int conjgrad_loopcount=0, conjgrad_looplimit=100;
888 float conjgrad_epsilon=0.0001f, conjgrad_lasterror=0;
889 lfVector *q, *d, *tmp, *r;
890 float s, starget, a, s_prev;
891 unsigned int numverts = lA[0].vcount;
892 q = create_lfvector(numverts);
893 d = create_lfvector(numverts);
894 tmp = create_lfvector(numverts);
895 r = create_lfvector(numverts);
897 // zero_lfvector(ldV, CLOTHPARTICLES);
900 add_lfvector_lfvector(ldV, ldV, z, numverts);
902 // r = B - Mul(tmp,A,X); // just use B if X known to be zero
903 cp_lfvector(r, lB, numverts);
904 mul_bfmatrix_lfvector(tmp, lA, ldV);
905 sub_lfvector_lfvector(r, r, tmp, numverts);
909 cp_lfvector(d, r, numverts);
911 s = dot_lfvector(r, r, numverts);
912 starget = s * sqrt(conjgrad_epsilon);
914 while((s>starget && conjgrad_loopcount < conjgrad_looplimit))
916 // Mul(q,A,d); // q = A*d;
917 mul_bfmatrix_lfvector(q, lA, d);
921 a = s/dot_lfvector(d, q, numverts);
924 add_lfvector_lfvectorS(ldV, ldV, d, a, numverts);
927 sub_lfvector_lfvectorS(r, r, q, a, numverts);
930 s = dot_lfvector(r, r, numverts);
932 //d = r+d*(s/s_prev);
933 add_lfvector_lfvectorS(d, r, d, (s/s_prev), numverts);
937 conjgrad_loopcount++;
939 conjgrad_lasterror = s;
945 // printf("W/O conjgrad_loopcount: %d\n", conjgrad_loopcount);
947 return conjgrad_loopcount<conjgrad_looplimit; // true means we reached desired accuracy in given time - ie stable
950 // block diagonalizer
951 DO_INLINE void BuildPPinv(fmatrix3x3 *lA, fmatrix3x3 *P, fmatrix3x3 *Pinv)
955 // Take only the diagonal blocks of A
956 // #pragma omp parallel for private(i)
957 for(i = 0; i<lA[0].vcount; i++)
959 // block diagonalizer
960 cp_fmatrix(P[i].m, lA[i].m);
961 inverse_fmatrix(Pinv[i].m, P[i].m);
967 int cg_filtered_pre(lfVector *dv, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S, fmatrix3x3 *P, fmatrix3x3 *Pinv)
969 unsigned int numverts = lA[0].vcount, iterations = 0, conjgrad_looplimit=100;
970 float delta0 = 0, deltaNew = 0, deltaOld = 0, alpha = 0;
971 float conjgrad_epsilon=0.0001; // 0.2 is dt for steps=5
972 lfVector *r = create_lfvector(numverts);
973 lfVector *p = create_lfvector(numverts);
974 lfVector *s = create_lfvector(numverts);
975 lfVector *h = create_lfvector(numverts);
977 BuildPPinv(lA, P, Pinv);
980 add_lfvector_lfvector(dv, dv, z, numverts);
982 mul_bfmatrix_lfvector(r, lA, dv);
983 sub_lfvector_lfvector(r, lB, r, numverts);
986 mul_prevfmatrix_lfvector(p, Pinv, r);
989 deltaNew = dot_lfvector(r, p, numverts);
991 delta0 = deltaNew * sqrt(conjgrad_epsilon);
995 while ((deltaNew > delta0) && (iterations < conjgrad_looplimit))
999 mul_bfmatrix_lfvector(s, lA, p);
1002 alpha = deltaNew / dot_lfvector(p, s, numverts);
1004 add_lfvector_lfvectorS(dv, dv, p, alpha, numverts);
1006 add_lfvector_lfvectorS(r, r, s, -alpha, numverts);
1008 mul_prevfmatrix_lfvector(h, Pinv, r);
1011 deltaOld = deltaNew;
1013 deltaNew = dot_lfvector(r, h, numverts);
1015 add_lfvector_lfvectorS(p, h, p, deltaNew / deltaOld, numverts);
1022 // printf("cg_filtered_pre time: %f\n", (float)itval());
1029 printf("iterations: %d\n", iterations);
1031 return iterations<conjgrad_looplimit;
1034 // outer product is NOT cross product!!!
1035 DO_INLINE void dfdx_spring_type1(float to[3][3], float dir[3],float length,float L,float k)
1037 // dir is unit length direction, rest is spring's restlength, k is spring constant.
1038 // return (outerprod(dir,dir)*k + (I - outerprod(dir,dir))*(k - ((k*L)/length)));
1040 mul_fvectorT_fvector(temp, dir, dir);
1041 sub_fmatrix_fmatrix(to, I, temp);
1042 mul_fmatrix_S(to, k* (1.0f-(L/length)));
1043 mul_fmatrix_S(temp, k);
1044 add_fmatrix_fmatrix(to, temp, to);
1047 DO_INLINE void dfdx_spring_type2(float to[3][3], float dir[3],float length,float L,float k, float cb)
1049 // return outerprod(dir,dir)*fbstar_jacobi(length, L, k, cb);
1050 mul_fvectorT_fvectorS(to, dir, dir, fbstar_jacobi(length, L, k, cb));
1053 DO_INLINE void dfdv_damp(float to[3][3], float dir[3], float damping)
1055 // derivative of force wrt velocity.
1056 // return outerprod(dir,dir) * damping;
1057 mul_fvectorT_fvectorS(to, dir, dir, damping);
1060 DO_INLINE void dfdx_spring(float to[3][3], float dir[3],float length,float L,float k)
1062 // dir is unit length direction, rest is spring's restlength, k is spring constant.
1063 //return ( (I-outerprod(dir,dir))*Min(1.0f,rest/length) - I) * -k;
1064 mul_fvectorT_fvector(to, dir, dir);
1065 sub_fmatrix_fmatrix(to, I, to);
1066 mul_fmatrix_S(to, (((L/length)> 1.0f) ? (1.0f): (L/length)));
1067 sub_fmatrix_fmatrix(to, to, I);
1068 mul_fmatrix_S(to, -k);
1071 DO_INLINE void dfdx_damp(float to[3][3], float dir[3],float length,const float vel[3],float rest,float damping)
1073 // inner spring damping vel is the relative velocity of the endpoints.
1074 // return (I-outerprod(dir,dir)) * (-damping * -(dot(dir,vel)/Max(length,rest)));
1075 mul_fvectorT_fvector(to, dir, dir);
1076 sub_fmatrix_fmatrix(to, I, to);
1077 mul_fmatrix_S(to, (-damping * -(INPR(dir,vel)/MAX2(length,rest))));
1081 DO_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s, lfVector *lF, lfVector *X, lfVector *V, fmatrix3x3 *dFdV, fmatrix3x3 *dFdX)
1085 float dir[3] = {0,0,0};
1088 float L = s->restlen;
1089 float cb = clmd->sim_parms->structural;
1091 float nullf[3] = {0,0,0};
1092 float stretch_force[3] = {0,0,0};
1093 float bending_force[3] = {0,0,0};
1094 float damping_force[3] = {0,0,0};
1095 float nulldfdx[3][3]={ {0,0,0}, {0,0,0}, {0,0,0}};
1097 float scaling = 0.0;
1099 VECCOPY(s->f, nullf);
1100 cp_fmatrix(s->dfdx, nulldfdx);
1101 cp_fmatrix(s->dfdv, nulldfdx);
1103 // calculate elonglation
1104 VECSUB(extent, X[s->kl], X[s->ij]);
1105 VECSUB(vel, V[s->kl], V[s->ij]);
1106 length = sqrt(INPR(extent, extent));
1108 s->flags &= ~CLOTH_SPRING_FLAG_NEEDED;
1110 if(length > ABS(ALMOST_ZERO))
1115 if((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED)
1116 && ((((length-L)*100.0f/L) > clmd->sim_parms->maxspringlen))) // cut spring!
1118 s->flags |= CSPRING_FLAG_DEACTIVATE;
1123 mul_fvector_S(dir, extent, 1.0f/length);
1127 mul_fvector_S(dir, extent, 0.0f);
1130 // calculate force of structural + shear springs
1131 if(s->type != CLOTH_SPRING_TYPE_BENDING)
1133 if(length > L) // only on elonglation
1135 s->flags |= CLOTH_SPRING_FLAG_NEEDED;
1137 k = clmd->sim_parms->structural;
1139 scaling = k + s->stiffness * ABS(clmd->sim_parms->max_struct-k);
1142 // printf("scaling: %f, stiffness: %f\n", k, s->stiffness);
1144 if((s->ij == 109) || (s->kl == 109))
1146 printf("length-L: %f, f: %f, len: %f, L: %f\n", length-L, (k*(length-L)), length, L);
1147 printf("kl X-x: %f, f-y: %f, f-z: %f\n", X[s->kl][0], X[s->kl][1], X[s->kl][2]);
1148 printf("ij X-x: %f, f-y: %f, f-z: %f\n\n", X[s->ij][0], X[s->ij][1], X[s->ij][2]);
1152 mul_fvector_S(stretch_force, dir, (k*(length-L)));
1154 VECADD(s->f, s->f, stretch_force);
1156 // Ascher & Boxman, p.21: Damping only during elonglation
1157 mul_fvector_S(damping_force, extent, clmd->sim_parms->Cdis * ((INPR(vel,extent)/length)));
1158 VECADD(s->f, s->f, damping_force);
1160 dfdx_spring_type1(s->dfdx, dir,length,L,k);
1162 dfdv_damp(s->dfdv, dir,clmd->sim_parms->Cdis);
1165 else // calculate force of bending springs
1169 s->flags |= CLOTH_SPRING_FLAG_NEEDED;
1171 k = clmd->sim_parms->bending;
1173 scaling = k + s->stiffness * ABS(clmd->sim_parms->max_bend-k);
1176 mul_fvector_S(bending_force, dir, fbstar(length, L, k, cb));
1177 VECADD(s->f, s->f, bending_force);
1179 dfdx_spring_type2(s->dfdx, dir,length,L,k, cb);
1183 if((s->ij == 109) || (s->kl == 109))
1185 printf("type: %d, f-x: %f, f-y: %f, f-z: %f\n", s->type, s->f[0], s->f[1], s->f[2]);
1190 DO_INLINE void cloth_apply_spring_force(ClothModifierData *clmd, ClothSpring *s, lfVector *lF, lfVector *X, lfVector *V, fmatrix3x3 *dFdV, fmatrix3x3 *dFdX)
1192 if(s->flags & CLOTH_SPRING_FLAG_NEEDED)
1194 if(s->type != CLOTH_SPRING_TYPE_BENDING)
1196 sub_fmatrix_fmatrix(dFdV[s->ij].m, dFdV[s->ij].m, s->dfdv);
1197 sub_fmatrix_fmatrix(dFdV[s->kl].m, dFdV[s->kl].m, s->dfdv);
1198 add_fmatrix_fmatrix(dFdV[s->matrix_index].m, dFdV[s->matrix_index].m, s->dfdv);
1201 VECADD(lF[s->ij], lF[s->ij], s->f);
1202 VECSUB(lF[s->kl], lF[s->kl], s->f);
1204 sub_fmatrix_fmatrix(dFdX[s->ij].m, dFdX[s->ij].m, s->dfdx);
1205 sub_fmatrix_fmatrix(dFdX[s->kl].m, dFdX[s->kl].m, s->dfdx);
1207 add_fmatrix_fmatrix(dFdX[s->matrix_index].m, dFdX[s->matrix_index].m, s->dfdx);
1211 DO_INLINE void calculateTriangleNormal(float to[3], lfVector *X, MFace mface)
1215 VECSUB(v1, X[mface.v2], X[mface.v1]);
1216 VECSUB(v2, X[mface.v3], X[mface.v1]);
1217 cross_fvector(to, v1, v2);
1220 DO_INLINE void calculatQuadNormal(float to[3], lfVector *X, MFace mface)
1222 float temp = CalcNormFloat4(X[mface.v1],X[mface.v2],X[mface.v3],X[mface.v4],to);
1223 mul_fvector_S(to, to, temp);
1226 void calculateWeightedVertexNormal(ClothModifierData *clmd, MFace *mfaces, float to[3], int index, lfVector *X)
1230 Cloth *cloth = clmd->clothObject;
1232 for(i = 0; i < cloth->numfaces; i++)
1234 // check if this triangle contains the selected vertex
1235 if(mfaces[i].v1 == index || mfaces[i].v2 == index || mfaces[i].v3 == index || mfaces[i].v4 == index)
1237 calculatQuadNormal(temp, X, mfaces[i]);
1238 VECADD(to, to, temp);
1242 float calculateVertexWindForce(float wind[3], float vertexnormal[3])
1244 return fabs(INPR(wind, vertexnormal) * 0.5f);
1247 DO_INLINE void calc_triangle_force(ClothModifierData *clmd, MFace mface, lfVector *F, lfVector *X, lfVector *V, fmatrix3x3 *dFdV, fmatrix3x3 *dFdX, ListBase *effectors)
1252 void cloth_calc_force(ClothModifierData *clmd, lfVector *lF, lfVector *lX, lfVector *lV, fmatrix3x3 *dFdV, fmatrix3x3 *dFdX, ListBase *effectors, float time)
1254 /* Collect forces and derivatives: F,dFdX,dFdV */
1255 Cloth *cloth = clmd->clothObject;
1257 float spring_air = clmd->sim_parms->Cvi * 0.01f; /* viscosity of air scaled in percent */
1259 float tm2[3][3] = {{-spring_air,0,0}, {0,-spring_air,0},{0,0,-spring_air}};
1260 ClothVertex *verts = cloth->verts;
1261 MFace *mfaces = cloth->mfaces;
1262 float wind_normalized[3];
1263 unsigned int numverts = cloth->numverts;
1264 float auxvect[3], velgoal[3], tvect[3];
1266 LinkNode *search = cloth->springs;
1269 VECCOPY(gravity, clmd->sim_parms->gravity);
1270 mul_fvector_S(gravity, gravity, 0.001f); /* scale gravity force */
1272 /* set dFdX jacobi matrix to zero */
1273 init_bfmatrix(dFdX, ZERO);
1274 /* set dFdX jacobi matrix diagonal entries to -spring_air */
1275 initdiag_bfmatrix(dFdV, tm2);
1277 init_lfvector(lF, gravity, numverts);
1279 submul_lfvectorS(lF, lV, spring_air, numverts);
1282 if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL)
1284 for(i = 0; i < numverts; i++)
1286 if(verts [i].goal < SOFTGOALSNAP)
1288 // current_position = xold + t * (newposition - xold)
1289 VECSUB(tvect, verts[i].xconst, verts[i].xold);
1290 mul_fvector_S(tvect, tvect, time);
1291 VECADD(tvect, tvect, verts[i].xold);
1293 VECSUB(auxvect, tvect, lX[i]);
1294 ks = 1.0f/(1.0f- verts [i].goal*clmd->sim_parms->goalspring)-1.0f ;
1295 VECADDS(lF[i], lF[i], auxvect, -ks);
1297 // calulate damping forces generated by goals
1299 VECSUB(velgoal,verts[i].xold, verts[i].xconst);
1300 kd = clmd->sim_parms->goalfrict * 0.01f; // friction force scale taken from SB
1301 VECSUBADDSS(lF[i], velgoal, kd, lV[i], kd);
1308 /* handle external forces like wind */
1311 float speed[3] = {0.0f, 0.0f,0.0f};
1312 float force[3]= {0.0f, 0.0f, 0.0f};
1314 #pragma omp parallel for private (i) shared(lF)
1315 for(i = 0; i < cloth->numverts; i++)
1317 float vertexnormal[3]={0,0,0};
1318 float fieldfactor = 1000.0f; // windfactor = 250.0f; // from sb
1320 pdDoEffectors(effectors, lX[i], force, speed, (float)G.scene->r.cfra, 0.0f, PE_WIND_AS_SPEED);
1322 // TODO apply forcefields here
1323 VECADDS(lF[i], lF[i], force, fieldfactor*0.01f);
1325 VECCOPY(wind_normalized, speed);
1326 Normalize(wind_normalized);
1328 calculateWeightedVertexNormal(clmd, mfaces, vertexnormal, i, lX);
1329 VECADDS(lF[i], lF[i], wind_normalized, -calculateVertexWindForce(speed, vertexnormal));
1333 // calculate spring forces
1334 search = cloth->springs;
1337 // only handle active springs
1338 // if(((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED) && !(springs[i].flags & CSPRING_FLAG_DEACTIVATE))|| !(clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED)){}
1339 cloth_calc_spring_force(clmd, search->link, lF, lX, lV, dFdV, dFdX);
1341 search = search->next;
1344 // apply spring forces
1345 search = cloth->springs;
1348 // only handle active springs
1349 // if(((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED) && !(springs[i].flags & CSPRING_FLAG_DEACTIVATE))|| !(clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED))
1350 cloth_apply_spring_force(clmd, search->link, lF, lX, lV, dFdV, dFdX);
1351 search = search->next;
1356 void simulate_implicit_euler(lfVector *Vnew, lfVector *lX, lfVector *lV, lfVector *lF, fmatrix3x3 *dFdV, fmatrix3x3 *dFdX, float dt, fmatrix3x3 *A, lfVector *B, lfVector *dV, fmatrix3x3 *S, lfVector *z, lfVector *olddV, fmatrix3x3 *P, fmatrix3x3 *Pinv)
1358 unsigned int numverts = dFdV[0].vcount;
1360 lfVector *dFdXmV = create_lfvector(numverts);
1361 initdiag_bfmatrix(A, I);
1362 zero_lfvector(dV, numverts);
1364 subadd_bfmatrixS_bfmatrixS(A, dFdV, dt, dFdX, (dt*dt));
1366 mul_bfmatrix_lfvector(dFdXmV, dFdX, lV);
1368 add_lfvectorS_lfvectorS(B, lF, dt, dFdXmV, (dt*dt), numverts);
1372 cg_filtered(dV, A, B, z, S); /* conjugate gradient algorithm to solve Ax=b */
1373 // cg_filtered_pre(dV, A, B, z, olddV, P, Pinv, dt);
1376 // printf("cg_filtered calc time: %f\n", (float)itval());
1378 cp_lfvector(olddV, dV, numverts);
1380 // advance velocities
1381 add_lfvector_lfvector(Vnew, lV, dV, numverts);
1384 del_lfvector(dFdXmV);
1387 int implicit_solver (Object *ob, float frame, ClothModifierData *clmd, ListBase *effectors)
1390 float step=0.0f, tf=1.0f;
1391 Cloth *cloth = clmd->clothObject;
1392 ClothVertex *verts = cloth->verts;
1393 unsigned int numverts = cloth->numverts;
1394 float dt = 1.0f / clmd->sim_parms->stepsPerFrame;
1395 Implicit_Data *id = cloth->implicit;
1398 if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) /* do goal stuff */
1400 for(i = 0; i < numverts; i++)
1402 // update velocities with constrained velocities from pinned verts
1403 if(verts [i].goal >= SOFTGOALSNAP)
1405 VECSUB(id->V[i], verts[i].xconst, verts[i].xold);
1406 // VecMulf(id->V[i], 1.0 / dt);
1413 effectors= pdInitEffectors(ob,NULL);
1416 cloth_calc_force(clmd, id->F, id->X, id->V, id->dFdV, id->dFdX, effectors, step );
1418 // printf("F -> x: %f, y: %f; z: %f\n\n", id->F[109][0], id->F[109][1], id->F[109][2]);
1420 simulate_implicit_euler(id->Vnew, id->X, id->V, id->F, id->dFdV, id->dFdX, dt, id->A, id->B, id->dV, id->S, id->z, id->olddV, id->P, id->Pinv);
1422 add_lfvector_lfvectorS(id->Xnew, id->X, id->Vnew, dt, numverts);
1425 printf("dt: %f\n", dt);
1426 printf("Xnew -> x: %f, y: %f; z: %f\n", id->Xnew[109][0], id->Xnew[109][1], id->Xnew[109][2]);
1427 printf("X -> x: %f, y: %f; z: %f\n", id->X[109][0], id->X[109][1], id->X[109][2]);
1428 printf("Vnew -> x: %f, y: %f; z: %f\n\n", id->Vnew[109][0], id->Vnew[109][1], id->Vnew[109][2]);
1431 // clmd->coll_parms->flags &= ~CLOTH_COLLSETTINGS_FLAG_ENABLED;
1433 if(clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_ENABLED)
1438 // update verts to current positions
1439 for(i = 0; i < numverts; i++)
1442 if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL)
1444 if(verts [i].goal >= SOFTGOALSNAP)
1446 float tvect[3] = {.0,.0,.0};
1447 // VECSUB(tvect, id->Xnew[i], verts[i].xold);
1448 mul_fvector_S(tvect, id->V[i], step+dt);
1449 VECADD(tvect, tvect, verts[i].xold);
1450 VECCOPY(id->Xnew[i], tvect);
1455 VECCOPY(verts[i].tx, id->Xnew[i]);
1457 VECSUB(verts[i].tv, verts[i].tx, verts[i].txold);
1458 VECCOPY(verts[i].v, verts[i].tv);
1461 // call collision function
1462 result = cloth_bvh_objcollision(clmd, step + dt, dt);
1464 // copy corrected positions back to simulation
1465 for(i = 0; i < numverts; i++)
1470 if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL)
1472 if(verts [i].goal >= SOFTGOALSNAP)
1479 // VECADD(verts[i].tx, verts[i].txold, verts[i].tv);
1481 VECCOPY(verts[i].txold, verts[i].tx);
1483 VECCOPY(id->Xnew[i], verts[i].tx);
1485 VECCOPY(id->Vnew[i], verts[i].tv);
1486 VecMulf(id->Vnew[i], 1.0f / dt);
1490 VECCOPY(verts[i].txold, id->Xnew[i]);
1495 cp_lfvector(id->X, id->Xnew, numverts);
1497 // if there were collisions, advance the velocity from v_n+1/2 to v_n+1
1501 cp_lfvector(id->V, id->Vnew, numverts);
1504 cloth_calc_force(clmd, id->F, id->X, id->V, id->dFdV, id->dFdX, effectors, step);
1505 simulate_implicit_euler(id->Vnew, id->X, id->V, id->F, id->dFdV, id->dFdX, dt / 2.0f, id->A, id->B, id->dV, id->S, id->z, id->olddV, id->P, id->Pinv);
1511 cp_lfvector(id->X, id->Xnew, numverts);
1515 // printf("collision time: %f\n", (float)itval());
1518 cp_lfvector(id->V, id->Vnew, numverts);
1522 if(effectors) pdEndEffectors(effectors);
1525 for(i = 0; i < numverts; i++)
1527 if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL)
1529 if(verts [i].goal < SOFTGOALSNAP)
1531 VECCOPY(verts[i].txold, id->X[i]);
1532 VECCOPY(verts[i].x, id->X[i]);
1533 VECCOPY(verts[i].v, id->V[i]);
1537 VECCOPY(verts[i].txold, verts[i].xconst);
1538 VECCOPY(verts[i].x, verts[i].xconst);
1539 VECCOPY(verts[i].v, id->V[i]);
1544 VECCOPY(verts[i].txold, id->X[i]);
1545 VECCOPY(verts[i].x, id->X[i]);
1546 VECCOPY(verts[i].v, id->V[i]);
1552 void implicit_set_positions (ClothModifierData *clmd)
1554 Cloth *cloth = clmd->clothObject;
1555 ClothVertex *verts = cloth->verts;
1556 unsigned int numverts = cloth->numverts, i;
1557 Implicit_Data *id = cloth->implicit;
1559 for(i = 0; i < numverts; i++)
1561 VECCOPY(id->X[i], verts[i].x);
1562 VECCOPY(id->V[i], verts[i].v);
1565 printf("implicit_set_positions\n");