~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/cloth.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  cloth.c
2
 
*
3
 
*
4
 
* ***** BEGIN GPL LICENSE BLOCK *****
5
 
*
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.
10
 
*
11
 
* This program is distributed in the hope that it will be useful,
12
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
* GNU General Public License for more details.
15
 
*
16
 
* You should have received a copy of the GNU General Public License
17
 
* along with this program; if not, write to the Free Software Foundation,
18
 
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 
*
20
 
* The Original Code is Copyright (C) Blender Foundation
21
 
* All rights reserved.
22
 
*
23
 
* Contributor(s): Daniel Genrich
24
 
*
25
 
* ***** END GPL LICENSE BLOCK *****
26
 
*/
 
1
/*
 
2
 * ***** BEGIN GPL LICENSE BLOCK *****
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software Foundation,
 
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
17
 *
 
18
 * The Original Code is Copyright (C) Blender Foundation
 
19
 * All rights reserved.
 
20
 *
 
21
 * Contributor(s): Daniel Genrich
 
22
 *
 
23
 * ***** END GPL LICENSE BLOCK *****
 
24
 */
 
25
 
 
26
/** \file blender/blenkernel/intern/cloth.c
 
27
 *  \ingroup bke
 
28
 */
 
29
 
27
30
 
28
31
#include "MEM_guardedalloc.h"
29
32
 
 
33
#include "DNA_cloth_types.h"
 
34
#include "DNA_scene_types.h"
 
35
#include "DNA_object_types.h"
 
36
#include "DNA_meshdata_types.h"
 
37
 
 
38
#include "BLI_utildefines.h"
 
39
#include "BLI_math.h"
 
40
#include "BLI_edgehash.h"
 
41
#include "BLI_utildefines.h"
 
42
#include "BLI_linklist.h"
 
43
 
 
44
#include "BLF_translation.h"
 
45
 
30
46
#include "BKE_cdderivedmesh.h"
31
47
#include "BKE_cloth.h"
32
48
#include "BKE_effect.h"
33
49
#include "BKE_global.h"
34
50
#include "BKE_modifier.h"
35
51
#include "BKE_pointcache.h"
36
 
#include "BKE_utildefines.h"
37
52
 
38
53
#ifdef _WIN32
39
54
void tstart ( void )
41
56
void tend ( void )
42
57
{
43
58
}
44
 
double tval()
 
59
double tval( void )
45
60
{
46
61
        return 0;
47
62
}
48
63
#else
49
64
#include <sys/time.h>
50
 
                         static struct timeval _tstart, _tend;
51
 
         static struct timezone tz;
52
 
         void tstart ( void )
 
65
static struct timeval _tstart, _tend;
 
66
static struct timezone tz;
 
67
void tstart ( void )
53
68
{
54
69
        gettimeofday ( &_tstart, &tz );
55
70
}
57
72
{
58
73
        gettimeofday ( &_tend,&tz );
59
74
}
60
 
double tval()
 
75
double tval(void)
61
76
{
62
77
        double t1, t2;
63
78
        t1 = ( double ) _tstart.tv_sec + ( double ) _tstart.tv_usec/ ( 1000*1000 );
78
93
/* ********** cloth engine ******* */
79
94
/* Prototypes for internal functions.
80
95
*/
81
 
static void cloth_to_object (Object *ob,  ClothModifierData *clmd, DerivedMesh *dm);
82
 
static void cloth_from_mesh ( Object *ob, ClothModifierData *clmd, DerivedMesh *dm );
 
96
static void cloth_to_object (Object *ob,  ClothModifierData *clmd, float (*vertexCos)[3]);
 
97
static void cloth_from_mesh ( ClothModifierData *clmd, DerivedMesh *dm );
83
98
static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float framenr, int first);
84
99
static int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm );
85
100
static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm );
99
114
void cloth_init ( ClothModifierData *clmd )
100
115
{       
101
116
        /* Initialize our new data structure to reasonable values. */
102
 
        clmd->sim_parms->gravity [0] = 0.0;
103
 
        clmd->sim_parms->gravity [1] = 0.0;
104
 
        clmd->sim_parms->gravity [2] = -9.81;
 
117
        clmd->sim_parms->gravity[0] = 0.0;
 
118
        clmd->sim_parms->gravity[1] = 0.0;
 
119
        clmd->sim_parms->gravity[2] = -9.81;
105
120
        clmd->sim_parms->structural = 15.0;
106
121
        clmd->sim_parms->shear = 15.0;
107
122
        clmd->sim_parms->bending = 0.5;
118
133
        clmd->sim_parms->presets = 2; /* cotton as start setting */
119
134
        clmd->sim_parms->timescale = 1.0f; /* speed factor, describes how fast cloth moves */
120
135
        clmd->sim_parms->reset = 0;
 
136
        clmd->sim_parms->vel_damping = 1.0f; /* 1.0 = no damping, 0.0 = fully dampened */
121
137
        
122
138
        clmd->coll_parms->self_friction = 5.0;
123
139
        clmd->coll_parms->friction = 5.0;
142
158
        clmd->sim_parms->goalfrict = 0.0f;
143
159
        clmd->sim_parms->velocity_smooth = 0.0f;
144
160
 
145
 
        if(!clmd->sim_parms->effector_weights)
 
161
        if (!clmd->sim_parms->effector_weights)
146
162
                clmd->sim_parms->effector_weights = BKE_add_effector_weights(NULL);
147
163
 
148
 
        if(clmd->point_cache)
 
164
        if (clmd->point_cache)
149
165
                clmd->point_cache->step = 1;
150
166
}
151
167
 
155
171
        BVHTree *bvhtree;
156
172
        Cloth *cloth;
157
173
        ClothVertex *verts;
158
 
        MFace *mfaces;
159
174
        float co[12];
160
175
 
161
 
        if(!clmd)
 
176
        if (!clmd)
162
177
                return NULL;
163
178
 
164
179
        cloth = clmd->clothObject;
165
180
 
166
 
        if(!cloth)
 
181
        if (!cloth)
167
182
                return NULL;
168
183
        
169
184
        verts = cloth->verts;
170
 
        mfaces = cloth->mfaces;
171
185
        
172
186
        // in the moment, return zero if no faces there
173
 
        if(!cloth->numverts)
 
187
        if (!cloth->numverts)
174
188
                return NULL;
175
189
        
176
190
        // create quadtree with k=26
177
191
        bvhtree = BLI_bvhtree_new(cloth->numverts, epsilon, 4, 6);
178
192
        
179
193
        // fill tree
180
 
        for(i = 0; i < cloth->numverts; i++, verts++)
 
194
        for (i = 0; i < cloth->numverts; i++, verts++)
