~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2005-11-06 12:40:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051106124003-3pgs7tcg5rox96xg
Tags: 2.37a-1.1
* Non-maintainer upload.
* Split out parts of 01_SConstruct_debian.dpatch again: root_build_dir
  really needs to get adjusted before the clean target runs - closes: #333958,
  see #288882 for reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 * 
5
5
 *      Reevan McKay
6
6
 *
7
 
 * $Id: deform.c,v 1.4 2004/02/23 19:05:42 sirdude Exp $
 
7
 * $Id: deform.c,v 1.17 2005/05/02 13:28:11 ton Exp $
8
8
 *
9
9
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
10
10
 *
36
36
 */
37
37
 
38
38
#include <string.h>
 
39
#include <math.h>
 
40
 
39
41
#include "MEM_guardedalloc.h"
40
 
#include "BLI_blenlib.h"
 
42
 
 
43
#include "DNA_curve_types.h"
 
44
#include "DNA_effect_types.h"
 
45
#include "DNA_lattice_types.h"
 
46
#include "DNA_mesh_types.h"
 
47
#include "DNA_meshdata_types.h"
41
48
#include "DNA_object_types.h"
 
49
#include "DNA_object_force.h"
 
50
#include "DNA_scene_types.h"
 
51
 
 
52
#include "BKE_curve.h"
42
53
#include "BKE_deform.h"
 
54
#include "BKE_displist.h"
 
55
#include "BKE_effect.h"
 
56
#include "BKE_global.h"
 
57
#include "BKE_lattice.h"
 
58
#include "BKE_object.h"
 
59
#include "BKE_softbody.h"
 
60
#include "BKE_utildefines.h"
 
61
 
 
62
#include "BLI_blenlib.h"
 
63
#include "BLI_arithb.h"
43
64
 
44
65
#ifdef HAVE_CONFIG_H
45
66
#include <config.h>
46
67
#endif
47
68
 
48
 
void color_temperature (float input, unsigned char *r, unsigned char *g, unsigned char *b)
49
 
{
50
 
        
51
 
        /* blue to red */
52
 
        
53
 
        float fr = (float)(*r);
54
 
        float fg = (float)(*g);
55
 
        float fb = (float)(*b);
56
 
 
57
 
        if (input < 0.0F)
58
 
                input = 0.0F;
59
 
 
60
 
        if (input > 1.0F)
61
 
                input = 1.0F;
62
 
 
63
 
        if (input<=0.25f){
64
 
                fr=0.0f;
65
 
                fg=255.0f * (input*4.0f);
66
 
                fb=255.0f;
67
 
        }
68
 
        else if (input<=0.50f){
69
 
                fr=0.0f;
70
 
                fg=255.0f;
71
 
                fb=255.0f * (1.0f-((input-0.25f)*4.0f)); 
72
 
        }
73
 
        else if (input<=0.75){
74
 
                fr=255.0f * ((input-0.50f)*4.0f);
75
 
                fg=255.0f;
76
 
                fb=0.0f;
77
 
        }
78
 
        else if (input<=1.0){
79
 
                fr=255.0f;
80
 
                fg=255.0f * (1.0f-((input-0.75f)*4.0f)); 
81
 
                fb=0.0f;
82
 
        }
83
 
 
84
 
        (*r) = (unsigned char)(fr * ((input/2.0f)+0.5f));
85
 
        (*g) = (unsigned char)(fg * ((input/2.0f)+0.5f));
86
 
        (*b) = (unsigned char)(fb * ((input/2.0f)+0.5f));
87
 
 
88
 
 
89
 
}
90
69
 
91
70
void copy_defgroups(ListBase *outbase, ListBase *inbase)
92
71
{
117
96
        return outgroup;
118
97
}
119
98
 
 
99
bDeformGroup *get_named_vertexgroup(Object *ob, char *name)
 
100
{
 
101
        /* return a pointer to the deform group with this name
 
102
         * or return NULL otherwise.
 
103
         */
 
104
        bDeformGroup *curdef;
 
105
 
 
106
        for (curdef = ob->defbase.first; curdef; curdef=curdef->next){
 
107
                if (!strcmp(curdef->name, name)){
 
108
                        return curdef;
 
109
                }
 
110
        }
 
111
        return NULL;
 
112
}
 
