compile fix: linux BLI_gzopen declare was conflicting.
[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= dvert->dw;
167         unsigned int i;
168         for (i= dvert->totweight; i != 0; i--, dw++) {
169                 if (dw->def_nr < map_len) {
170                         dw->def_nr= map[dw->def_nr];
171
172                         /* just in case */
173                         BLI_assert(dw->def_nr >= 0);
174                 }
175         }
176 }
177
178 void defvert_normalize(MDeformVert *dvert)
179 {
180         if (dvert->totweight <= 0) {
181                 /* nothing */
182         }
183         else if (dvert->totweight==1) {
184                 dvert->dw[0].weight= 1.0f;
185         }
186         else {
187                 MDeformWeight *dw;
188                 unsigned int i;
189                 float tot_weight= 0.0f;
190
191                 for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
192                         tot_weight += dw->weight;
193                 }
194
195                 if (tot_weight > 0.0f) {
196                         float scalar= 1.0f / tot_weight;
197                         for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
198                                 dw->weight *= scalar;
199
200                                 /* in case of division errors with very low weights */
201                                 CLAMP(dw->weight, 0.0f, 1.0f);
202                         }
203                 }
204         }
205 }
206
207 void defvert_normalize_lock(MDeformVert *dvert, const int def_nr_lock)
208 {
209         if (dvert->totweight <= 0) {
210                 /* nothing */
211         }
212         else if (dvert->totweight==1) {
213                 dvert->dw[0].weight= 1.0f;
214         }
215         else {
216                 MDeformWeight *dw_lock = NULL;
217                 MDeformWeight *dw;
218                 unsigned int i;
219                 float tot_weight= 0.0f;
220                 float lock_iweight= 1.0f;
221
222                 for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
223                         if(dw->def_nr != def_nr_lock) {
224                                 tot_weight += dw->weight;
225                         }
226                         else {
227                                 dw_lock= dw;
228                                 lock_iweight = (1.0f - dw_lock->weight);
229                                 CLAMP(lock_iweight, 0.0f, 1.0f);
230                         }
231                 }
232
233                 if (tot_weight > 0.0f) {
234                         /* paranoid, should be 1.0 but in case of float error clamp anyway */
235
236                         float scalar= (1.0f / tot_weight) * lock_iweight;
237                         for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
238                                 if(dw != dw_lock) {
239                                         dw->weight *= scalar;
240
241                                         /* in case of division errors with very low weights */
242                                         CLAMP(dw->weight, 0.0f, 1.0f);
243                                 }
244                         }
245                 }
246         }
247 }
248
249 void defvert_flip(MDeformVert *dvert, const int *flip_map, const int flip_map_len)
250 {
251         MDeformWeight *dw;
252         int i;
253
254         for (dw= dvert->dw, i=0; i<dvert->totweight; dw++, i++) {
255                 if (dw->def_nr < flip_map_len) {
256                         if (flip_map[dw->def_nr] >= 0) {
257                                 dw->def_nr= flip_map[dw->def_nr];
258                         }
259                 }
260         }
261 }
262
263 void defvert_flip_merged(MDeformVert *dvert, const int *flip_map, const int flip_map_len)
264 {
265         MDeformWeight *dw, *dw_cpy;
266         float weight;
267         int i, totweight = dvert->totweight;
268
269         /* copy weights */
270         for (dw = dvert->dw, i=0; i < totweight; dw++, i++) {
271                 if (dw->def_nr < flip_map_len) {
272                         if (flip_map[dw->def_nr] >= 0) {
273                                 /* error checkers complain of this but we'll never get NULL return */
274                                 dw_cpy = defvert_verify_index(dvert, flip_map[dw->def_nr]);
275                                 dw = &dvert->dw[i]; /* in case array got realloced */
276
277                                 /* distribute weights: if only one of the vertex groups was
278                                  * assigned this will halve the weights, otherwise it gets
279                                  * evened out. this keeps it proportional to other groups */
280                                 weight = 0.5f * (dw_cpy->weight + dw->weight);
281                                 dw_cpy->weight = weight;
282                                 dw->weight = weight;
283                         }
284                 }
285         }
286 }
287
288 bDeformGroup *defgroup_find_name(Object *ob, const char *name)
289 {
290         /* return a pointer to the deform group with this name
291          * or return NULL otherwise.
292          */
293         bDeformGroup *curdef;
294
295         for (curdef = ob->defbase.first; curdef; curdef=curdef->next) {
296                 if (!strcmp(curdef->name, name)) {
297                         return curdef;
298                 }
299         }
300         return NULL;
301 }
302
303 int defgroup_name_index(Object *ob, const char *name)
304 {
305         /* Return the location of the named deform group within the list of
306          * deform groups. This function is a combination of BLI_findlink and
307          * defgroup_find_name. The other two could be called instead, but that
308          * require looping over the vertexgroups twice.
309          */
310         bDeformGroup *curdef;
311         int def_nr;
312
313         if (name && name[0] != '\0') {
314                 for (curdef=ob->defbase.first, def_nr=0; curdef; curdef=curdef->next, def_nr++) {
315                         if (!strcmp(curdef->name, name))
316                                 return def_nr;
317                 }
318         }
319
320         return -1;
321 }
322
323 /* note, must be freed */
324 int *defgroup_flip_map(Object *ob, int *flip_map_len, int use_default)
325 {
326         int defbase_tot= *flip_map_len= BLI_countlist(&ob->defbase);
327
328         if (defbase_tot==0) {
329                 return NULL;
330         }
331         else {
332                 bDeformGroup *dg;
333                 char name[sizeof(dg->name)];
334                 int i, flip_num, *map= MEM_mallocN(defbase_tot * sizeof(int), __func__);
335
336                 for (i=0; i < defbase_tot; i++) {
337                         map[i]= -1;
338                 }
339
340                 for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
341                         if (map[i] == -1) { /* may be calculated previously */
342
343                                 /* in case no valid value is found, use this */
344                                 if (use_default)
345                                         map[i]= i;
346
347                                 flip_side_name(name, dg->name, FALSE);
348                                 if (strcmp(name, dg->name)) {
349                                         flip_num= defgroup_name_index(ob, name);
350                                         if (flip_num >= 0) {
351                                                 map[i]= flip_num;
352                                                 map[flip_num]= i; /* save an extra lookup */
353                                         }
354                                 }
355                         }
356                 }
357                 return map;
358         }
359 }
360
361 /* note, must be freed */
362 int *defgroup_flip_map_single(Object *ob, int *flip_map_len, int use_default, int defgroup)
363 {
364         int defbase_tot= *flip_map_len= BLI_countlist(&ob->defbase);
365
366         if (defbase_tot==0) {
367                 return NULL;
368         }
369         else {
370                 bDeformGroup *dg;
371                 char name[sizeof(dg->name)];
372                 int i, flip_num, *map= MEM_mallocN(defbase_tot * sizeof(int), __func__);
373
374                 for (i=0; i < defbase_tot; i++) {
375                         if (use_default) map[i]= i;
376                         else             map[i]= -1;
377                 }
378
379                 dg= BLI_findlink(&ob->defbase, defgroup);
380
381                 flip_side_name(name, dg->name, FALSE);
382                 if (strcmp(name, dg->name)) {
383                         flip_num= defgroup_name_index(ob, name);
384
385                         if (flip_num >= 0) {
386                                 map[defgroup]= flip_num;
387                                 map[flip_num]= defgroup;
388                         }
389                 }
390
391                 return map;
392         }
393 }
394
395 int defgroup_flip_index(Object *ob, int index, int use_default)
396 {
397         bDeformGroup *dg= BLI_findlink(&ob->defbase, index);
398         int flip_index = -1;
399
400         if (dg) {
401                 char name[sizeof(dg->name)];
402                 flip_side_name(name, dg->name, 0);
403
404                 if (strcmp(name, dg->name))
405                         flip_index= defgroup_name_index(ob, name);
406         }
407
408         return (flip_index==-1 && use_default) ? index : flip_index;
409 }
410
411 static int defgroup_find_name_dupe(const char *name, bDeformGroup *dg, Object *ob)
412 {
413         bDeformGroup *curdef;
414
415         for (curdef = ob->defbase.first; curdef; curdef=curdef->next) {
416                 if (dg!=curdef) {
417                         if (!strcmp(curdef->name, name)) {
418                                 return 1;
419                         }
420                 }
421         }
422
423         return 0;
424 }
425
426 static int defgroup_unique_check(void *arg, const char *name)
427 {
428         struct {Object *ob; void *dg;} *data= arg;
429         return defgroup_find_name_dupe(name, data->dg, data->ob);
430 }
431
432 void defgroup_unique_name(bDeformGroup *dg, Object *ob)
433 {
434         struct {Object *ob; void *dg;} data;
435         data.ob= ob;
436         data.dg= dg;
437
438         BLI_uniquename_cb(defgroup_unique_check, &data, "Group", '.', dg->name, sizeof(dg->name));
439 }
440
441 /* finds the best possible flipped name. For renaming; check for unique names afterwards */
442 /* if strip_number: removes number extensions
443  * note: don't use sizeof() for 'name' or 'from_name' */
444 void flip_side_name(char name[MAX_VGROUP_NAME], const char from_name[MAX_VGROUP_NAME], int strip_number)
445 {
446         int     len;
447         char    prefix[MAX_VGROUP_NAME]=  "";   /* The part before the facing */
448         char    suffix[MAX_VGROUP_NAME]=  "";   /* The part after the facing */
449         char    replace[MAX_VGROUP_NAME]= "";   /* The replacement string */
450         char    number[MAX_VGROUP_NAME]=  "";   /* The number extension string */
451         char    *index=NULL;
452
453         /* always copy the name, since this can be called with an uninitialized string */
454         BLI_strncpy(name, from_name, MAX_VGROUP_NAME);
455
456         len= BLI_strnlen(from_name, MAX_VGROUP_NAME);
457         if (len < 3) {
458                 /* we don't do names like .R or .L */
459                 return;
460         }
461
462         /* We first check the case with a .### extension, let's find the last period */
463         if (isdigit(name[len-1])) {
464                 index= strrchr(name, '.'); // last occurrence
465                 if (index && isdigit(index[1]) ) { // doesnt handle case bone.1abc2 correct..., whatever!
466                         if (strip_number==0)
467                                 BLI_strncpy(number, index, sizeof(number));
468                         *index= 0;
469                         len= BLI_strnlen(name, MAX_VGROUP_NAME);
470                 }
471         }
472
473         BLI_strncpy(prefix, name, sizeof(prefix));
474
475 #define IS_SEPARATOR(a) ((a)=='.' || (a)==' ' || (a)=='-' || (a)=='_')
476
477         /* first case; separator . - _ with extensions r R l L  */
478         if (IS_SEPARATOR(name[len-2]) ) {
479                 switch(name[len-1]) {
480                         case 'l':
481                                 prefix[len-1]= 0;
482                                 strcpy(replace, "r");
483                                 break;
484                         case 'r':
485                                 prefix[len-1]= 0;
486                                 strcpy(replace, "l");
487                                 break;
488                         case 'L':
489                                 prefix[len-1]= 0;
490                                 strcpy(replace, "R");
491                                 break;
492                         case 'R':
493                                 prefix[len-1]= 0;
494                                 strcpy(replace, "L");
495                                 break;
496                 }
497         }
498         /* case; beginning with r R l L , with separator after it */
499         else if (IS_SEPARATOR(name[1]) ) {
500                 switch(name[0]) {
501                         case 'l':
502                                 strcpy(replace, "r");
503                                 BLI_strncpy(suffix, name+1, sizeof(suffix));
504                                 prefix[0]= 0;
505                                 break;
506                         case 'r':
507                                 strcpy(replace, "l");
508                                 BLI_strncpy(suffix, name+1, sizeof(suffix));
509                                 prefix[0]= 0;
510                                 break;
511                         case 'L':
512                                 strcpy(replace, "R");
513                                 BLI_strncpy(suffix, name+1, sizeof(suffix));
514                                 prefix[0]= 0;
515                                 break;
516                         case 'R':
517                                 strcpy(replace, "L");
518                                 BLI_strncpy(suffix, name+1, sizeof(suffix));
519                                 prefix[0]= 0;
520                                 break;
521                 }
522         }
523         else if (len > 5) {
524                 /* hrms, why test for a separator? lets do the rule 'ultimate left or right' */
525                 index = BLI_strcasestr(prefix, "right");
526                 if (index==prefix || index==prefix+len-5) {
527                         if (index[0]=='r')
528                                 strcpy(replace, "left");
529                         else {
530                                 if (index[1]=='I')
531                                         strcpy(replace, "LEFT");
532                                 else
533                                         strcpy(replace, "Left");
534                         }
535                         *index= 0;
536                         BLI_strncpy(suffix, index+5, sizeof(suffix));
537                 }
538                 else {
539                         index = BLI_strcasestr(prefix, "left");
540                         if (index==prefix || index==prefix+len-4) {
541                                 if (index[0]=='l')
542                                         strcpy(replace, "right");
543                                 else {
544                                         if (index[1]=='E')
545                                                 strcpy(replace, "RIGHT");
546                                         else
547                                                 strcpy(replace, "Right");
548                                 }
549                                 *index= 0;
550                                 BLI_strncpy(suffix, index + 4, sizeof(suffix));
551                         }
552                 }
553         }
554
555 #undef IS_SEPARATOR
556
557         BLI_snprintf (name, MAX_VGROUP_NAME, "%s%s%s%s", prefix, replace, suffix, number);
558 }
559
560 float defvert_find_weight(const struct MDeformVert *dvert, const int defgroup)
561 {
562         MDeformWeight *dw= defvert_find_index(dvert, defgroup);
563         return dw ? dw->weight : 0.0f;
564 }
565
566 /* take care with this the rationale is:
567  * - if the object has no vertex group. act like vertex group isn't set and return 1.0,
568  * - if the vertex group exists but the 'defgroup' isn't found on this vertex, _still_ return 0.0
569  *
570  * This is a bit confusing, just saves some checks from the caller.
571  */
572 float defvert_array_find_weight_safe(const struct MDeformVert *dvert, const int index, const int defgroup)
573 {
574         if (defgroup == -1 || dvert == NULL)
575                 return 1.0f;
576
577         return defvert_find_weight(dvert+index, defgroup);
578 }
579
580
581 MDeformWeight *defvert_find_index(const MDeformVert *dvert, const int defgroup)
582 {
583         if (dvert && defgroup >= 0) {
584                 MDeformWeight *dw = dvert->dw;
585                 unsigned int i;
586
587                 for (i= dvert->totweight; i != 0; i--, dw++) {
588                         if (dw->def_nr == defgroup) {
589                                 return dw;
590                         }
591                 }
592         }
593
594         return NULL;
595 }
596
597 /* Ensures that mv has a deform weight entry for the specified defweight group */
598 /* Note this function is mirrored in editmesh_tools.c, for use for editvertices */
599 MDeformWeight *defvert_verify_index(MDeformVert *dvert, const int defgroup)
600 {
601         MDeformWeight *dw_new;
602
603         /* do this check always, this function is used to check for it */
604         if (!dvert || defgroup < 0)
605                 return NULL;
606
607         dw_new= defvert_find_index(dvert, defgroup);
608         if (dw_new)
609                 return dw_new;
610
611         dw_new= MEM_callocN(sizeof(MDeformWeight)*(dvert->totweight+1), "deformWeight");
612         if (dvert->dw) {
613                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight)*dvert->totweight);
614                 MEM_freeN(dvert->dw);
615         }
616         dvert->dw= dw_new;
617         dw_new += dvert->totweight;
618         dw_new->weight= 0.0f;
619         dw_new->def_nr= defgroup;
620         /* Group index */
621
622         dvert->totweight++;
623
624         return dw_new;
625 }
626
627 /* TODO. merge with code above! */
628
629 /* Adds the given vertex to the specified vertex group, with given weight.
630  * warning, this does NOT check for existing, assume caller already knows its not there */
631 void defvert_add_index_notest(MDeformVert *dvert, int defgroup, const float weight)
632 {
633         MDeformWeight *dw_new;
634
635         /* do this check always, this function is used to check for it */
636         if (!dvert || defgroup < 0)
637                 return;
638
639         dw_new = MEM_callocN(sizeof(MDeformWeight)*(dvert->totweight+1), "defvert_add_to group, new deformWeight");
640         if(dvert->dw) {
641                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight)*dvert->totweight);
642                 MEM_freeN(dvert->dw);
643         }
644         dvert->dw = dw_new;
645         dw_new += dvert->totweight;
646         dw_new->weight = weight;
647         dw_new->def_nr = defgroup;
648         dvert->totweight++;
649 }
650
651
652 /* Removes the given vertex from the vertex group.
653  * WARNING: This function frees the given MDeformWeight, do not use it afterward! */
654 void defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
655 {
656         if (dvert && dw) {
657                 MDeformWeight *dw_new;
658                 int i = dw - dvert->dw;
659
660                 /* Security check! */
661                 if(i < 0 || i >= dvert->totweight) {
662                         return;
663                 }
664
665                 dvert->totweight--;
666                 /* If there are still other deform weights attached to this vert then remove
667                  * this deform weight, and reshuffle the others.
668                  */
669                 if (dvert->totweight) {
670                         dw_new = MEM_mallocN(sizeof(MDeformWeight)*(dvert->totweight), __func__);
671                         if (dvert->dw) {
672 #if 1                   /* since we don't care about order, swap this with the last, save a memcpy */
673                                 if (i != dvert->totweight) {
674                                         dvert->dw[i]= dvert->dw[dvert->totweight];
675                                 }
676                                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight) * dvert->totweight);
677 #else
678                                 memcpy(dw_new, dvert->dw, sizeof(MDeformWeight)*i);
679                                 memcpy(dw_new+i, dvert->dw+i+1, sizeof(MDeformWeight)*(dvert->totweight-i));
680 #endif
681                                 MEM_freeN(dvert->dw);
682                         }
683                         dvert->dw = dw_new;
684                 }
685                 else {
686                         /* If there are no other deform weights left then just remove this one. */
687                         MEM_freeN(dvert->dw);
688                         dvert->dw = NULL;
689                 }
690         }
691 }