181
195
        {
182
 
                VECCOPY(&co[0*3], verts->xold);
 
196
                copy_v3_v3(&co[0*3], verts->xold);
183
197
                
184
198
                BLI_bvhtree_insert(bvhtree, i, co, 1);
185
199
        }
199
213
        MFace *mfaces;
200
214
        float co[12];
201
215
 
202
 
        if(!clmd)
 
216
        if (!clmd)
203
217
                return NULL;
204
218
 
205
219
        cloth = clmd->clothObject;
206
220
 
207
 
        if(!cloth)
 
221
        if (!cloth)
208
222
                return NULL;
209
223
        
210
224
        verts = cloth->verts;
211
225
        mfaces = cloth->mfaces;
212
226
        
213
227
        // in the moment, return zero if no faces there
214
 
        if(!cloth->numfaces)
 
228
        if (!cloth->numfaces)
215
229
                return NULL;
216
230
        
217
231
        // create quadtree with k=26
218
232
        bvhtree = BLI_bvhtree_new(cloth->numfaces, epsilon, 4, 26);
219
233
        
220
234
        // fill tree
221
 
        for(i = 0; i < cloth->numfaces; i++, mfaces++)
 
235
        for (i = 0; i < cloth->numfaces; i++, mfaces++)
222
236
        {
223
 
                VECCOPY(&co[0*3], verts[mfaces->v1].xold);
224
 
                VECCOPY(&co[1*3], verts[mfaces->v2].xold);
225
 
                VECCOPY(&co[2*3], verts[mfaces->v3].xold);
 
237
                copy_v3_v3(&co[0*3], verts[mfaces->v1].xold);
 
238
                copy_v3_v3(&co[1*3], verts[mfaces->v2].xold);
 
239
                copy_v3_v3(&co[2*3], verts[mfaces->v3].xold);
226
240
                
227
 
                if(mfaces->v4)
228
 
                        VECCOPY(&co[3*3], verts[mfaces->v4].xold);
 
241
                if (mfaces->v4)
 
242
                        copy_v3_v3(&co[3*3], verts[mfaces->v4].xold);
229
243
                
230
244
                BLI_bvhtree_insert(bvhtree, i, co, (mfaces->v4 ? 4 : 3));
231
245
        }
246
260
        float co[12], co_moving[12];
247
261
        int ret = 0;
248
262
        
249
 
        if(!bvhtree)
 
263
        if (!bvhtree)
250
264
                return;
251
265
        
252
266
        mfaces = cloth->mfaces;
253
267
        
254
268
        // update vertex position in bvh tree
255
 
        if(verts && mfaces)
 
269
        if (verts && mfaces)
