|
Blender
V2.59
|
00001 /* 00002 * $Id: implicit.c 36423 2011-05-02 03:44:02Z campbellbarton $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) Blender Foundation 00021 * All rights reserved. 00022 * 00023 * The Original Code is: all of this file. 00024 * 00025 * Contributor(s): none yet. 00026 * 00027 * ***** END GPL LICENSE BLOCK ***** 00028 */ 00029 00035 #include "MEM_guardedalloc.h" 00036 00037 #include "DNA_scene_types.h" 00038 #include "DNA_object_types.h" 00039 #include "DNA_object_force.h" 00040 #include "DNA_meshdata_types.h" 00041 00042 #include "BLI_threads.h" 00043 #include "BLI_math.h" 00044 #include "BLI_linklist.h" 00045 #include "BLI_utildefines.h" 00046 00047 #include "BKE_cloth.h" 00048 #include "BKE_collision.h" 00049 #include "BKE_effect.h" 00050 #include "BKE_global.h" 00051 00052 00053 #define CLOTH_OPENMP_LIMIT 512 00054 00055 #ifdef _WIN32 00056 #include <windows.h> 00057 static LARGE_INTEGER _itstart, _itend; 00058 static LARGE_INTEGER ifreq; 00059 static void itstart(void) 00060 { 00061 static int first = 1; 00062 if(first) { 00063 QueryPerformanceFrequency(&ifreq); 00064 first = 0; 00065 } 00066 QueryPerformanceCounter(&_itstart); 00067 } 00068 static void itend(void) 00069 { 00070 QueryPerformanceCounter(&_itend); 00071 } 00072 double itval(void) 00073 { 00074 return ((double)_itend.QuadPart - 00075 (double)_itstart.QuadPart)/((double)ifreq.QuadPart); 00076 } 00077 #else 00078 #include <sys/time.h> 00079 // intrinsics need better compile flag checking 00080 // #include <xmmintrin.h> 00081 // #include <pmmintrin.h> 00082 // #include <pthread.h> 00083 00084 static struct timeval _itstart, _itend; 00085 static struct timezone itz; 00086 void itstart(void) 00087 { 00088 gettimeofday(&_itstart, &itz); 00089 } 00090 static void itend(void) 00091 { 00092 gettimeofday(&_itend,&itz); 00093 } 00094 double itval(void) 00095 { 00096 double t1, t2; 00097 t1 = (double)_itstart.tv_sec + (double)_itstart.tv_usec/(1000*1000); 00098 t2 = (double)_itend.tv_sec + (double)_itend.tv_usec/(1000*1000); 00099 return t2-t1; 00100 } 00101 #endif 00102 00103 static float I[3][3] = {{1,0,0},{0,1,0},{0,0,1}}; 00104 static float ZERO[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}}; 00105 00106 /* 00107 #define C99 00108 #ifdef C99 00109 #defineDO_INLINE inline 00110 #else 00111 #defineDO_INLINE static 00112 #endif 00113 */ 00114 struct Cloth; 00115 00117 /* fast vector / matrix library, enhancements are welcome :) -dg */ 00119 00120 /* DEFINITIONS */ 00121 typedef float lfVector[3]; 00122 typedef struct fmatrix3x3 { 00123 float m[3][3]; /* 3x3 matrix */ 00124 unsigned int c,r; /* column and row number */ 00125 int pinned; /* is this vertex allowed to move? */ 00126 float n1,n2,n3; /* three normal vectors for collision constrains */ 00127 unsigned int vcount; /* vertex count */ 00128 unsigned int scount; /* spring count */ 00129 } fmatrix3x3; 00130 00132 // float[3] vector 00134 /* simple vector code */ 00135 /* STATUS: verified */ 00136 DO_INLINE void mul_fvector_S(float to[3], float from[3], float scalar) 00137 { 00138 to[0] = from[0] * scalar; 00139 to[1] = from[1] * scalar; 00140 to[2] = from[2] * scalar; 00141 } 00142 /* simple cross product */ 00143 /* STATUS: verified */ 00144 DO_INLINE void cross_fvector(float to[3], float vectorA[3], float vectorB[3]) 00145 { 00146 to[0] = vectorA[1] * vectorB[2] - vectorA[2] * vectorB[1]; 00147 to[1] = vectorA[2] * vectorB[0] - vectorA[0] * vectorB[2]; 00148 to[2] = vectorA[0] * vectorB[1] - vectorA[1] * vectorB[0]; 00149 } 00150 /* simple v^T * v product ("outer product") */ 00151 /* STATUS: HAS TO BE verified (*should* work) */ 00152 DO_INLINE void mul_fvectorT_fvector(float to[3][3], float vectorA[3], float vectorB[3]) 00153 { 00154 mul_fvector_S(to[0], vectorB, vectorA[0]); 00155 mul_fvector_S(to[1], vectorB, vectorA[1]); 00156 mul_fvector_S(to[2], vectorB, vectorA[2]); 00157 } 00158 /* simple v^T * v product with scalar ("outer product") */ 00159 /* STATUS: HAS TO BE verified (*should* work) */ 00160 DO_INLINE void mul_fvectorT_fvectorS(float to[3][3], float vectorA[3], float vectorB[3], float aS) 00161 { 00162 mul_fvectorT_fvector(to, vectorA, vectorB); 00163 00164 mul_fvector_S(to[0], to[0], aS); 00165 mul_fvector_S(to[1], to[1], aS); 00166 mul_fvector_S(to[2], to[2], aS); 00167 } 00168 00169 00170 /* printf vector[3] on console: for debug output */ 00171 static void print_fvector(float m3[3]) 00172 { 00173 printf("%f\n%f\n%f\n\n",m3[0],m3[1],m3[2]); 00174 } 00175 00177 // long float vector float (*)[3] 00179 /* print long vector on console: for debug output */ 00180 DO_INLINE void print_lfvector(float (*fLongVector)[3], unsigned int verts) 00181 { 00182 unsigned int i = 0; 00183 for(i = 0; i < verts; i++) 00184 { 00185 print_fvector(fLongVector[i]); 00186 } 00187 } 00188 /* create long vector */ 00189 DO_INLINE lfVector *create_lfvector(unsigned int verts) 00190 { 00191 // TODO: check if memory allocation was successfull */ 00192 return (lfVector *)MEM_callocN (verts * sizeof(lfVector), "cloth_implicit_alloc_vector"); 00193 // return (lfVector *)cloth_aligned_malloc(&MEMORY_BASE, verts * sizeof(lfVector)); 00194 } 00195 /* delete long vector */ 00196 DO_INLINE void del_lfvector(float (*fLongVector)[3]) 00197 { 00198 if (fLongVector != NULL) 00199 { 00200 MEM_freeN (fLongVector); 00201 // cloth_aligned_free(&MEMORY_BASE, fLongVector); 00202 } 00203 } 00204 /* copy long vector */ 00205 DO_INLINE void cp_lfvector(float (*to)[3], float (*from)[3], unsigned int verts) 00206 { 00207 memcpy(to, from, verts * sizeof(lfVector)); 00208 } 00209 /* init long vector with float[3] */ 00210 DO_INLINE void init_lfvector(float (*fLongVector)[3], float vector[3], unsigned int verts) 00211 { 00212 unsigned int i = 0; 00213 for(i = 0; i < verts; i++) 00214 { 00215 VECCOPY(fLongVector[i], vector); 00216 } 00217 } 00218 /* zero long vector with float[3] */ 00219 DO_INLINE void zero_lfvector(float (*to)[3], unsigned int verts) 00220 { 00221 memset(to, 0.0f, verts * sizeof(lfVector)); 00222 } 00223 /* multiply long vector with scalar*/ 00224 DO_INLINE void mul_lfvectorS(float (*to)[3], float (*fLongVector)[3], float scalar, unsigned int verts) 00225 { 00226 unsigned int i = 0; 00227 00228 for(i = 0; i < verts; i++) 00229 { 00230 mul_fvector_S(to[i], fLongVector[i], scalar); 00231 } 00232 } 00233 /* multiply long vector with scalar*/ 00234 /* A -= B * float */ 00235 DO_INLINE void submul_lfvectorS(float (*to)[3], float (*fLongVector)[3], float scalar, unsigned int verts) 00236 { 00237 unsigned int i = 0; 00238 for(i = 0; i < verts; i++) 00239 { 00240 VECSUBMUL(to[i], fLongVector[i], scalar); 00241 } 00242 } 00243 /* dot product for big vector */ 00244 DO_INLINE float dot_lfvector(float (*fLongVectorA)[3], float (*fLongVectorB)[3], unsigned int verts) 00245 { 00246 long i = 0; 00247 float temp = 0.0; 00248 // XXX brecht, disabled this for now (first schedule line was already disabled), 00249 // due to non-commutative nature of floating point ops this makes the sim give 00250 // different results each time you run it! 00251 // schedule(guided, 2) 00252 //#pragma omp parallel for reduction(+: temp) if(verts > CLOTH_OPENMP_LIMIT) 00253 for(i = 0; i < (long)verts; i++) 00254 { 00255 temp += INPR(fLongVectorA[i], fLongVectorB[i]); 00256 } 00257 return temp; 00258 } 00259 /* A = B + C --> for big vector */ 00260 DO_INLINE void add_lfvector_lfvector(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], unsigned int verts) 00261 { 00262 unsigned int i = 0; 00263 00264 for(i = 0; i < verts; i++) 00265 { 00266 VECADD(to[i], fLongVectorA[i], fLongVectorB[i]); 00267 } 00268 00269 } 00270 /* A = B + C * float --> for big vector */ 00271 DO_INLINE void add_lfvector_lfvectorS(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], float bS, unsigned int verts) 00272 { 00273 unsigned int i = 0; 00274 00275 for(i = 0; i < verts; i++) 00276 { 00277 VECADDS(to[i], fLongVectorA[i], fLongVectorB[i], bS); 00278 00279 } 00280 } 00281 /* A = B * float + C * float --> for big vector */ 00282 DO_INLINE void add_lfvectorS_lfvectorS(float (*to)[3], float (*fLongVectorA)[3], float aS, float (*fLongVectorB)[3], float bS, unsigned int verts) 00283 { 00284 unsigned int i = 0; 00285 00286 for(i = 0; i < verts; i++) 00287 { 00288 VECADDSS(to[i], fLongVectorA[i], aS, fLongVectorB[i], bS); 00289 } 00290 } 00291 /* A = B - C * float --> for big vector */ 00292 DO_INLINE void sub_lfvector_lfvectorS(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], float bS, unsigned int verts) 00293 { 00294 unsigned int i = 0; 00295 for(i = 0; i < verts; i++) 00296 { 00297 VECSUBS(to[i], fLongVectorA[i], fLongVectorB[i], bS); 00298 } 00299 00300 } 00301 /* A = B - C --> for big vector */ 00302 DO_INLINE void sub_lfvector_lfvector(float (*to)[3], float (*fLongVectorA)[3], float (*fLongVectorB)[3], unsigned int verts) 00303 { 00304 unsigned int i = 0; 00305 00306 for(i = 0; i < verts; i++) 00307 { 00308 VECSUB(to[i], fLongVectorA[i], fLongVectorB[i]); 00309 } 00310 00311 } 00313 // 3x3 matrix 00315 #if 0 00316 /* printf 3x3 matrix on console: for debug output */ 00317 static void print_fmatrix(float m3[3][3]) 00318 { 00319 printf("%f\t%f\t%f\n",m3[0][0],m3[0][1],m3[0][2]); 00320 printf("%f\t%f\t%f\n",m3[1][0],m3[1][1],m3[1][2]); 00321 printf("%f\t%f\t%f\n\n",m3[2][0],m3[2][1],m3[2][2]); 00322 } 00323 #endif 00324 00325 /* copy 3x3 matrix */ 00326 DO_INLINE void cp_fmatrix(float to[3][3], float from[3][3]) 00327 { 00328 // memcpy(to, from, sizeof (float) * 9); 00329 VECCOPY(to[0], from[0]); 00330 VECCOPY(to[1], from[1]); 00331 VECCOPY(to[2], from[2]); 00332 } 00333 00334 /* copy 3x3 matrix */ 00335 DO_INLINE void initdiag_fmatrixS(float to[3][3], float aS) 00336 { 00337 cp_fmatrix(to, ZERO); 00338 00339 to[0][0] = aS; 00340 to[1][1] = aS; 00341 to[2][2] = aS; 00342 } 00343 00344 /* calculate determinant of 3x3 matrix */ 00345 DO_INLINE float det_fmatrix(float m[3][3]) 00346 { 00347 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] 00348 -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]; 00349 } 00350 00351 DO_INLINE void inverse_fmatrix(float to[3][3], float from[3][3]) 00352 { 00353 unsigned int i, j; 00354 float d; 00355 00356 if((d=det_fmatrix(from))==0) 00357 { 00358 printf("can't build inverse"); 00359 exit(0); 00360 } 00361 for(i=0;i<3;i++) 00362 { 00363 for(j=0;j<3;j++) 00364 { 00365 int i1=(i+1)%3; 00366 int i2=(i+2)%3; 00367 int j1=(j+1)%3; 00368 int j2=(j+2)%3; 00369 // reverse indexs i&j to take transpose 00370 to[j][i] = (from[i1][j1]*from[i2][j2]-from[i1][j2]*from[i2][j1])/d; 00371 /* 00372 if(i==j) 00373 to[i][j] = 1.0f / from[i][j]; 00374 else 00375 to[i][j] = 0; 00376 */ 00377 } 00378 } 00379 00380 } 00381 00382 /* 3x3 matrix multiplied by a scalar */ 00383 /* STATUS: verified */ 00384 DO_INLINE void mul_fmatrix_S(float matrix[3][3], float scalar) 00385 { 00386 mul_fvector_S(matrix[0], matrix[0],scalar); 00387 mul_fvector_S(matrix[1], matrix[1],scalar); 00388 mul_fvector_S(matrix[2], matrix[2],scalar); 00389 } 00390 00391 /* a vector multiplied by a 3x3 matrix */ 00392 /* STATUS: verified */ 00393 DO_INLINE void mul_fvector_fmatrix(float *to, float *from, float matrix[3][3]) 00394 { 00395 to[0] = matrix[0][0]*from[0] + matrix[1][0]*from[1] + matrix[2][0]*from[2]; 00396 to[1] = matrix[0][1]*from[0] + matrix[1][1]*from[1] + matrix[2][1]*from[2]; 00397 to[2] = matrix[0][2]*from[0] + matrix[1][2]*from[1] + matrix[2][2]*from[2]; 00398 } 00399 00400 /* 3x3 matrix multiplied by a vector */ 00401 /* STATUS: verified */ 00402 DO_INLINE void mul_fmatrix_fvector(float *to, float matrix[3][3], float *from) 00403 { 00404 to[0] = INPR(matrix[0],from); 00405 to[1] = INPR(matrix[1],from); 00406 to[2] = INPR(matrix[2],from); 00407 } 00408 /* 3x3 matrix multiplied by a 3x3 matrix */ 00409 /* STATUS: verified */ 00410 DO_INLINE void mul_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3]) 00411 { 00412 mul_fvector_fmatrix(to[0], matrixA[0],matrixB); 00413 mul_fvector_fmatrix(to[1], matrixA[1],matrixB); 00414 mul_fvector_fmatrix(to[2], matrixA[2],matrixB); 00415 } 00416 /* 3x3 matrix addition with 3x3 matrix */ 00417 DO_INLINE void add_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3]) 00418 { 00419 VECADD(to[0], matrixA[0], matrixB[0]); 00420 VECADD(to[1], matrixA[1], matrixB[1]); 00421 VECADD(to[2], matrixA[2], matrixB[2]); 00422 } 00423 /* 3x3 matrix add-addition with 3x3 matrix */ 00424 DO_INLINE void addadd_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3]) 00425 { 00426 VECADDADD(to[0], matrixA[0], matrixB[0]); 00427 VECADDADD(to[1], matrixA[1], matrixB[1]); 00428 VECADDADD(to[2], matrixA[2], matrixB[2]); 00429 } 00430 /* 3x3 matrix sub-addition with 3x3 matrix */ 00431 DO_INLINE void addsub_fmatrixS_fmatrixS(float to[3][3], float matrixA[3][3], float aS, float matrixB[3][3], float bS) 00432 { 00433 VECADDSUBSS(to[0], matrixA[0], aS, matrixB[0], bS); 00434 VECADDSUBSS(to[1], matrixA[1], aS, matrixB[1], bS); 00435 VECADDSUBSS(to[2], matrixA[2], aS, matrixB[2], bS); 00436 } 00437 /* A -= B + C (3x3 matrix sub-addition with 3x3 matrix) */ 00438 DO_INLINE void subadd_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3]) 00439 { 00440 VECSUBADD(to[0], matrixA[0], matrixB[0]); 00441 VECSUBADD(to[1], matrixA[1], matrixB[1]); 00442 VECSUBADD(to[2], matrixA[2], matrixB[2]); 00443 } 00444 /* A -= B*x + C*y (3x3 matrix sub-addition with 3x3 matrix) */ 00445 DO_INLINE void subadd_fmatrixS_fmatrixS(float to[3][3], float matrixA[3][3], float aS, float matrixB[3][3], float bS) 00446 { 00447 VECSUBADDSS(to[0], matrixA[0], aS, matrixB[0], bS); 00448 VECSUBADDSS(to[1], matrixA[1], aS, matrixB[1], bS); 00449 VECSUBADDSS(to[2], matrixA[2], aS, matrixB[2], bS); 00450 } 00451 /* A = B - C (3x3 matrix subtraction with 3x3 matrix) */ 00452 DO_INLINE void sub_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3]) 00453 { 00454 VECSUB(to[0], matrixA[0], matrixB[0]); 00455 VECSUB(to[1], matrixA[1], matrixB[1]); 00456 VECSUB(to[2], matrixA[2], matrixB[2]); 00457 } 00458 /* A += B - C (3x3 matrix add-subtraction with 3x3 matrix) */ 00459 DO_INLINE void addsub_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3]) 00460 { 00461 VECADDSUB(to[0], matrixA[0], matrixB[0]); 00462 VECADDSUB(to[1], matrixA[1], matrixB[1]); 00463 VECADDSUB(to[2], matrixA[2], matrixB[2]); 00464 } 00466 // special functions 00468 /* a vector multiplied and added to/by a 3x3 matrix */ 00469 DO_INLINE void muladd_fvector_fmatrix(float to[3], float from[3], float matrix[3][3]) 00470 { 00471 to[0] += matrix[0][0]*from[0] + matrix[1][0]*from[1] + matrix[2][0]*from[2]; 00472 to[1] += matrix[0][1]*from[0] + matrix[1][1]*from[1] + matrix[2][1]*from[2]; 00473 to[2] += matrix[0][2]*from[0] + matrix[1][2]*from[1] + matrix[2][2]*from[2]; 00474 } 00475 /* 3x3 matrix multiplied and added to/by a 3x3 matrix and added to another 3x3 matrix */ 00476 DO_INLINE void muladd_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3]) 00477 { 00478 muladd_fvector_fmatrix(to[0], matrixA[0],matrixB); 00479 muladd_fvector_fmatrix(to[1], matrixA[1],matrixB); 00480 muladd_fvector_fmatrix(to[2], matrixA[2],matrixB); 00481 } 00482 /* a vector multiplied and sub'd to/by a 3x3 matrix */ 00483 DO_INLINE void mulsub_fvector_fmatrix(float to[3], float from[3], float matrix[3][3]) 00484 { 00485 to[0] -= matrix[0][0]*from[0] + matrix[1][0]*from[1] + matrix[2][0]*from[2]; 00486 to[1] -= matrix[0][1]*from[0] + matrix[1][1]*from[1] + matrix[2][1]*from[2]; 00487 to[2] -= matrix[0][2]*from[0] + matrix[1][2]*from[1] + matrix[2][2]*from[2]; 00488 } 00489 /* 3x3 matrix multiplied and sub'd to/by a 3x3 matrix and added to another 3x3 matrix */ 00490 DO_INLINE void mulsub_fmatrix_fmatrix(float to[3][3], float matrixA[3][3], float matrixB[3][3]) 00491 { 00492 mulsub_fvector_fmatrix(to[0], matrixA[0],matrixB); 00493 mulsub_fvector_fmatrix(to[1], matrixA[1],matrixB); 00494 mulsub_fvector_fmatrix(to[2], matrixA[2],matrixB); 00495 } 00496 /* 3x3 matrix multiplied+added by a vector */ 00497 /* STATUS: verified */ 00498 DO_INLINE void muladd_fmatrix_fvector(float to[3], float matrix[3][3], float from[3]) 00499 { 00500 to[0] += INPR(matrix[0],from); 00501 to[1] += INPR(matrix[1],from); 00502 to[2] += INPR(matrix[2],from); 00503 } 00504 /* 3x3 matrix multiplied+sub'ed by a vector */ 00505 DO_INLINE void mulsub_fmatrix_fvector(float to[3], float matrix[3][3], float from[3]) 00506 { 00507 to[0] -= INPR(matrix[0],from); 00508 to[1] -= INPR(matrix[1],from); 00509 to[2] -= INPR(matrix[2],from); 00510 } 00512 00514 // SPARSE SYMMETRIC big matrix with 3x3 matrix entries 00516 /* printf a big matrix on console: for debug output */ 00517 #if 0 00518 static void print_bfmatrix(fmatrix3x3 *m3) 00519 { 00520 unsigned int i = 0; 00521 00522 for(i = 0; i < m3[0].vcount + m3[0].scount; i++) 00523 { 00524 print_fmatrix(m3[i].m); 00525 } 00526 } 00527 #endif 00528 00529 /* create big matrix */ 00530 DO_INLINE fmatrix3x3 *create_bfmatrix(unsigned int verts, unsigned int springs) 00531 { 00532 // TODO: check if memory allocation was successfull */ 00533 fmatrix3x3 *temp = (fmatrix3x3 *)MEM_callocN (sizeof (fmatrix3x3) * (verts + springs), "cloth_implicit_alloc_matrix"); 00534 temp[0].vcount = verts; 00535 temp[0].scount = springs; 00536 return temp; 00537 } 00538 /* delete big matrix */ 00539 DO_INLINE void del_bfmatrix(fmatrix3x3 *matrix) 00540 { 00541 if (matrix != NULL) 00542 { 00543 MEM_freeN (matrix); 00544 } 00545 } 00546 00547 /* copy big matrix */ 00548 DO_INLINE void cp_bfmatrix(fmatrix3x3 *to, fmatrix3x3 *from) 00549 { 00550 // TODO bounds checking 00551 memcpy(to, from, sizeof(fmatrix3x3) * (from[0].vcount+from[0].scount) ); 00552 } 00553 00554 /* init big matrix */ 00555 // slow in parallel 00556 DO_INLINE void init_bfmatrix(fmatrix3x3 *matrix, float m3[3][3]) 00557 { 00558 unsigned int i; 00559 00560 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++) 00561 { 00562 cp_fmatrix(matrix[i].m, m3); 00563 } 00564 } 00565 00566 /* init the diagonal of big matrix */ 00567 // slow in parallel 00568 DO_INLINE void initdiag_bfmatrix(fmatrix3x3 *matrix, float m3[3][3]) 00569 { 00570 unsigned int i,j; 00571 float tmatrix[3][3] = {{0,0,0},{0,0,0},{0,0,0}}; 00572 00573 for(i = 0; i < matrix[0].vcount; i++) 00574 { 00575 cp_fmatrix(matrix[i].m, m3); 00576 } 00577 for(j = matrix[0].vcount; j < matrix[0].vcount+matrix[0].scount; j++) 00578 { 00579 cp_fmatrix(matrix[j].m, tmatrix); 00580 } 00581 } 00582 00583 /* multiply big matrix with scalar*/ 00584 DO_INLINE void mul_bfmatrix_S(fmatrix3x3 *matrix, float scalar) 00585 { 00586 unsigned int i = 0; 00587 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++) 00588 { 00589 mul_fmatrix_S(matrix[i].m, scalar); 00590 } 00591 } 00592 00593 /* SPARSE SYMMETRIC multiply big matrix with long vector*/ 00594 /* STATUS: verified */ 00595 DO_INLINE void mul_bfmatrix_lfvector( float (*to)[3], fmatrix3x3 *from, lfVector *fLongVector) 00596 { 00597 unsigned int i = 0; 00598 unsigned int vcount = from[0].vcount; 00599 lfVector *temp = create_lfvector(vcount); 00600 00601 zero_lfvector(to, vcount); 00602 00603 #pragma omp parallel sections private(i) if(vcount > CLOTH_OPENMP_LIMIT) 00604 { 00605 #pragma omp section 00606 { 00607 for(i = from[0].vcount; i < from[0].vcount+from[0].scount; i++) 00608 { 00609 muladd_fmatrix_fvector(to[from[i].c], from[i].m, fLongVector[from[i].r]); 00610 } 00611 } 00612 #pragma omp section 00613 { 00614 for(i = 0; i < from[0].vcount+from[0].scount; i++) 00615 { 00616 muladd_fmatrix_fvector(temp[from[i].r], from[i].m, fLongVector[from[i].c]); 00617 } 00618 } 00619 } 00620 add_lfvector_lfvector(to, to, temp, from[0].vcount); 00621 00622 del_lfvector(temp); 00623 00624 00625 } 00626 00627 /* SPARSE SYMMETRIC multiply big matrix with long vector (for diagonal preconditioner) */ 00628 /* STATUS: verified */ 00629 DO_INLINE void mul_prevfmatrix_lfvector( float (*to)[3], fmatrix3x3 *from, lfVector *fLongVector) 00630 { 00631 unsigned int i = 0; 00632 00633 for(i = 0; i < from[0].vcount; i++) 00634 { 00635 mul_fmatrix_fvector(to[from[i].r], from[i].m, fLongVector[from[i].c]); 00636 } 00637 } 00638 00639 /* SPARSE SYMMETRIC add big matrix with big matrix: A = B + C*/ 00640 DO_INLINE void add_bfmatrix_bfmatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix) 00641 { 00642 unsigned int i = 0; 00643 00644 /* process diagonal elements */ 00645 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++) 00646 { 00647 add_fmatrix_fmatrix(to[i].m, from[i].m, matrix[i].m); 00648 } 00649 00650 } 00651 /* SPARSE SYMMETRIC add big matrix with big matrix: A += B + C */ 00652 DO_INLINE void addadd_bfmatrix_bfmatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix) 00653 { 00654 unsigned int i = 0; 00655 00656 /* process diagonal elements */ 00657 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++) 00658 { 00659 addadd_fmatrix_fmatrix(to[i].m, from[i].m, matrix[i].m); 00660 } 00661 00662 } 00663 /* SPARSE SYMMETRIC subadd big matrix with big matrix: A -= B + C */ 00664 DO_INLINE void subadd_bfmatrix_bfmatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix) 00665 { 00666 unsigned int i = 0; 00667 00668 /* process diagonal elements */ 00669 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++) 00670 { 00671 subadd_fmatrix_fmatrix(to[i].m, from[i].m, matrix[i].m); 00672 } 00673 00674 } 00675 /* A = B - C (SPARSE SYMMETRIC sub big matrix with big matrix) */ 00676 DO_INLINE void sub_bfmatrix_bfmatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix) 00677 { 00678 unsigned int i = 0; 00679 00680 /* process diagonal elements */ 00681 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++) 00682 { 00683 sub_fmatrix_fmatrix(to[i].m, from[i].m, matrix[i].m); 00684 } 00685 00686 } 00687 /* SPARSE SYMMETRIC sub big matrix with big matrix S (special constraint matrix with limited entries) */ 00688 DO_INLINE void sub_bfmatrix_Smatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix) 00689 { 00690 unsigned int i = 0; 00691 00692 /* process diagonal elements */ 00693 for(i = 0; i < matrix[0].vcount; i++) 00694 { 00695 sub_fmatrix_fmatrix(to[matrix[i].c].m, from[matrix[i].c].m, matrix[i].m); 00696 } 00697 00698 } 00699 /* A += B - C (SPARSE SYMMETRIC addsub big matrix with big matrix) */ 00700 DO_INLINE void addsub_bfmatrix_bfmatrix( fmatrix3x3 *to, fmatrix3x3 *from, fmatrix3x3 *matrix) 00701 { 00702 unsigned int i = 0; 00703 00704 /* process diagonal elements */ 00705 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++) 00706 { 00707 addsub_fmatrix_fmatrix(to[i].m, from[i].m, matrix[i].m); 00708 } 00709 00710 } 00711 /* SPARSE SYMMETRIC sub big matrix with big matrix*/ 00712 /* A -= B * float + C * float --> for big matrix */ 00713 /* VERIFIED */ 00714 DO_INLINE void subadd_bfmatrixS_bfmatrixS( fmatrix3x3 *to, fmatrix3x3 *from, float aS, fmatrix3x3 *matrix, float bS) 00715 { 00716 unsigned int i = 0; 00717 00718 /* process diagonal elements */ 00719 for(i = 0; i < matrix[0].vcount+matrix[0].scount; i++) 00720 { 00721 subadd_fmatrixS_fmatrixS(to[i].m, from[i].m, aS, matrix[i].m, bS); 00722 } 00723 00724 } 00725 00727 // simulator start 00729 typedef struct Implicit_Data 00730 { 00731 lfVector *X, *V, *Xnew, *Vnew, *olddV, *F, *B, *dV, *z; 00732 fmatrix3x3 *A, *dFdV, *dFdX, *S, *P, *Pinv, *bigI, *M; 00733 } Implicit_Data; 00734 00735 int implicit_init (Object *UNUSED(ob), ClothModifierData *clmd) 00736 { 00737 unsigned int i = 0; 00738 unsigned int pinned = 0; 00739 Cloth *cloth = NULL; 00740 ClothVertex *verts = NULL; 00741 ClothSpring *spring = NULL; 00742 Implicit_Data *id = NULL; 00743 LinkNode *search = NULL; 00744 00745 if(G.rt > 0) 00746 printf("implicit_init\n"); 00747 00748 // init memory guard 00749 // MEMORY_BASE.first = MEMORY_BASE.last = NULL; 00750 00751 cloth = (Cloth *)clmd->clothObject; 00752 verts = cloth->verts; 00753 00754 // create implicit base 00755 id = (Implicit_Data *)MEM_callocN (sizeof(Implicit_Data), "implicit vecmat"); 00756 cloth->implicit = id; 00757 00758 /* process diagonal elements */ 00759 id->A = create_bfmatrix(cloth->numverts, cloth->numsprings); 00760 id->dFdV = create_bfmatrix(cloth->numverts, cloth->numsprings); 00761 id->dFdX = create_bfmatrix(cloth->numverts, cloth->numsprings); 00762 id->S = create_bfmatrix(cloth->numverts, 0); 00763 id->Pinv = create_bfmatrix(cloth->numverts, cloth->numsprings); 00764 id->P = create_bfmatrix(cloth->numverts, cloth->numsprings); 00765 id->bigI = create_bfmatrix(cloth->numverts, cloth->numsprings); // TODO 0 springs 00766 id->M = create_bfmatrix(cloth->numverts, cloth->numsprings); 00767 id->X = create_lfvector(cloth->numverts); 00768 id->Xnew = create_lfvector(cloth->numverts); 00769 id->V = create_lfvector(cloth->numverts); 00770 id->Vnew = create_lfvector(cloth->numverts); 00771 id->olddV = create_lfvector(cloth->numverts); 00772 zero_lfvector(id->olddV, cloth->numverts); 00773 id->F = create_lfvector(cloth->numverts); 00774 id->B = create_lfvector(cloth->numverts); 00775 id->dV = create_lfvector(cloth->numverts); 00776 id->z = create_lfvector(cloth->numverts); 00777 00778 for(i=0;i<cloth->numverts;i++) 00779 { 00780 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; 00781 00782 if(verts [i].flags & CLOTH_VERT_FLAG_PINNED) 00783 { 00784 id->S[pinned].pinned = 1; 00785 id->S[pinned].c = id->S[pinned].r = i; 00786 pinned++; 00787 } 00788 00789 initdiag_fmatrixS(id->M[i].m, verts[i].mass); 00790 } 00791 00792 // S is special and needs specific vcount and scount 00793 id->S[0].vcount = pinned; id->S[0].scount = 0; 00794 00795 // init springs 00796 search = cloth->springs; 00797 for(i=0;i<cloth->numsprings;i++) 00798 { 00799 spring = search->link; 00800 00801 // dFdV_start[i].r = big_I[i].r = big_zero[i].r = 00802 id->A[i+cloth->numverts].r = id->dFdV[i+cloth->numverts].r = id->dFdX[i+cloth->numverts].r = 00803 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; 00804 00805 // dFdV_start[i].c = big_I[i].c = big_zero[i].c = 00806 id->A[i+cloth->numverts].c = id->dFdV[i+cloth->numverts].c = id->dFdX[i+cloth->numverts].c = 00807 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; 00808 00809 spring->matrix_index = i + cloth->numverts; 00810 00811 search = search->next; 00812 } 00813 00814 initdiag_bfmatrix(id->bigI, I); 00815 00816 for(i = 0; i < cloth->numverts; i++) 00817 { 00818 VECCOPY(id->X[i], verts[i].x); 00819 } 00820 00821 return 1; 00822 } 00823 int implicit_free (ClothModifierData *clmd) 00824 { 00825 Implicit_Data *id; 00826 Cloth *cloth; 00827 cloth = (Cloth *)clmd->clothObject; 00828 00829 if(cloth) 00830 { 00831 id = cloth->implicit; 00832 00833 if(id) 00834 { 00835 del_bfmatrix(id->A); 00836 del_bfmatrix(id->dFdV); 00837 del_bfmatrix(id->dFdX); 00838 del_bfmatrix(id->S); 00839 del_bfmatrix(id->P); 00840 del_bfmatrix(id->Pinv); 00841 del_bfmatrix(id->bigI); 00842 del_bfmatrix(id->M); 00843 00844 del_lfvector(id->X); 00845 del_lfvector(id->Xnew); 00846 del_lfvector(id->V); 00847 del_lfvector(id->Vnew); 00848 del_lfvector(id->olddV); 00849 del_lfvector(id->F); 00850 del_lfvector(id->B); 00851 del_lfvector(id->dV); 00852 del_lfvector(id->z); 00853 00854 MEM_freeN(id); 00855 } 00856 } 00857 00858 return 1; 00859 } 00860 00861 DO_INLINE float fb(float length, float L) 00862 { 00863 float x = length/L; 00864 return (-11.541f*pow(x,4)+34.193f*pow(x,3)-39.083f*pow(x,2)+23.116f*x-9.713f); 00865 } 00866 00867 DO_INLINE float fbderiv(float length, float L) 00868 { 00869 float x = length/L; 00870 00871 return (-46.164f*pow(x,3)+102.579f*pow(x,2)-78.166f*x+23.116f); 00872 } 00873 00874 DO_INLINE float fbstar(float length, float L, float kb, float cb) 00875 { 00876 float tempfb = kb * fb(length, L); 00877 00878 float fbstar = cb * (length - L); 00879 00880 if(tempfb < fbstar) 00881 return fbstar; 00882 else 00883 return tempfb; 00884 } 00885 00886 // function to calculae bending spring force (taken from Choi & Co) 00887 DO_INLINE float fbstar_jacobi(float length, float L, float kb, float cb) 00888 { 00889 float tempfb = kb * fb(length, L); 00890 float fbstar = cb * (length - L); 00891 00892 if(tempfb < fbstar) 00893 { 00894 return cb; 00895 } 00896 else 00897 { 00898 return kb * fbderiv(length, L); 00899 } 00900 } 00901 00902 DO_INLINE void filter(lfVector *V, fmatrix3x3 *S) 00903 { 00904 unsigned int i=0; 00905 00906 for(i=0;i<S[0].vcount;i++) 00907 { 00908 mul_fvector_fmatrix(V[S[i].r], V[S[i].r], S[i].m); 00909 } 00910 } 00911 00912 static int cg_filtered(lfVector *ldV, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S) 00913 { 00914 // Solves for unknown X in equation AX=B 00915 unsigned int conjgrad_loopcount=0, conjgrad_looplimit=100; 00916 float conjgrad_epsilon=0.0001f, conjgrad_lasterror=0; 00917 lfVector *q, *d, *tmp, *r; 00918 float s, starget, a, s_prev; 00919 unsigned int numverts = lA[0].vcount; 00920 q = create_lfvector(numverts); 00921 d = create_lfvector(numverts); 00922 tmp = create_lfvector(numverts); 00923 r = create_lfvector(numverts); 00924 00925 // zero_lfvector(ldV, CLOTHPARTICLES); 00926 filter(ldV, S); 00927 00928 add_lfvector_lfvector(ldV, ldV, z, numverts); 00929 00930 // r = B - Mul(tmp,A,X); // just use B if X known to be zero 00931 cp_lfvector(r, lB, numverts); 00932 mul_bfmatrix_lfvector(tmp, lA, ldV); 00933 sub_lfvector_lfvector(r, r, tmp, numverts); 00934 00935 filter(r,S); 00936 00937 cp_lfvector(d, r, numverts); 00938 00939 s = dot_lfvector(r, r, numverts); 00940 starget = s * sqrt(conjgrad_epsilon); 00941 00942 while(s>starget && conjgrad_loopcount < conjgrad_looplimit) 00943 { 00944 // Mul(q,A,d); // q = A*d; 00945 mul_bfmatrix_lfvector(q, lA, d); 00946 00947 filter(q,S); 00948 00949 a = s/dot_lfvector(d, q, numverts); 00950 00951 // X = X + d*a; 00952 add_lfvector_lfvectorS(ldV, ldV, d, a, numverts); 00953 00954 // r = r - q*a; 00955 sub_lfvector_lfvectorS(r, r, q, a, numverts); 00956 00957 s_prev = s; 00958 s = dot_lfvector(r, r, numverts); 00959 00960 //d = r+d*(s/s_prev); 00961 add_lfvector_lfvectorS(d, r, d, (s/s_prev), numverts); 00962 00963 filter(d,S); 00964 00965 conjgrad_loopcount++; 00966 } 00967 conjgrad_lasterror = s; 00968 00969 del_lfvector(q); 00970 del_lfvector(d); 00971 del_lfvector(tmp); 00972 del_lfvector(r); 00973 // printf("W/O conjgrad_loopcount: %d\n", conjgrad_loopcount); 00974 00975 return conjgrad_loopcount<conjgrad_looplimit; // true means we reached desired accuracy in given time - ie stable 00976 } 00977 00978 // block diagonalizer 00979 DO_INLINE void BuildPPinv(fmatrix3x3 *lA, fmatrix3x3 *P, fmatrix3x3 *Pinv) 00980 { 00981 unsigned int i = 0; 00982 00983 // Take only the diagonal blocks of A 00984 // #pragma omp parallel for private(i) if(lA[0].vcount > CLOTH_OPENMP_LIMIT) 00985 for(i = 0; i<lA[0].vcount; i++) 00986 { 00987 // block diagonalizer 00988 cp_fmatrix(P[i].m, lA[i].m); 00989 inverse_fmatrix(Pinv[i].m, P[i].m); 00990 00991 } 00992 } 00993 #if 0 00994 /* 00995 // version 1.3 00996 static int cg_filtered_pre(lfVector *dv, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S, fmatrix3x3 *P, fmatrix3x3 *Pinv) 00997 { 00998 unsigned int numverts = lA[0].vcount, iterations = 0, conjgrad_looplimit=100; 00999 float delta0 = 0, deltaNew = 0, deltaOld = 0, alpha = 0; 01000 float conjgrad_epsilon=0.0001; // 0.2 is dt for steps=5 01001 lfVector *r = create_lfvector(numverts); 01002 lfVector *p = create_lfvector(numverts); 01003 lfVector *s = create_lfvector(numverts); 01004 lfVector *h = create_lfvector(numverts); 01005 01006 BuildPPinv(lA, P, Pinv); 01007 01008 filter(dv, S); 01009 add_lfvector_lfvector(dv, dv, z, numverts); 01010 01011 mul_bfmatrix_lfvector(r, lA, dv); 01012 sub_lfvector_lfvector(r, lB, r, numverts); 01013 filter(r, S); 01014 01015 mul_prevfmatrix_lfvector(p, Pinv, r); 01016 filter(p, S); 01017 01018 deltaNew = dot_lfvector(r, p, numverts); 01019 01020 delta0 = deltaNew * sqrt(conjgrad_epsilon); 01021 01022 // itstart(); 01023 01024 while ((deltaNew > delta0) && (iterations < conjgrad_looplimit)) 01025 { 01026 iterations++; 01027 01028 mul_bfmatrix_lfvector(s, lA, p); 01029 filter(s, S); 01030 01031 alpha = deltaNew / dot_lfvector(p, s, numverts); 01032 01033 add_lfvector_lfvectorS(dv, dv, p, alpha, numverts); 01034 01035 add_lfvector_lfvectorS(r, r, s, -alpha, numverts); 01036 01037 mul_prevfmatrix_lfvector(h, Pinv, r); 01038 filter(h, S); 01039 01040 deltaOld = deltaNew; 01041 01042 deltaNew = dot_lfvector(r, h, numverts); 01043 01044 add_lfvector_lfvectorS(p, h, p, deltaNew / deltaOld, numverts); 01045 01046 filter(p, S); 01047 01048 } 01049 01050 // itend(); 01051 // printf("cg_filtered_pre time: %f\n", (float)itval()); 01052 01053 del_lfvector(h); 01054 del_lfvector(s); 01055 del_lfvector(p); 01056 del_lfvector(r); 01057 01058 printf("iterations: %d\n", iterations); 01059 01060 return iterations<conjgrad_looplimit; 01061 } 01062 */ 01063 // version 1.4 01064 static int cg_filtered_pre(lfVector *dv, fmatrix3x3 *lA, lfVector *lB, lfVector *z, fmatrix3x3 *S, fmatrix3x3 *P, fmatrix3x3 *Pinv, fmatrix3x3 *bigI) 01065 { 01066 unsigned int numverts = lA[0].vcount, iterations = 0, conjgrad_looplimit=100; 01067 float delta0 = 0, deltaNew = 0, deltaOld = 0, alpha = 0, tol = 0; 01068 lfVector *r = create_lfvector(numverts); 01069 lfVector *p = create_lfvector(numverts); 01070 lfVector *s = create_lfvector(numverts); 01071 lfVector *h = create_lfvector(numverts); 01072 lfVector *bhat = create_lfvector(numverts); 01073 lfVector *btemp = create_lfvector(numverts); 01074 01075 BuildPPinv(lA, P, Pinv); 01076 01077 initdiag_bfmatrix(bigI, I); 01078 sub_bfmatrix_Smatrix(bigI, bigI, S); 01079 01080 // x = Sx_0+(I-S)z 01081 filter(dv, S); 01082 add_lfvector_lfvector(dv, dv, z, numverts); 01083 01084 // b_hat = S(b-A(I-S)z) 01085 mul_bfmatrix_lfvector(r, lA, z); 01086 mul_bfmatrix_lfvector(bhat, bigI, r); 01087 sub_lfvector_lfvector(bhat, lB, bhat, numverts); 01088 01089 // r = S(b-Ax) 01090 mul_bfmatrix_lfvector(r, lA, dv); 01091 sub_lfvector_lfvector(r, lB, r, numverts); 01092 filter(r, S); 01093 01094 // p = SP^-1r 01095 mul_prevfmatrix_lfvector(p, Pinv, r); 01096 filter(p, S); 01097 01098 // delta0 = bhat^TP^-1bhat 01099 mul_prevfmatrix_lfvector(btemp, Pinv, bhat); 01100 delta0 = dot_lfvector(bhat, btemp, numverts); 01101 01102 // deltaNew = r^TP 01103 deltaNew = dot_lfvector(r, p, numverts); 01104 01105 /* 01106 filter(dv, S); 01107 add_lfvector_lfvector(dv, dv, z, numverts); 01108 01109 mul_bfmatrix_lfvector(r, lA, dv); 01110 sub_lfvector_lfvector(r, lB, r, numverts); 01111 filter(r, S); 01112 01113 mul_prevfmatrix_lfvector(p, Pinv, r); 01114 filter(p, S); 01115 01116 deltaNew = dot_lfvector(r, p, numverts); 01117 01118 delta0 = deltaNew * sqrt(conjgrad_epsilon); 01119 */ 01120 01121 // itstart(); 01122 01123 tol = (0.01*0.2); 01124 01125 while ((deltaNew > delta0*tol*tol) && (iterations < conjgrad_looplimit)) 01126 { 01127 iterations++; 01128 01129 mul_bfmatrix_lfvector(s, lA, p); 01130 filter(s, S); 01131 01132 alpha = deltaNew / dot_lfvector(p, s, numverts); 01133 01134 add_lfvector_lfvectorS(dv, dv, p, alpha, numverts); 01135 01136 add_lfvector_lfvectorS(r, r, s, -alpha, numverts); 01137 01138 mul_prevfmatrix_lfvector(h, Pinv, r); 01139 filter(h, S); 01140 01141 deltaOld = deltaNew; 01142 01143 deltaNew = dot_lfvector(r, h, numverts); 01144 01145 add_lfvector_lfvectorS(p, h, p, deltaNew / deltaOld, numverts); 01146 01147 filter(p, S); 01148 01149 } 01150 01151 // itend(); 01152 // printf("cg_filtered_pre time: %f\n", (float)itval()); 01153 01154 del_lfvector(btemp); 01155 del_lfvector(bhat); 01156 del_lfvector(h); 01157 del_lfvector(s); 01158 del_lfvector(p); 01159 del_lfvector(r); 01160 01161 // printf("iterations: %d\n", iterations); 01162 01163 return iterations<conjgrad_looplimit; 01164 } 01165 #endif 01166 01167 // outer product is NOT cross product!!! 01168 DO_INLINE void dfdx_spring_type1(float to[3][3], float extent[3], float length, float L, float dot, float k) 01169 { 01170 // dir is unit length direction, rest is spring's restlength, k is spring constant. 01171 // return (outerprod(dir,dir)*k + (I - outerprod(dir,dir))*(k - ((k*L)/length))); 01172 float temp[3][3]; 01173 float temp1 = k*(1.0 - (L/length)); 01174 01175 mul_fvectorT_fvectorS(temp, extent, extent, 1.0 / dot); 01176 sub_fmatrix_fmatrix(to, I, temp); 01177 mul_fmatrix_S(to, temp1); 01178 01179 mul_fvectorT_fvectorS(temp, extent, extent, k/ dot); 01180 add_fmatrix_fmatrix(to, to, temp); 01181 01182 /* 01183 mul_fvectorT_fvector(temp, dir, dir); 01184 sub_fmatrix_fmatrix(to, I, temp); 01185 mul_fmatrix_S(to, k* (1.0f-(L/length))); 01186 mul_fmatrix_S(temp, k); 01187 add_fmatrix_fmatrix(to, temp, to); 01188 */ 01189 } 01190 01191 DO_INLINE void dfdx_spring_type2(float to[3][3], float dir[3], float length, float L, float k, float cb) 01192 { 01193 // return outerprod(dir,dir)*fbstar_jacobi(length, L, k, cb); 01194 mul_fvectorT_fvectorS(to, dir, dir, fbstar_jacobi(length, L, k, cb)); 01195 } 01196 01197 DO_INLINE void dfdv_damp(float to[3][3], float dir[3], float damping) 01198 { 01199 // derivative of force wrt velocity. 01200 mul_fvectorT_fvectorS(to, dir, dir, damping); 01201 01202 } 01203 01204 DO_INLINE void dfdx_spring(float to[3][3], float dir[3],float length,float L,float k) 01205 { 01206 // dir is unit length direction, rest is spring's restlength, k is spring constant. 01207 //return ( (I-outerprod(dir,dir))*Min(1.0f,rest/length) - I) * -k; 01208 mul_fvectorT_fvector(to, dir, dir); 01209 sub_fmatrix_fmatrix(to, I, to); 01210 01211 mul_fmatrix_S(to, (L/length)); 01212 sub_fmatrix_fmatrix(to, to, I); 01213 mul_fmatrix_S(to, -k); 01214 } 01215 01216 // unused atm 01217 DO_INLINE void dfdx_damp(float to[3][3], float dir[3],float length,const float vel[3],float rest,float damping) 01218 { 01219 // inner spring damping vel is the relative velocity of the endpoints. 01220 // return (I-outerprod(dir,dir)) * (-damping * -(dot(dir,vel)/Max(length,rest))); 01221 mul_fvectorT_fvector(to, dir, dir); 01222 sub_fmatrix_fmatrix(to, I, to); 01223 mul_fmatrix_S(to, (-damping * -(INPR(dir,vel)/MAX2(length,rest)))); 01224 01225 } 01226 01227 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) 01228 { 01229 Cloth *cloth = clmd->clothObject; 01230 ClothVertex *verts = cloth->verts; 01231 float extent[3]; 01232 float length = 0, dot = 0; 01233 float dir[3] = {0,0,0}; 01234 float vel[3]; 01235 float k = 0.0f; 01236 float L = s->restlen; 01237 float cb; /* = clmd->sim_parms->structural; */ /*UNUSED*/ 01238 01239 float nullf[3] = {0,0,0}; 01240 float stretch_force[3] = {0,0,0}; 01241 float bending_force[3] = {0,0,0}; 01242 float damping_force[3] = {0,0,0}; 01243 float nulldfdx[3][3]={ {0,0,0}, {0,0,0}, {0,0,0}}; 01244 01245 float scaling = 0.0; 01246 01247 int no_compress = clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_NO_SPRING_COMPRESS; 01248 01249 VECCOPY(s->f, nullf); 01250 cp_fmatrix(s->dfdx, nulldfdx); 01251 cp_fmatrix(s->dfdv, nulldfdx); 01252 01253 // calculate elonglation 01254 VECSUB(extent, X[s->kl], X[s->ij]); 01255 VECSUB(vel, V[s->kl], V[s->ij]); 01256 dot = INPR(extent, extent); 01257 length = sqrt(dot); 01258 01259 s->flags &= ~CLOTH_SPRING_FLAG_NEEDED; 01260 01261 if(length > ALMOST_ZERO) 01262 { 01263 /* 01264 if(length>L) 01265 { 01266 if((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED) 01267 && ((((length-L)*100.0f/L) > clmd->sim_parms->maxspringlen))) // cut spring! 01268 { 01269 s->flags |= CSPRING_FLAG_DEACTIVATE; 01270 return; 01271 } 01272 } 01273 */ 01274 mul_fvector_S(dir, extent, 1.0f/length); 01275 } 01276 else 01277 { 01278 mul_fvector_S(dir, extent, 0.0f); 01279 } 01280 01281 // calculate force of structural + shear springs 01282 if((s->type & CLOTH_SPRING_TYPE_STRUCTURAL) || (s->type & CLOTH_SPRING_TYPE_SHEAR)) 01283 { 01284 if(length > L || no_compress) 01285 { 01286 s->flags |= CLOTH_SPRING_FLAG_NEEDED; 01287 01288 k = clmd->sim_parms->structural; 01289 01290 scaling = k + s->stiffness * ABS(clmd->sim_parms->max_struct-k); 01291 01292 k = scaling / (clmd->sim_parms->avg_spring_len + FLT_EPSILON); 01293 01294 // TODO: verify, half verified (couldn't see error) 01295 mul_fvector_S(stretch_force, dir, k*(length-L)); 01296 01297 VECADD(s->f, s->f, stretch_force); 01298 01299 // Ascher & Boxman, p.21: Damping only during elonglation 01300 // something wrong with it... 01301 mul_fvector_S(damping_force, dir, clmd->sim_parms->Cdis * INPR(vel,dir)); 01302 VECADD(s->f, s->f, damping_force); 01303 01304 /* VERIFIED */ 01305 dfdx_spring(s->dfdx, dir, length, L, k); 01306 01307 /* VERIFIED */ 01308 dfdv_damp(s->dfdv, dir, clmd->sim_parms->Cdis); 01309 01310 } 01311 } 01312 else if(s->type & CLOTH_SPRING_TYPE_GOAL) 01313 { 01314 float tvect[3]; 01315 01316 s->flags |= CLOTH_SPRING_FLAG_NEEDED; 01317 01318 // current_position = xold + t * (newposition - xold) 01319 VECSUB(tvect, verts[s->ij].xconst, verts[s->ij].xold); 01320 mul_fvector_S(tvect, tvect, time); 01321 VECADD(tvect, tvect, verts[s->ij].xold); 01322 01323 VECSUB(extent, X[s->ij], tvect); 01324 01325 // SEE MSG BELOW (these are UNUSED) 01326 // dot = INPR(extent, extent); 01327 // length = sqrt(dot); 01328 01329 k = clmd->sim_parms->goalspring; 01330 01331 scaling = k + s->stiffness * ABS(clmd->sim_parms->max_struct-k); 01332 01333 k = verts [s->ij].goal * scaling / (clmd->sim_parms->avg_spring_len + FLT_EPSILON); 01334 01335 VECADDS(s->f, s->f, extent, -k); 01336 01337 mul_fvector_S(damping_force, dir, clmd->sim_parms->goalfrict * 0.01 * INPR(vel,dir)); 01338 VECADD(s->f, s->f, damping_force); 01339 01340 // HERE IS THE PROBLEM!!!! 01341 // dfdx_spring(s->dfdx, dir, length, 0.0, k); 01342 // dfdv_damp(s->dfdv, dir, MIN2(1.0, (clmd->sim_parms->goalfrict/100.0))); 01343 } 01344 else // calculate force of bending springs 01345 { 01346 if(length < L) 01347 { 01348 s->flags |= CLOTH_SPRING_FLAG_NEEDED; 01349 01350 k = clmd->sim_parms->bending; 01351 01352 scaling = k + s->stiffness * ABS(clmd->sim_parms->max_bend-k); 01353 cb = k = scaling / (20.0*(clmd->sim_parms->avg_spring_len + FLT_EPSILON)); 01354 01355 mul_fvector_S(bending_force, dir, fbstar(length, L, k, cb)); 01356 VECADD(s->f, s->f, bending_force); 01357 01358 dfdx_spring_type2(s->dfdx, dir, length,L, k, cb); 01359 } 01360 } 01361 } 01362 01363 DO_INLINE void cloth_apply_spring_force(ClothModifierData *UNUSED(clmd), ClothSpring *s, lfVector *lF, lfVector *UNUSED(X), lfVector *UNUSED(V), fmatrix3x3 *dFdV, fmatrix3x3 *dFdX) 01364 { 01365 if(s->flags & CLOTH_SPRING_FLAG_NEEDED) 01366 { 01367 if(!(s->type & CLOTH_SPRING_TYPE_BENDING)) 01368 { 01369 sub_fmatrix_fmatrix(dFdV[s->ij].m, dFdV[s->ij].m, s->dfdv); 01370 sub_fmatrix_fmatrix(dFdV[s->kl].m, dFdV[s->kl].m, s->dfdv); 01371 add_fmatrix_fmatrix(dFdV[s->matrix_index].m, dFdV[s->matrix_index].m, s->dfdv); 01372 } 01373 01374 VECADD(lF[s->ij], lF[s->ij], s->f); 01375 01376 if(!(s->type & CLOTH_SPRING_TYPE_GOAL)) 01377 VECSUB(lF[s->kl], lF[s->kl], s->f); 01378 01379 sub_fmatrix_fmatrix(dFdX[s->kl].m, dFdX[s->kl].m, s->dfdx); 01380 sub_fmatrix_fmatrix(dFdX[s->ij].m, dFdX[s->ij].m, s->dfdx); 01381 add_fmatrix_fmatrix(dFdX[s->matrix_index].m, dFdX[s->matrix_index].m, s->dfdx); 01382 } 01383 } 01384 01385 01386 static void CalcFloat( float *v1, float *v2, float *v3, float *n) 01387 { 01388 float n1[3],n2[3]; 01389 01390 n1[0]= v1[0]-v2[0]; 01391 n2[0]= v2[0]-v3[0]; 01392 n1[1]= v1[1]-v2[1]; 01393 n2[1]= v2[1]-v3[1]; 01394 n1[2]= v1[2]-v2[2]; 01395 n2[2]= v2[2]-v3[2]; 01396 n[0]= n1[1]*n2[2]-n1[2]*n2[1]; 01397 n[1]= n1[2]*n2[0]-n1[0]*n2[2]; 01398 n[2]= n1[0]*n2[1]-n1[1]*n2[0]; 01399 } 01400 01401 static void CalcFloat4( float *v1, float *v2, float *v3, float *v4, float *n) 01402 { 01403 /* real cross! */ 01404 float n1[3],n2[3]; 01405 01406 n1[0]= v1[0]-v3[0]; 01407 n1[1]= v1[1]-v3[1]; 01408 n1[2]= v1[2]-v3[2]; 01409 01410 n2[0]= v2[0]-v4[0]; 01411 n2[1]= v2[1]-v4[1]; 01412 n2[2]= v2[2]-v4[2]; 01413 01414 n[0]= n1[1]*n2[2]-n1[2]*n2[1]; 01415 n[1]= n1[2]*n2[0]-n1[0]*n2[2]; 01416 n[2]= n1[0]*n2[1]-n1[1]*n2[0]; 01417 } 01418 01419 static float calculateVertexWindForce(float wind[3], float vertexnormal[3]) 01420 { 01421 return (INPR(wind, vertexnormal)); 01422 } 01423 01424 typedef struct HairGridVert { 01425 float velocity[3]; 01426 float density; 01427 } HairGridVert; 01428 #define HAIR_GRID_INDEX(vec, min, max, axis) (int)( (vec[axis] - min[axis]) / (max[axis] - min[axis]) * 9.99f ); 01429 /* Smoothing of hair velocities: 01430 * adapted from 01431 Volumetric Methods for Simulation and Rendering of Hair 01432 by Lena Petrovic, Mark Henne and John Anderson 01433 * Pixar Technical Memo #06-08, Pixar Animation Studios 01434 */ 01435 static void hair_velocity_smoothing(ClothModifierData *clmd, lfVector *lF, lfVector *lX, lfVector *lV, unsigned int numverts) 01436 { 01437 /* TODO: This is an initial implementation and should be made much better in due time. 01438 * What should at least be implemented is a grid size parameter and a smoothing kernel 01439 * for bigger grids. 01440 */ 01441 01442 /* 10x10x10 grid gives nice initial results */ 01443 HairGridVert grid[10][10][10]; 01444 HairGridVert colg[10][10][10]; 01445 ListBase *colliders = get_collider_cache(clmd->scene, NULL, NULL); 01446 ColliderCache *col = NULL; 01447 float gmin[3], gmax[3], density; 01448 /* 2.0f is an experimental value that seems to give good results */ 01449 float smoothfac = 2.0f * clmd->sim_parms->velocity_smooth; 01450 float collfac = 2.0f * clmd->sim_parms->collider_friction; 01451 unsigned int v = 0; 01452 unsigned int i = 0; 01453 int j = 0; 01454 int k = 0; 01455 01456 INIT_MINMAX(gmin, gmax); 01457 01458 for(i = 0; i < numverts; i++) 01459 DO_MINMAX(lX[i], gmin, gmax); 01460 01461 /* initialize grid */ 01462 for(i = 0; i < 10; i++) { 01463 for(j = 0; j < 10; j++) { 01464 for(k = 0; k < 10; k++) { 01465 grid[i][j][k].velocity[0] = 0.0f; 01466 grid[i][j][k].velocity[1] = 0.0f; 01467 grid[i][j][k].velocity[2] = 0.0f; 01468 grid[i][j][k].density = 0.0f; 01469 01470 colg[i][j][k].velocity[0] = 0.0f; 01471 colg[i][j][k].velocity[1] = 0.0f; 01472 colg[i][j][k].velocity[2] = 0.0f; 01473 colg[i][j][k].density = 0.0f; 01474 } 01475 } 01476 } 01477 01478 /* gather velocities & density */ 01479 if(smoothfac > 0.0f) for(v = 0; v < numverts; v++) { 01480 i = HAIR_GRID_INDEX(lX[v], gmin, gmax, 0); 01481 j = HAIR_GRID_INDEX(lX[v], gmin, gmax, 1); 01482 k = HAIR_GRID_INDEX(lX[v], gmin, gmax, 2); 01483 if (i < 0 || j < 0 || k < 0 || i > 10 || j >= 10 || k >= 10) 01484 continue; 01485 01486 grid[i][j][k].velocity[0] += lV[v][0]; 01487 grid[i][j][k].velocity[1] += lV[v][1]; 01488 grid[i][j][k].velocity[2] += lV[v][2]; 01489 grid[i][j][k].density += 1.0f; 01490 } 01491 01492 /* gather colliders */ 01493 if(colliders && collfac > 0.0f) for(col = colliders->first; col; col = col->next) 01494 { 01495 MVert *loc0 = col->collmd->x; 01496 MVert *loc1 = col->collmd->xnew; 01497 float vel[3]; 01498 01499 for(v=0; v<col->collmd->numverts; v++, loc0++, loc1++) { 01500 i = HAIR_GRID_INDEX(loc1->co, gmin, gmax, 0); 01501 01502 if(i>=0 && i<10) { 01503 j = HAIR_GRID_INDEX(loc1->co, gmin, gmax, 1); 01504 01505 if(j>=0 && j<10) { 01506 k = HAIR_GRID_INDEX(loc1->co, gmin, gmax, 2); 01507 01508 if(k>=0 && k<10) { 01509 VECSUB(vel, loc1->co, loc0->co); 01510 01511 colg[i][j][k].velocity[0] += vel[0]; 01512 colg[i][j][k].velocity[1] += vel[1]; 01513 colg[i][j][k].velocity[2] += vel[2]; 01514 colg[i][j][k].density += 1.0; 01515 } 01516 } 01517 } 01518 } 01519 } 01520 01521 01522 /* divide velocity with density */ 01523 for(i = 0; i < 10; i++) { 01524 for(j = 0; j < 10; j++) { 01525 for(k = 0; k < 10; k++) { 01526 density = grid[i][j][k].density; 01527 if(density > 0.0f) { 01528 grid[i][j][k].velocity[0] /= density; 01529 grid[i][j][k].velocity[1] /= density; 01530 grid[i][j][k].velocity[2] /= density; 01531 } 01532 01533 density = colg[i][j][k].density; 01534 if(density > 0.0f) { 01535 colg[i][j][k].velocity[0] /= density; 01536 colg[i][j][k].velocity[1] /= density; 01537 colg[i][j][k].velocity[2] /= density; 01538 } 01539 } 01540 } 01541 } 01542 01543 /* calculate forces */ 01544 for(v = 0; v < numverts; v++) { 01545 i = HAIR_GRID_INDEX(lX[v], gmin, gmax, 0); 01546 j = HAIR_GRID_INDEX(lX[v], gmin, gmax, 1); 01547 k = HAIR_GRID_INDEX(lX[v], gmin, gmax, 2); 01548 if (i < 0 || j < 0 || k < 0 || i > 10 || j >= 10 || k >= 10) 01549 continue; 01550 01551 lF[v][0] += smoothfac * (grid[i][j][k].velocity[0] - lV[v][0]); 01552 lF[v][1] += smoothfac * (grid[i][j][k].velocity[1] - lV[v][1]); 01553 lF[v][2] += smoothfac * (grid[i][j][k].velocity[2] - lV[v][2]); 01554 01555 if(colg[i][j][k].density > 0.0f) { 01556 lF[v][0] += collfac * (colg[i][j][k].velocity[0] - lV[v][0]); 01557 lF[v][1] += collfac * (colg[i][j][k].velocity[1] - lV[v][1]); 01558 lF[v][2] += collfac * (colg[i][j][k].velocity[2] - lV[v][2]); 01559 } 01560 } 01561 01562 free_collider_cache(&colliders); 01563 } 01564 01565 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) 01566 { 01567 /* Collect forces and derivatives: F,dFdX,dFdV */ 01568 Cloth *cloth = clmd->clothObject; 01569 unsigned int i = 0; 01570 float spring_air = clmd->sim_parms->Cvi * 0.01f; /* viscosity of air scaled in percent */ 01571 float gravity[3] = {0.0f, 0.0f, 0.0f}; 01572 float tm2[3][3] = {{0}}; 01573 MFace *mfaces = cloth->mfaces; 01574 unsigned int numverts = cloth->numverts; 01575 LinkNode *search; 01576 lfVector *winvec; 01577 EffectedPoint epoint; 01578 01579 tm2[0][0]= tm2[1][1]= tm2[2][2]= -spring_air; 01580 01581 /* global acceleration (gravitation) */ 01582 if(clmd->scene->physics_settings.flag & PHYS_GLOBAL_GRAVITY) { 01583 VECCOPY(gravity, clmd->scene->physics_settings.gravity); 01584 mul_fvector_S(gravity, gravity, 0.001f * clmd->sim_parms->effector_weights->global_gravity); /* scale gravity force */ 01585 } 01586 01587 /* set dFdX jacobi matrix to zero */ 01588 init_bfmatrix(dFdX, ZERO); 01589 /* set dFdX jacobi matrix diagonal entries to -spring_air */ 01590 initdiag_bfmatrix(dFdV, tm2); 01591 01592 init_lfvector(lF, gravity, numverts); 01593 01594 if(clmd->sim_parms->velocity_smooth > 0.0f || clmd->sim_parms->collider_friction > 0.0f) 01595 hair_velocity_smoothing(clmd, lF, lX, lV, numverts); 01596 01597 /* multiply lF with mass matrix 01598 // force = mass * acceleration (in this case: gravity) 01599 */ 01600 for(i = 0; i < numverts; i++) 01601 { 01602 float temp[3]; 01603 VECCOPY(temp, lF[i]); 01604 mul_fmatrix_fvector(lF[i], M[i].m, temp); 01605 } 01606 01607 submul_lfvectorS(lF, lV, spring_air, numverts); 01608 01609 /* handle external forces like wind */ 01610 if(effectors) 01611 { 01612 // 0 = force, 1 = normalized force 01613 winvec = create_lfvector(cloth->numverts); 01614 01615 if(!winvec) 01616 printf("winvec: out of memory in implicit.c\n"); 01617 01618 // precalculate wind forces 01619 for(i = 0; i < cloth->numverts; i++) 01620 { 01621 pd_point_from_loc(clmd->scene, (float*)lX[i], (float*)lV[i], i, &epoint); 01622 pdDoEffectors(effectors, NULL, clmd->sim_parms->effector_weights, &epoint, winvec[i], NULL); 01623 } 01624 01625 for(i = 0; i < cloth->numfaces; i++) 01626 { 01627 float trinormal[3]={0,0,0}; // normalized triangle normal 01628 float triunnormal[3]={0,0,0}; // not-normalized-triangle normal 01629 float tmp[3]={0,0,0}; 01630 float factor = (mfaces[i].v4) ? 0.25 : 1.0 / 3.0; 01631 factor *= 0.02; 01632 01633 // calculate face normal 01634 if(mfaces[i].v4) 01635 CalcFloat4(lX[mfaces[i].v1],lX[mfaces[i].v2],lX[mfaces[i].v3],lX[mfaces[i].v4],triunnormal); 01636 else 01637 CalcFloat(lX[mfaces[i].v1],lX[mfaces[i].v2],lX[mfaces[i].v3],triunnormal); 01638 01639 normalize_v3_v3(trinormal, triunnormal); 01640 01641 // add wind from v1 01642 VECCOPY(tmp, trinormal); 01643 mul_v3_fl(tmp, calculateVertexWindForce(winvec[mfaces[i].v1], triunnormal)); 01644 VECADDS(lF[mfaces[i].v1], lF[mfaces[i].v1], tmp, factor); 01645 01646 // add wind from v2 01647 VECCOPY(tmp, trinormal); 01648 mul_v3_fl(tmp, calculateVertexWindForce(winvec[mfaces[i].v2], triunnormal)); 01649 VECADDS(lF[mfaces[i].v2], lF[mfaces[i].v2], tmp, factor); 01650 01651 // add wind from v3 01652 VECCOPY(tmp, trinormal); 01653 mul_v3_fl(tmp, calculateVertexWindForce(winvec[mfaces[i].v3], triunnormal)); 01654 VECADDS(lF[mfaces[i].v3], lF[mfaces[i].v3], tmp, factor); 01655 01656 // add wind from v4 01657 if(mfaces[i].v4) 01658 { 01659 VECCOPY(tmp, trinormal); 01660 mul_v3_fl(tmp, calculateVertexWindForce(winvec[mfaces[i].v4], triunnormal)); 01661 VECADDS(lF[mfaces[i].v4], lF[mfaces[i].v4], tmp, factor); 01662 } 01663 } 01664 01665 /* Hair has only edges */ 01666 if(cloth->numfaces == 0) { 01667 ClothSpring *spring; 01668 float edgevec[3]={0,0,0}; //edge vector 01669 float edgeunnormal[3]={0,0,0}; // not-normalized-edge normal 01670 float tmp[3]={0,0,0}; 01671 float factor = 0.01; 01672 01673 search = cloth->springs; 01674 while(search) { 01675 spring = search->link; 01676 01677 if(spring->type == CLOTH_SPRING_TYPE_STRUCTURAL) { 01678 VECSUB(edgevec, (float*)lX[spring->ij], (float*)lX[spring->kl]); 01679 01680 project_v3_v3v3(tmp, winvec[spring->ij], edgevec); 01681 VECSUB(edgeunnormal, winvec[spring->ij], tmp); 01682 /* hair doesn't stretch too much so we can use restlen pretty safely */ 01683 VECADDS(lF[spring->ij], lF[spring->ij], edgeunnormal, spring->restlen * factor); 01684 01685 project_v3_v3v3(tmp, winvec[spring->kl], edgevec); 01686 VECSUB(edgeunnormal, winvec[spring->kl], tmp); 01687 VECADDS(lF[spring->kl], lF[spring->kl], edgeunnormal, spring->restlen * factor); 01688 } 01689 01690 search = search->next; 01691 } 01692 } 01693 01694 del_lfvector(winvec); 01695 } 01696 01697 // calculate spring forces 01698 search = cloth->springs; 01699 while(search) 01700 { 01701 // only handle active springs 01702 // if(((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED) && !(springs[i].flags & CSPRING_FLAG_DEACTIVATE))|| !(clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED)){} 01703 cloth_calc_spring_force(clmd, search->link, lF, lX, lV, dFdV, dFdX, time); 01704 01705 search = search->next; 01706 } 01707 01708 // apply spring forces 01709 search = cloth->springs; 01710 while(search) 01711 { 01712 // only handle active springs 01713 // if(((clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED) && !(springs[i].flags & CSPRING_FLAG_DEACTIVATE))|| !(clmd->sim_parms->flags & CSIMSETT_FLAG_TEARING_ENABLED)) 01714 cloth_apply_spring_force(clmd, search->link, lF, lX, lV, dFdV, dFdX); 01715 search = search->next; 01716 } 01717 // printf("\n"); 01718 } 01719 01720 static void simulate_implicit_euler(lfVector *Vnew, lfVector *UNUSED(lX), lfVector *lV, lfVector *lF, fmatrix3x3 *dFdV, fmatrix3x3 *dFdX, float dt, fmatrix3x3 *A, lfVector *B, lfVector *dV, fmatrix3x3 *S, lfVector *z, lfVector *olddV, fmatrix3x3 *UNUSED(P), fmatrix3x3 *UNUSED(Pinv), fmatrix3x3 *M, fmatrix3x3 *UNUSED(bigI)) 01721 { 01722 unsigned int numverts = dFdV[0].vcount; 01723 01724 lfVector *dFdXmV = create_lfvector(numverts); 01725 zero_lfvector(dV, numverts); 01726 01727 cp_bfmatrix(A, M); 01728 01729 subadd_bfmatrixS_bfmatrixS(A, dFdV, dt, dFdX, (dt*dt)); 01730 01731 mul_bfmatrix_lfvector(dFdXmV, dFdX, lV); 01732 01733 add_lfvectorS_lfvectorS(B, lF, dt, dFdXmV, (dt*dt), numverts); 01734 01735 itstart(); 01736 01737 cg_filtered(dV, A, B, z, S); /* conjugate gradient algorithm to solve Ax=b */ 01738 // cg_filtered_pre(dV, A, B, z, S, P, Pinv, bigI); 01739 01740 itend(); 01741 // printf("cg_filtered calc time: %f\n", (float)itval()); 01742 01743 cp_lfvector(olddV, dV, numverts); 01744 01745 // advance velocities 01746 add_lfvector_lfvector(Vnew, lV, dV, numverts); 01747 01748 01749 del_lfvector(dFdXmV); 01750 } 01751 01752 /*computes where the cloth would be if it were subject to perfectly stiff edges 01753 (edge distance constraints) in a lagrangian solver. then add forces to help 01754 guide the implicit solver to that state. this function is called after 01755 collisions*/ 01756 int cloth_calc_helper_forces(Object *UNUSED(ob), ClothModifierData * clmd, float (*initial_cos)[3], float UNUSED(step), float dt) 01757 { 01758 Cloth *cloth= clmd->clothObject; 01759 float (*cos)[3] = MEM_callocN(sizeof(float)*3*cloth->numverts, "cos cloth_calc_helper_forces"); 01760 float *masses = MEM_callocN(sizeof(float)*cloth->numverts, "cos cloth_calc_helper_forces"); 01761 LinkNode *node; 01762 ClothSpring *spring; 01763 ClothVertex *cv; 01764 int i, steps; 01765 01766 cv = cloth->verts; 01767 for (i=0; i<cloth->numverts; i++, cv++) { 01768 copy_v3_v3(cos[i], cv->tx); 01769 01770 if (cv->goal == 1.0f || len_v3v3(initial_cos[i], cv->tx) != 0.0) { 01771 masses[i] = 1e+10; 01772 } else { 01773 masses[i] = cv->mass; 01774 } 01775 } 01776 01777 steps = 55; 01778 for (i=0; i<steps; i++) { 01779 for (node=cloth->springs; node; node=node->next) { 01780 ClothVertex *cv1, *cv2; 01781 int v1, v2; 01782 float len, c, l, vec[3]; 01783 01784 spring = node->link; 01785 if (spring->type != CLOTH_SPRING_TYPE_STRUCTURAL && spring->type != CLOTH_SPRING_TYPE_SHEAR) 01786 continue; 01787 01788 v1 = spring->ij; v2 = spring->kl; 01789 cv1 = cloth->verts + v1; 01790 cv2 = cloth->verts + v2; 01791 len = len_v3v3(cos[v1], cos[v2]); 01792 01793 sub_v3_v3v3(vec, cos[v1], cos[v2]); 01794 normalize_v3(vec); 01795 01796 c = (len - spring->restlen); 01797 if (c == 0.0) 01798 continue; 01799 01800 l = c / ((1.0/masses[v1]) + (1.0/masses[v2])); 01801 01802 mul_v3_fl(vec, -(1.0/masses[v1])*l); 01803 add_v3_v3(cos[v1], vec); 01804 01805 sub_v3_v3v3(vec, cos[v2], cos[v1]); 01806 normalize_v3(vec); 01807 01808 mul_v3_fl(vec, -(1.0/masses[v2])*l); 01809 add_v3_v3(cos[v2], vec); 01810 } 01811 } 01812 01813 cv = cloth->verts; 01814 for (i=0; i<cloth->numverts; i++, cv++) { 01815 float vec[3]; 01816 01817 /*compute forces*/ 01818 sub_v3_v3v3(vec, cos[i], cv->tx); 01819 mul_v3_fl(vec, cv->mass*dt*20.0); 01820 add_v3_v3(cv->tv, vec); 01821 //copy_v3_v3(cv->tx, cos[i]); 01822 } 01823 01824 MEM_freeN(cos); 01825 MEM_freeN(masses); 01826 01827 return 1; 01828 } 01829 int implicit_solver (Object *ob, float frame, ClothModifierData *clmd, ListBase *effectors) 01830 { 01831 unsigned int i=0; 01832 float step=0.0f, tf=clmd->sim_parms->timescale; 01833 Cloth *cloth = clmd->clothObject; 01834 ClothVertex *verts = cloth->verts, *cv; 01835 unsigned int numverts = cloth->numverts; 01836 float dt = clmd->sim_parms->timescale / clmd->sim_parms->stepsPerFrame; 01837 float spf = (float)clmd->sim_parms->stepsPerFrame / clmd->sim_parms->timescale; 01838 float (*initial_cos)[3] = MEM_callocN(sizeof(float)*3*cloth->numverts, "initial_cos implicit.c"); 01839 Implicit_Data *id = cloth->implicit; 01840 int do_extra_solve; 01841 01842 if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) /* do goal stuff */ 01843 { 01844 for(i = 0; i < numverts; i++) 01845 { 01846 // update velocities with constrained velocities from pinned verts 01847 if(verts [i].flags & CLOTH_VERT_FLAG_PINNED) 01848 { 01849 VECSUB(id->V[i], verts[i].xconst, verts[i].xold); 01850 // mul_v3_fl(id->V[i], clmd->sim_parms->stepsPerFrame); 01851 } 01852 } 01853 } 01854 01855 while(step < tf) 01856 { 01857 // calculate forces 01858 cloth_calc_force(clmd, frame, id->F, id->X, id->V, id->dFdV, id->dFdX, effectors, step, id->M); 01859 01860 // calculate new velocity 01861 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, id->M, id->bigI); 01862 01863 // advance positions 01864 add_lfvector_lfvectorS(id->Xnew, id->X, id->Vnew, dt, numverts); 01865 01866 /* move pinned verts to correct position */ 01867 for(i = 0; i < numverts; i++) 01868 { 01869 if(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) 01870 { 01871 if(verts [i].flags & CLOTH_VERT_FLAG_PINNED) 01872 { 01873 float tvect[3] = {.0,.0,.0}; 01874 VECSUB(tvect, verts[i].xconst, verts[i].xold); 01875 mul_fvector_S(tvect, tvect, step+dt); 01876 VECADD(tvect, tvect, verts[i].xold); 01877 VECCOPY(id->Xnew[i], tvect); 01878 } 01879 } 01880 01881 VECCOPY(verts[i].txold, id->X[i]); 01882 } 01883 01884 if(clmd->coll_parms->flags & CLOTH_COLLSETTINGS_FLAG_ENABLED && clmd->clothObject->bvhtree) 01885 { 01886 // collisions 01887 // itstart(); 01888 01889 // update verts to current positions 01890 for(i = 0; i < numverts; i++) 01891 { 01892 VECCOPY(verts[i].tx, id->Xnew[i]); 01893 01894 VECSUB(verts[i].tv, verts[i].tx, verts[i].txold); 01895 VECCOPY(verts[i].v, verts[i].tv); 01896 } 01897 01898 for (i=0, cv=cloth->verts; i<cloth->numverts; i++, cv++) { 01899 copy_v3_v3(initial_cos[i], cv->tx); 01900 } 01901 01902 // call collision function 01903 // TODO: check if "step" or "step+dt" is correct - dg 01904 do_extra_solve = cloth_bvh_objcollision(ob, clmd, step/clmd->sim_parms->timescale, dt/clmd->sim_parms->timescale); 01905 01906 // copy corrected positions back to simulation 01907 for(i = 0; i < numverts; i++) 01908 { 01909 // correct velocity again, just to be sure we had to change it due to adaptive collisions 01910 VECSUB(verts[i].tv, verts[i].tx, id->X[i]); 01911 } 01912 01913 //if (do_extra_solve) 01914 // cloth_calc_helper_forces(ob, clmd, initial_cos, step/clmd->sim_parms->timescale, dt/clmd->sim_parms->timescale); 01915 01916 for(i = 0; i < numverts; i++) 01917 { 01918 01919 if(do_extra_solve) 01920 { 01921 01922 if((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) && (verts [i].flags & CLOTH_VERT_FLAG_PINNED)) 01923 continue; 01924 01925 VECCOPY(id->Xnew[i], verts[i].tx); 01926 VECCOPY(id->Vnew[i], verts[i].tv); 01927 mul_v3_fl(id->Vnew[i], spf); 01928 } 01929 } 01930 01931 // X = Xnew; 01932 cp_lfvector(id->X, id->Xnew, numverts); 01933 01934 // if there were collisions, advance the velocity from v_n+1/2 to v_n+1 01935 01936 if(do_extra_solve) 01937 { 01938 // V = Vnew; 01939 cp_lfvector(id->V, id->Vnew, numverts); 01940 01941 // calculate 01942 cloth_calc_force(clmd, frame, id->F, id->X, id->V, id->dFdV, id->dFdX, effectors, step+dt, id->M); 01943 01944 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, id->M, id->bigI); 01945 } 01946 } 01947 else 01948 { 01949 // X = Xnew; 01950 cp_lfvector(id->X, id->Xnew, numverts); 01951 } 01952 01953 // itend(); 01954 // printf("collision time: %f\n", (float)itval()); 01955 01956 // V = Vnew; 01957 cp_lfvector(id->V, id->Vnew, numverts); 01958 01959 step += dt; 01960 } 01961 01962 for(i = 0; i < numverts; i++) 01963 { 01964 if((clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL) && (verts [i].flags & CLOTH_VERT_FLAG_PINNED)) 01965 { 01966 VECCOPY(verts[i].txold, verts[i].xconst); // TODO: test --> should be .x 01967 VECCOPY(verts[i].x, verts[i].xconst); 01968 VECCOPY(verts[i].v, id->V[i]); 01969 } 01970 else 01971 { 01972 VECCOPY(verts[i].txold, id->X[i]); 01973 VECCOPY(verts[i].x, id->X[i]); 01974 VECCOPY(verts[i].v, id->V[i]); 01975 } 01976 } 01977 01978 MEM_freeN(initial_cos); 01979 01980 return 1; 01981 } 01982 01983 void implicit_set_positions (ClothModifierData *clmd) 01984 { 01985 Cloth *cloth = clmd->clothObject; 01986 ClothVertex *verts = cloth->verts; 01987 unsigned int numverts = cloth->numverts, i; 01988 Implicit_Data *id = cloth->implicit; 01989 01990 for(i = 0; i < numverts; i++) 01991 { 01992 VECCOPY(id->X[i], verts[i].x); 01993 VECCOPY(id->V[i], verts[i].v); 01994 } 01995 if(G.rt > 0) 01996 printf("implicit_set_positions\n"); 01997 } 01998