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)
209
214
if (dvert->totweight <= 0) {
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;
216
223
MDeformWeight *dw_lock = NULL;
256
void defvert_normalize_lock_map(MDeformVert *dvert, const char *lock_flags, const int defbase_tot)
258
if (dvert->totweight <= 0) {
261
else if (dvert->totweight == 1) {
262
if (LIKELY(defbase_tot >= 1) && lock_flags[0]) {
263
dvert->dw[0].weight = 1.0f;
269
float tot_weight = 0.0f;
270
float lock_iweight = 0.0f;
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;
278
lock_iweight += dw->weight;
282
lock_iweight = max_ff(0.0f, 1.0f - lock_iweight);
284
if (tot_weight > 0.0f) {
285
/* paranoid, should be 1.0 but in case of float error clamp anyway */
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;
292
/* in case of division errors with very low weights */
293
CLAMP(dw->weight, 0.0f, 1.0f);
249
300
void defvert_flip(MDeformVert *dvert, const int *flip_map, const int flip_map_len)
251
302
MDeformWeight *dw;
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];
288
339
bDeformGroup *defgroup_find_name(Object *ob, const char *name)
290
/* return a pointer to the deform group with this name
291
* or return NULL otherwise.
293
bDeformGroup *curdef;
295
for (curdef = ob->defbase.first; curdef; curdef = curdef->next) {
296
if (!strcmp(curdef->name, name)) {
341
return BLI_findstring(&ob->defbase, name, offsetof(bDeformGroup, name));
303
344
int defgroup_name_index(Object *ob, const char *name)
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.
310
bDeformGroup *curdef;
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))
346
return (name) ? BLI_findstringindex(&ob->defbase, name, offsetof(bDeformGroup, name)) : -1;
323
349
/* note, must be freed */
425
451
static int defgroup_unique_check(void *arg, const char *name)
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);
431
457
void defgroup_unique_name(bDeformGroup *dg, Object *ob)
433
struct {Object *ob; void *dg;} data;
459
struct {Object *ob; void *dg; } data;
437
463
BLI_uniquename_cb(defgroup_unique_check, &data, "Group", '.', dg->name, sizeof(dg->name));
440
BLI_INLINE int is_char_sep(const char c)
466
static int is_char_sep(const char c)
442
468
return ELEM4(c, '.', ' ', '-', '_');
471
/* based on BLI_split_dirfile() / os.path.splitext(), "a.b.c" -> ("a.b", ".c") */
473
void BKE_deform_split_suffix(const char string[MAX_VGROUP_NAME], char body[MAX_VGROUP_NAME], char suf[MAX_VGROUP_NAME])
475
size_t len = BLI_strnlen(string, MAX_VGROUP_NAME);
478
body[0] = suf[0] = '\0';
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);
488
BLI_strncpy(body, string, len);
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])
494
size_t len = BLI_strnlen(string, MAX_VGROUP_NAME);
497
body[0] = pre[0] = '\0';
499
for (i = 1; i < len; i++) {
500
if (is_char_sep(string[i])) {
502
BLI_strncpy(pre, string, i + 1);
503
BLI_strncpy(body, string + i, (len + 1) - i);
508
BLI_strncpy(body, string, len);
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));
501
/* case; beginning with r R l L , with separator after it */
502
else if (is_char_sep(name[1]) ) {
567
/* case; beginning with r R l L, with separator after it */
568
else if (is_char_sep(name[1])) {
505
571
strcpy(replace, "r");
506
572
BLI_strncpy(suffix, name + 1, sizeof(suffix));
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);
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__);
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];
701
767
dvert->totweight = 0;
771
* \return The first group index shared by both deform verts
772
* or -1 if none are found.
774
int defvert_find_shared(const MDeformVert *dvert_a, const MDeformVert *dvert_b)
776
if (dvert_a->totweight && dvert_b->totweight) {
777
MDeformWeight *dw = dvert_a->dw;
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) {
790
/* -------------------------------------------------------------------- */
791
/* Defvert Array functions */
793
void BKE_defvert_array_copy(MDeformVert *dst, const MDeformVert *src, int copycount)
795
/* Assumes dst is already set up */
801
memcpy(dst, src, copycount * sizeof(MDeformVert));
803
for (i = 0; i < copycount; i++) {
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);
812
void BKE_defvert_array_free_elems(MDeformVert *dvert, int totvert)
814
/* Instead of freeing the verts directly,
815
* call this function to delete any special
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);
828
void BKE_defvert_array_free(MDeformVert *dvert, int totvert)
830
/* Instead of freeing the verts directly,
831
* call this function to delete any special
836
/* Free any special data from the verts */
837
BKE_defvert_array_free_elems(dvert, totvert);