bugfix [#21036] crash with glsl shading
[blender.git] / source / blender / blenkernel / intern / deform.c
1 /*  deform.c   June 2001
2  *  
3  *  support for deformation groups
4  * 
5  *      Reevan McKay
6  *
7  * $Id$
8  *
9  * ***** BEGIN GPL LICENSE BLOCK *****
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL LICENSE BLOCK *****
33  */
34
35 #include <string.h>
36 #include <math.h>
37 #include "ctype.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "DNA_curve_types.h"
42 #include "DNA_effect_types.h"
43 #include "DNA_lattice_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_object_force.h"
48 #include "DNA_scene_types.h"
49
50 #include "BKE_curve.h"
51 #include "BKE_deform.h"
52 #include "BKE_displist.h"
53 #include "BKE_effect.h"
54 #include "BKE_global.h"
55 #include "BKE_key.h"
56 #include "BKE_lattice.h"
57 #include "BKE_object.h"
58 #include "BKE_softbody.h"
59 #include "BKE_utildefines.h"
60 #include "BKE_mesh.h"
61
62 #include "BLI_blenlib.h"
63 #include "BLI_math.h"
64
65 #ifdef HAVE_CONFIG_H
66 #include <config.h>
67 #endif
68
69
70 void defgroup_copy_list (ListBase *outbase, ListBase *inbase)
71 {
72         bDeformGroup *defgroup, *defgroupn;
73
74         outbase->first= outbase->last= 0;
75
76         for (defgroup = inbase->first; defgroup; defgroup=defgroup->next){
77                 defgroupn= defgroup_duplicate(defgroup);
78                 BLI_addtail(outbase, defgroupn);
79         }
80 }
81
82 bDeformGroup *defgroup_duplicate (bDeformGroup *ingroup)
83 {
84         bDeformGroup *outgroup;
85
86         if (!ingroup)
87                 return NULL;
88
89         outgroup=MEM_callocN(sizeof(bDeformGroup), "copy deformGroup");
90         
91         /* For now, just copy everything over. */
92         memcpy (outgroup, ingroup, sizeof(bDeformGroup));
93
94         outgroup->next=outgroup->prev=NULL;
95
96         return outgroup;
97 }
98
99 /* copy & overwrite weights */
100 void defvert_copy (MDeformVert *dvert_r, const MDeformVert *dvert)
101 {
102         if(dvert_r->totweight == dvert->totweight) {
103                 if(dvert->totweight)
104                         memcpy(dvert_r->dw, dvert->dw, dvert->totweight * sizeof(MDeformWeight));
105         }
106         else {
107                 if(dvert_r->dw)
108                         MEM_freeN(dvert_r->dw);
109
110                 if(dvert->totweight)
111                         dvert_r->dw= MEM_dupallocN(dvert->dw);
112                 else
113                         dvert_r->dw= NULL;
114
115                 dvert_r->totweight = dvert->totweight;
116         }
117 }
118
119 /* only sync over matching weights, don't add or remove groups
120  * warning, loop within loop.
121  */
122 void defvert_sync (MDeformVert *dvert_r, const MDeformVert *dvert, int use_verify)
123 {
124         if(dvert->totweight && dvert_r->totweight) {
125                 int i;
126                 MDeformWeight *dw;
127                 for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++) {
128                         MDeformWeight *dw_r;
129                         if(use_verify)  dw_r= defvert_find_index(dvert_r, dw->def_nr);
130                         else                    dw_r= defvert_verify_index(dvert_r, dw->def_nr);
131
132                         if(dw_r) {
133                                 dw_r->weight= dw->weight;
134                         }
135                 }
136         }
137 }
138
139 /* be sure all flip_map values are valid */
140 void defvert_sync_mapped (MDeformVert *dvert_r, const MDeformVert *dvert, int *flip_map, int use_verify)
141 {
142         if(dvert->totweight && dvert_r->totweight) {
143                 int i;
144                 MDeformWeight *dw;
145                 for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++) {
146                         MDeformWeight *dw_r;
147                         if(use_verify)  dw_r= defvert_find_index(dvert_r, flip_map[dw->def_nr]);
148                         else                    dw_r= defvert_verify_index(dvert_r, flip_map[dw->def_nr]);
149
150                         if(dw_r) {
151                                 dw_r->weight= dw->weight;
152                         }
153                 }
154         }
155 }
156
157 /* be sure all flip_map values are valid */
158 void defvert_remap (MDeformVert *dvert, int *map)
159 {
160         MDeformWeight *dw;
161         int i;
162         for(i=0, dw=dvert->dw; i<dvert->totweight; i++, dw++) {
163                 dw->def_nr= map[dw->def_nr];
164         }
165 }
166
167 void defvert_normalize (MDeformVert *dvert)
168 {
169         if(dvert->totweight<=0) {
170                 /* nothing */
171         }
172         else if (dvert->totweight==1) {
173                 dvert->dw[0].weight= 1.0f;
174         }
175         else {
176                 int i;
177                 float tot= 0.0f;
178                 MDeformWeight *dw;
179                 for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++)
180                         tot += dw->weight;
181
182                 if(tot > 0.0f) {
183                         for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++)
184                                 dw->weight /= tot;
185                 }
186         }
187 }
188
189 void defvert_flip (MDeformVert *dvert, int *flip_map)
190 {
191         MDeformWeight *dw;
192         int i;
193
194         for(dw= dvert->dw, i=0; i<dvert->totweight; dw++, i++)
195                 if(flip_map[dw->def_nr] >= 0)
196                         dw->def_nr= flip_map[dw->def_nr];
197 }
198
199
200 bDeformGroup *defgroup_find_name (Object *ob, char *name)
201 {
202         /* return a pointer to the deform group with this name
203          * or return NULL otherwise.
204          */
205         bDeformGroup *curdef;
206
207         for (curdef = ob->defbase.first; curdef; curdef=curdef->next) {
208                 if (!strcmp(curdef->name, name)) {
209                         return curdef;
210                 }
211         }
212         return NULL;
213 }
214
215 int defgroup_name_index (Object *ob, const char *name)
216 {
217         /* Return the location of the named deform group within the list of
218          * deform groups. This function is a combination of defgroup_find_index and
219          * defgroup_find_name. The other two could be called instead, but that
220          * require looping over the vertexgroups twice.
221          */
222         bDeformGroup *curdef;
223         int def_nr;
224         
225         if(name[0] != '\0') {
226                 for (curdef=ob->defbase.first, def_nr=0; curdef; curdef=curdef->next, def_nr++) {
227                         if (!strcmp(curdef->name, name))
228                                 return def_nr;
229                 }
230         }
231
232         return -1;
233 }
234
235 int defgroup_find_index (Object *ob, bDeformGroup *dg)
236 {
237         /* Fetch the location of this deform group
238          * within the linked list of deform groups.
239          * (this number is stored in the deform
240          * weights of the deform verts to link them
241          * to this deform group).
242          *
243          * note: this is zero based, ob->actdef starts at 1.
244          */
245
246         bDeformGroup *eg;
247         int def_nr;
248
249         eg = ob->defbase.first;
250         def_nr = 0;
251
252         /* loop through all deform groups */
253         while (eg != NULL) {
254
255                 /* if the current deform group is
256                  * the one we are after, return
257                  * def_nr
258                  */
259                 if (eg == dg) {
260                         break;
261                 }
262                 ++def_nr;
263                 eg = eg->next;
264         }
265
266         /* if there was no deform group found then
267          * return -1 (should set up a nice symbolic
268          * constant for this)
269          */
270         if (eg == NULL) return -1;
271         
272         return def_nr;
273     
274 }
275
276 /* note, must be freed */
277 int *defgroup_flip_map(Object *ob, int use_default)
278 {
279         bDeformGroup *dg;
280         int totdg= BLI_countlist(&ob->defbase);
281
282         if(totdg==0) {
283                 return NULL;
284         }
285         else {
286                 char name[sizeof(dg->name)];
287                 int i, flip_num, *map= MEM_mallocN(totdg * sizeof(int), "get_defgroup_flip_map");
288
289                 memset(map, -1, totdg * sizeof(int));
290
291                 for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
292                         if(map[i] == -1) { /* may be calculated previously */
293
294                                 /* incase no valid value is found, use this */
295                                 if(use_default)
296                                         map[i]= i;
297
298                                 flip_side_name(name, dg->name, 0);
299                                 if(strcmp(name, dg->name)) {
300                                         flip_num= defgroup_name_index(ob, name);
301                                         if(flip_num >= 0) {
302                                                 map[i]= flip_num;
303                                                 map[flip_num]= i; /* save an extra lookup */
304                                         }
305                                 }
306                         }
307                 }
308                 return map;
309         }
310 }
311
312 int defgroup_flip_index(Object *ob, int index, int use_default)
313 {
314         bDeformGroup *dg= BLI_findlink(&ob->defbase, index);
315         int flip_index = -1;
316
317         if(dg) {
318                 char name[sizeof(dg->name)];
319                 flip_side_name(name, dg->name, 0);
320
321                 if(strcmp(name, dg->name))
322                         flip_index= defgroup_name_index(ob, name);
323         }
324
325         return (flip_index==-1 && use_default) ? index : flip_index;
326 }
327
328 void defgroup_unique_name (bDeformGroup *dg, Object *ob)
329 {
330         bDeformGroup *curdef;
331         int number;
332         int exists = 0;
333         char tempname[64];
334         char *dot;
335         
336         if (!ob)
337                 return;
338                 
339         /* See if we are given an empty string */
340         if (dg->name[0] == '\0') {
341                 /* give it default name first */
342                 strcpy (dg->name, "Group");
343         }       
344                 
345         /* See if we even need to do this */
346         for (curdef = ob->defbase.first; curdef; curdef=curdef->next) {
347                 if (dg!=curdef) {
348                         if (!strcmp(curdef->name, dg->name)) {
349                                 exists = 1;
350                                 break;
351                         }
352                 }
353         }
354         
355         if (!exists)
356                 return;
357
358         /*      Strip off the suffix */
359         dot=strchr(dg->name, '.');
360         if (dot)
361                 *dot=0;
362         
363         for (number = 1; number <=999; number++) {
364                 sprintf (tempname, "%s.%03d", dg->name, number);
365                 
366                 exists = 0;
367                 for (curdef=ob->defbase.first; curdef; curdef=curdef->next) {
368                         if (dg!=curdef) {
369                                 if (!strcmp (curdef->name, tempname)) {
370                                         exists = 1;
371                                         break;
372                                 }
373                         }
374                 }
375                 if (!exists) {
376                         BLI_strncpy (dg->name, tempname, 32);
377                         return;
378                 }
379         }       
380 }
381
382
383 /* finds the best possible flipped name. For renaming; check for unique names afterwards */
384 /* if strip_number: removes number extensions */
385 void flip_side_name (char *name, const char *from_name, int strip_number)
386 {
387         int     len;
388         char    prefix[sizeof((bDeformGroup *)NULL)->name]={""};   /* The part before the facing */
389         char    suffix[sizeof((bDeformGroup *)NULL)->name]={""};   /* The part after the facing */
390         char    replace[sizeof((bDeformGroup *)NULL)->name]={""};  /* The replacement string */
391         char    number[sizeof((bDeformGroup *)NULL)->name]={""};   /* The number extension string */
392         char    *index=NULL;
393
394         len= strlen(from_name);
395         if(len<3) return; // we don't do names like .R or .L
396
397         strcpy(name, from_name);
398
399         /* We first check the case with a .### extension, let's find the last period */
400         if(isdigit(name[len-1])) {
401                 index= strrchr(name, '.'); // last occurrence
402                 if (index && isdigit(index[1]) ) { // doesnt handle case bone.1abc2 correct..., whatever!
403                         if(strip_number==0)
404                                 strcpy(number, index);
405                         *index= 0;
406                         len= strlen(name);
407                 }
408         }
409
410         strcpy (prefix, name);
411
412 #define IS_SEPARATOR(a) ((a)=='.' || (a)==' ' || (a)=='-' || (a)=='_')
413
414         /* first case; separator . - _ with extensions r R l L  */
415         if( IS_SEPARATOR(name[len-2]) ) {
416                 switch(name[len-1]) {
417                         case 'l':
418                                 prefix[len-1]= 0;
419                                 strcpy(replace, "r");
420                                 break;
421                         case 'r':
422                                 prefix[len-1]= 0;
423                                 strcpy(replace, "l");
424                                 break;
425                         case 'L':
426                                 prefix[len-1]= 0;
427                                 strcpy(replace, "R");
428                                 break;
429                         case 'R':
430                                 prefix[len-1]= 0;
431                                 strcpy(replace, "L");
432                                 break;
433                 }
434         }
435         /* case; beginning with r R l L , with separator after it */
436         else if( IS_SEPARATOR(name[1]) ) {
437                 switch(name[0]) {
438                         case 'l':
439                                 strcpy(replace, "r");
440                                 strcpy(suffix, name+1);
441                                 prefix[0]= 0;
442                                 break;
443                         case 'r':
444                                 strcpy(replace, "l");
445                                 strcpy(suffix, name+1);
446                                 prefix[0]= 0;
447                                 break;
448                         case 'L':
449                                 strcpy(replace, "R");
450                                 strcpy(suffix, name+1);
451                                 prefix[0]= 0;
452                                 break;
453                         case 'R':
454                                 strcpy(replace, "L");
455                                 strcpy(suffix, name+1);
456                                 prefix[0]= 0;
457                                 break;
458                 }
459         }
460         else if(len > 5) {
461                 /* hrms, why test for a separator? lets do the rule 'ultimate left or right' */
462                 index = BLI_strcasestr(prefix, "right");
463                 if (index==prefix || index==prefix+len-5) {
464                         if(index[0]=='r')
465                                 strcpy (replace, "left");
466                         else {
467                                 if(index[1]=='I')
468                                         strcpy (replace, "LEFT");
469                                 else
470                                         strcpy (replace, "Left");
471                         }
472                         *index= 0;
473                         strcpy (suffix, index+5);
474                 }
475                 else {
476                         index = BLI_strcasestr(prefix, "left");
477                         if (index==prefix || index==prefix+len-4) {
478                                 if(index[0]=='l')
479                                         strcpy (replace, "right");
480                                 else {
481                                         if(index[1]=='E')
482                                                 strcpy (replace, "RIGHT");
483                                         else
484                                                 strcpy (replace, "Right");
485                                 }
486                                 *index= 0;
487                                 strcpy (suffix, index+4);
488                         }
489                 }
490         }
491
492 #undef IS_SEPARATOR
493
494         sprintf (name, "%s%s%s%s", prefix, replace, suffix, number);
495 }
496
497 float defvert_find_weight(const struct MDeformVert *dvert, int group_num)
498 {
499         MDeformWeight *dw= defvert_find_index(dvert, group_num);
500         return dw ? dw->weight : 0.0f;
501 }
502
503 float defvert_array_find_weight_safe(const struct MDeformVert *dvert, int index, int group_num)
504 {
505         if(group_num == -1 || dvert == NULL)
506                 return 1.0f;
507
508         return defvert_find_weight(dvert+index, group_num);
509 }
510
511
512 MDeformWeight *defvert_find_index(const MDeformVert *dvert, int defgroup)
513 {
514         if(dvert && defgroup >= 0) {
515                 MDeformWeight *dw = dvert->dw;
516                 int i;
517
518                 for(i=dvert->totweight; i>0; i--, dw++)
519                         if(dw->def_nr == defgroup)
520                                 return dw;
521         }
522
523         return NULL;
524 }
525
526 /* Ensures that mv has a deform weight entry for the specified defweight group */
527 /* Note this function is mirrored in editmesh_tools.c, for use for editvertices */
528 MDeformWeight *defvert_verify_index(MDeformVert *dv, int defgroup)
529 {
530         MDeformWeight *newdw;
531
532         /* do this check always, this function is used to check for it */
533         if(!dv || defgroup<0)
534                 return NULL;
535
536         newdw = defvert_find_index(dv, defgroup);
537         if(newdw)
538                 return newdw;
539
540         newdw = MEM_callocN(sizeof(MDeformWeight)*(dv->totweight+1), "deformWeight");
541         if(dv->dw) {
542                 memcpy(newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight);
543                 MEM_freeN(dv->dw);
544         }
545         dv->dw=newdw;
546
547         dv->dw[dv->totweight].weight=0.0f;
548         dv->dw[dv->totweight].def_nr=defgroup;
549         /* Group index */
550
551         dv->totweight++;
552
553         return dv->dw+(dv->totweight-1);
554 }