Merging r42602 through r42620 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenkernel / intern / deform.c
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) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Reevan McKay
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/deform.c
29  *  \ingroup bke
30  */
31
32
33 #include <string.h>
34 #include <math.h>
35 #include "ctype.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_meshdata_types.h"
40 #include "DNA_object_types.h"
41
42 #include "BKE_deform.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_utildefines.h"
46
47
48 void defgroup_copy_list(ListBase *outbase, ListBase *inbase)
49 {
50         bDeformGroup *defgroup, *defgroupn;
51
52         outbase->first= outbase->last= NULL;
53
54         for (defgroup = inbase->first; defgroup; defgroup=defgroup->next){
55                 defgroupn= defgroup_duplicate(defgroup);
56                 BLI_addtail(outbase, defgroupn);
57         }
58 }
59
60 bDeformGroup *defgroup_duplicate(bDeformGroup *ingroup)
61 {
62         bDeformGroup *outgroup;
63
64         if (!ingroup)
65                 return NULL;
66
67         outgroup=MEM_callocN(sizeof(bDeformGroup), "copy deformGroup");
68
69         /* For now, just copy everything over. */
70         memcpy (outgroup, ingroup, sizeof(bDeformGroup));
71
72         outgroup->next=outgroup->prev=NULL;
73
74         return outgroup;
75 }
76
77 /* copy & overwrite weights */
78 void defvert_copy(MDeformVert *dvert_dst, const MDeformVert *dvert_src)
79 {
80         if (dvert_dst->totweight == dvert_src->totweight) {
81                 if (dvert_src->totweight)
82                         memcpy(dvert_dst->dw, dvert_src->dw, dvert_src->totweight * sizeof(MDeformWeight));
83         }
84         else {
85                 if (dvert_dst->dw)
86                         MEM_freeN(dvert_dst->dw);
87
88                 if (dvert_src->totweight)
89                         dvert_dst->dw= MEM_dupallocN(dvert_src->dw);
90                 else
91                         dvert_dst->dw= NULL;
92
93                 dvert_dst->totweight = dvert_src->totweight;
94         }
95 }
96
97 /* copy an index from one dvert to another
98  * - do nothing if neither are set.
99  * - add destination weight if needed.
100  */
101 void defvert_copy_index(MDeformVert *dvert_dst, const MDeformVert *dvert_src, const int defgroup)
102 {
103         MDeformWeight *dw_src, *dw_dst;
104
105         dw_src= defvert_find_index(dvert_src, defgroup);
106
107         if (dw_src) {
108                 /* source is valid, verify destination */
109                 dw_dst= defvert_verify_index(dvert_dst, defgroup);
110                 dw_dst->weight= dw_src->weight;
111         }
112         else {
113                 /* source was NULL, assign zero, could also remove */
114                 dw_dst= defvert_find_index(dvert_dst, defgroup);
115
116                 if (dw_dst) {
117                         dw_dst->weight= 0.0f;
118                 }
119         }
120 }
121
122 /* only sync over matching weights, don't add or remove groups
123  * warning, loop within loop.
124  */
125 void defvert_sync(MDeformVert *dvert_dst, const MDeformVert *dvert_src, int use_verify)
126 {
127         if (dvert_src->totweight && dvert_dst->totweight) {
128                 int i;
129                 MDeformWeight *dw_src;
130                 for (i=0, dw_src=dvert_src->dw; i < dvert_src->totweight; i++, dw_src++) {
131                         MDeformWeight *dw_dst;
132                         if (use_verify) dw_dst= defvert_verify_index(dvert_dst, dw_src->def_nr);
133                         else            dw_dst= defvert_find_index(dvert_dst, dw_src->def_nr);
134
135                         if (dw_dst) {
136                                 dw_dst->weight= dw_src->weight;
137                         }
138                 }
139         }
140 }
141
142 /* be sure all flip_map values are valid */
143 void defvert_sync_mapped(MDeformVert *dvert_dst, const MDeformVert *dvert_src,
144                          const int *flip_map, const int flip_map_len, const int use_verify)
145 {
146         if (dvert_src->totweight && dvert_dst->totweight) {
147                 int i;
148                 MDeformWeight *dw_src;
149                 for (i=0, dw_src=dvert_src->dw; i < dvert_src->totweight; i++, dw_src++) {
150                         if (dw_src->def_nr < flip_map_len) {
151                                 MDeformWeight *dw_dst;
152                                 if (use_verify) dw_dst= defvert_verify_index(dvert_dst, flip_map[dw_src->def_nr]);
153                                 else            dw_dst= defvert_find_index(dvert_dst, flip_map[dw_src->def_nr]);
154
155                                 if (dw_dst) {
156                                         dw_dst->weight= dw_src->weight;
157                                 }
158                         }
159                 }
160         }
161 }
162
163 /* be sure all flip_map values are valid */
164 void defvert_remap(MDeformVert *dvert, int *map, const int map_len)
165 {
166         MDeformWeight *dw;
167         int i;
168         for (i=0, dw=dvert->dw; i<dvert->totweight; i++, dw++) {
169                 if (dw->def_nr < map_len) {
170                         dw->def_nr= map[dw->def_nr];
171                 }
172         }
173 }
174
175 void defvert_normalize(MDeformVert *dvert)
176 {
177         if (dvert->totweight<=0) {
178                 /* nothing */
179         }
180         else if (dvert->totweight==1) {
181                 dvert->dw[0].weight= 1.0f;
182         }
183         else {
184                 int i;
185                 float tot= 0.0f;
186                 MDeformWeight *dw;
187                 for (i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++)
188                         tot += dw->weight;
189
190                 if (tot > 0.0f) {
191                         for (i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++)
192                                 dw->weight /= tot;
193                 }
194         }
195 }
196
197 void defvert_flip(MDeformVert *dvert, const int *flip_map, const int flip_map_len)
198 {
199         MDeformWeight *dw;
200         int i;
201
202         for (dw= dvert->dw, i=0; i<dvert->totweight; dw++, i++) {
203                 if (dw->def_nr < flip_map_len) {
204                         if (flip_map[dw->def_nr] >= 0) {
205                                 dw->def_nr= flip_map[dw->def_nr];
206                         }
207                 }
208         }
209 }
210
211
212 bDeformGroup *defgroup_find_name(Object *ob, const char *name)
213 {
214         /* return a pointer to the deform group with this name
215          * or return NULL otherwise.
216          */
217         bDeformGroup *curdef;
218
219         for (curdef = ob->defbase.first; curdef; curdef=curdef->next) {
220                 if (!strcmp(curdef->name, name)) {
221                         return curdef;
222                 }
223         }
224         return NULL;
225 }
226
227 int defgroup_name_index(Object *ob, const char *name)
228 {
229         /* Return the location of the named deform group within the list of
230          * deform groups. This function is a combination of defgroup_find_index and
231          * defgroup_find_name. The other two could be called instead, but that
232          * require looping over the vertexgroups twice.
233          */
234         bDeformGroup *curdef;
235         int def_nr;
236
237         if (name && name[0] != '\0') {
238                 for (curdef=ob->defbase.first, def_nr=0; curdef; curdef=curdef->next, def_nr++) {
239                         if (!strcmp(curdef->name, name))
240                                 return def_nr;
241                 }
242         }
243
244         return -1;
245 }
246
247 int defgroup_find_index(Object *ob, bDeformGroup *dg)
248 {
249         /* Fetch the location of this deform group
250          * within the linked list of deform groups.
251          * (this number is stored in the deform
252          * weights of the deform verts to link them
253          * to this deform group).
254          *
255          * note: this is zero based, ob->actdef starts at 1.
256          */
257
258         bDeformGroup *eg;
259         int def_nr;
260
261         eg = ob->defbase.first;
262         def_nr = 0;
263
264         /* loop through all deform groups */
265         while (eg != NULL) {
266
267                 /* if the current deform group is
268                  * the one we are after, return
269                  * def_nr
270                  */
271                 if (eg == dg) {
272                         break;
273                 }
274                 ++def_nr;
275                 eg = eg->next;
276         }
277
278         /* if there was no deform group found then
279          * return -1 (should set up a nice symbolic
280          * constant for this)
281          */
282         if (eg == NULL) return -1;
283
284         return def_nr;
285 }
286
287 /* note, must be freed */
288 int *defgroup_flip_map(Object *ob, int *flip_map_len, int use_default)
289 {
290         int defbase_tot= *flip_map_len= BLI_countlist(&ob->defbase);
291
292         if (defbase_tot==0) {
293                 return NULL;
294         }
295         else {
296                 bDeformGroup *dg;
297                 char name[sizeof(dg->name)];
298                 int i, flip_num, *map= MEM_mallocN(defbase_tot * sizeof(int), __func__);
299
300                 for (i=0; i < defbase_tot; i++) {
301                         map[i]= -1;
302                 }
303
304                 for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
305                         if (map[i] == -1) { /* may be calculated previously */
306
307                                 /* incase no valid value is found, use this */
308                                 if (use_default)
309                                         map[i]= i;
310
311                                 flip_side_name(name, dg->name, FALSE);
312                                 if (strcmp(name, dg->name)) {
313                                         flip_num= defgroup_name_index(ob, name);
314                                         if (flip_num >= 0) {
315                                                 map[i]= flip_num;
316                                                 map[flip_num]= i; /* save an extra lookup */
317                                         }
318                                 }
319                         }
320                 }
321                 return map;
322         }
323 }
324
325 /* note, must be freed */
326 int *defgroup_flip_map_single(Object *ob, int *flip_map_len, int use_default, int defgroup)
327 {
328         int defbase_tot= *flip_map_len= BLI_countlist(&ob->defbase);
329
330         if (defbase_tot==0) {
331                 return NULL;
332         }
333         else {
334                 bDeformGroup *dg;
335                 char name[sizeof(dg->name)];
336                 int i, flip_num, *map= MEM_mallocN(defbase_tot * sizeof(int), __func__);
337
338                 for (i=0; i < defbase_tot; i++) {
339                         if (use_default) map[i]= i;
340                         else             map[i]= -1;
341                 }
342
343                 dg= BLI_findlink(&ob->defbase, defgroup);
344
345                 flip_side_name(name, dg->name, FALSE);
346                 if (strcmp(name, dg->name)) {
347                         flip_num= defgroup_name_index(ob, name);
348
349                         if (flip_num >= 0) {
350                                 map[defgroup]= flip_num;
351                                 map[flip_num]= defgroup;
352                         }
353                 }
354
355                 return map;
356         }
357 }
358
359 int defgroup_flip_index(Object *ob, int index, int use_default)
360 {
361         bDeformGroup *dg= BLI_findlink(&ob->defbase, index);
362         int flip_index = -1;
363
364         if (dg) {
365                 char name[sizeof(dg->name)];
366                 flip_side_name(name, dg->name, 0);
367
368                 if (strcmp(name, dg->name))
369                         flip_index= defgroup_name_index(ob, name);
370         }
371
372         return (flip_index==-1 && use_default) ? index : flip_index;
373 }
374
375 static int defgroup_find_name_dupe(const char *name, bDeformGroup *dg, Object *ob)
376 {
377         bDeformGroup *curdef;
378
379         for (curdef = ob->defbase.first; curdef; curdef=curdef->next) {
380                 if (dg!=curdef) {
381                         if (!strcmp(curdef->name, name)) {
382                                 return 1;
383                         }
384                 }
385         }
386
387         return 0;
388 }
389
390 static int defgroup_unique_check(void *arg, const char *name)
391 {
392         struct {Object *ob; void *dg;} *data= arg;
393         return defgroup_find_name_dupe(name, data->dg, data->ob);
394 }
395
396 void defgroup_unique_name(bDeformGroup *dg, Object *ob)
397 {
398         struct {Object *ob; void *dg;} data;
399         data.ob= ob;
400         data.dg= dg;
401
402         BLI_uniquename_cb(defgroup_unique_check, &data, "Group", '.', dg->name, sizeof(dg->name));
403 }
404
405 /* finds the best possible flipped name. For renaming; check for unique names afterwards */
406 /* if strip_number: removes number extensions
407  * note: dont use sizeof() for 'name' or 'from_name' */
408 void flip_side_name(char name[MAX_VGROUP_NAME], const char from_name[MAX_VGROUP_NAME], int strip_number)
409 {
410         int     len;
411         char    prefix[MAX_VGROUP_NAME]=  "";   /* The part before the facing */
412         char    suffix[MAX_VGROUP_NAME]=  "";   /* The part after the facing */
413         char    replace[MAX_VGROUP_NAME]= "";   /* The replacement string */
414         char    number[MAX_VGROUP_NAME]=  "";   /* The number extension string */
415         char    *index=NULL;
416
417         /* always copy the name, since this can be called with an uninitialized string */
418         BLI_strncpy(name, from_name, MAX_VGROUP_NAME);
419
420         len= BLI_strnlen(from_name, MAX_VGROUP_NAME);
421         if (len < 3) {
422                 /* we don't do names like .R or .L */
423                 return;
424         }
425
426         /* We first check the case with a .### extension, let's find the last period */
427         if (isdigit(name[len-1])) {
428                 index= strrchr(name, '.'); // last occurrence
429                 if (index && isdigit(index[1]) ) { // doesnt handle case bone.1abc2 correct..., whatever!
430                         if (strip_number==0)
431                                 BLI_strncpy(number, index, sizeof(number));
432                         *index= 0;
433                         len= BLI_strnlen(name, MAX_VGROUP_NAME);
434                 }
435         }
436
437         BLI_strncpy(prefix, name, sizeof(prefix));
438
439 #define IS_SEPARATOR(a) ((a)=='.' || (a)==' ' || (a)=='-' || (a)=='_')
440
441         /* first case; separator . - _ with extensions r R l L  */
442         if (IS_SEPARATOR(name[len-2]) ) {
443                 switch(name[len-1]) {
444                         case 'l':
445                                 prefix[len-1]= 0;
446                                 strcpy(replace, "r");
447                                 break;
448                         case 'r':
449                                 prefix[len-1]= 0;
450                                 strcpy(replace, "l");
451                                 break;
452                         case 'L':
453                                 prefix[len-1]= 0;
454                                 strcpy(replace, "R");
455                                 break;
456                         case 'R':
457                                 prefix[len-1]= 0;
458                                 strcpy(replace, "L");
459                                 break;
460                 }
461         }
462         /* case; beginning with r R l L , with separator after it */
463         else if (IS_SEPARATOR(name[1]) ) {
464                 switch(name[0]) {
465                         case 'l':
466                                 strcpy(replace, "r");
467                                 strcpy(suffix, name+1);
468                                 prefix[0]= 0;
469                                 break;
470                         case 'r':
471                                 strcpy(replace, "l");
472                                 strcpy(suffix, name+1);
473                                 prefix[0]= 0;
474                                 break;
475                         case 'L':
476                                 strcpy(replace, "R");
477                                 strcpy(suffix, name+1);
478                                 prefix[0]= 0;
479                                 break;
480                         case 'R':
481                                 strcpy(replace, "L");
482                                 strcpy(suffix, name+1);
483                                 prefix[0]= 0;
484                                 break;
485                 }
486         }
487         else if (len > 5) {
488                 /* hrms, why test for a separator? lets do the rule 'ultimate left or right' */
489                 index = BLI_strcasestr(prefix, "right");
490                 if (index==prefix || index==prefix+len-5) {
491                         if (index[0]=='r')
492                                 strcpy (replace, "left");
493                         else {
494                                 if (index[1]=='I')
495                                         strcpy (replace, "LEFT");
496                                 else
497                                         strcpy (replace, "Left");
498                         }
499                         *index= 0;
500                         strcpy (suffix, index+5);
501                 }
502                 else {
503                         index = BLI_strcasestr(prefix, "left");
504                         if (index==prefix || index==prefix+len-4) {
505                                 if (index[0]=='l')
506                                         strcpy (replace, "right");
507                                 else {
508                                         if (index[1]=='E')
509                                                 strcpy (replace, "RIGHT");
510                                         else
511                                                 strcpy (replace, "Right");
512                                 }
513                                 *index= 0;
514                                 strcpy (suffix, index+4);
515                         }
516                 }
517         }
518
519 #undef IS_SEPARATOR
520
521         BLI_snprintf (name, MAX_VGROUP_NAME, "%s%s%s%s", prefix, replace, suffix, number);
522 }
523
524 float defvert_find_weight(const struct MDeformVert *dvert, const int defgroup)
525 {
526         MDeformWeight *dw= defvert_find_index(dvert, defgroup);
527         return dw ? dw->weight : 0.0f;
528 }
529
530 float defvert_array_find_weight_safe(const struct MDeformVert *dvert, const int index, const int defgroup)
531 {
532         if (defgroup == -1 || dvert == NULL)
533                 return 1.0f;
534
535         return defvert_find_weight(dvert+index, defgroup);
536 }
537
538
539 MDeformWeight *defvert_find_index(const MDeformVert *dvert, const int defgroup)
540 {
541         if (dvert && defgroup >= 0) {
542                 MDeformWeight *dw = dvert->dw;
543                 int i;
544
545                 for (i=dvert->totweight; i>0; i--, dw++) {
546                         if (dw->def_nr == defgroup) {
547                                 return dw;
548                         }
549                 }
550         }
551
552         return NULL;
553 }
554
555 /* Ensures that mv has a deform weight entry for the specified defweight group */
556 /* Note this function is mirrored in editmesh_tools.c, for use for editvertices */
557 MDeformWeight *defvert_verify_index(MDeformVert *dvert, const int defgroup)
558 {
559         MDeformWeight *dw_new;
560
561         /* do this check always, this function is used to check for it */
562         if (!dvert || defgroup < 0)
563                 return NULL;
564
565         dw_new= defvert_find_index(dvert, defgroup);
566         if (dw_new)
567                 return dw_new;
568
569         dw_new= MEM_callocN(sizeof(MDeformWeight)*(dvert->totweight+1), "deformWeight");
570         if (dvert->dw) {
571                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight)*dvert->totweight);
572                 MEM_freeN(dvert->dw);
573         }
574         dvert->dw= dw_new;
575         dw_new += dvert->totweight;
576         dw_new->weight= 0.0f;
577         dw_new->def_nr= defgroup;
578         /* Group index */
579
580         dvert->totweight++;
581
582         return dw_new;
583 }
584
585 /* TODO. merge with code above! */
586
587 /* Adds the given vertex to the specified vertex group, with given weight.
588  * warning, this does NOT check for existign, assume caller already knows its not there */
589 void defvert_add_index_notest(MDeformVert *dvert, int defgroup, const float weight)
590 {
591         MDeformWeight *dw_new;
592
593         /* do this check always, this function is used to check for it */
594         if (!dvert || defgroup < 0)
595                 return;
596
597         dw_new = MEM_callocN(sizeof(MDeformWeight)*(dvert->totweight+1), "defvert_add_to group, new deformWeight");
598         if(dvert->dw) {
599                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight)*dvert->totweight);
600                 MEM_freeN(dvert->dw);
601         }
602         dvert->dw = dw_new;
603         dw_new += dvert->totweight;
604         dw_new->weight = weight;
605         dw_new->def_nr = defgroup;
606         dvert->totweight++;
607 }
608
609
610 /* Removes the given vertex from the vertex group.
611  * WARNING: This function frees the given MDeformWeight, do not use it afterward! */
612 void defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
613 {
614         if (dvert && dw) {
615                 MDeformWeight *dw_new;
616                 int i = dw - dvert->dw;
617
618                 /* Security check! */
619                 if(i < 0 || i >= dvert->totweight) {
620                         return;
621                 }
622
623                 dvert->totweight--;
624                 /* If there are still other deform weights attached to this vert then remove
625                  * this deform weight, and reshuffle the others.
626                  */
627                 if (dvert->totweight) {
628                         dw_new = MEM_mallocN(sizeof(MDeformWeight)*(dvert->totweight), __func__);
629                         if (dvert->dw){
630                                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight)*i);
631                                 memcpy(dw_new+i, dvert->dw+i+1, sizeof(MDeformWeight)*(dvert->totweight-i));
632                                 MEM_freeN(dvert->dw);
633                         }
634                         dvert->dw = dw_new;
635                 }
636                 else {
637                         /* If there are no other deform weights left then just remove this one. */
638                         MEM_freeN(dvert->dw);
639                         dvert->dw = NULL;
640                 }
641         }
642 }