113
 
 
114
int  get_defgroup_num (Object *ob, bDeformGroup *dg)
 
115
{
 
116
        /* Fetch the location of this deform group
 
117
         * within the linked list of deform groups.
 
118
         * (this number is stored in the deform
 
119
         * weights of the deform verts to link them
 
120
         * to this deform group) deform deform
 
121
         * deform blah blah deform
 
122
         */
 
123
 
 
124
        bDeformGroup    *eg;
 
125
        int def_nr;
 
126
 
 
127
        eg = ob->defbase.first;
 
128
        def_nr = 0;
 
129
 
 
130
        /* loop through all deform groups
 
131
         */
 
132
        while (eg != NULL){
 
133
 
 
134
                /* if the current deform group is
 
135
                 * the one we are after, return
 
136
                 * def_nr
 
137
                 */
 
138
                if (eg == dg){
 
139
                        break;
 
140
                }
 
141
                ++def_nr;
 
142
                eg = eg->next;
 
143
        }
 
144
 
 
145
        /* if there was no deform group found then
 
146
         * return -1 (should set up a nice symbolic
 
147
         * constant for this)
 
148
         */
 
149
        if (eg == NULL) return -1;
 
150
        
 
151
        return def_nr;
 
152
    
 
153
}
 
154
 
 
155
/* *************** HOOK ****************** */
 
156
 
 
157
/* vec==NULL: init
 
158
   vec is supposed to be local coord, deform happens in local space
 
159
*/
 
160
 
 
161
void hook_object_deform(Object *ob, int index, float *vec)
 
162
{
 
163
        float totforce;
 
164
        ObHook *hook;
 
165
        float vect[3], vectot[3];
 
166
        
 
167
        if(ob->hooks.first==NULL) return;
 
168
        
 
169
        /* reinitialize if... */
 
170
        if(vec==NULL) {
 
171
                totforce= 0.0;
 
172
                for(hook= ob->hooks.first; hook; hook= hook->next) {
 
173
                        if(hook->parent) {
 
174
                                hook->curindex= 0;
 
175
                                Mat4Invert(ob->imat, ob->obmat);
 
176
                                /* apparently this call goes from right to left... */
 
177
                                Mat4MulSerie(hook->mat, ob->imat, hook->parent->obmat, hook->parentinv, NULL, 
 
178
                                                        NULL, NULL, NULL, NULL);
 
179
                        }
 
180
                }
 
181
                return;
 
182
        }
 
183
 
 
184
        totforce= 0.0;
 
185
        vectot[0]= vectot[1]= vectot[2]= 0.0;
 
186
        
 
187
        for(hook= ob->hooks.first; hook; hook= hook->next) {
 
188
                if(hook->parent) {
 
189
                        
 
190
                        /* is 'index' in hook array? */
 
191
                        while(hook->curindex < hook->totindex-1) {
 
192
                                if( hook->indexar[hook->curindex] < index ) hook->curindex++;
 
193
                                else break;
 
194
                        }
 
195
                        
 
196
                        if( hook->indexar[hook->curindex]==index ) {
 
197
                                float fac= hook->force, len;
 
198
                                
 
199
                                VecMat4MulVecfl(vect, hook->mat, vec);
 
200
 
 
201
                                if(hook->falloff!=0.0) {
 
202
                                        /* hook->cent is in local coords */
 
203
                                        len= VecLenf(vec, hook->cent);
 
204
                                        if(len > hook->falloff) fac= 0.0;
 
205
                                        else if(len>0.0) fac*= sqrt(1.0 - len/hook->falloff);
 
206
                                }
 
207
                                if(fac!=0.0) {
 
208
                                        totforce+= fac;
 
209
                                        vectot[0]+= fac*vect[0];
 
210
                                        vectot[1]+= fac*vect[1];
 
211
                                        vectot[2]+= fac*vect[2];
 
212
                                }
 
213
                        }
 
214
                }
 
215
        }
 
216
 
 
217
        /* if totforce < 1.0, we take old position also into account */
 
218
        if(totforce<1.0) {
 
219
                vectot[0]+= (1.0-totforce)*vec[0];
 
220
                vectot[1]+= (1.0-totforce)*vec[1];
 
221
                vectot[2]+= (1.0-totforce)*vec[2];
 
222
        }
 
223
        else VecMulf(vectot, 1.0/totforce);
 
224
        
 
225
        VECCOPY(vec, vectot);
 
226
}
 
