panel for adjusting the active vertex groups weights
[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 copy_defgroups (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= copy_defgroup(defgroup);
78                 BLI_addtail(outbase, defgroupn);
79         }
80 }
81
82 bDeformGroup *copy_defgroup (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 void copy_defvert (MDeformVert *dvert_r, const MDeformVert *dvert)
100 {
101         if(dvert_r->totweight == dvert->totweight) {
102                 if(dvert->totweight)
103                         memcpy(dvert_r->dw, dvert->dw, dvert->totweight * sizeof(MDeformWeight));
104         }
105         else {
106                 if(dvert_r->dw)
107                         MEM_freeN(dvert_r->dw);
108
109                 if(dvert->totweight)
110                         dvert_r->dw= MEM_dupallocN(dvert->dw);
111                 else
112                         dvert_r->dw= NULL;
113
114                 dvert_r->totweight = dvert->totweight;
115         }
116 }
117
118 void normalize_defvert (MDeformVert *dvert)
119 {
120         if(dvert->totweight<=0) {
121                 /* nothing */
122         }
123         else if (dvert->totweight==1) {
124                 dvert->dw[0].weight= 1.0f;
125         }
126         else {
127                 int i;
128                 float tot= 0.0f;
129                 MDeformWeight *dw;
130                 for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++)
131                         tot += dw->weight;
132
133                 if(tot > 0.0f) {
134                         for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++)
135                                 dw->weight /= tot;
136                 }
137         }
138 }
139
140 void flip_defvert (MDeformVert *dvert, int *flip_map)
141 {
142         MDeformWeight *dw;
143         int i;
144
145         for(dw= dvert->dw, i=0; i<dvert->totweight; dw++, i++)
146                 if(flip_map[dw->def_nr] >= 0)
147                         dw->def_nr= flip_map[dw->def_nr];
148 }
149
150
151 bDeformGroup *get_named_vertexgroup (Object *ob, char *name)
152 {
153         /* return a pointer to the deform group with this name
154          * or return NULL otherwise.
155          */
156         bDeformGroup *curdef;
157
158         for (curdef = ob->defbase.first; curdef; curdef=curdef->next) {
159                 if (!strcmp(curdef->name, name)) {
160                         return curdef;
161                 }
162         }
163         return NULL;
164 }
165
166 int get_named_vertexgroup_num (Object *ob, const char *name)
167 {
168         /* Return the location of the named deform group within the list of
169          * deform groups. This function is a combination of get_defgroup_num and
170          * get_named_vertexgroup. The other two could be called instead, but that
171          * require looping over the vertexgroups twice.
172          */
173         bDeformGroup *curdef;
174         int def_nr;
175         
176         for (curdef=ob->defbase.first, def_nr=0; curdef; curdef=curdef->next, def_nr++) {
177                 if (!strcmp(curdef->name, name))
178                         return def_nr;
179         }
180         
181         return -1;
182 }
183
184 int get_defgroup_num (Object *ob, bDeformGroup *dg)
185 {
186         /* Fetch the location of this deform group
187          * within the linked list of deform groups.
188          * (this number is stored in the deform
189          * weights of the deform verts to link them
190          * to this deform group).
191          *
192          * note: this is zero based, ob->actdef starts at 1.
193          */
194
195         bDeformGroup *eg;
196         int def_nr;
197
198         eg = ob->defbase.first;
199         def_nr = 0;
200
201         /* loop through all deform groups */
202         while (eg != NULL) {
203
204                 /* if the current deform group is
205                  * the one we are after, return
206                  * def_nr
207                  */
208                 if (eg == dg) {
209                         break;
210                 }
211                 ++def_nr;
212                 eg = eg->next;
213         }
214
215         /* if there was no deform group found then
216          * return -1 (should set up a nice symbolic
217          * constant for this)
218          */
219         if (eg == NULL) return -1;
220         
221         return def_nr;
222     
223 }
224
225 /* note, must be freed */
226 int *get_defgroup_flip_map(Object *ob)
227 {
228         bDeformGroup *dg;
229         int totdg= BLI_countlist(&ob->defbase);
230
231         if(totdg==0) {
232                 return NULL;
233         }
234         else {
235                 char name[sizeof(dg->name)];
236                 int i, flip_num, *map= MEM_mallocN(totdg * sizeof(int), "get_defgroup_flip_map");
237                 memset(map, -1, totdg * sizeof(int));
238
239                 for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
240                         if(map[i] == -1) { /* may be calculated previously */
241                                 flip_vertexgroup_name(name, dg->name, 0);
242                                 if(strcmp(name, dg->name)) {
243                                         flip_num= get_named_vertexgroup_num(ob, name);
244                                         if(flip_num > -1) {
245                                                 map[i]= flip_num;
246                                                 map[flip_num]= i; /* save an extra lookup */
247                                         }
248                                 }
249                         }
250                 }
251                 return map;
252         }
253 }
254
255 void unique_vertexgroup_name (bDeformGroup *dg, Object *ob)
256 {
257         bDeformGroup *curdef;
258         int number;
259         int exists = 0;
260         char tempname[64];
261         char *dot;
262         
263         if (!ob)
264                 return;
265                 
266         /* See if we are given an empty string */
267         if (dg->name[0] == '\0') {
268                 /* give it default name first */
269                 strcpy (dg->name, "Group");
270         }       
271                 
272         /* See if we even need to do this */
273         for (curdef = ob->defbase.first; curdef; curdef=curdef->next) {
274                 if (dg!=curdef) {
275                         if (!strcmp(curdef->name, dg->name)) {
276                                 exists = 1;
277                                 break;
278                         }
279                 }
280         }
281         
282         if (!exists)
283                 return;
284
285         /*      Strip off the suffix */
286         dot=strchr(dg->name, '.');
287         if (dot)
288                 *dot=0;
289         
290         for (number = 1; number <=999; number++) {
291                 sprintf (tempname, "%s.%03d", dg->name, number);
292                 
293                 exists = 0;
294                 for (curdef=ob->defbase.first; curdef; curdef=curdef->next) {
295                         if (dg!=curdef) {
296                                 if (!strcmp (curdef->name, tempname)) {
297                                         exists = 1;
298                                         break;
299                                 }
300                         }
301                 }
302                 if (!exists) {
303                         BLI_strncpy (dg->name, tempname, 32);
304                         return;
305                 }
306         }       
307 }
308
309
310 /* finds the best possible flipped name. For renaming; check for unique names afterwards */
311 /* if strip_number: removes number extensions */
312 void flip_vertexgroup_name (char *name, const char *from_name, int strip_number)
313 {
314         int     len;
315         char    prefix[sizeof((bDeformGroup *)NULL)->name]={""};   /* The part before the facing */
316         char    suffix[sizeof((bDeformGroup *)NULL)->name]={""};   /* The part after the facing */
317         char    replace[sizeof((bDeformGroup *)NULL)->name]={""};  /* The replacement string */
318         char    number[sizeof((bDeformGroup *)NULL)->name]={""};   /* The number extension string */
319         char    *index=NULL;
320
321         len= strlen(from_name);
322         if(len<3) return; // we don't do names like .R or .L
323
324         strcpy(name, from_name);
325
326         /* We first check the case with a .### extension, let's find the last period */
327         if(isdigit(name[len-1])) {
328                 index= strrchr(name, '.'); // last occurrence
329                 if (index && isdigit(index[1]) ) { // doesnt handle case bone.1abc2 correct..., whatever!
330                         if(strip_number==0)
331                                 strcpy(number, index);
332                         *index= 0;
333                         len= strlen(name);
334                 }
335         }
336
337         strcpy (prefix, name);
338
339 #define IS_SEPARATOR(a) ((a)=='.' || (a)==' ' || (a)=='-' || (a)=='_')
340
341         /* first case; separator . - _ with extensions r R l L  */
342         if( IS_SEPARATOR(name[len-2]) ) {
343                 switch(name[len-1]) {
344                         case 'l':
345                                 prefix[len-1]= 0;
346                                 strcpy(replace, "r");
347                                 break;
348                         case 'r':
349                                 prefix[len-1]= 0;
350                                 strcpy(replace, "l");
351                                 break;
352                         case 'L':
353                                 prefix[len-1]= 0;
354                                 strcpy(replace, "R");
355                                 break;
356                         case 'R':
357                                 prefix[len-1]= 0;
358                                 strcpy(replace, "L");
359                                 break;
360                 }
361         }
362         /* case; beginning with r R l L , with separator after it */
363         else if( IS_SEPARATOR(name[1]) ) {
364                 switch(name[0]) {
365                         case 'l':
366                                 strcpy(replace, "r");
367                                 strcpy(suffix, name+1);
368                                 prefix[0]= 0;
369                                 break;
370                         case 'r':
371                                 strcpy(replace, "l");
372                                 strcpy(suffix, name+1);
373                                 prefix[0]= 0;
374                                 break;
375                         case 'L':
376                                 strcpy(replace, "R");
377                                 strcpy(suffix, name+1);
378                                 prefix[0]= 0;
379                                 break;
380                         case 'R':
381                                 strcpy(replace, "L");
382                                 strcpy(suffix, name+1);
383                                 prefix[0]= 0;
384                                 break;
385                 }
386         }
387         else if(len > 5) {
388                 /* hrms, why test for a separator? lets do the rule 'ultimate left or right' */
389                 index = BLI_strcasestr(prefix, "right");
390                 if (index==prefix || index==prefix+len-5) {
391                         if(index[0]=='r')
392                                 strcpy (replace, "left");
393                         else {
394                                 if(index[1]=='I')
395                                         strcpy (replace, "LEFT");
396                                 else
397                                         strcpy (replace, "Left");
398                         }
399                         *index= 0;
400                         strcpy (suffix, index+5);
401                 }
402                 else {
403                         index = BLI_strcasestr(prefix, "left");
404                         if (index==prefix || index==prefix+len-4) {
405                                 if(index[0]=='l')
406                                         strcpy (replace, "right");
407                                 else {
408                                         if(index[1]=='E')
409                                                 strcpy (replace, "RIGHT");
410                                         else
411                                                 strcpy (replace, "Right");
412                                 }
413                                 *index= 0;
414                                 strcpy (suffix, index+4);
415                         }
416                 }
417         }
418
419 #undef IS_SEPARATOR
420
421         sprintf (name, "%s%s%s%s", prefix, replace, suffix, number);
422 }
423
424
425
426 float deformvert_get_weight(const struct MDeformVert *dvert, int group_num)
427 {
428         if(dvert)
429         {
430                 const MDeformWeight *dw = dvert->dw;
431                 int i;
432
433                 for(i=dvert->totweight; i>0; i--, dw++)
434                         if(dw->def_nr == group_num)
435                                 return dw->weight;
436         }
437
438         /* Not found */
439         return 0.0;
440 }
441
442 float vertexgroup_get_vertex_weight(const struct MDeformVert *dvert, int index, int group_num)
443 {
444         if(group_num == -1 || dvert == NULL)
445                 return 1.0;
446
447         return deformvert_get_weight(dvert+index, group_num);
448 }
449