~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#include <string.h>
34
34
#include <math.h>
35
35
#include <ctype.h>
 
36
#include <stdlib.h>
 
37
#include <stddef.h>
36
38
 
37
39
#include "MEM_guardedalloc.h"
38
40
 
41
43
 
42
44
#include "BKE_deform.h"
43
45
 
44
 
#include "BLI_blenlib.h"
 
46
#include "BLI_listbase.h"
 
47
#include "BLI_math.h"
 
48
#include "BLI_path_util.h"
 
49
#include "BLI_string.h"
45
50
#include "BLI_utildefines.h"
46
51
 
47
52
 
204
209
        }
205
210
}
206
211
 
207
 
void defvert_normalize_lock(MDeformVert *dvert, const int def_nr_lock)
 
212
void defvert_normalize_lock_single(MDeformVert *dvert, const int def_nr_lock)
208
213
{
209
214
        if (dvert->totweight <= 0) {
210
215
                /* nothing */
211
216
        }
212
217
        else if (dvert->totweight == 1) {
213
 
                dvert->dw[0].weight = 1.0f;
 
218
                if (def_nr_lock != 0) {
 
219
                        dvert->dw[0].weight = 1.0f;
 
220
                }
214
221
        }
215
222
        else {
216
223
                MDeformWeight *dw_lock = NULL;
246
253
        }
247
254
}
248
255
 
 
256
void defvert_normalize_lock_map(MDeformVert *dvert, const char *lock_flags, const int defbase_tot)
 
257
{
 
258
        if (dvert->totweight <= 0) {
 
259
                /* nothing */
 
260
        }
 
261
        else if (dvert->totweight == 1) {
 
262
                if (LIKELY(defbase_tot >= 1) && lock_flags[0]) {
 
263
                        dvert->dw[0].weight = 1.0f;
 
264
                }
 
265
        }
 
266
        else {
 
267
                MDeformWeight *dw;
 
268
                unsigned int i;
 
269
                float tot_weight = 0.0f;
 
270
                float lock_iweight = 0.0f;
 
271
 
 
272
                for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
 
273
                        if ((dw->def_nr < defbase_tot) && (lock_flags[dw->def_nr] == FALSE)) {
 
274
                                tot_weight += dw->weight;
 
275
                        }
 
276
                        else {
 
277
                                /* invert after */
 
278
                                lock_iweight += dw->weight;
 
279
                        }
 
280
                }
 
281
 
 
282
                lock_iweight = max_ff(0.0f, 1.0f - lock_iweight);
 
283
 
 
284
                if (tot_weight > 0.0f) {
 
285
                        /* paranoid, should be 1.0 but in case of float error clamp anyway */
 
286
 
 
287
                        float scalar = (1.0f / tot_weight) * lock_iweight;
 
288
                        for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
 
289
                                if ((dw->def_nr < defbase_tot) && (lock_flags[dw->def_nr] == FALSE)) {
 
290
                                        dw->weight *= scalar;
 
291
 
 
292
                                        /* in case of division errors with very low weights */
 
293
                                        CLAMP(dw->weight, 0.0f, 1.0f);
 
294
                                }
 
295
                        }
 
296
                }
 
297
        }
 
298
}
 
299
 