256
270
        {
257
 
                for(i = 0; i < cloth->numfaces; i++, mfaces++)
 
271
                for (i = 0; i < cloth->numfaces; i++, mfaces++)
258
272
                {
259
 
                        VECCOPY(&co[0*3], verts[mfaces->v1].txold);
260
 
                        VECCOPY(&co[1*3], verts[mfaces->v2].txold);
261
 
                        VECCOPY(&co[2*3], verts[mfaces->v3].txold);
 
273
                        copy_v3_v3(&co[0*3], verts[mfaces->v1].txold);
 
274
                        copy_v3_v3(&co[1*3], verts[mfaces->v2].txold);
 
275
                        copy_v3_v3(&co[2*3], verts[mfaces->v3].txold);
262
276
                        
263
 
                        if(mfaces->v4)
264
 
                                VECCOPY(&co[3*3], verts[mfaces->v4].txold);
 
277
                        if (mfaces->v4)
 
278
                                copy_v3_v3(&co[3*3], verts[mfaces->v4].txold);
265
279
                
266
280
                        // copy new locations into array
267
 
                        if(moving)
 
281
                        if (moving)
268
282
                        {
269
283
                                // update moving positions
270
 
                                VECCOPY(&co_moving[0*3], verts[mfaces->v1].tx);
271
 
                                VECCOPY(&co_moving[1*3], verts[mfaces->v2].tx);
272
 
                                VECCOPY(&co_moving[2*3], verts[mfaces->v3].tx);
 
284
                                copy_v3_v3(&co_moving[0*3], verts[mfaces->v1].tx);
 
285
                                copy_v3_v3(&co_moving[1*3], verts[mfaces->v2].tx);
 
286
                                copy_v3_v3(&co_moving[2*3], verts[mfaces->v3].tx);
273
287
                                
274
 
                                if(mfaces->v4)
275
 
                                        VECCOPY(&co_moving[3*3], verts[mfaces->v4].tx);
 
288
                                if (mfaces->v4)
 
289
                                        copy_v3_v3(&co_moving[3*3], verts[mfaces->v4].tx);
276
290
                                
277
291
                                ret = BLI_bvhtree_update_node(bvhtree, i, co, co_moving, (mfaces->v4 ? 4 : 3));
278
292
                        }
279
 
                        else
280
 
                        {
 
293
                        else {
281
294
                                ret = BLI_bvhtree_update_node(bvhtree, i, co, NULL, (mfaces->v4 ? 4 : 3));
282
295
                        }
283
296
                        
284
297
                        // check if tree is already full
285
 
                        if(!ret)
 
298
                        if (!ret)
286
299
                                break;
287
300
                }
288
301
                
300
313
        float co[12], co_moving[12];
301
314
        int ret = 0;
302
315
        
303
 
        if(!bvhtree)
 
316
        if (!bvhtree)
304
317
                return;
305
318
        
306
319
        mfaces = cloth->mfaces;
307
320
        
308
321
        // update vertex position in bvh tree
309
 
        if(verts && mfaces)
 
322
        if (verts && mfaces)
310
323
        {
311
 
                for(i = 0; i < cloth->numverts; i++, verts++)
 
324
                for (i = 0; i < cloth->numverts; i++, verts++)
312
325
                {
313
 
                        VECCOPY(&co[0*3], verts->txold);
 
326
                        copy_v3_v3(&co[0*3], verts->txold);
314
327
                        
315
328
                        // copy new locations into array
316
 
                        if(moving)
 
329
                        if (moving)
317
330
                        {
318
331
                                // update moving positions
319
 
                                VECCOPY(&co_moving[0*3], verts->tx);
 
332
                                copy_v3_v3(&co_moving[0*3], verts->tx);
320
333
                                
321
334
                                ret = BLI_bvhtree_update_node(bvhtree, i, co, co_moving, 1);
322
335
                        }
323
 
                        else
324
 
                        {
 
336
                        else {
325
337
                                ret = BLI_bvhtree_update_node(bvhtree, i, co, NULL, 1);
326
338
                        }
327
339
                        
328
340
                        // check if tree is already full
329
 
                        if(!ret)
 
341
                        if (!ret)
330
342
                                break;
331
343
                }
332
344
                
341
353
        BKE_ptcache_id_from_cloth(&pid, ob, clmd);
342
354
 
343
355
        // don't do anything as long as we're in editmode!
344
 
        if(pid.cache->edit && ob->mode & OB_MODE_PARTICLE_EDIT)
 
356
        if (pid.cache->edit && ob->mode & OB_MODE_PARTICLE_EDIT)
345
357
                return;
346
358
        
347
359
        BKE_ptcache_id_clear(&pid, PTCACHE_CLEAR_AFTER, framenr);
354
366
        cache= clmd->point_cache;
355
367
 
356
368
        /* initialize simulation data if it didn't exist already */
357
 
        if(clmd->clothObject == NULL) { 
358
 
                if(!cloth_from_object(ob, clmd, result, framenr, 1)) {
 
369
        if (clmd->clothObject == NULL) {        
 
370
                if (!cloth_from_object(ob, clmd, result, framenr, 1)) {
359
371
                        BKE_ptcache_invalidate(cache);
360
372
                        return 0;
361
373
                }
362
374
        
363
 
                if(clmd->clothObject == NULL) {
 
375
                if (clmd->clothObject == NULL) {
364
376
                        BKE_ptcache_invalidate(cache);
365
377
                        return 0;
366
378
                }
367
379
        
368
380
                implicit_set_positions(clmd);
 
381
 
 
382
                clmd->clothObject->last_frame= MINFRAME-1;
369
383
        }
370
384
 
371
385
        return 1;
377
391
        Cloth *cloth;
378
392
        ListBase *effectors = NULL;
379
393
        MVert *mvert;
380
 
        int i, ret = 0;
 
394
        unsigned int i = 0;
 
395
        int ret = 0;
381
396
 
382
397
        /* simulate 1 frame forward */
383
398
        cloth = clmd->clothObject;
385
400
        mvert = result->getVertArray(result);
386
401
 
387
402
        /* force any pinned verts to their constrained location. */
388
 
        for(i = 0; i < clmd->clothObject->numverts; i++, verts++) {
 
403
        for (i = 0; i < clmd->clothObject->numverts; i++, verts++) {
389
404
                /* save the previous position. */
390
 
                VECCOPY(verts->xold, verts->xconst);
391
 
                VECCOPY(verts->txold, verts->x);
 
405
                copy_v3_v3(verts->xold, verts->xconst);
 
406
                copy_v3_v3(verts->txold, verts->x);
392
407
 
393
408
                /* Get the current position. */
394
 
                VECCOPY(verts->xconst, mvert[i].co);
 
409
                copy_v3_v3(verts->xconst, mvert[i].co);
395
410
                mul_m4_v3(ob->obmat, verts->xconst);
396
411
        }
397
412
 
400
415
        tstart();
401
416
 
402
417
        /* call the solver. */
403
 
        if(solvers [clmd->sim_parms->solver_type].solver)
 
418
        if (solvers [clmd->sim_parms->solver_type].solver)
404
419
                ret = solvers[clmd->sim_parms->solver_type].solver(ob, framenr, clmd, effectors);
405
420
 
406
421
        tend();
415
430
/************************************************
416
431
 * clothModifier_do - main simulation function
417
432
************************************************/
418
 
DerivedMesh *clothModifier_do(ClothModifierData *clmd, Scene *scene, Object *ob, DerivedMesh *dm, int useRenderParams, int isFinalCalc)
 
433
void clothModifier_do(ClothModifierData *clmd, Scene *scene, Object *ob, DerivedMesh *dm, float (*vertexCos)[3])
419
434
{
420
 
        DerivedMesh *result;
421
435
        PointCache *cache;
422
436
        PTCacheID pid;
423
437
        float timescale;
424
 
        int framedelta, framenr, startframe, endframe;
 
438
        int framenr, startframe, endframe;
425
439
        int cache_result;
426
440
 
427
441
        clmd->scene= scene;     /* nice to pass on later :) */
428
442
        framenr= (int)scene->r.cfra;
429
443
        cache= clmd->point_cache;
430
 
        result = CDDM_copy(dm);
431
444
 
432
445
        BKE_ptcache_id_from_cloth(&pid, ob, clmd);
433
446
        BKE_ptcache_id_time(&pid, scene, framenr, &startframe, &endframe, &timescale);
434
447
        clmd->sim_parms->timescale= timescale;
435
448
 
436
 
        if(!result) {
437
 
                BKE_ptcache_invalidate(cache);
438
 
                return dm;
439
 
        }
440
 
 
441
 
        if(clmd->sim_parms->reset || (framenr == (startframe - clmd->sim_parms->preroll)))
 
449
        if (clmd->sim_parms->reset ||
 
450
            (framenr == (startframe - clmd->sim_parms->preroll) && clmd->sim_parms->preroll != 0) ||
 
451
            (clmd->clothObject && dm->getNumVerts(dm) != clmd->clothObject->numverts))
442
452
        {
443
453
                clmd->sim_parms->reset = 0;
444
454
                cache->flag |= PTCACHE_OUTDATED;
446
456
                BKE_ptcache_validate(cache, 0);
447
457
                cache->last_exact= 0;
448
458
                cache->flag &= ~PTCACHE_REDO_NEEDED;
449
 
                return result;
450
 
        }
451
 
        
452
 
        /* verify we still have the same number of vertices, if not do nothing.
453
 
         * note that this should only happen if the number of vertices changes
454
 
         * during an animation due to a preceding modifier, this should not
455
 
         * happen because of object changes! */
456
 
        if(clmd->clothObject) {
457
 
                if(result->getNumVerts(result) != clmd->clothObject->numverts) {
458
 
                        BKE_ptcache_invalidate(cache);
459
 
                        return result;
460
 
                }
 
459
                return;
461
460
        }
462
461
        
463
462
        // unused in the moment, calculated separately in implicit.c
464
463
        clmd->sim_parms->dt = clmd->sim_parms->timescale / clmd->sim_parms->stepsPerFrame;
465
464
 
466
465
        /* handle continuous simulation with the play button */
467
 
        if(BKE_ptcache_get_continue_physics() || ((clmd->sim_parms->preroll > 0) && (framenr > startframe - clmd->sim_parms->preroll) && (framenr < startframe))) {
 
466
        if (BKE_ptcache_get_continue_physics() || ((clmd->sim_parms->preroll > 0) && (framenr > startframe - clmd->sim_parms->preroll) && (framenr < startframe))) {
468
467
                BKE_ptcache_invalidate(cache);
469
468
 
470
469
                /* do simulation */
471
 
                if(!do_init_cloth(ob, clmd, result, framenr))
472
 
                        return result;
473
 
 
474
 
                do_step_cloth(ob, clmd, result, framenr);
475
 
                cloth_to_object(ob, clmd, result);
476
 
 
477
 
                return result;
 
470
                if (!do_init_cloth(ob, clmd, dm, framenr))
 
471
                        return;
 
472
 
 
473
                do_step_cloth(ob, clmd, dm, framenr);
 
474
                cloth_to_object(ob, clmd, vertexCos);
 
475
 
 
476
                clmd->clothObject->last_frame= framenr;
 
477
 
 
478
                return;
478
479
        }
479
480
 
480
481
        /* simulation is only active during a specific period */
481
 
        if(framenr < startframe) {
 
482
        if (framenr < startframe) {
482
483
                BKE_ptcache_invalidate(cache);
483
 
                return result;
 
484
                return;
484
485
        }
485
 
        else if(framenr > endframe) {
 
486
        else if (framenr > endframe) {
486
487
                framenr= endframe;
487
488
        }
488
489
 
489
 
        if(cache->flag & PTCACHE_SIMULATION_VALID)
490
 
                framedelta= framenr - cache->simframe;
491
 
        else
492
 
                framedelta= -1;
493
 
 
494
490
        /* initialize simulation data if it didn't exist already */
495
 
        if(!do_init_cloth(ob, clmd, result, framenr))
496
 
                return result;
 
491
        if (!do_init_cloth(ob, clmd, dm, framenr))
 
492
                return;
497
493
 
498
 
        if((framenr == startframe) && (clmd->sim_parms->preroll == 0)) {
 
494
        if ((framenr == startframe) && (clmd->sim_parms->preroll == 0)) {
499
495
                BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
500
 
                do_init_cloth(ob, clmd, result, framenr);
 
496
                do_init_cloth(ob, clmd, dm, framenr);
501
497
                BKE_ptcache_validate(cache, framenr);
502
498
                cache->flag &= ~PTCACHE_REDO_NEEDED;
503
 
                return result;
 
499
                clmd->clothObject->last_frame= framenr;
 
500
                return;
504
501
        }
505
502
 
506
503
        /* try to read from cache */
507
 
        cache_result = BKE_ptcache_read_cache(&pid, (float)framenr, scene->r.frs_sec);
 
504
        cache_result = BKE_ptcache_read(&pid, (float)framenr+scene->r.subframe);
508
505
 
509
 
        if(cache_result == PTCACHE_READ_EXACT || cache_result == PTCACHE_READ_INTERPOLATED) {
 
506
        if (cache_result == PTCACHE_READ_EXACT || cache_result == PTCACHE_READ_INTERPOLATED) {
510
507
                implicit_set_positions(clmd);
511
 
                cloth_to_object (ob, clmd, result);
 
508
                cloth_to_object (ob, clmd, vertexCos);
512
509
 
513
510
                BKE_ptcache_validate(cache, framenr);
514
511
 
515
 
                if(cache_result == PTCACHE_READ_INTERPOLATED && cache->flag & PTCACHE_REDO_NEEDED)
516
 
                        BKE_ptcache_write_cache(&pid, framenr);
517
 
 
518
 
                return result;
 
512
                if (cache_result == PTCACHE_READ_INTERPOLATED && cache->flag & PTCACHE_REDO_NEEDED)
 
513
                        BKE_ptcache_write(&pid, framenr);
 
514
 
 
515
                clmd->clothObject->last_frame= framenr;
 
516
 
 
517
                return;
519
518
        }
520
 
        else if(cache_result==PTCACHE_READ_OLD) {
 
519
        else if (cache_result==PTCACHE_READ_OLD) {
521
520
                implicit_set_positions(clmd);
522
521
        }
523
 
        else if( /*ob->id.lib ||*/ (cache->flag & PTCACHE_BAKED)) { /* 2.4x disabled lib, but this can be used in some cases, testing further - campbell */
 
522
        else if ( /*ob->id.lib ||*/ (cache->flag & PTCACHE_BAKED)) { /* 2.4x disabled lib, but this can be used in some cases, testing further - campbell */
524
523
                /* if baked and nothing in cache, do nothing */
525
524
                BKE_ptcache_invalidate(cache);
526
 
                return result;
 
525
                return;
527
526
        }
528
527
 
 
528
        if (framenr!=clmd->clothObject->last_frame+1)
 
529
                return;
 
530
 
529
531
        /* if on second frame, write cache for first frame */
530
 
        if(cache->simframe == startframe && (cache->flag & PTCACHE_OUTDATED || cache->last_exact==0))
531
 
                BKE_ptcache_write_cache(&pid, startframe);
 
532
        if (cache->simframe == startframe && (cache->flag & PTCACHE_OUTDATED || cache->last_exact==0))
 
533
                BKE_ptcache_write(&pid, startframe);
532
534
 
533
535
        clmd->sim_parms->timescale *= framenr - cache->simframe;
534
536
 
535
537
        /* do simulation */
536
538
        BKE_ptcache_validate(cache, framenr);
537
539
 
538
 
        if(!do_step_cloth(ob, clmd, result, framenr)) {
 
540
        if (!do_step_cloth(ob, clmd, dm, framenr)) {
539
541
                BKE_ptcache_invalidate(cache);
540
542
        }
541
543
        else
542
 
                BKE_ptcache_write_cache(&pid, framenr);
543
 
 
544
 
        cloth_to_object (ob, clmd, result);
545
 
 
546
 
        return result;
 
544
                BKE_ptcache_write(&pid, framenr);
 
545
 
 
546
        cloth_to_object (ob, clmd, vertexCos);
 
547
        clmd->clothObject->last_frame= framenr;
547
548
}
548
549
 
549
550
/* frees all */
550
 
void cloth_free_modifier ( Object *ob, ClothModifierData *clmd )
 
551
void cloth_free_modifier(ClothModifierData *clmd )
551
552
{
552
553
        Cloth   *cloth = NULL;
553
554
        
576
577
                if ( cloth->springs != NULL )
577
578
                {
578
579
                        LinkNode *search = cloth->springs;
579
 
                        while(search)
 
580
                        while (search)
580
581
                        {
581
582
                                ClothSpring *spring = search->link;
582
583
                                                
602
603
                if ( cloth->mfaces )
603
604
                        MEM_freeN ( cloth->mfaces );
604
605
                
605
 
                if(cloth->edgehash)
 
606
                if (cloth->edgehash)
606
607
                        BLI_edgehash_free ( cloth->edgehash, NULL );
607
608
                
608
609
                
609
610
                /*
610
 
                if(clmd->clothObject->facemarks)
 
611
                if (clmd->clothObject->facemarks)
611
612
                MEM_freeN(clmd->clothObject->facemarks);
612
613
                */
613
614
                MEM_freeN ( cloth );
619
620
void cloth_free_modifier_extern ( ClothModifierData *clmd )
620
621
{
621
622
        Cloth   *cloth = NULL;
622
 
        if(G.rt > 0)
 
623
        if (G.rt > 0)
623
624
                printf("cloth_free_modifier_extern\n");
624
625
        
625
626
        if ( !clmd )
629
630
        
630
631
        if ( cloth )
631
632
        {       
632
 
                if(G.rt > 0)
 
633
                if (G.rt > 0)
633
634
                        printf("cloth_free_modifier_extern in\n");
634
635
                
635
636
                // If our solver provides a free function, call it
649
650
                if ( cloth->springs != NULL )
650
651
                {
651
652
                        LinkNode *search = cloth->springs;
652
 
                        while(search)
 
653
                        while (search)
653
654
                        {
654
655
                                ClothSpring *spring = search->link;
655
656
                                                
675
676
                if ( cloth->mfaces )
676
677
                        MEM_freeN ( cloth->mfaces );
677
678
                
678
 
                if(cloth->edgehash)
 
679
                if (cloth->edgehash)
679
680
                        BLI_edgehash_free ( cloth->edgehash, NULL );
680
681
                
681
682
                
682
683
                /*
683
 
                if(clmd->clothObject->facemarks)
 
684
                if (clmd->clothObject->facemarks)
684
685
                MEM_freeN(clmd->clothObject->facemarks);
685
686
                */
686
687
                MEM_freeN ( cloth );
698
699
 * cloth_to_object - copies the deformed vertices to the object.
699
700
 *
700
701
 **/
701
 
static void cloth_to_object (Object *ob,  ClothModifierData *clmd, DerivedMesh *dm)
 
702
static void cloth_to_object (Object *ob,  ClothModifierData *clmd, float (*vertexCos)[3])
702
703
{
703
704
        unsigned int    i = 0;
704
 
        MVert *mvert = NULL;
705
 
        unsigned int numverts;
706
705
        Cloth *cloth = clmd->clothObject;
707
706
 
708
707
        if (clmd->clothObject) {
709
708
                /* inverse matrix is not uptodate... */
710
709
                invert_m4_m4(ob->imat, ob->obmat);
711
710
 
712
 
                mvert = CDDM_get_verts(dm);
713
 
                numverts = dm->getNumVerts(dm);
714
 
 
715
 
                for (i = 0; i < numverts; i++)
 
711
                for (i = 0; i < cloth->numverts; i++)
716
712
                {
717
 
                        VECCOPY (mvert[i].co, cloth->verts[i].x);
718
 
                        mul_m4_v3(ob->imat, mvert[i].co);       /* cloth is in global coords */
 
713
                        copy_v3_v3 (vertexCos[i], cloth->verts[i].x);
 
714
                        mul_m4_v3(ob->imat, vertexCos[i]);      /* cloth is in global coords */
719
715
                }
720
716
        }
721
717
}
742
738
        MDeformVert *dvert = NULL;
743
739
        Cloth *clothObj = NULL;
744
740
        int numverts;
745
 
        float goalfac = 0;
 
741
        /* float goalfac = 0; */ /* UNUSED */
746
742
        ClothVertex *verts = NULL;
747
743
 
748
744
        if (!clmd || !dm) return;
765
761
                                        if (( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_mass-1)) && (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL ))
766
762
                                        {
767
763
                                                verts->goal = dvert->dw [j].weight;
768
 
                                                goalfac= 1.0f;
 
764
                                                /* goalfac= 1.0f; */ /* UNUSED */
769
765
                                                
770
766
                                                /*
771
767
                                                // Kicking goal factor to simplify things...who uses that anyway?
781
777
                                        
782
778
                                        if (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_SCALING )
783
779
                                        {
784
 
                                                if( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_struct-1))
 
780
                                                if ( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_struct-1))
785
781
                                                {
786
782
                                                        verts->struct_stiff = dvert->dw [j].weight;
787
783
                                                        verts->shear_stiff = dvert->dw [j].weight;
788
784
                                                }
789
785
                                                
790
 
                                                if( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_bend-1))
 
786
                                                if ( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_bend-1))
791
787
                                                {
792
788
                                                        verts->bend_stiff = dvert->dw [j].weight;
793
789
                                                }
794
790
                                        }
795
791
                                        /*
796
792
                                        // for later
797
 
                                        if( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_weight-1))
 
793
                                        if ( dvert->dw[j].def_nr == (clmd->sim_parms->vgroup_weight-1))
798
794
                                        {
799
795
                                                verts->mass = dvert->dw [j].weight;
800
796
                                        }
805
801
        }
806
802
}
807
803
 
808
 
static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float framenr, int first)
 
804
static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float UNUSED(framenr), int first)
809
805
{
810
806
        int i = 0;
811
807
        MVert *mvert = NULL;
818
814
        // If we have a clothObject, free it. 
819
815
        if ( clmd->clothObject != NULL )
820
816
        {
821
 
                cloth_free_modifier ( ob, clmd );
822
 
                if(G.rt > 0)
 
817
                cloth_free_modifier ( clmd );
 
818
                if (G.rt > 0)
823
819
                        printf("cloth_free_modifier cloth_from_object\n");
824
820
        }
825
821
 
832
828
                cloth = clmd->clothObject;
833
829
                clmd->clothObject->edgehash = NULL;
834
830
        }
835
 
        else if ( !clmd->clothObject )
836
 
        {
837
 
                modifier_setError ( & ( clmd->modifier ), "Out of memory on allocating clmd->clothObject." );
 
831
        else if (!clmd->clothObject) {
 
832
                modifier_setError(&(clmd->modifier), "%s", TIP_("Out of memory on allocating clmd->clothObject."));
838
833
                return 0;
839
834
        }
840
835
 
842
837
        if ( !dm )
843
838
                return 0;
844
839
 
845
 
        cloth_from_mesh ( ob, clmd, dm );
 
840
        cloth_from_mesh ( clmd, dm );
846
841
 
847
842
        // create springs 
848
843
        clmd->clothObject->springs = NULL;
849
844
        clmd->clothObject->numsprings = -1;
850
845
        
851
 
        if( clmd->sim_parms->shapekey_rest )
 
846
        if ( clmd->sim_parms->shapekey_rest )
852
847
                shapekey_rest = dm->getVertDataArray ( dm, CD_CLOTH_ORCO );
853
848
 
854
849
        mvert = dm->getVertArray ( dm );
858
853
        // set initial values
859
854
        for ( i = 0; i < dm->getNumVerts(dm); i++, verts++ )
860
855
        {
861
 
                if(first)
 
856
                if (first)
862
857
                {
863
858
                        copy_v3_v3( verts->x, mvert[i].co );
864
859
 
865
860
                        mul_m4_v3( ob->obmat, verts->x );
866
861
 
867
 
                        if( shapekey_rest ) {
 
862
                        if ( shapekey_rest ) {
868
863
                                verts->xrest= shapekey_rest[i];
869
864
                                mul_m4_v3( ob->obmat, verts->xrest );
870
865
                        }
882
877
                        verts->goal= 0.0f;
883
878
 
884
879
                verts->flags = 0;
885
 
                VECCOPY ( verts->xold, verts->x );
886
 
                VECCOPY ( verts->xconst, verts->x );
887
 
                VECCOPY ( verts->txold, verts->x );
888
 
                VECCOPY ( verts->tx, verts->x );
 
880
                copy_v3_v3 ( verts->xold, verts->x );
 
881
                copy_v3_v3 ( verts->xconst, verts->x );
 
882
                copy_v3_v3 ( verts->txold, verts->x );
 
883
                copy_v3_v3 ( verts->tx, verts->x );
889
884
                mul_v3_fl( verts->v, 0.0f );
890
885
 
891
886
                verts->impulse_count = 0;
892
 
                VECCOPY ( verts->impulse, tnull );
 
887
                copy_v3_v3 ( verts->impulse, tnull );
893
888
        }
894
889
        
895
890
        // apply / set vertex groups
898
893
 
899
894
        if ( !cloth_build_springs ( clmd, dm ) )
900
895
        {
901
 
                cloth_free_modifier ( ob, clmd );
902
 
                modifier_setError ( & ( clmd->modifier ), "Can't build springs." );
 
896
                cloth_free_modifier ( clmd );
 
897
                modifier_setError(&(clmd->modifier), "%s", TIP_("Can't build springs."));
903
898
                printf("cloth_free_modifier cloth_build_springs\n");
904
899
                return 0;
905
900
        }
906
901
        
907
902
        for ( i = 0; i < dm->getNumVerts(dm); i++)
908
903
        {
909
 
                if((!(cloth->verts[i].flags & CLOTH_VERT_FLAG_PINNED)) && (cloth->verts[i].goal > ALMOST_ZERO))
 
904
                if ((!(cloth->verts[i].flags & CLOTH_VERT_FLAG_PINNED)) && (cloth->verts[i].goal > ALMOST_ZERO))
910
905
                {
911
906
                        cloth_add_spring (clmd, i, i, 0.0, CLOTH_SPRING_TYPE_GOAL);
912
907
                }
917
912
                solvers [clmd->sim_parms->solver_type].init ( ob, clmd );
918
913
        }
919
914
        
920
 
        if(!first)
 
915
        if (!first)
921
916
                implicit_set_positions(clmd);
922
917
 
923
 
        clmd->clothObject->bvhtree = bvhtree_build_from_cloth ( clmd, clmd->coll_parms->epsilon );
 
918
        clmd->clothObject->bvhtree = bvhtree_build_from_cloth ( clmd, MAX2(clmd->coll_parms->epsilon, clmd->coll_parms->distance_repel) );
924
919
        
925
 
        for(i = 0; i < dm->getNumVerts(dm); i++)
 
920
        for (i = 0; i < dm->getNumVerts(dm); i++)
926
921
        {
927
 
                maxdist = MAX2(maxdist, clmd->coll_parms->selfepsilon* ( cloth->verts[i].avg_spring_len*2.0));
 
922
                maxdist = MAX2(maxdist, clmd->coll_parms->selfepsilon* ( cloth->verts[i].avg_spring_len*2.0f));
928
923
        }
929
924
        
930
925
        clmd->clothObject->bvhselftree = bvhselftree_build_from_cloth ( clmd, maxdist );
932
927
        return 1;
933
928
}
934
929
 
935
 
static void cloth_from_mesh ( Object *ob, ClothModifierData *clmd, DerivedMesh *dm )
 
930
static void cloth_from_mesh ( ClothModifierData *clmd, DerivedMesh *dm )
936
931
{
937
932
        unsigned int numverts = dm->getNumVerts ( dm );
938
 
        unsigned int numfaces = dm->getNumFaces ( dm );
939
 
        MFace *mface = CDDM_get_faces(dm);
 
933
        unsigned int numfaces = dm->getNumTessFaces ( dm );
 
934
        MFace *mface = dm->getTessFaceArray( dm );
940
935
        unsigned int i = 0;
941
936
 
942
937
        /* Allocate our vertices. */
944
939
        clmd->clothObject->verts = MEM_callocN ( sizeof ( ClothVertex ) * clmd->clothObject->numverts, "clothVertex" );
945
940
        if ( clmd->clothObject->verts == NULL )
946
941
        {
947
 
                cloth_free_modifier ( ob, clmd );
948
 
                modifier_setError ( & ( clmd->modifier ), "Out of memory on allocating clmd->clothObject->verts." );
 
942
                cloth_free_modifier ( clmd );
 
943
                modifier_setError(&(clmd->modifier), "%s", TIP_("Out of memory on allocating clmd->clothObject->verts."));
949
944
                printf("cloth_free_modifier clmd->clothObject->verts\n");
950
945
                return;
951
946
        }
955
950
        clmd->clothObject->mfaces = MEM_callocN ( sizeof ( MFace ) * clmd->clothObject->numfaces, "clothMFaces" );
956
951
        if ( clmd->clothObject->mfaces == NULL )
957
952
        {
958
 
                cloth_free_modifier ( ob, clmd );
959
 
                modifier_setError ( & ( clmd->modifier ), "Out of memory on allocating clmd->clothObject->mfaces." );
 
953
                cloth_free_modifier ( clmd );
 
954
                modifier_setError(&(clmd->modifier), "%s", TIP_("Out of memory on allocating clmd->clothObject->mfaces."));
960
955
                printf("cloth_free_modifier clmd->clothObject->mfaces\n");
961
956
                return;
962
957
        }
975
970
* SPRING NETWORK BUILDING IMPLEMENTATION BEGIN
976
971
***************************************************************************************/
977
972
 
978
 
// be carefull: implicit solver has to be resettet when using this one!
 
973
// be careful: implicit solver has to be resettet when using this one!
979
974
// --> only for implicit handling of this spring!
980
975
int cloth_add_spring ( ClothModifierData *clmd, unsigned int indexA, unsigned int indexB, float restlength, int spring_type)
981
976
{
982
977
        Cloth *cloth = clmd->clothObject;
983
978
        ClothSpring *spring = NULL;
984
979
        
985
 
        if(cloth)
 
980
        if (cloth)
986
981
        {
987
982
                // TODO: look if this spring is already there
988
983
                
989
984
                spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
990
985
                
991
 
                if(!spring)
 
986
                if (!spring)
992
987
                        return 0;
993
988
                
994
989
                spring->ij = indexA;
1007
1002
        return 0;
1008
1003
}
1009
1004
 
1010
 
static void cloth_free_errorsprings(Cloth *cloth, EdgeHash *edgehash, LinkNode **edgelist)
 
1005
static void cloth_free_errorsprings(Cloth *cloth, EdgeHash *UNUSED(edgehash), LinkNode **edgelist)
1011
1006
{
1012
1007
        unsigned int i = 0;
1013
1008
        
1014
1009
        if ( cloth->springs != NULL )
1015
1010
        {
1016
1011
                LinkNode *search = cloth->springs;
1017
 
                while(search)
 
1012
                while (search)
1018
1013
                {
1019
1014
                        ClothSpring *spring = search->link;
1020
1015
                                                
1026
1021
                cloth->springs = NULL;
1027
1022
        }
1028
1023
        
1029
 
        if(edgelist)
 
1024
        if (edgelist)
1030
1025
        {
1031
1026
                for ( i = 0; i < cloth->numverts; i++ )
1032
1027
                {
1036
1031
                MEM_freeN ( edgelist );
1037
1032
        }
1038
1033
        
1039
 
        if(cloth->edgehash)
 
1034
        if (cloth->edgehash)
1040
1035
                BLI_edgehash_free ( cloth->edgehash, NULL );
1041
1036
}
1042
1037
 
1045
1040
        Cloth *cloth = clmd->clothObject;
1046
1041
        ClothSpring *spring = NULL, *tspring = NULL, *tspring2 = NULL;
1047
1042
        unsigned int struct_springs = 0, shear_springs=0, bend_springs = 0;
1048
 
        int i = 0;
1049
 
        int numverts = dm->getNumVerts ( dm );
1050
 
        int numedges = dm->getNumEdges ( dm );
1051
 
        int numfaces = dm->getNumFaces ( dm );
1052
 
        MEdge *medge = CDDM_get_edges ( dm );
1053
 
        MFace *mface = CDDM_get_faces ( dm );
 
1043
        unsigned int i = 0;
 
1044
        unsigned int numverts = (unsigned int)dm->getNumVerts ( dm );
 
1045
        unsigned int numedges = (unsigned int)dm->getNumEdges ( dm );
 
1046
        unsigned int numfaces = (unsigned int)dm->getNumTessFaces ( dm );
 
1047
        MEdge *medge = dm->getEdgeArray ( dm );
 
1048
        MFace *mface = dm->getTessFaceArray ( dm );
1054
1049
        int index2 = 0; // our second vertex index
1055
1050
        LinkNode **edgelist = NULL;
1056
1051
        EdgeHash *edgehash = NULL;
1057
1052
        LinkNode *search = NULL, *search2 = NULL;
1058
 
        float temp[3];
1059
1053
        
1060
1054
        // error handling
1061
1055
        if ( numedges==0 )
1065
1059
 
1066
1060
        edgelist = MEM_callocN ( sizeof ( LinkNode * ) * numverts, "cloth_edgelist_alloc" );
1067
1061
        
1068
 
        if(!edgelist)
 
1062
        if (!edgelist)
1069
1063
                return 0;
1070
1064
        
1071
1065
        for ( i = 0; i < numverts; i++ )
1088
1082
                {
1089
1083
                        spring->ij = MIN2(medge[i].v1, medge[i].v2);
1090
1084
                        spring->kl = MAX2(medge[i].v2, medge[i].v1);
1091
 
                        VECSUB ( temp, cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest );
1092
 
                        spring->restlen =  sqrt ( INPR ( temp, temp ) );
 
1085
                        spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest);
1093
1086
                        clmd->sim_parms->avg_spring_len += spring->restlen;
1094
1087
                        cloth->verts[spring->ij].avg_spring_len += spring->restlen;
1095
1088
                        cloth->verts[spring->kl].avg_spring_len += spring->restlen;
1097
1090
                        cloth->verts[spring->kl].spring_count++;
1098
1091
                        spring->type = CLOTH_SPRING_TYPE_STRUCTURAL;
1099
1092
                        spring->flags = 0;
1100
 
                        spring->stiffness = (cloth->verts[spring->kl].struct_stiff + cloth->verts[spring->ij].struct_stiff) / 2.0;
 
1093
                        spring->stiffness = (cloth->verts[spring->kl].struct_stiff + cloth->verts[spring->ij].struct_stiff) / 2.0f;
1101
1094
                        struct_springs++;
1102
1095
                        
1103
1096
                        BLI_linklist_prepend ( &cloth->springs, spring );
1104
1097
                }
1105
 
                else
1106
 
                {
 
1098
                else {
1107
1099
                        cloth_free_errorsprings(cloth, edgehash, edgelist);
1108
1100
                        return 0;
1109
1101
                }
1110
1102
        }
1111
1103
        
1112
 
        if(struct_springs > 0)
 
1104
        if (struct_springs > 0)
1113
1105
                clmd->sim_parms->avg_spring_len /= struct_springs;
1114
1106
        
1115
 
        for(i = 0; i < numverts; i++)
 
1107
        for (i = 0; i < numverts; i++)
1116
1108
        {
1117
 
                cloth->verts[i].avg_spring_len = cloth->verts[i].avg_spring_len * 0.49 / ((float)cloth->verts[i].spring_count);
 
1109
                cloth->verts[i].avg_spring_len = cloth->verts[i].avg_spring_len * 0.49f / ((float)cloth->verts[i].spring_count);
1118
1110
        }
1119
1111
        
1120
1112
        // shear springs
1126
1118
                
1127
1119
                spring = ( ClothSpring *) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1128
1120
                
1129
 
                if(!spring)
 
1121
                if (!spring)
1130
1122
                {
1131
1123
                        cloth_free_errorsprings(cloth, edgehash, edgelist);
1132
1124
                        return 0;
1134
1126
 
1135
1127
                spring->ij = MIN2(mface[i].v1, mface[i].v3);
1136
1128
                spring->kl = MAX2(mface[i].v3, mface[i].v1);
1137
 
                VECSUB ( temp, cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest );
1138
 
                spring->restlen =  sqrt ( INPR ( temp, temp ) );
 
1129
                spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest);
1139
1130
                spring->type = CLOTH_SPRING_TYPE_SHEAR;
1140
 
                spring->stiffness = (cloth->verts[spring->kl].shear_stiff + cloth->verts[spring->ij].shear_stiff) / 2.0;
 
1131
                spring->stiffness = (cloth->verts[spring->kl].shear_stiff + cloth->verts[spring->ij].shear_stiff) / 2.0f;
1141
1132
 
1142
1133
                BLI_linklist_append ( &edgelist[spring->ij], spring );
1143
1134
                BLI_linklist_append ( &edgelist[spring->kl], spring );
1149
1140
                // if ( mface[i].v4 ) --> Quad face
1150
1141
                spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1151
1142
                
1152
 
                if(!spring)
 
1143
                if (!spring)
1153
1144
                {
1154
1145
                        cloth_free_errorsprings(cloth, edgehash, edgelist);
1155
1146
                        return 0;
1157
1148
 
1158
1149
                spring->ij = MIN2(mface[i].v2, mface[i].v4);
1159
1150
                spring->kl = MAX2(mface[i].v4, mface[i].v2);
1160
 
                VECSUB ( temp, cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest );
1161
 
                spring->restlen =  sqrt ( INPR ( temp, temp ) );
 
1151
                spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest);
1162
1152
                spring->type = CLOTH_SPRING_TYPE_SHEAR;
1163
 
                spring->stiffness = (cloth->verts[spring->kl].shear_stiff + cloth->verts[spring->ij].shear_stiff) / 2.0;
 
1153
                spring->stiffness = (cloth->verts[spring->kl].shear_stiff + cloth->verts[spring->ij].shear_stiff) / 2.0f;
1164
1154
 
1165
1155
                BLI_linklist_append ( &edgelist[spring->ij], spring );
1166
1156
                BLI_linklist_append ( &edgelist[spring->kl], spring );
1169
1159
                BLI_linklist_prepend ( &cloth->springs, spring );
1170
1160
        }
1171
1161
        
1172
 
        if(numfaces) {
 
1162
        if (numfaces) {
1173
1163
                // bending springs
1174
1164
                search2 = cloth->springs;
1175
1165
                for ( i = struct_springs; i < struct_springs+shear_springs; i++ )
1186
1176
                                
1187
1177
                                // check for existing spring
1188
1178
                                // check also if startpoint is equal to endpoint
1189
 
                                if ( !BLI_edgehash_haskey ( edgehash, MIN2(tspring2->ij, index2), MAX2(tspring2->ij, index2) )
1190
 
                                && ( index2!=tspring2->ij ) )
 
1179
                                if (!BLI_edgehash_haskey(edgehash, MIN2(tspring2->ij, index2), MAX2(tspring2->ij, index2)) &&
 
1180
                                    (index2 != tspring2->ij))
1191
1181
                                {
1192
1182
                                        spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1193
1183
                                        
1194
 
                                        if(!spring)
 
1184
                                        if (!spring)
1195
1185
                                        {
1196
1186
                                                cloth_free_errorsprings(cloth, edgehash, edgelist);
1197
1187
                                                return 0;
1199
1189
 
1200
1190
                                        spring->ij = MIN2(tspring2->ij, index2);
1201
1191
                                        spring->kl = MAX2(tspring2->ij, index2);
1202
 
                                        VECSUB ( temp, cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest );
1203
 
                                        spring->restlen =  sqrt ( INPR ( temp, temp ) );
 
1192
                                        spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest);
1204
1193
                                        spring->type = CLOTH_SPRING_TYPE_BENDING;
1205
 
                                        spring->stiffness = (cloth->verts[spring->kl].bend_stiff + cloth->verts[spring->ij].bend_stiff) / 2.0;
 
1194
                                        spring->stiffness = (cloth->verts[spring->kl].bend_stiff + cloth->verts[spring->ij].bend_stiff) / 2.0f;
1206
1195
                                        BLI_edgehash_insert ( edgehash, spring->ij, spring->kl, NULL );
1207
1196
                                        bend_springs++;
1208
1197
 
1213
1202
                        search2 = search2->next;
1214
1203
                }
1215
1204
        }
1216
 
        else if(struct_springs > 2) {
 
1205
        else if (struct_springs > 2) {
1217
1206
                /* bending springs for hair strands */
1218
1207
                /* The current algorightm only goes through the edges in order of the mesh edges list   */
1219
1208
                /* and makes springs between the outer vert of edges sharing a vertice. This works just */
1223
1212
                /* of the strands. -jahka */
1224
1213
                search = cloth->springs;
1225
1214
                search2 = search->next;
1226
 
                while(search && search2)
 
1215
                while (search && search2)
1227
1216
                {
1228
1217
                        tspring = search->link;
1229
1218
                        tspring2 = search2->link;
1230
1219
 
1231
 
                        if(tspring->ij == tspring2->kl) {
 
1220
                        if (tspring->ij == tspring2->kl) {
1232
1221
                                spring = ( ClothSpring * ) MEM_callocN ( sizeof ( ClothSpring ), "cloth spring" );
1233
1222
                                
1234
 
                                if(!spring)
 
1223
                                if (!spring)
1235
1224
                                {
1236
1225
                                        cloth_free_errorsprings(cloth, edgehash, edgelist);
1237
1226
                                        return 0;
1239
1228
 
1240
1229
                                spring->ij = tspring2->ij;
1241
1230
                                spring->kl = tspring->kl;
1242
 
                                VECSUB ( temp, cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest );
1243
 
                                spring->restlen =  sqrt ( INPR ( temp, temp ) );
 
1231
                                spring->restlen = len_v3v3(cloth->verts[spring->kl].xrest, cloth->verts[spring->ij].xrest);
1244
1232
                                spring->type = CLOTH_SPRING_TYPE_BENDING;
1245
 
                                spring->stiffness = (cloth->verts[spring->kl].bend_stiff + cloth->verts[spring->ij].bend_stiff) / 2.0;
 
1233
                                spring->stiffness = (cloth->verts[spring->kl].bend_stiff + cloth->verts[spring->ij].bend_stiff) / 2.0f;
1246
1234
                                bend_springs++;
1247
1235
 
1248
1236
                                BLI_linklist_prepend ( &cloth->springs, spring );
1259
1247
        
1260
1248
        for ( i = 0; i < numfaces; i++ ) // edge springs
1261
1249
        {
1262
 
                if(mface[i].v4)
 
1250
                if (mface[i].v4)
1263
1251
                {
1264
1252
                        BLI_edgehash_insert ( edgehash, MIN2(mface[i].v1, mface[i].v3), MAX2(mface[i].v3, mface[i].v1), NULL );
1265
1253
                        
1282
1270
        
1283
1271
        cloth->edgehash = edgehash;
1284
1272
        
1285
 
        if(G.rt>0)
 
1273
        if (G.rt>0)
1286
1274
                printf("avg_len: %f\n",clmd->sim_parms->avg_spring_len);
1287
1275
 
1288
1276
        return 1;