227
 
 
228
 
 
229
/* modifiers: hooks, deform, softbody 
 
230
   mode=='s' is start, 'e' is end , 'a' is apply
 
231
*/
 
232
 
 
233
int mesh_modifier(Object *ob, char mode)
 
234
{
 
235
        static MVert *mvert=NULL;
 
236
        Mesh *me= ob->data;
 
237
        MVert *mv;
 
238
        int a, done=0;
 
239
        
 
240
        /* conditions if it's needed */
 
241
        if(ob->hooks.first);
 
242
        else if(ob->effect.first);      // weak... particles too
 
243
        else if(ob->parent && ob->parent->type==OB_LATTICE);
 
244
        else if(ob->parent && ob->partype==PARSKEL); 
 
245
        else if(ob->softflag & OB_SB_ENABLE);
 
246
        else return 0;
 
247
        
 
248
        if(me->totvert==0) return 0;
 
249
        
 
250
        if(mode=='s') { // "start"
 
251
                /* copy  */
 
252
                mvert= MEM_dupallocN(me->mvert);
 
253
                
 
254
                /* hooks */
 
255
                if(ob->hooks.first) {
 
256
                        done= 1;
 
257
                        
 
258
                        /* NULL signals initialize */
 
259
                        hook_object_deform(ob, 0, NULL);
 
260
                        
 
261
                        for(a=0, mv= me->mvert; a<me->totvert; a++, mv++) {
 
262
                                hook_object_deform(ob, a, mv->co);
 
263
                        }
 
264
                }
 
265
                
 
266
                if(ob->effect.first) done |= object_wave(ob);
 
267
 
 
268
                if((ob->softflag & OB_SB_ENABLE) && !(ob->softflag & OB_SB_POSTDEF)) {
 
269
                        done= 1;
 
270
                        sbObjectStep(ob, (float)G.scene->r.cfra);
 
271
                }
 
272
 
 
273
                /* object_deform: output for mesh is in mesh->mvert */
 
274
                done |= object_deform(ob);      
 
275
 
 
276
                if((ob->softflag & OB_SB_ENABLE) && (ob->softflag & OB_SB_POSTDEF)) {
 
277
                        done= 1;
 
278
                        sbObjectStep(ob, (float)G.scene->r.cfra);
 
279
                }
 
280
                
 
281
                /* put deformed vertices in dl->verts, optional subsurf will replace that */
 
282
                if(done) {
 
283
                        DispList *dl= find_displist_create(&ob->disp, DL_VERTS);
 
284
                        float *fp;
 
285
                        
 
286
                        if(dl->verts) MEM_freeN(dl->verts);
 
287
                        if(dl->nors) MEM_freeN(dl->nors);
 
288
                        dl->nr= me->totvert;
 
289
                        if(dl->nr) {
 
290
                                
 
291
                                /* make disp array */
 
292
                                dl->verts= fp= MEM_mallocN(3*sizeof(float)*me->totvert, "deform1");
 
293
                                mv= me->mvert;
 
294
                                for(a=0; a<me->totvert; a++, mv++, fp+=3) {
 
295
                                        VECCOPY(fp, mv->co);
 
296
                                }
 
297
                        }
 
298
                }
 
299
                
 
300
        }
 
301
        else if(mode=='e') { // end
 
302
                if(mvert) {
 
303
                        if(me->mvert) MEM_freeN(me->mvert);
 
304
                        me->mvert= mvert;
 
305
                        mvert= NULL;
 
306
                }
 
307
        }
 
308
        else if(mode=='a') { // apply
 
309
                if(mvert) MEM_freeN(mvert);
 
310
                mvert= NULL;
 
311
        }
 
312
        
 
313
        return done;
 
314
}
 
315
 
 
316
int curve_modifier(Object *ob, char mode)
 