249
300
void defvert_flip(MDeformVert *dvert, const int *flip_map, const int flip_map_len)
250
301
{
251
302
        MDeformWeight *dw;
252
303
        int i;
253
304
 
254
 
        for (dw = dvert->dw, i = 0; i<dvert->totweight; dw++, i++) {
 
305
        for (dw = dvert->dw, i = 0; i < dvert->totweight; dw++, i++) {
255
306
                if (dw->def_nr < flip_map_len) {
256
307
                        if (flip_map[dw->def_nr] >= 0) {
257
308
                                dw->def_nr = flip_map[dw->def_nr];
287
338
 
288
339
bDeformGroup *defgroup_find_name(Object *ob, const char *name)
289
340
{
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;
 
341
        return BLI_findstring(&ob->defbase, name, offsetof(bDeformGroup, name));
301
342
}
302
343
 
303
344
int defgroup_name_index(Object *ob, const char *name)
304
345
{
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;
 
346
        return (name) ? BLI_findstringindex(&ob->defbase, name, offsetof(bDeformGroup, name)) : -1;
321
347
}
322
348
 
323
349
/* note, must be freed */
381
407
                if (strcmp(name, dg->name)) {
382
408
                        flip_num = defgroup_name_index(ob, name);
383
409
 
384
 
                        if (flip_num >= 0) {
 
410
                        if (flip_num != -1) {
385
411
                                map[defgroup] = flip_num;
386
412
                                map[flip_num] = defgroup;
387
413
                        }
424
450
 
425
451
static int defgroup_unique_check(void *arg, const char *name)
426
452
{
427
 
        struct {Object *ob; void *dg;} *data = arg;
 
453
        struct {Object *ob; void *dg; } *data = arg;
428
454
        return defgroup_find_name_dupe(name, data->dg, data->ob);
429
455
}
430
456
 
431
457
void defgroup_unique_name(bDeformGroup *dg, Object *ob)
432
458
{
433
 
        struct {Object *ob; void *dg;} data;
 
459
        struct {Object *ob; void *dg; } data;
434
460
        data.ob = ob;
435
461
        data.dg = dg;
436
462
 
437
463
        BLI_uniquename_cb(defgroup_unique_check, &data, "Group", '.', dg->name, sizeof(dg->name));
438
464
}
439
465
 
440
 
BLI_INLINE int is_char_sep(const char c)
 
466
static int is_char_sep(const char c)
441
467
{
442
468
        return ELEM4(c, '.', ' ', '-', '_');
443
469
}
444
470
 
 
471
/* based on BLI_split_dirfile() / os.path.splitext(), "a.b.c" -> ("a.b", ".c") */
 
472
 
 
473
void BKE_deform_split_suffix(const char string[MAX_VGROUP_NAME], char body[MAX_VGROUP_NAME], char suf[MAX_VGROUP_NAME])
 
474
{
 
475
        size_t len = BLI_strnlen(string, MAX_VGROUP_NAME);
 
476
        size_t i;
 
477
 
 
478
        body[0] = suf[0] = '\0';
 
479
 
 
480
        for (i = len - 1; i > 1; i--) {
 
481
                if (is_char_sep(string[i])) {
 
482
                        BLI_strncpy(body, string, i + 1);
 
483
                        BLI_strncpy(suf, string + i,  (len + 1) - i);
 
484
                        return;
 
485
                }
 
486
        }
 
487
 
 
488
        BLI_strncpy(body, string, len);
 
489
}
 
490
 
 
491
/* "a.b.c" -> ("a.", "b.c") */
 
492
void BKE_deform_split_prefix(const char string[MAX_VGROUP_NAME], char pre[MAX_VGROUP_NAME], char body[MAX_VGROUP_NAME])
 
493
{
 
494
        size_t len = BLI_strnlen(string, MAX_VGROUP_NAME);
 
495
        size_t i;
 
496
 
 
497
        body[0] = pre[0] = '\0';
 
498
 
 
499
        for (i = 1; i < len; i++) {
 
500
                if (is_char_sep(string[i])) {
 
501
                        i++;
 
502
                        BLI_strncpy(pre, string, i + 1);
 
503
                        BLI_strncpy(body, string + i, (len + 1) - i);
 
504
                        return;
 
505
                }
 
506
        }
 
507
 
 
508
        BLI_strncpy(body, string, len);
 
509
}
 
510
 
445
511
/* finds the best possible flipped name. For renaming; check for unique names afterwards */
446
512
/* if strip_number: removes number extensions
447
513
 * note: don't use sizeof() for 'name' or 'from_name' */
466
532
        /* We first check the case with a .### extension, let's find the last period */
467
533
        if (isdigit(name[len - 1])) {
468
534
                index = strrchr(name, '.'); // last occurrence
469
 
                if (index && isdigit(index[1]) ) { // doesnt handle case bone.1abc2 correct..., whatever!
 
535
                if (index && isdigit(index[1])) { // doesnt handle case bone.1abc2 correct..., whatever!
470
536
                        if (strip_number == 0) {
471
537
                                BLI_strncpy(number, index, sizeof(number));
472
538
                        }
478
544
        BLI_strncpy(prefix, name, sizeof(prefix));
479
545
 
480
546
        /* first case; separator . - _ with extensions r R l L  */
481
 
        if (is_char_sep(name[len - 2]) ) {
482
 
                switch(name[len - 1]) {
 
547
        if (is_char_sep(name[len - 2])) {
 
548
                switch (name[len - 1]) {
483
549
                        case 'l':
484
550
                                prefix[len - 1] = 0;
485
551
                                strcpy(replace, "r");
498
564
                                break;
499
565
                }
500
566
        }
501
 
        /* case; beginning with r R l L , with separator after it */
502
 
        else if (is_char_sep(name[1]) ) {
503
 
                switch(name[0]) {
 
567
        /* case; beginning with r R l L, with separator after it */
 
568
        else if (is_char_sep(name[1])) {
 
569
                switch (name[0]) {
504
570
                        case 'l':
505
571
                                strcpy(replace, "r");
506
572
                                BLI_strncpy(suffix, name + 1, sizeof(suffix));
555
621
                }
556
622
        }
557
623
 
558
 
        BLI_snprintf (name, MAX_VGROUP_NAME, "%s%s%s%s", prefix, replace, suffix, number);
 
624
        BLI_snprintf(name, MAX_VGROUP_NAME, "%s%s%s%s", prefix, replace, suffix, number);
559
625
}
560
626
 
561
627
float defvert_find_weight(const struct MDeformVert *dvert, const int defgroup)
670
736
                if (dvert->totweight) {
671
737
                        dw_new = MEM_mallocN(sizeof(MDeformWeight) * (dvert->totweight), __func__);
672
738
                        if (dvert->dw) {
673
 
#if 1                   /* since we don't care about order, swap this with the last, save a memcpy */
 
739
#if 1           /* since we don't care about order, swap this with the last, save a memcpy */
674
740
                                if (i != dvert->totweight) {
675
741
                                        dvert->dw[i] = dvert->dw[dvert->totweight];
676
742
                                }
700
766
 
701
767
        dvert->totweight = 0;
702
768
}
 
769
 
 
770
/**
 
771
 * \return The first group index shared by both deform verts
 
772
 * or -1 if none are found.
 
773
 */
 
774
int defvert_find_shared(const MDeformVert *dvert_a, const MDeformVert *dvert_b)
 
775
{
 
776
        if (dvert_a->totweight && dvert_b->totweight) {
 
777
                MDeformWeight *dw = dvert_a->dw;
 
778
                unsigned int i;
 
779
 
 
780
                for (i = dvert_a->totweight; i != 0; i--, dw++) {
 
781
                        if (dw->weight > 0.0f && defvert_find_weight(dvert_b, dw->def_nr) > 0.0f) {
 
782
                                return dw->def_nr;
 
783
                        }
 
784
                }
 
785
        }
 
786
 
 
787
        return -1;
 
788
}
 
789
 
 
790
/* -------------------------------------------------------------------- */
 
791
/* Defvert Array functions */
 
792
 
 
793
void BKE_defvert_array_copy(MDeformVert *dst, const MDeformVert *src, int copycount)
 
794
{
 
795
        /* Assumes dst is already set up */
 
796
        int i;
 
797
 
 
798
        if (!src || !dst)
 
799
                return;
 
800
 
 
801
        memcpy(dst, src, copycount * sizeof(MDeformVert));
 
802
 
 
803
        for (i = 0; i < copycount; i++) {
 
804
                if (src[i].dw) {
 
805
                        dst[i].dw = MEM_mallocN(sizeof(MDeformWeight) * src[i].totweight, "copy_deformWeight");
 
806
                        memcpy(dst[i].dw, src[i].dw, sizeof(MDeformWeight) * src[i].totweight);
 
807
                }
 
808
        }
 
809
 
 
810
}
 
811
 
 
812
void BKE_defvert_array_free_elems(MDeformVert *dvert, int totvert)
 
813
{
 
814
        /* Instead of freeing the verts directly,
 
815
         * call this function to delete any special
 
816
         * vert data */
 
817
        int i;
 
818
 
 
819
        if (!dvert)
 
820
                return;
 
821
 
 
822
        /* Free any special data from the verts */
 
823
        for (i = 0; i < totvert; i++) {
 
824
                if (dvert[i].dw) MEM_freeN(dvert[i].dw);
 
825
        }
 
826
}
 
827
 
 
828
void BKE_defvert_array_free(MDeformVert *dvert, int totvert)
 
829
{
 
830
        /* Instead of freeing the verts directly,
 
831
         * call this function to delete any special
 
832
         * vert data */
 
833
        if (!dvert)
 
834
                return;
 
835
 
 
836
        /* Free any special data from the verts */
 
837
        BKE_defvert_array_free_elems(dvert, totvert);
 
838
 
 
839
        MEM_freeN(dvert);
 
840
}