317
{
 
318
        static ListBase nurb={NULL, NULL};
 
319
        Curve *cu= ob->data;
 
320
        Nurb *nu, *newnu;
 
321
        BezTriple *bezt;
 
322
        BPoint *bp;
 
323
        int a, index, done= 0;
 
324
        
 
325
        /* conditions if it's needed */
 
326
        if(ob->hooks.first);
 
327
        else if(ob->parent && ob->partype==PARSKEL); 
 
328
        else if(ob->parent && ob->parent->type==OB_LATTICE);
 
329
        else return 0;
 
330
        
 
331
        if(mode=='s') { // "start"
 
332
                /* copy  */
 
333
                nurb.first= nurb.last= NULL;    
 
334
                nu= cu->nurb.first;
 
335
                while(nu) {
 
336
                        newnu= duplicateNurb(nu);
 
337
                        BLI_addtail(&nurb, newnu);
 
338
                        nu= nu->next;
 
339
                }
 
340
                
 
341
                /* hooks */
 
342
                if(ob->hooks.first) {
 
343
                        done= 1;
 
344
                        
 
345
                        /* NULL signals initialize */
 
346
                        hook_object_deform(ob, 0, NULL);
 
347
                        index= 0;
 
348
                        
 
349
                        nu= cu->nurb.first;
 
350
                        while(nu) {
 
351
                                if((nu->type & 7)==CU_BEZIER) {
 
352
                                        bezt= nu->bezt;
 
353
                                        a= nu->pntsu;
 
354
                                        while(a--) {
 
355
                                                hook_object_deform(ob, index++, bezt->vec[0]);
 
356
                                                hook_object_deform(ob, index++, bezt->vec[1]);
 
357
                                                hook_object_deform(ob, index++, bezt->vec[2]);
 
358
                                                bezt++;
 
359
                                        }
 
360
                                }
 
361
                                else {
 
362
                                        bp= nu->bp;
 
363
                                        a= nu->pntsu*nu->pntsv;
 
364
                                        while(a--) {
 
365
                                                hook_object_deform(ob, index++, bp->vec);
 
366
                                                bp++;
 
367
                                        }
 
368
                                }
 
369
                                        
 
370
                                nu= nu->next;
 
371
                        }
 
372
                }
 
373
        }
 
374
        else if(mode=='e') {
 
375
                /* paste */
 
376
                freeNurblist(&cu->nurb);
 
377
                cu->nurb= nurb;
 
378
        }
 
379
        else if(mode=='a') {
 
380
                freeNurblist(&nurb);
 
381
        }
 
382
        
 
383
        return done;
 
384
}
 
385
 
 
386
int lattice_modifier(Object *ob, char mode)
 
387
{
 
388
        static BPoint *bpoint;
 
389
        Lattice *lt= ob->data;
 
390
        BPoint *bp;
 
391
        int a, index, done= 0;
 
392
        
 
393
        /* conditions if it's needed */
 
394
        if(ob->hooks.first);
 
395
        else if(ob->parent && ob->partype==PARSKEL); 
 
396
        else if((ob->softflag & OB_SB_ENABLE));
 
397
        else return 0;
 
398
        
 
399
        if(mode=='s') { // "start"
 
400
                /* copy  */
 
401
                bpoint= MEM_dupallocN(lt->def);
 
402
                
 
403
                /* hooks */
 
404
                if(ob->hooks.first) {
 
405
                        done= 1;
 
406
                        
 
407
                        /* NULL signals initialize */
 
408
                        hook_object_deform(ob, 0, NULL);
 
409
                        index= 0;
 
410
                        bp= lt->def;
 
411
                        a= lt->pntsu*lt->pntsv*lt->pntsw;
 
412
                        while(a--) {
 
413
                                hook_object_deform(ob, index++, bp->vec);
 
414
                                bp++;
 
415
                        }
 
416
                }
 
417
                
 
418
                if((ob->softflag & OB_SB_ENABLE)) {
 
419
                        sbObjectStep(ob, (float)G.scene->r.cfra);
 
420
                }
 
421
                
 
422
        }
 
423
        else { // end
 
424
                MEM_freeN(lt->def);
 
425
                lt->def= bpoint;
 
426
                bpoint= NULL;
 
427
        }
 
428
        
 
429
        return done;
 
430
}
 
431